Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions doc/classes/SizeContainer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="SizeContainer" inherits="Container" keywords="size" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
A container that supports a maximum size for itself.
</brief_description>
<description>
[SizeContainer] allows setting a maximum size for itself. When the container's size exceeds the defined maximum size, it will not grow any larger, effectively capping its dimensions. This is useful for creating UI elements that should not exceed a certain size regardless of their content or parent container's size.
To control the [SizeContainer]'s maximum size, use the [code skip-lint]max_size[/code] property as shown below.
[codeblocks]
[gdscript]
# This code sample assumes the current script is extending SizeContainer.
var max_size_value = Vector2(400, 300)
set_max_size(max_size_value)
[/gdscript]
[csharp]
// This code sample assumes the current script is extending SizeContainer.
Vector2 maxSizeValue = new Vector2(400, 300);
SetMaxSize(maxSizeValue);
[/csharp]
[/codeblocks]
</description>
<tutorials>
<link title="Using Containers">$DOCS_URL/tutorials/ui/gui_containers.html</link>
</tutorials>
<members>
<member name="clip_contents" type="bool" setter="set_clip_contents" getter="is_clipping_contents" overrides="Control" default="true" />
<member name="maximum_size" type="Vector2" setter="set_maximum_size" getter="get_maximum_size" default="Vector2(0, 0)">
If greater than [code]0[/code] on either axis, the container will expand on that axis to fit its children, up to the specified maximum size.
</member>
</members>
</class>
1 change: 1 addition & 0 deletions editor/icons/SizeContainer.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions scene/gui/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,10 @@ Size2 Control::get_combined_minimum_size() const {
return data.minimum_size_cache;
}

Size2 Control::get_combined_maximum_size() const {
return get_combined_minimum_size();
}

