| Reply From: | archeron |
You can place a text label (or button, or anything, really) centered horizontally in another container, for example a PanelContainer, by doing the following:
- Make sure the label’s Container has size flags on “Expand”, the left anchor set to 0 and the right anchor set to 1. This assures that the Container that contains the Label will take up all the horizontal space it can.
- Now set the label’s size flags: Disable horizontal “Fill” and enable horizontal “Expand”, and “Shrink Center”.
You don’t need to touch the label’s margins; these will be auto-calculated by it’s container.
The thing to realize is that when you place something in a container, you lose much of the control you get via the position, rect size and margin properties (the only thing that stays under your full control is the minimum size, AFAIK). For controls placed in containers, what really matters for layouting is their size flags.
To answer your question 2, I’d add an instanced scene that contains control nodes that I expect to be layouted correctly as a child of a Control container; this way, the layouting of the instanced scene will just work inside the container. If you want the instanced scene to expand to the size of the viewport, you make sure that the container you instance it in has that size (by setting anchors and expand flags).
As to your question 3, I’m not sure. I imagine it’s for when you want to place “naked” controls without any containers doing the layouting for you. Possibly “position” and “margin” are just two views into data that’s connected at a lower level (kinda like a Rect2 has an “end” property but that depends on the “position” and “size” properties).
Re 1: That’s why I said “or anything, really” in my first sentence. You can of course replace the Label by another Container, in which you then place the Label. That should work fine, and you can set the inner container’s minimum size if you want to control that.
Re 2: That might be a problem. One way to solve this might be to listen to the “resized” signal and set the size manually, then.
archeron | 2021-03-26 11:29
Re: 1. if I do this, the PanelContainer will span the whole horizontal area, whereas I’d Iike it to wrap the label, like in the screenshot.
Re: 2: the problem is, the top_node (which is meant to be a parent) is not a Control, so I can’t set the anchors there
sygi | 2021-03-26 11:49