Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/gui/container.cpp
9903 views
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
void Container::_child_minsize_changed() {
34
update_minimum_size();
35
queue_sort();
36
}
37
38
void Container::add_child_notify(Node *p_child) {
39
Control::add_child_notify(p_child);
40
41
Control *control = Object::cast_to<Control>(p_child);
42
if (!control) {
43
return;
44
}
45
46
control->connect(SceneStringName(size_flags_changed), callable_mp(this, &Container::queue_sort));
47
control->connect(SceneStringName(minimum_size_changed), callable_mp(this, &Container::_child_minsize_changed));
48
control->connect(SceneStringName(visibility_changed), callable_mp(this, &Container::_child_minsize_changed));
49
50
update_minimum_size();
51
queue_sort();
52
}
53
54
void Container::move_child_notify(Node *p_child) {
55
Control::move_child_notify(p_child);
56
57
if (!Object::cast_to<Control>(p_child)) {
58
return;
59
}
60
61
update_minimum_size();
62
queue_sort();
63
}
64
65
void Container::remove_child_notify(Node *p_child) {
66
Control::remove_child_notify(p_child);
67
68
Control *control = Object::cast_to<Control>(p_child);
69
if (!control) {
70
return;
71
}
72
73
control->disconnect(SceneStringName(size_flags_changed), callable_mp(this, &Container::queue_sort));
74
control->disconnect(SceneStringName(minimum_size_changed), callable_mp(this, &Container::_child_minsize_changed));
75
control->disconnect(SceneStringName(visibility_changed), callable_mp(this, &Container::_child_minsize_changed));
76
77
update_minimum_size();
78
queue_sort();
79
}
80
81
void Container::_sort_children() {
82
if (!is_inside_tree()) {
83
pending_sort = false;
84
return;
85
}
86
87
notification(NOTIFICATION_PRE_SORT_CHILDREN);
88
emit_signal(SceneStringName(pre_sort_children));
89
90
notification(NOTIFICATION_SORT_CHILDREN);
91
emit_signal(SceneStringName(sort_children));
92
pending_sort = false;
93
}
94
95
void Container::fit_child_in_rect(Control *p_child, const Rect2 &p_rect) {
96
ERR_FAIL_NULL(p_child);
97
ERR_FAIL_COND(p_child->get_parent() != this);
98
99
bool rtl = is_layout_rtl();
100
Size2 minsize = p_child->get_combined_minimum_size();
101
Rect2 r = p_rect;
102
103
if (!(p_child->get_h_size_flags().has_flag(SIZE_FILL))) {
104
r.size.x = minsize.width;
105
if (p_child->get_h_size_flags().has_flag(SIZE_SHRINK_END)) {
106
r.position.x += rtl ? 0 : (p_rect.size.width - minsize.width);
107
} else if (p_child->get_h_size_flags().has_flag(SIZE_SHRINK_CENTER)) {
108
r.position.x += Math::floor((p_rect.size.x - minsize.width) / 2);
109
} else {
110
r.position.x += rtl ? (p_rect.size.width - minsize.width) : 0;
111
}
112
}
113
114
if (!(p_child->get_v_size_flags().has_flag(SIZE_FILL))) {
115
r.size.y = minsize.y;
116
if (p_child->get_v_size_flags().has_flag(SIZE_SHRINK_END)) {
117
r.position.y += p_rect.size.height - minsize.height;
118
} else if (p_child->get_v_size_flags().has_flag(SIZE_SHRINK_CENTER)) {
119
r.position.y += Math::floor((p_rect.size.y - minsize.height) / 2);
120
} else {
121
r.position.y += 0;
122
}
123
}
124
125
p_child->set_rect(r);
126
p_child->set_rotation(0);
127
p_child->set_scale(Vector2(1, 1));
128
}
129
130
void Container::queue_sort() {
131
if (!is_inside_tree()) {
132
return;
133
}
134
135
if (pending_sort) {
136
return;
137
}
138
139
callable_mp(this, &Container::_sort_children).call_deferred();
140
pending_sort = true;
141
}
142
143
Control *Container::as_sortable_control(Node *p_node, SortableVisibilityMode p_visibility_mode) const {
144
Control *c = Object::cast_to<Control>(p_node);
145
if (!c || c->is_set_as_top_level()) {
146
return nullptr;
147
}
148
if (p_visibility_mode == SortableVisibilityMode::VISIBLE && !c->is_visible()) {
149
return nullptr;
150
}
151
if (p_visibility_mode == SortableVisibilityMode::VISIBLE_IN_TREE && !c->is_visible_in_tree()) {
152
return nullptr;
153
}
154
return c;
155
}
156
157
Vector<int> Container::get_allowed_size_flags_horizontal() const {
158
Vector<int> flags;
159
if (GDVIRTUAL_CALL(_get_allowed_size_flags_horizontal, flags)) {
160
return flags;
161
}
162
163
flags.append(SIZE_FILL);
164
flags.append(SIZE_EXPAND);
165
flags.append(SIZE_SHRINK_BEGIN);
166
flags.append(SIZE_SHRINK_CENTER);
167
flags.append(SIZE_SHRINK_END);
168
return flags;
169
}
170
171
Vector<int> Container::get_allowed_size_flags_vertical() const {
172
Vector<int> flags;
173
if (GDVIRTUAL_CALL(_get_allowed_size_flags_vertical, flags)) {
174
return flags;
175
}
176
177
flags.append(SIZE_FILL);
178
flags.append(SIZE_EXPAND);
179
flags.append(SIZE_SHRINK_BEGIN);
180
flags.append(SIZE_SHRINK_CENTER);
181
flags.append(SIZE_SHRINK_END);
182
return flags;
183
}
184
185
void Container::_notification(int p_what) {
186
switch (p_what) {
187
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
188
RID ae = get_accessibility_element();
189
ERR_FAIL_COND(ae.is_null());
190
191
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_CONTAINER);
192
} break;
193
194
case NOTIFICATION_RESIZED:
195
case NOTIFICATION_THEME_CHANGED: {
196
queue_sort();
197
} break;
198
199
case NOTIFICATION_VISIBILITY_CHANGED: {
200
if (is_visible_in_tree()) {
201
queue_sort();
202
}
203
} break;
204
}
205
}
206
207
PackedStringArray Container::get_configuration_warnings() const {
208
PackedStringArray warnings = Control::get_configuration_warnings();
209
210
if (get_class() == "Container" && get_script().is_null()) {
211
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."));
212
}
213
214
return warnings;
215
}
216
217
void Container::_bind_methods() {
218
ClassDB::bind_method(D_METHOD("queue_sort"), &Container::queue_sort);
219
ClassDB::bind_method(D_METHOD("fit_child_in_rect", "child", "rect"), &Container::fit_child_in_rect);
220
221
GDVIRTUAL_BIND(_get_allowed_size_flags_horizontal);
222
GDVIRTUAL_BIND(_get_allowed_size_flags_vertical);
223
224
BIND_CONSTANT(NOTIFICATION_PRE_SORT_CHILDREN);
225
BIND_CONSTANT(NOTIFICATION_SORT_CHILDREN);
226
227
ADD_SIGNAL(MethodInfo("pre_sort_children"));
228
ADD_SIGNAL(MethodInfo("sort_children"));
229
}
230
231
Container::Container() {
232
// All containers should let mouse events pass by default.
233
set_mouse_filter(MOUSE_FILTER_PASS);
234
}
235
236