WidgetTypes
« on: 02 April 2015, 22:14:19 »
Hi Texus,

I have a panel that has labels and the gui that have labels. Depending on what the user clicks, the content of the labels may change. Now rather than try to figure out what label changes and update the text, I've decided at the beginning of each menu call, to remove all label type widgets from the gui and any panels within the gui. Then re-add them with potentially new text.

So essentially it is pretty simple

- if widget type = label then remove widget
- else if widget type = panel then
       - get all widgets assigned to the panel
       - if any panel widgets are label then remove them from the panel


I've written the below which I think will do it? LabelList is a vector of labels that I use to set the position, text etc of the label. I tried just clearing this vector but the gui and panels always seem to retain the widgets. Unsure if there is an easier way to do this?

void guiEngine::clearLabelWidgets()
{
        LabelList.clear();
       
        vector<Widget::Ptr> widgetList = gui.getWidgets();
       
        for (int i = 0; i < widgetList.size(); i++)
        {
                if (widgetList[i]->getWidgetType() == "Label")
                {
                        gui.remove(widgetList[i]);
                }
                else if (widgetList[i]->getWidgetType() == "Panel")
                {
                        vector<Widget::Ptr> panelWidgetList = widgetList[i]->getWidgets();
                       
                        for (int j = 0; j < panelWidgetList.size(); j++)
                        {
                                if (widgetList[i]->getWidgetType() == "Label")
                                {
                                        widgetList[i]->remove(panelWidgetList[j]);
                                }
                        }
                }
        }
}
 

Anyway, the above doesn't compile as "Label" isn't a valid WidgetType. I've looked at the documentation but it doesn't give a list of each valid i.e.

https://tgui.eu/documentation/v0.6/namespacetgui.html#a6f721be5dfcacf4e5b73b8d3aef75b14

I looked in the source files of widget.cpp but I couldn't see them in there either.

*

texus

  • *****
  • 1163
    • View Profile
    • Texus's Blog
Re: WidgetTypes
« Reply #1 on: 02 April 2015, 22:32:36 »
It seems like the documentation of WidgetType is indeed lacking.
It isn't a string, it's an enum: https://tgui.eu/documentation/v0.6/Global_8hpp_source.html#l00080

So the code should be
Code: [Select]
if (widgetList[i]->getWidgetType() == tgui::Type_Label)

*

texus

  • *****
  • 1163
    • View Profile
    • Texus's Blog
Re: WidgetTypes
« Reply #2 on: 02 April 2015, 22:47:15 »
Quote
vector<Widget::Ptr> widgetList = gui.getWidgets();
I was going to add that by doing the above you were copying that list and that you could take a reference to it instead and change the list directly. However I realize that although this is possible, it is a serious bug. If you do change the list directly then tgui might crash because just removing an element from that list is not enough to properly remove a widget. So I'll make a change soon that will prevent a reference and forces you to either copy the list like you do now, or take a constant reference "const vector<Widget::Ptr>& widgetList = ..." to avoid unnecessary copy.

Quote
I've written the below which I think will do it?
The code will not work for panels inside panels. In order to support that the function should be made recursive: remove labels and make a recursive call for each panel. Nevertheless it will probably work fine in your situation.

I tend to always just reload the whole screen when changing game state (possible create the screens up front and just swap the visible panel), but there is no right way to do it. You should probably do whatever is easier for your situations.

Re: WidgetTypes
« Reply #3 on: 05 April 2015, 07:47:15 »
Thanks Texus - I didn't think to look in globals.cpp......I hate using windows as I don't have grep. I should probably install cygwin. :)