-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
/
control.cpp
3738 lines (3106 loc) · 126 KB
/
control.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* control.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "control.h"
#include "container.h"
#include "core/config/project_settings.h"
#include "core/math/geometry_2d.h"
#include "core/os/keyboard.h"
#include "core/os/os.h"
#include "core/string/print_string.h"
#include "core/string/translation.h"
#include "scene/gui/label.h"
#include "scene/gui/panel.h"
#include "scene/main/canvas_layer.h"
#include "scene/main/window.h"
#include "scene/scene_string_names.h"
#include "scene/theme/theme_db.h"
#include "scene/theme/theme_owner.h"
#include "servers/rendering_server.h"
#include "servers/text_server.h"
#ifdef TOOLS_ENABLED
#include "editor/plugins/control_editor_plugin.h"
#endif
// Editor plugin interoperability.
// TODO: Decouple controls from their editor plugin and get rid of this.
#ifdef TOOLS_ENABLED
Dictionary Control::_edit_get_state() const {
Dictionary s;
s["rotation"] = get_rotation();
s["scale"] = get_scale();
s["pivot"] = get_pivot_offset();
Array anchors;
anchors.push_back(get_anchor(SIDE_LEFT));
anchors.push_back(get_anchor(SIDE_TOP));
anchors.push_back(get_anchor(SIDE_RIGHT));
anchors.push_back(get_anchor(SIDE_BOTTOM));
s["anchors"] = anchors;
Array offsets;
offsets.push_back(get_offset(SIDE_LEFT));
offsets.push_back(get_offset(SIDE_TOP));
offsets.push_back(get_offset(SIDE_RIGHT));
offsets.push_back(get_offset(SIDE_BOTTOM));
s["offsets"] = offsets;
s["layout_mode"] = _get_layout_mode();
s["anchors_layout_preset"] = _get_anchors_layout_preset();
return s;
}
void Control::_edit_set_state(const Dictionary &p_state) {
ERR_FAIL_COND((p_state.size() <= 0) ||
!p_state.has("rotation") || !p_state.has("scale") ||
!p_state.has("pivot") || !p_state.has("anchors") || !p_state.has("offsets") ||
!p_state.has("layout_mode") || !p_state.has("anchors_layout_preset"));
Dictionary state = p_state;
set_rotation(state["rotation"]);
set_scale(state["scale"]);
set_pivot_offset(state["pivot"]);
Array anchors = state["anchors"];
// If anchors are not in their default position, force the anchor layout mode in place of position.
LayoutMode _layout = (LayoutMode)(int)state["layout_mode"];
if (_layout == LayoutMode::LAYOUT_MODE_POSITION) {
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);
if (anchors_mode) {
_layout = LayoutMode::LAYOUT_MODE_ANCHORS;
}
}
_set_layout_mode(_layout);
if (_layout == LayoutMode::LAYOUT_MODE_ANCHORS || _layout == LayoutMode::LAYOUT_MODE_UNCONTROLLED) {
_set_anchors_layout_preset((int)state["anchors_layout_preset"]);
}
data.anchor[SIDE_LEFT] = anchors[0];
data.anchor[SIDE_TOP] = anchors[1];
data.anchor[SIDE_RIGHT] = anchors[2];
data.anchor[SIDE_BOTTOM] = anchors[3];
Array offsets = state["offsets"];
data.offset[SIDE_LEFT] = offsets[0];
data.offset[SIDE_TOP] = offsets[1];
data.offset[SIDE_RIGHT] = offsets[2];
data.offset[SIDE_BOTTOM] = offsets[3];
_size_changed();
}
void Control::_edit_set_position(const Point2 &p_position) {
ERR_FAIL_COND_MSG(!Engine::get_singleton()->is_editor_hint(), "This function can only be used from editor plugins.");
set_position(p_position, ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled() && get_parent_control());
};
Point2 Control::_edit_get_position() const {
return get_position();
};
void Control::_edit_set_scale(const Size2 &p_scale) {
set_scale(p_scale);
}
Size2 Control::_edit_get_scale() const {
return data.scale;
}
void Control::_edit_set_rect(const Rect2 &p_edit_rect) {
ERR_FAIL_COND_MSG(!Engine::get_singleton()->is_editor_hint(), "This function can only be used from editor plugins.");
set_position((get_position() + get_transform().basis_xform(p_edit_rect.position)).snapped(Vector2(1, 1)), ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
set_size(p_edit_rect.size.snapped(Vector2(1, 1)), ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
}
Rect2 Control::_edit_get_rect() const {
return Rect2(Point2(), get_size());
}
bool Control::_edit_use_rect() const {
return true;
}
void Control::_edit_set_rotation(real_t p_rotation) {
set_rotation(p_rotation);
}
real_t Control::_edit_get_rotation() const {
return get_rotation();
}
bool Control::_edit_use_rotation() const {
return true;
}
void Control::_edit_set_pivot(const Point2 &p_pivot) {
Vector2 delta_pivot = p_pivot - get_pivot_offset();
Vector2 move = Vector2((cos(data.rotation) - 1.0) * delta_pivot.x - sin(data.rotation) * delta_pivot.y, sin(data.rotation) * delta_pivot.x + (cos(data.rotation) - 1.0) * delta_pivot.y);
set_position(get_position() + move);
set_pivot_offset(p_pivot);
}
Point2 Control::_edit_get_pivot() const {
return get_pivot_offset();
}
bool Control::_edit_use_pivot() const {
return true;
}
Size2 Control::_edit_get_minimum_size() const {
return get_combined_minimum_size();
}
#endif
void Control::reparent(Node *p_parent, bool p_keep_global_transform) {
ERR_MAIN_THREAD_GUARD;
Transform2D temp = get_global_transform();
Node::reparent(p_parent);
if (p_keep_global_transform) {
set_global_position(temp.get_origin());
}
}
// Editor integration.
int Control::root_layout_direction = 0;
void Control::set_root_layout_direction(int p_root_dir) {
root_layout_direction = p_root_dir;
}
void Control::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
ERR_READ_THREAD_GUARD;
CanvasItem::get_argument_options(p_function, p_idx, r_options);
if (p_idx == 0) {
List<StringName> sn;
String pf = p_function;
if (pf == "add_theme_color_override" || pf == "has_theme_color" || pf == "has_theme_color_override" || pf == "get_theme_color") {
ThemeDB::get_singleton()->get_default_theme()->get_color_list(get_class(), &sn);
} else if (pf == "add_theme_style_override" || pf == "has_theme_style" || pf == "has_theme_style_override" || pf == "get_theme_style") {
ThemeDB::get_singleton()->get_default_theme()->get_stylebox_list(get_class(), &sn);
} else if (pf == "add_theme_font_override" || pf == "has_theme_font" || pf == "has_theme_font_override" || pf == "get_theme_font") {
ThemeDB::get_singleton()->get_default_theme()->get_font_list(get_class(), &sn);
} else if (pf == "add_theme_font_size_override" || pf == "has_theme_font_size" || pf == "has_theme_font_size_override" || pf == "get_theme_font_size") {
ThemeDB::get_singleton()->get_default_theme()->get_font_size_list(get_class(), &sn);
} else if (pf == "add_theme_constant_override" || pf == "has_theme_constant" || pf == "has_theme_constant_override" || pf == "get_theme_constant") {
ThemeDB::get_singleton()->get_default_theme()->get_constant_list(get_class(), &sn);
}
sn.sort_custom<StringName::AlphCompare>();
for (const StringName &name : sn) {
r_options->push_back(String(name).quote());
}
}
}
PackedStringArray Control::get_configuration_warnings() const {
ERR_READ_THREAD_GUARD_V(PackedStringArray());
PackedStringArray warnings = Node::get_configuration_warnings();
if (data.mouse_filter == MOUSE_FILTER_IGNORE && !data.tooltip.is_empty()) {
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\"."));
}
if (get_z_index() != 0) {
warnings.push_back(RTR("Changing the Z index of a control only affects the drawing order, not the input event handling order."));
}
return warnings;
}
bool Control::is_text_field() const {
ERR_READ_THREAD_GUARD_V(false);
return false;
}
// Dynamic properties.
String Control::properties_managed_by_container[] = {
"offset_left",
"offset_top",
"offset_right",
"offset_bottom",
"anchor_left",
"anchor_top",
"anchor_right",
"anchor_bottom",
"position",
"rotation",
"scale",
"size"
};
bool Control::_set(const StringName &p_name, const Variant &p_value) {
ERR_MAIN_THREAD_GUARD_V(false);
String name = p_name;
if (!name.begins_with("theme_override")) {
return false;
}
if (p_value.get_type() == Variant::NIL || (p_value.get_type() == Variant::OBJECT && (Object *)p_value == nullptr)) {
if (name.begins_with("theme_override_icons/")) {
String dname = name.get_slicec('/', 1);
if (data.theme_icon_override.has(dname)) {
data.theme_icon_override[dname]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
}
data.theme_icon_override.erase(dname);
_notify_theme_override_changed();
} else if (name.begins_with("theme_override_styles/")) {
String dname = name.get_slicec('/', 1);
if (data.theme_style_override.has(dname)) {
data.theme_style_override[dname]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
}
data.theme_style_override.erase(dname);
_notify_theme_override_changed();
} else if (name.begins_with("theme_override_fonts/")) {
String dname = name.get_slicec('/', 1);
if (data.theme_font_override.has(dname)) {
data.theme_font_override[dname]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
}
data.theme_font_override.erase(dname);
_notify_theme_override_changed();
} else if (name.begins_with("theme_override_font_sizes/")) {
String dname = name.get_slicec('/', 1);
data.theme_font_size_override.erase(dname);
_notify_theme_override_changed();
} else if (name.begins_with("theme_override_colors/")) {
String dname = name.get_slicec('/', 1);
data.theme_color_override.erase(dname);
_notify_theme_override_changed();
} else if (name.begins_with("theme_override_constants/")) {
String dname = name.get_slicec('/', 1);
data.theme_constant_override.erase(dname);
_notify_theme_override_changed();
} else {
return false;
}
} else {
if (name.begins_with("theme_override_icons/")) {
String dname = name.get_slicec('/', 1);
add_theme_icon_override(dname, p_value);
} else if (name.begins_with("theme_override_styles/")) {
String dname = name.get_slicec('/', 1);
add_theme_style_override(dname, p_value);
} else if (name.begins_with("theme_override_fonts/")) {
String dname = name.get_slicec('/', 1);
add_theme_font_override(dname, p_value);
} else if (name.begins_with("theme_override_font_sizes/")) {
String dname = name.get_slicec('/', 1);
add_theme_font_size_override(dname, p_value);
} else if (name.begins_with("theme_override_colors/")) {
String dname = name.get_slicec('/', 1);
add_theme_color_override(dname, p_value);
} else if (name.begins_with("theme_override_constants/")) {
String dname = name.get_slicec('/', 1);
add_theme_constant_override(dname, p_value);
} else {
return false;
}
}
return true;
}
bool Control::_get(const StringName &p_name, Variant &r_ret) const {
ERR_MAIN_THREAD_GUARD_V(false);
String sname = p_name;
if (!sname.begins_with("theme_override")) {
return false;
}
if (sname.begins_with("theme_override_icons/")) {
String name = sname.get_slicec('/', 1);
r_ret = data.theme_icon_override.has(name) ? Variant(data.theme_icon_override[name]) : Variant();
} else if (sname.begins_with("theme_override_styles/")) {
String name = sname.get_slicec('/', 1);
r_ret = data.theme_style_override.has(name) ? Variant(data.theme_style_override[name]) : Variant();
} else if (sname.begins_with("theme_override_fonts/")) {
String name = sname.get_slicec('/', 1);
r_ret = data.theme_font_override.has(name) ? Variant(data.theme_font_override[name]) : Variant();
} else if (sname.begins_with("theme_override_font_sizes/")) {
String name = sname.get_slicec('/', 1);
r_ret = data.theme_font_size_override.has(name) ? Variant(data.theme_font_size_override[name]) : Variant();
} else if (sname.begins_with("theme_override_colors/")) {
String name = sname.get_slicec('/', 1);
r_ret = data.theme_color_override.has(name) ? Variant(data.theme_color_override[name]) : Variant();
} else if (sname.begins_with("theme_override_constants/")) {
String name = sname.get_slicec('/', 1);
r_ret = data.theme_constant_override.has(name) ? Variant(data.theme_constant_override[name]) : Variant();
} else {
return false;
}
return true;
}
void Control::_get_property_list(List<PropertyInfo> *p_list) const {
ERR_MAIN_THREAD_GUARD;
Ref<Theme> default_theme = ThemeDB::get_singleton()->get_default_theme();
p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Theme Overrides", "theme_override_"), PROPERTY_HINT_NONE, "theme_override_", PROPERTY_USAGE_GROUP));
{
List<StringName> names;
default_theme->get_color_list(get_class_name(), &names);
for (const StringName &E : names) {
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
if (data.theme_color_override.has(E)) {
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
}
p_list->push_back(PropertyInfo(Variant::COLOR, PNAME("theme_override_colors") + String("/") + E, PROPERTY_HINT_NONE, "", usage));
}
}
{
List<StringName> names;
default_theme->get_constant_list(get_class_name(), &names);
for (const StringName &E : names) {
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
if (data.theme_constant_override.has(E)) {
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
}
p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_constants") + String("/") + E, PROPERTY_HINT_RANGE, "-16384,16384", usage));
}
}
{
List<StringName> names;
default_theme->get_font_list(get_class_name(), &names);
for (const StringName &E : names) {
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
if (data.theme_font_override.has(E)) {
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
}
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_fonts") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "Font", usage));
}
}
{
List<StringName> names;
default_theme->get_font_size_list(get_class_name(), &names);
for (const StringName &E : names) {
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
if (data.theme_font_size_override.has(E)) {
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
}
p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_font_sizes") + String("/") + E, PROPERTY_HINT_RANGE, "1,256,1,or_greater,suffix:px", usage));
}
}
{
List<StringName> names;
default_theme->get_icon_list(get_class_name(), &names);
for (const StringName &E : names) {
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
if (data.theme_icon_override.has(E)) {
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
}
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_icons") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", usage));
}
}
{
List<StringName> names;
default_theme->get_stylebox_list(get_class_name(), &names);
for (const StringName &E : names) {
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
if (data.theme_style_override.has(E)) {
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
}
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_styles") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", usage));
}
}
}
void Control::_validate_property(PropertyInfo &p_property) const {
// Update theme type variation options.
if (p_property.name == "theme_type_variation") {
List<StringName> names;
// Only the default theme and the project theme are used for the list of options.
// This is an imposed limitation to simplify the logic needed to leverage those options.
ThemeDB::get_singleton()->get_default_theme()->get_type_variation_list(get_class_name(), &names);
if (ThemeDB::get_singleton()->get_project_theme().is_valid()) {
ThemeDB::get_singleton()->get_project_theme()->get_type_variation_list(get_class_name(), &names);
}
names.sort_custom<StringName::AlphCompare>();
Vector<StringName> unique_names;
String hint_string;
for (const StringName &E : names) {
// Skip duplicate values.
if (unique_names.has(E)) {
continue;
}
hint_string += String(E) + ",";
unique_names.append(E);
}
p_property.hint_string = hint_string;
}
if (p_property.name == "mouse_force_pass_scroll_events") {
// Disable force pass if the control is not stopping the event.
if (data.mouse_filter != MOUSE_FILTER_STOP) {
p_property.usage |= PROPERTY_USAGE_READ_ONLY;
}
}
if (p_property.name == "scale") {
p_property.hint = PROPERTY_HINT_LINK;
}
// Validate which positioning properties should be displayed depending on the parent and the layout mode.
Node *parent_node = get_parent_control();
if (!parent_node) {
// If there is no parent, display both anchor and container options.
// Set the layout mode to be disabled with the proper value.
if (p_property.name == "layout_mode") {
p_property.hint_string = "Position,Anchors,Container,Uncontrolled";
p_property.usage |= PROPERTY_USAGE_READ_ONLY;
}
// Use the layout mode to display or hide advanced anchoring properties.
bool use_custom_anchors = _get_anchors_layout_preset() == -1; // Custom "preset".
if (!use_custom_anchors && (p_property.name.begins_with("anchor_") || p_property.name.begins_with("offset_") || p_property.name.begins_with("grow_"))) {
p_property.usage ^= PROPERTY_USAGE_EDITOR;
}
} else if (Object::cast_to<Container>(parent_node)) {
// If the parent is a container, display only container-related properties.
if (p_property.name.begins_with("anchor_") || p_property.name.begins_with("offset_") || p_property.name.begins_with("grow_") || p_property.name == "anchors_preset") {
p_property.usage ^= PROPERTY_USAGE_DEFAULT;
} 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.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY;
} else if (p_property.name == "layout_mode") {
// Set the layout mode to be disabled with the proper value.
p_property.hint_string = "Position,Anchors,Container,Uncontrolled";
p_property.usage |= PROPERTY_USAGE_READ_ONLY;
} else if (p_property.name == "size_flags_horizontal" || p_property.name == "size_flags_vertical") {
// Filter allowed size flags based on the parent container configuration.
Container *parent_container = Object::cast_to<Container>(parent_node);
Vector<int> size_flags;
if (p_property.name == "size_flags_horizontal") {
size_flags = parent_container->get_allowed_size_flags_horizontal();
} else if (p_property.name == "size_flags_vertical") {
size_flags = parent_container->get_allowed_size_flags_vertical();
}
// Enforce the order of the options, regardless of what the container provided.
String hint_string;
if (size_flags.has(SIZE_FILL)) {
hint_string += "Fill:1";
}
if (size_flags.has(SIZE_EXPAND)) {
if (!hint_string.is_empty()) {
hint_string += ",";
}
hint_string += "Expand:2";
}
if (size_flags.has(SIZE_SHRINK_CENTER)) {
if (!hint_string.is_empty()) {
hint_string += ",";
}
hint_string += "Shrink Center:4";
}
if (size_flags.has(SIZE_SHRINK_END)) {
if (!hint_string.is_empty()) {
hint_string += ",";
}
hint_string += "Shrink End:8";
}
if (hint_string.is_empty()) {
p_property.hint_string = "";
p_property.usage |= PROPERTY_USAGE_READ_ONLY;
} else {
p_property.hint_string = hint_string;
}
}
} else {
// If the parent is NOT a container or not a control at all, display only anchoring-related properties.
if (p_property.name.begins_with("size_flags_")) {
p_property.usage ^= PROPERTY_USAGE_EDITOR;
} else if (p_property.name == "layout_mode") {
// Set the layout mode to be enabled with proper options.
p_property.hint_string = "Position,Anchors";
}
// Use the layout mode to display or hide advanced anchoring properties.
LayoutMode _layout = _get_layout_mode();
bool use_anchors = (_layout == LayoutMode::LAYOUT_MODE_ANCHORS || _layout == LayoutMode::LAYOUT_MODE_UNCONTROLLED);
if (!use_anchors && p_property.name == "anchors_preset") {
p_property.usage ^= PROPERTY_USAGE_EDITOR;
}
bool use_custom_anchors = use_anchors && _get_anchors_layout_preset() == -1; // Custom "preset".
if (!use_custom_anchors && (p_property.name.begins_with("anchor_") || p_property.name.begins_with("offset_") || p_property.name.begins_with("grow_"))) {
p_property.usage ^= PROPERTY_USAGE_EDITOR;
}
}
// Disable the property if it's managed by the parent container.
if (!Object::cast_to<Container>(parent_node)) {
return;
}
bool property_is_managed_by_container = false;
for (unsigned i = 0; i < properties_managed_by_container_count; i++) {
property_is_managed_by_container = properties_managed_by_container[i] == p_property.name;
if (property_is_managed_by_container) {
break;
}
}
if (property_is_managed_by_container) {
p_property.usage |= PROPERTY_USAGE_READ_ONLY;
}
}
bool Control::_property_can_revert(const StringName &p_name) const {
if (p_name == "layout_mode" || p_name == "anchors_preset") {
return true;
}
return false;
}
bool Control::_property_get_revert(const StringName &p_name, Variant &r_property) const {
if (p_name == "layout_mode") {
r_property = _get_default_layout_mode();
return true;
} else if (p_name == "anchors_preset") {
r_property = LayoutPreset::PRESET_TOP_LEFT;
return true;
}
return false;
}
// Global relations.
bool Control::is_top_level_control() const {
ERR_READ_THREAD_GUARD_V(false);
return is_inside_tree() && (!data.parent_canvas_item && !data.RI && is_set_as_top_level());
}
Control *Control::get_parent_control() const {
ERR_READ_THREAD_GUARD_V(nullptr);
return data.parent_control;
}
Window *Control::get_parent_window() const {
ERR_READ_THREAD_GUARD_V(nullptr);
return data.parent_window;
}
Control *Control::get_root_parent_control() const {
ERR_READ_THREAD_GUARD_V(nullptr);
const CanvasItem *ci = this;
const Control *root = this;
while (ci) {
const Control *c = Object::cast_to<Control>(ci);
if (c) {
root = c;
if (c->data.RI || c->is_top_level_control()) {
break;
}
}
ci = ci->get_parent_item();
}
return const_cast<Control *>(root);
}
Rect2 Control::get_parent_anchorable_rect() const {
ERR_READ_THREAD_GUARD_V(Rect2());
if (!is_inside_tree()) {
return Rect2();
}
Rect2 parent_rect;
if (data.parent_canvas_item) {
parent_rect = data.parent_canvas_item->get_anchorable_rect();
} else {
#ifdef TOOLS_ENABLED
Node *edited_scene_root = get_tree()->get_edited_scene_root();
Node *scene_root_parent = edited_scene_root ? edited_scene_root->get_parent() : nullptr;
if (scene_root_parent && get_viewport() == scene_root_parent->get_viewport()) {
parent_rect.size = Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
} else {
parent_rect = get_viewport()->get_visible_rect();
}
#else
parent_rect = get_viewport()->get_visible_rect();
#endif
}
return parent_rect;
}
Size2 Control::get_parent_area_size() const {
ERR_READ_THREAD_GUARD_V(Size2());
return get_parent_anchorable_rect().size;
}
// Positioning and sizing.
Transform2D Control::_get_internal_transform() const {
Transform2D rot_scale;
rot_scale.set_rotation_and_scale(data.rotation, data.scale);
Transform2D offset;
offset.set_origin(-data.pivot_offset);
return offset.affine_inverse() * (rot_scale * offset);
}
void Control::_update_canvas_item_transform() {
Transform2D xform = _get_internal_transform();
xform[2] += get_position();
// We use a little workaround to avoid flickering when moving the pivot with _edit_set_pivot()
if (is_inside_tree() && Math::abs(Math::sin(data.rotation * 4.0f)) < 0.00001f && get_viewport()->is_snap_controls_to_pixels_enabled()) {
xform[2] = xform[2].round();
}
RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), xform);
}
Transform2D Control::get_transform() const {
ERR_READ_THREAD_GUARD_V(Transform2D());
Transform2D xform = _get_internal_transform();
xform[2] += get_position();
return xform;
}
void Control::_top_level_changed_on_parent() {
// Update root control status.
_notification(NOTIFICATION_EXIT_CANVAS);
_notification(NOTIFICATION_ENTER_CANVAS);
}
/// Anchors and offsets.
void Control::_set_anchor(Side p_side, real_t p_anchor) {
set_anchor(p_side, p_anchor);
}
void Control::set_anchor(Side p_side, real_t p_anchor, bool p_keep_offset, bool p_push_opposite_anchor) {
ERR_MAIN_THREAD_GUARD;
ERR_FAIL_INDEX((int)p_side, 4);
Rect2 parent_rect = get_parent_anchorable_rect();
real_t parent_range = (p_side == SIDE_LEFT || p_side == SIDE_RIGHT) ? parent_rect.size.x : parent_rect.size.y;
real_t previous_pos = data.offset[p_side] + data.anchor[p_side] * parent_range;
real_t previous_opposite_pos = data.offset[(p_side + 2) % 4] + data.anchor[(p_side + 2) % 4] * parent_range;
data.anchor[p_side] = p_anchor;
if (((p_side == SIDE_LEFT || p_side == SIDE_TOP) && data.anchor[p_side] > data.anchor[(p_side + 2) % 4]) ||
((p_side == SIDE_RIGHT || p_side == SIDE_BOTTOM) && data.anchor[p_side] < data.anchor[(p_side + 2) % 4])) {
if (p_push_opposite_anchor) {
data.anchor[(p_side + 2) % 4] = data.anchor[p_side];
} else {
data.anchor[p_side] = data.anchor[(p_side + 2) % 4];
}
}
if (!p_keep_offset) {
data.offset[p_side] = previous_pos - data.anchor[p_side] * parent_range;
if (p_push_opposite_anchor) {
data.offset[(p_side + 2) % 4] = previous_opposite_pos - data.anchor[(p_side + 2) % 4] * parent_range;
}
}
if (is_inside_tree()) {
_size_changed();
}
queue_redraw();
}
real_t Control::get_anchor(Side p_side) const {
ERR_READ_THREAD_GUARD_V(0);
ERR_FAIL_INDEX_V(int(p_side), 4, 0.0);
return data.anchor[p_side];
}
void Control::set_offset(Side p_side, real_t p_value) {
ERR_MAIN_THREAD_GUARD;
ERR_FAIL_INDEX((int)p_side, 4);
if (data.offset[p_side] == p_value) {
return;
}
data.offset[p_side] = p_value;
_size_changed();
}
real_t Control::get_offset(Side p_side) const {
ERR_READ_THREAD_GUARD_V(0);
ERR_FAIL_INDEX_V((int)p_side, 4, 0);
return data.offset[p_side];
}
void Control::set_anchor_and_offset(Side p_side, real_t p_anchor, real_t p_pos, bool p_push_opposite_anchor) {
ERR_MAIN_THREAD_GUARD;
set_anchor(p_side, p_anchor, false, p_push_opposite_anchor);
set_offset(p_side, p_pos);
}
void Control::set_begin(const Point2 &p_point) {
ERR_MAIN_THREAD_GUARD;
ERR_FAIL_COND(!isfinite(p_point.x) || !isfinite(p_point.y));
if (data.offset[0] == p_point.x && data.offset[1] == p_point.y) {
return;
}
data.offset[0] = p_point.x;
data.offset[1] = p_point.y;
_size_changed();
}
Point2 Control::get_begin() const {
ERR_READ_THREAD_GUARD_V(Vector2());
return Point2(data.offset[0], data.offset[1]);
}
void Control::set_end(const Point2 &p_point) {
ERR_MAIN_THREAD_GUARD;
if (data.offset[2] == p_point.x && data.offset[3] == p_point.y) {
return;
}
data.offset[2] = p_point.x;
data.offset[3] = p_point.y;
_size_changed();
}
Point2 Control::get_end() const {
ERR_READ_THREAD_GUARD_V(Point2());
return Point2(data.offset[2], data.offset[3]);
}
void Control::set_h_grow_direction(GrowDirection p_direction) {
ERR_MAIN_THREAD_GUARD;
if (data.h_grow == p_direction) {
return;
}
ERR_FAIL_INDEX((int)p_direction, 3);
data.h_grow = p_direction;
_size_changed();
}
Control::GrowDirection Control::get_h_grow_direction() const {
ERR_READ_THREAD_GUARD_V(GROW_DIRECTION_BEGIN);
return data.h_grow;
}
void Control::set_v_grow_direction(GrowDirection p_direction) {
ERR_MAIN_THREAD_GUARD;
if (data.v_grow == p_direction) {
return;
}
ERR_FAIL_INDEX((int)p_direction, 3);
data.v_grow = p_direction;
_size_changed();
}
Control::GrowDirection Control::get_v_grow_direction() const {
ERR_READ_THREAD_GUARD_V(GROW_DIRECTION_BEGIN);
return data.v_grow;
}
void Control::_compute_anchors(Rect2 p_rect, const real_t p_offsets[4], real_t (&r_anchors)[4]) {
Size2 parent_rect_size = get_parent_anchorable_rect().size;
ERR_FAIL_COND(parent_rect_size.x == 0.0);
ERR_FAIL_COND(parent_rect_size.y == 0.0);
real_t x = p_rect.position.x;
if (is_layout_rtl()) {
x = parent_rect_size.x - x - p_rect.size.x;
}
r_anchors[0] = (x - p_offsets[0]) / parent_rect_size.x;
r_anchors[1] = (p_rect.position.y - p_offsets[1]) / parent_rect_size.y;
r_anchors[2] = (x + p_rect.size.x - p_offsets[2]) / parent_rect_size.x;
r_anchors[3] = (p_rect.position.y + p_rect.size.y - p_offsets[3]) / parent_rect_size.y;
}
void Control::_compute_offsets(Rect2 p_rect, const real_t p_anchors[4], real_t (&r_offsets)[4]) {
Size2 parent_rect_size = get_parent_anchorable_rect().size;
real_t x = p_rect.position.x;
if (is_layout_rtl()) {
x = parent_rect_size.x - x - p_rect.size.x;
}
r_offsets[0] = x - (p_anchors[0] * parent_rect_size.x);
r_offsets[1] = p_rect.position.y - (p_anchors[1] * parent_rect_size.y);
r_offsets[2] = x + p_rect.size.x - (p_anchors[2] * parent_rect_size.x);
r_offsets[3] = p_rect.position.y + p_rect.size.y - (p_anchors[3] * parent_rect_size.y);
}
/// Presets and layout modes.
void Control::_set_layout_mode(LayoutMode p_mode) {
bool list_changed = false;
if (data.stored_layout_mode != p_mode) {
list_changed = true;
data.stored_layout_mode = p_mode;
}
if (data.stored_layout_mode == LayoutMode::LAYOUT_MODE_POSITION) {
data.stored_use_custom_anchors = false;
set_anchors_and_offsets_preset(LayoutPreset::PRESET_TOP_LEFT, LayoutPresetMode::PRESET_MODE_KEEP_SIZE);
set_grow_direction_preset(LayoutPreset::PRESET_TOP_LEFT);
}
if (list_changed) {
notify_property_list_changed();
}
}
void Control::_update_layout_mode() {
LayoutMode computed_layout = _get_layout_mode();
if (data.stored_layout_mode != computed_layout) {
data.stored_layout_mode = computed_layout;
notify_property_list_changed();
}
}
Control::LayoutMode Control::_get_layout_mode() const {
Node *parent_node = get_parent_control();
// In these modes the property is read-only.
if (!parent_node) {
return LayoutMode::LAYOUT_MODE_UNCONTROLLED;
} else if (Object::cast_to<Container>(parent_node)) {
return LayoutMode::LAYOUT_MODE_CONTAINER;
}
// If anchors are not in the top-left position, this is definitely in anchors mode.
if (_get_anchors_layout_preset() != (int)LayoutPreset::PRESET_TOP_LEFT) {
return LayoutMode::LAYOUT_MODE_ANCHORS;
}
// Otherwise fallback on what's stored.
return data.stored_layout_mode;
}
Control::LayoutMode Control::_get_default_layout_mode() const {
Node *parent_node = get_parent_control();
// In these modes the property is read-only.
if (!parent_node) {
return LayoutMode::LAYOUT_MODE_UNCONTROLLED;
} else if (Object::cast_to<Container>(parent_node)) {
return LayoutMode::LAYOUT_MODE_CONTAINER;
}
// Otherwise fallback on the position mode.
return LayoutMode::LAYOUT_MODE_POSITION;
}
void Control::_set_anchors_layout_preset(int p_preset) {
if (data.stored_layout_mode != LayoutMode::LAYOUT_MODE_UNCONTROLLED && data.stored_layout_mode != LayoutMode::LAYOUT_MODE_ANCHORS) {
// In other modes the anchor preset is non-operational and shouldn't be set to anything.
return;
}
if (p_preset == -1) {
if (!data.stored_use_custom_anchors) {
data.stored_use_custom_anchors = true;
notify_property_list_changed();
}
return; // Keep settings as is.
}
bool list_changed = false;
if (data.stored_use_custom_anchors) {
list_changed = true;
data.stored_use_custom_anchors = false;
}
LayoutPreset preset = (LayoutPreset)p_preset;
// Set correct anchors.
set_anchors_preset(preset);
// Select correct preset mode.
switch (preset) {
case PRESET_TOP_LEFT:
case PRESET_TOP_RIGHT:
case PRESET_BOTTOM_LEFT:
case PRESET_BOTTOM_RIGHT:
case PRESET_CENTER_LEFT:
case PRESET_CENTER_TOP:
case PRESET_CENTER_RIGHT:
case PRESET_CENTER_BOTTOM:
case PRESET_CENTER:
set_offsets_preset(preset, LayoutPresetMode::PRESET_MODE_KEEP_SIZE);
break;
case PRESET_LEFT_WIDE:
case PRESET_TOP_WIDE:
case PRESET_RIGHT_WIDE:
case PRESET_BOTTOM_WIDE:
case PRESET_VCENTER_WIDE:
case PRESET_HCENTER_WIDE:
case PRESET_FULL_RECT:
set_offsets_preset(preset, LayoutPresetMode::PRESET_MODE_MINSIZE);
break;
}
// Select correct grow directions.
set_grow_direction_preset(preset);
if (list_changed) {
notify_property_list_changed();