Can you get theme properties of "subclass"

Started by Grzyboleusz, 17 May 2017, 17:28:25

Grzyboleusz

Hello.

I would like to get the "TextColor" property of "EditBox.Invalid" and use it in editbox widget after i check if it's valid. Tried theme->getProperty(... , "TextColor") with "EditBox.Invalid", "Invalid.Editbox", with lowercase but none of them works. Is there some other way to get that or you can' get "subclasses" properties at all with this method? It would be great to do it this way cause I'd like to keep all style data in same file. Maybe I have to reload the widget with "Invalid" class?

Here's my theme file:

Code (css) Select

EditBox {
    BackgroundColorNormal       : rgba(69, 69, 69, 255);
    BackgroundColorHover        : rgba(69, 69, 69, 255);
    TextColor                   : rgba(190, 190, 190, 255);
    SelectedTextColor           : rgba(255, 255, 255, 255);
    SelectedTextBackgroundColor : rgba(255, 100, 0, 255);
    DefaultTextColor            : rgba(190, 190, 190, 255);
    CaretColor                  : rgba(190, 190, 190, 255);
    Padding                     : (3, 0, 3, 0);
}

EditBox.Invalid {
    BackgroundColorNormal       : rgba(69, 69, 69, 255);
    BackgroundColorHover        : rgba(69, 69, 69, 255);
    TextColor                   : rgba(192, 26, 26, 255);
    SelectedTextColor           : rgba(255, 255, 255, 255);
    SelectedTextBackgroundColor : rgba(255, 100, 0, 255);
    DefaultTextColor            : rgba(190, 190, 190, 255);
    CaretColor                  : rgba(190, 190, 190, 255);
    Padding                     : (3, 0, 3, 0);
}


texus

This is indeed a bit tricky to do with the way the Theme class works in 0.7. Reloading the look of a widget without recreating the widget is something that is much easier to do in 0.8-dev, but I can't fully recommend that version yet.

You should be able to get the right property by calling theme->getProperty("Invalid", "TextColor"). The "EditBox." in front of the name is just so that the theme class knows what type of widget to construct when you call the theme->load(...). The first "EditBox" section is in fact implicitly renamed to "EditBox.EditBox", which is why the first one can be loaded with the name "EditBox" while the second one has name "Invalid".

Grzyboleusz

Tried theme->getProperty("Invalid", "TextColor"). Still returns empty string, maybe bug. Checked couple of times for typos. Looks like I'll have to reload widget.
Thanks for quick answer.  :)

texus

I haven't tried it, but based on my Theme class code I guess the EditBox.Invalid is never loaded.

Try adding the following somewhere after you create the theme:
Code (cpp) Select
theme->load("Invalid");

You don't have to do anything with the return value of that function call, it is just needed so that the Theme class reads the EditBox.Invalid section.

Grzyboleusz

It worked. For some reason I thought theme loads all classes when it's constructed. Thanks again.  ;D

texus

In a design that makes some sense (i.e. in tgui 0.8-dev), the Theme class stores all properties for all sections when the theme file is read. But the class in 0.7 was more like a prototype and there were several bad design choices related to the theme and renderers. But issues like this one of course help me with making decisions on how to improve it in future versions.

If you have multiple of these places where you change the look of the widget dynamically then maybe you would benefit from using 0.8-dev (changing the look of the edit box would be as simple as `editBox->setRenderer(theme.getRenderer("EditBox.Invalid"));`). But if it is just for one or two places in your code then it is probably not worth the effort to upgrade.