|
| 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 | + ERR_FAIL_COND(p_max_size.height < 0 || p_max_size.width < 0); |
| 59 | + queue_sort(); |
| 60 | +} |
| 61 | + |
| 62 | +Size2 SizeContainer::get_maximum_size() const { |
| 63 | + return maximum_size; |
| 64 | +} |
| 65 | + |
| 66 | +Size2 SizeContainer::get_minimum_size() const { |
| 67 | + Size2 ms; |
| 68 | + for (int i = 0; i < get_child_count(); i++) { |
| 69 | + Control *c = as_sortable_control(get_child(i), SortableVisibilityMode::VISIBLE); |
| 70 | + if (!c) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + Size2 minsize = c->get_combined_minimum_size(); |
| 74 | + ms = ms.max(minsize); |
| 75 | + } |
| 76 | + |
| 77 | + if (maximum_size.x > 0) { |
| 78 | + ms.x = MIN(ms.x, maximum_size.x); |
| 79 | + } |
| 80 | + if (maximum_size.y > 0) { |
| 81 | + ms.y = MIN(ms.y, maximum_size.y); |
| 82 | + } |
| 83 | + |
| 84 | + return ms; |
| 85 | +} |
| 86 | + |
| 87 | +void SizeContainer::_notification(int p_what) { |
| 88 | + switch (p_what) { |
| 89 | + case NOTIFICATION_SORT_CHILDREN: { |
| 90 | + Size2 size = get_size(); |
| 91 | + |
| 92 | + // Clamp container size to max_size if set |
| 93 | + if (maximum_size.x > 0) { |
| 94 | + size.x = MIN(size.x, maximum_size.x); |
| 95 | + } |
| 96 | + if (maximum_size.y > 0) { |
| 97 | + size.y = MIN(size.y, maximum_size.y); |
| 98 | + } |
| 99 | + |
| 100 | + for (int i = 0; i < get_child_count(); i++) { |
| 101 | + Control *c = as_sortable_control(get_child(i)); |
| 102 | + if (!c) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + // Fit child within the constrained size |
| 107 | + fit_child_in_rect(c, Rect2(Point2(), size)); |
| 108 | + } |
| 109 | + } break; |
| 110 | + |
| 111 | + case NOTIFICATION_THEME_CHANGED: { |
| 112 | + update_minimum_size(); |
| 113 | + } break; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +void SizeContainer::_bind_methods() { |
| 118 | + ClassDB::bind_method(D_METHOD("set_maximum_size", "maximum_size"), &SizeContainer::set_maximum_size); |
| 119 | + ClassDB::bind_method(D_METHOD("get_maximum_size"), &SizeContainer::get_maximum_size); |
| 120 | + |
| 121 | + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "maximum_size"), "set_maximum_size", "get_maximum_size"); |
| 122 | +} |
| 123 | + |
| 124 | +SizeContainer::SizeContainer() { |
| 125 | + set_clip_contents(true); |
| 126 | +} |
0 commit comments