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