Reputation: 368
I expect a container in godot 4 to have its minimum size reflecting the minimum size of its children. For instance a VBoxContainer having its minimum y size the sum of the custom minimum size of its children and its minimum x size the max of the custom minimum size of its children.
But with a simple scene with the following structure:
+ Control Node
|- VBoxContainer
|- Label (with custom_minimum_size set to 300x100 px)
It is possible to make the window shrink below the custom minimum size of the label. The only fix I found is to programmatically set the minimum size of the Control Node which eliminates the advantage of using containers.
It looks to me that I miss something there.
Note: this is the same with the following structure:
+ VBoxContainer
|- Label (with custom_minimum_size set to 300x100 px)
The programmatic solution I used is as follow: attach a script to the scene with the following function in it
func setup_sizes():
# This function must be called whenever a child in the scene
# changes its minimum size.
var min_size = $"VBoxContainer".get_minimum_size()
# Set the window size to match the custom minimum size
DisplayServer.window_set_min_size(min_size)
func _ready():
setup_sizes()
The problem with this solution is that if a new child is added with its own custom minimum size (or if one of the children custom minimum size is modified) the setup_sizes
function has to be called.
A solution could be to call setup_sizes
each time the window size is changed by connecting setup_sizes
to the signal emitted when the window changes in the _ready
function
func _ready():
setup_sizes()
get_tree().get_root().size_changed.connect(setup_sizes)
The downside of this approach is that it may traverse all the scene children to compute the minimum size (the function get_minimum_size
for BoxContainer in box_container.cpp does exactly that).
It would be more efficient to have a signal emitted when the minimum size changes and be able to get it to update the window size and have a specific container doing exactly that.
Such container could make automatically the link between the DisplayServer window size and the scene size.
Upvotes: 1
Views: 579
Reputation: 21
I think the problem is that you're resizing Control node, not the container. If this is the case, try doing that in Editor and see what happens to the size of the VBox: I bet it will stop at 300x100 and not shrink anymore, while the parent Control may shrink further.
This is not a bug, because VBox is layed out with anchors (because VBox's parent is a Control node, not a container), and anchors don't give you guarantees about min size. If you want to achieve behavior like "I resize an ancestor node, and it must respect min size of its descendants of whatever level" - use containers all the way through. E.g. replace Control node with PanelContainer, CenterContainer, another VBoxContainer, etc.
If you want really a Window, it has wrap_controls
property which you may set to true
: in this case window won't shrink past the children's min size.
Upvotes: 0