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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
|
/*************************************************************************/
/* editor_properties_array_dict.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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 "editor_properties_array_dict.h"
#include "core/input/input.h"
#include "core/io/marshalls.h"
#include "editor/editor_node.h"
#include "editor/editor_scale.h"
#include "editor_properties.h"
bool EditorPropertyArrayObject::_set(const StringName &p_name, const Variant &p_value) {
String name = p_name;
if (name.begins_with("indices")) {
int index = name.get_slicec('/', 1).to_int();
array.set(index, p_value);
return true;
}
return false;
}
bool EditorPropertyArrayObject::_get(const StringName &p_name, Variant &r_ret) const {
String name = p_name;
if (name.begins_with("indices")) {
int index = name.get_slicec('/', 1).to_int();
bool valid;
r_ret = array.get(index, &valid);
if (r_ret.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(r_ret)) {
r_ret = Object::cast_to<EncodedObjectAsID>(r_ret)->get_object_id();
}
return valid;
}
return false;
}
void EditorPropertyArrayObject::set_array(const Variant &p_array) {
array = p_array;
}
Variant EditorPropertyArrayObject::get_array() {
return array;
}
EditorPropertyArrayObject::EditorPropertyArrayObject() {
}
///////////////////
bool EditorPropertyDictionaryObject::_set(const StringName &p_name, const Variant &p_value) {
String name = p_name;
if (name == "new_item_key") {
new_item_key = p_value;
return true;
}
if (name == "new_item_value") {
new_item_value = p_value;
return true;
}
if (name.begins_with("indices")) {
int index = name.get_slicec('/', 1).to_int();
Variant key = dict.get_key_at_index(index);
dict[key] = p_value;
return true;
}
return false;
}
bool EditorPropertyDictionaryObject::_get(const StringName &p_name, Variant &r_ret) const {
String name = p_name;
if (name == "new_item_key") {
r_ret = new_item_key;
return true;
}
if (name == "new_item_value") {
r_ret = new_item_value;
return true;
}
if (name.begins_with("indices")) {
int index = name.get_slicec('/', 1).to_int();
Variant key = dict.get_key_at_index(index);
r_ret = dict[key];
if (r_ret.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(r_ret)) {
r_ret = Object::cast_to<EncodedObjectAsID>(r_ret)->get_object_id();
}
return true;
}
return false;
}
void EditorPropertyDictionaryObject::set_dict(const Dictionary &p_dict) {
dict = p_dict;
}
Dictionary EditorPropertyDictionaryObject::get_dict() {
return dict;
}
void EditorPropertyDictionaryObject::set_new_item_key(const Variant &p_new_item) {
new_item_key = p_new_item;
}
Variant EditorPropertyDictionaryObject::get_new_item_key() {
return new_item_key;
}
void EditorPropertyDictionaryObject::set_new_item_value(const Variant &p_new_item) {
new_item_value = p_new_item;
}
Variant EditorPropertyDictionaryObject::get_new_item_value() {
return new_item_value;
}
EditorPropertyDictionaryObject::EditorPropertyDictionaryObject() {
}
///////////////////// ARRAY ///////////////////////////
void EditorPropertyArray::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
if (p_property.begins_with("indices")) {
int index = p_property.get_slice("/", 1).to_int();
Variant array = object->get_array();
array.set(index, p_value);
emit_changed(get_edited_property(), array, "", true);
if (array.get_type() == Variant::ARRAY) {
array = array.call("duplicate"); // Duplicate, so undo/redo works better.
}
object->set_array(array);
}
}
void EditorPropertyArray::_change_type(Object *p_button, int p_index) {
Button *button = Object::cast_to<Button>(p_button);
changing_type_index = p_index;
Rect2 rect = button->get_screen_rect();
change_type->set_as_minsize();
change_type->set_position(rect.get_end() - Vector2(change_type->get_contents_minimum_size().x, 0));
change_type->popup();
}
void EditorPropertyArray::_change_type_menu(int p_index) {
if (p_index == Variant::VARIANT_MAX) {
_remove_pressed(changing_type_index);
return;
}
Variant value;
Callable::CallError ce;
Variant::construct(Variant::Type(p_index), value, nullptr, 0, ce);
Variant array = object->get_array();
array.set(changing_type_index, value);
emit_changed(get_edited_property(), array, "", true);
if (array.get_type() == Variant::ARRAY) {
array = array.call("duplicate"); // Duplicate, so undo/redo works better.
}
object->set_array(array);
update_property();
}
void EditorPropertyArray::_object_id_selected(const StringName &p_property, ObjectID p_id) {
emit_signal(SNAME("object_id_selected"), p_property, p_id);
}
void EditorPropertyArray::update_property() {
Variant array = get_edited_object()->get(get_edited_property());
String arrtype = "";
switch (array_type) {
case Variant::ARRAY: {
arrtype = "Array";
} break;
// Arrays.
case Variant::PACKED_BYTE_ARRAY: {
arrtype = "PackedByteArray";
} break;
case Variant::PACKED_INT32_ARRAY: {
arrtype = "PackedInt32Array";
} break;
case Variant::PACKED_FLOAT32_ARRAY: {
arrtype = "PackedFloat32Array";
} break;
case Variant::PACKED_INT64_ARRAY: {
arrtype = "PackedInt64Array";
} break;
case Variant::PACKED_FLOAT64_ARRAY: {
arrtype = "PackedFloat64Array";
} break;
case Variant::PACKED_STRING_ARRAY: {
arrtype = "PackedStringArray";
} break;
case Variant::PACKED_VECTOR2_ARRAY: {
arrtype = "PackedVector2Array";
} break;
case Variant::PACKED_VECTOR3_ARRAY: {
arrtype = "PackedVector3Array";
} break;
case Variant::PACKED_COLOR_ARRAY: {
arrtype = "PackedColorArray";
} break;
default: {
}
}
if (array.get_type() == Variant::NIL) {
edit->set_text(String("(Nil) ") + arrtype);
edit->set_pressed(false);
if (vbox) {
set_bottom_editor(nullptr);
memdelete(vbox);
vbox = nullptr;
}
return;
}
int size = array.call("size");
int pages = MAX(0, size - 1) / page_length + 1;
page_index = MIN(page_index, pages - 1);
int offset = page_index * page_length;
edit->set_text(arrtype + " (size " + itos(size) + ")");
bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
if (edit->is_pressed() != unfolded) {
edit->set_pressed(unfolded);
}
if (unfolded) {
updating = true;
if (!vbox) {
vbox = memnew(VBoxContainer);
add_child(vbox);
set_bottom_editor(vbox);
HBoxContainer *hbox = memnew(HBoxContainer);
vbox->add_child(hbox);
Label *label = memnew(Label(TTR("Size: ")));
label->set_h_size_flags(SIZE_EXPAND_FILL);
hbox->add_child(label);
size_slider = memnew(EditorSpinSlider);
size_slider->set_step(1);
size_slider->set_max(1000000);
size_slider->set_h_size_flags(SIZE_EXPAND_FILL);
size_slider->connect("value_changed", callable_mp(this, &EditorPropertyArray::_length_changed));
hbox->add_child(size_slider);
page_hbox = memnew(HBoxContainer);
vbox->add_child(page_hbox);
label = memnew(Label(TTR("Page: ")));
label->set_h_size_flags(SIZE_EXPAND_FILL);
page_hbox->add_child(label);
page_slider = memnew(EditorSpinSlider);
page_slider->set_step(1);
page_slider->set_h_size_flags(SIZE_EXPAND_FILL);
page_slider->connect("value_changed", callable_mp(this, &EditorPropertyArray::_page_changed));
page_hbox->add_child(page_slider);
} else {
// Bye bye children of the box.
for (int i = vbox->get_child_count() - 1; i >= 2; i--) {
Node *child = vbox->get_child(i);
if (child == reorder_selected_element_hbox) {
continue; // Don't remove the property that the user is moving.
}
child->queue_delete(); // Button still needed after pressed is called.
vbox->remove_child(child);
}
}
size_slider->set_value(size);
page_slider->set_max(pages);
page_slider->set_value(page_index);
page_hbox->set_visible(pages > 1);
if (array.get_type() == Variant::ARRAY) {
array = array.call("duplicate");
}
object->set_array(array);
int amount = MIN(size - offset, page_length);
for (int i = 0; i < amount; i++) {
bool reorder_is_from_current_page = reorder_from_index / page_length == page_index;
if (reorder_is_from_current_page && i == reorder_from_index % page_length) {
// Don't duplicate the property that the user is moving.
continue;
}
if (!reorder_is_from_current_page && i == reorder_to_index % page_length) {
// Don't create the property the moving property will take the place of,
// e.g. (if page_length == 20) don't create element 20 if dragging an item from
// the first page to the second page because element 20 would become element 19.
continue;
}
HBoxContainer *hbox = memnew(HBoxContainer);
vbox->add_child(hbox);
Button *reorder_button = memnew(Button);
reorder_button->set_icon(get_theme_icon(SNAME("TripleBar"), SNAME("EditorIcons")));
reorder_button->set_default_cursor_shape(Control::CURSOR_MOVE);
reorder_button->connect("gui_input", callable_mp(this, &EditorPropertyArray::_reorder_button_gui_input));
reorder_button->connect("button_down", callable_mp(this, &EditorPropertyArray::_reorder_button_down), varray(i + offset));
reorder_button->connect("button_up", callable_mp(this, &EditorPropertyArray::_reorder_button_up));
hbox->add_child(reorder_button);
String prop_name = "indices/" + itos(i + offset);
EditorProperty *prop = nullptr;
Variant value = array.get(i + offset);
Variant::Type value_type = value.get_type();
if (value_type == Variant::NIL && subtype != Variant::NIL) {
value_type = subtype;
}
if (value_type == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(value)) {
EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
editor->setup("Object");
prop = editor;
} else {
prop = EditorInspector::instantiate_property_editor(nullptr, value_type, "", subtype_hint, subtype_hint_string, PROPERTY_USAGE_NONE);
}
prop->set_object_and_property(object.ptr(), prop_name);
prop->set_label(itos(i + offset));
prop->set_selectable(false);
prop->connect("property_changed", callable_mp(this, &EditorPropertyArray::_property_changed));
prop->connect("object_id_selected", callable_mp(this, &EditorPropertyArray::_object_id_selected));
prop->set_h_size_flags(SIZE_EXPAND_FILL);
hbox->add_child(prop);
bool is_untyped_array = array.get_type() == Variant::ARRAY && subtype == Variant::NIL;
if (is_untyped_array) {
Button *edit = memnew(Button);
edit->set_icon(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
hbox->add_child(edit);
edit->connect("pressed", callable_mp(this, &EditorPropertyArray::_change_type), varray(edit, i + offset));
} else {
Button *remove = memnew(Button);
remove->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
remove->connect("pressed", callable_mp(this, &EditorPropertyArray::_remove_pressed), varray(i + offset));
hbox->add_child(remove);
}
prop->update_property();
}
if (reorder_to_index % page_length > 0) {
vbox->move_child(vbox->get_child(2), reorder_to_index % page_length + 2);
}
updating = false;
} else {
if (vbox) {
set_bottom_editor(nullptr);
memdelete(vbox);
vbox = nullptr;
}
}
}
void EditorPropertyArray::_remove_pressed(int p_index) {
Variant array = object->get_array();
array.call("remove_at", p_index);
emit_changed(get_edited_property(), array, "", false);
update_property();
}
void EditorPropertyArray::_button_draw() {
if (dropping) {
Color color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
edit->draw_rect(Rect2(Point2(), edit->get_size()), color, false);
}
}
bool EditorPropertyArray::_is_drop_valid(const Dictionary &p_drag_data) const {
String allowed_type = Variant::get_type_name(subtype);
// When the subtype is of type Object, an additional subtype may be specified in the hint string
// (e.g. Resource, Texture2D, ShaderMaterial, etc). We want the allowed type to be that, not just "Object".
if (subtype == Variant::OBJECT && subtype_hint_string != "") {
allowed_type = subtype_hint_string;
}
Dictionary drag_data = p_drag_data;
if (drag_data.has("type") && String(drag_data["type"]) == "files") {
Vector<String> files = drag_data["files"];
for (int i = 0; i < files.size(); i++) {
String file = files[i];
String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
for (int j = 0; j < allowed_type.get_slice_count(","); j++) {
String at = allowed_type.get_slice(",", j).strip_edges();
// Fail if one of the files is not of allowed type.
if (!ClassDB::is_parent_class(ftype, at)) {
return false;
}
}
}
// If no files fail, drop is valid.
return true;
}
return false;
}
bool EditorPropertyArray::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
return _is_drop_valid(p_data);
}
void EditorPropertyArray::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
ERR_FAIL_COND(!_is_drop_valid(p_data));
Dictionary drag_data = p_data;
if (drag_data.has("type") && String(drag_data["type"]) == "files") {
Vector<String> files = drag_data["files"];
Variant array = object->get_array();
// Handle the case where array is not initialised yet.
if (!array.is_array()) {
Callable::CallError ce;
Variant::construct(array_type, array, nullptr, 0, ce);
}
// Loop the file array and add to existing array.
for (int i = 0; i < files.size(); i++) {
String file = files[i];
RES res = ResourceLoader::load(file);
if (res.is_valid()) {
array.call("push_back", res);
}
}
if (array.get_type() == Variant::ARRAY) {
array = array.call("duplicate");
}
emit_changed(get_edited_property(), array, "", false);
object->set_array(array);
update_property();
}
}
void EditorPropertyArray::_notification(int p_what) {
if (p_what == NOTIFICATION_DRAG_BEGIN) {
if (is_visible_in_tree()) {
if (_is_drop_valid(get_viewport()->gui_get_drag_data())) {
dropping = true;
edit->update();
}
}
}
if (p_what == NOTIFICATION_DRAG_END) {
if (dropping) {
dropping = false;
edit->update();
}
}
}
void EditorPropertyArray::_edit_pressed() {
Variant array = get_edited_object()->get(get_edited_property());
if (!array.is_array()) {
Callable::CallError ce;
Variant::construct(array_type, array, nullptr, 0, ce);
get_edited_object()->set(get_edited_property(), array);
}
get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
update_property();
}
void EditorPropertyArray::_page_changed(double p_page) {
if (updating) {
return;
}
page_index = p_page;
update_property();
}
void EditorPropertyArray::_length_changed(double p_page) {
if (updating) {
return;
}
Variant array = object->get_array();
int previous_size = array.call("size");
array.call("resize", int(p_page));
if (array.get_type() == Variant::ARRAY) {
if (subtype != Variant::NIL) {
int size = array.call("size");
for (int i = previous_size; i < size; i++) {
if (array.get(i).get_type() == Variant::NIL) {
Callable::CallError ce;
Variant r;
Variant::construct(subtype, r, nullptr, 0, ce);
array.set(i, r);
}
}
}
array = array.call("duplicate"); // Duplicate, so undo/redo works better.
} else {
int size = array.call("size");
// Pool*Array don't initialize their elements, have to do it manually.
for (int i = previous_size; i < size; i++) {
Callable::CallError ce;
Variant r;
Variant::construct(array.get(i).get_type(), r, nullptr, 0, ce);
array.set(i, r);
}
}
emit_changed(get_edited_property(), array, "", false);
object->set_array(array);
update_property();
}
void EditorPropertyArray::setup(Variant::Type p_array_type, const String &p_hint_string) {
array_type = p_array_type;
// The format of p_hint_string is:
// subType/subTypeHint:nextSubtype ... etc.
if (array_type == Variant::ARRAY && !p_hint_string.is_empty()) {
int hint_subtype_separator = p_hint_string.find(":");
if (hint_subtype_separator >= 0) {
String subtype_string = p_hint_string.substr(0, hint_subtype_separator);
int slash_pos = subtype_string.find("/");
if (slash_pos >= 0) {
subtype_hint = PropertyHint(subtype_string.substr(slash_pos + 1, subtype_string.size() - slash_pos - 1).to_int());
subtype_string = subtype_string.substr(0, slash_pos);
}
subtype_hint_string = p_hint_string.substr(hint_subtype_separator + 1, p_hint_string.size() - hint_subtype_separator - 1);
subtype = Variant::Type(subtype_string.to_int());
}
}
}
void EditorPropertyArray::_reorder_button_gui_input(const Ref<InputEvent> &p_event) {
if (reorder_from_index < 0) {
return;
}
Ref<InputEventMouseMotion> mm = p_event;
if (mm.is_valid()) {
Variant array = object->get_array();
int size = array.call("size");
if ((reorder_to_index == 0 && mm->get_relative().y < 0.0f) || (reorder_to_index == size - 1 && mm->get_relative().y > 0.0f)) {
return;
}
reorder_mouse_y_delta += mm->get_relative().y;
float required_y_distance = 20.0f * EDSCALE;
if (ABS(reorder_mouse_y_delta) > required_y_distance) {
int direction = reorder_mouse_y_delta > 0.0f ? 1 : -1;
reorder_mouse_y_delta -= required_y_distance * direction;
reorder_to_index += direction;
if ((direction < 0 && reorder_to_index % page_length == page_length - 1) || (direction > 0 && reorder_to_index % page_length == 0)) {
// Automatically move to the next/previous page.
page_slider->set_value(page_index + direction);
}
vbox->move_child(reorder_selected_element_hbox, reorder_to_index % page_length + 2);
// Ensure the moving element is visible.
EditorNode::get_singleton()->get_inspector()->ensure_control_visible(reorder_selected_element_hbox);
}
}
}
void EditorPropertyArray::_reorder_button_down(int p_index) {
reorder_from_index = p_index;
reorder_to_index = p_index;
reorder_selected_element_hbox = Object::cast_to<HBoxContainer>(vbox->get_child(p_index % page_length + 2));
reorder_selected_button = Object::cast_to<Button>(reorder_selected_element_hbox->get_child(0));
// Ideally it'd to be able to show the mouse but I had issues with
// Control's `mouse_exit()`/`mouse_entered()` signals not getting called.
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
}
void EditorPropertyArray::_reorder_button_up() {
if (reorder_from_index != reorder_to_index) {
// Move the element.
Variant array = object->get_array();
Variant value_to_move = array.get(reorder_from_index);
array.call("remove", reorder_from_index);
array.call("insert", reorder_to_index, value_to_move);
emit_changed(get_edited_property(), array, "", false);
object->set_array(array);
update_property();
}
reorder_from_index = -1;
reorder_to_index = -1;
reorder_mouse_y_delta = 0.0f;
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
reorder_selected_button->warp_mouse(reorder_selected_button->get_size() / 2.0f);
reorder_selected_element_hbox = nullptr;
reorder_selected_button = nullptr;
}
void EditorPropertyArray::_bind_methods() {
ClassDB::bind_method(D_METHOD("_can_drop_data_fw"), &EditorPropertyArray::can_drop_data_fw);
ClassDB::bind_method(D_METHOD("_drop_data_fw"), &EditorPropertyArray::drop_data_fw);
}
EditorPropertyArray::EditorPropertyArray() {
object.instantiate();
page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
edit = memnew(Button);
edit->set_h_size_flags(SIZE_EXPAND_FILL);
edit->set_clip_text(true);
edit->connect("pressed", callable_mp(this, &EditorPropertyArray::_edit_pressed));
edit->set_toggle_mode(true);
edit->set_drag_forwarding(this);
edit->connect("draw", callable_mp(this, &EditorPropertyArray::_button_draw));
add_child(edit);
add_focusable(edit);
vbox = nullptr;
page_slider = nullptr;
size_slider = nullptr;
updating = false;
change_type = memnew(PopupMenu);
add_child(change_type);
change_type->connect("id_pressed", callable_mp(this, &EditorPropertyArray::_change_type_menu));
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
String type = Variant::get_type_name(Variant::Type(i));
change_type->add_item(type, i);
}
change_type->add_separator();
change_type->add_item(TTR("Remove Item"), Variant::VARIANT_MAX);
changing_type_index = -1;
subtype = Variant::NIL;
subtype_hint = PROPERTY_HINT_NONE;
subtype_hint_string = "";
dropping = false;
}
///////////////////// DICTIONARY ///////////////////////////
void EditorPropertyDictionary::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
if (p_property == "new_item_key") {
object->set_new_item_key(p_value);
} else if (p_property == "new_item_value") {
object->set_new_item_value(p_value);
} else if (p_property.begins_with("indices")) {
int index = p_property.get_slice("/", 1).to_int();
Dictionary dict = object->get_dict();
Variant key = dict.get_key_at_index(index);
dict[key] = p_value;
emit_changed(get_edited_property(), dict, "", true);
dict = dict.duplicate(); // Duplicate, so undo/redo works better\.
object->set_dict(dict);
}
}
void EditorPropertyDictionary::_change_type(Object *p_button, int p_index) {
Button *button = Object::cast_to<Button>(p_button);
Rect2 rect = button->get_screen_rect();
change_type->set_as_minsize();
change_type->set_position(rect.get_end() - Vector2(change_type->get_contents_minimum_size().x, 0));
change_type->popup();
changing_type_index = p_index;
}
void EditorPropertyDictionary::_add_key_value() {
// Do not allow nil as valid key. I experienced errors with this
if (object->get_new_item_key().get_type() == Variant::NIL) {
return;
}
Dictionary dict = object->get_dict();
dict[object->get_new_item_key()] = object->get_new_item_value();
object->set_new_item_key(Variant());
object->set_new_item_value(Variant());
emit_changed(get_edited_property(), dict, "", false);
dict = dict.duplicate(); // Duplicate, so undo/redo works better.
object->set_dict(dict);
update_property();
}
void EditorPropertyDictionary::_change_type_menu(int p_index) {
if (changing_type_index < 0) {
Variant value;
Callable::CallError ce;
Variant::construct(Variant::Type(p_index), value, nullptr, 0, ce);
if (changing_type_index == -1) {
object->set_new_item_key(value);
} else {
object->set_new_item_value(value);
}
update_property();
return;
}
Dictionary dict = object->get_dict();
if (p_index < Variant::VARIANT_MAX) {
Variant value;
Callable::CallError ce;
Variant::construct(Variant::Type(p_index), value, nullptr, 0, ce);
Variant key = dict.get_key_at_index(changing_type_index);
dict[key] = value;
} else {
Variant key = dict.get_key_at_index(changing_type_index);
dict.erase(key);
}
emit_changed(get_edited_property(), dict, "", false);
dict = dict.duplicate(); // Duplicate, so undo/redo works better\.
object->set_dict(dict);
update_property();
}
void EditorPropertyDictionary::update_property() {
Variant updated_val = get_edited_object()->get(get_edited_property());
if (updated_val.get_type() == Variant::NIL) {
edit->set_text("Dictionary (Nil)"); // This provides symmetry with the array property.
edit->set_pressed(false);
if (vbox) {
set_bottom_editor(nullptr);
memdelete(vbox);
vbox = nullptr;
}
return;
}
Dictionary dict = updated_val;
edit->set_text("Dictionary (size " + itos(dict.size()) + ")");
bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
if (edit->is_pressed() != unfolded) {
edit->set_pressed(unfolded);
}
if (unfolded) {
updating = true;
if (!vbox) {
vbox = memnew(VBoxContainer);
add_child(vbox);
set_bottom_editor(vbox);
page_hbox = memnew(HBoxContainer);
vbox->add_child(page_hbox);
Label *label = memnew(Label(TTR("Page: ")));
label->set_h_size_flags(SIZE_EXPAND_FILL);
page_hbox->add_child(label);
page_slider = memnew(EditorSpinSlider);
page_slider->set_step(1);
page_hbox->add_child(page_slider);
page_slider->set_h_size_flags(SIZE_EXPAND_FILL);
page_slider->connect("value_changed", callable_mp(this, &EditorPropertyDictionary::_page_changed));
} else {
// Queue children for deletion, deleting immediately might cause errors.
for (int i = 1; i < vbox->get_child_count(); i++) {
vbox->get_child(i)->queue_delete();
}
}
int size = dict.size();
int pages = MAX(0, size - 1) / page_length + 1;
page_slider->set_max(pages);
page_index = MIN(page_index, pages - 1);
page_slider->set_value(page_index);
page_hbox->set_visible(pages > 1);
int offset = page_index * page_length;
int amount = MIN(size - offset, page_length);
dict = dict.duplicate();
object->set_dict(dict);
VBoxContainer *add_vbox = nullptr;
double default_float_step = EDITOR_GET("interface/inspector/default_float_step");
for (int i = 0; i < amount + 2; i++) {
String prop_name;
Variant key;
Variant value;
if (i < amount) {
prop_name = "indices/" + itos(i + offset);
key = dict.get_key_at_index(i + offset);
value = dict.get_value_at_index(i + offset);
} else if (i == amount) {
prop_name = "new_item_key";
value = object->get_new_item_key();
} else if (i == amount + 1) {
prop_name = "new_item_value";
value = object->get_new_item_value();
}
EditorProperty *prop = nullptr;
switch (value.get_type()) {
case Variant::NIL: {
prop = memnew(EditorPropertyNil);
} break;
// Atomic types.
case Variant::BOOL: {
prop = memnew(EditorPropertyCheck);
} break;
case Variant::INT: {
EditorPropertyInteger *editor = memnew(EditorPropertyInteger);
editor->setup(-100000, 100000, 1, true, true);
prop = editor;
} break;
case Variant::FLOAT: {
EditorPropertyFloat *editor = memnew(EditorPropertyFloat);
editor->setup(-100000, 100000, default_float_step, true, false, true, true);
prop = editor;
} break;
case Variant::STRING: {
prop = memnew(EditorPropertyText);
} break;
// Math types.
case Variant::VECTOR2: {
EditorPropertyVector2 *editor = memnew(EditorPropertyVector2);
editor->setup(-100000, 100000, default_float_step, true);
prop = editor;
} break;
case Variant::VECTOR2I: {
EditorPropertyVector2i *editor = memnew(EditorPropertyVector2i);
editor->setup(-100000, 100000, true);
prop = editor;
} break;
case Variant::RECT2: {
EditorPropertyRect2 *editor = memnew(EditorPropertyRect2);
editor->setup(-100000, 100000, default_float_step, true);
prop = editor;
} break;
case Variant::RECT2I: {
EditorPropertyRect2i *editor = memnew(EditorPropertyRect2i);
editor->setup(-100000, 100000, true);
prop = editor;
} break;
case Variant::VECTOR3: {
EditorPropertyVector3 *editor = memnew(EditorPropertyVector3);
editor->setup(-100000, 100000, default_float_step, true);
prop = editor;
} break;
case Variant::VECTOR3I: {
EditorPropertyVector3i *editor = memnew(EditorPropertyVector3i);
editor->setup(-100000, 100000, true);
prop = editor;
} break;
case Variant::TRANSFORM2D: {
EditorPropertyTransform2D *editor = memnew(EditorPropertyTransform2D);
editor->setup(-100000, 100000, default_float_step, true);
prop = editor;
} break;
case Variant::PLANE: {
EditorPropertyPlane *editor = memnew(EditorPropertyPlane);
editor->setup(-100000, 100000, default_float_step, true);
prop = editor;
} break;
case Variant::QUATERNION: {
EditorPropertyQuaternion *editor = memnew(EditorPropertyQuaternion);
editor->setup(-100000, 100000, default_float_step, true);
prop = editor;
} break;
case Variant::AABB: {
EditorPropertyAABB *editor = memnew(EditorPropertyAABB);
editor->setup(-100000, 100000, default_float_step, true);
prop = editor;
} break;
case Variant::BASIS: {
EditorPropertyBasis *editor = memnew(EditorPropertyBasis);
editor->setup(-100000, 100000, default_float_step, true);
prop = editor;
} break;
case Variant::TRANSFORM3D: {
EditorPropertyTransform3D *editor = memnew(EditorPropertyTransform3D);
editor->setup(-100000, 100000, default_float_step, true);
prop = editor;
} break;
// Miscellaneous types.
case Variant::COLOR: {
prop = memnew(EditorPropertyColor);
} break;
case Variant::STRING_NAME: {
EditorPropertyText *ept = memnew(EditorPropertyText);
ept->set_string_name(true);
prop = ept;
} break;
case Variant::NODE_PATH: {
prop = memnew(EditorPropertyNodePath);
} break;
case Variant::RID: {
prop = memnew(EditorPropertyRID);
} break;
case Variant::OBJECT: {
if (Object::cast_to<EncodedObjectAsID>(value)) {
EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
editor->setup("Object");
prop = editor;
} else {
EditorPropertyResource *editor = memnew(EditorPropertyResource);
editor->setup(object.ptr(), prop_name, "Resource");
prop = editor;
}
} break;
case Variant::DICTIONARY: {
prop = memnew(EditorPropertyDictionary);
} break;
case Variant::ARRAY: {
EditorPropertyArray *editor = memnew(EditorPropertyArray);
editor->setup(Variant::ARRAY);
prop = editor;
} break;
// Arrays.
case Variant::PACKED_BYTE_ARRAY: {
EditorPropertyArray *editor = memnew(EditorPropertyArray);
editor->setup(Variant::PACKED_BYTE_ARRAY);
prop = editor;
} break;
case Variant::PACKED_INT32_ARRAY: {
EditorPropertyArray *editor = memnew(EditorPropertyArray);
editor->setup(Variant::PACKED_INT32_ARRAY);
prop = editor;
} break;
case Variant::PACKED_FLOAT32_ARRAY: {
EditorPropertyArray *editor = memnew(EditorPropertyArray);
editor->setup(Variant::PACKED_FLOAT32_ARRAY);
prop = editor;
} break;
case Variant::PACKED_INT64_ARRAY: {
EditorPropertyArray *editor = memnew(EditorPropertyArray);
editor->setup(Variant::PACKED_INT64_ARRAY);
prop = editor;
} break;
case Variant::PACKED_FLOAT64_ARRAY: {
EditorPropertyArray *editor = memnew(EditorPropertyArray);
editor->setup(Variant::PACKED_FLOAT64_ARRAY);
prop = editor;
} break;
case Variant::PACKED_STRING_ARRAY: {
EditorPropertyArray *editor = memnew(EditorPropertyArray);
editor->setup(Variant::PACKED_STRING_ARRAY);
prop = editor;
} break;
case Variant::PACKED_VECTOR2_ARRAY: {
EditorPropertyArray *editor = memnew(EditorPropertyArray);
editor->setup(Variant::PACKED_VECTOR2_ARRAY);
prop = editor;
} break;
case Variant::PACKED_VECTOR3_ARRAY: {
EditorPropertyArray *editor = memnew(EditorPropertyArray);
editor->setup(Variant::PACKED_VECTOR3_ARRAY);
prop = editor;
} break;
case Variant::PACKED_COLOR_ARRAY: {
EditorPropertyArray *editor = memnew(EditorPropertyArray);
editor->setup(Variant::PACKED_COLOR_ARRAY);
prop = editor;
} break;
default: {
}
}
if (i == amount) {
PanelContainer *pc = memnew(PanelContainer);
vbox->add_child(pc);
Ref<StyleBoxFlat> flat;
flat.instantiate();
for (int j = 0; j < 4; j++) {
flat->set_default_margin(Side(j), 2 * EDSCALE);
}
flat->set_bg_color(get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
pc->add_theme_style_override("panel", flat);
add_vbox = memnew(VBoxContainer);
pc->add_child(add_vbox);
}
prop->set_object_and_property(object.ptr(), prop_name);
int change_index = 0;
if (i < amount) {
String cs = key.get_construct_string();
prop->set_label(key.get_construct_string());
prop->set_tooltip(cs);
change_index = i + offset;
} else if (i == amount) {
prop->set_label(TTR("New Key:"));
change_index = -1;
} else if (i == amount + 1) {
prop->set_label(TTR("New Value:"));
change_index = -2;
}
prop->set_selectable(false);
prop->connect("property_changed", callable_mp(this, &EditorPropertyDictionary::_property_changed));
prop->connect("object_id_selected", callable_mp(this, &EditorPropertyDictionary::_object_id_selected));
HBoxContainer *hbox = memnew(HBoxContainer);
if (add_vbox) {
add_vbox->add_child(hbox);
} else {
vbox->add_child(hbox);
}
hbox->add_child(prop);
prop->set_h_size_flags(SIZE_EXPAND_FILL);
Button *edit = memnew(Button);
edit->set_icon(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
hbox->add_child(edit);
edit->connect("pressed", callable_mp(this, &EditorPropertyDictionary::_change_type), varray(edit, change_index));
prop->update_property();
if (i == amount + 1) {
Button *butt_add_item = memnew(Button);
butt_add_item->set_text(TTR("Add Key/Value Pair"));
butt_add_item->connect("pressed", callable_mp(this, &EditorPropertyDictionary::_add_key_value));
add_vbox->add_child(butt_add_item);
}
}
updating = false;
} else {
if (vbox) {
set_bottom_editor(nullptr);
memdelete(vbox);
vbox = nullptr;
}
}
}
void EditorPropertyDictionary::_object_id_selected(const StringName &p_property, ObjectID p_id) {
emit_signal(SNAME("object_id_selected"), p_property, p_id);
}
void EditorPropertyDictionary::_notification(int p_what) {
}
void EditorPropertyDictionary::_edit_pressed() {
Variant prop_val = get_edited_object()->get(get_edited_property());
if (prop_val.get_type() == Variant::NIL) {
Callable::CallError ce;
Variant::construct(Variant::DICTIONARY, prop_val, nullptr, 0, ce);
get_edited_object()->set(get_edited_property(), prop_val);
}
get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
update_property();
}
void EditorPropertyDictionary::_page_changed(double p_page) {
if (updating) {
return;
}
page_index = p_page;
update_property();
}
void EditorPropertyDictionary::_bind_methods() {
}
EditorPropertyDictionary::EditorPropertyDictionary() {
object.instantiate();
page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
edit = memnew(Button);
edit->set_h_size_flags(SIZE_EXPAND_FILL);
edit->set_clip_text(true);
edit->connect("pressed", callable_mp(this, &EditorPropertyDictionary::_edit_pressed));
edit->set_toggle_mode(true);
add_child(edit);
add_focusable(edit);
vbox = nullptr;
page_slider = nullptr;
updating = false;
change_type = memnew(PopupMenu);
add_child(change_type);
change_type->connect("id_pressed", callable_mp(this, &EditorPropertyDictionary::_change_type_menu));
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
String type = Variant::get_type_name(Variant::Type(i));
change_type->add_item(type, i);
}
change_type->add_separator();
change_type->add_item(TTR("Remove Item"), Variant::VARIANT_MAX);
changing_type_index = -1;
}
|