|
| 1 | +/**************************************************************************/ |
| 2 | +/* size_container.cpp */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | +/* */ |
| 11 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | +/* a copy of this software and associated documentation files (the */ |
| 13 | +/* "Software"), to deal in the Software without restriction, including */ |
| 14 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | +/* the following conditions: */ |
| 18 | +/* */ |
| 19 | +/* The above copyright notice and this permission notice shall be */ |
| 20 | +/* included in all copies or substantial portions of the Software. */ |
| 21 | +/* */ |
| 22 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | +/**************************************************************************/ |
| 30 | + |
| 31 | +#include "size_container.h" |
| 32 | + |
| 33 | +void SizeContainer::set_custom_minimum_size(const Size2 &p_custom) { |
| 34 | + if ((p_custom.x > maximum_size.x && maximum_size.x > 0) || (p_custom.y > maximum_size.y && maximum_size.y > 0)) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + Control::set_custom_minimum_size(p_custom); |
| 39 | +} |
| 40 | + |
| 41 | +void SizeContainer::set_size(const Size2 &p_size, bool p_keep_offsets) { |
| 42 | + Size2 size = p_size; |
| 43 | + if (maximum_size.x > 0) { |
| 44 | + size.x = MIN(size.x, maximum_size.x); |
| 45 | + } |
| 46 | + if (maximum_size.y > 0) { |
| 47 | + size.y = MIN(size.y, maximum_size.y); |
| 48 | + } |
| 49 | + |
| 50 | + Control::set_size(size, p_keep_offsets); |
| 51 | +} |
| 52 | + |
| 53 | +void SizeContainer::set_maximum_size(const Size2 &p_max_size) { |
| 54 | + if (maximum_size == p_max_size) { |
| 55 | + return; |
| 56 | + } |
| 57 | + maximum_size = p_max_size.maxf(0.0); |
| 58 | + set_size(get_minimum_size()); |
| 59 | +} |
| 60 | + |
| 61 | +Size2 SizeContainer::get_maximum_size() const { |
| 62 | + return maximum_size; |
| 63 | +} |
| 64 | + |
| 65 | +Size2 SizeContainer::get_minimum_size() const { |
| 66 | + Size2 ms; |
| 67 | + bool expand_horizontal = false; |
| 68 | + bool expand_vertical = false; |
| 69 | + for (int i = 0; i < get_child_count(); i++) { |
| 70 | + Control *c = as_sortable_control(get_child(i), SortableVisibilityMode::VISIBLE); |
| 71 | + if (!c) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + Size2 minsize = c->get_combined_minimum_size(); |
| 75 | + Size2 maxsize = c->get_combined_maximum_size(); |
| 76 | + ms = ms.max(minsize).max(maxsize); |
| 77 | + expand_horizontal = expand_horizontal || (c->get_h_size_flags() & SIZE_EXPAND); |
| 78 | + expand_vertical = expand_vertical || (c->get_v_size_flags() & SIZE_EXPAND); |
| 79 | + } |
| 80 | + |
| 81 | + if (maximum_size.x > 0 && (ms.x > maximum_size.x || expand_horizontal)) { |
| 82 | + ms.x = maximum_size.x; |
| 83 | + } |
| 84 | + if (maximum_size.y > 0 && (ms.y > maximum_size.y || expand_vertical)) { |
| 85 | + ms.y = maximum_size.y; |
| 86 | + } |
| 87 | + |
| 88 | + return ms; |
| 89 | +} |
| 90 | + |
| 91 | +void SizeContainer::_validate_property(PropertyInfo &p_property) const { |
| 92 | + // Remove FULL_RECT and other wide presets from the anchors_preset options |
| 93 | + // since they conflict with the maximum_size constraint. |
| 94 | + if (p_property.name == "anchors_preset") { |
| 95 | + Vector<String> options = p_property.hint_string.split(","); |
| 96 | + String new_hint_string; |
| 97 | + |
| 98 | + for (int i = 0; i < options.size(); i++) { |
| 99 | + Vector<String> parts = options[i].split(":"); |
| 100 | + if (parts.size() == 2) { |
| 101 | + int preset_value = parts[1].to_int(); |
| 102 | + if (preset_value != PRESET_LEFT_WIDE && |
| 103 | + preset_value != PRESET_TOP_WIDE && |
| 104 | + preset_value != PRESET_RIGHT_WIDE && |
| 105 | + preset_value != PRESET_BOTTOM_WIDE && |
| 106 | + preset_value != PRESET_VCENTER_WIDE && |
| 107 | + preset_value != PRESET_HCENTER_WIDE && |
| 108 | + preset_value != PRESET_FULL_RECT) { |
| 109 | + if (!new_hint_string.is_empty()) { |
| 110 | + new_hint_string += ","; |
| 111 | + } |
| 112 | + new_hint_string += options[i]; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + p_property.hint_string = new_hint_string; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +void SizeContainer::set_anchors_preset(LayoutPreset p_preset, bool p_keep_offsets) { |
| 122 | + // Prevent setting wide presets that would conflict with maximum_size |
| 123 | + if (p_preset == PRESET_LEFT_WIDE || p_preset == PRESET_TOP_WIDE || |
| 124 | + p_preset == PRESET_RIGHT_WIDE || p_preset == PRESET_BOTTOM_WIDE || |
| 125 | + p_preset == PRESET_VCENTER_WIDE || p_preset == PRESET_HCENTER_WIDE || |
| 126 | + p_preset == PRESET_FULL_RECT) { |
| 127 | + WARN_PRINT("SizeContainer does not support wide anchor presets (including FULL_RECT) as they conflict with maximum_size constraints."); |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + Control::set_anchors_preset(p_preset, p_keep_offsets); |
| 132 | +} |
| 133 | + |
| 134 | +void SizeContainer::_notification(int p_what) { |
| 135 | + switch (p_what) { |
| 136 | + case NOTIFICATION_SORT_CHILDREN: { |
| 137 | + Size2 size = get_size(); |
| 138 | + |
| 139 | + // Clamp container size to max_size if set |
| 140 | + if (maximum_size.x > 0) { |
| 141 | + size.x = MIN(size.x, maximum_size.x); |
| 142 | + } |
| 143 | + if (maximum_size.y > 0) { |
| 144 | + size.y = MIN(size.y, maximum_size.y); |
| 145 | + } |
| 146 | + |
| 147 | + for (int i = 0; i < get_child_count(); i++) { |
| 148 | + Control *c = as_sortable_control(get_child(i)); |
| 149 | + if (!c) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + // Fit child within the constrained size |
| 154 | + fit_child_in_rect(c, Rect2(Point2(), size)); |
| 155 | + } |
| 156 | + } break; |
| 157 | + |
| 158 | + case NOTIFICATION_THEME_CHANGED: { |
| 159 | + update_minimum_size(); |
| 160 | + } break; |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +void SizeContainer::_bind_methods() { |
| 165 | + ClassDB::bind_method(D_METHOD("set_maximum_size", "maximum_size"), &SizeContainer::set_maximum_size); |
| 166 | + ClassDB::bind_method(D_METHOD("get_maximum_size"), &SizeContainer::get_maximum_size); |
| 167 | + |
| 168 | + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "maximum_size"), "set_maximum_size", "get_maximum_size"); |
| 169 | +} |
| 170 | + |
| 171 | +SizeContainer::SizeContainer() { |
| 172 | + set_clip_contents(true); |
| 173 | +} |
0 commit comments