Trouble with getAbsolutePosition()

Started by Nafffen, 28 March 2023, 10:55:15

Nafffen

I'm having trouble with getAbsolutePosition() which returns a value I don't expect
My tree widget is something like:
Gui
|-->group1
       |-->VehicleManager (custom widget)
              |-->Group (= "panels")
I use panels->getAbsolutePosition() but it returns the pos in the VehicleManager reference, not in screen reference.

Capture.PNG

I used gui-builder to load the group (can it be the source of the problem ?):
    auto panels = tgui::Group::create();
    panels->setPosition(0, tgui::bindBottom(btnWarehouse)+m_spaceY);
    panels->setSize("100%", "100%"-tgui::bindHeight(btnWarehouse)-m_spaceY*2);
    panels->loadWidgetsFromFile("data/forms/vehicleManager.fc");

    m_container->add(panels,"panels");

data/forms/vehicleManager.fc only contains two scrollable panels

Did I missunderstand something ?

texus

#1
Does VehicleManager inherit from SubwidgetContainer?
If so then this is a bug and I will need to patch SubwidgetContainer.

Edit: this seems harder to fix than expected, I don't immediately see what I could change in SubwidgetContainer to make getAbsolutePosition work. What is the reason you need this function?

Edit 2: I think VehicleManager should inherit from Group instead of SubwidgetContainer (assuming this is what is happening). The SubwidgetContainer class exists so that you can create a widget that internally works like a container, but otherwise pretends to be a normal non-container widget to the rest of the gui. If VehicleManager should work like a container (which you seem to want with your getAbsolutePosition call), then you should probably inherit from a Container-based widget like Group.

Nafffen

QuoteDoes VehicleManager inherit from SubwidgetContainer?
Yes, sry I should have included that in the first post

I inherit now from Group and it works as wanted. I needed to call getAbsolutePosition to add a widget in a branch of the widget tree in the same global pos as another widget in another branch of the widget tree

Do you plan to correct this behavior ? Or maybe forbid user to use the function with this class

Thank you !

texus

QuoteDo you plan to correct this behavior ? Or maybe forbid user to use the function with this class
I'm not sure how I can. The getAbsolutePosition call never actually reaches the SubwidgetContainer class (it ends in the m_container member which has no idea it is part of SubwidgetContainer), so I can't add code there to make an exception.

Widgets that are placed into the SubwidgetContainer aren't actually part of the gui, they are just part of your custom widget. While my first reaction was that the getAbsolutePosition might be buggy, the behavior is actually by design: the SubwidgetContainer really is the root of everything inside it. You should think of a SubwidgetContainer as a canvas on which you render: the widgets added to m_container are drawn to the canvas (without knowledge of the gui), and the canvas image is later drawn to the gui (without knowledge of how the image was created).

The only thing that I know I can do is better document this. While the current class description of "consist of subwidgets that act together as if they are a single widget" is accurate, it doesn't really mention any details about the consequences and side-effects of this design. The tutorial about custom widgets might also be improved to explain in which case you should or shouldn't use SubwidgetContainer.

Nafffen

QuoteWidgets that are placed into the SubwidgetContainer aren't actually part of the gui, they are just part of your custom widget. While my first reaction was that the getAbsolutePosition might be buggy, the behavior is actually by design: the SubwidgetContainer really is the root of everything inside it. You should think of a SubwidgetContainer as a canvas on which you render: the widgets added to m_container are drawn to the canvas (without knowledge of the gui), and the canvas image is later drawn to the gui (without knowledge of how the image was created).
Oh ok I understand it better now !
Therefor what are the benefits to inherit from SubwidgetContainer instead of Group ?

texus

QuoteTherefor what are the benefits to inherit from SubwidgetContainer instead of Group ?
The only benefit would be to not expose the container interface.
For example someone wrote a SpinControl widget which consists of an EditBox and a SpinButton. If SpinControl would inherit from Group, it would have functions such as "add" and "remove" (which you would never use). By inheriting from SubwidgetContainer, it doesn't have these functions.