void Control::_size_changed() {
Rect2 parent_rect = get_parent_anchorable_rect();

Expand Down
8 changes: 5 additions & 3 deletions scene/gui/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ class Control : public CanvasItem {
void set_v_grow_direction(GrowDirection p_direction);
GrowDirection get_v_grow_direction() const;

void set_anchors_preset(LayoutPreset p_preset, bool p_keep_offsets = true);
virtual void set_anchors_preset(LayoutPreset p_preset, bool p_keep_offsets = true);
void set_offsets_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode = PRESET_MODE_MINSIZE, int p_margin = 0);
void set_anchors_and_offsets_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode = PRESET_MODE_MINSIZE, int p_margin = 0);
void set_grow_direction_preset(LayoutPreset p_preset);
Expand All @@ -520,7 +520,7 @@ class Control : public CanvasItem {
Point2 get_global_position() const;
Point2 get_screen_position() const;

void set_size(const Size2 &p_size, bool p_keep_offsets = false);
virtual void set_size(const Size2 &p_size, bool p_keep_offsets = false);
Size2 get_size() const;
void reset_size();

Expand Down Expand Up @@ -549,9 +549,11 @@ class Control : public CanvasItem {
virtual Size2 get_minimum_size() const;
virtual Size2 get_combined_minimum_size() const;

void set_custom_minimum_size(const Size2 &p_custom);
virtual void set_custom_minimum_size(const Size2 &p_custom);
Size2 get_custom_minimum_size() const;

virtual Size2 get_combined_maximum_size() const;

// Container sizing.

void set_h_size_flags(BitField<SizeFlags> p_flags);
Expand Down
38 changes: 38 additions & 0 deletions scene/gui/scroll_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,44 @@ Size2 ScrollContainer::get_minimum_size() const {
return min_size;
}

Size2 ScrollContainer::get_combined_maximum_size() const {
largest_child_min_size = Size2();

for (int i = 0; i < get_child_count(); i++) {
Control *c = as_sortable_control(get_child(i), SortableVisibilityMode::VISIBLE);
if (!c) {
continue;
}
if (c == h_scroll || c == v_scroll || c == focus_panel) {
continue;
}

Size2 child_min_size = c->get_combined_minimum_size();

largest_child_min_size = largest_child_min_size.max(child_min_size);
}

Size2 max_size;
const Size2 size = get_size();

max_size.x = largest_child_min_size.x;
bool v_scroll_show = vertical_scroll_mode == SCROLL_MODE_SHOW_ALWAYS || vertical_scroll_mode == SCROLL_MODE_RESERVE || (vertical_scroll_mode == SCROLL_MODE_AUTO && largest_child_min_size.y > size.y);
if (v_scroll_show && v_scroll->get_parent() == this) {
max_size.x += v_scroll->get_minimum_size().x;
}

max_size.y = largest_child_min_size.y;
bool h_scroll_show = horizontal_scroll_mode == SCROLL_MODE_SHOW_ALWAYS || horizontal_scroll_mode == SCROLL_MODE_RESERVE || (horizontal_scroll_mode == SCROLL_MODE_AUTO && largest_child_min_size.x > size.x);
if (h_scroll_show && h_scroll->get_parent() == this) {
max_size.y += h_scroll->get_minimum_size().y;
}

Rect2 margins = _get_margins();
max_size += margins.position + margins.size;

return max_size;
}

void ScrollContainer::_cancel_drag() {
set_process_internal(false);
drag_touching_deaccel = false;
Expand Down
2 changes: 2 additions & 0 deletions scene/gui/scroll_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class ScrollContainer : public Container {
protected:
Size2 get_minimum_size() const override;

Size2 get_combined_maximum_size() const override;

void _gui_focus_changed(Control *p_control);
void _reposition_children();

Expand Down
173 changes: 173 additions & 0 deletions scene/gui/size_container.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/**************************************************************************/
/* size_container.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "size_container.h"

void SizeContainer::set_custom_minimum_size(const Size2 &p_custom) {
if ((p_custom.x > maximum_size.x && maximum_size.x > 0) || (p_custom.y > maximum_size.y && maximum_size.y > 0)) {
return;
}

Control::set_custom_minimum_size(p_custom);
}

void SizeContainer::set_size(const Size2 &p_size, bool p_keep_offsets) {
Size2 size = p_size;
if (maximum_size.x > 0) {
size.x = MIN(size.x, maximum_size.x);
}
if (maximum_size.y > 0) {
size.y = MIN(size.y, maximum_size.y);
}

Control::set_size(size, p_keep_offsets);
}

void SizeContainer::set_maximum_size(const Size2 &p_max_size) {
if (maximum_size == p_max_size) {
return;
}
maximum_size = p_max_size.maxf(0.0);
set_size(get_minimum_size());
}

Size2 SizeContainer::get_maximum_size() const {
return maximum_size;
}

Size2 SizeContainer::get_minimum_size() const {
Size2 ms;
bool expand_horizontal = false;
bool expand_vertical = false;
for (int i = 0; i < get_child_count(); i++) {
Control *c = as_sortable_control(get_child(i), SortableVisibilityMode::VISIBLE);
if (!c) {
continue;
}
Size2 minsize = c->get_combined_minimum_size();
Size2 maxsize = c->get_combined_maximum_size();
ms = ms.max(minsize).max(maxsize);
expand_horizontal = expand_horizontal || (c->get_h_size_flags() & SIZE_EXPAND);
expand_vertical = expand_vertical || (c->get_v_size_flags() & SIZE_EXPAND);
}

if (maximum_size.x > 0 && (ms.x > maximum_size.x || expand_horizontal)) {
ms.x = maximum_size.x;
}
if (maximum_size.y > 0 && (ms.y > maximum_size.y || expand_vertical)) {
ms.y = maximum_size.y;
}

return ms;
}

void SizeContainer::_validate_property(PropertyInfo &p_property) const {
// Remove FULL_RECT and other wide presets from the anchors_preset options
// since they conflict with the maximum_size constraint.
if (p_property.name == "anchors_preset") {
Vector<String> options = p_property.hint_string.split(",");
String new_hint_string;

for (int i = 0; i < options.size(); i++) {
Vector<String> parts = options[i].split(":");
if (parts.size() == 2) {
int preset_value = parts[1].to_int();
if (preset_value != PRESET_LEFT_WIDE &&
preset_value != PRESET_TOP_WIDE &&
preset_value != PRESET_RIGHT_WIDE &&
preset_value != PRESET_BOTTOM_WIDE &&
preset_value != PRESET_VCENTER_WIDE &&
preset_value != PRESET_HCENTER_WIDE &&
preset_value != PRESET_FULL_RECT) {
if (!new_hint_string.is_empty()) {
new_hint_string += ",";
}
new_hint_string += options[i];
}
}
}

p_property.hint_string = new_hint_string;
}
}

void SizeContainer::set_anchors_preset(LayoutPreset p_preset, bool p_keep_offsets) {
// Prevent setting wide presets that would conflict with maximum_size
if (p_preset == PRESET_LEFT_WIDE || p_preset == PRESET_TOP_WIDE ||
p_preset == PRESET_RIGHT_WIDE || p_preset == PRESET_BOTTOM_WIDE ||
p_preset == PRESET_VCENTER_WIDE || p_preset == PRESET_HCENTER_WIDE ||
p_preset == PRESET_FULL_RECT) {
WARN_PRINT("SizeContainer does not support wide anchor presets (including FULL_RECT) as they conflict with maximum_size constraints.");
return;
}

Control::set_anchors_preset(p_preset, p_keep_offsets);
}

void SizeContainer::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_SORT_CHILDREN: {
Size2 size = get_size();

// Clamp container size to max_size if set
if (maximum_size.x > 0) {
size.x = MIN(size.x, maximum_size.x);
}
if (maximum_size.y > 0) {
size.y = MIN(size.y, maximum_size.y);
}

for (int i = 0; i < get_child_count(); i++) {
Control *c = as_sortable_control(get_child(i));
if (!c) {
continue;
}

// Fit child within the constrained size
fit_child_in_rect(c, Rect2(Point2(), size));
}
} break;

case NOTIFICATION_THEME_CHANGED: {
update_minimum_size();
} break;
}
}

void SizeContainer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_maximum_size", "maximum_size"), &SizeContainer::set_maximum_size);
ClassDB::bind_method(D_METHOD("get_maximum_size"), &SizeContainer::get_maximum_size);

ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "maximum_size"), "set_maximum_size", "get_maximum_size");
}

SizeContainer::SizeContainer() {
set_clip_contents(true);
}
56 changes: 56 additions & 0 deletions scene/gui/size_container.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**************************************************************************/
/* size_container.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#pragma once

#include "scene/gui/container.h"

class SizeContainer : public Container {
GDCLASS(SizeContainer, Container);

private:
Size2 maximum_size;

protected:
void _notification(int p_what);
void _validate_property(PropertyInfo &p_property) const;
static void _bind_methods();

public:
virtual Size2 get_minimum_size() const override;
virtual void set_custom_minimum_size(const Size2 &p_custom) override;
virtual void set_size(const Size2 &p_size, bool p_keep_offsets = false) override;
virtual void set_anchors_preset(LayoutPreset p_preset, bool p_keep_offsets = true) override;

void set_maximum_size(const Size2 &p_max_size);
Size2 get_maximum_size() const;

SizeContainer();
};
2 changes: 2 additions & 0 deletions scene/register_scene_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
#include "scene/gui/scroll_bar.h"
#include "scene/gui/scroll_container.h"
#include "scene/gui/separator.h"
#include "scene/gui/size_container.h"
#include "scene/gui/slider.h"
#include "scene/gui/spin_box.h"
#include "scene/gui/split_container.h"
Expand Down Expand Up @@ -491,6 +492,7 @@ void register_scene_types() {
GDREGISTER_CLASS(HFlowContainer);
GDREGISTER_CLASS(VFlowContainer);
GDREGISTER_CLASS(MarginContainer);
GDREGISTER_CLASS(SizeContainer);

OS::get_singleton()->yield(); // may take time to init

Expand Down