Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/gui/control.cpp
9903 views
1
/**************************************************************************/
2
/* control.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 "control.h"
32
33
#include "container.h"
34
#include "core/config/project_settings.h"
35
#include "core/input/input_map.h"
36
#include "core/os/os.h"
37
#include "core/string/string_builder.h"
38
#include "core/string/translation_server.h"
39
#include "scene/gui/scroll_container.h"
40
#include "scene/main/canvas_layer.h"
41
#include "scene/main/window.h"
42
#include "scene/theme/theme_db.h"
43
#include "scene/theme/theme_owner.h"
44
#include "servers/rendering_server.h"
45
#include "servers/text_server.h"
46
47
#ifdef TOOLS_ENABLED
48
#include "editor/scene/gui/control_editor_plugin.h"
49
#endif // TOOLS_ENABLED
50
51
// Editor plugin interoperability.
52
53
// TODO: Decouple controls from their editor plugin and get rid of this.
54
#ifdef TOOLS_ENABLED
55
Dictionary Control::_edit_get_state() const {
56
Dictionary s;
57
s["rotation"] = get_rotation();
58
s["scale"] = get_scale();
59
s["pivot"] = get_pivot_offset();
60
61
Array anchors = { get_anchor(SIDE_LEFT), get_anchor(SIDE_TOP), get_anchor(SIDE_RIGHT), get_anchor(SIDE_BOTTOM) };
62
s["anchors"] = anchors;
63
64
Array offsets = { get_offset(SIDE_LEFT), get_offset(SIDE_TOP), get_offset(SIDE_RIGHT), get_offset(SIDE_BOTTOM) };
65
s["offsets"] = offsets;
66
67
s["layout_mode"] = _get_layout_mode();
68
s["anchors_layout_preset"] = _get_anchors_layout_preset();
69
70
return s;
71
}
72
73
void Control::_edit_set_state(const Dictionary &p_state) {
74
ERR_FAIL_COND(p_state.is_empty() ||
75
!p_state.has("rotation") || !p_state.has("scale") ||
76
!p_state.has("pivot") || !p_state.has("anchors") || !p_state.has("offsets") ||
77
!p_state.has("layout_mode") || !p_state.has("anchors_layout_preset"));
78
Dictionary state = p_state;
79
80
set_rotation(state["rotation"]);
81
set_scale(state["scale"]);
82
set_pivot_offset(state["pivot"]);
83
84
Array anchors = state["anchors"];
85
86
// If anchors are not in their default position, force the anchor layout mode in place of position.
87
LayoutMode _layout = (LayoutMode)(int)state["layout_mode"];
88
if (_layout == LayoutMode::LAYOUT_MODE_POSITION) {
89
bool anchors_mode = ((real_t)anchors[0] != 0.0 || (real_t)anchors[1] != 0.0 || (real_t)anchors[2] != 0.0 || (real_t)anchors[3] != 0.0);
90
if (anchors_mode) {
91
_layout = LayoutMode::LAYOUT_MODE_ANCHORS;
92
}
93
}
94
95
_set_layout_mode(_layout);
96
if (_layout == LayoutMode::LAYOUT_MODE_ANCHORS || _layout == LayoutMode::LAYOUT_MODE_UNCONTROLLED) {
97
_set_anchors_layout_preset((int)state["anchors_layout_preset"]);
98
}
99
100
data.anchor[SIDE_LEFT] = anchors[0];
101
data.anchor[SIDE_TOP] = anchors[1];
102
data.anchor[SIDE_RIGHT] = anchors[2];
103
data.anchor[SIDE_BOTTOM] = anchors[3];
104
105
Array offsets = state["offsets"];
106
data.offset[SIDE_LEFT] = offsets[0];
107
data.offset[SIDE_TOP] = offsets[1];
108
data.offset[SIDE_RIGHT] = offsets[2];
109
data.offset[SIDE_BOTTOM] = offsets[3];
110
111
_size_changed();
112
}
113
114
void Control::_edit_set_position(const Point2 &p_position) {
115
ERR_FAIL_COND_MSG(!Engine::get_singleton()->is_editor_hint(), "This function can only be used from editor plugins.");
116
set_position(p_position, ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled() && get_parent_control());
117
}
118
119
Point2 Control::_edit_get_position() const {
120
return get_position();
121
}
122
123
void Control::_edit_set_scale(const Size2 &p_scale) {
124
set_scale(p_scale);
125
}
126
127
Size2 Control::_edit_get_scale() const {
128
return data.scale;
129
}
130
131
void Control::_edit_set_rect(const Rect2 &p_edit_rect) {
132
ERR_FAIL_COND_MSG(!Engine::get_singleton()->is_editor_hint(), "This function can only be used from editor plugins.");
133
set_position((get_position() + get_transform().basis_xform(p_edit_rect.position)), ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
134
set_size(p_edit_rect.size, ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
135
}
136
137
void Control::_edit_set_rotation(real_t p_rotation) {
138
set_rotation(p_rotation);
139
}
140
141
real_t Control::_edit_get_rotation() const {
142
return get_rotation();
143
}
144
145
bool Control::_edit_use_rotation() const {
146
return true;
147
}
148
149
void Control::_edit_set_pivot(const Point2 &p_pivot) {
150
Vector2 delta_pivot = p_pivot - get_pivot_offset();
151
Vector2 move = Vector2((std::cos(data.rotation) - 1.0) * delta_pivot.x - std::sin(data.rotation) * delta_pivot.y, std::sin(data.rotation) * delta_pivot.x + (std::cos(data.rotation) - 1.0) * delta_pivot.y);
152
set_position(get_position() + move);
153
set_pivot_offset(p_pivot);
154
}
155
156
Point2 Control::_edit_get_pivot() const {
157
return get_pivot_offset();
158
}
159
160
bool Control::_edit_use_pivot() const {
161
return true;
162
}
163
164
Size2 Control::_edit_get_minimum_size() const {
165
return get_combined_minimum_size();
166
}
167
#endif // TOOLS_ENABLED
168
169
#ifdef DEBUG_ENABLED
170
Rect2 Control::_edit_get_rect() const {
171
return Rect2(Point2(), get_size());
172
}
173
174
bool Control::_edit_use_rect() const {
175
return true;
176
}
177
#endif // DEBUG_ENABLED
178
179
void Control::reparent(Node *p_parent, bool p_keep_global_transform) {
180
ERR_MAIN_THREAD_GUARD;
181
if (p_keep_global_transform) {
182
Transform2D temp = get_global_transform();
183
Node::reparent(p_parent);
184
set_global_position(temp.get_origin());
185
} else {
186
Node::reparent(p_parent);
187
}
188
}
189
190
// Editor integration.
191
192
int Control::root_layout_direction = 0;
193
194
void Control::set_root_layout_direction(int p_root_dir) {
195
root_layout_direction = p_root_dir;
196
}
197
198
#ifdef TOOLS_ENABLED
199
void Control::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
200
ERR_READ_THREAD_GUARD;
201
if (p_idx == 0) {
202
const String pf = p_function;
203
Theme::DataType type = Theme::DATA_TYPE_MAX;
204
205
if (pf == "add_theme_color_override" || pf == "has_theme_color" || pf == "has_theme_color_override" || pf == "get_theme_color" || pf == "remove_theme_color_override") {
206
type = Theme::DATA_TYPE_COLOR;
207
} else if (pf == "add_theme_constant_override" || pf == "has_theme_constant" || pf == "has_theme_constant_override" || pf == "get_theme_constant" || pf == "remove_theme_constant_override") {
208
type = Theme::DATA_TYPE_CONSTANT;
209
} else if (pf == "add_theme_font_override" || pf == "has_theme_font" || pf == "has_theme_font_override" || pf == "get_theme_font" || pf == "remove_theme_font_override") {
210
type = Theme::DATA_TYPE_FONT;
211
} else if (pf == "add_theme_font_size_override" || pf == "has_theme_font_size" || pf == "has_theme_font_size_override" || pf == "get_theme_font_size" || pf == "remove_theme_font_size_override") {
212
type = Theme::DATA_TYPE_FONT_SIZE;
213
} else if (pf == "add_theme_icon_override" || pf == "has_theme_icon" || pf == "has_theme_icon_override" || pf == "get_theme_icon" || pf == "remove_theme_icon_override") {
214
type = Theme::DATA_TYPE_ICON;
215
} else if (pf == "add_theme_stylebox_override" || pf == "has_theme_stylebox" || pf == "has_theme_stylebox_override" || pf == "get_theme_stylebox" || pf == "remove_theme_stylebox_override") {
216
type = Theme::DATA_TYPE_STYLEBOX;
217
}
218
219
if (type != Theme::DATA_TYPE_MAX) {
220
List<ThemeDB::ThemeItemBind> theme_items;
221
ThemeDB::get_singleton()->get_class_items(get_class_name(), &theme_items, true, type);
222
223
List<StringName> sn;
224
for (const ThemeDB::ThemeItemBind &E : theme_items) {
225
if (E.data_type == type) {
226
sn.push_back(E.item_name);
227
}
228
}
229
230
sn.sort_custom<StringName::AlphCompare>();
231
for (const StringName &name : sn) {
232
r_options->push_back(String(name).quote());
233
}
234
}
235
}
236
CanvasItem::get_argument_options(p_function, p_idx, r_options);
237
}
238
#endif // TOOLS_ENABLED
239
240
PackedStringArray Control::get_configuration_warnings() const {
241
ERR_READ_THREAD_GUARD_V(PackedStringArray());
242
PackedStringArray warnings = CanvasItem::get_configuration_warnings();
243
244
if (data.mouse_filter == MOUSE_FILTER_IGNORE && !data.tooltip.is_empty()) {
245
warnings.push_back(RTR("The Hint Tooltip won't be displayed as the control's Mouse Filter is set to \"Ignore\". To solve this, set the Mouse Filter to \"Stop\" or \"Pass\"."));
246
}
247
248
return warnings;
249
}
250
251
PackedStringArray Control::get_accessibility_configuration_warnings() const {
252
ERR_READ_THREAD_GUARD_V(PackedStringArray());
253
PackedStringArray warnings = Node::get_accessibility_configuration_warnings();
254
255
String ac_name = get_accessibility_name().strip_edges();
256
if (ac_name.is_empty()) {
257
warnings.push_back(RTR("Accessibility Name must not be empty, or contain only spaces."));
258
}
259
if (ac_name.contains(get_class_name())) {
260
warnings.push_back(RTR("Accessibility Name must not include Node class name."));
261
}
262
for (int i = 0; i < ac_name.length(); i++) {
263
if (is_control(ac_name[i])) {
264
warnings.push_back(RTR("Accessibility Name must not include control character."));
265
break;
266
}
267
}
268
269
return warnings;
270
}
271
272
bool Control::is_text_field() const {
273
ERR_READ_THREAD_GUARD_V(false);
274
return false;
275
}
276
277
// Dynamic properties.
278
279
String Control::properties_managed_by_container[] = {
280
"offset_left",
281
"offset_top",
282
"offset_right",
283
"offset_bottom",
284
"anchor_left",
285
"anchor_top",
286
"anchor_right",
287
"anchor_bottom",
288
"position",
289
"rotation",
290
"scale",
291
"size"
292
};
293
294
bool Control::_set(const StringName &p_name, const Variant &p_value) {
295
ERR_MAIN_THREAD_GUARD_V(false);
296
String name = p_name;
297
298
if (!name.begins_with("theme_override")) {
299
return false;
300
}
301
302
if (p_value.get_type() == Variant::NIL || (p_value.get_type() == Variant::OBJECT && (Object *)p_value == nullptr)) {
303
if (name.begins_with("theme_override_icons/")) {
304
String dname = name.get_slicec('/', 1);
305
if (data.theme_icon_override.has(dname)) {
306
data.theme_icon_override[dname]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
307
}
308
data.theme_icon_override.erase(dname);
309
_notify_theme_override_changed();
310
} else if (name.begins_with("theme_override_styles/")) {
311
String dname = name.get_slicec('/', 1);
312
if (data.theme_style_override.has(dname)) {
313
data.theme_style_override[dname]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
314
}
315
data.theme_style_override.erase(dname);
316
_notify_theme_override_changed();
317
} else if (name.begins_with("theme_override_fonts/")) {
318
String dname = name.get_slicec('/', 1);
319
if (data.theme_font_override.has(dname)) {
320
data.theme_font_override[dname]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
321
}
322
data.theme_font_override.erase(dname);
323
_notify_theme_override_changed();
324
} else if (name.begins_with("theme_override_font_sizes/")) {
325
String dname = name.get_slicec('/', 1);
326
data.theme_font_size_override.erase(dname);
327
_notify_theme_override_changed();
328
} else if (name.begins_with("theme_override_colors/")) {
329
String dname = name.get_slicec('/', 1);
330
data.theme_color_override.erase(dname);
331
_notify_theme_override_changed();
332
} else if (name.begins_with("theme_override_constants/")) {
333
String dname = name.get_slicec('/', 1);
334
data.theme_constant_override.erase(dname);
335
_notify_theme_override_changed();
336
} else {
337
return false;
338
}
339
} else {
340
if (name.begins_with("theme_override_icons/")) {
341
String dname = name.get_slicec('/', 1);
342
add_theme_icon_override(dname, p_value);
343
} else if (name.begins_with("theme_override_styles/")) {
344
String dname = name.get_slicec('/', 1);
345
add_theme_style_override(dname, p_value);
346
} else if (name.begins_with("theme_override_fonts/")) {
347
String dname = name.get_slicec('/', 1);
348
add_theme_font_override(dname, p_value);
349
} else if (name.begins_with("theme_override_font_sizes/")) {
350
String dname = name.get_slicec('/', 1);
351
add_theme_font_size_override(dname, p_value);
352
} else if (name.begins_with("theme_override_colors/")) {
353
String dname = name.get_slicec('/', 1);
354
add_theme_color_override(dname, p_value);
355
} else if (name.begins_with("theme_override_constants/")) {
356
String dname = name.get_slicec('/', 1);
357
add_theme_constant_override(dname, p_value);
358
} else {
359
return false;
360
}
361
}
362
363
return true;
364
}
365
366
bool Control::_get(const StringName &p_name, Variant &r_ret) const {
367
ERR_MAIN_THREAD_GUARD_V(false);
368
String sname = p_name;
369
370
if (!sname.begins_with("theme_override")) {
371
return false;
372
}
373
374
if (sname.begins_with("theme_override_icons/")) {
375
String name = sname.get_slicec('/', 1);
376
r_ret = data.theme_icon_override.has(name) ? Variant(data.theme_icon_override[name]) : Variant();
377
} else if (sname.begins_with("theme_override_styles/")) {
378
String name = sname.get_slicec('/', 1);
379
r_ret = data.theme_style_override.has(name) ? Variant(data.theme_style_override[name]) : Variant();
380
} else if (sname.begins_with("theme_override_fonts/")) {
381
String name = sname.get_slicec('/', 1);
382
r_ret = data.theme_font_override.has(name) ? Variant(data.theme_font_override[name]) : Variant();
383
} else if (sname.begins_with("theme_override_font_sizes/")) {
384
String name = sname.get_slicec('/', 1);
385
r_ret = data.theme_font_size_override.has(name) ? Variant(data.theme_font_size_override[name]) : Variant();
386
} else if (sname.begins_with("theme_override_colors/")) {
387
String name = sname.get_slicec('/', 1);
388
r_ret = data.theme_color_override.has(name) ? Variant(data.theme_color_override[name]) : Variant();
389
} else if (sname.begins_with("theme_override_constants/")) {
390
String name = sname.get_slicec('/', 1);
391
r_ret = data.theme_constant_override.has(name) ? Variant(data.theme_constant_override[name]) : Variant();
392
} else {
393
return false;
394
}
395
396
return true;
397
}
398
399
void Control::_get_property_list(List<PropertyInfo> *p_list) const {
400
ERR_MAIN_THREAD_GUARD;
401
List<ThemeDB::ThemeItemBind> theme_items;
402
ThemeDB::get_singleton()->get_class_items(get_class_name(), &theme_items, true);
403
404
p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Theme Overrides", "theme_override_"), PROPERTY_HINT_NONE, "theme_override_", PROPERTY_USAGE_GROUP));
405
406
for (const ThemeDB::ThemeItemBind &E : theme_items) {
407
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
408
409
switch (E.data_type) {
410
case Theme::DATA_TYPE_COLOR: {
411
if (data.theme_color_override.has(E.item_name)) {
412
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
413
}
414
p_list->push_back(PropertyInfo(Variant::COLOR, PNAME("theme_override_colors") + String("/") + E.item_name, PROPERTY_HINT_NONE, "", usage));
415
} break;
416
417
case Theme::DATA_TYPE_CONSTANT: {
418
if (data.theme_constant_override.has(E.item_name)) {
419
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
420
}
421
p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_constants") + String("/") + E.item_name, PROPERTY_HINT_RANGE, "-16384,16384", usage));
422
} break;
423
424
case Theme::DATA_TYPE_FONT: {
425
if (data.theme_font_override.has(E.item_name)) {
426
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
427
}
428
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_fonts") + String("/") + E.item_name, PROPERTY_HINT_RESOURCE_TYPE, "Font", usage));
429
} break;
430
431
case Theme::DATA_TYPE_FONT_SIZE: {
432
if (data.theme_font_size_override.has(E.item_name)) {
433
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
434
}
435
p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_font_sizes") + String("/") + E.item_name, PROPERTY_HINT_RANGE, "1,256,1,or_greater,suffix:px", usage));
436
} break;
437
438
case Theme::DATA_TYPE_ICON: {
439
if (data.theme_icon_override.has(E.item_name)) {
440
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
441
}
442
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_icons") + String("/") + E.item_name, PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", usage));
443
} break;
444
445
case Theme::DATA_TYPE_STYLEBOX: {
446
if (data.theme_style_override.has(E.item_name)) {
447
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
448
}
449
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_styles") + String("/") + E.item_name, PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", usage));
450
} break;
451
452
default: {
453
// Silences warning.
454
} break;
455
}
456
}
457
}
458
459
void Control::_validate_property(PropertyInfo &p_property) const {
460
// Update theme type variation options.
461
if (Engine::get_singleton()->is_editor_hint() && p_property.name == "theme_type_variation") {
462
List<StringName> names;
463
464
ThemeDB::get_singleton()->get_default_theme()->get_type_variation_list(get_class_name(), &names);
465
466
// Iterate to find all themes.
467
Control *tmp_control = Object::cast_to<Control>(get_parent());
468
Window *tmp_window = Object::cast_to<Window>(get_parent());
469
while (tmp_control || tmp_window) {
470
// We go up and any non Control/Window will break the chain.
471
if (tmp_control) {
472
if (tmp_control->get_theme().is_valid()) {
473
tmp_control->get_theme()->get_type_variation_list(get_class_name(), &names);
474
}
475
tmp_window = Object::cast_to<Window>(tmp_control->get_parent());
476
tmp_control = Object::cast_to<Control>(tmp_control->get_parent());
477
} else { // Window.
478
if (tmp_window->get_theme().is_valid()) {
479
tmp_window->get_theme()->get_type_variation_list(get_class_name(), &names);
480
}
481
tmp_control = Object::cast_to<Control>(tmp_window->get_parent());
482
tmp_window = Object::cast_to<Window>(tmp_window->get_parent());
483
}
484
}
485
if (get_theme().is_valid()) {
486
get_theme()->get_type_variation_list(get_class_name(), &names);
487
}
488
if (ThemeDB::get_singleton()->get_project_theme().is_valid()) {
489
ThemeDB::get_singleton()->get_project_theme()->get_type_variation_list(get_class_name(), &names);
490
}
491
names.sort_custom<StringName::AlphCompare>();
492
493
Vector<StringName> unique_names;
494
String hint_string;
495
for (const StringName &E : names) {
496
// Skip duplicate values.
497
if (unique_names.has(E)) {
498
continue;
499
}
500
501
hint_string += String(E) + ",";
502
unique_names.append(E);
503
}
504
505
p_property.hint_string = hint_string;
506
}
507
508
if (Engine::get_singleton()->is_editor_hint() && p_property.name == "mouse_force_pass_scroll_events") {
509
// Disable force pass if the control is not stopping the event.
510
if (data.mouse_filter != MOUSE_FILTER_STOP) {
511
p_property.usage |= PROPERTY_USAGE_READ_ONLY;
512
}
513
}
514
515
if (Engine::get_singleton()->is_editor_hint() && p_property.name == "scale") {
516
p_property.hint = PROPERTY_HINT_LINK;
517
}
518
// Validate which positioning properties should be displayed depending on the parent and the layout mode.
519
Control *parent_control = get_parent_control();
520
if (Engine::get_singleton()->is_editor_hint() && !parent_control) {
521
// If there is no parent control, display both anchor and container options.
522
523
// Set the layout mode to be disabled with the proper value.
524
if (p_property.name == "layout_mode") {
525
p_property.hint_string = "Position,Anchors,Container,Uncontrolled";
526
p_property.usage |= PROPERTY_USAGE_READ_ONLY;
527
}
528
529
// Use the layout mode to display or hide advanced anchoring properties.
530
bool use_custom_anchors = _get_anchors_layout_preset() == -1; // Custom "preset".
531
if (!use_custom_anchors && (p_property.name.begins_with("anchor_") || p_property.name.begins_with("offset_") || p_property.name.begins_with("grow_"))) {
532
p_property.usage ^= PROPERTY_USAGE_EDITOR;
533
}
534
} else if (Object::cast_to<Container>(parent_control)) {
535
// If the parent is a container, display only container-related properties.
536
if (p_property.name.begins_with("anchor_") || p_property.name.begins_with("offset_") || p_property.name.begins_with("grow_") || p_property.name == "anchors_preset") {
537
p_property.usage ^= PROPERTY_USAGE_DEFAULT;
538
} else if (p_property.name == "position" || p_property.name == "rotation" || p_property.name == "scale" || p_property.name == "size" || p_property.name == "pivot_offset") {
539
p_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY;
540
} else if (Engine::get_singleton()->is_editor_hint() && p_property.name == "layout_mode") {
541
// Set the layout mode to be disabled with the proper value.
542
p_property.hint_string = "Position,Anchors,Container,Uncontrolled";
543
p_property.usage |= PROPERTY_USAGE_READ_ONLY;
544
} else if (Engine::get_singleton()->is_editor_hint() && (p_property.name == "size_flags_horizontal" || p_property.name == "size_flags_vertical")) {
545
// Filter allowed size flags based on the parent container configuration.
546
Container *parent_container = Object::cast_to<Container>(parent_control);
547
Vector<int> size_flags;
548
if (p_property.name == "size_flags_horizontal") {
549
size_flags = parent_container->get_allowed_size_flags_horizontal();
550
} else if (p_property.name == "size_flags_vertical") {
551
size_flags = parent_container->get_allowed_size_flags_vertical();
552
}
553
554
// Enforce the order of the options, regardless of what the container provided.
555
String hint_string;
556
if (size_flags.has(SIZE_FILL)) {
557
hint_string += "Fill:1";
558
}
559
if (size_flags.has(SIZE_EXPAND)) {
560
if (!hint_string.is_empty()) {
561
hint_string += ",";
562
}
563
hint_string += "Expand:2";
564
}
565
if (size_flags.has(SIZE_SHRINK_CENTER)) {
566
if (!hint_string.is_empty()) {
567
hint_string += ",";
568
}
569
hint_string += "Shrink Center:4";
570
}
571
if (size_flags.has(SIZE_SHRINK_END)) {
572
if (!hint_string.is_empty()) {
573
hint_string += ",";
574
}
575
hint_string += "Shrink End:8";
576
}
577
578
if (hint_string.is_empty()) {
579
p_property.hint_string = "";
580
p_property.usage |= PROPERTY_USAGE_READ_ONLY;
581
} else {
582
p_property.hint_string = hint_string;
583
}
584
}
585
} else if (Engine::get_singleton()->is_editor_hint()) {
586
// If the parent is a non-container control, display only anchoring-related properties.
587
if (p_property.name.begins_with("size_flags_")) {
588
p_property.usage ^= PROPERTY_USAGE_EDITOR;
589
590
} else if (p_property.name == "layout_mode") {
591
// Set the layout mode to be enabled with proper options.
592
p_property.hint_string = "Position,Anchors";
593
}
594
595
// Use the layout mode to display or hide advanced anchoring properties.
596
LayoutMode _layout = _get_layout_mode();
597
bool use_anchors = (_layout == LayoutMode::LAYOUT_MODE_ANCHORS || _layout == LayoutMode::LAYOUT_MODE_UNCONTROLLED);
598
if (!use_anchors && p_property.name == "anchors_preset") {
599
p_property.usage ^= PROPERTY_USAGE_EDITOR;
600
}
601
bool use_custom_anchors = use_anchors && _get_anchors_layout_preset() == -1; // Custom "preset".
602
if (!use_custom_anchors && (p_property.name.begins_with("anchor_") || p_property.name.begins_with("offset_") || p_property.name.begins_with("grow_"))) {
603
p_property.usage ^= PROPERTY_USAGE_EDITOR;
604
}
605
}
606
if (!Engine::get_singleton()->is_editor_hint()) {
607
return;
608
}
609
// Disable the property if it's managed by the parent container.
610
if (!Object::cast_to<Container>(parent_control)) {
611
return;
612
}
613
bool property_is_managed_by_container = false;
614
for (unsigned i = 0; i < properties_managed_by_container_count; i++) {
615
property_is_managed_by_container = properties_managed_by_container[i] == p_property.name;
616
if (property_is_managed_by_container) {
617
break;
618
}
619
}
620
if (property_is_managed_by_container) {
621
p_property.usage |= PROPERTY_USAGE_READ_ONLY;
622
}
623
}
624
625
bool Control::_property_can_revert(const StringName &p_name) const {
626
if (p_name == "layout_mode" || p_name == "anchors_preset") {
627
return true;
628
}
629
630
return false;
631
}
632
633
bool Control::_property_get_revert(const StringName &p_name, Variant &r_property) const {
634
if (p_name == "layout_mode") {
635
r_property = _get_default_layout_mode();
636
return true;
637
} else if (p_name == "anchors_preset") {
638
r_property = LayoutPreset::PRESET_TOP_LEFT;
639
return true;
640
}
641
642
return false;
643
}
644
645
// Global relations.
646
647
Control *Control::get_parent_control() const {
648
ERR_READ_THREAD_GUARD_V(nullptr);
649
return data.parent_control;
650
}
651
652
Window *Control::get_parent_window() const {
653
ERR_READ_THREAD_GUARD_V(nullptr);
654
return data.parent_window;
655
}
656
657
Control *Control::get_root_parent_control() const {
658
ERR_READ_THREAD_GUARD_V(nullptr);
659
const CanvasItem *ci = this;
660
const Control *root = this;
661
662
while (ci) {
663
const Control *c = Object::cast_to<Control>(ci);
664
if (c) {
665
root = c;
666
}
667
668
ci = ci->get_parent_item();
669
}
670
671
return const_cast<Control *>(root);
672
}
673
674
Rect2 Control::get_parent_anchorable_rect() const {
675
ERR_READ_THREAD_GUARD_V(Rect2());
676
if (!is_inside_tree()) {
677
return Rect2();
678
}
679
680
Rect2 parent_rect;
681
if (data.parent_canvas_item) {
682
parent_rect = data.parent_canvas_item->get_anchorable_rect();
683
} else {
684
#ifdef TOOLS_ENABLED
685
Node *edited_scene_root = get_tree()->get_edited_scene_root();
686
Node *scene_root_parent = edited_scene_root ? edited_scene_root->get_parent() : nullptr;
687
688
if (scene_root_parent && get_viewport() == scene_root_parent->get_viewport()) {
689
parent_rect.size = Size2(GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_width"), GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_height"));
690
} else {
691
parent_rect = get_viewport()->get_visible_rect();
692
}
693
694
#else
695
parent_rect = get_viewport()->get_visible_rect();
696
#endif // TOOLS_ENABLED
697
}
698
699
return parent_rect;
700
}
701
702
Size2 Control::get_parent_area_size() const {
703
ERR_READ_THREAD_GUARD_V(Size2());
704
return get_parent_anchorable_rect().size;
705
}
706
707
// Positioning and sizing.
708
709
Transform2D Control::_get_internal_transform() const {
710
// T(pivot_offset) * R(rotation) * S(scale) * T(-pivot_offset)
711
Transform2D xform(data.rotation, data.scale, 0.0f, data.pivot_offset);
712
xform.translate_local(-data.pivot_offset);
713
return xform;
714
}
715
716
void Control::_update_canvas_item_transform() {
717
Transform2D xform = _get_internal_transform();
718
xform[2] += get_position();
719
720
// We use a little workaround to avoid flickering when moving the pivot with _edit_set_pivot()
721
if (is_inside_tree() && Math::abs(Math::sin(data.rotation * 4.0f)) < 0.00001f && get_viewport()->is_snap_controls_to_pixels_enabled()) {
722
xform[2] = (xform[2] + Vector2(0.5, 0.5)).floor();
723
}
724
725
RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), xform);
726
}
727
728
Transform2D Control::get_transform() const {
729
ERR_READ_THREAD_GUARD_V(Transform2D());
730
Transform2D xform = _get_internal_transform();
731
xform[2] += get_position();
732
return xform;
733
}
734
735
void Control::_top_level_changed_on_parent() {
736
// Update root control status.
737
_notification(NOTIFICATION_EXIT_CANVAS);
738
_notification(NOTIFICATION_ENTER_CANVAS);
739
}
740
741
/// Anchors and offsets.
742
743
void Control::_set_anchor(Side p_side, real_t p_anchor) {
744
set_anchor(p_side, p_anchor);
745
}
746
747
void Control::set_anchor(Side p_side, real_t p_anchor, bool p_keep_offset, bool p_push_opposite_anchor) {
748
ERR_MAIN_THREAD_GUARD;
749
ERR_FAIL_INDEX((int)p_side, 4);
750
751
Rect2 parent_rect = get_parent_anchorable_rect();
752
real_t parent_range = (p_side == SIDE_LEFT || p_side == SIDE_RIGHT) ? parent_rect.size.x : parent_rect.size.y;
753
real_t previous_pos = data.offset[p_side] + data.anchor[p_side] * parent_range;
754
real_t previous_opposite_pos = data.offset[(p_side + 2) % 4] + data.anchor[(p_side + 2) % 4] * parent_range;
755
756
data.anchor[p_side] = p_anchor;
757
758
if (((p_side == SIDE_LEFT || p_side == SIDE_TOP) && data.anchor[p_side] > data.anchor[(p_side + 2) % 4]) ||
759
((p_side == SIDE_RIGHT || p_side == SIDE_BOTTOM) && data.anchor[p_side] < data.anchor[(p_side + 2) % 4])) {
760
if (p_push_opposite_anchor) {
761
data.anchor[(p_side + 2) % 4] = data.anchor[p_side];
762
} else {
763
data.anchor[p_side] = data.anchor[(p_side + 2) % 4];
764
}
765
}
766
767
if (!p_keep_offset) {
768
data.offset[p_side] = previous_pos - data.anchor[p_side] * parent_range;
769
if (p_push_opposite_anchor) {
770
data.offset[(p_side + 2) % 4] = previous_opposite_pos - data.anchor[(p_side + 2) % 4] * parent_range;
771
}
772
}
773
if (is_inside_tree()) {
774
_size_changed();
775
}
776
777
queue_redraw();
778
}
779
780
real_t Control::get_anchor(Side p_side) const {
781
ERR_READ_THREAD_GUARD_V(0);
782
ERR_FAIL_INDEX_V(int(p_side), 4, 0.0);
783
784
return data.anchor[p_side];
785
}
786
787
void Control::set_offset(Side p_side, real_t p_value) {
788
ERR_MAIN_THREAD_GUARD;
789
ERR_FAIL_INDEX((int)p_side, 4);
790
if (data.offset[p_side] == p_value) {
791
return;
792
}
793
794
data.offset[p_side] = p_value;
795
_size_changed();
796
}
797
798
real_t Control::get_offset(Side p_side) const {
799
ERR_READ_THREAD_GUARD_V(0);
800
ERR_FAIL_INDEX_V((int)p_side, 4, 0);
801
802
return data.offset[p_side];
803
}
804
805
void Control::set_anchor_and_offset(Side p_side, real_t p_anchor, real_t p_pos, bool p_push_opposite_anchor) {
806
ERR_MAIN_THREAD_GUARD;
807
set_anchor(p_side, p_anchor, false, p_push_opposite_anchor);
808
set_offset(p_side, p_pos);
809
}
810
811
void Control::set_begin(const Point2 &p_point) {
812
ERR_MAIN_THREAD_GUARD;
813
ERR_FAIL_COND(!std::isfinite(p_point.x) || !std::isfinite(p_point.y));
814
if (data.offset[0] == p_point.x && data.offset[1] == p_point.y) {
815
return;
816
}
817
818
data.offset[0] = p_point.x;
819
data.offset[1] = p_point.y;
820
_size_changed();
821
}
822
823
Point2 Control::get_begin() const {
824
ERR_READ_THREAD_GUARD_V(Vector2());
825
return Point2(data.offset[0], data.offset[1]);
826
}
827
828
void Control::set_end(const Point2 &p_point) {
829
ERR_MAIN_THREAD_GUARD;
830
if (data.offset[2] == p_point.x && data.offset[3] == p_point.y) {
831
return;
832
}
833
834
data.offset[2] = p_point.x;
835
data.offset[3] = p_point.y;
836
_size_changed();
837
}
838
839
Point2 Control::get_end() const {
840
ERR_READ_THREAD_GUARD_V(Point2());
841
return Point2(data.offset[2], data.offset[3]);
842
}
843
844
void Control::set_h_grow_direction(GrowDirection p_direction) {
845
ERR_MAIN_THREAD_GUARD;
846
if (data.h_grow == p_direction) {
847
return;
848
}
849
850
ERR_FAIL_INDEX((int)p_direction, 3);
851
852
data.h_grow = p_direction;
853
_size_changed();
854
}
855
856
Control::GrowDirection Control::get_h_grow_direction() const {
857
ERR_READ_THREAD_GUARD_V(GROW_DIRECTION_BEGIN);
858
return data.h_grow;
859
}
860
861
void Control::set_v_grow_direction(GrowDirection p_direction) {
862
ERR_MAIN_THREAD_GUARD;
863
if (data.v_grow == p_direction) {
864
return;
865
}
866
867
ERR_FAIL_INDEX((int)p_direction, 3);
868
869
data.v_grow = p_direction;
870
_size_changed();
871
}
872
873
Control::GrowDirection Control::get_v_grow_direction() const {
874
ERR_READ_THREAD_GUARD_V(GROW_DIRECTION_BEGIN);
875
return data.v_grow;
876
}
877
878
void Control::_compute_anchors(Rect2 p_rect, const real_t p_offsets[4], real_t (&r_anchors)[4]) {
879
Size2 parent_rect_size = get_parent_anchorable_rect().size;
880
ERR_FAIL_COND(parent_rect_size.x == 0.0);
881
ERR_FAIL_COND(parent_rect_size.y == 0.0);
882
883
real_t x = p_rect.position.x;
884
if (is_layout_rtl()) {
885
x = parent_rect_size.x - x - p_rect.size.x;
886
}
887
r_anchors[0] = (x - p_offsets[0]) / parent_rect_size.x;
888
r_anchors[1] = (p_rect.position.y - p_offsets[1]) / parent_rect_size.y;
889
r_anchors[2] = (x + p_rect.size.x - p_offsets[2]) / parent_rect_size.x;
890
r_anchors[3] = (p_rect.position.y + p_rect.size.y - p_offsets[3]) / parent_rect_size.y;
891
}
892
893
void Control::_compute_offsets(Rect2 p_rect, const real_t p_anchors[4], real_t (&r_offsets)[4]) {
894
Size2 parent_rect_size = get_parent_anchorable_rect().size;
895
896
real_t x = p_rect.position.x;
897
if (is_layout_rtl()) {
898
x = parent_rect_size.x - x - p_rect.size.x;
899
}
900
r_offsets[0] = x - (p_anchors[0] * parent_rect_size.x);
901
r_offsets[1] = p_rect.position.y - (p_anchors[1] * parent_rect_size.y);
902
r_offsets[2] = x + p_rect.size.x - (p_anchors[2] * parent_rect_size.x);
903
r_offsets[3] = p_rect.position.y + p_rect.size.y - (p_anchors[3] * parent_rect_size.y);
904
}
905
906
/// Presets and layout modes.
907
908
void Control::_set_layout_mode(LayoutMode p_mode) {
909
bool list_changed = false;
910
911
if (data.stored_layout_mode != p_mode) {
912
list_changed = true;
913
data.stored_layout_mode = p_mode;
914
}
915
916
if (data.stored_layout_mode == LayoutMode::LAYOUT_MODE_POSITION) {
917
data.stored_use_custom_anchors = false;
918
set_anchors_and_offsets_preset(LayoutPreset::PRESET_TOP_LEFT, LayoutPresetMode::PRESET_MODE_KEEP_SIZE);
919
set_grow_direction_preset(LayoutPreset::PRESET_TOP_LEFT);
920
}
921
922
if (list_changed) {
923
notify_property_list_changed();
924
}
925
}
926
927
void Control::_update_layout_mode() {
928
LayoutMode computed_layout = _get_layout_mode();
929
if (data.stored_layout_mode != computed_layout) {
930
data.stored_layout_mode = computed_layout;
931
notify_property_list_changed();
932
}
933
}
934
935
Control::LayoutMode Control::_get_layout_mode() const {
936
Control *parent_control = get_parent_control();
937
// In these modes the property is read-only.
938
if (!parent_control) {
939
return LayoutMode::LAYOUT_MODE_UNCONTROLLED;
940
} else if (Object::cast_to<Container>(parent_control)) {
941
return LayoutMode::LAYOUT_MODE_CONTAINER;
942
}
943
944
// If anchors are not in the top-left position, this is definitely in anchors mode.
945
if (_get_anchors_layout_preset() != (int)LayoutPreset::PRESET_TOP_LEFT) {
946
return LayoutMode::LAYOUT_MODE_ANCHORS;
947
}
948
949
// Only position/anchors modes are valid for non-container control parent.
950
if (data.stored_layout_mode == LayoutMode::LAYOUT_MODE_POSITION || data.stored_layout_mode == LayoutMode::LAYOUT_MODE_ANCHORS) {
951
return data.stored_layout_mode;
952
}
953
954
// Otherwise fallback to position mode.
955
return LayoutMode::LAYOUT_MODE_POSITION;
956
}
957
958
Control::LayoutMode Control::_get_default_layout_mode() const {
959
Control *parent_control = get_parent_control();
960
// In these modes the property is read-only.
961
if (!parent_control) {
962
return LayoutMode::LAYOUT_MODE_UNCONTROLLED;
963
} else if (Object::cast_to<Container>(parent_control)) {
964
return LayoutMode::LAYOUT_MODE_CONTAINER;
965
}
966
967
// Otherwise fallback to the position mode.
968
return LayoutMode::LAYOUT_MODE_POSITION;
969
}
970
971
void Control::_set_anchors_layout_preset(int p_preset) {
972
if (data.stored_layout_mode != LayoutMode::LAYOUT_MODE_UNCONTROLLED && data.stored_layout_mode != LayoutMode::LAYOUT_MODE_ANCHORS) {
973
// In other modes the anchor preset is non-operational and shouldn't be set to anything.
974
return;
975
}
976
977
if (p_preset == -1) {
978
if (!data.stored_use_custom_anchors) {
979
data.stored_use_custom_anchors = true;
980
notify_property_list_changed();
981
}
982
return; // Keep settings as is.
983
}
984
985
bool list_changed = false;
986
987
if (data.stored_use_custom_anchors) {
988
list_changed = true;
989
data.stored_use_custom_anchors = false;
990
}
991
992
LayoutPreset preset = (LayoutPreset)p_preset;
993
// Set correct anchors.
994
set_anchors_preset(preset);
995
996
// Select correct preset mode.
997
switch (preset) {
998
case PRESET_TOP_LEFT:
999
case PRESET_TOP_RIGHT:
1000
case PRESET_BOTTOM_LEFT:
1001
case PRESET_BOTTOM_RIGHT:
1002
case PRESET_CENTER_LEFT:
1003
case PRESET_CENTER_TOP:
1004
case PRESET_CENTER_RIGHT:
1005
case PRESET_CENTER_BOTTOM:
1006
case PRESET_CENTER:
1007
set_offsets_preset(preset, LayoutPresetMode::PRESET_MODE_KEEP_SIZE);
1008
break;
1009
case PRESET_LEFT_WIDE:
1010
case PRESET_TOP_WIDE:
1011
case PRESET_RIGHT_WIDE:
1012
case PRESET_BOTTOM_WIDE:
1013
case PRESET_VCENTER_WIDE:
1014
case PRESET_HCENTER_WIDE:
1015
case PRESET_FULL_RECT:
1016
set_offsets_preset(preset, LayoutPresetMode::PRESET_MODE_MINSIZE);
1017
break;
1018
}
1019
1020
// Select correct grow directions.
1021
set_grow_direction_preset(preset);
1022
1023
if (list_changed) {
1024
notify_property_list_changed();
1025
}
1026
}
1027
1028
int Control::_get_anchors_layout_preset() const {
1029
// If this is a layout mode that doesn't rely on anchors, avoid excessive checks.
1030
if (data.stored_layout_mode != LayoutMode::LAYOUT_MODE_UNCONTROLLED && data.stored_layout_mode != LayoutMode::LAYOUT_MODE_ANCHORS) {
1031
return LayoutPreset::PRESET_TOP_LEFT;
1032
}
1033
1034
// If the custom preset was selected by user, use it.
1035
if (data.stored_use_custom_anchors) {
1036
return -1;
1037
}
1038
1039
// Check anchors to determine if the current state matches a preset, or not.
1040
1041
float left = get_anchor(SIDE_LEFT);
1042
float right = get_anchor(SIDE_RIGHT);
1043
float top = get_anchor(SIDE_TOP);
1044
float bottom = get_anchor(SIDE_BOTTOM);
1045
1046
if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_BEGIN && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_BEGIN) {
1047
return (int)LayoutPreset::PRESET_TOP_LEFT;
1048
}
1049
if (left == (float)ANCHOR_END && right == (float)ANCHOR_END && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_BEGIN) {
1050
return (int)LayoutPreset::PRESET_TOP_RIGHT;
1051
}
1052
if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_BEGIN && top == (float)ANCHOR_END && bottom == (float)ANCHOR_END) {
1053
return (int)LayoutPreset::PRESET_BOTTOM_LEFT;
1054
}
1055
if (left == (float)ANCHOR_END && right == (float)ANCHOR_END && top == (float)ANCHOR_END && bottom == (float)ANCHOR_END) {
1056
return (int)LayoutPreset::PRESET_BOTTOM_RIGHT;
1057
}
1058
1059
if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_BEGIN && top == 0.5 && bottom == 0.5) {
1060
return (int)LayoutPreset::PRESET_CENTER_LEFT;
1061
}
1062
if (left == (float)ANCHOR_END && right == (float)ANCHOR_END && top == 0.5 && bottom == 0.5) {
1063
return (int)LayoutPreset::PRESET_CENTER_RIGHT;
1064
}
1065
if (left == 0.5 && right == 0.5 && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_BEGIN) {
1066
return (int)LayoutPreset::PRESET_CENTER_TOP;
1067
}
1068
if (left == 0.5 && right == 0.5 && top == (float)ANCHOR_END && bottom == (float)ANCHOR_END) {
1069
return (int)LayoutPreset::PRESET_CENTER_BOTTOM;
1070
}
1071
if (left == 0.5 && right == 0.5 && top == 0.5 && bottom == 0.5) {
1072
return (int)LayoutPreset::PRESET_CENTER;
1073
}
1074
1075
if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_BEGIN && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_END) {
1076
return (int)LayoutPreset::PRESET_LEFT_WIDE;
1077
}
1078
if (left == (float)ANCHOR_END && right == (float)ANCHOR_END && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_END) {
1079
return (int)LayoutPreset::PRESET_RIGHT_WIDE;
1080
}
1081
if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_END && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_BEGIN) {
1082
return (int)LayoutPreset::PRESET_TOP_WIDE;
1083
}
1084
if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_END && top == (float)ANCHOR_END && bottom == (float)ANCHOR_END) {
1085
return (int)LayoutPreset::PRESET_BOTTOM_WIDE;
1086
}
1087
1088
if (left == 0.5 && right == 0.5 && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_END) {
1089
return (int)LayoutPreset::PRESET_VCENTER_WIDE;
1090
}
1091
if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_END && top == 0.5 && bottom == 0.5) {
1092
return (int)LayoutPreset::PRESET_HCENTER_WIDE;
1093
}
1094
1095
if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_END && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_END) {
1096
return (int)LayoutPreset::PRESET_FULL_RECT;
1097
}
1098
1099
// Does not match any preset, return "Custom".
1100
return -1;
1101
}
1102
1103
void Control::set_anchors_preset(LayoutPreset p_preset, bool p_keep_offsets) {
1104
ERR_MAIN_THREAD_GUARD;
1105
ERR_FAIL_INDEX((int)p_preset, 16);
1106
1107
//Left
1108
switch (p_preset) {
1109
case PRESET_TOP_LEFT:
1110
case PRESET_BOTTOM_LEFT:
1111
case PRESET_CENTER_LEFT:
1112
case PRESET_TOP_WIDE:
1113
case PRESET_BOTTOM_WIDE:
1114
case PRESET_LEFT_WIDE:
1115
case PRESET_HCENTER_WIDE:
1116
case PRESET_FULL_RECT:
1117
set_anchor(SIDE_LEFT, ANCHOR_BEGIN, p_keep_offsets);
1118
break;
1119
1120
case PRESET_CENTER_TOP:
1121
case PRESET_CENTER_BOTTOM:
1122
case PRESET_CENTER:
1123
case PRESET_VCENTER_WIDE:
1124
set_anchor(SIDE_LEFT, 0.5, p_keep_offsets);
1125
break;
1126
1127
case PRESET_TOP_RIGHT:
1128
case PRESET_BOTTOM_RIGHT:
1129
case PRESET_CENTER_RIGHT:
1130
case PRESET_RIGHT_WIDE:
1131
set_anchor(SIDE_LEFT, ANCHOR_END, p_keep_offsets);
1132
break;
1133
}
1134
1135
// Top
1136
switch (p_preset) {
1137
case PRESET_TOP_LEFT:
1138
case PRESET_TOP_RIGHT:
1139
case PRESET_CENTER_TOP:
1140
case PRESET_LEFT_WIDE:
1141
case PRESET_RIGHT_WIDE:
1142
case PRESET_TOP_WIDE:
1143
case PRESET_VCENTER_WIDE:
1144
case PRESET_FULL_RECT:
1145
set_anchor(SIDE_TOP, ANCHOR_BEGIN, p_keep_offsets);
1146
break;
1147
1148
case PRESET_CENTER_LEFT:
1149
case PRESET_CENTER_RIGHT:
1150
case PRESET_CENTER:
1151
case PRESET_HCENTER_WIDE:
1152
set_anchor(SIDE_TOP, 0.5, p_keep_offsets);
1153
break;
1154
1155
case PRESET_BOTTOM_LEFT:
1156
case PRESET_BOTTOM_RIGHT:
1157
case PRESET_CENTER_BOTTOM:
1158
case PRESET_BOTTOM_WIDE:
1159
set_anchor(SIDE_TOP, ANCHOR_END, p_keep_offsets);
1160
break;
1161
}
1162
1163
// Right
1164
switch (p_preset) {
1165
case PRESET_TOP_LEFT:
1166
case PRESET_BOTTOM_LEFT:
1167
case PRESET_CENTER_LEFT:
1168
case PRESET_LEFT_WIDE:
1169
set_anchor(SIDE_RIGHT, ANCHOR_BEGIN, p_keep_offsets);
1170
break;
1171
1172
case PRESET_CENTER_TOP:
1173
case PRESET_CENTER_BOTTOM:
1174
case PRESET_CENTER:
1175
case PRESET_VCENTER_WIDE:
1176
set_anchor(SIDE_RIGHT, 0.5, p_keep_offsets);
1177
break;
1178
1179
case PRESET_TOP_RIGHT:
1180
case PRESET_BOTTOM_RIGHT:
1181
case PRESET_CENTER_RIGHT:
1182
case PRESET_TOP_WIDE:
1183
case PRESET_RIGHT_WIDE:
1184
case PRESET_BOTTOM_WIDE:
1185
case PRESET_HCENTER_WIDE:
1186
case PRESET_FULL_RECT:
1187
set_anchor(SIDE_RIGHT, ANCHOR_END, p_keep_offsets);
1188
break;
1189
}
1190
1191
// Bottom
1192
switch (p_preset) {
1193
case PRESET_TOP_LEFT:
1194
case PRESET_TOP_RIGHT:
1195
case PRESET_CENTER_TOP:
1196
case PRESET_TOP_WIDE:
1197
set_anchor(SIDE_BOTTOM, ANCHOR_BEGIN, p_keep_offsets);
1198
break;
1199
1200
case PRESET_CENTER_LEFT:
1201
case PRESET_CENTER_RIGHT:
1202
case PRESET_CENTER:
1203
case PRESET_HCENTER_WIDE:
1204
set_anchor(SIDE_BOTTOM, 0.5, p_keep_offsets);
1205
break;
1206
1207
case PRESET_BOTTOM_LEFT:
1208
case PRESET_BOTTOM_RIGHT:
1209
case PRESET_CENTER_BOTTOM:
1210
case PRESET_LEFT_WIDE:
1211
case PRESET_RIGHT_WIDE:
1212
case PRESET_BOTTOM_WIDE:
1213
case PRESET_VCENTER_WIDE:
1214
case PRESET_FULL_RECT:
1215
set_anchor(SIDE_BOTTOM, ANCHOR_END, p_keep_offsets);
1216
break;
1217
}
1218
}
1219
1220
void Control::set_offsets_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode, int p_margin) {
1221
ERR_MAIN_THREAD_GUARD;
1222
ERR_FAIL_INDEX((int)p_preset, 16);
1223
ERR_FAIL_INDEX((int)p_resize_mode, 4);
1224
1225
// Calculate the size if the node is not resized
1226
Size2 min_size = get_minimum_size();
1227
Size2 new_size = get_size();
1228
if (p_resize_mode == PRESET_MODE_MINSIZE || p_resize_mode == PRESET_MODE_KEEP_HEIGHT) {
1229
new_size.x = min_size.x;
1230
}
1231
if (p_resize_mode == PRESET_MODE_MINSIZE || p_resize_mode == PRESET_MODE_KEEP_WIDTH) {
1232
new_size.y = min_size.y;
1233
}
1234
1235
Rect2 parent_rect = get_parent_anchorable_rect();
1236
1237
real_t x = parent_rect.size.x;
1238
if (is_layout_rtl()) {
1239
x = parent_rect.size.x - x - new_size.x;
1240
}
1241
//Left
1242
switch (p_preset) {
1243
case PRESET_TOP_LEFT:
1244
case PRESET_BOTTOM_LEFT:
1245
case PRESET_CENTER_LEFT:
1246
case PRESET_TOP_WIDE:
1247
case PRESET_BOTTOM_WIDE:
1248
case PRESET_LEFT_WIDE:
1249
case PRESET_HCENTER_WIDE:
1250
case PRESET_FULL_RECT:
1251
data.offset[0] = x * (0.0 - data.anchor[0]) + p_margin + parent_rect.position.x;
1252
break;
1253
1254
case PRESET_CENTER_TOP:
1255
case PRESET_CENTER_BOTTOM:
1256
case PRESET_CENTER:
1257
case PRESET_VCENTER_WIDE:
1258
data.offset[0] = x * (0.5 - data.anchor[0]) - new_size.x / 2 + parent_rect.position.x;
1259
break;
1260
1261
case PRESET_TOP_RIGHT:
1262
case PRESET_BOTTOM_RIGHT:
1263
case PRESET_CENTER_RIGHT:
1264
case PRESET_RIGHT_WIDE:
1265
data.offset[0] = x * (1.0 - data.anchor[0]) - new_size.x - p_margin + parent_rect.position.x;
1266
break;
1267
}
1268
1269
// Top
1270
switch (p_preset) {
1271
case PRESET_TOP_LEFT:
1272
case PRESET_TOP_RIGHT:
1273
case PRESET_CENTER_TOP:
1274
case PRESET_LEFT_WIDE:
1275
case PRESET_RIGHT_WIDE:
1276
case PRESET_TOP_WIDE:
1277
case PRESET_VCENTER_WIDE:
1278
case PRESET_FULL_RECT:
1279
data.offset[1] = parent_rect.size.y * (0.0 - data.anchor[1]) + p_margin + parent_rect.position.y;
1280
break;
1281
1282
case PRESET_CENTER_LEFT:
1283
case PRESET_CENTER_RIGHT:
1284
case PRESET_CENTER:
1285
case PRESET_HCENTER_WIDE:
1286
data.offset[1] = parent_rect.size.y * (0.5 - data.anchor[1]) - new_size.y / 2 + parent_rect.position.y;
1287
break;
1288
1289
case PRESET_BOTTOM_LEFT:
1290
case PRESET_BOTTOM_RIGHT:
1291
case PRESET_CENTER_BOTTOM:
1292
case PRESET_BOTTOM_WIDE:
1293
data.offset[1] = parent_rect.size.y * (1.0 - data.anchor[1]) - new_size.y - p_margin + parent_rect.position.y;
1294
break;
1295
}
1296
1297
// Right
1298
switch (p_preset) {
1299
case PRESET_TOP_LEFT:
1300
case PRESET_BOTTOM_LEFT:
1301
case PRESET_CENTER_LEFT:
1302
case PRESET_LEFT_WIDE:
1303
data.offset[2] = x * (0.0 - data.anchor[2]) + new_size.x + p_margin + parent_rect.position.x;
1304
break;
1305
1306
case PRESET_CENTER_TOP:
1307
case PRESET_CENTER_BOTTOM:
1308
case PRESET_CENTER:
1309
case PRESET_VCENTER_WIDE:
1310
data.offset[2] = x * (0.5 - data.anchor[2]) + new_size.x / 2 + parent_rect.position.x;
1311
break;
1312
1313
case PRESET_TOP_RIGHT:
1314
case PRESET_BOTTOM_RIGHT:
1315
case PRESET_CENTER_RIGHT:
1316
case PRESET_TOP_WIDE:
1317
case PRESET_RIGHT_WIDE:
1318
case PRESET_BOTTOM_WIDE:
1319
case PRESET_HCENTER_WIDE:
1320
case PRESET_FULL_RECT:
1321
data.offset[2] = x * (1.0 - data.anchor[2]) - p_margin + parent_rect.position.x;
1322
break;
1323
}
1324
1325
// Bottom
1326
switch (p_preset) {
1327
case PRESET_TOP_LEFT:
1328
case PRESET_TOP_RIGHT:
1329
case PRESET_CENTER_TOP:
1330
case PRESET_TOP_WIDE:
1331
data.offset[3] = parent_rect.size.y * (0.0 - data.anchor[3]) + new_size.y + p_margin + parent_rect.position.y;
1332
break;
1333
1334
case PRESET_CENTER_LEFT:
1335
case PRESET_CENTER_RIGHT:
1336
case PRESET_CENTER:
1337
case PRESET_HCENTER_WIDE:
1338
data.offset[3] = parent_rect.size.y * (0.5 - data.anchor[3]) + new_size.y / 2 + parent_rect.position.y;
1339
break;
1340
1341
case PRESET_BOTTOM_LEFT:
1342
case PRESET_BOTTOM_RIGHT:
1343
case PRESET_CENTER_BOTTOM:
1344
case PRESET_LEFT_WIDE:
1345
case PRESET_RIGHT_WIDE:
1346
case PRESET_BOTTOM_WIDE:
1347
case PRESET_VCENTER_WIDE:
1348
case PRESET_FULL_RECT:
1349
data.offset[3] = parent_rect.size.y * (1.0 - data.anchor[3]) - p_margin + parent_rect.position.y;
1350
break;
1351
}
1352
1353
_size_changed();
1354
}
1355
1356
void Control::set_anchors_and_offsets_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode, int p_margin) {
1357
ERR_MAIN_THREAD_GUARD;
1358
set_anchors_preset(p_preset);
1359
set_offsets_preset(p_preset, p_resize_mode, p_margin);
1360
}
1361
1362
void Control::set_grow_direction_preset(LayoutPreset p_preset) {
1363
ERR_MAIN_THREAD_GUARD;
1364
// Select correct horizontal grow direction.
1365
switch (p_preset) {
1366
case PRESET_TOP_LEFT:
1367
case PRESET_BOTTOM_LEFT:
1368
case PRESET_CENTER_LEFT:
1369
case PRESET_LEFT_WIDE:
1370
set_h_grow_direction(GrowDirection::GROW_DIRECTION_END);
1371
break;
1372
case PRESET_TOP_RIGHT:
1373
case PRESET_BOTTOM_RIGHT:
1374
case PRESET_CENTER_RIGHT:
1375
case PRESET_RIGHT_WIDE:
1376
set_h_grow_direction(GrowDirection::GROW_DIRECTION_BEGIN);
1377
break;
1378
case PRESET_CENTER_TOP:
1379
case PRESET_CENTER_BOTTOM:
1380
case PRESET_CENTER:
1381
case PRESET_TOP_WIDE:
1382
case PRESET_BOTTOM_WIDE:
1383
case PRESET_VCENTER_WIDE:
1384
case PRESET_HCENTER_WIDE:
1385
case PRESET_FULL_RECT:
1386
set_h_grow_direction(GrowDirection::GROW_DIRECTION_BOTH);
1387
break;
1388
}
1389
1390
// Select correct vertical grow direction.
1391
switch (p_preset) {
1392
case PRESET_TOP_LEFT:
1393
case PRESET_TOP_RIGHT:
1394
case PRESET_CENTER_TOP:
1395
case PRESET_TOP_WIDE:
1396
set_v_grow_direction(GrowDirection::GROW_DIRECTION_END);
1397
break;
1398
1399
case PRESET_BOTTOM_LEFT:
1400
case PRESET_BOTTOM_RIGHT:
1401
case PRESET_CENTER_BOTTOM:
1402
case PRESET_BOTTOM_WIDE:
1403
set_v_grow_direction(GrowDirection::GROW_DIRECTION_BEGIN);
1404
break;
1405
1406
case PRESET_CENTER_LEFT:
1407
case PRESET_CENTER_RIGHT:
1408
case PRESET_CENTER:
1409
case PRESET_LEFT_WIDE:
1410
case PRESET_RIGHT_WIDE:
1411
case PRESET_VCENTER_WIDE:
1412
case PRESET_HCENTER_WIDE:
1413
case PRESET_FULL_RECT:
1414
set_v_grow_direction(GrowDirection::GROW_DIRECTION_BOTH);
1415
break;
1416
}
1417
}
1418
1419
/// Manual positioning.
1420
1421
void Control::_set_position(const Point2 &p_point) {
1422
set_position(p_point);
1423
}
1424
1425
void Control::set_position(const Point2 &p_point, bool p_keep_offsets) {
1426
ERR_MAIN_THREAD_GUARD;
1427
1428
#ifdef TOOLS_ENABLED
1429
// Can't compute anchors, set position directly and return immediately.
1430
if (saving && !is_inside_tree()) {
1431
data.pos_cache = p_point;
1432
return;
1433
}
1434
#endif // TOOLS_ENABLED
1435
1436
if (p_keep_offsets) {
1437
_compute_anchors(Rect2(p_point, data.size_cache), data.offset, data.anchor);
1438
} else {
1439
_compute_offsets(Rect2(p_point, data.size_cache), data.anchor, data.offset);
1440
}
1441
_size_changed();
1442
}
1443
1444
Size2 Control::get_position() const {
1445
ERR_READ_THREAD_GUARD_V(Size2());
1446
return data.pos_cache;
1447
}
1448
1449
void Control::_set_global_position(const Point2 &p_point) {
1450
set_global_position(p_point);
1451
}
1452
1453
void Control::set_global_position(const Point2 &p_point, bool p_keep_offsets) {
1454
ERR_MAIN_THREAD_GUARD;
1455
// (parent_global_transform * T(new_position) * internal_transform).origin == new_global_position
1456
// (T(new_position) * internal_transform).origin == new_position_in_parent_space
1457
// new_position == new_position_in_parent_space - internal_transform.origin
1458
Point2 position_in_parent_space = data.parent_canvas_item ? data.parent_canvas_item->get_global_transform().affine_inverse().xform(p_point) : p_point;
1459
set_position(position_in_parent_space - _get_internal_transform().get_origin(), p_keep_offsets);
1460
}
1461
1462
Point2 Control::get_global_position() const {
1463
ERR_READ_THREAD_GUARD_V(Point2());
1464
return get_global_transform().get_origin();
1465
}
1466
1467
Point2 Control::get_screen_position() const {
1468
ERR_READ_THREAD_GUARD_V(Point2());
1469
ERR_FAIL_COND_V(!is_inside_tree(), Point2());
1470
return get_screen_transform().get_origin();
1471
}
1472
1473
void Control::_set_size(const Size2 &p_size) {
1474
#ifdef DEBUG_ENABLED
1475
if (data.size_warning && (data.anchor[SIDE_LEFT] != data.anchor[SIDE_RIGHT] || data.anchor[SIDE_TOP] != data.anchor[SIDE_BOTTOM])) {
1476
WARN_PRINT("Nodes with non-equal opposite anchors will have their size overridden after _ready(). \nIf you want to set size, change the anchors or consider using set_deferred().");
1477
}
1478
#endif // DEBUG_ENABLED
1479
set_size(p_size);
1480
}
1481
1482
void Control::set_size(const Size2 &p_size, bool p_keep_offsets) {
1483
ERR_MAIN_THREAD_GUARD;
1484
ERR_FAIL_COND(!std::isfinite(p_size.x) || !std::isfinite(p_size.y));
1485
Size2 new_size = p_size;
1486
Size2 min = get_combined_minimum_size();
1487
if (new_size.x < min.x) {
1488
new_size.x = min.x;
1489
}
1490
if (new_size.y < min.y) {
1491
new_size.y = min.y;
1492
}
1493
1494
#ifdef TOOLS_ENABLED
1495
// Can't compute anchors, set size directly and return immediately.
1496
if (saving && !is_inside_tree()) {
1497
data.size_cache = new_size;
1498
return;
1499
}
1500
#endif // TOOLS_ENABLED
1501
1502
if (p_keep_offsets) {
1503
_compute_anchors(Rect2(data.pos_cache, new_size), data.offset, data.anchor);
1504
} else {
1505
_compute_offsets(Rect2(data.pos_cache, new_size), data.anchor, data.offset);
1506
}
1507
_size_changed();
1508
}
1509
1510
Size2 Control::get_size() const {
1511
ERR_READ_THREAD_GUARD_V(Size2());
1512
return data.size_cache;
1513
}
1514
1515
void Control::reset_size() {
1516
ERR_MAIN_THREAD_GUARD;
1517
set_size(Size2());
1518
}
1519
1520
void Control::set_rect(const Rect2 &p_rect) {
1521
ERR_MAIN_THREAD_GUARD;
1522
for (int i = 0; i < 4; i++) {
1523
data.anchor[i] = ANCHOR_BEGIN;
1524
}
1525
1526
_compute_offsets(p_rect, data.anchor, data.offset);
1527
if (is_inside_tree()) {
1528
_size_changed();
1529
}
1530
}
1531
1532
Rect2 Control::get_rect() const {
1533
ERR_READ_THREAD_GUARD_V(Rect2());
1534
Transform2D xform = get_transform();
1535
return Rect2(xform.get_origin(), xform.get_scale() * get_size());
1536
}
1537
1538
Rect2 Control::get_global_rect() const {
1539
ERR_READ_THREAD_GUARD_V(Rect2());
1540
Transform2D xform = get_global_transform();
1541
return Rect2(xform.get_origin(), xform.get_scale() * get_size());
1542
}
1543
1544
Rect2 Control::get_screen_rect() const {
1545
ERR_READ_THREAD_GUARD_V(Rect2());
1546
ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
1547
1548
Transform2D xform = get_screen_transform();
1549
return Rect2(xform.get_origin(), xform.get_scale() * get_size());
1550
}
1551
1552
Rect2 Control::get_anchorable_rect() const {
1553
ERR_READ_THREAD_GUARD_V(Rect2());
1554
return Rect2(Point2(), get_size());
1555
}
1556
1557
void Control::set_scale(const Vector2 &p_scale) {
1558
ERR_MAIN_THREAD_GUARD;
1559
if (data.scale == p_scale) {
1560
return;
1561
}
1562
1563
data.scale = p_scale;
1564
// Avoid having 0 scale values, can lead to errors in physics and rendering.
1565
if (data.scale.x == 0) {
1566
data.scale.x = CMP_EPSILON;
1567
}
1568
if (data.scale.y == 0) {
1569
data.scale.y = CMP_EPSILON;
1570
}
1571
queue_redraw();
1572
_notify_transform();
1573
queue_accessibility_update();
1574
}
1575
1576
Vector2 Control::get_scale() const {
1577
ERR_READ_THREAD_GUARD_V(Vector2());
1578
return data.scale;
1579
}
1580
1581
void Control::set_rotation(real_t p_radians) {
1582
ERR_MAIN_THREAD_GUARD;
1583
if (data.rotation == p_radians) {
1584
return;
1585
}
1586
1587
data.rotation = p_radians;
1588
queue_redraw();
1589
_notify_transform();
1590
queue_accessibility_update();
1591
}
1592
1593
void Control::set_rotation_degrees(real_t p_degrees) {
1594
ERR_MAIN_THREAD_GUARD;
1595
set_rotation(Math::deg_to_rad(p_degrees));
1596
}
1597
1598
real_t Control::get_rotation() const {
1599
ERR_READ_THREAD_GUARD_V(0);
1600
return data.rotation;
1601
}
1602
1603
real_t Control::get_rotation_degrees() const {
1604
ERR_READ_THREAD_GUARD_V(0);
1605
return Math::rad_to_deg(get_rotation());
1606
}
1607
1608
void Control::set_pivot_offset(const Vector2 &p_pivot) {
1609
ERR_MAIN_THREAD_GUARD;
1610
if (data.pivot_offset == p_pivot) {
1611
return;
1612
}
1613
1614
data.pivot_offset = p_pivot;
1615
queue_redraw();
1616
_notify_transform();
1617
queue_accessibility_update();
1618
}
1619
1620
Vector2 Control::get_pivot_offset() const {
1621
ERR_READ_THREAD_GUARD_V(Vector2());
1622
return data.pivot_offset;
1623
}
1624
1625
/// Sizes.
1626
1627
void Control::_update_minimum_size() {
1628
if (!is_inside_tree()) {
1629
data.updating_last_minimum_size = false;
1630
return;
1631
}
1632
1633
Size2 minsize = get_combined_minimum_size();
1634
data.updating_last_minimum_size = false;
1635
1636
if (minsize != data.last_minimum_size) {
1637
data.last_minimum_size = minsize;
1638
_size_changed();
1639
emit_signal(SceneStringName(minimum_size_changed));
1640
}
1641
}
1642
1643
void Control::update_minimum_size() {
1644
ERR_MAIN_THREAD_GUARD;
1645
if (!is_inside_tree() || data.block_minimum_size_adjust) {
1646
return;
1647
}
1648
1649
// Invalidate cache upwards.
1650
Control *invalidate = this;
1651
while (invalidate && invalidate->data.minimum_size_valid) {
1652
invalidate->data.minimum_size_valid = false;
1653
if (invalidate->is_set_as_top_level()) {
1654
break; // Do not go further up.
1655
}
1656
1657
Window *parent_window = invalidate->get_parent_window();
1658
if (parent_window && parent_window->is_wrapping_controls()) {
1659
parent_window->child_controls_changed();
1660
break; // Stop on a window as well.
1661
}
1662
1663
invalidate = invalidate->get_parent_control();
1664
}
1665
1666
if (!is_visible_in_tree()) {
1667
return;
1668
}
1669
1670
if (data.updating_last_minimum_size) {
1671
return;
1672
}
1673
data.updating_last_minimum_size = true;
1674
1675
callable_mp(this, &Control::_update_minimum_size).call_deferred();
1676
}
1677
1678
void Control::set_block_minimum_size_adjust(bool p_block) {
1679
ERR_MAIN_THREAD_GUARD;
1680
data.block_minimum_size_adjust = p_block;
1681
}
1682
1683
Size2 Control::get_minimum_size() const {
1684
ERR_READ_THREAD_GUARD_V(Size2());
1685
Vector2 ms;
1686
GDVIRTUAL_CALL(_get_minimum_size, ms);
1687
return ms;
1688
}
1689
1690
void Control::set_custom_minimum_size(const Size2 &p_custom) {
1691
ERR_MAIN_THREAD_GUARD;
1692
if (p_custom == data.custom_minimum_size) {
1693
return;
1694
}
1695
1696
if (!std::isfinite(p_custom.x) || !std::isfinite(p_custom.y)) {
1697
// Prevent infinite loop.
1698
return;
1699
}
1700
1701
data.custom_minimum_size = p_custom;
1702
update_minimum_size();
1703
update_configuration_warnings();
1704
}
1705
1706
Size2 Control::get_custom_minimum_size() const {
1707
ERR_READ_THREAD_GUARD_V(Size2());
1708
return data.custom_minimum_size;
1709
}
1710
1711
void Control::_update_minimum_size_cache() const {
1712
Size2 minsize = get_minimum_size();
1713
minsize = minsize.max(data.custom_minimum_size);
1714
1715
data.minimum_size_cache = minsize;
1716
data.minimum_size_valid = true;
1717
}
1718
1719
Size2 Control::get_combined_minimum_size() const {
1720
ERR_READ_THREAD_GUARD_V(Size2());
1721
if (!data.minimum_size_valid) {
1722
_update_minimum_size_cache();
1723
}
1724
return data.minimum_size_cache;
1725
}
1726
1727
void Control::_size_changed() {
1728
Rect2 parent_rect = get_parent_anchorable_rect();
1729
1730
real_t edge_pos[4];
1731
1732
for (int i = 0; i < 4; i++) {
1733
real_t area = parent_rect.size[i & 1];
1734
edge_pos[i] = data.offset[i] + (data.anchor[i] * area);
1735
}
1736
1737
Point2 new_pos_cache = Point2(edge_pos[0], edge_pos[1]);
1738
Size2 new_size_cache = Point2(edge_pos[2], edge_pos[3]) - new_pos_cache;
1739
1740
Size2 minimum_size = get_combined_minimum_size();
1741
1742
if (minimum_size.width > new_size_cache.width) {
1743
if (data.h_grow == GROW_DIRECTION_BEGIN) {
1744
new_pos_cache.x += new_size_cache.width - minimum_size.width;
1745
} else if (data.h_grow == GROW_DIRECTION_BOTH) {
1746
new_pos_cache.x += 0.5 * (new_size_cache.width - minimum_size.width);
1747
}
1748
1749
new_size_cache.width = minimum_size.width;
1750
}
1751
1752
if (is_layout_rtl()) {
1753
new_pos_cache.x = parent_rect.size.x + 2 * parent_rect.position.x - new_pos_cache.x - new_size_cache.x;
1754
}
1755
1756
if (minimum_size.height > new_size_cache.height) {
1757
if (data.v_grow == GROW_DIRECTION_BEGIN) {
1758
new_pos_cache.y += new_size_cache.height - minimum_size.height;
1759
} else if (data.v_grow == GROW_DIRECTION_BOTH) {
1760
new_pos_cache.y += 0.5 * (new_size_cache.height - minimum_size.height);
1761
}
1762
1763
new_size_cache.height = minimum_size.height;
1764
}
1765
1766
bool pos_changed = new_pos_cache != data.pos_cache;
1767
bool size_changed = new_size_cache != data.size_cache;
1768
// Below helps in getting rid of floating point errors for signaling resized.
1769
bool approx_pos_changed = !new_pos_cache.is_equal_approx(data.pos_cache);
1770
bool approx_size_changed = !new_size_cache.is_equal_approx(data.size_cache);
1771
1772
if (pos_changed) {
1773
data.pos_cache = new_pos_cache;
1774
}
1775
if (size_changed) {
1776
data.size_cache = new_size_cache;
1777
}
1778
1779
if (is_inside_tree()) {
1780
if (approx_pos_changed || approx_size_changed) {
1781
// Ensure global transform is marked as dirty before `NOTIFICATION_RESIZED` / `item_rect_changed` signal
1782
// so an up to date global transform could be obtained when handling these.
1783
_notify_transform();
1784
1785
item_rect_changed(approx_size_changed);
1786
if (approx_size_changed) {
1787
notification(NOTIFICATION_RESIZED);
1788
}
1789
}
1790
1791
if (pos_changed && !approx_size_changed) {
1792
_update_canvas_item_transform();
1793
}
1794
1795
queue_accessibility_update();
1796
} else if (pos_changed) {
1797
_notify_transform();
1798
}
1799
}
1800
1801
void Control::_clear_size_warning() {
1802
data.size_warning = false;
1803
}
1804
1805
// Container sizing.
1806
1807
void Control::set_h_size_flags(BitField<SizeFlags> p_flags) {
1808
ERR_MAIN_THREAD_GUARD;
1809
if ((int)data.h_size_flags == (int)p_flags) {
1810
return;
1811
}
1812
data.h_size_flags = p_flags;
1813
emit_signal(SceneStringName(size_flags_changed));
1814
}
1815
1816
BitField<Control::SizeFlags> Control::get_h_size_flags() const {
1817
ERR_READ_THREAD_GUARD_V(SIZE_EXPAND_FILL);
1818
return data.h_size_flags;
1819
}
1820
1821
void Control::set_v_size_flags(BitField<SizeFlags> p_flags) {
1822
ERR_MAIN_THREAD_GUARD;
1823
if ((int)data.v_size_flags == (int)p_flags) {
1824
return;
1825
}
1826
data.v_size_flags = p_flags;
1827
emit_signal(SceneStringName(size_flags_changed));
1828
}
1829
1830
BitField<Control::SizeFlags> Control::get_v_size_flags() const {
1831
ERR_READ_THREAD_GUARD_V(SIZE_EXPAND_FILL);
1832
return data.v_size_flags;
1833
}
1834
1835
void Control::set_stretch_ratio(real_t p_ratio) {
1836
ERR_MAIN_THREAD_GUARD;
1837
if (data.expand == p_ratio) {
1838
return;
1839
}
1840
1841
data.expand = p_ratio;
1842
emit_signal(SceneStringName(size_flags_changed));
1843
}
1844
1845
real_t Control::get_stretch_ratio() const {
1846
ERR_READ_THREAD_GUARD_V(0);
1847
return data.expand;
1848
}
1849
1850
// Input events.
1851
1852
void Control::_call_gui_input(const Ref<InputEvent> &p_event) {
1853
if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
1854
emit_signal(SceneStringName(gui_input), p_event); // Signal should be first, so it's possible to override an event (and then accept it).
1855
}
1856
if (!is_inside_tree() || get_viewport()->is_input_handled()) {
1857
return; // Input was handled, abort.
1858
}
1859
1860
if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
1861
GDVIRTUAL_CALL(_gui_input, p_event);
1862
}
1863
if (!is_inside_tree() || get_viewport()->is_input_handled()) {
1864
return; // Input was handled, abort.
1865
}
1866
gui_input(p_event);
1867
}
1868
1869
void Control::gui_input(const Ref<InputEvent> &p_event) {
1870
}
1871
1872
void Control::accept_event() {
1873
ERR_MAIN_THREAD_GUARD;
1874
if (is_inside_tree()) {
1875
get_viewport()->_gui_accept_event();
1876
}
1877
}
1878
1879
bool Control::has_point(const Point2 &p_point) const {
1880
ERR_READ_THREAD_GUARD_V(false);
1881
bool ret;
1882
if (GDVIRTUAL_CALL(_has_point, p_point, ret)) {
1883
return ret;
1884
}
1885
return Rect2(Point2(), get_size()).has_point(p_point);
1886
}
1887
1888
void Control::set_mouse_filter(MouseFilter p_filter) {
1889
ERR_MAIN_THREAD_GUARD;
1890
ERR_FAIL_INDEX(p_filter, 3);
1891
1892
if (data.mouse_filter == p_filter) {
1893
return;
1894
}
1895
1896
data.mouse_filter = p_filter;
1897
notify_property_list_changed();
1898
update_configuration_warnings();
1899
1900
if (get_viewport()) {
1901
get_viewport()->_gui_update_mouse_over();
1902
}
1903
}
1904
1905
Control::MouseFilter Control::get_mouse_filter() const {
1906
ERR_READ_THREAD_GUARD_V(MOUSE_FILTER_IGNORE);
1907
return data.mouse_filter;
1908
}
1909
1910
Control::MouseFilter Control::get_mouse_filter_with_override() const {
1911
ERR_READ_THREAD_GUARD_V(MOUSE_FILTER_IGNORE);
1912
if (!_is_mouse_filter_enabled()) {
1913
return MOUSE_FILTER_IGNORE;
1914
}
1915
return data.mouse_filter;
1916
}
1917
1918
void Control::set_mouse_behavior_recursive(MouseBehaviorRecursive p_mouse_behavior_recursive) {
1919
ERR_MAIN_THREAD_GUARD;
1920
ERR_FAIL_INDEX(p_mouse_behavior_recursive, 3);
1921
if (data.mouse_behavior_recursive == p_mouse_behavior_recursive) {
1922
return;
1923
}
1924
data.mouse_behavior_recursive = p_mouse_behavior_recursive;
1925
_update_mouse_behavior_recursive();
1926
}
1927
1928
Control::MouseBehaviorRecursive Control::get_mouse_behavior_recursive() const {
1929
ERR_READ_THREAD_GUARD_V(MOUSE_BEHAVIOR_INHERITED);
1930
return data.mouse_behavior_recursive;
1931
}
1932
1933
bool Control::_is_mouse_filter_enabled() const {
1934
ERR_READ_THREAD_GUARD_V(false);
1935
if (data.mouse_behavior_recursive == MOUSE_BEHAVIOR_INHERITED) {
1936
if (data.parent_control) {
1937
return data.parent_mouse_behavior_recursive_enabled;
1938
}
1939
return true;
1940
}
1941
return data.mouse_behavior_recursive == MOUSE_BEHAVIOR_ENABLED;
1942
}
1943
1944
void Control::_update_mouse_behavior_recursive() {
1945
if (data.mouse_behavior_recursive == MOUSE_BEHAVIOR_INHERITED) {
1946
if (data.parent_control) {
1947
_propagate_mouse_behavior_recursive_recursively(data.parent_control->_is_mouse_filter_enabled(), false);
1948
} else {
1949
_propagate_mouse_behavior_recursive_recursively(true, false);
1950
}
1951
} else {
1952
_propagate_mouse_behavior_recursive_recursively(data.mouse_behavior_recursive == MOUSE_BEHAVIOR_ENABLED, false);
1953
}
1954
if (get_viewport()) {
1955
get_viewport()->_gui_update_mouse_over();
1956
}
1957
}
1958
1959
void Control::_propagate_mouse_behavior_recursive_recursively(bool p_enabled, bool p_skip_non_inherited) {
1960
if (p_skip_non_inherited && data.mouse_behavior_recursive != MOUSE_BEHAVIOR_INHERITED) {
1961
return;
1962
}
1963
1964
data.parent_mouse_behavior_recursive_enabled = p_enabled;
1965
for (int i = 0; i < get_child_count(); i++) {
1966
Control *control = Object::cast_to<Control>(get_child(i));
1967
if (control) {
1968
control->_propagate_mouse_behavior_recursive_recursively(p_enabled, true);
1969
}
1970
}
1971
}
1972
1973
void Control::set_force_pass_scroll_events(bool p_force_pass_scroll_events) {
1974
ERR_MAIN_THREAD_GUARD;
1975
data.force_pass_scroll_events = p_force_pass_scroll_events;
1976
}
1977
1978
bool Control::is_force_pass_scroll_events() const {
1979
ERR_READ_THREAD_GUARD_V(false);
1980
return data.force_pass_scroll_events;
1981
}
1982
1983
void Control::warp_mouse(const Point2 &p_position) {
1984
ERR_MAIN_THREAD_GUARD;
1985
ERR_FAIL_COND(!is_inside_tree());
1986
get_viewport()->warp_mouse(get_global_transform_with_canvas().xform(p_position));
1987
}
1988
1989
void Control::set_shortcut_context(const Node *p_node) {
1990
ERR_MAIN_THREAD_GUARD;
1991
if (p_node != nullptr) {
1992
data.shortcut_context = p_node->get_instance_id();
1993
} else {
1994
data.shortcut_context = ObjectID();
1995
}
1996
}
1997
1998
Node *Control::get_shortcut_context() const {
1999
ERR_READ_THREAD_GUARD_V(nullptr);
2000
Object *ctx_obj = ObjectDB::get_instance(data.shortcut_context);
2001
Node *ctx_node = Object::cast_to<Node>(ctx_obj);
2002
2003
return ctx_node;
2004
}
2005
2006
bool Control::is_focus_owner_in_shortcut_context() const {
2007
ERR_READ_THREAD_GUARD_V(false);
2008
if (data.shortcut_context == ObjectID()) {
2009
// No context, therefore global - always "in" context.
2010
return true;
2011
}
2012
2013
const Node *ctx_node = get_shortcut_context();
2014
const Control *vp_focus = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
2015
2016
// If the context is valid and the viewport focus is valid, check if the context is the focus or is a parent of it.
2017
return ctx_node && vp_focus && (ctx_node == vp_focus || ctx_node->is_ancestor_of(vp_focus));
2018
}
2019
2020
// Drag and drop handling.
2021
2022
void Control::set_drag_forwarding(const Callable &p_drag, const Callable &p_can_drop, const Callable &p_drop) {
2023
ERR_MAIN_THREAD_GUARD;
2024
data.forward_drag = p_drag;
2025
data.forward_can_drop = p_can_drop;
2026
data.forward_drop = p_drop;
2027
}
2028
2029
Variant Control::get_drag_data(const Point2 &p_point) {
2030
ERR_READ_THREAD_GUARD_V(Variant());
2031
Variant ret;
2032
if (data.forward_drag.is_valid()) {
2033
Variant p = p_point;
2034
const Variant *vp[1] = { &p };
2035
Callable::CallError ce;
2036
data.forward_drag.callp((const Variant **)vp, 1, ret, ce);
2037
if (ce.error != Callable::CallError::CALL_OK) {
2038
ERR_FAIL_V_MSG(Variant(), "Error calling forwarded method from 'get_drag_data': " + Variant::get_callable_error_text(data.forward_drag, (const Variant **)vp, 1, ce) + ".");
2039
}
2040
return ret;
2041
}
2042
2043
GDVIRTUAL_CALL(_get_drag_data, p_point, ret);
2044
return ret;
2045
}
2046
2047
bool Control::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
2048
ERR_READ_THREAD_GUARD_V(false);
2049
if (data.forward_can_drop.is_valid()) {
2050
Variant ret;
2051
Variant p = p_point;
2052
const Variant *vp[2] = { &p, &p_data };
2053
Callable::CallError ce;
2054
data.forward_can_drop.callp((const Variant **)vp, 2, ret, ce);
2055
if (ce.error != Callable::CallError::CALL_OK) {
2056
ERR_FAIL_V_MSG(Variant(), "Error calling forwarded method from 'can_drop_data': " + Variant::get_callable_error_text(data.forward_can_drop, (const Variant **)vp, 2, ce) + ".");
2057
}
2058
return ret;
2059
}
2060
2061
bool ret = false;
2062
GDVIRTUAL_CALL(_can_drop_data, p_point, p_data, ret);
2063
return ret;
2064
}
2065
2066
void Control::drop_data(const Point2 &p_point, const Variant &p_data) {
2067
ERR_READ_THREAD_GUARD;
2068
if (data.forward_drop.is_valid()) {
2069
Variant ret;
2070
Variant p = p_point;
2071
const Variant *vp[2] = { &p, &p_data };
2072
Callable::CallError ce;
2073
data.forward_drop.callp((const Variant **)vp, 2, ret, ce);
2074
if (ce.error != Callable::CallError::CALL_OK) {
2075
ERR_FAIL_MSG("Error calling forwarded method from 'drop_data': " + Variant::get_callable_error_text(data.forward_drop, (const Variant **)vp, 2, ce) + ".");
2076
}
2077
return;
2078
}
2079
2080
GDVIRTUAL_CALL(_drop_data, p_point, p_data);
2081
}
2082
2083
void Control::force_drag(const Variant &p_data, Control *p_control) {
2084
ERR_MAIN_THREAD_GUARD;
2085
ERR_FAIL_COND(!is_inside_tree());
2086
ERR_FAIL_COND(p_data.get_type() == Variant::NIL);
2087
2088
Viewport *vp = get_viewport();
2089
2090
vp->_gui_force_drag_start();
2091
vp->_gui_force_drag(this, p_data, p_control);
2092
}
2093
2094
void Control::accessibility_drag() {
2095
ERR_MAIN_THREAD_GUARD;
2096
ERR_FAIL_COND(!is_inside_tree());
2097
2098
Viewport *vp = get_viewport();
2099
2100
vp->_gui_force_drag_start();
2101
Variant dnd_data = get_drag_data(Vector2(Math::INF, Math::INF));
2102
if (dnd_data.get_type() != Variant::NIL) {
2103
Window *w = Window::get_from_id(get_window()->get_window_id());
2104
if (w) {
2105
w->accessibility_announcement(vformat(RTR("%s grabbed. Select target and use %s to drop, use %s to cancel."), vp->gui_get_drag_description(), InputMap::get_singleton()->get_action_description("ui_accessibility_drag_and_drop"), InputMap::get_singleton()->get_action_description("ui_cancel")));
2106
}
2107
vp->_gui_force_drag(this, dnd_data, nullptr);
2108
queue_accessibility_update();
2109
} else {
2110
vp->_gui_force_drag_cancel();
2111
}
2112
}
2113
2114
void Control::accessibility_drop() {
2115
ERR_MAIN_THREAD_GUARD;
2116
ERR_FAIL_COND(!is_inside_tree());
2117
ERR_FAIL_COND(!get_viewport()->gui_is_dragging());
2118
2119
get_viewport()->gui_perform_drop_at(Vector2(Math::INF, Math::INF), this);
2120
2121
queue_accessibility_update();
2122
}
2123
2124
String Control::get_accessibility_container_name(const Node *p_node) const {
2125
String ret;
2126
if (GDVIRTUAL_CALL(_get_accessibility_container_name, p_node, ret)) {
2127
} else if (data.parent_control) {
2128
ret = data.parent_control->get_accessibility_container_name(this);
2129
}
2130
return ret;
2131
}
2132
2133
void Control::set_accessibility_name(const String &p_name) {
2134
ERR_THREAD_GUARD
2135
if (data.accessibility_name != p_name) {
2136
data.accessibility_name = p_name;
2137
queue_accessibility_update();
2138
update_configuration_warnings();
2139
}
2140
}
2141
2142
String Control::get_accessibility_name() const {
2143
return tr(data.accessibility_name);
2144
}
2145
2146
void Control::set_accessibility_description(const String &p_description) {
2147
ERR_THREAD_GUARD
2148
if (data.accessibility_description != p_description) {
2149
data.accessibility_description = p_description;
2150
queue_accessibility_update();
2151
}
2152
}
2153
2154
String Control::get_accessibility_description() const {
2155
return tr(data.accessibility_description);
2156
}
2157
2158
void Control::set_accessibility_live(DisplayServer::AccessibilityLiveMode p_mode) {
2159
ERR_THREAD_GUARD
2160
if (data.accessibility_live != p_mode) {
2161
data.accessibility_live = p_mode;
2162
queue_accessibility_update();
2163
}
2164
}
2165
2166
DisplayServer::AccessibilityLiveMode Control::get_accessibility_live() const {
2167
return data.accessibility_live;
2168
}
2169
2170
void Control::set_accessibility_controls_nodes(const TypedArray<NodePath> &p_node_path) {
2171
ERR_MAIN_THREAD_GUARD;
2172
if (data.accessibility_controls_nodes != p_node_path) {
2173
data.accessibility_controls_nodes = p_node_path;
2174
queue_accessibility_update();
2175
}
2176
}
2177
2178
TypedArray<NodePath> Control::get_accessibility_controls_nodes() const {
2179
return data.accessibility_controls_nodes;
2180
}
2181
2182
void Control::set_accessibility_described_by_nodes(const TypedArray<NodePath> &p_node_path) {
2183
ERR_MAIN_THREAD_GUARD;
2184
if (data.accessibility_described_by_nodes != p_node_path) {
2185
data.accessibility_described_by_nodes = p_node_path;
2186
queue_accessibility_update();
2187
}
2188
}
2189
2190
TypedArray<NodePath> Control::get_accessibility_described_by_nodes() const {
2191
return data.accessibility_described_by_nodes;
2192
}
2193
2194
void Control::set_accessibility_labeled_by_nodes(const TypedArray<NodePath> &p_node_path) {
2195
ERR_MAIN_THREAD_GUARD;
2196
if (data.accessibility_labeled_by_nodes != p_node_path) {
2197
data.accessibility_labeled_by_nodes = p_node_path;
2198
queue_accessibility_update();
2199
}
2200
}
2201
2202
TypedArray<NodePath> Control::get_accessibility_labeled_by_nodes() const {
2203
return data.accessibility_labeled_by_nodes;
2204
}
2205
2206
void Control::set_accessibility_flow_to_nodes(const TypedArray<NodePath> &p_node_path) {
2207
ERR_MAIN_THREAD_GUARD;
2208
if (data.accessibility_flow_to_nodes != p_node_path) {
2209
data.accessibility_flow_to_nodes = p_node_path;
2210
queue_accessibility_update();
2211
}
2212
}
2213
2214
TypedArray<NodePath> Control::get_accessibility_flow_to_nodes() const {
2215
return data.accessibility_flow_to_nodes;
2216
}
2217
2218
void Control::set_drag_preview(Control *p_control) {
2219
ERR_MAIN_THREAD_GUARD;
2220
ERR_FAIL_COND(!is_inside_tree());
2221
ERR_FAIL_COND(!get_viewport()->gui_is_dragging());
2222
get_viewport()->_gui_set_drag_preview(this, p_control);
2223
}
2224
2225
bool Control::is_drag_successful() const {
2226
ERR_READ_THREAD_GUARD_V(false);
2227
return is_inside_tree() && get_viewport()->gui_is_drag_successful();
2228
}
2229
2230
// Focus.
2231
2232
void Control::set_focus_mode(FocusMode p_focus_mode) {
2233
ERR_MAIN_THREAD_GUARD;
2234
ERR_FAIL_INDEX((int)p_focus_mode, 4);
2235
2236
if (is_inside_tree() && p_focus_mode == FOCUS_NONE && data.focus_mode != FOCUS_NONE && has_focus()) {
2237
release_focus();
2238
}
2239
2240
data.focus_mode = p_focus_mode;
2241
}
2242
2243
Control::FocusMode Control::get_focus_mode() const {
2244
ERR_READ_THREAD_GUARD_V(FOCUS_NONE);
2245
return data.focus_mode;
2246
}
2247
2248
Control::FocusMode Control::get_focus_mode_with_override() const {
2249
ERR_READ_THREAD_GUARD_V(FOCUS_NONE);
2250
if (!_is_focus_mode_enabled()) {
2251
return FOCUS_NONE;
2252
}
2253
return data.focus_mode;
2254
}
2255
2256
void Control::set_focus_behavior_recursive(FocusBehaviorRecursive p_focus_behavior_recursive) {
2257
ERR_MAIN_THREAD_GUARD;
2258
ERR_FAIL_INDEX((int)p_focus_behavior_recursive, 3);
2259
if (data.focus_behavior_recursive == p_focus_behavior_recursive) {
2260
return;
2261
}
2262
data.focus_behavior_recursive = p_focus_behavior_recursive;
2263
_update_focus_behavior_recursive();
2264
}
2265
2266
Control::FocusBehaviorRecursive Control::get_focus_behavior_recursive() const {
2267
ERR_READ_THREAD_GUARD_V(FOCUS_BEHAVIOR_INHERITED);
2268
return data.focus_behavior_recursive;
2269
}
2270
2271
bool Control::_is_focusable() const {
2272
bool ac_enabled = is_inside_tree() && get_tree()->is_accessibility_enabled();
2273
return (is_visible_in_tree() && ((get_focus_mode_with_override() == FOCUS_ALL) || (get_focus_mode_with_override() == FOCUS_CLICK) || (ac_enabled && get_focus_mode_with_override() == FOCUS_ACCESSIBILITY)));
2274
}
2275
2276
bool Control::_is_focus_mode_enabled() const {
2277
if (data.focus_behavior_recursive == FOCUS_BEHAVIOR_INHERITED) {
2278
if (data.parent_control) {
2279
return data.parent_focus_behavior_recursive_enabled;
2280
}
2281
return true;
2282
}
2283
return data.focus_behavior_recursive == FOCUS_BEHAVIOR_ENABLED;
2284
}
2285
2286
void Control::_update_focus_behavior_recursive() {
2287
if (data.focus_behavior_recursive == FOCUS_BEHAVIOR_INHERITED) {
2288
Control *parent = get_parent_control();
2289
if (parent) {
2290
_propagate_focus_behavior_recursive_recursively(parent->_is_focus_mode_enabled(), false);
2291
} else {
2292
_propagate_focus_behavior_recursive_recursively(true, false);
2293
}
2294
} else {
2295
_propagate_focus_behavior_recursive_recursively(data.focus_behavior_recursive == FOCUS_BEHAVIOR_ENABLED, false);
2296
}
2297
}
2298
2299
void Control::_propagate_focus_behavior_recursive_recursively(bool p_enabled, bool p_skip_non_inherited) {
2300
if (is_inside_tree() && (data.focus_behavior_recursive == FOCUS_BEHAVIOR_DISABLED || (data.focus_behavior_recursive == FOCUS_BEHAVIOR_INHERITED && !p_enabled)) && has_focus()) {
2301
release_focus();
2302
}
2303
2304
if (p_skip_non_inherited && data.focus_behavior_recursive != FOCUS_BEHAVIOR_INHERITED) {
2305
return;
2306
}
2307
2308
data.parent_focus_behavior_recursive_enabled = p_enabled;
2309
2310
for (int i = 0; i < get_child_count(); i++) {
2311
Control *control = Object::cast_to<Control>(get_child(i));
2312
if (control) {
2313
control->_propagate_focus_behavior_recursive_recursively(p_enabled, true);
2314
}
2315
}
2316
}
2317
2318
bool Control::has_focus() const {
2319
ERR_READ_THREAD_GUARD_V(false);
2320
return is_inside_tree() && get_viewport()->_gui_control_has_focus(this);
2321
}
2322
2323
void Control::grab_focus() {
2324
ERR_MAIN_THREAD_GUARD;
2325
ERR_FAIL_COND(!is_inside_tree());
2326
2327
if (get_focus_mode_with_override() == FOCUS_NONE) {
2328
WARN_PRINT("This control can't grab focus. Use set_focus_mode() and set_focus_behavior_recursive() to allow a control to get focus.");
2329
return;
2330
}
2331
2332
get_viewport()->_gui_control_grab_focus(this);
2333
}
2334
2335
void Control::grab_click_focus() {
2336
ERR_MAIN_THREAD_GUARD;
2337
ERR_FAIL_COND(!is_inside_tree());
2338
2339
get_viewport()->_gui_grab_click_focus(this);
2340
}
2341
2342
void Control::release_focus() {
2343
ERR_MAIN_THREAD_GUARD;
2344
ERR_FAIL_COND(!is_inside_tree());
2345
2346
if (!has_focus()) {
2347
return;
2348
}
2349
2350
get_viewport()->gui_release_focus();
2351
}
2352
2353
static Control *_next_control(Control *p_from) {
2354
if (p_from->is_set_as_top_level()) {
2355
return nullptr; // Can't go above.
2356
}
2357
2358
Control *parent = p_from->get_parent_control();
2359
2360
if (!parent) {
2361
return nullptr;
2362
}
2363
2364
int next = p_from->get_index();
2365
ERR_FAIL_INDEX_V(next, parent->get_child_count(), nullptr);
2366
for (int i = (next + 1); i < parent->get_child_count(); i++) {
2367
Control *c = Object::cast_to<Control>(parent->get_child(i));
2368
if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
2369
continue;
2370
}
2371
2372
return c;
2373
}
2374
2375
// No next in parent, try the same in parent.
2376
return _next_control(parent);
2377
}
2378
2379
Control *Control::find_next_valid_focus() const {
2380
ERR_READ_THREAD_GUARD_V(nullptr);
2381
2382
// If the focus property is manually overwritten, attempt to use it.
2383
if (!data.focus_next.is_empty()) {
2384
Node *n = get_node_or_null(data.focus_next);
2385
ERR_FAIL_NULL_V_MSG(n, nullptr, "Next focus node path is invalid: '" + String(data.focus_next) + "'.");
2386
Control *c = Object::cast_to<Control>(n);
2387
ERR_FAIL_NULL_V_MSG(c, nullptr, "Next focus node is not a control: '" + n->get_name() + "'.");
2388
if (c->_is_focusable()) {
2389
return c;
2390
}
2391
}
2392
2393
Control *from = const_cast<Control *>(this);
2394
bool ac_enabled = get_tree() && get_tree()->is_accessibility_enabled();
2395
2396
// Index of the current `Control` subtree within the containing `Window`.
2397
int window_next = -1;
2398
2399
while (true) {
2400
// Find next child.
2401
2402
Control *next_child = nullptr;
2403
2404
for (int i = 0; i < from->get_child_count(); i++) {
2405
Control *c = Object::cast_to<Control>(from->get_child(i));
2406
if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
2407
continue;
2408
}
2409
2410
next_child = c;
2411
break;
2412
}
2413
2414
if (!next_child) {
2415
next_child = _next_control(from);
2416
if (!next_child) { // Nothing else. Go up and find either window or subwindow.
2417
next_child = const_cast<Control *>(this);
2418
2419
while (next_child) {
2420
if (next_child->is_set_as_top_level()) {
2421
break;
2422
}
2423
2424
if (next_child->data.RI) {
2425
break;
2426
}
2427
next_child = next_child->data.parent_control;
2428
}
2429
2430
Window *win = next_child == nullptr ? nullptr : next_child->data.parent_window;
2431
if (win) { // Cycle through `Control` subtrees of the parent window
2432
if (window_next == -1) {
2433
window_next = next_child->get_index();
2434
ERR_FAIL_INDEX_V(window_next, win->get_child_count(), nullptr);
2435
}
2436
2437
for (int i = 1; i < win->get_child_count() + 1; i++) {
2438
int next = Math::wrapi(window_next + i, 0, win->get_child_count());
2439
Control *c = Object::cast_to<Control>(win->get_child(next));
2440
if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
2441
continue;
2442
}
2443
window_next = next;
2444
next_child = c;
2445
break;
2446
}
2447
}
2448
}
2449
}
2450
2451
if (!next_child) {
2452
break;
2453
}
2454
2455
if ((next_child->get_focus_mode_with_override() == FOCUS_ALL) || (ac_enabled && next_child->get_focus_mode_with_override() == FOCUS_ACCESSIBILITY)) {
2456
return next_child;
2457
}
2458
2459
if (next_child == from || next_child == this) {
2460
return nullptr; // Stuck in a loop with no next control.
2461
}
2462
2463
from = next_child; // Try to find the next control with focus mode FOCUS_ALL.
2464
}
2465
2466
return nullptr;
2467
}
2468
2469
static Control *_prev_control(Control *p_from) {
2470
for (int i = p_from->get_child_count() - 1; i >= 0; i--) {
2471
Control *c = Object::cast_to<Control>(p_from->get_child(i));
2472
if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
2473
continue;
2474
}
2475
2476
// Find the last child as prev, try the same in the last child.
2477
return _prev_control(c);
2478
}
2479
2480
return p_from; // Not found in the children, return itself.
2481
}
2482
2483
Control *Control::find_prev_valid_focus() const {
2484
ERR_READ_THREAD_GUARD_V(nullptr);
2485
2486
// If the focus property is manually overwritten, attempt to use it.
2487
if (!data.focus_prev.is_empty()) {
2488
Node *n = get_node_or_null(data.focus_prev);
2489
ERR_FAIL_NULL_V_MSG(n, nullptr, "Previous focus node path is invalid: '" + String(data.focus_prev) + "'.");
2490
Control *c = Object::cast_to<Control>(n);
2491
ERR_FAIL_NULL_V_MSG(c, nullptr, "Previous focus node is not a control: '" + n->get_name() + "'.");
2492
if (c->_is_focusable()) {
2493
return c;
2494
}
2495
}
2496
2497
Control *from = const_cast<Control *>(this);
2498
bool ac_enabled = get_tree() && get_tree()->is_accessibility_enabled();
2499
2500
// Index of the current `Control` subtree within the containing `Window`.
2501
int window_prev = -1;
2502
2503
while (true) {
2504
// Find prev child.
2505
2506
Control *prev_child = nullptr;
2507
2508
if (from->is_set_as_top_level() || !from->data.parent_control) {
2509
// Find last of the children.
2510
2511
Window *win = from->data.parent_window;
2512
if (win) { // Cycle through `Control` subtrees of the parent window
2513
if (window_prev == -1) {
2514
window_prev = from->get_index();
2515
ERR_FAIL_INDEX_V(window_prev, win->get_child_count(), nullptr);
2516
}
2517
2518
for (int i = 1; i < win->get_child_count() + 1; i++) {
2519
int prev = Math::wrapi(window_prev - i, 0, win->get_child_count());
2520
Control *c = Object::cast_to<Control>(win->get_child(prev));
2521
if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
2522
continue;
2523
}
2524
window_prev = prev;
2525
prev_child = _prev_control(c);
2526
break;
2527
}
2528
}
2529
2530
if (!prev_child) {
2531
prev_child = _prev_control(from); // Wrap start here.
2532
}
2533
2534
} else {
2535
for (int i = (from->get_index() - 1); i >= 0; i--) {
2536
Control *c = Object::cast_to<Control>(from->get_parent()->get_child(i));
2537
2538
if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
2539
continue;
2540
}
2541
2542
prev_child = c;
2543
break;
2544
}
2545
2546
if (!prev_child) {
2547
prev_child = from->data.parent_control;
2548
} else {
2549
prev_child = _prev_control(prev_child);
2550
}
2551
}
2552
2553
if ((prev_child->get_focus_mode_with_override() == FOCUS_ALL) || (ac_enabled && prev_child->get_focus_mode_with_override() == FOCUS_ACCESSIBILITY)) {
2554
return prev_child;
2555
}
2556
2557
if (prev_child == from || prev_child == this) {
2558
return nullptr; // Stuck in a loop with no prev control.
2559
}
2560
2561
from = prev_child; // Try to find the prev control with focus mode FOCUS_ALL.
2562
}
2563
2564
return nullptr;
2565
}
2566
2567
void Control::set_focus_neighbor(Side p_side, const NodePath &p_neighbor) {
2568
ERR_MAIN_THREAD_GUARD;
2569
ERR_FAIL_INDEX((int)p_side, 4);
2570
data.focus_neighbor[p_side] = p_neighbor;
2571
}
2572
2573
NodePath Control::get_focus_neighbor(Side p_side) const {
2574
ERR_READ_THREAD_GUARD_V(NodePath());
2575
ERR_FAIL_INDEX_V((int)p_side, 4, NodePath());
2576
return data.focus_neighbor[p_side];
2577
}
2578
2579
void Control::set_focus_next(const NodePath &p_next) {
2580
ERR_MAIN_THREAD_GUARD;
2581
data.focus_next = p_next;
2582
}
2583
2584
NodePath Control::get_focus_next() const {
2585
ERR_READ_THREAD_GUARD_V(NodePath());
2586
return data.focus_next;
2587
}
2588
2589
void Control::set_focus_previous(const NodePath &p_prev) {
2590
ERR_MAIN_THREAD_GUARD;
2591
data.focus_prev = p_prev;
2592
}
2593
2594
NodePath Control::get_focus_previous() const {
2595
ERR_READ_THREAD_GUARD_V(NodePath());
2596
return data.focus_prev;
2597
}
2598
2599
#define MAX_NEIGHBOR_SEARCH_COUNT 512
2600
2601
Control *Control::_get_focus_neighbor(Side p_side, int p_count) {
2602
ERR_FAIL_INDEX_V((int)p_side, 4, nullptr);
2603
2604
if (p_count >= MAX_NEIGHBOR_SEARCH_COUNT) {
2605
return nullptr;
2606
}
2607
if (!data.focus_neighbor[p_side].is_empty()) {
2608
Node *n = get_node_or_null(data.focus_neighbor[p_side]);
2609
ERR_FAIL_NULL_V_MSG(n, nullptr, "Neighbor focus node path is invalid: '" + String(data.focus_neighbor[p_side]) + "'.");
2610
Control *c = Object::cast_to<Control>(n);
2611
ERR_FAIL_NULL_V_MSG(c, nullptr, "Neighbor focus node is not a control: '" + n->get_name() + "'.");
2612
if (c->_is_focusable()) {
2613
return c;
2614
}
2615
2616
c = c->_get_focus_neighbor(p_side, p_count + 1);
2617
return c;
2618
}
2619
2620
real_t square_of_dist = 1e14;
2621
Control *result = nullptr;
2622
2623
const Vector2 dir[4] = {
2624
Vector2(-1, 0),
2625
Vector2(0, -1),
2626
Vector2(1, 0),
2627
Vector2(0, 1)
2628
};
2629
2630
Vector2 vdir = dir[p_side];
2631
2632
Rect2 r = get_global_rect();
2633
real_t begin_d = vdir.dot(r.get_position());
2634
real_t end_d = vdir.dot(r.get_end());
2635
real_t maxd = MAX(begin_d, end_d);
2636
2637
Rect2 clamp = Rect2(-1e7, -1e7, 2e7, 2e7);
2638
Rect2 result_rect;
2639
2640
Node *base = this;
2641
2642
while (base) {
2643
ScrollContainer *sc = Object::cast_to<ScrollContainer>(base);
2644
2645
if (sc) {
2646
Rect2 sc_r = sc->get_global_rect();
2647
bool follow_focus = sc->is_following_focus();
2648
2649
if (result && !follow_focus && !sc_r.intersects(result_rect)) {
2650
result = nullptr; // Skip invisible control.
2651
}
2652
2653
if (result == nullptr) {
2654
real_t sc_begin_d = vdir.dot(sc_r.get_position());
2655
real_t sc_end_d = vdir.dot(sc_r.get_end());
2656
real_t sc_maxd = sc_begin_d;
2657
real_t sc_mind = sc_end_d;
2658
if (sc_begin_d < sc_end_d) {
2659
sc_maxd = sc_end_d;
2660
sc_mind = sc_begin_d;
2661
}
2662
2663
if (!follow_focus && maxd < sc_mind) {
2664
// Reposition to find visible control.
2665
maxd = sc_mind;
2666
r.set_position(r.get_position() + (sc_mind - maxd) * vdir);
2667
}
2668
2669
if (follow_focus || sc_maxd > maxd) {
2670
_window_find_focus_neighbor(vdir, base, r, clamp, maxd, square_of_dist, &result);
2671
}
2672
2673
if (result == nullptr) {
2674
// Reposition to search upwards.
2675
maxd = sc_maxd;
2676
r.set_position(r.get_position() + (sc_maxd - maxd) * vdir);
2677
} else {
2678
result_rect = result->get_global_rect();
2679
if (follow_focus) {
2680
real_t r_begin_d = vdir.dot(result_rect.get_position());
2681
real_t r_end_d = vdir.dot(result_rect.get_end());
2682
real_t r_maxd = r_begin_d;
2683
real_t r_mind = r_end_d;
2684
if (r_begin_d < r_end_d) {
2685
r_maxd = r_end_d;
2686
r_mind = r_begin_d;
2687
}
2688
2689
if (r_maxd > sc_maxd) {
2690
result_rect.set_position(result_rect.get_position() + (sc_maxd - r_maxd) * vdir);
2691
} else if (r_mind < sc_mind) {
2692
result_rect.set_position(result_rect.get_position() + (sc_mind - r_mind) * vdir);
2693
}
2694
}
2695
}
2696
}
2697
}
2698
2699
Control *c = Object::cast_to<Control>(base);
2700
if (c) {
2701
if (c->data.RI) {
2702
break;
2703
}
2704
}
2705
base = base->get_parent();
2706
}
2707
2708
if (result) {
2709
return result;
2710
}
2711
2712
if (!base) {
2713
return nullptr;
2714
}
2715
2716
_window_find_focus_neighbor(vdir, base, r, clamp, maxd, square_of_dist, &result);
2717
2718
return result;
2719
}
2720
2721
Control *Control::find_valid_focus_neighbor(Side p_side) const {
2722
return const_cast<Control *>(this)->_get_focus_neighbor(p_side);
2723
}
2724
2725
void Control::_window_find_focus_neighbor(const Vector2 &p_dir, Node *p_at, const Rect2 &p_rect, const Rect2 &p_clamp, real_t p_min, real_t &r_closest_dist_squared, Control **r_closest) {
2726
if (Object::cast_to<Viewport>(p_at)) {
2727
return; // Bye.
2728
}
2729
2730
bool ac_enabled = get_tree() && get_tree()->is_accessibility_enabled();
2731
2732
Control *c = Object::cast_to<Control>(p_at);
2733
Container *container = Object::cast_to<Container>(p_at);
2734
bool in_container = container ? container->is_ancestor_of(this) : false;
2735
2736
if (c && c != this && ((c->get_focus_mode_with_override() == FOCUS_ALL) || (ac_enabled && c->get_focus_mode_with_override() == FOCUS_ACCESSIBILITY)) && !in_container && p_clamp.intersects(c->get_global_rect())) {
2737
Rect2 r_c = c->get_global_rect();
2738
r_c = r_c.intersection(p_clamp);
2739
real_t begin_d = p_dir.dot(r_c.get_position());
2740
real_t end_d = p_dir.dot(r_c.get_end());
2741
real_t max = MAX(begin_d, end_d);
2742
2743
// Use max to allow navigation to overlapping controls (for ScrollContainer case).
2744
if (max > (p_min + CMP_EPSILON)) {
2745
// Calculate the shortest distance. (No shear transform)
2746
// Flip along axis(es) so that C falls in the first quadrant of c (as origin) for easy calculation.
2747
// The same transformation would put the direction vector in the positive direction (+x or +y).
2748
// | -------------
2749
// | | | |
2750
// | |-----C-----|
2751
// ----|---a | | |
2752
// | | | b------------
2753
// -|---c---|----------------------->
2754
// | | |
2755
// ----|----
2756
// cC = ca + ab + bC
2757
// The shortest distance is the vector ab's length or its positive projection length.
2758
2759
Vector2 cC_origin = r_c.get_center() - p_rect.get_center();
2760
Vector2 cC = cC_origin.abs(); // Converted to fall in the first quadrant of c.
2761
2762
Vector2 ab = cC - 0.5 * r_c.get_size() - 0.5 * p_rect.get_size();
2763
2764
real_t min_d_squared = 0.0;
2765
if (ab.x > 0.0) {
2766
min_d_squared += ab.x * ab.x;
2767
}
2768
if (ab.y > 0.0) {
2769
min_d_squared += ab.y * ab.y;
2770
}
2771
2772
if (min_d_squared < r_closest_dist_squared || *r_closest == nullptr) {
2773
r_closest_dist_squared = min_d_squared;
2774
*r_closest = c;
2775
} else if (min_d_squared == r_closest_dist_squared) {
2776
// Tie-breaking aims to address situations where a potential focus neighbor's bounding rect
2777
// is right next to the currently focused control (e.g. in BoxContainer with
2778
// separation overridden to 0). This needs specific handling so that the correct
2779
// focus neighbor is selected.
2780
2781
Point2 p_center = p_rect.get_center();
2782
Control *closest = *r_closest;
2783
Point2 closest_center = closest->get_global_rect().get_center();
2784
2785
// Tie-break in favor of the control most aligned with p_dir.
2786
if (Math::abs(p_dir.cross(cC_origin)) < Math::abs(p_dir.cross(closest_center - p_center))) {
2787
*r_closest = c;
2788
}
2789
}
2790
}
2791
}
2792
2793
ScrollContainer *sc = Object::cast_to<ScrollContainer>(c);
2794
Rect2 intersection = p_clamp;
2795
if (sc) {
2796
if (!sc->is_following_focus() || !in_container) {
2797
intersection = p_clamp.intersection(sc->get_global_rect());
2798
if (!intersection.has_area()) {
2799
return;
2800
}
2801
}
2802
}
2803
2804
for (int i = 0; i < p_at->get_child_count(); i++) {
2805
Node *child = p_at->get_child(i);
2806
Control *childc = Object::cast_to<Control>(child);
2807
if (childc) {
2808
if (childc->data.RI) {
2809
continue; // Subwindow, ignore.
2810
}
2811
if (!childc->is_visible_in_tree()) {
2812
continue; // Skip invisible node trees.
2813
}
2814
if (Object::cast_to<ScrollContainer>(childc) && childc->is_ancestor_of(this)) {
2815
continue; // Already searched in it, skip it.
2816
}
2817
}
2818
_window_find_focus_neighbor(p_dir, child, p_rect, intersection, p_min, r_closest_dist_squared, r_closest);
2819
}
2820
}
2821
2822
// Rendering.
2823
2824
void Control::set_default_cursor_shape(CursorShape p_shape) {
2825
ERR_MAIN_THREAD_GUARD;
2826
ERR_FAIL_INDEX(int(p_shape), CURSOR_MAX);
2827
2828
if (data.default_cursor == p_shape) {
2829
return;
2830
}
2831
data.default_cursor = p_shape;
2832
2833
if (!is_inside_tree()) {
2834
return;
2835
}
2836
if (!get_global_rect().has_point(get_global_mouse_position())) {
2837
return;
2838
}
2839
2840
// Display the new cursor shape instantly.
2841
get_viewport()->update_mouse_cursor_state();
2842
}
2843
2844
Control::CursorShape Control::get_default_cursor_shape() const {
2845
ERR_READ_THREAD_GUARD_V(CURSOR_ARROW);
2846
return data.default_cursor;
2847
}
2848
2849
Control::CursorShape Control::get_cursor_shape(const Point2 &p_pos) const {
2850
ERR_READ_THREAD_GUARD_V(CURSOR_ARROW);
2851
return data.default_cursor;
2852
}
2853
2854
void Control::set_disable_visibility_clip(bool p_ignore) {
2855
ERR_MAIN_THREAD_GUARD;
2856
if (data.disable_visibility_clip == p_ignore) {
2857
return;
2858
}
2859
data.disable_visibility_clip = p_ignore;
2860
queue_redraw();
2861
}
2862
2863
bool Control::is_visibility_clip_disabled() const {
2864
ERR_READ_THREAD_GUARD_V(false);
2865
return data.disable_visibility_clip;
2866
}
2867
2868
void Control::set_clip_contents(bool p_clip) {
2869
ERR_MAIN_THREAD_GUARD;
2870
if (data.clip_contents == p_clip) {
2871
return;
2872
}
2873
data.clip_contents = p_clip;
2874
queue_redraw();
2875
}
2876
2877
bool Control::is_clipping_contents() {
2878
ERR_READ_THREAD_GUARD_V(false);
2879
return data.clip_contents;
2880
}
2881
2882
// Theming.
2883
2884
void Control::_theme_changed() {
2885
if (is_inside_tree()) {
2886
data.theme_owner->propagate_theme_changed(this, this, true, false);
2887
}
2888
}
2889
2890
void Control::_notify_theme_override_changed() {
2891
if (!data.bulk_theme_override && is_inside_tree()) {
2892
notification(NOTIFICATION_THEME_CHANGED);
2893
}
2894
}
2895
2896
void Control::_invalidate_theme_cache() {
2897
data.theme_icon_cache.clear();
2898
data.theme_style_cache.clear();
2899
data.theme_font_cache.clear();
2900
data.theme_font_size_cache.clear();
2901
data.theme_color_cache.clear();
2902
data.theme_constant_cache.clear();
2903
}
2904
2905
void Control::_update_theme_item_cache() {
2906
ThemeDB::get_singleton()->update_class_instance_items(this);
2907
}
2908
2909
void Control::set_theme_owner_node(Node *p_node) {
2910
ERR_MAIN_THREAD_GUARD;
2911
data.theme_owner->set_owner_node(p_node);
2912
}
2913
2914
Node *Control::get_theme_owner_node() const {
2915
ERR_READ_THREAD_GUARD_V(nullptr);
2916
return data.theme_owner->get_owner_node();
2917
}
2918
2919
bool Control::has_theme_owner_node() const {
2920
ERR_READ_THREAD_GUARD_V(false);
2921
return data.theme_owner->has_owner_node();
2922
}
2923
2924
void Control::set_theme_context(ThemeContext *p_context, bool p_propagate) {
2925
ERR_MAIN_THREAD_GUARD;
2926
data.theme_owner->set_owner_context(p_context, p_propagate);
2927
}
2928
2929
void Control::set_theme(const Ref<Theme> &p_theme) {
2930
ERR_MAIN_THREAD_GUARD;
2931
if (data.theme == p_theme) {
2932
return;
2933
}
2934
2935
if (data.theme.is_valid()) {
2936
data.theme->disconnect_changed(callable_mp(this, &Control::_theme_changed));
2937
}
2938
2939
data.theme = p_theme;
2940
if (data.theme.is_valid()) {
2941
data.theme_owner->propagate_theme_changed(this, this, is_inside_tree(), true);
2942
data.theme->connect_changed(callable_mp(this, &Control::_theme_changed), CONNECT_DEFERRED);
2943
return;
2944
}
2945
2946
Control *parent_c = Object::cast_to<Control>(get_parent());
2947
if (parent_c && parent_c->has_theme_owner_node()) {
2948
data.theme_owner->propagate_theme_changed(this, parent_c->get_theme_owner_node(), is_inside_tree(), true);
2949
return;
2950
}
2951
2952
Window *parent_w = cast_to<Window>(get_parent());
2953
if (parent_w && parent_w->has_theme_owner_node()) {
2954
data.theme_owner->propagate_theme_changed(this, parent_w->get_theme_owner_node(), is_inside_tree(), true);
2955
return;
2956
}
2957
2958
data.theme_owner->propagate_theme_changed(this, nullptr, is_inside_tree(), true);
2959
}
2960
2961
Ref<Theme> Control::get_theme() const {
2962
ERR_READ_THREAD_GUARD_V(Ref<Theme>());
2963
return data.theme;
2964
}
2965
2966
void Control::set_theme_type_variation(const StringName &p_theme_type) {
2967
ERR_MAIN_THREAD_GUARD;
2968
if (data.theme_type_variation == p_theme_type) {
2969
return;
2970
}
2971
data.theme_type_variation = p_theme_type;
2972
if (is_inside_tree()) {
2973
notification(NOTIFICATION_THEME_CHANGED);
2974
}
2975
}
2976
2977
StringName Control::get_theme_type_variation() const {
2978
ERR_READ_THREAD_GUARD_V(StringName());
2979
return data.theme_type_variation;
2980
}
2981
2982
/// Theme property lookup.
2983
2984
Ref<Texture2D> Control::get_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
2985
ERR_READ_THREAD_GUARD_V(Ref<Texture2D>());
2986
if (!data.initialized) {
2987
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2988
}
2989
2990
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
2991
const Ref<Texture2D> *tex = data.theme_icon_override.getptr(p_name);
2992
if (tex) {
2993
return *tex;
2994
}
2995
}
2996
2997
if (data.theme_icon_cache.has(p_theme_type) && data.theme_icon_cache[p_theme_type].has(p_name)) {
2998
return data.theme_icon_cache[p_theme_type][p_name];
2999
}
3000
3001
Vector<StringName> theme_types;
3002
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
3003
Ref<Texture2D> icon = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
3004
data.theme_icon_cache[p_theme_type][p_name] = icon;
3005
return icon;
3006
}
3007
3008
Ref<StyleBox> Control::get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
3009
ERR_READ_THREAD_GUARD_V(Ref<StyleBox>());
3010
if (!data.initialized) {
3011
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
3012
}
3013
3014
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
3015
const Ref<StyleBox> *style = data.theme_style_override.getptr(p_name);
3016
if (style) {
3017
return *style;
3018
}
3019
}
3020
3021
if (data.theme_style_cache.has(p_theme_type) && data.theme_style_cache[p_theme_type].has(p_name)) {
3022
return data.theme_style_cache[p_theme_type][p_name];
3023
}
3024
3025
Vector<StringName> theme_types;
3026
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
3027
Ref<StyleBox> style = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
3028
data.theme_style_cache[p_theme_type][p_name] = style;
3029
return style;
3030
}
3031
3032
Ref<Font> Control::get_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
3033
ERR_READ_THREAD_GUARD_V(Ref<Font>());
3034
if (!data.initialized) {
3035
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
3036
}
3037
3038
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
3039
const Ref<Font> *font = data.theme_font_override.getptr(p_name);
3040
if (font) {
3041
return *font;
3042
}
3043
}
3044
3045
if (data.theme_font_cache.has(p_theme_type) && data.theme_font_cache[p_theme_type].has(p_name)) {
3046
return data.theme_font_cache[p_theme_type][p_name];
3047
}
3048
3049
Vector<StringName> theme_types;
3050
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
3051
Ref<Font> font = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
3052
data.theme_font_cache[p_theme_type][p_name] = font;
3053
return font;
3054
}
3055
3056
int Control::get_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
3057
ERR_READ_THREAD_GUARD_V(0);
3058
if (!data.initialized) {
3059
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
3060
}
3061
3062
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
3063
const int *font_size = data.theme_font_size_override.getptr(p_name);
3064
if (font_size && (*font_size) > 0) {
3065
return *font_size;
3066
}
3067
}
3068
3069
if (data.theme_font_size_cache.has(p_theme_type) && data.theme_font_size_cache[p_theme_type].has(p_name)) {
3070
return data.theme_font_size_cache[p_theme_type][p_name];
3071
}
3072
3073
Vector<StringName> theme_types;
3074
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
3075
int font_size = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
3076
data.theme_font_size_cache[p_theme_type][p_name] = font_size;
3077
return font_size;
3078
}
3079
3080
Color Control::get_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
3081
ERR_READ_THREAD_GUARD_V(Color());
3082
if (!data.initialized) {
3083
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
3084
}
3085
3086
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
3087
const Color *color = data.theme_color_override.getptr(p_name);
3088
if (color) {
3089
return *color;
3090
}
3091
}
3092
3093
if (data.theme_color_cache.has(p_theme_type) && data.theme_color_cache[p_theme_type].has(p_name)) {
3094
return data.theme_color_cache[p_theme_type][p_name];
3095
}
3096
3097
Vector<StringName> theme_types;
3098
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
3099
Color color = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
3100
data.theme_color_cache[p_theme_type][p_name] = color;
3101
return color;
3102
}
3103
3104
int Control::get_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
3105
ERR_READ_THREAD_GUARD_V(0);
3106
if (!data.initialized) {
3107
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
3108
}
3109
3110
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
3111
const int *constant = data.theme_constant_override.getptr(p_name);
3112
if (constant) {
3113
return *constant;
3114
}
3115
}
3116
3117
if (data.theme_constant_cache.has(p_theme_type) && data.theme_constant_cache[p_theme_type].has(p_name)) {
3118
return data.theme_constant_cache[p_theme_type][p_name];
3119
}
3120
3121
Vector<StringName> theme_types;
3122
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
3123
int constant = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
3124
data.theme_constant_cache[p_theme_type][p_name] = constant;
3125
return constant;
3126
}
3127
3128
Variant Control::get_theme_item(Theme::DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
3129
switch (p_data_type) {
3130
case Theme::DATA_TYPE_COLOR:
3131
return get_theme_color(p_name, p_theme_type);
3132
case Theme::DATA_TYPE_CONSTANT:
3133
return get_theme_constant(p_name, p_theme_type);
3134
case Theme::DATA_TYPE_FONT:
3135
return get_theme_font(p_name, p_theme_type);
3136
case Theme::DATA_TYPE_FONT_SIZE:
3137
return get_theme_font_size(p_name, p_theme_type);
3138
case Theme::DATA_TYPE_ICON:
3139
return get_theme_icon(p_name, p_theme_type);
3140
case Theme::DATA_TYPE_STYLEBOX:
3141
return get_theme_stylebox(p_name, p_theme_type);
3142
case Theme::DATA_TYPE_MAX:
3143
break; // Can't happen, but silences warning.
3144
}
3145
3146
return Variant();
3147
}
3148
3149
Variant Control::get_used_theme_item(const String &p_full_name, const StringName &p_theme_type) const {
3150
if (p_full_name.begins_with("theme_override_icons/")) {
3151
String name = p_full_name.substr(strlen("theme_override_icons/"));
3152
if (has_theme_icon(name)) { // Exclude cached and default ones.
3153
return get_theme_icon(name);
3154
}
3155
} else if (p_full_name.begins_with("theme_override_styles/")) {
3156
String name = p_full_name.substr(strlen("theme_override_styles/"));
3157
if (has_theme_stylebox(name)) {
3158
return get_theme_stylebox(name);
3159
}
3160
} else if (p_full_name.begins_with("theme_override_fonts/")) {
3161
String name = p_full_name.substr(strlen("theme_override_fonts/"));
3162
if (has_theme_font(name)) {
3163
return get_theme_font(name);
3164
}
3165
} else if (p_full_name.begins_with("theme_override_font_sizes/")) {
3166
String name = p_full_name.substr(strlen("theme_override_font_sizes/"));
3167
if (has_theme_font_size(name)) {
3168
return get_theme_font_size(name);
3169
}
3170
} else if (p_full_name.begins_with("theme_override_colors/")) {
3171
String name = p_full_name.substr(strlen("theme_override_colors/"));
3172
if (has_theme_color(name)) {
3173
return get_theme_color(name);
3174
}
3175
} else if (p_full_name.begins_with("theme_override_constants/")) {
3176
String name = p_full_name.substr(strlen("theme_override_constants/"));
3177
if (has_theme_constant(name)) {
3178
return get_theme_constant(name);
3179
}
3180
} else {
3181
ERR_FAIL_V_MSG(Variant(), vformat("The property %s is not a theme item.", p_full_name));
3182
}
3183
3184
return Variant();
3185
}
3186
3187
#ifdef TOOLS_ENABLED
3188
Ref<Texture2D> Control::get_editor_theme_icon(const StringName &p_name) const {
3189
return get_theme_icon(p_name, SNAME("EditorIcons"));
3190
}
3191
#endif // TOOLS_ENABLED
3192
3193
bool Control::has_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
3194
ERR_READ_THREAD_GUARD_V(false);
3195
if (!data.initialized) {
3196
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
3197
}
3198
3199
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
3200
if (has_theme_icon_override(p_name)) {
3201
return true;
3202
}
3203
}
3204
3205
Vector<StringName> theme_types;
3206
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
3207
return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
3208
}
3209
3210
bool Control::has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
3211
ERR_READ_THREAD_GUARD_V(false);
3212
if (!data.initialized) {
3213
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
3214
}
3215
3216
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
3217
if (has_theme_stylebox_override(p_name)) {
3218
return true;
3219
}
3220
}
3221
3222
Vector<StringName> theme_types;
3223
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
3224
return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
3225
}
3226
3227
bool Control::has_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
3228
ERR_READ_THREAD_GUARD_V(false);
3229
if (!data.initialized) {
3230
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
3231
}
3232
3233
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
3234
if (has_theme_font_override(p_name)) {
3235
return true;
3236
}
3237
}
3238
3239
Vector<StringName> theme_types;
3240
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
3241
return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
3242
}
3243
3244
bool Control::has_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
3245
ERR_READ_THREAD_GUARD_V(false);
3246
if (!data.initialized) {
3247
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
3248
}
3249
3250
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
3251
if (has_theme_font_size_override(p_name)) {
3252
return true;
3253
}
3254
}
3255
3256
Vector<StringName> theme_types;
3257
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
3258
return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
3259
}
3260
3261
bool Control::has_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
3262
ERR_READ_THREAD_GUARD_V(false);
3263
if (!data.initialized) {
3264
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
3265
}
3266
3267
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
3268
if (has_theme_color_override(p_name)) {
3269
return true;
3270
}
3271
}
3272
3273
Vector<StringName> theme_types;
3274
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
3275
return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
3276
}
3277
3278
bool Control::has_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
3279
ERR_READ_THREAD_GUARD_V(false);
3280
if (!data.initialized) {
3281
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
3282
}
3283
3284
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
3285
if (has_theme_constant_override(p_name)) {
3286
return true;
3287
}
3288
}
3289
3290
Vector<StringName> theme_types;
3291
data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
3292
return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
3293
}
3294
3295
/// Local property overrides.
3296
3297
void Control::add_theme_icon_override(const StringName &p_name, const Ref<Texture2D> &p_icon) {
3298
ERR_MAIN_THREAD_GUARD;
3299
ERR_FAIL_COND(p_icon.is_null());
3300
3301
if (data.theme_icon_override.has(p_name)) {
3302
data.theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
3303
}
3304
3305
data.theme_icon_override[p_name] = p_icon;
3306
data.theme_icon_override[p_name]->connect_changed(callable_mp(this, &Control::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
3307
_notify_theme_override_changed();
3308
}
3309
3310
void Control::add_theme_style_override(const StringName &p_name, const Ref<StyleBox> &p_style) {
3311
ERR_MAIN_THREAD_GUARD;
3312
ERR_FAIL_COND(p_style.is_null());
3313
3314
if (data.theme_style_override.has(p_name)) {
3315
data.theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
3316
}
3317
3318
data.theme_style_override[p_name] = p_style;
3319
data.theme_style_override[p_name]->connect_changed(callable_mp(this, &Control::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
3320
_notify_theme_override_changed();
3321
}
3322
3323
void Control::add_theme_font_override(const StringName &p_name, const Ref<Font> &p_font) {
3324
ERR_MAIN_THREAD_GUARD;
3325
ERR_FAIL_COND(p_font.is_null());
3326
3327
if (data.theme_font_override.has(p_name)) {
3328
data.theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
3329
}
3330
3331
data.theme_font_override[p_name] = p_font;
3332
data.theme_font_override[p_name]->connect_changed(callable_mp(this, &Control::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
3333
_notify_theme_override_changed();
3334
}
3335
3336
void Control::add_theme_font_size_override(const StringName &p_name, int p_font_size) {
3337
ERR_MAIN_THREAD_GUARD;
3338
data.theme_font_size_override[p_name] = p_font_size;
3339
_notify_theme_override_changed();
3340
}
3341
3342
void Control::add_theme_color_override(const StringName &p_name, const Color &p_color) {
3343
ERR_MAIN_THREAD_GUARD;
3344
data.theme_color_override[p_name] = p_color;
3345
_notify_theme_override_changed();
3346
}
3347
3348
void Control::add_theme_constant_override(const StringName &p_name, int p_constant) {
3349
ERR_MAIN_THREAD_GUARD;
3350
data.theme_constant_override[p_name] = p_constant;
3351
_notify_theme_override_changed();
3352
}
3353
3354
void Control::remove_theme_icon_override(const StringName &p_name) {
3355
ERR_MAIN_THREAD_GUARD;
3356
if (data.theme_icon_override.has(p_name)) {
3357
data.theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
3358
}
3359
3360
data.theme_icon_override.erase(p_name);
3361
_notify_theme_override_changed();
3362
}
3363
3364
void Control::remove_theme_style_override(const StringName &p_name) {
3365
ERR_MAIN_THREAD_GUARD;
3366
if (data.theme_style_override.has(p_name)) {
3367
data.theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
3368
}
3369
3370
data.theme_style_override.erase(p_name);
3371
_notify_theme_override_changed();
3372
}
3373
3374
void Control::remove_theme_font_override(const StringName &p_name) {
3375
ERR_MAIN_THREAD_GUARD;
3376
if (data.theme_font_override.has(p_name)) {
3377
data.theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
3378
}
3379
3380
data.theme_font_override.erase(p_name);
3381
_notify_theme_override_changed();
3382
}
3383
3384
void Control::remove_theme_font_size_override(const StringName &p_name) {
3385
ERR_MAIN_THREAD_GUARD;
3386
data.theme_font_size_override.erase(p_name);
3387
_notify_theme_override_changed();
3388
}
3389
3390
void Control::remove_theme_color_override(const StringName &p_name) {
3391
ERR_MAIN_THREAD_GUARD;
3392
data.theme_color_override.erase(p_name);
3393
_notify_theme_override_changed();
3394
}
3395
3396
void Control::remove_theme_constant_override(const StringName &p_name) {
3397
ERR_MAIN_THREAD_GUARD;
3398
data.theme_constant_override.erase(p_name);
3399
_notify_theme_override_changed();
3400
}
3401
3402
bool Control::has_theme_icon_override(const StringName &p_name) const {
3403
ERR_READ_THREAD_GUARD_V(false);
3404
const Ref<Texture2D> *tex = data.theme_icon_override.getptr(p_name);
3405
return tex != nullptr;
3406
}
3407
3408
bool Control::has_theme_stylebox_override(const StringName &p_name) const {
3409
ERR_READ_THREAD_GUARD_V(false);
3410
const Ref<StyleBox> *style = data.theme_style_override.getptr(p_name);
3411
return style != nullptr;
3412
}
3413
3414
bool Control::has_theme_font_override(const StringName &p_name) const {
3415
ERR_READ_THREAD_GUARD_V(false);
3416
const Ref<Font> *font = data.theme_font_override.getptr(p_name);
3417
return font != nullptr;
3418
}
3419
3420
bool Control::has_theme_font_size_override(const StringName &p_name) const {
3421
ERR_READ_THREAD_GUARD_V(false);
3422
const int *font_size = data.theme_font_size_override.getptr(p_name);
3423
return font_size != nullptr;
3424
}
3425
3426
bool Control::has_theme_color_override(const StringName &p_name) const {
3427
ERR_READ_THREAD_GUARD_V(false);
3428
const Color *color = data.theme_color_override.getptr(p_name);
3429
return color != nullptr;
3430
}
3431
3432
bool Control::has_theme_constant_override(const StringName &p_name) const {
3433
ERR_READ_THREAD_GUARD_V(false);
3434
const int *constant = data.theme_constant_override.getptr(p_name);
3435
return constant != nullptr;
3436
}
3437
3438
/// Default theme properties.
3439
3440
float Control::get_theme_default_base_scale() const {
3441
ERR_READ_THREAD_GUARD_V(0);
3442
return data.theme_owner->get_theme_default_base_scale();
3443
}
3444
3445
Ref<Font> Control::get_theme_default_font() const {
3446
ERR_READ_THREAD_GUARD_V(Ref<Font>());
3447
return data.theme_owner->get_theme_default_font();
3448
}
3449
3450
int Control::get_theme_default_font_size() const {
3451
ERR_READ_THREAD_GUARD_V(0);
3452
return data.theme_owner->get_theme_default_font_size();
3453
}
3454
3455
/// Bulk actions.
3456
3457
void Control::begin_bulk_theme_override() {
3458
ERR_MAIN_THREAD_GUARD;
3459
data.bulk_theme_override = true;
3460
}
3461
3462
void Control::end_bulk_theme_override() {
3463
ERR_MAIN_THREAD_GUARD;
3464
ERR_FAIL_COND(!data.bulk_theme_override);
3465
3466
data.bulk_theme_override = false;
3467
_notify_theme_override_changed();
3468
}
3469
3470
// Internationalization.
3471
3472
TypedArray<Vector3i> Control::structured_text_parser(TextServer::StructuredTextParser p_parser_type, const Array &p_args, const String &p_text) const {
3473
ERR_READ_THREAD_GUARD_V(TypedArray<Vector3i>());
3474
if (p_parser_type == TextServer::STRUCTURED_TEXT_CUSTOM) {
3475
TypedArray<Vector3i> ret;
3476
GDVIRTUAL_CALL(_structured_text_parser, p_args, p_text, ret);
3477
return ret;
3478
} else {
3479
return TS->parse_structured_text(p_parser_type, p_args, p_text);
3480
}
3481
}
3482
3483
void Control::set_layout_direction(Control::LayoutDirection p_direction) {
3484
ERR_MAIN_THREAD_GUARD;
3485
if (data.layout_dir == p_direction) {
3486
return;
3487
}
3488
ERR_FAIL_INDEX(p_direction, LAYOUT_DIRECTION_MAX);
3489
3490
data.layout_dir = p_direction;
3491
3492
propagate_notification(NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
3493
}
3494
3495
Control::LayoutDirection Control::get_layout_direction() const {
3496
ERR_READ_THREAD_GUARD_V(LAYOUT_DIRECTION_INHERITED);
3497
return data.layout_dir;
3498
}
3499
3500
bool Control::is_layout_rtl() const {
3501
ERR_READ_THREAD_GUARD_V(false);
3502
if (data.is_rtl_dirty) {
3503
data.is_rtl_dirty = false;
3504
if (data.layout_dir == LAYOUT_DIRECTION_INHERITED) {
3505
#ifdef TOOLS_ENABLED
3506
if (is_part_of_edited_scene() && GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
3507
data.is_rtl = true;
3508
return data.is_rtl;
3509
}
3510
if (is_inside_tree()) {
3511
Node *edited_scene_root = get_tree()->get_edited_scene_root();
3512
if (edited_scene_root == this) {
3513
int proj_root_layout_direction = GLOBAL_GET_CACHED(int, "internationalization/rendering/root_node_layout_direction");
3514
if (proj_root_layout_direction == 1) {
3515
data.is_rtl = false;
3516
} else if (proj_root_layout_direction == 2) {
3517
data.is_rtl = true;
3518
} else if (proj_root_layout_direction == 3) {
3519
String locale = OS::get_singleton()->get_locale();
3520
data.is_rtl = TS->is_locale_right_to_left(locale);
3521
} else {
3522
const Ref<Translation> &t = TranslationServer::get_singleton()->get_translation_object(TranslationServer::get_singleton()->get_locale());
3523
String locale = t.is_valid() ? t->get_locale() : TranslationServer::get_singleton()->get_fallback_locale();
3524
data.is_rtl = TS->is_locale_right_to_left(locale);
3525
}
3526
return data.is_rtl;
3527
}
3528
}
3529
#else
3530
if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
3531
data.is_rtl = true;
3532
return data.is_rtl;
3533
}
3534
#endif // TOOLS_ENABLED
3535
Node *parent_node = get_parent();
3536
while (parent_node) {
3537
Control *parent_control = Object::cast_to<Control>(parent_node);
3538
if (parent_control) {
3539
data.is_rtl = parent_control->is_layout_rtl();
3540
return data.is_rtl;
3541
}
3542
3543
Window *parent_window = Object::cast_to<Window>(parent_node);
3544
if (parent_window) {
3545
data.is_rtl = parent_window->is_layout_rtl();
3546
return data.is_rtl;
3547
}
3548
parent_node = parent_node->get_parent();
3549
}
3550
3551
if (root_layout_direction == 1) {
3552
data.is_rtl = false;
3553
} else if (root_layout_direction == 2) {
3554
data.is_rtl = true;
3555
} else if (root_layout_direction == 3) {
3556
String locale = OS::get_singleton()->get_locale();
3557
data.is_rtl = TS->is_locale_right_to_left(locale);
3558
} else {
3559
String locale = TranslationServer::get_singleton()->get_tool_locale();
3560
data.is_rtl = TS->is_locale_right_to_left(locale);
3561
}
3562
} else if (data.layout_dir == LAYOUT_DIRECTION_APPLICATION_LOCALE) {
3563
if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
3564
data.is_rtl = true;
3565
} else {
3566
String locale = TranslationServer::get_singleton()->get_tool_locale();
3567
data.is_rtl = TS->is_locale_right_to_left(locale);
3568
}
3569
} else if (data.layout_dir == LAYOUT_DIRECTION_SYSTEM_LOCALE) {
3570
if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
3571
const_cast<Control *>(this)->data.is_rtl = true;
3572
} else {
3573
String locale = OS::get_singleton()->get_locale();
3574
const_cast<Control *>(this)->data.is_rtl = TS->is_locale_right_to_left(locale);
3575
}
3576
} else {
3577
data.is_rtl = (data.layout_dir == LAYOUT_DIRECTION_RTL);
3578
}
3579
}
3580
return data.is_rtl;
3581
}
3582
3583
void Control::set_localize_numeral_system(bool p_enable) {
3584
ERR_MAIN_THREAD_GUARD;
3585
if (p_enable == data.localize_numeral_system) {
3586
return;
3587
}
3588
3589
data.localize_numeral_system = p_enable;
3590
3591
notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
3592
}
3593
3594
bool Control::is_localizing_numeral_system() const {
3595
ERR_READ_THREAD_GUARD_V(false);
3596
return data.localize_numeral_system;
3597
}
3598
3599
#ifndef DISABLE_DEPRECATED
3600
void Control::set_auto_translate(bool p_enable) {
3601
ERR_MAIN_THREAD_GUARD;
3602
set_auto_translate_mode(p_enable ? AUTO_TRANSLATE_MODE_ALWAYS : AUTO_TRANSLATE_MODE_DISABLED);
3603
}
3604
3605
bool Control::is_auto_translating() const {
3606
ERR_READ_THREAD_GUARD_V(false);
3607
return can_auto_translate();
3608
}
3609
#endif // DISABLE_DEPRECATED
3610
3611
void Control::set_tooltip_auto_translate_mode(AutoTranslateMode p_mode) {
3612
ERR_MAIN_THREAD_GUARD;
3613
data.tooltip_auto_translate_mode = p_mode;
3614
}
3615
3616
Node::AutoTranslateMode Control::get_tooltip_auto_translate_mode() const {
3617
ERR_READ_THREAD_GUARD_V(AUTO_TRANSLATE_MODE_INHERIT);
3618
return data.tooltip_auto_translate_mode;
3619
}
3620
3621
// Extra properties.
3622
3623
void Control::set_tooltip_text(const String &p_hint) {
3624
ERR_MAIN_THREAD_GUARD;
3625
data.tooltip = p_hint;
3626
update_configuration_warnings();
3627
}
3628
3629
String Control::get_tooltip_text() const {
3630
ERR_READ_THREAD_GUARD_V(String());
3631
return data.tooltip;
3632
}
3633
3634
String Control::get_tooltip(const Point2 &p_pos) const {
3635
ERR_READ_THREAD_GUARD_V(String());
3636
String ret;
3637
if (GDVIRTUAL_CALL(_get_tooltip, p_pos, ret)) {
3638
return ret;
3639
}
3640
return data.tooltip;
3641
}
3642
3643
String Control::accessibility_get_contextual_info() const {
3644
ERR_READ_THREAD_GUARD_V(String());
3645
String ret;
3646
GDVIRTUAL_CALL(_accessibility_get_contextual_info, ret);
3647
return ret;
3648
}
3649
3650
Control *Control::make_custom_tooltip(const String &p_text) const {
3651
ERR_READ_THREAD_GUARD_V(nullptr);
3652
Object *ret = nullptr;
3653
GDVIRTUAL_CALL(_make_custom_tooltip, p_text, ret);
3654
return Object::cast_to<Control>(ret);
3655
}
3656
3657
// Base object overrides.
3658
3659
void Control::_accessibility_action_foucs(const Variant &p_data) {
3660
grab_focus();
3661
}
3662
3663
void Control::_accessibility_action_blur(const Variant &p_data) {
3664
release_focus();
3665
}
3666
3667
void Control::_accessibility_action_show_tooltip(const Variant &p_data) {
3668
Viewport *vp = get_viewport();
3669
if (vp) {
3670
vp->show_tooltip(this);
3671
}
3672
}
3673
3674
void Control::_accessibility_action_hide_tooltip(const Variant &p_data) {
3675
Viewport *vp = get_viewport();
3676
if (vp) {
3677
vp->cancel_tooltip();
3678
}
3679
}
3680
3681
void Control::_accessibility_action_scroll_into_view(const Variant &p_data) {
3682
ScrollContainer *sc = Object::cast_to<ScrollContainer>(get_parent());
3683
if (sc) {
3684
sc->ensure_control_visible(this);
3685
}
3686
}
3687
3688
void Control::_notification(int p_notification) {
3689
ERR_MAIN_THREAD_GUARD;
3690
switch (p_notification) {
3691
#ifdef TOOLS_ENABLED
3692
case NOTIFICATION_EDITOR_PRE_SAVE: {
3693
saving = true;
3694
} break;
3695
case NOTIFICATION_EDITOR_POST_SAVE: {
3696
saving = false;
3697
} break;
3698
#endif // TOOLS_ENABLED
3699
3700
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
3701
RID ae = get_accessibility_element();
3702
ERR_FAIL_COND(ae.is_null());
3703
3704
// Base info.
3705
if (get_parent_control()) {
3706
String container_info = get_parent_control()->get_accessibility_container_name(this);
3707
DisplayServer::get_singleton()->accessibility_update_set_name(ae, container_info.is_empty() ? get_accessibility_name() : get_accessibility_name() + " " + container_info);
3708
} else {
3709
DisplayServer::get_singleton()->accessibility_update_set_name(ae, get_accessibility_name());
3710
}
3711
DisplayServer::get_singleton()->accessibility_update_set_description(ae, get_accessibility_description());
3712
DisplayServer::get_singleton()->accessibility_update_set_live(ae, get_accessibility_live());
3713
3714
DisplayServer::get_singleton()->accessibility_update_set_transform(ae, get_transform());
3715
DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, Rect2(Vector2(), data.size_cache));
3716
DisplayServer::get_singleton()->accessibility_update_set_tooltip(ae, data.tooltip);
3717
DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_CLIPS_CHILDREN, data.clip_contents);
3718
DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_TOUCH_PASSTHROUGH, data.mouse_filter == MOUSE_FILTER_PASS);
3719
3720
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &Control::_accessibility_action_foucs));
3721
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &Control::_accessibility_action_blur));
3722
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_TOOLTIP, callable_mp(this, &Control::_accessibility_action_show_tooltip));
3723
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_HIDE_TOOLTIP, callable_mp(this, &Control::_accessibility_action_hide_tooltip));
3724
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &Control::_accessibility_action_scroll_into_view));
3725
if (is_inside_tree() && get_viewport()->gui_is_dragging()) {
3726
if (can_drop_data(Vector2(Math::INF, Math::INF), get_viewport()->gui_get_drag_data())) {
3727
DisplayServer::get_singleton()->accessibility_update_set_extra_info(ae, vformat(RTR("%s can be dropped here. Use %s to drop, use %s to cancel."), get_viewport()->gui_get_drag_description(), InputMap::get_singleton()->get_action_description("ui_accessibility_drag_and_drop"), InputMap::get_singleton()->get_action_description("ui_cancel")));
3728
} else {
3729
DisplayServer::get_singleton()->accessibility_update_set_extra_info(ae, vformat(RTR("%s can not be dropped here. Use %s to cancel."), get_viewport()->gui_get_drag_description(), InputMap::get_singleton()->get_action_description("ui_cancel")));
3730
}
3731
}
3732
3733
// Related nodes.
3734
for (int i = 0; i < data.accessibility_controls_nodes.size(); i++) {
3735
const NodePath &np = data.accessibility_controls_nodes[i];
3736
if (!np.is_empty()) {
3737
Node *n = get_node(np);
3738
if (n && !n->is_part_of_edited_scene()) {
3739
DisplayServer::get_singleton()->accessibility_update_add_related_controls(ae, n->get_accessibility_element());
3740
}
3741
}
3742
}
3743
for (int i = 0; i < data.accessibility_described_by_nodes.size(); i++) {
3744
const NodePath &np = data.accessibility_described_by_nodes[i];
3745
if (!np.is_empty()) {
3746
Node *n = get_node(np);
3747
if (n && !n->is_part_of_edited_scene()) {
3748
DisplayServer::get_singleton()->accessibility_update_add_related_described_by(ae, n->get_accessibility_element());
3749
}
3750
}
3751
}
3752
for (int i = 0; i < data.accessibility_labeled_by_nodes.size(); i++) {
3753
const NodePath &np = data.accessibility_labeled_by_nodes[i];
3754
if (!np.is_empty()) {
3755
Node *n = get_node(np);
3756
if (n && !n->is_part_of_edited_scene()) {
3757
DisplayServer::get_singleton()->accessibility_update_add_related_labeled_by(ae, n->get_accessibility_element());
3758
}
3759
}
3760
}
3761
for (int i = 0; i < data.accessibility_flow_to_nodes.size(); i++) {
3762
const NodePath &np = data.accessibility_flow_to_nodes[i];
3763
if (!np.is_empty()) {
3764
Node *n = get_node(np);
3765
if (n && !n->is_part_of_edited_scene()) {
3766
DisplayServer::get_singleton()->accessibility_update_add_related_flow_to(ae, n->get_accessibility_element());
3767
}
3768
}
3769
}
3770
} break;
3771
3772
case NOTIFICATION_POSTINITIALIZE: {
3773
data.initialized = true;
3774
3775
_invalidate_theme_cache();
3776
_update_theme_item_cache();
3777
} break;
3778
3779
case NOTIFICATION_PARENTED: {
3780
Node *parent_node = get_parent();
3781
data.parent_control = Object::cast_to<Control>(parent_node);
3782
data.parent_window = Object::cast_to<Window>(parent_node);
3783
3784
data.theme_owner->assign_theme_on_parented(this);
3785
3786
_update_layout_mode();
3787
3788
_update_focus_behavior_recursive();
3789
_update_mouse_behavior_recursive();
3790
} break;
3791
3792
case NOTIFICATION_UNPARENTED: {
3793
data.parent_control = nullptr;
3794
data.parent_window = nullptr;
3795
3796
data.theme_owner->clear_theme_on_unparented(this);
3797
} break;
3798
3799
case NOTIFICATION_ENTER_TREE: {
3800
// Emits NOTIFICATION_THEME_CHANGED internally.
3801
set_theme_context(ThemeDB::get_singleton()->get_nearest_theme_context(this));
3802
} break;
3803
3804
case NOTIFICATION_POST_ENTER_TREE: {
3805
data.is_rtl_dirty = true;
3806
update_minimum_size();
3807
_size_changed();
3808
} break;
3809
3810
case NOTIFICATION_EXIT_TREE: {
3811
set_theme_context(nullptr, false);
3812
3813
release_focus();
3814
get_viewport()->_gui_remove_control(this);
3815
} break;
3816
3817
case NOTIFICATION_READY: {
3818
#ifdef DEBUG_ENABLED
3819
connect(SceneStringName(ready), callable_mp(this, &Control::_clear_size_warning), CONNECT_DEFERRED | CONNECT_ONE_SHOT);
3820
#endif // DEBUG_ENABLED
3821
} break;
3822
3823
case NOTIFICATION_ENTER_CANVAS: {
3824
data.is_rtl_dirty = true;
3825
3826
CanvasItem *node = this;
3827
bool has_parent_control = false;
3828
3829
while (!node->is_set_as_top_level()) {
3830
CanvasItem *parent = Object::cast_to<CanvasItem>(node->get_parent());
3831
if (!parent) {
3832
break;
3833
}
3834
3835
Control *parent_control = Object::cast_to<Control>(parent);
3836
if (parent_control) {
3837
has_parent_control = true;
3838
break;
3839
}
3840
3841
node = parent;
3842
}
3843
3844
if (has_parent_control) {
3845
// Do nothing, has a parent control.
3846
} else {
3847
// Is a regular root control or top_level.
3848
Viewport *viewport = get_viewport();
3849
ERR_FAIL_NULL(viewport);
3850
data.RI = viewport->_gui_add_root_control(this);
3851
3852
get_parent()->connect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::gui_set_root_order_dirty), CONNECT_REFERENCE_COUNTED);
3853
}
3854
3855
data.parent_canvas_item = get_parent_item();
3856
3857
if (data.parent_canvas_item) {
3858
data.parent_canvas_item->connect(SceneStringName(item_rect_changed), callable_mp(this, &Control::_size_changed));
3859
} else {
3860
// Connect viewport.
3861
Viewport *viewport = get_viewport();
3862
ERR_FAIL_NULL(viewport);
3863
viewport->connect("size_changed", callable_mp(this, &Control::_size_changed));
3864
}
3865
} break;
3866
3867
case NOTIFICATION_EXIT_CANVAS: {
3868
if (data.parent_canvas_item) {
3869
data.parent_canvas_item->disconnect(SceneStringName(item_rect_changed), callable_mp(this, &Control::_size_changed));
3870
data.parent_canvas_item = nullptr;
3871
} else {
3872
// Disconnect viewport.
3873
Viewport *viewport = get_viewport();
3874
ERR_FAIL_NULL(viewport);
3875
viewport->disconnect("size_changed", callable_mp(this, &Control::_size_changed));
3876
}
3877
3878
if (data.RI) {
3879
get_viewport()->_gui_remove_root_control(data.RI);
3880
data.RI = nullptr;
3881
get_parent()->disconnect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::gui_set_root_order_dirty));
3882
}
3883
3884
data.parent_canvas_item = nullptr;
3885
data.is_rtl_dirty = true;
3886
} break;
3887
3888
case NOTIFICATION_CHILD_ORDER_CHANGED: {
3889
// Some parents need to know the order of the children to draw (like TabContainer),
3890
// so we update them just in case.
3891
queue_redraw();
3892
} break;
3893
3894
case NOTIFICATION_RESIZED: {
3895
emit_signal(SceneStringName(resized));
3896
} break;
3897
3898
case NOTIFICATION_DRAW: {
3899
_update_canvas_item_transform();
3900
RenderingServer::get_singleton()->canvas_item_set_custom_rect(get_canvas_item(), !data.disable_visibility_clip, Rect2(Point2(), get_size()));
3901
RenderingServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), data.clip_contents);
3902
} break;
3903
3904
case NOTIFICATION_MOUSE_ENTER: {
3905
emit_signal(SceneStringName(mouse_entered));
3906
} break;
3907
3908
case NOTIFICATION_MOUSE_EXIT: {
3909
emit_signal(SceneStringName(mouse_exited));
3910
} break;
3911
3912
case NOTIFICATION_FOCUS_ENTER: {
3913
emit_signal(SceneStringName(focus_entered));
3914
queue_redraw();
3915
} break;
3916
3917
case NOTIFICATION_FOCUS_EXIT: {
3918
emit_signal(SceneStringName(focus_exited));
3919
queue_redraw();
3920
} break;
3921
3922
case NOTIFICATION_THEME_CHANGED: {
3923
emit_signal(SceneStringName(theme_changed));
3924
3925
_invalidate_theme_cache();
3926
_update_theme_item_cache();
3927
queue_redraw();
3928
3929
update_minimum_size();
3930
_size_changed();
3931
} break;
3932
3933
case NOTIFICATION_VISIBILITY_CHANGED: {
3934
if (!is_visible_in_tree()) {
3935
if (get_viewport() != nullptr) {
3936
get_viewport()->_gui_hide_control(this);
3937
}
3938
} else {
3939
update_minimum_size();
3940
_size_changed();
3941
}
3942
} break;
3943
3944
case NOTIFICATION_TRANSLATION_CHANGED:
3945
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
3946
if (is_inside_tree()) {
3947
data.is_rtl_dirty = true;
3948
3949
_invalidate_theme_cache();
3950
_update_theme_item_cache();
3951
queue_redraw();
3952
3953
update_minimum_size();
3954
_size_changed();
3955
}
3956
} break;
3957
}
3958
}
3959
3960
void Control::_bind_methods() {
3961
ClassDB::bind_method(D_METHOD("accept_event"), &Control::accept_event);
3962
ClassDB::bind_method(D_METHOD("get_minimum_size"), &Control::get_minimum_size);
3963
ClassDB::bind_method(D_METHOD("get_combined_minimum_size"), &Control::get_combined_minimum_size);
3964
3965
ClassDB::bind_method(D_METHOD("_set_layout_mode", "mode"), &Control::_set_layout_mode);
3966
ClassDB::bind_method(D_METHOD("_get_layout_mode"), &Control::_get_layout_mode);
3967
ClassDB::bind_method(D_METHOD("_set_anchors_layout_preset", "preset"), &Control::_set_anchors_layout_preset);
3968
ClassDB::bind_method(D_METHOD("_get_anchors_layout_preset"), &Control::_get_anchors_layout_preset);
3969
ClassDB::bind_method(D_METHOD("set_anchors_preset", "preset", "keep_offsets"), &Control::set_anchors_preset, DEFVAL(false));
3970
ClassDB::bind_method(D_METHOD("set_offsets_preset", "preset", "resize_mode", "margin"), &Control::set_offsets_preset, DEFVAL(PRESET_MODE_MINSIZE), DEFVAL(0));
3971
ClassDB::bind_method(D_METHOD("set_anchors_and_offsets_preset", "preset", "resize_mode", "margin"), &Control::set_anchors_and_offsets_preset, DEFVAL(PRESET_MODE_MINSIZE), DEFVAL(0));
3972
3973
ClassDB::bind_method(D_METHOD("_set_anchor", "side", "anchor"), &Control::_set_anchor);
3974
ClassDB::bind_method(D_METHOD("set_anchor", "side", "anchor", "keep_offset", "push_opposite_anchor"), &Control::set_anchor, DEFVAL(false), DEFVAL(true));
3975
ClassDB::bind_method(D_METHOD("get_anchor", "side"), &Control::get_anchor);
3976
ClassDB::bind_method(D_METHOD("set_offset", "side", "offset"), &Control::set_offset);
3977
ClassDB::bind_method(D_METHOD("get_offset", "offset"), &Control::get_offset);
3978
ClassDB::bind_method(D_METHOD("set_anchor_and_offset", "side", "anchor", "offset", "push_opposite_anchor"), &Control::set_anchor_and_offset, DEFVAL(false));
3979
3980
ClassDB::bind_method(D_METHOD("set_begin", "position"), &Control::set_begin);
3981
ClassDB::bind_method(D_METHOD("set_end", "position"), &Control::set_end);
3982
ClassDB::bind_method(D_METHOD("set_position", "position", "keep_offsets"), &Control::set_position, DEFVAL(false));
3983
ClassDB::bind_method(D_METHOD("_set_position", "position"), &Control::_set_position);
3984
ClassDB::bind_method(D_METHOD("set_size", "size", "keep_offsets"), &Control::set_size, DEFVAL(false));
3985
ClassDB::bind_method(D_METHOD("reset_size"), &Control::reset_size);
3986
ClassDB::bind_method(D_METHOD("_set_size", "size"), &Control::_set_size);
3987
ClassDB::bind_method(D_METHOD("set_custom_minimum_size", "size"), &Control::set_custom_minimum_size);
3988
ClassDB::bind_method(D_METHOD("set_global_position", "position", "keep_offsets"), &Control::set_global_position, DEFVAL(false));
3989
ClassDB::bind_method(D_METHOD("_set_global_position", "position"), &Control::_set_global_position);
3990
ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &Control::set_rotation);
3991
ClassDB::bind_method(D_METHOD("set_rotation_degrees", "degrees"), &Control::set_rotation_degrees);
3992
ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Control::set_scale);
3993
ClassDB::bind_method(D_METHOD("set_pivot_offset", "pivot_offset"), &Control::set_pivot_offset);
3994
ClassDB::bind_method(D_METHOD("get_begin"), &Control::get_begin);
3995
ClassDB::bind_method(D_METHOD("get_end"), &Control::get_end);
3996
ClassDB::bind_method(D_METHOD("get_position"), &Control::get_position);
3997
ClassDB::bind_method(D_METHOD("get_size"), &Control::get_size);
3998
ClassDB::bind_method(D_METHOD("get_rotation"), &Control::get_rotation);
3999
ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Control::get_rotation_degrees);
4000
ClassDB::bind_method(D_METHOD("get_scale"), &Control::get_scale);
4001
ClassDB::bind_method(D_METHOD("get_pivot_offset"), &Control::get_pivot_offset);
4002
ClassDB::bind_method(D_METHOD("get_custom_minimum_size"), &Control::get_custom_minimum_size);
4003
ClassDB::bind_method(D_METHOD("get_parent_area_size"), &Control::get_parent_area_size);
4004
ClassDB::bind_method(D_METHOD("get_global_position"), &Control::get_global_position);
4005
ClassDB::bind_method(D_METHOD("get_screen_position"), &Control::get_screen_position);
4006
ClassDB::bind_method(D_METHOD("get_rect"), &Control::get_rect);
4007
ClassDB::bind_method(D_METHOD("get_global_rect"), &Control::get_global_rect);
4008
ClassDB::bind_method(D_METHOD("set_focus_mode", "mode"), &Control::set_focus_mode);
4009
ClassDB::bind_method(D_METHOD("get_focus_mode"), &Control::get_focus_mode);
4010
ClassDB::bind_method(D_METHOD("get_focus_mode_with_override"), &Control::get_focus_mode_with_override);
4011
ClassDB::bind_method(D_METHOD("set_focus_behavior_recursive", "focus_behavior_recursive"), &Control::set_focus_behavior_recursive);
4012
ClassDB::bind_method(D_METHOD("get_focus_behavior_recursive"), &Control::get_focus_behavior_recursive);
4013
ClassDB::bind_method(D_METHOD("has_focus"), &Control::has_focus);
4014
ClassDB::bind_method(D_METHOD("grab_focus"), &Control::grab_focus);
4015
ClassDB::bind_method(D_METHOD("release_focus"), &Control::release_focus);
4016
ClassDB::bind_method(D_METHOD("find_prev_valid_focus"), &Control::find_prev_valid_focus);
4017
ClassDB::bind_method(D_METHOD("find_next_valid_focus"), &Control::find_next_valid_focus);
4018
ClassDB::bind_method(D_METHOD("find_valid_focus_neighbor", "side"), &Control::find_valid_focus_neighbor);
4019
4020
ClassDB::bind_method(D_METHOD("set_h_size_flags", "flags"), &Control::set_h_size_flags);
4021
ClassDB::bind_method(D_METHOD("get_h_size_flags"), &Control::get_h_size_flags);
4022
4023
ClassDB::bind_method(D_METHOD("set_stretch_ratio", "ratio"), &Control::set_stretch_ratio);
4024
ClassDB::bind_method(D_METHOD("get_stretch_ratio"), &Control::get_stretch_ratio);
4025
4026
ClassDB::bind_method(D_METHOD("set_v_size_flags", "flags"), &Control::set_v_size_flags);
4027
ClassDB::bind_method(D_METHOD("get_v_size_flags"), &Control::get_v_size_flags);
4028
4029
ClassDB::bind_method(D_METHOD("set_theme", "theme"), &Control::set_theme);
4030
ClassDB::bind_method(D_METHOD("get_theme"), &Control::get_theme);
4031
4032
ClassDB::bind_method(D_METHOD("set_theme_type_variation", "theme_type"), &Control::set_theme_type_variation);
4033
ClassDB::bind_method(D_METHOD("get_theme_type_variation"), &Control::get_theme_type_variation);
4034
4035
ClassDB::bind_method(D_METHOD("begin_bulk_theme_override"), &Control::begin_bulk_theme_override);
4036
ClassDB::bind_method(D_METHOD("end_bulk_theme_override"), &Control::end_bulk_theme_override);
4037
4038
ClassDB::bind_method(D_METHOD("add_theme_icon_override", "name", "texture"), &Control::add_theme_icon_override);
4039
ClassDB::bind_method(D_METHOD("add_theme_stylebox_override", "name", "stylebox"), &Control::add_theme_style_override);
4040
ClassDB::bind_method(D_METHOD("add_theme_font_override", "name", "font"), &Control::add_theme_font_override);
4041
ClassDB::bind_method(D_METHOD("add_theme_font_size_override", "name", "font_size"), &Control::add_theme_font_size_override);
4042
ClassDB::bind_method(D_METHOD("add_theme_color_override", "name", "color"), &Control::add_theme_color_override);
4043
ClassDB::bind_method(D_METHOD("add_theme_constant_override", "name", "constant"), &Control::add_theme_constant_override);
4044
4045
ClassDB::bind_method(D_METHOD("remove_theme_icon_override", "name"), &Control::remove_theme_icon_override);
4046
ClassDB::bind_method(D_METHOD("remove_theme_stylebox_override", "name"), &Control::remove_theme_style_override);
4047
ClassDB::bind_method(D_METHOD("remove_theme_font_override", "name"), &Control::remove_theme_font_override);
4048
ClassDB::bind_method(D_METHOD("remove_theme_font_size_override", "name"), &Control::remove_theme_font_size_override);
4049
ClassDB::bind_method(D_METHOD("remove_theme_color_override", "name"), &Control::remove_theme_color_override);
4050
ClassDB::bind_method(D_METHOD("remove_theme_constant_override", "name"), &Control::remove_theme_constant_override);
4051
4052
ClassDB::bind_method(D_METHOD("get_theme_icon", "name", "theme_type"), &Control::get_theme_icon, DEFVAL(StringName()));
4053
ClassDB::bind_method(D_METHOD("get_theme_stylebox", "name", "theme_type"), &Control::get_theme_stylebox, DEFVAL(StringName()));
4054
ClassDB::bind_method(D_METHOD("get_theme_font", "name", "theme_type"), &Control::get_theme_font, DEFVAL(StringName()));
4055
ClassDB::bind_method(D_METHOD("get_theme_font_size", "name", "theme_type"), &Control::get_theme_font_size, DEFVAL(StringName()));
4056
ClassDB::bind_method(D_METHOD("get_theme_color", "name", "theme_type"), &Control::get_theme_color, DEFVAL(StringName()));
4057
ClassDB::bind_method(D_METHOD("get_theme_constant", "name", "theme_type"), &Control::get_theme_constant, DEFVAL(StringName()));
4058
4059
ClassDB::bind_method(D_METHOD("has_theme_icon_override", "name"), &Control::has_theme_icon_override);
4060
ClassDB::bind_method(D_METHOD("has_theme_stylebox_override", "name"), &Control::has_theme_stylebox_override);
4061
ClassDB::bind_method(D_METHOD("has_theme_font_override", "name"), &Control::has_theme_font_override);
4062
ClassDB::bind_method(D_METHOD("has_theme_font_size_override", "name"), &Control::has_theme_font_size_override);
4063
ClassDB::bind_method(D_METHOD("has_theme_color_override", "name"), &Control::has_theme_color_override);
4064
ClassDB::bind_method(D_METHOD("has_theme_constant_override", "name"), &Control::has_theme_constant_override);
4065
4066
ClassDB::bind_method(D_METHOD("has_theme_icon", "name", "theme_type"), &Control::has_theme_icon, DEFVAL(StringName()));
4067
ClassDB::bind_method(D_METHOD("has_theme_stylebox", "name", "theme_type"), &Control::has_theme_stylebox, DEFVAL(StringName()));
4068
ClassDB::bind_method(D_METHOD("has_theme_font", "name", "theme_type"), &Control::has_theme_font, DEFVAL(StringName()));
4069
ClassDB::bind_method(D_METHOD("has_theme_font_size", "name", "theme_type"), &Control::has_theme_font_size, DEFVAL(StringName()));
4070
ClassDB::bind_method(D_METHOD("has_theme_color", "name", "theme_type"), &Control::has_theme_color, DEFVAL(StringName()));
4071
ClassDB::bind_method(D_METHOD("has_theme_constant", "name", "theme_type"), &Control::has_theme_constant, DEFVAL(StringName()));
4072
4073
ClassDB::bind_method(D_METHOD("get_theme_default_base_scale"), &Control::get_theme_default_base_scale);
4074
ClassDB::bind_method(D_METHOD("get_theme_default_font"), &Control::get_theme_default_font);
4075
ClassDB::bind_method(D_METHOD("get_theme_default_font_size"), &Control::get_theme_default_font_size);
4076
4077
ClassDB::bind_method(D_METHOD("get_parent_control"), &Control::get_parent_control);
4078
4079
ClassDB::bind_method(D_METHOD("set_h_grow_direction", "direction"), &Control::set_h_grow_direction);
4080
ClassDB::bind_method(D_METHOD("get_h_grow_direction"), &Control::get_h_grow_direction);
4081
4082
ClassDB::bind_method(D_METHOD("set_v_grow_direction", "direction"), &Control::set_v_grow_direction);
4083
ClassDB::bind_method(D_METHOD("get_v_grow_direction"), &Control::get_v_grow_direction);
4084
4085
ClassDB::bind_method(D_METHOD("set_tooltip_auto_translate_mode", "mode"), &Control::set_tooltip_auto_translate_mode);
4086
ClassDB::bind_method(D_METHOD("get_tooltip_auto_translate_mode"), &Control::get_tooltip_auto_translate_mode);
4087
ClassDB::bind_method(D_METHOD("set_tooltip_text", "hint"), &Control::set_tooltip_text);
4088
ClassDB::bind_method(D_METHOD("get_tooltip_text"), &Control::get_tooltip_text);
4089
ClassDB::bind_method(D_METHOD("get_tooltip", "at_position"), &Control::get_tooltip, DEFVAL(Point2()));
4090
4091
ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Control::set_default_cursor_shape);
4092
ClassDB::bind_method(D_METHOD("get_default_cursor_shape"), &Control::get_default_cursor_shape);
4093
ClassDB::bind_method(D_METHOD("get_cursor_shape", "position"), &Control::get_cursor_shape, DEFVAL(Point2()));
4094
4095
ClassDB::bind_method(D_METHOD("set_focus_neighbor", "side", "neighbor"), &Control::set_focus_neighbor);
4096
ClassDB::bind_method(D_METHOD("get_focus_neighbor", "side"), &Control::get_focus_neighbor);
4097
4098
ClassDB::bind_method(D_METHOD("set_focus_next", "next"), &Control::set_focus_next);
4099
ClassDB::bind_method(D_METHOD("get_focus_next"), &Control::get_focus_next);
4100
4101
ClassDB::bind_method(D_METHOD("set_focus_previous", "previous"), &Control::set_focus_previous);
4102
ClassDB::bind_method(D_METHOD("get_focus_previous"), &Control::get_focus_previous);
4103
4104
ClassDB::bind_method(D_METHOD("force_drag", "data", "preview"), &Control::force_drag);
4105
4106
ClassDB::bind_method(D_METHOD("accessibility_drag"), &Control::accessibility_drag);
4107
ClassDB::bind_method(D_METHOD("accessibility_drop"), &Control::accessibility_drop);
4108
4109
ClassDB::bind_method(D_METHOD("set_accessibility_name", "name"), &Control::set_accessibility_name);
4110
ClassDB::bind_method(D_METHOD("get_accessibility_name"), &Control::get_accessibility_name);
4111
ClassDB::bind_method(D_METHOD("set_accessibility_description", "description"), &Control::set_accessibility_description);
4112
ClassDB::bind_method(D_METHOD("get_accessibility_description"), &Control::get_accessibility_description);
4113
ClassDB::bind_method(D_METHOD("set_accessibility_live", "mode"), &Control::set_accessibility_live);
4114
ClassDB::bind_method(D_METHOD("get_accessibility_live"), &Control::get_accessibility_live);
4115
4116
ClassDB::bind_method(D_METHOD("set_accessibility_controls_nodes", "node_path"), &Control::set_accessibility_controls_nodes);
4117
ClassDB::bind_method(D_METHOD("get_accessibility_controls_nodes"), &Control::get_accessibility_controls_nodes);
4118
ClassDB::bind_method(D_METHOD("set_accessibility_described_by_nodes", "node_path"), &Control::set_accessibility_described_by_nodes);
4119
ClassDB::bind_method(D_METHOD("get_accessibility_described_by_nodes"), &Control::get_accessibility_described_by_nodes);
4120
ClassDB::bind_method(D_METHOD("set_accessibility_labeled_by_nodes", "node_path"), &Control::set_accessibility_labeled_by_nodes);
4121
ClassDB::bind_method(D_METHOD("get_accessibility_labeled_by_nodes"), &Control::get_accessibility_labeled_by_nodes);
4122
ClassDB::bind_method(D_METHOD("set_accessibility_flow_to_nodes", "node_path"), &Control::set_accessibility_flow_to_nodes);
4123
ClassDB::bind_method(D_METHOD("get_accessibility_flow_to_nodes"), &Control::get_accessibility_flow_to_nodes);
4124
4125
ClassDB::bind_method(D_METHOD("set_mouse_filter", "filter"), &Control::set_mouse_filter);
4126
ClassDB::bind_method(D_METHOD("get_mouse_filter"), &Control::get_mouse_filter);
4127
ClassDB::bind_method(D_METHOD("get_mouse_filter_with_override"), &Control::get_mouse_filter_with_override);
4128
4129
ClassDB::bind_method(D_METHOD("set_mouse_behavior_recursive", "mouse_behavior_recursive"), &Control::set_mouse_behavior_recursive);
4130
ClassDB::bind_method(D_METHOD("get_mouse_behavior_recursive"), &Control::get_mouse_behavior_recursive);
4131
4132
ClassDB::bind_method(D_METHOD("set_force_pass_scroll_events", "force_pass_scroll_events"), &Control::set_force_pass_scroll_events);
4133
ClassDB::bind_method(D_METHOD("is_force_pass_scroll_events"), &Control::is_force_pass_scroll_events);
4134
4135
ClassDB::bind_method(D_METHOD("set_clip_contents", "enable"), &Control::set_clip_contents);
4136
ClassDB::bind_method(D_METHOD("is_clipping_contents"), &Control::is_clipping_contents);
4137
4138
ClassDB::bind_method(D_METHOD("grab_click_focus"), &Control::grab_click_focus);
4139
4140
ClassDB::bind_method(D_METHOD("set_drag_forwarding", "drag_func", "can_drop_func", "drop_func"), &Control::set_drag_forwarding);
4141
ClassDB::bind_method(D_METHOD("set_drag_preview", "control"), &Control::set_drag_preview);
4142
ClassDB::bind_method(D_METHOD("is_drag_successful"), &Control::is_drag_successful);
4143
4144
ClassDB::bind_method(D_METHOD("warp_mouse", "position"), &Control::warp_mouse);
4145
4146
ClassDB::bind_method(D_METHOD("set_shortcut_context", "node"), &Control::set_shortcut_context);
4147
ClassDB::bind_method(D_METHOD("get_shortcut_context"), &Control::get_shortcut_context);
4148
4149
ClassDB::bind_method(D_METHOD("update_minimum_size"), &Control::update_minimum_size);
4150
4151
ClassDB::bind_method(D_METHOD("set_layout_direction", "direction"), &Control::set_layout_direction);
4152
ClassDB::bind_method(D_METHOD("get_layout_direction"), &Control::get_layout_direction);
4153
ClassDB::bind_method(D_METHOD("is_layout_rtl"), &Control::is_layout_rtl);
4154
4155
#ifndef DISABLE_DEPRECATED
4156
ClassDB::bind_method(D_METHOD("set_auto_translate", "enable"), &Control::set_auto_translate);
4157
ClassDB::bind_method(D_METHOD("is_auto_translating"), &Control::is_auto_translating);
4158
#endif // DISABLE_DEPRECATED
4159
4160
ClassDB::bind_method(D_METHOD("set_localize_numeral_system", "enable"), &Control::set_localize_numeral_system);
4161
ClassDB::bind_method(D_METHOD("is_localizing_numeral_system"), &Control::is_localizing_numeral_system);
4162
4163
ADD_GROUP("Layout", "");
4164
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_contents"), "set_clip_contents", "is_clipping_contents");
4165
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "custom_minimum_size", PROPERTY_HINT_NONE, "suffix:px"), "set_custom_minimum_size", "get_custom_minimum_size");
4166
ADD_PROPERTY(PropertyInfo(Variant::INT, "layout_direction", PROPERTY_HINT_ENUM, "Inherited,Based on Application Locale,Left-to-Right,Right-to-Left,Based on System Locale"), "set_layout_direction", "get_layout_direction");
4167
ADD_PROPERTY(PropertyInfo(Variant::INT, "layout_mode", PROPERTY_HINT_ENUM, "Position,Anchors,Container,Uncontrolled", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "_set_layout_mode", "_get_layout_mode");
4168
ADD_PROPERTY_DEFAULT("layout_mode", LayoutMode::LAYOUT_MODE_POSITION);
4169
4170
constexpr struct {
4171
const char *name;
4172
LayoutPreset value;
4173
} anchors_presets[] = {
4174
{ TTRC("Full Rect"), PRESET_FULL_RECT },
4175
{ TTRC("Top Left"), PRESET_TOP_LEFT },
4176
{ TTRC("Top Right"), PRESET_TOP_RIGHT },
4177
{ TTRC("Bottom Right"), PRESET_BOTTOM_RIGHT },
4178
{ TTRC("Bottom Left"), PRESET_BOTTOM_LEFT },
4179
{ TTRC("Center Left"), PRESET_CENTER_LEFT },
4180
{ TTRC("Center Top"), PRESET_CENTER_TOP },
4181
{ TTRC("Center Right"), PRESET_CENTER_RIGHT },
4182
{ TTRC("Center Bottom"), PRESET_CENTER_BOTTOM },
4183
{ TTRC("Center"), PRESET_CENTER },
4184
{ TTRC("Left Wide"), PRESET_LEFT_WIDE },
4185
{ TTRC("Top Wide"), PRESET_TOP_WIDE },
4186
{ TTRC("Right Wide"), PRESET_RIGHT_WIDE },
4187
{ TTRC("Bottom Wide"), PRESET_BOTTOM_WIDE },
4188
{ TTRC("VCenter Wide"), PRESET_VCENTER_WIDE },
4189
{ TTRC("HCenter Wide"), PRESET_HCENTER_WIDE },
4190
};
4191
StringBuilder builder;
4192
builder.append(TTRC("Custom"));
4193
builder.append(":-1");
4194
for (size_t i = 0; i < std::size(anchors_presets); i++) {
4195
builder.append(",");
4196
builder.append(anchors_presets[i].name);
4197
builder.append(":");
4198
builder.append(itos(anchors_presets[i].value));
4199
}
4200
const String anchors_presets_options = builder.as_string();
4201
4202
ADD_PROPERTY(PropertyInfo(Variant::INT, "anchors_preset", PROPERTY_HINT_ENUM, anchors_presets_options, PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "_set_anchors_layout_preset", "_get_anchors_layout_preset");
4203
ADD_PROPERTY_DEFAULT("anchors_preset", -1);
4204
4205
ADD_SUBGROUP_INDENT("Anchor Points", "anchor_", 1);
4206
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anchor_left", PROPERTY_HINT_RANGE, "0,1,0.001,or_less,or_greater"), "_set_anchor", "get_anchor", SIDE_LEFT);
4207
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anchor_top", PROPERTY_HINT_RANGE, "0,1,0.001,or_less,or_greater"), "_set_anchor", "get_anchor", SIDE_TOP);
4208
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anchor_right", PROPERTY_HINT_RANGE, "0,1,0.001,or_less,or_greater"), "_set_anchor", "get_anchor", SIDE_RIGHT);
4209
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anchor_bottom", PROPERTY_HINT_RANGE, "0,1,0.001,or_less,or_greater"), "_set_anchor", "get_anchor", SIDE_BOTTOM);
4210
4211
ADD_SUBGROUP_INDENT("Anchor Offsets", "offset_", 1);
4212
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "offset_left", PROPERTY_HINT_RANGE, "-4096,4096,1,or_less,or_greater,suffix:px"), "set_offset", "get_offset", SIDE_LEFT);
4213
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "offset_top", PROPERTY_HINT_RANGE, "-4096,4096,1,or_less,or_greater,suffix:px"), "set_offset", "get_offset", SIDE_TOP);
4214
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "offset_right", PROPERTY_HINT_RANGE, "-4096,4096,1,or_less,or_greater,suffix:px"), "set_offset", "get_offset", SIDE_RIGHT);
4215
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "offset_bottom", PROPERTY_HINT_RANGE, "-4096,4096,1,or_less,or_greater,suffix:px"), "set_offset", "get_offset", SIDE_BOTTOM);
4216
4217
ADD_SUBGROUP_INDENT("Grow Direction", "grow_", 1);
4218
ADD_PROPERTY(PropertyInfo(Variant::INT, "grow_horizontal", PROPERTY_HINT_ENUM, "Left,Right,Both"), "set_h_grow_direction", "get_h_grow_direction");
4219
ADD_PROPERTY(PropertyInfo(Variant::INT, "grow_vertical", PROPERTY_HINT_ENUM, "Top,Bottom,Both"), "set_v_grow_direction", "get_v_grow_direction");
4220
4221
ADD_SUBGROUP("Transform", "");
4222
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_EDITOR), "_set_size", "get_size");
4223
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_EDITOR), "_set_position", "get_position");
4224
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NONE), "_set_global_position", "get_global_position");
4225
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_less,or_greater,radians_as_degrees"), "set_rotation", "get_rotation");
4226
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_rotation_degrees", "get_rotation_degrees");
4227
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale"), "set_scale", "get_scale");
4228
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "pivot_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_pivot_offset", "get_pivot_offset");
4229
4230
ADD_SUBGROUP("Container Sizing", "size_flags_");
4231
ADD_PROPERTY(PropertyInfo(Variant::INT, "size_flags_horizontal", PROPERTY_HINT_FLAGS, "Fill:1,Expand:2,Shrink Center:4,Shrink End:8"), "set_h_size_flags", "get_h_size_flags");
4232
ADD_PROPERTY(PropertyInfo(Variant::INT, "size_flags_vertical", PROPERTY_HINT_FLAGS, "Fill:1,Expand:2,Shrink Center:4,Shrink End:8"), "set_v_size_flags", "get_v_size_flags");
4233
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size_flags_stretch_ratio", PROPERTY_HINT_RANGE, "0,20,0.01,or_greater"), "set_stretch_ratio", "get_stretch_ratio");
4234
4235
ADD_GROUP("Localization", "");
4236
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "localize_numeral_system"), "set_localize_numeral_system", "is_localizing_numeral_system");
4237
4238
#ifndef DISABLE_DEPRECATED
4239
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_translate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_auto_translate", "is_auto_translating");
4240
#endif // DISABLE_DEPRECATED
4241
4242
ADD_GROUP("Tooltip", "tooltip_");
4243
ADD_PROPERTY(PropertyInfo(Variant::STRING, "tooltip_text", PROPERTY_HINT_MULTILINE_TEXT), "set_tooltip_text", "get_tooltip_text");
4244
ADD_PROPERTY(PropertyInfo(Variant::INT, "tooltip_auto_translate_mode", PROPERTY_HINT_ENUM, "Inherit,Always,Disabled"), "set_tooltip_auto_translate_mode", "get_tooltip_auto_translate_mode");
4245
4246
ADD_GROUP("Focus", "focus_");
4247
ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_left", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_LEFT);
4248
ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_top", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_TOP);
4249
ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_right", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_RIGHT);
4250
ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_bottom", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_BOTTOM);
4251
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "focus_next", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_next", "get_focus_next");
4252
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "focus_previous", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_previous", "get_focus_previous");
4253
ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_mode", PROPERTY_HINT_ENUM, "None,Click,All,Accessibility"), "set_focus_mode", "get_focus_mode");
4254
ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_behavior_recursive", PROPERTY_HINT_ENUM, "Inherited,Disabled,Enabled"), "set_focus_behavior_recursive", "get_focus_behavior_recursive");
4255
4256
ADD_GROUP("Mouse", "mouse_");
4257
ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_filter", PROPERTY_HINT_ENUM, "Stop,Pass (Propagate Up),Ignore"), "set_mouse_filter", "get_mouse_filter");
4258
ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_behavior_recursive", PROPERTY_HINT_ENUM, "Inherited,Disabled,Enabled"), "set_mouse_behavior_recursive", "get_mouse_behavior_recursive");
4259
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mouse_force_pass_scroll_events"), "set_force_pass_scroll_events", "is_force_pass_scroll_events");
4260
ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_default_cursor_shape", PROPERTY_HINT_ENUM, "Arrow,I-Beam,Pointing Hand,Cross,Wait,Busy,Drag,Can Drop,Forbidden,Vertical Resize,Horizontal Resize,Secondary Diagonal Resize,Main Diagonal Resize,Move,Vertical Split,Horizontal Split,Help"), "set_default_cursor_shape", "get_default_cursor_shape");
4261
4262
ADD_GROUP("Input", "");
4263
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut_context", PROPERTY_HINT_NODE_TYPE, "Node"), "set_shortcut_context", "get_shortcut_context");
4264
4265
ADD_GROUP("Accessibility", "accessibility_");
4266
ADD_PROPERTY(PropertyInfo(Variant::STRING, "accessibility_name"), "set_accessibility_name", "get_accessibility_name");
4267
ADD_PROPERTY(PropertyInfo(Variant::STRING, "accessibility_description"), "set_accessibility_description", "get_accessibility_description");
4268
ADD_PROPERTY(PropertyInfo(Variant::INT, "accessibility_live", PROPERTY_HINT_ENUM, "Off,Polite,Assertive"), "set_accessibility_live", "get_accessibility_live");
4269
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "accessibility_controls_nodes", PROPERTY_HINT_ARRAY_TYPE, "NodePath"), "set_accessibility_controls_nodes", "get_accessibility_controls_nodes");
4270
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "accessibility_described_by_nodes", PROPERTY_HINT_ARRAY_TYPE, "NodePath"), "set_accessibility_described_by_nodes", "get_accessibility_described_by_nodes");
4271
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "accessibility_labeled_by_nodes", PROPERTY_HINT_ARRAY_TYPE, "NodePath"), "set_accessibility_labeled_by_nodes", "get_accessibility_labeled_by_nodes");
4272
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "accessibility_flow_to_nodes", PROPERTY_HINT_ARRAY_TYPE, "NodePath"), "set_accessibility_flow_to_nodes", "get_accessibility_flow_to_nodes");
4273
4274
ADD_GROUP("Theme", "theme_");
4275
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme");
4276
ADD_PROPERTY(PropertyInfo(Variant::STRING, "theme_type_variation", PROPERTY_HINT_ENUM_SUGGESTION), "set_theme_type_variation", "get_theme_type_variation");
4277
4278
BIND_ENUM_CONSTANT(FOCUS_NONE);
4279
BIND_ENUM_CONSTANT(FOCUS_CLICK);
4280
BIND_ENUM_CONSTANT(FOCUS_ALL);
4281
BIND_ENUM_CONSTANT(FOCUS_ACCESSIBILITY);
4282
4283
BIND_ENUM_CONSTANT(FOCUS_BEHAVIOR_INHERITED);
4284
BIND_ENUM_CONSTANT(FOCUS_BEHAVIOR_DISABLED);
4285
BIND_ENUM_CONSTANT(FOCUS_BEHAVIOR_ENABLED);
4286
4287
BIND_ENUM_CONSTANT(MOUSE_BEHAVIOR_INHERITED);
4288
BIND_ENUM_CONSTANT(MOUSE_BEHAVIOR_DISABLED);
4289
BIND_ENUM_CONSTANT(MOUSE_BEHAVIOR_ENABLED);
4290
4291
BIND_CONSTANT(NOTIFICATION_RESIZED);
4292
BIND_CONSTANT(NOTIFICATION_MOUSE_ENTER);
4293
BIND_CONSTANT(NOTIFICATION_MOUSE_EXIT);
4294
BIND_CONSTANT(NOTIFICATION_MOUSE_ENTER_SELF);
4295
BIND_CONSTANT(NOTIFICATION_MOUSE_EXIT_SELF);
4296
BIND_CONSTANT(NOTIFICATION_FOCUS_ENTER);
4297
BIND_CONSTANT(NOTIFICATION_FOCUS_EXIT);
4298
BIND_CONSTANT(NOTIFICATION_THEME_CHANGED);
4299
BIND_CONSTANT(NOTIFICATION_SCROLL_BEGIN);
4300
BIND_CONSTANT(NOTIFICATION_SCROLL_END);
4301
BIND_CONSTANT(NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
4302
4303
BIND_ENUM_CONSTANT(CURSOR_ARROW);
4304
BIND_ENUM_CONSTANT(CURSOR_IBEAM);
4305
BIND_ENUM_CONSTANT(CURSOR_POINTING_HAND);
4306
BIND_ENUM_CONSTANT(CURSOR_CROSS);
4307
BIND_ENUM_CONSTANT(CURSOR_WAIT);
4308
BIND_ENUM_CONSTANT(CURSOR_BUSY);
4309
BIND_ENUM_CONSTANT(CURSOR_DRAG);
4310
BIND_ENUM_CONSTANT(CURSOR_CAN_DROP);
4311
BIND_ENUM_CONSTANT(CURSOR_FORBIDDEN);
4312
BIND_ENUM_CONSTANT(CURSOR_VSIZE);
4313
BIND_ENUM_CONSTANT(CURSOR_HSIZE);
4314
BIND_ENUM_CONSTANT(CURSOR_BDIAGSIZE);
4315
BIND_ENUM_CONSTANT(CURSOR_FDIAGSIZE);
4316
BIND_ENUM_CONSTANT(CURSOR_MOVE);
4317
BIND_ENUM_CONSTANT(CURSOR_VSPLIT);
4318
BIND_ENUM_CONSTANT(CURSOR_HSPLIT);
4319
BIND_ENUM_CONSTANT(CURSOR_HELP);
4320
4321
BIND_ENUM_CONSTANT(PRESET_TOP_LEFT);
4322
BIND_ENUM_CONSTANT(PRESET_TOP_RIGHT);
4323
BIND_ENUM_CONSTANT(PRESET_BOTTOM_LEFT);
4324
BIND_ENUM_CONSTANT(PRESET_BOTTOM_RIGHT);
4325
BIND_ENUM_CONSTANT(PRESET_CENTER_LEFT);
4326
BIND_ENUM_CONSTANT(PRESET_CENTER_TOP);
4327
BIND_ENUM_CONSTANT(PRESET_CENTER_RIGHT);
4328
BIND_ENUM_CONSTANT(PRESET_CENTER_BOTTOM);
4329
BIND_ENUM_CONSTANT(PRESET_CENTER);
4330
BIND_ENUM_CONSTANT(PRESET_LEFT_WIDE);
4331
BIND_ENUM_CONSTANT(PRESET_TOP_WIDE);
4332
BIND_ENUM_CONSTANT(PRESET_RIGHT_WIDE);
4333
BIND_ENUM_CONSTANT(PRESET_BOTTOM_WIDE);
4334
BIND_ENUM_CONSTANT(PRESET_VCENTER_WIDE);
4335
BIND_ENUM_CONSTANT(PRESET_HCENTER_WIDE);
4336
BIND_ENUM_CONSTANT(PRESET_FULL_RECT);
4337
4338
BIND_ENUM_CONSTANT(PRESET_MODE_MINSIZE);
4339
BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_WIDTH);
4340
BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_HEIGHT);
4341
BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_SIZE);
4342
4343
BIND_BITFIELD_FLAG(SIZE_SHRINK_BEGIN);
4344
BIND_BITFIELD_FLAG(SIZE_FILL);
4345
BIND_BITFIELD_FLAG(SIZE_EXPAND);
4346
BIND_BITFIELD_FLAG(SIZE_EXPAND_FILL);
4347
BIND_BITFIELD_FLAG(SIZE_SHRINK_CENTER);
4348
BIND_BITFIELD_FLAG(SIZE_SHRINK_END);
4349
4350
BIND_ENUM_CONSTANT(MOUSE_FILTER_STOP);
4351
BIND_ENUM_CONSTANT(MOUSE_FILTER_PASS);
4352
BIND_ENUM_CONSTANT(MOUSE_FILTER_IGNORE);
4353
4354
BIND_ENUM_CONSTANT(GROW_DIRECTION_BEGIN);
4355
BIND_ENUM_CONSTANT(GROW_DIRECTION_END);
4356
BIND_ENUM_CONSTANT(GROW_DIRECTION_BOTH);
4357
4358
BIND_ENUM_CONSTANT(ANCHOR_BEGIN);
4359
BIND_ENUM_CONSTANT(ANCHOR_END);
4360
4361
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_INHERITED);
4362
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_APPLICATION_LOCALE);
4363
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LTR);
4364
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_RTL);
4365
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_SYSTEM_LOCALE);
4366
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_MAX);
4367
#ifndef DISABLE_DEPRECATED
4368
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LOCALE);
4369
#endif // DISABLE_DEPRECATED
4370
4371
BIND_ENUM_CONSTANT(TEXT_DIRECTION_INHERITED);
4372
BIND_ENUM_CONSTANT(TEXT_DIRECTION_AUTO);
4373
BIND_ENUM_CONSTANT(TEXT_DIRECTION_LTR);
4374
BIND_ENUM_CONSTANT(TEXT_DIRECTION_RTL);
4375
4376
ADD_SIGNAL(MethodInfo("resized"));
4377
ADD_SIGNAL(MethodInfo("gui_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
4378
ADD_SIGNAL(MethodInfo("mouse_entered"));
4379
ADD_SIGNAL(MethodInfo("mouse_exited"));
4380
ADD_SIGNAL(MethodInfo("focus_entered"));
4381
ADD_SIGNAL(MethodInfo("focus_exited"));
4382
ADD_SIGNAL(MethodInfo("size_flags_changed"));
4383
ADD_SIGNAL(MethodInfo("minimum_size_changed"));
4384
ADD_SIGNAL(MethodInfo("theme_changed"));
4385
4386
GDVIRTUAL_BIND(_has_point, "point");
4387
GDVIRTUAL_BIND(_structured_text_parser, "args", "text");
4388
GDVIRTUAL_BIND(_get_minimum_size);
4389
GDVIRTUAL_BIND(_get_tooltip, "at_position");
4390
4391
GDVIRTUAL_BIND(_get_drag_data, "at_position");
4392
GDVIRTUAL_BIND(_can_drop_data, "at_position", "data");
4393
GDVIRTUAL_BIND(_drop_data, "at_position", "data");
4394
GDVIRTUAL_BIND(_make_custom_tooltip, "for_text");
4395
4396
GDVIRTUAL_BIND(_accessibility_get_contextual_info);
4397
GDVIRTUAL_BIND(_get_accessibility_container_name, "node");
4398
4399
GDVIRTUAL_BIND(_gui_input, "event");
4400
}
4401
4402
Control::Control() {
4403
data.theme_owner = memnew(ThemeOwner(this));
4404
4405
set_physics_interpolation_mode(Node::PHYSICS_INTERPOLATION_MODE_OFF);
4406
}
4407
4408
Control::~Control() {
4409
memdelete(data.theme_owner);
4410
4411
// Resources need to be disconnected.
4412
for (KeyValue<StringName, Ref<Texture2D>> &E : data.theme_icon_override) {
4413
E.value->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
4414
}
4415
for (KeyValue<StringName, Ref<StyleBox>> &E : data.theme_style_override) {
4416
E.value->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
4417
}
4418
for (KeyValue<StringName, Ref<Font>> &E : data.theme_font_override) {
4419
E.value->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
4420
}
4421
4422
// Then override maps can be simply cleared.
4423
data.theme_icon_override.clear();
4424
data.theme_style_override.clear();
4425
data.theme_font_override.clear();
4426
data.theme_font_size_override.clear();
4427
data.theme_color_override.clear();
4428
data.theme_constant_override.clear();
4429
}
4430
4431