Setting just one size coordinate for Panel
« on: 22 June 2018, 11:29:09 »
I want to set the x size of a panel but allow it’s y size to grow as you add widgets to it.
I thought that I could just use getSize() when all the widgets were added and then feed the output back into setSize() with a decreased x component but the size is reported as 0, 0 all the time so I can’t get the y value after the widgets have been added.
However if I use setSize( 0, 0 ) the panel actually get’s set to 0 size so I guess there’s some sort of flag being set after I use setSize(). I’ve looked at the code but can’t figure out how it how it all works.
Is it possible to get the y size after adding widgets somehow? I’m using 0.8.

*

texus

  • *****
  • 1236
    • View Profile
    • Texus's Blog
Re: Setting just one size coordinate for Panel
« Reply #1 on: 22 June 2018, 18:30:35 »
Panel is initialized with size {"100%, "100%"}, which will only be (0,0) if the parent of the panel has size (0,0).
My guess is that you haven't added the panel to its parent yet (by doing something like "gui.add(panel)") when you are calling getSize. If you don't call setSize then the panel will automatically change its size when you add it to its parent (since it detects a parent size change at that moment). When you call setSize(0,0) then you are giving it a fixed size instead of binding its parent size which is why it will remain (0,0) even after adding it to its parent.

Panels can't auto-size based on its content, you can't have its size depend on the widgets inside it, you can only make it depend on its parent (e.g. panel->setSize({50, "100%"}) will set a fixed width and variable height).

You can calculate the size needed by the panel by looping over all its widgets (accessible with panel->getWidgets()) and calculating the maximum value of "widget->getPosition() + widget->getFullSize()". (I use that calculation myself in ScrollbablePanel but just realized that this may be incorrect and you probably also have to add widget->getWidgetOffset() to be fully correct for all types of widgets). You would have to keep track of when you add/remove widgets to the panel yourself and update the size of the panel manually.

Re: Setting just one size coordinate for Panel
« Reply #2 on: 23 June 2018, 16:01:12 »
Thanks a million texus. You're right I didn't realize I called getSize() on the panel before it was added to the gui.
Iterating through the widgets sounds like a good solution for what I want.