texus / TGUI

Cross-platform modern c++ GUI

Home Page:https://tgui.eu

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use of Theme in v0.8

eduherminio opened this issue · comments

Hi!

I'm updating TGUI version from some old code and I've found the following lines:

// Providing tgui::Button::Ptr load_button being a member of Panel
void Panel::Init()
{
     tgui::Theme::Ptr theme = std::make_shared<tgui::Theme>("../resources/Black.txt");
     load_button = tgui::Button::Ptr(theme->load("button"));
     // ...
}

Just to make sure, is this the correct way of achieving the same in v0.8?

// Providing tgui::Button::Ptr load_button being a member of Panel
void Panel::Init()
{
     tgui::Theme theme("../resources/Black.txt");
     load_button = tgui::Button::Ptr();
     load_button->setRenderer(theme.getRenderer("button"));
     // ...
}

Don't I have to worry about theme scope any more (i.e. saving it in a shared_ptr)?
Thanks in advance,

Eduardo

tgui::Button::Ptr is just an std::shared_ptr<Button> now, so default initializing it like you do gives you a nullptr instead of an object. You should instead create the widget like this:

load_button = tgui::Button::create();

The Theme class is just a collection of renderers, so you can indeed just let it go out of scope.

If most widgets use the same theme then it may be beneficial to set a default theme:

tgui::Theme theme{"../resources/Black.txt"};
tgui::Theme::setDefault(&theme);

After doing that anywhere in your code, you can remove the load_button->setRenderer(theme.getRenderer("button")); line, as long as the name of the renderer equals the widget type (which is the case for Black.txt, but may not be the case if you have a custom theme where you have a section called e.g. "blue_button").

You will have to keep the theme variable alive in this case. When it gets destroyed, the default theme will automatically be reset to the default White theme.

Thanks very much for your quick answer and your clarifications!