diff --git a/examples/dactylographe.ttf b/examples/dactylographe.ttf new file mode 100644 index 0000000..e795dc3 Binary files /dev/null and b/examples/dactylographe.ttf differ diff --git a/examples/widgets_example.hpp b/examples/widgets_example.hpp index 4c539f0..d082035 100644 --- a/examples/widgets_example.hpp +++ b/examples/widgets_example.hpp @@ -12,12 +12,21 @@ namespace WidgetsExample MainContainer(gcn::Gui& gui, int width, int height) : demoListModel{{"zero", "one", "two", "three", "four"}} { +#if USE_SDL2_TTF + TTF_Init(); + + trueFont = std::make_unique("dactylographe.ttf", 12); +#endif // Load the image font. - font = std::make_unique( + imageFont = std::make_unique( "fixedfont.bmp", " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); - // The global font is static and must be set. - gcn::Widget::setGlobalFont(font.get()); + // The global font is static and must be set. +#if USE_SDL2_TTF + gcn::Widget::setGlobalFont(trueFont.get()); +#else + gcn::Widget::setGlobalFont(imageFont.get()); +#endif top = std::make_unique(); // Set the dimension of the top container to match the screen. top->setDimension(gcn::Rectangle(0, 0, width, height)); @@ -82,7 +91,8 @@ namespace WidgetsExample checkBox1 = std::make_unique("Checkbox 1"); checkBox2 = std::make_unique("Checkbox 2"); - toggleButton = std::make_unique("Toggle button"); + toggleButton = std::make_unique("Toggle font"); + toggleButton->addActionListener(this); radioButton1 = std::make_unique("Radio Button 1", "radiogroup", true); radioButton2 = std::make_unique("Radio Button 2", "radiogroup"); @@ -178,6 +188,19 @@ namespace WidgetsExample label->adjustSize(); } } + else if (actionEvent.getSource() == toggleButton.get()) + { + if (gcn::Widget::getGlobalFont() == imageFont.get()) + { +#if USE_SDL2_TTF + gcn::Widget::setGlobalFont(trueFont.get()); +#endif + } + else + { + gcn::Widget::setGlobalFont(imageFont.get()); + } + } else if (actionEvent.getSource() == messageBox.get()) { switch (messageBox->getClickedButton()) @@ -195,8 +218,10 @@ namespace WidgetsExample } private: - std::unique_ptr font; // A font - + std::unique_ptr imageFont; // A font +#if USE_SDL2_TTF + std::unique_ptr trueFont; // A font +#endif /* * All of the widgets */ diff --git a/include/guisan/widget.hpp b/include/guisan/widget.hpp index 1311906..0e0e933 100644 --- a/include/guisan/widget.hpp +++ b/include/guisan/widget.hpp @@ -721,6 +721,15 @@ namespace gcn */ static void setGlobalFont(Font* font); + /** + * Gets the global font to be used by default for all widgets. + * + * @return font The global font. + * @see setGlobalFont + * @since 1.2.0 + */ + static Font* getGlobalFont(); + /** * Set the base color for all widgets * diff --git a/src/widget.cpp b/src/widget.cpp index 7b5c329..55785af 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -513,6 +513,11 @@ namespace gcn } } + Font* Widget::getGlobalFont() + { + return mGlobalFont; + } + void Widget::setWidgetsBaseColor(Color color) { for (Widget* w : mWidgetInstances)