Search code examples
godotgodot4

When can "shrink" size flags be used in combination with "expand" in Godot's container system?


Godot's documentation of its SizeFlags mentions for the three "shrink-like" flags SIZE_SHRINK_BEGIN, SIZE_SHRINK_CENTER, and SIZE_SHRINK_END:

It is mutually exclusive with SIZE_FILL and other shrink size flags, but can be used with SIZE_EXPAND in some containers.

Combining "shrink" with "expand" sounds counter-intuitive.

What would be an example of such a use case, i.e., which containers do allow this combination and is there a common semantic what "shrink and expand" should mean?


Solution

  • A container that is trying to fill some allotted space (that would be VBoxContainer or HBoxContainer) will see if it can fit the children at their minimum size. If there is space left, it might be distributed among the children.

    That is controlled by SIZE_EXPAND and stretch_ratio: The children Controls with SIZE_EXPAND will get some of the extra space. They take as much of the total available extra space as is their stretch_ratio of the sum of all the stretch_ratio of it and siblings with SIZE_EXPAND.

    Once we have the space available for a Control then it either takes all that space (if it has SIZE_FILL) or it might be placed at the end of the space (SIZE_SHRINK_END), centered on the space (SIZE_SHRINK_CENTER) or at the start (SIZE_SHRINK_BEGIN, i.e. none of the others).

    Thus using SIZE_EXPAND but not SIZE_FILL (i.e. combining expand and shrink) will result in giving more space to the Control but keeping its minimum size.

    For example, if you have a HBoxContainer with multiple children with SIZE_EXPAND and SIZE_SHRINK_CENTER, they will be distributed horizontally with large gaps while keeping their minimum size. On the other hand, if you used SIZE_EXPAND and SIZE_FILL they would be as taking as much space as possible with minimum gaps between them. If you didn't use SIZE_EXPAND, then they would be at their minimum size and packed together with minimum gaps between them.