| 1 | /**************************************************************************/ |
| 2 | /* 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 "container.h" |
| 32 | |
| 33 | #include "core/object/message_queue.h" |
| 34 | #include "scene/scene_string_names.h" |
| 35 | |
| 36 | void Container::_child_minsize_changed() { |
| 37 | update_minimum_size(); |
| 38 | queue_sort(); |
| 39 | } |
| 40 | |
| 41 | void Container::add_child_notify(Node *p_child) { |
| 42 | Control::add_child_notify(p_child); |
| 43 | |
| 44 | Control *control = Object::cast_to<Control>(p_child); |
| 45 | if (!control) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | control->connect(SNAME("size_flags_changed" ), callable_mp(this, &Container::queue_sort)); |
| 50 | control->connect(SNAME("minimum_size_changed" ), callable_mp(this, &Container::_child_minsize_changed)); |
| 51 | control->connect(SNAME("visibility_changed" ), callable_mp(this, &Container::_child_minsize_changed)); |
| 52 | |
| 53 | update_minimum_size(); |
| 54 | queue_sort(); |
| 55 | } |
| 56 | |
| 57 | void Container::move_child_notify(Node *p_child) { |
| 58 | Control::move_child_notify(p_child); |
| 59 | |
| 60 | if (!Object::cast_to<Control>(p_child)) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | update_minimum_size(); |
| 65 | queue_sort(); |
| 66 | } |
| 67 | |
| 68 | void Container::remove_child_notify(Node *p_child) { |
| 69 | Control::remove_child_notify(p_child); |
| 70 | |
| 71 | Control *control = Object::cast_to<Control>(p_child); |
| 72 | if (!control) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | control->disconnect("size_flags_changed" , callable_mp(this, &Container::queue_sort)); |
| 77 | control->disconnect("minimum_size_changed" , callable_mp(this, &Container::_child_minsize_changed)); |
| 78 | control->disconnect("visibility_changed" , callable_mp(this, &Container::_child_minsize_changed)); |
| 79 | |
| 80 | update_minimum_size(); |
| 81 | queue_sort(); |
| 82 | } |
| 83 | |
| 84 | void Container::_sort_children() { |
| 85 | if (!is_inside_tree()) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | notification(NOTIFICATION_PRE_SORT_CHILDREN); |
| 90 | emit_signal(SceneStringNames::get_singleton()->pre_sort_children); |
| 91 | |
| 92 | notification(NOTIFICATION_SORT_CHILDREN); |
| 93 | emit_signal(SceneStringNames::get_singleton()->sort_children); |
| 94 | pending_sort = false; |
| 95 | } |
| 96 | |
| 97 | void Container::fit_child_in_rect(Control *p_child, const Rect2 &p_rect) { |
| 98 | ERR_FAIL_NULL(p_child); |
| 99 | ERR_FAIL_COND(p_child->get_parent() != this); |
| 100 | |
| 101 | bool rtl = is_layout_rtl(); |
| 102 | Size2 minsize = p_child->get_combined_minimum_size(); |
| 103 | Rect2 r = p_rect; |
| 104 | |
| 105 | if (!(p_child->get_h_size_flags().has_flag(SIZE_FILL))) { |
| 106 | r.size.x = minsize.width; |
| 107 | if (p_child->get_h_size_flags().has_flag(SIZE_SHRINK_END)) { |
| 108 | r.position.x += rtl ? 0 : (p_rect.size.width - minsize.width); |
| 109 | } else if (p_child->get_h_size_flags().has_flag(SIZE_SHRINK_CENTER)) { |
| 110 | r.position.x += Math::floor((p_rect.size.x - minsize.width) / 2); |
| 111 | } else { |
| 112 | r.position.x += rtl ? (p_rect.size.width - minsize.width) : 0; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (!(p_child->get_v_size_flags().has_flag(SIZE_FILL))) { |
| 117 | r.size.y = minsize.y; |
| 118 | if (p_child->get_v_size_flags().has_flag(SIZE_SHRINK_END)) { |
| 119 | r.position.y += p_rect.size.height - minsize.height; |
| 120 | } else if (p_child->get_v_size_flags().has_flag(SIZE_SHRINK_CENTER)) { |
| 121 | r.position.y += Math::floor((p_rect.size.y - minsize.height) / 2); |
| 122 | } else { |
| 123 | r.position.y += 0; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | p_child->set_rect(r); |
| 128 | p_child->set_rotation(0); |
| 129 | p_child->set_scale(Vector2(1, 1)); |
| 130 | } |
| 131 | |
| 132 | void Container::queue_sort() { |
| 133 | if (!is_inside_tree()) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | if (pending_sort) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | MessageQueue::get_singleton()->push_callable(callable_mp(this, &Container::_sort_children)); |
| 142 | pending_sort = true; |
| 143 | } |
| 144 | |
| 145 | Vector<int> Container::get_allowed_size_flags_horizontal() const { |
| 146 | Vector<int> flags; |
| 147 | if (GDVIRTUAL_CALL(_get_allowed_size_flags_horizontal, flags)) { |
| 148 | return flags; |
| 149 | } |
| 150 | |
| 151 | flags.append(SIZE_FILL); |
| 152 | flags.append(SIZE_EXPAND); |
| 153 | flags.append(SIZE_SHRINK_BEGIN); |
| 154 | flags.append(SIZE_SHRINK_CENTER); |
| 155 | flags.append(SIZE_SHRINK_END); |
| 156 | return flags; |
| 157 | } |
| 158 | |
| 159 | Vector<int> Container::get_allowed_size_flags_vertical() const { |
| 160 | Vector<int> flags; |
| 161 | if (GDVIRTUAL_CALL(_get_allowed_size_flags_vertical, flags)) { |
| 162 | return flags; |
| 163 | } |
| 164 | |
| 165 | flags.append(SIZE_FILL); |
| 166 | flags.append(SIZE_EXPAND); |
| 167 | flags.append(SIZE_SHRINK_BEGIN); |
| 168 | flags.append(SIZE_SHRINK_CENTER); |
| 169 | flags.append(SIZE_SHRINK_END); |
| 170 | return flags; |
| 171 | } |
| 172 | |
| 173 | void Container::_notification(int p_what) { |
| 174 | switch (p_what) { |
| 175 | case NOTIFICATION_ENTER_TREE: { |
| 176 | pending_sort = false; |
| 177 | queue_sort(); |
| 178 | } break; |
| 179 | |
| 180 | case NOTIFICATION_RESIZED: |
| 181 | case NOTIFICATION_THEME_CHANGED: { |
| 182 | queue_sort(); |
| 183 | } break; |
| 184 | |
| 185 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 186 | if (is_visible_in_tree()) { |
| 187 | queue_sort(); |
| 188 | } |
| 189 | } break; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | PackedStringArray Container::get_configuration_warnings() const { |
| 194 | PackedStringArray warnings = Control::get_configuration_warnings(); |
| 195 | |
| 196 | if (get_class() == "Container" && get_script().is_null()) { |
| 197 | warnings.push_back(RTR("Container by itself serves no purpose unless a script configures its children placement behavior.\nIf you don't intend to add a script, use a plain Control node instead." )); |
| 198 | } |
| 199 | |
| 200 | return warnings; |
| 201 | } |
| 202 | |
| 203 | void Container::_bind_methods() { |
| 204 | ClassDB::bind_method(D_METHOD("queue_sort" ), &Container::queue_sort); |
| 205 | ClassDB::bind_method(D_METHOD("fit_child_in_rect" , "child" , "rect" ), &Container::fit_child_in_rect); |
| 206 | |
| 207 | GDVIRTUAL_BIND(_get_allowed_size_flags_horizontal); |
| 208 | GDVIRTUAL_BIND(_get_allowed_size_flags_vertical); |
| 209 | |
| 210 | BIND_CONSTANT(NOTIFICATION_PRE_SORT_CHILDREN); |
| 211 | BIND_CONSTANT(NOTIFICATION_SORT_CHILDREN); |
| 212 | |
| 213 | ADD_SIGNAL(MethodInfo("pre_sort_children" )); |
| 214 | ADD_SIGNAL(MethodInfo("sort_children" )); |
| 215 | } |
| 216 | |
| 217 | Container::Container() { |
| 218 | // All containers should let mouse events pass by default. |
| 219 | set_mouse_filter(MOUSE_FILTER_PASS); |
| 220 | } |
| 221 | |