Skip to content

Commit

Permalink
Add missing Widget::getGlobalFont.
Browse files Browse the repository at this point in the history
[Example] Use SdlTrueTypeFont
  • Loading branch information
Jarod42 committed Dec 8, 2024
1 parent 8eed837 commit 3d503af
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
Binary file added examples/dactylographe.ttf
Binary file not shown.
37 changes: 31 additions & 6 deletions examples/widgets_example.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<gcn::SDLTrueTypeFont>("dactylographe.ttf", 12);
#endif
// Load the image font.
font = std::make_unique<gcn::ImageFont>(
imageFont = std::make_unique<gcn::ImageFont>(
"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<gcn::Container>();
// Set the dimension of the top container to match the screen.
top->setDimension(gcn::Rectangle(0, 0, width, height));
Expand Down Expand Up @@ -82,7 +91,8 @@ namespace WidgetsExample
checkBox1 = std::make_unique<gcn::CheckBox>("Checkbox 1");
checkBox2 = std::make_unique<gcn::CheckBox>("Checkbox 2");

toggleButton = std::make_unique<gcn::ToggleButton>("Toggle button");
toggleButton = std::make_unique<gcn::ToggleButton>("Toggle font");
toggleButton->addActionListener(this);

radioButton1 = std::make_unique<gcn::RadioButton>("Radio Button 1", "radiogroup", true);
radioButton2 = std::make_unique<gcn::RadioButton>("Radio Button 2", "radiogroup");
Expand Down Expand Up @@ -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())
Expand All @@ -195,8 +218,10 @@ namespace WidgetsExample
}

private:
std::unique_ptr<gcn::ImageFont> font; // A font

std::unique_ptr<gcn::Font> imageFont; // A font
#if USE_SDL2_TTF
std::unique_ptr<gcn::Font> trueFont; // A font
#endif
/*
* All of the widgets
*/
Expand Down
9 changes: 9 additions & 0 deletions include/guisan/widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
5 changes: 5 additions & 0 deletions src/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,11 @@ namespace gcn
}
}

Font* Widget::getGlobalFont()
{
return mGlobalFont;
}

void Widget::setWidgetsBaseColor(Color color)
{
for (Widget* w : mWidgetInstances)
Expand Down

0 comments on commit 3d503af

Please sign in to comment.