Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/inspector/editor_inspector.cpp
20875 views
1
/**************************************************************************/
2
/* editor_inspector.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 "editor_inspector.h"
32
#include "editor_inspector.compat.inc"
33
34
#include "core/input/input.h"
35
#include "core/os/keyboard.h"
36
#include "editor/debugger/editor_debugger_inspector.h"
37
#include "editor/doc/doc_tools.h"
38
#include "editor/docks/inspector_dock.h"
39
#include "editor/editor_main_screen.h"
40
#include "editor/editor_node.h"
41
#include "editor/editor_string_names.h"
42
#include "editor/editor_undo_redo_manager.h"
43
#include "editor/gui/editor_toaster.h"
44
#include "editor/inspector/add_metadata_dialog.h"
45
#include "editor/inspector/editor_properties.h"
46
#include "editor/inspector/editor_property_name_processor.h"
47
#include "editor/inspector/editor_resource_picker.h"
48
#include "editor/inspector/multi_node_edit.h"
49
#include "editor/script/script_editor_plugin.h"
50
#include "editor/settings/editor_feature_profile.h"
51
#include "editor/settings/editor_settings.h"
52
#include "editor/themes/editor_scale.h"
53
#include "scene/gui/margin_container.h"
54
#include "scene/gui/separator.h"
55
#include "scene/gui/spin_box.h"
56
#include "scene/gui/texture_rect.h"
57
#include "scene/main/timer.h"
58
#include "scene/property_utils.h"
59
#include "scene/resources/packed_scene.h"
60
#include "scene/resources/style_box_flat.h"
61
#include "scene/scene_string_names.h"
62
63
void EditorInspectorActionButton::_notification(int p_what) {
64
switch (p_what) {
65
case NOTIFICATION_THEME_CHANGED: {
66
set_button_icon(get_editor_theme_icon(icon_name));
67
} break;
68
}
69
}
70
71
EditorInspectorActionButton::EditorInspectorActionButton(const String &p_text, const StringName &p_icon_name) {
72
icon_name = p_icon_name;
73
set_text(p_text);
74
set_theme_type_variation(SNAME("InspectorActionButton"));
75
set_h_size_flags(SIZE_SHRINK_CENTER);
76
}
77
78
bool EditorInspector::_property_path_matches(const String &p_property_path, const String &p_filter, EditorPropertyNameProcessor::Style p_style) {
79
if (p_property_path.containsn(p_filter)) {
80
return true;
81
}
82
83
const Vector<String> prop_sections = p_property_path.split("/");
84
for (int i = 0; i < prop_sections.size(); i++) {
85
if (p_filter.is_subsequence_ofn(EditorPropertyNameProcessor::get_singleton()->process_name(prop_sections[i], p_style, p_property_path))) {
86
return true;
87
}
88
}
89
return false;
90
}
91
92
bool EditorInspector::_resource_properties_matches(const Ref<Resource> &p_resource, const String &p_filter) {
93
String group;
94
String group_base;
95
String subgroup;
96
String subgroup_base;
97
98
List<PropertyInfo> plist;
99
p_resource->get_property_list(&plist, true);
100
101
// Employ a lighter version of the update_tree() property listing to find a match.
102
for (PropertyInfo &p : plist) {
103
if (p.usage & PROPERTY_USAGE_SUBGROUP) {
104
subgroup = p.name;
105
subgroup_base = p.hint_string.get_slicec(',', 0);
106
107
continue;
108
109
} else if (p.usage & PROPERTY_USAGE_GROUP) {
110
group = p.name;
111
group_base = p.hint_string.get_slicec(',', 0);
112
subgroup = "";
113
subgroup_base = "";
114
115
continue;
116
117
} else if (p.usage & PROPERTY_USAGE_CATEGORY) {
118
group = "";
119
group_base = "";
120
subgroup = "";
121
subgroup_base = "";
122
123
continue;
124
125
} else if (p.name.begins_with("metadata/_") || !(p.usage & PROPERTY_USAGE_EDITOR) || _is_property_disabled_by_feature_profile(p.name) ||
126
(p_filter.is_empty() && restrict_to_basic && !(p.usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
127
// Ignore properties that are not supposed to be in the inspector.
128
continue;
129
}
130
131
if (p.usage & PROPERTY_USAGE_HIGH_END_GFX && RS::get_singleton()->is_low_end()) {
132
// Do not show this property in low end gfx.
133
continue;
134
}
135
136
if (p.name == "script") {
137
// The script is always hidden in sub inspectors.
138
continue;
139
}
140
141
if (p.name.begins_with("metadata/") && bool(object->call(SNAME("_hide_metadata_from_inspector")))) {
142
// Hide metadata from inspector if required.
143
continue;
144
}
145
146
String path = p.name;
147
148
// Check if we exit or not a subgroup. If there is a prefix, remove it from the property label string.
149
if (!subgroup.is_empty() && !subgroup_base.is_empty()) {
150
if (path.begins_with(subgroup_base)) {
151
path = path.trim_prefix(subgroup_base);
152
} else if (subgroup_base.begins_with(path)) {
153
// Keep it, this is used pretty often.
154
} else {
155
subgroup = ""; // The prefix changed, we are no longer in the subgroup.
156
}
157
}
158
159
// Check if we exit or not a group. If there is a prefix, remove it from the property label string.
160
if (!group.is_empty() && !group_base.is_empty() && subgroup.is_empty()) {
161
if (path.begins_with(group_base)) {
162
path = path.trim_prefix(group_base);
163
} else if (group_base.begins_with(path)) {
164
// Keep it, this is used pretty often.
165
} else {
166
group = ""; // The prefix changed, we are no longer in the group.
167
subgroup = "";
168
}
169
}
170
171
// Add the group and subgroup to the path.
172
if (!subgroup.is_empty()) {
173
path = subgroup + "/" + path;
174
}
175
if (!group.is_empty()) {
176
path = group + "/" + path;
177
}
178
179
// Get the property label's string.
180
String name_override = (path.contains_char('/')) ? path.substr(path.rfind_char('/') + 1) : path;
181
const int dot = name_override.find_char('.');
182
if (dot != -1) {
183
name_override = name_override.substr(0, dot);
184
}
185
186
// Remove the property from the path.
187
int idx = path.rfind_char('/');
188
if (idx > -1) {
189
path = path.left(idx);
190
} else {
191
path = "";
192
}
193
194
// Check if the property matches the filter.
195
const String property_path = (path.is_empty() ? "" : path + "/") + name_override;
196
if (_property_path_matches(property_path, p_filter, property_name_style)) {
197
return true;
198
}
199
200
// Check if the sub-resource has any properties that match the filter.
201
if (p.hint && p.hint == PROPERTY_HINT_RESOURCE_TYPE) {
202
Ref<Resource> res = p_resource->get(p.name);
203
if (res.is_valid() && _resource_properties_matches(res, p_filter)) {
204
return true;
205
}
206
}
207
}
208
209
return false;
210
}
211
212
String EditorProperty::get_tooltip_string(const String &p_string) const {
213
// Trim to 100 characters to prevent the tooltip from being too long.
214
constexpr int TOOLTIP_MAX_LENGTH = 100;
215
return p_string.left(TOOLTIP_MAX_LENGTH).strip_edges() + String((p_string.length() > TOOLTIP_MAX_LENGTH) ? "..." : "");
216
}
217
218
Size2 EditorProperty::get_minimum_size() const {
219
if (theme_cache.font.is_null()) {
220
// Too early.
221
return Vector2();
222
}
223
224
Size2 ms = Size2(0, theme_cache.inspector_property_height);
225
for (int i = 0; i < get_child_count(); i++) {
226
Control *c = as_sortable_control(get_child(i));
227
if (!c) {
228
continue;
229
}
230
if (c == bottom_editor) {
231
continue;
232
}
233
if (c == left_container) {
234
continue;
235
}
236
if (c == right_container) {
237
continue;
238
}
239
240
Size2 minsize = c->get_combined_minimum_size();
241
ms = ms.max(minsize);
242
}
243
244
if (keying) {
245
ms.width += theme_cache.key_icon->get_width() + theme_cache.padding + theme_cache.horizontal_separation;
246
}
247
248
if (deletable) {
249
ms.width += theme_cache.delete_icon->get_width() + theme_cache.padding + theme_cache.horizontal_separation;
250
}
251
252
if (checkable) {
253
ms.width += theme_cache.checked_icon->get_width() + theme_cache.padding + theme_cache.horizontal_separation;
254
}
255
256
Size2 ls = left_container->get_combined_minimum_size();
257
ms.width += ls.x;
258
ms.height = MAX(ms.height, ls.y);
259
260
Size2 rs = right_container->get_combined_minimum_size();
261
ms.width += rs.x;
262
ms.height = MAX(ms.height, rs.y);
263
264
if (bottom_editor != nullptr && bottom_editor->is_visible()) {
265
ms.height += label.is_empty() ? 0 : _get_v_separation();
266
Size2 bems = bottom_editor->get_combined_minimum_size();
267
ms.height += bems.height;
268
ms.width = MAX(ms.width, bems.width);
269
}
270
271
return ms;
272
}
273
274
void EditorProperty::emit_changed(const StringName &p_property, const Variant &p_value, const StringName &p_field, bool p_changing) {
275
Variant args[4] = { p_property, p_value, p_field, p_changing };
276
const Variant *argptrs[4] = { &args[0], &args[1], &args[2], &args[3] };
277
278
cache[p_property] = p_value;
279
emit_signalp(SNAME("property_changed"), (const Variant **)argptrs, 4);
280
}
281
282
void EditorProperty::_notification(int p_what) {
283
switch (p_what) {
284
case NOTIFICATION_THEME_CHANGED: {
285
EditorInspector::initialize_property_theme(theme_cache, this);
286
} break;
287
288
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
289
RID ae = get_accessibility_element();
290
ERR_FAIL_COND(ae.is_null());
291
292
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
293
294
DisplayServer::get_singleton()->accessibility_update_set_name(ae, vformat(TTR("Property: %s"), label));
295
DisplayServer::get_singleton()->accessibility_update_set_value(ae, vformat(TTR("Property: %s"), label));
296
297
DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_MENU);
298
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &EditorProperty::_accessibility_action_menu));
299
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &EditorProperty::_accessibility_action_click));
300
301
DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_READONLY, read_only);
302
if (checkable) {
303
DisplayServer::get_singleton()->accessibility_update_set_checked(ae, checked);
304
}
305
} break;
306
307
case NOTIFICATION_SORT_CHILDREN: {
308
Size2 size = get_size();
309
Rect2 rect;
310
Rect2 bottom_rect;
311
312
right_child_rect = Rect2();
313
bottom_child_rect = Rect2();
314
315
{
316
int child_room = size.width * (1.0 - split_ratio);
317
int separation = 4 * EDSCALE;
318
int height = theme_cache.inspector_property_height;
319
int half_padding = theme_cache.padding / 2;
320
bool no_children = true;
321
322
//compute room needed
323
for (int i = 0; i < get_child_count(); i++) {
324
Control *c = as_sortable_control(get_child(i));
325
if (!c) {
326
continue;
327
}
328
if (c == bottom_editor) {
329
continue;
330
}
331
332
Size2 minsize = c->get_combined_minimum_size();
333
if (c != left_container && c != right_container) {
334
child_room = MAX(child_room, minsize.width);
335
no_children = false;
336
}
337
height = MAX(height, minsize.height);
338
}
339
340
if (no_children) {
341
text_size = size.width;
342
rect = Rect2(size.width - 1, 0, 1, height);
343
} else if (!draw_label) {
344
text_size = 0;
345
rect = Rect2(1, 0, size.width - 1, height);
346
} else {
347
text_size = MAX(0, size.width - (child_room + separation));
348
if (is_layout_rtl()) {
349
rect = Rect2(1, 0, child_room, height);
350
} else {
351
rect = Rect2(size.width - child_room, 0, child_room, height);
352
}
353
}
354
355
if (rect.size.x > 1) {
356
rect.size.x -= right_container->get_combined_minimum_size().x;
357
if (is_layout_rtl()) {
358
rect.position.x += right_container->get_combined_minimum_size().x;
359
}
360
}
361
362
if (bottom_editor) {
363
int v_offset = label.is_empty() ? 0 : _get_v_separation();
364
bottom_rect = Rect2(0, rect.size.height + v_offset, size.width, bottom_editor->get_combined_minimum_size().height);
365
}
366
367
if (keying) {
368
Ref<Texture2D> key;
369
370
if (use_keying_next()) {
371
key = theme_cache.key_next_icon;
372
} else {
373
key = theme_cache.key_icon;
374
}
375
376
rect.size.x -= key->get_width() + half_padding + theme_cache.horizontal_separation;
377
if (is_layout_rtl()) {
378
rect.position.x += key->get_width() + half_padding + theme_cache.horizontal_separation;
379
}
380
381
if (no_children) {
382
// Use full padding to avoid overlapping with the revert button.
383
text_size -= key->get_width() + half_padding * 2 + separation;
384
}
385
}
386
387
if (deletable) {
388
rect.size.x -= theme_cache.delete_icon->get_width() + half_padding + theme_cache.horizontal_separation;
389
390
if (is_layout_rtl()) {
391
rect.position.x += theme_cache.delete_icon->get_width() + half_padding + theme_cache.horizontal_separation;
392
}
393
394
if (no_children) {
395
text_size -= theme_cache.delete_icon->get_width() + half_padding + separation;
396
}
397
}
398
399
// Account for the space needed on the outer side
400
// when any of the icons are visible.
401
if (keying || deletable) {
402
separation = theme_cache.horizontal_separation;
403
rect.size.x -= separation;
404
405
if (is_layout_rtl()) {
406
rect.position.x += separation;
407
}
408
}
409
}
410
411
//set children
412
for (int i = 0; i < get_child_count(); i++) {
413
Control *c = as_sortable_control(get_child(i));
414
if (!c) {
415
continue;
416
}
417
if (c == bottom_editor) {
418
continue;
419
}
420
if (c == left_container) {
421
continue;
422
}
423
if (c == right_container) {
424
continue;
425
}
426
427
fit_child_in_rect(c, rect);
428
right_child_rect = rect;
429
}
430
431
if (bottom_editor) {
432
fit_child_in_rect(bottom_editor, bottom_rect);
433
bottom_child_rect = bottom_rect;
434
}
435
436
Size2 rs = right_container->get_combined_minimum_size();
437
rs.y = MAX(rs.y, rect.size.y);
438
if (is_layout_rtl()) {
439
fit_child_in_rect(right_container, Rect2(0, 0, rs.width, rs.y));
440
} else {
441
fit_child_in_rect(right_container, Rect2(size.width - rs.width, 0, rs.width, rs.y));
442
}
443
444
Size2 ls = left_container->get_combined_minimum_size();
445
real_t right_size = rect.size.x + rs.x;
446
ls.y = MAX(ls.y, rect.size.y);
447
if (is_layout_rtl()) {
448
fit_child_in_rect(left_container, Rect2(right_size, 0, size.x - right_size, ls.y));
449
} else {
450
fit_child_in_rect(left_container, Rect2(0, 0, size.x - right_size, ls.y));
451
}
452
453
queue_redraw(); //need to redraw text
454
} break;
455
456
case NOTIFICATION_DRAW: {
457
Ref<Font> font = theme_cache.font;
458
int font_size = theme_cache.font_size;
459
bool rtl = is_layout_rtl();
460
461
Size2 size = get_size();
462
if (bottom_editor) {
463
size.height = bottom_editor->get_offset(SIDE_TOP) - _get_v_separation();
464
} else if (label_reference) {
465
size.height = label_reference->get_size().height;
466
}
467
size.height = MAX(size.height, left_container->get_size().height);
468
469
// Only draw the label if it's not empty.
470
if (label.is_empty()) {
471
size.height = 0;
472
} else if (sub_inspector_color_level >= 0 && theme_cache.sub_inspector_background[sub_inspector_color_level].is_valid()) {
473
draw_style_box(theme_cache.sub_inspector_background[sub_inspector_color_level], Rect2(Vector2(), size));
474
} else {
475
draw_style_box(selected ? theme_cache.background_selected : theme_cache.background, Rect2(Vector2(), size));
476
}
477
478
if (draw_top_bg && right_child_rect != Rect2() && draw_background) {
479
draw_style_box(theme_cache.child_background, right_child_rect);
480
}
481
if (bottom_child_rect != Rect2() && draw_background) {
482
draw_style_box(theme_cache.child_background, bottom_child_rect);
483
}
484
485
Color color;
486
if (draw_warning || draw_prop_warning) {
487
color = is_read_only() ? theme_cache.readonly_warning_color : theme_cache.warning_color;
488
} else if (is_read_only()) {
489
color = (sub_inspector_color_level >= 0) ? theme_cache.sub_inspector_property_color : theme_cache.readonly_property_color;
490
} else {
491
color = (sub_inspector_color_level >= 0) ? theme_cache.sub_inspector_property_color : theme_cache.property_color;
492
}
493
if (label.contains_char('.')) {
494
// FIXME: Move this to the project settings editor, as this is only used
495
// for project settings feature tag overrides.
496
color.a = 0.5;
497
}
498
499
int ofs = theme_cache.font_offset;
500
int text_limit = text_size - ofs;
501
int half_padding = EDITOR_GET("interface/theme/base_spacing");
502
int padding = half_padding * 2;
503
504
int left_ofs = left_container->get_combined_minimum_size().x;
505
ofs += left_ofs;
506
text_limit -= left_ofs;
507
508
if (checkable) {
509
Ref<Texture2D> checkbox;
510
if (checked) {
511
checkbox = theme_cache.checked_icon;
512
} else {
513
checkbox = theme_cache.unchecked_icon;
514
}
515
516
check_rect = Rect2(ofs, 0, checkbox->get_width() + padding, size.height);
517
518
Point2 rtl_pos;
519
if (rtl) {
520
rtl_pos = Point2(size.width - check_rect.position.x - (checkbox->get_width() + padding + (1 * EDSCALE)), check_rect.position.y);
521
}
522
523
Color color2(1, 1, 1);
524
if (check_hover) {
525
color2.r *= 1.2;
526
color2.g *= 1.2;
527
color2.b *= 1.2;
528
529
if (rtl) {
530
draw_style_box(theme_cache.hover, Rect2(rtl_pos, check_rect.size));
531
} else {
532
draw_style_box(theme_cache.hover, check_rect);
533
}
534
}
535
536
Point2 icon_ofs = (Point2(padding, size.height - checkbox->get_height()) / 2).round();
537
if (rtl) {
538
draw_texture(checkbox, rtl_pos + icon_ofs, color2);
539
} else {
540
draw_texture(checkbox, check_rect.position + icon_ofs, color2);
541
}
542
543
int check_ofs = checkbox->get_width() + padding + theme_cache.horizontal_separation;
544
ofs += check_ofs;
545
text_limit -= check_ofs;
546
} else {
547
check_rect = Rect2();
548
}
549
550
if (can_revert && !is_read_only()) {
551
const Ref<Texture2D> &reload_icon = theme_cache.revert_icon;
552
text_limit -= reload_icon->get_width() + half_padding + theme_cache.horizontal_separation;
553
revert_rect = Rect2(ofs + text_limit, 0, reload_icon->get_width() + padding + (1 * EDSCALE), size.height);
554
555
Point2 rtl_pos;
556
if (rtl) {
557
rtl_pos = Point2(size.width - revert_rect.position.x - (reload_icon->get_width() + padding + (1 * EDSCALE)), revert_rect.position.y);
558
}
559
560
Color color2(1, 1, 1);
561
if (revert_hover) {
562
color2.r *= 1.2;
563
color2.g *= 1.2;
564
color2.b *= 1.2;
565
566
if (rtl) {
567
draw_style_box(theme_cache.hover, Rect2(rtl_pos, revert_rect.size));
568
} else {
569
draw_style_box(theme_cache.hover, revert_rect);
570
}
571
}
572
573
Point2 icon_ofs = (Point2(padding, size.height - reload_icon->get_height()) / 2).round();
574
if (rtl) {
575
draw_texture(reload_icon, rtl_pos + icon_ofs, color2);
576
} else {
577
draw_texture(reload_icon, revert_rect.position + icon_ofs, color2);
578
}
579
580
text_limit -= half_padding;
581
} else {
582
revert_rect = Rect2();
583
}
584
585
if (!pin_hidden && pinned) {
586
const Ref<Texture2D> &pinned_icon = theme_cache.pin_icon;
587
int margin_w = theme_cache.horizontal_separation;
588
int total_icon_w = margin_w + pinned_icon->get_width();
589
int text_w = font->get_string_size(label, rtl ? HORIZONTAL_ALIGNMENT_RIGHT : HORIZONTAL_ALIGNMENT_LEFT, text_limit - total_icon_w, font_size).x;
590
int y = (size.height - pinned_icon->get_height()) / 2;
591
if (rtl) {
592
draw_texture(pinned_icon, Vector2(size.width - ofs - text_w - total_icon_w, y), color);
593
} else {
594
draw_texture(pinned_icon, Vector2(ofs + text_w + margin_w, y), color);
595
}
596
text_limit -= total_icon_w;
597
}
598
599
int v_ofs = (size.height - font->get_height(font_size)) / 2;
600
if (rtl) {
601
draw_string(font, Point2(size.width - ofs - text_limit, v_ofs + font->get_ascent(font_size)), label, HORIZONTAL_ALIGNMENT_RIGHT, text_limit, font_size, color);
602
} else {
603
draw_string(font, Point2(ofs, v_ofs + font->get_ascent(font_size)), label, HORIZONTAL_ALIGNMENT_LEFT, text_limit, font_size, color);
604
}
605
606
ofs = size.width;
607
ofs -= right_container->get_minimum_size().x;
608
609
if (keying) {
610
Ref<Texture2D> key;
611
612
if (use_keying_next()) {
613
key = theme_cache.key_next_icon;
614
} else {
615
key = theme_cache.key_icon;
616
}
617
618
ofs -= key->get_width() + half_padding + theme_cache.horizontal_separation;
619
keying_rect = Rect2(ofs, 0, key->get_width() + padding, size.height);
620
621
Point2 rtl_pos;
622
if (rtl) {
623
rtl_pos = Point2(size.width - keying_rect.position.x - (key->get_width() + padding), keying_rect.position.y);
624
}
625
626
Color color2(1, 1, 1);
627
if (keying_hover) {
628
color2.r *= 1.2;
629
color2.g *= 1.2;
630
color2.b *= 1.2;
631
632
if (rtl) {
633
draw_style_box(theme_cache.hover, Rect2(rtl_pos, keying_rect.size));
634
} else {
635
draw_style_box(theme_cache.hover, keying_rect);
636
}
637
}
638
639
Point2 icon_ofs = (Point2(padding, size.height - key->get_height()) / 2).round();
640
if (rtl) {
641
draw_texture(key, rtl_pos + icon_ofs, color2);
642
} else {
643
draw_texture(key, keying_rect.position + icon_ofs, color2);
644
}
645
646
} else {
647
keying_rect = Rect2();
648
}
649
650
if (deletable) {
651
const Ref<Texture2D> &close = theme_cache.delete_icon;
652
653
ofs -= close->get_width() + half_padding + theme_cache.horizontal_separation;
654
delete_rect = Rect2(ofs, 0, close->get_width() + padding, size.height);
655
656
Point2 rtl_pos;
657
if (rtl) {
658
rtl_pos = Point2(size.width - delete_rect.position.x - (close->get_width() + padding), delete_rect.position.y);
659
}
660
661
Color color2(1, 1, 1);
662
if (delete_hover) {
663
color2.r *= 1.2;
664
color2.g *= 1.2;
665
color2.b *= 1.2;
666
667
if (rtl) {
668
draw_style_box(theme_cache.hover, Rect2(rtl_pos, delete_rect.size));
669
} else {
670
draw_style_box(theme_cache.hover, delete_rect);
671
}
672
}
673
674
Point2 icon_ofs = (Point2(padding, size.height - close->get_height()) / 2).round();
675
if (rtl) {
676
draw_texture(close, rtl_pos + icon_ofs, color2);
677
} else {
678
draw_texture(close, delete_rect.position + icon_ofs, color2);
679
}
680
} else {
681
delete_rect = Rect2();
682
}
683
} break;
684
685
case NOTIFICATION_ENTER_TREE: {
686
EditorInspector *inspector = get_parent_inspector();
687
if (inspector) {
688
inspector = inspector->get_root_inspector();
689
}
690
set_shortcut_context(inspector);
691
692
if (has_borders) {
693
get_parent()->connect(SceneStringName(theme_changed), callable_mp(this, &EditorProperty::_update_property_bg));
694
_update_property_bg();
695
}
696
} break;
697
698
case NOTIFICATION_EXIT_TREE: {
699
if (has_borders) {
700
get_parent()->disconnect(SceneStringName(theme_changed), callable_mp(this, &EditorProperty::_update_property_bg));
701
}
702
} break;
703
704
case NOTIFICATION_MOUSE_EXIT_SELF:
705
case NOTIFICATION_MOUSE_EXIT: {
706
if (keying_hover || revert_hover || check_hover || delete_hover) {
707
keying_hover = false;
708
revert_hover = false;
709
check_hover = false;
710
delete_hover = false;
711
queue_redraw();
712
}
713
} break;
714
}
715
}
716
717
void EditorProperty::set_label(const String &p_label) {
718
label = p_label;
719
queue_redraw();
720
}
721
722
String EditorProperty::get_label() const {
723
return label;
724
}
725
726
Object *EditorProperty::get_edited_object() {
727
return object;
728
}
729
730
StringName EditorProperty::get_edited_property() const {
731
return property;
732
}
733
734
Variant EditorProperty::get_edited_property_display_value() const {
735
ERR_FAIL_NULL_V(object, Variant());
736
Control *control = Object::cast_to<Control>(object);
737
if (checkable && !checked && control && String(property).begins_with("theme_override_")) {
738
return control->get_used_theme_item(property);
739
} else {
740
return get_edited_property_value();
741
}
742
}
743
744
EditorInspector *EditorProperty::get_parent_inspector() const {
745
Node *parent = get_parent();
746
while (parent) {
747
EditorInspector *ei = Object::cast_to<EditorInspector>(parent);
748
if (ei) {
749
return ei;
750
}
751
parent = parent->get_parent();
752
}
753
return nullptr;
754
}
755
756
void EditorProperty::set_doc_path(const String &p_doc_path) {
757
doc_path = p_doc_path;
758
}
759
760
void EditorProperty::set_internal(bool p_internal) {
761
internal = p_internal;
762
}
763
764
void EditorProperty::update_property() {
765
GDVIRTUAL_CALL(_update_property);
766
}
767
768
void EditorProperty::_set_read_only(bool p_read_only) {
769
}
770
771
void EditorProperty::set_read_only(bool p_read_only) {
772
read_only = p_read_only;
773
if (GDVIRTUAL_CALL(_set_read_only, p_read_only)) {
774
return;
775
}
776
_set_read_only(p_read_only);
777
}
778
779
bool EditorProperty::is_read_only() const {
780
return read_only;
781
}
782
783
Variant EditorPropertyRevert::get_property_revert_value(Object *p_object, const StringName &p_property, bool *r_is_valid) {
784
if (p_object->property_can_revert(p_property)) {
785
if (r_is_valid) {
786
*r_is_valid = true;
787
}
788
return p_object->property_get_revert(p_property);
789
}
790
791
return PropertyUtils::get_property_default_value(p_object, p_property, r_is_valid);
792
}
793
794
bool EditorPropertyRevert::can_property_revert(Object *p_object, const StringName &p_property, const Variant *p_custom_current_value) {
795
bool is_valid_revert = false;
796
Variant revert_value = EditorPropertyRevert::get_property_revert_value(p_object, p_property, &is_valid_revert);
797
if (!is_valid_revert) {
798
return false;
799
}
800
Variant current_value = p_custom_current_value ? *p_custom_current_value : p_object->get(p_property);
801
return PropertyUtils::is_property_value_different(p_object, current_value, revert_value);
802
}
803
804
StringName EditorProperty::_get_revert_property() const {
805
return property;
806
}
807
808
void EditorProperty::_update_property_bg() {
809
// This function is to be called on EditorPropertyResource, EditorPropertyArray, and EditorPropertyDictionary.
810
// Behavior is undetermined on any other EditorProperty.
811
if (!is_inside_tree()) {
812
return;
813
}
814
815
if (bottom_editor) {
816
ColorationMode nested_color_mode = (ColorationMode)(int)EDITOR_GET("interface/inspector/nested_color_mode");
817
bool delimitate_all_container_and_resources = EDITOR_GET("interface/inspector/delimitate_all_container_and_resources");
818
819
sub_inspector_color_level = 0;
820
if (is_colored(nested_color_mode)) {
821
Node *n = this;
822
while (n) {
823
EditorProperty *ep = Object::cast_to<EditorProperty>(n);
824
if (ep && ep->is_colored(nested_color_mode)) {
825
sub_inspector_color_level++;
826
if (sub_inspector_color_level == 16) {
827
break;
828
}
829
}
830
n = n->get_parent();
831
}
832
}
833
add_theme_style_override(SNAME("DictionaryAddItem"), get_theme_stylebox("DictionaryAddItem" + itos(sub_inspector_color_level), EditorStringName(EditorStyles)));
834
if (delimitate_all_container_and_resources || is_colored(nested_color_mode)) {
835
bottom_editor->add_theme_style_override(SceneStringName(panel), get_theme_stylebox("sub_inspector_bg" + itos(sub_inspector_color_level), EditorStringName(EditorStyles)));
836
} else {
837
bottom_editor->add_theme_style_override(SceneStringName(panel), get_theme_stylebox("sub_inspector_bg_no_border", EditorStringName(EditorStyles)));
838
sub_inspector_color_level = -1;
839
}
840
} else {
841
sub_inspector_color_level = -1;
842
}
843
queue_redraw();
844
}
845
846
void EditorProperty::update_editor_property_status() {
847
if (property == StringName()) {
848
return; //no property, so nothing to do
849
}
850
851
bool new_pinned = false;
852
if (can_pin) {
853
Node *node = Object::cast_to<Node>(object);
854
CRASH_COND(!node);
855
new_pinned = node->is_property_pinned(property);
856
}
857
858
bool new_warning = false;
859
if (object->has_method("_get_property_warning")) {
860
new_warning = !String(object->call("_get_property_warning", property)).is_empty();
861
}
862
863
Variant current = object->get(_get_revert_property());
864
bool new_can_revert = EditorPropertyRevert::can_property_revert(object, property, &current) && !is_read_only();
865
866
bool new_checked = checked;
867
if (checkable) { // for properties like theme overrides.
868
bool valid = false;
869
Variant value = object->get(property, &valid);
870
if (valid) {
871
new_checked = value.get_type() != Variant::NIL;
872
}
873
}
874
875
if (new_can_revert != can_revert || new_pinned != pinned || new_checked != checked || new_warning != draw_prop_warning) {
876
if (new_can_revert != can_revert) {
877
emit_signal(SNAME("property_can_revert_changed"), property, new_can_revert);
878
}
879
draw_prop_warning = new_warning;
880
can_revert = new_can_revert;
881
pinned = new_pinned;
882
checked = new_checked;
883
queue_redraw();
884
}
885
}
886
887
bool EditorProperty::use_keying_next() const {
888
List<PropertyInfo> plist;
889
object->get_property_list(&plist, true);
890
891
for (const PropertyInfo &p : plist) {
892
if (p.name == property) {
893
return (p.usage & PROPERTY_USAGE_KEYING_INCREMENTS);
894
}
895
}
896
897
return false;
898
}
899
900
void EditorProperty::set_draw_label(bool p_draw_label) {
901
draw_label = p_draw_label;
902
queue_redraw();
903
queue_sort();
904
}
905
906
bool EditorProperty::is_draw_label() const {
907
return draw_label;
908
}
909
910
void EditorProperty::set_draw_background(bool p_draw_background) {
911
draw_background = p_draw_background;
912
queue_redraw();
913
}
914
915
bool EditorProperty::is_draw_background() const {
916
return draw_background;
917
}
918
919
void EditorProperty::set_checkable(bool p_checkable) {
920
checkable = p_checkable;
921
queue_redraw();
922
queue_sort();
923
}
924
925
bool EditorProperty::is_checkable() const {
926
return checkable;
927
}
928
929
void EditorProperty::set_checked(bool p_checked) {
930
checked = p_checked;
931
queue_redraw();
932
}
933
934
bool EditorProperty::is_checked() const {
935
return checked;
936
}
937
938
void EditorProperty::set_draw_warning(bool p_draw_warning) {
939
draw_warning = p_draw_warning;
940
queue_redraw();
941
}
942
943
void EditorProperty::set_keying(bool p_keying) {
944
keying = p_keying;
945
queue_redraw();
946
queue_sort();
947
}
948
949
void EditorProperty::set_deletable(bool p_deletable) {
950
deletable = p_deletable;
951
queue_redraw();
952
queue_sort();
953
}
954
955
bool EditorProperty::is_deletable() const {
956
return deletable;
957
}
958
959
bool EditorProperty::is_keying() const {
960
return keying;
961
}
962
963
bool EditorProperty::is_draw_warning() const {
964
return draw_warning;
965
}
966
967
void EditorProperty::_focusable_focused(int p_index) {
968
if (!selectable) {
969
return;
970
}
971
bool already_selected = selected;
972
selected = true;
973
selected_focusable = p_index;
974
queue_redraw();
975
if (!already_selected && selected) {
976
emit_signal(SNAME("selected"), property, selected_focusable);
977
}
978
}
979
980
void EditorProperty::add_focusable(Control *p_control) {
981
p_control->connect(SceneStringName(focus_entered), callable_mp(this, &EditorProperty::_focusable_focused).bind(focusables.size()));
982
focusables.push_back(p_control);
983
}
984
985
void EditorProperty::grab_focus(int p_focusable) {
986
if (focusables.is_empty()) {
987
return;
988
}
989
990
if (p_focusable >= 0) {
991
ERR_FAIL_INDEX(p_focusable, focusables.size());
992
focusables[p_focusable]->grab_focus(true);
993
} else {
994
focusables[0]->grab_focus(true);
995
}
996
}
997
998
void EditorProperty::select(int p_focusable) {
999
bool already_selected = selected;
1000
if (!selectable) {
1001
return;
1002
}
1003
1004
if (p_focusable >= 0) {
1005
ERR_FAIL_INDEX(p_focusable, focusables.size());
1006
focusables[p_focusable]->grab_focus(true);
1007
} else {
1008
selected = true;
1009
queue_redraw();
1010
}
1011
1012
if (!already_selected && selected) {
1013
emit_signal(SNAME("selected"), property, selected_focusable);
1014
}
1015
}
1016
1017
void EditorProperty::deselect() {
1018
selected = false;
1019
selected_focusable = -1;
1020
queue_redraw();
1021
}
1022
1023
bool EditorProperty::is_selected() const {
1024
return selected;
1025
}
1026
1027
void EditorProperty::add_inline_control(Control *p_control, InlineControlSide p_side) {
1028
Node *parent = p_control->get_parent();
1029
if (parent != nullptr) {
1030
parent->remove_child(p_control);
1031
}
1032
if (p_side == INLINE_CONTROL_LEFT) {
1033
left_container->add_child(p_control);
1034
} else {
1035
right_container->add_child(p_control);
1036
}
1037
}
1038
1039
void EditorProperty::set_label_overlayed(bool p_overlay) {
1040
label_overlayed = p_overlay;
1041
}
1042
1043
void EditorProperty::gui_input(const Ref<InputEvent> &p_event) {
1044
ERR_FAIL_COND(p_event.is_null());
1045
1046
if (property == StringName()) {
1047
return;
1048
}
1049
1050
Ref<InputEventMouse> me = p_event;
1051
1052
if (me.is_valid()) {
1053
Vector2 mpos = me->get_position();
1054
if (bottom_child_rect.has_point(mpos)) {
1055
return; // Makes child EditorProperties behave like sibling nodes when handling mouse events.
1056
}
1057
if (is_layout_rtl()) {
1058
mpos.x = get_size().x - mpos.x;
1059
}
1060
bool button_left = me->get_button_mask().has_flag(MouseButtonMask::LEFT);
1061
1062
bool new_keying_hover = keying_rect.has_point(mpos) && !button_left;
1063
if (new_keying_hover != keying_hover) {
1064
keying_hover = new_keying_hover;
1065
queue_redraw();
1066
}
1067
1068
bool new_delete_hover = delete_rect.has_point(mpos) && !button_left;
1069
if (new_delete_hover != delete_hover) {
1070
delete_hover = new_delete_hover;
1071
queue_redraw();
1072
}
1073
1074
bool new_revert_hover = revert_rect.has_point(mpos) && !button_left;
1075
if (new_revert_hover != revert_hover) {
1076
revert_hover = new_revert_hover;
1077
queue_redraw();
1078
}
1079
1080
bool new_check_hover = check_rect.has_point(mpos) && !button_left;
1081
if (new_check_hover != check_hover) {
1082
check_hover = new_check_hover;
1083
queue_redraw();
1084
}
1085
}
1086
1087
Ref<InputEventMouseButton> mb = p_event;
1088
1089
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
1090
Vector2 mpos = mb->get_position();
1091
if (is_layout_rtl()) {
1092
mpos.x = get_size().x - mpos.x;
1093
}
1094
1095
select();
1096
1097
if (keying_rect.has_point(mpos)) {
1098
accept_event();
1099
emit_signal(SNAME("property_keyed"), property, use_keying_next());
1100
1101
if (use_keying_next()) {
1102
if (property == "frame_coords" && (object->is_class("Sprite2D") || object->is_class("Sprite3D"))) {
1103
Vector2i new_coords = object->get(property);
1104
new_coords.x++;
1105
if (new_coords.x >= int64_t(object->get("hframes"))) {
1106
new_coords.x = 0;
1107
new_coords.y++;
1108
}
1109
if (new_coords.x < int64_t(object->get("hframes")) && new_coords.y < int64_t(object->get("vframes"))) {
1110
callable_mp(this, &EditorProperty::emit_changed).call_deferred(property, new_coords, "", false);
1111
}
1112
} else {
1113
if (int64_t(object->get(property)) + 1 < (int64_t(object->get("hframes")) * int64_t(object->get("vframes")))) {
1114
callable_mp(this, &EditorProperty::emit_changed).call_deferred(property, object->get(property).operator int64_t() + 1, "", false);
1115
}
1116
}
1117
callable_mp(this, &EditorProperty::update_property).call_deferred();
1118
}
1119
}
1120
if (delete_rect.has_point(mpos)) {
1121
accept_event();
1122
emit_signal(SNAME("property_deleted"), property);
1123
}
1124
1125
if (revert_rect.has_point(mpos)) {
1126
accept_event();
1127
get_viewport()->gui_release_focus();
1128
bool is_valid_revert = false;
1129
Variant revert_value = EditorPropertyRevert::get_property_revert_value(object, property, &is_valid_revert);
1130
ERR_FAIL_COND(!is_valid_revert);
1131
emit_changed(_get_revert_property(), revert_value);
1132
update_property();
1133
}
1134
1135
if (check_rect.has_point(mpos)) {
1136
accept_event();
1137
if (!checked && Object::cast_to<Control>(object) && property_path.begins_with("theme_override_")) {
1138
List<PropertyInfo> pinfo;
1139
object->get_property_list(&pinfo);
1140
for (const PropertyInfo &E : pinfo) {
1141
if (E.type == Variant::OBJECT && E.name == property_path) {
1142
EditorToaster::get_singleton()->popup_str(TTR("Toggling the checkbox is disabled for Resource properties. Modify the property using the resource picker instead."), EditorToaster::SEVERITY_WARNING);
1143
return; // Disallow clicking to toggle the checkbox of type Resource to checked.
1144
}
1145
}
1146
}
1147
checked = !checked;
1148
queue_redraw();
1149
emit_signal(SNAME("property_checked"), property, checked);
1150
}
1151
} else if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
1152
accept_event();
1153
_update_popup();
1154
menu->set_position(get_screen_position() + get_local_mouse_position());
1155
menu->reset_size();
1156
menu->popup();
1157
select();
1158
return;
1159
}
1160
}
1161
1162
void EditorProperty::_accessibility_action_click(const Variant &p_data) {
1163
select();
1164
if (checkable) {
1165
if (!checked && Object::cast_to<Control>(object) && property_path.begins_with("theme_override_")) {
1166
List<PropertyInfo> pinfo;
1167
object->get_property_list(&pinfo);
1168
for (const PropertyInfo &E : pinfo) {
1169
if (E.type == Variant::OBJECT && E.name == property_path) {
1170
EditorToaster::get_singleton()->popup_str(TTR("Toggling the checkbox is disabled for Resource properties. Modify the property using the resource picker instead."), EditorToaster::SEVERITY_WARNING);
1171
return;
1172
}
1173
}
1174
}
1175
1176
checked = !checked;
1177
queue_redraw();
1178
emit_signal(SNAME("property_checked"), property, checked);
1179
}
1180
}
1181
1182
void EditorProperty::_accessibility_action_menu(const Variant &p_data) {
1183
_update_popup();
1184
menu->set_position(get_screen_position());
1185
menu->reset_size();
1186
menu->popup();
1187
}
1188
1189
void EditorProperty::shortcut_input(const Ref<InputEvent> &p_event) {
1190
if (!selected || !p_event->is_pressed()) {
1191
return;
1192
}
1193
1194
const Ref<InputEventKey> k = p_event;
1195
1196
if (k.is_valid() && k->is_pressed()) {
1197
if (ED_IS_SHORTCUT("property_editor/copy_value", p_event)) {
1198
menu_option(MENU_COPY_VALUE);
1199
accept_event();
1200
} else if (!is_read_only() && ED_IS_SHORTCUT("property_editor/paste_value", p_event)) {
1201
menu_option(MENU_PASTE_VALUE);
1202
accept_event();
1203
} else if (!internal && ED_IS_SHORTCUT("property_editor/copy_property_path", p_event)) {
1204
menu_option(MENU_COPY_PROPERTY_PATH);
1205
accept_event();
1206
}
1207
}
1208
}
1209
1210
const Color *EditorProperty::_get_property_colors() {
1211
static Color c[4];
1212
c[0] = theme_cache.property_color_x;
1213
c[1] = theme_cache.property_color_y;
1214
c[2] = theme_cache.property_color_z;
1215
c[3] = theme_cache.property_color_w;
1216
return c;
1217
}
1218
1219
void EditorProperty::set_label_reference(Control *p_control) {
1220
label_reference = p_control;
1221
}
1222
1223
HBoxContainer *EditorProperty::get_inline_container(InlineControlSide p_side) {
1224
if (p_side == INLINE_CONTROL_LEFT) {
1225
return left_container;
1226
} else {
1227
return right_container;
1228
}
1229
}
1230
1231
void EditorProperty::set_bottom_editor(Control *p_control) {
1232
bottom_editor = p_control;
1233
if (has_borders) {
1234
_update_property_bg();
1235
}
1236
}
1237
1238
Variant EditorProperty::_get_cache_value(const StringName &p_prop, bool &r_valid) const {
1239
return object->get(p_prop, &r_valid);
1240
}
1241
1242
bool EditorProperty::is_cache_valid() const {
1243
if (object) {
1244
for (const KeyValue<StringName, Variant> &E : cache) {
1245
bool valid;
1246
Variant value = _get_cache_value(E.key, valid);
1247
if (!valid || value != E.value) {
1248
return false;
1249
}
1250
}
1251
}
1252
return true;
1253
}
1254
void EditorProperty::update_cache() {
1255
cache.clear();
1256
if (object && property != StringName()) {
1257
bool valid;
1258
Variant value = _get_cache_value(property, valid);
1259
if (valid) {
1260
cache[property] = value;
1261
}
1262
}
1263
}
1264
Variant EditorProperty::get_drag_data(const Point2 &p_point) {
1265
if (property == StringName()) {
1266
return Variant();
1267
}
1268
1269
Dictionary dp;
1270
dp["type"] = "obj_property";
1271
dp["object"] = object;
1272
dp["property"] = property;
1273
dp["value"] = object->get(property);
1274
1275
Label *drag_label = memnew(Label);
1276
drag_label->set_focus_mode(FOCUS_ACCESSIBILITY);
1277
drag_label->set_text(property);
1278
drag_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate raw property name.
1279
set_drag_preview(drag_label);
1280
return dp;
1281
}
1282
1283
void EditorProperty::set_use_folding(bool p_use_folding) {
1284
use_folding = p_use_folding;
1285
}
1286
1287
bool EditorProperty::is_using_folding() const {
1288
return use_folding;
1289
}
1290
1291
void EditorProperty::expand_all_folding() {
1292
}
1293
1294
void EditorProperty::collapse_all_folding() {
1295
}
1296
1297
void EditorProperty::expand_revertable() {
1298
}
1299
1300
void EditorProperty::set_selectable(bool p_selectable) {
1301
selectable = p_selectable;
1302
}
1303
1304
bool EditorProperty::is_selectable() const {
1305
return selectable;
1306
}
1307
1308
void EditorProperty::set_name_split_ratio(float p_ratio) {
1309
split_ratio = p_ratio;
1310
}
1311
1312
float EditorProperty::get_name_split_ratio() const {
1313
return split_ratio;
1314
}
1315
1316
void EditorProperty::set_favoritable(bool p_favoritable) {
1317
can_favorite = p_favoritable;
1318
}
1319
1320
bool EditorProperty::is_favoritable() const {
1321
return can_favorite;
1322
}
1323
1324
void EditorProperty::set_object_and_property(Object *p_object, const StringName &p_property) {
1325
object = p_object;
1326
property = p_property;
1327
1328
_update_flags();
1329
}
1330
1331
static bool _is_value_potential_override(Node *p_node, const String &p_property) {
1332
// Consider a value is potentially overriding another if either of the following is true:
1333
// a) The node is foreign (inheriting or an instance), so the original value may come from another scene.
1334
// b) The node belongs to the scene, but the original value comes from somewhere but the builtin class (i.e., a script).
1335
Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
1336
Vector<SceneState::PackState> states_stack = PropertyUtils::get_node_states_stack(p_node, edited_scene);
1337
if (states_stack.size()) {
1338
return true;
1339
} else {
1340
bool is_valid_default = false;
1341
bool is_class_default = false;
1342
PropertyUtils::get_property_default_value(p_node, p_property, &is_valid_default, &states_stack, false, nullptr, &is_class_default);
1343
return !is_class_default;
1344
}
1345
}
1346
1347
void EditorProperty::_update_flags() {
1348
can_pin = false;
1349
pin_hidden = true;
1350
1351
if (read_only) {
1352
return;
1353
}
1354
1355
if (Node *node = Object::cast_to<Node>(object)) {
1356
// Avoid errors down the road by ignoring nodes which are not part of a scene
1357
if (!node->get_owner()) {
1358
bool is_scene_root = false;
1359
for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); ++i) {
1360
if (EditorNode::get_editor_data().get_edited_scene_root(i) == node) {
1361
is_scene_root = true;
1362
break;
1363
}
1364
}
1365
if (!is_scene_root) {
1366
return;
1367
}
1368
}
1369
if (!_is_value_potential_override(node, property)) {
1370
return;
1371
}
1372
pin_hidden = false;
1373
{
1374
HashSet<StringName> storable_properties;
1375
node->get_storable_properties(storable_properties);
1376
if (storable_properties.has(node->get_property_store_alias(property))) {
1377
can_pin = true;
1378
}
1379
}
1380
}
1381
}
1382
1383
Control *EditorProperty::make_custom_tooltip(const String &p_text) const {
1384
String symbol;
1385
String prologue;
1386
1387
if (object->has_method("_get_property_warning")) {
1388
const String custom_warning = object->call("_get_property_warning", property);
1389
if (!custom_warning.is_empty()) {
1390
prologue = "[b][color=" + theme_cache.warning_color.to_html(false) + "]" + custom_warning + "[/color][/b]";
1391
}
1392
}
1393
1394
if (has_doc_tooltip) {
1395
symbol = p_text;
1396
1397
const EditorInspector *inspector = get_parent_inspector();
1398
if (inspector) {
1399
const String custom_description = inspector->get_custom_property_description(p_text);
1400
if (!custom_description.is_empty()) {
1401
if (!prologue.is_empty()) {
1402
prologue += '\n';
1403
}
1404
prologue += custom_description;
1405
}
1406
}
1407
}
1408
1409
if (!symbol.is_empty() || !prologue.is_empty()) {
1410
return EditorHelpBitTooltip::make_tooltip(const_cast<EditorProperty *>(this), symbol, prologue);
1411
}
1412
1413
return nullptr;
1414
}
1415
1416
void EditorProperty::menu_option(int p_option) {
1417
switch (p_option) {
1418
case MENU_COPY_VALUE: {
1419
EditorInspector::set_property_clipboard(object->get(property));
1420
} break;
1421
case MENU_PASTE_VALUE: {
1422
EditorPropertyResource *epr = Object::cast_to<EditorPropertyResource>(this);
1423
if (epr) {
1424
const Ref<Resource> res = InspectorDock::get_inspector_singleton()->get_property_clipboard();
1425
if (res.is_valid() && !epr->get_resource_picker()->is_resource_allowed(res)) {
1426
return;
1427
}
1428
}
1429
emit_changed(property, EditorInspector::get_property_clipboard());
1430
} break;
1431
case MENU_COPY_PROPERTY_PATH: {
1432
DisplayServer::get_singleton()->clipboard_set(property_path);
1433
} break;
1434
case MENU_OVERRIDE_FOR_PROJECT: {
1435
emit_signal(SNAME("property_overridden"));
1436
} break;
1437
case MENU_FAVORITE_PROPERTY: {
1438
emit_signal(SNAME("property_favorited"), property, !favorited);
1439
queue_redraw();
1440
} break;
1441
case MENU_PIN_VALUE: {
1442
emit_signal(SNAME("property_pinned"), property, !pinned);
1443
queue_redraw();
1444
} break;
1445
case MENU_DELETE: {
1446
accept_event();
1447
emit_signal(SNAME("property_deleted"), property);
1448
} break;
1449
case MENU_REVERT_VALUE: {
1450
accept_event();
1451
get_viewport()->gui_release_focus();
1452
bool is_valid_revert = false;
1453
Variant revert_value = EditorPropertyRevert::get_property_revert_value(object, property, &is_valid_revert);
1454
ERR_FAIL_COND(!is_valid_revert);
1455
emit_changed(_get_revert_property(), revert_value);
1456
update_property();
1457
} break;
1458
case MENU_OPEN_DOCUMENTATION: {
1459
ScriptEditor::get_singleton()->goto_help(doc_path);
1460
EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
1461
} break;
1462
}
1463
}
1464
1465
void EditorProperty::_bind_methods() {
1466
ClassDB::bind_method(D_METHOD("set_label", "text"), &EditorProperty::set_label);
1467
ClassDB::bind_method(D_METHOD("get_label"), &EditorProperty::get_label);
1468
1469
ClassDB::bind_method(D_METHOD("set_read_only", "read_only"), &EditorProperty::set_read_only);
1470
ClassDB::bind_method(D_METHOD("is_read_only"), &EditorProperty::is_read_only);
1471
1472
ClassDB::bind_method(D_METHOD("set_draw_label", "draw_label"), &EditorProperty::set_draw_label);
1473
ClassDB::bind_method(D_METHOD("is_draw_label"), &EditorProperty::is_draw_label);
1474
1475
ClassDB::bind_method(D_METHOD("set_draw_background", "draw_background"), &EditorProperty::set_draw_background);
1476
ClassDB::bind_method(D_METHOD("is_draw_background"), &EditorProperty::is_draw_background);
1477
1478
ClassDB::bind_method(D_METHOD("set_checkable", "checkable"), &EditorProperty::set_checkable);
1479
ClassDB::bind_method(D_METHOD("is_checkable"), &EditorProperty::is_checkable);
1480
1481
ClassDB::bind_method(D_METHOD("set_checked", "checked"), &EditorProperty::set_checked);
1482
ClassDB::bind_method(D_METHOD("is_checked"), &EditorProperty::is_checked);
1483
1484
ClassDB::bind_method(D_METHOD("set_draw_warning", "draw_warning"), &EditorProperty::set_draw_warning);
1485
ClassDB::bind_method(D_METHOD("is_draw_warning"), &EditorProperty::is_draw_warning);
1486
1487
ClassDB::bind_method(D_METHOD("set_keying", "keying"), &EditorProperty::set_keying);
1488
ClassDB::bind_method(D_METHOD("is_keying"), &EditorProperty::is_keying);
1489
1490
ClassDB::bind_method(D_METHOD("set_deletable", "deletable"), &EditorProperty::set_deletable);
1491
ClassDB::bind_method(D_METHOD("is_deletable"), &EditorProperty::is_deletable);
1492
1493
ClassDB::bind_method(D_METHOD("get_edited_property"), &EditorProperty::get_edited_property);
1494
ClassDB::bind_method(D_METHOD("get_edited_object"), &EditorProperty::get_edited_object);
1495
1496
ClassDB::bind_method(D_METHOD("update_property"), &EditorProperty::update_property);
1497
1498
ClassDB::bind_method(D_METHOD("add_focusable", "control"), &EditorProperty::add_focusable);
1499
ClassDB::bind_method(D_METHOD("set_bottom_editor", "editor"), &EditorProperty::set_bottom_editor);
1500
1501
ClassDB::bind_method(D_METHOD("set_selectable", "selectable"), &EditorProperty::set_selectable);
1502
ClassDB::bind_method(D_METHOD("is_selectable"), &EditorProperty::is_selectable);
1503
1504
ClassDB::bind_method(D_METHOD("set_use_folding", "use_folding"), &EditorProperty::set_use_folding);
1505
ClassDB::bind_method(D_METHOD("is_using_folding"), &EditorProperty::is_using_folding);
1506
1507
ClassDB::bind_method(D_METHOD("set_name_split_ratio", "ratio"), &EditorProperty::set_name_split_ratio);
1508
ClassDB::bind_method(D_METHOD("get_name_split_ratio"), &EditorProperty::get_name_split_ratio);
1509
1510
ClassDB::bind_method(D_METHOD("deselect"), &EditorProperty::deselect);
1511
ClassDB::bind_method(D_METHOD("is_selected"), &EditorProperty::is_selected);
1512
ClassDB::bind_method(D_METHOD("select", "focusable"), &EditorProperty::select, DEFVAL(-1));
1513
ClassDB::bind_method(D_METHOD("set_object_and_property", "object", "property"), &EditorProperty::set_object_and_property);
1514
ClassDB::bind_method(D_METHOD("set_label_reference", "control"), &EditorProperty::set_label_reference);
1515
1516
ClassDB::bind_method(D_METHOD("emit_changed", "property", "value", "field", "changing"), &EditorProperty::emit_changed, DEFVAL(StringName()), DEFVAL(false));
1517
1518
ADD_PROPERTY(PropertyInfo(Variant::STRING, "label"), "set_label", "get_label");
1519
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "is_read_only");
1520
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_label"), "set_draw_label", "is_draw_label");
1521
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_background"), "set_draw_background", "is_draw_background");
1522
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "checkable"), "set_checkable", "is_checkable");
1523
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "checked"), "set_checked", "is_checked");
1524
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_warning"), "set_draw_warning", "is_draw_warning");
1525
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keying"), "set_keying", "is_keying");
1526
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deletable"), "set_deletable", "is_deletable");
1527
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selectable"), "set_selectable", "is_selectable");
1528
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_folding"), "set_use_folding", "is_using_folding");
1529
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "name_split_ratio"), "set_name_split_ratio", "get_name_split_ratio");
1530
1531
ADD_SIGNAL(MethodInfo("property_changed", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), PropertyInfo(Variant::STRING_NAME, "field"), PropertyInfo(Variant::BOOL, "changing")));
1532
ADD_SIGNAL(MethodInfo("multiple_properties_changed", PropertyInfo(Variant::PACKED_STRING_ARRAY, "properties"), PropertyInfo(Variant::ARRAY, "value")));
1533
ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING_NAME, "property")));
1534
ADD_SIGNAL(MethodInfo("property_deleted", PropertyInfo(Variant::STRING_NAME, "property")));
1535
ADD_SIGNAL(MethodInfo("property_keyed_with_value", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
1536
ADD_SIGNAL(MethodInfo("property_checked", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "checked")));
1537
ADD_SIGNAL(MethodInfo("property_overridden"));
1538
ADD_SIGNAL(MethodInfo("property_favorited", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "favorited")));
1539
ADD_SIGNAL(MethodInfo("property_pinned", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "pinned")));
1540
ADD_SIGNAL(MethodInfo("property_can_revert_changed", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "can_revert")));
1541
ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, Resource::get_class_static())));
1542
ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::INT, "id")));
1543
ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "focusable_idx")));
1544
1545
GDVIRTUAL_BIND(_update_property)
1546
GDVIRTUAL_BIND(_set_read_only, "read_only")
1547
1548
ClassDB::bind_method(D_METHOD("_update_editor_property_status"), &EditorProperty::update_editor_property_status);
1549
}
1550
1551
EditorProperty::EditorProperty() {
1552
set_focus_mode(FOCUS_ACCESSIBILITY);
1553
1554
object = nullptr;
1555
split_ratio = 0.5;
1556
text_size = 0;
1557
property_usage = 0;
1558
selected_focusable = -1;
1559
label_reference = nullptr;
1560
bottom_editor = nullptr;
1561
menu = nullptr;
1562
1563
left_container = memnew(HBoxContainer);
1564
left_container->add_theme_constant_override(SNAME("separation"), 0);
1565
add_child(left_container);
1566
1567
right_container = memnew(HBoxContainer);
1568
right_container->add_theme_constant_override(SNAME("separation"), 0);
1569
add_child(right_container);
1570
1571
set_process_shortcut_input(true);
1572
}
1573
1574
void EditorProperty::_update_popup() {
1575
if (menu) {
1576
menu->clear();
1577
} else {
1578
menu = memnew(PopupMenu);
1579
add_child(menu);
1580
menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorProperty::menu_option));
1581
}
1582
menu->add_icon_shortcut(theme_cache.copy_icon, ED_GET_SHORTCUT("property_editor/copy_value"), MENU_COPY_VALUE);
1583
menu->add_icon_shortcut(theme_cache.paste_icon, ED_GET_SHORTCUT("property_editor/paste_value"), MENU_PASTE_VALUE);
1584
menu->add_icon_shortcut(theme_cache.copy_node_path_icon, ED_GET_SHORTCUT("property_editor/copy_property_path"), MENU_COPY_PROPERTY_PATH);
1585
menu->set_item_disabled(MENU_PASTE_VALUE, is_read_only());
1586
menu->set_item_disabled(MENU_COPY_PROPERTY_PATH, internal);
1587
1588
if (can_favorite || !pin_hidden) {
1589
menu->add_separator();
1590
}
1591
1592
if (can_favorite) {
1593
if (favorited) {
1594
menu->add_icon_item(theme_cache.unfavorite_icon, TTR("Unfavorite Property"), MENU_FAVORITE_PROPERTY);
1595
menu->set_item_tooltip(menu->get_item_index(MENU_FAVORITE_PROPERTY), TTR("Make this property be put back at its original place."));
1596
} else {
1597
// TRANSLATORS: This is a menu item to add a property to the favorites.
1598
menu->add_icon_item(theme_cache.favorite_icon, TTR("Favorite Property"), MENU_FAVORITE_PROPERTY);
1599
menu->set_item_tooltip(menu->get_item_index(MENU_FAVORITE_PROPERTY), TTR("Make this property be placed at the top for all objects of this class."));
1600
}
1601
}
1602
1603
if (!pin_hidden) {
1604
if (can_pin) {
1605
menu->add_icon_check_item(theme_cache.pin_icon, TTR("Pin Value"), MENU_PIN_VALUE);
1606
menu->set_item_checked(menu->get_item_index(MENU_PIN_VALUE), pinned);
1607
} else {
1608
menu->add_icon_check_item(theme_cache.pin_icon, vformat(TTR("Pin Value [Disabled because '%s' is editor-only]"), property), MENU_PIN_VALUE);
1609
menu->set_item_disabled(menu->get_item_index(MENU_PIN_VALUE), true);
1610
}
1611
menu->set_item_tooltip(menu->get_item_index(MENU_PIN_VALUE), TTR("Pinning a value forces it to be saved even if it's equal to the default."));
1612
}
1613
if (deletable || can_revert || can_override) {
1614
menu->add_separator();
1615
if (can_override) {
1616
menu->add_icon_item(theme_cache.override_icon, TTRC("Override for Project"), MENU_OVERRIDE_FOR_PROJECT);
1617
}
1618
if (deletable) {
1619
menu->add_icon_item(theme_cache.remove_icon, TTR("Delete Property"), MENU_DELETE);
1620
}
1621
if (can_revert) {
1622
menu->add_icon_item(theme_cache.revert_icon, TTR("Revert Value"), MENU_REVERT_VALUE);
1623
}
1624
}
1625
if (!doc_path.is_empty() && ScriptEditor::get_singleton() && EditorNode::get_singleton()) {
1626
menu->add_separator();
1627
menu->add_icon_item(theme_cache.help_icon, TTR("Open Documentation"), MENU_OPEN_DOCUMENTATION);
1628
}
1629
}
1630
1631
////////////////////////////////////////////////
1632
////////////////////////////////////////////////
1633
1634
void EditorInspectorPlugin::add_custom_control(Control *control) {
1635
AddedEditor ae;
1636
ae.property_editor = control;
1637
added_editors.push_back(ae);
1638
}
1639
1640
void EditorInspectorPlugin::add_property_editor(const String &p_for_property, Control *p_prop, bool p_add_to_end, const String &p_label) {
1641
AddedEditor ae;
1642
ae.properties.push_back(p_for_property);
1643
ae.property_editor = p_prop;
1644
ae.add_to_end = p_add_to_end;
1645
ae.label = p_label;
1646
added_editors.push_back(ae);
1647
}
1648
1649
void EditorInspectorPlugin::add_property_editor_for_multiple_properties(const String &p_label, const Vector<String> &p_properties, Control *p_prop) {
1650
AddedEditor ae;
1651
ae.properties = p_properties;
1652
ae.property_editor = p_prop;
1653
ae.label = p_label;
1654
added_editors.push_back(ae);
1655
}
1656
1657
bool EditorInspectorPlugin::can_handle(Object *p_object) {
1658
bool success = false;
1659
GDVIRTUAL_CALL(_can_handle, p_object, success);
1660
return success;
1661
}
1662
1663
void EditorInspectorPlugin::parse_begin(Object *p_object) {
1664
GDVIRTUAL_CALL(_parse_begin, p_object);
1665
}
1666
1667
void EditorInspectorPlugin::parse_category(Object *p_object, const String &p_category) {
1668
GDVIRTUAL_CALL(_parse_category, p_object, p_category);
1669
}
1670
1671
void EditorInspectorPlugin::parse_group(Object *p_object, const String &p_group) {
1672
GDVIRTUAL_CALL(_parse_group, p_object, p_group);
1673
}
1674
1675
bool EditorInspectorPlugin::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
1676
bool ret = false;
1677
GDVIRTUAL_CALL(_parse_property, p_object, p_type, p_path, p_hint, p_hint_text, p_usage, p_wide, ret);
1678
return ret;
1679
}
1680
1681
void EditorInspectorPlugin::parse_end(Object *p_object) {
1682
GDVIRTUAL_CALL(_parse_end, p_object);
1683
}
1684
1685
void EditorInspectorPlugin::_bind_methods() {
1686
ClassDB::bind_method(D_METHOD("add_custom_control", "control"), &EditorInspectorPlugin::add_custom_control);
1687
ClassDB::bind_method(D_METHOD("add_property_editor", "property", "editor", "add_to_end", "label"), &EditorInspectorPlugin::add_property_editor, DEFVAL(false), DEFVAL(String()));
1688
ClassDB::bind_method(D_METHOD("add_property_editor_for_multiple_properties", "label", "properties", "editor"), &EditorInspectorPlugin::add_property_editor_for_multiple_properties);
1689
1690
GDVIRTUAL_BIND(_can_handle, "object")
1691
GDVIRTUAL_BIND(_parse_begin, "object")
1692
GDVIRTUAL_BIND(_parse_category, "object", "category")
1693
GDVIRTUAL_BIND(_parse_group, "object", "group")
1694
GDVIRTUAL_BIND(_parse_property, "object", "type", "name", "hint_type", "hint_string", "usage_flags", "wide");
1695
GDVIRTUAL_BIND(_parse_end, "object")
1696
}
1697
1698
////////////////////////////////////////////////
1699
////////////////////////////////////////////////
1700
1701
static Ref<Script> _get_category_script(const PropertyInfo &p_info) {
1702
if (!p_info.hint_string.is_empty() && !EditorNode::get_editor_data().is_type_recognized(p_info.name) && ResourceLoader::exists(p_info.hint_string, "Script")) {
1703
return ResourceLoader::load(p_info.hint_string, "Script");
1704
}
1705
return Ref<Script>();
1706
}
1707
1708
void EditorInspectorCategory::_bind_methods() {
1709
ADD_SIGNAL(MethodInfo("unfavorite_all"));
1710
}
1711
1712
void EditorInspectorCategory::_notification(int p_what) {
1713
switch (p_what) {
1714
case NOTIFICATION_POSTINITIALIZE: {
1715
connect(SceneStringName(theme_changed), callable_mp(this, &EditorInspectorCategory::_theme_changed));
1716
} break;
1717
1718
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
1719
RID ae = get_accessibility_element();
1720
ERR_FAIL_COND(ae.is_null());
1721
1722
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
1723
1724
DisplayServer::get_singleton()->accessibility_update_set_name(ae, vformat(TTR("Category: %s"), label));
1725
DisplayServer::get_singleton()->accessibility_update_set_value(ae, vformat(TTR("Category: %s"), label));
1726
1727
DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_MENU);
1728
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &EditorInspectorCategory::_accessibility_action_menu));
1729
} break;
1730
1731
case NOTIFICATION_TRANSLATION_CHANGED: {
1732
if (is_favorite) {
1733
label = TTR("Favorites");
1734
}
1735
queue_accessibility_update();
1736
} break;
1737
1738
case NOTIFICATION_DRAW: {
1739
const Ref<StyleBox> &sb = theme_cache.background;
1740
1741
draw_style_box(sb, Rect2(Vector2(), get_size()));
1742
1743
const Ref<Font> &font = theme_cache.bold_font;
1744
int font_size = theme_cache.bold_font_size;
1745
1746
int hs = theme_cache.horizontal_separation;
1747
int icon_size = theme_cache.class_icon_size;
1748
1749
int w = font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width;
1750
if (icon.is_valid()) {
1751
w += hs + icon_size;
1752
}
1753
w = MIN(w, get_size().width - sb->get_minimum_size().width);
1754
1755
int ofs = (get_size().width - w) / 2;
1756
1757
float v_margin_offset = sb->get_content_margin(SIDE_TOP) - sb->get_content_margin(SIDE_BOTTOM);
1758
1759
if (icon.is_valid()) {
1760
Size2 rect_size = Size2(icon_size, icon_size);
1761
Point2 rect_pos = Point2(ofs, (get_size().height - icon_size) / 2 + v_margin_offset).round();
1762
if (is_layout_rtl()) {
1763
rect_pos.x = get_size().width - rect_pos.x - icon_size;
1764
}
1765
draw_texture_rect(icon, Rect2(rect_pos, rect_size));
1766
1767
ofs += hs + icon_size;
1768
w -= hs + icon_size;
1769
}
1770
1771
if (is_layout_rtl()) {
1772
ofs = get_size().width - ofs - w;
1773
}
1774
1775
// Use TextLine so we have access to accurate font metrics. This way,
1776
// we can ensure the line is vertically centered regardless of the font used
1777
// or its size.
1778
Ref<TextLine> tl;
1779
tl.instantiate();
1780
tl->add_string(label, font, font_size);
1781
tl->set_width(w);
1782
tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT);
1783
float text_pos_y = (get_size().height - tl->get_size().height) / 2 + v_margin_offset;
1784
Point2 text_pos = Point2(ofs, text_pos_y).round();
1785
tl->draw(get_canvas_item(), text_pos, theme_cache.font_color);
1786
} break;
1787
}
1788
}
1789
1790
void EditorInspectorCategory::_accessibility_action_menu(const Variant &p_data) {
1791
_popup_context_menu(get_screen_position());
1792
}
1793
1794
Control *EditorInspectorCategory::make_custom_tooltip(const String &p_text) const {
1795
// If it's not a doc tooltip, fallback to the default one.
1796
if (doc_class_name.is_empty()) {
1797
return nullptr;
1798
}
1799
1800
return EditorHelpBitTooltip::make_tooltip(const_cast<EditorInspectorCategory *>(this), p_text);
1801
}
1802
1803
void EditorInspectorCategory::set_as_favorite() {
1804
is_favorite = true;
1805
_update_icon();
1806
}
1807
1808
void EditorInspectorCategory::set_property_info(const PropertyInfo &p_info) {
1809
info = p_info;
1810
1811
Ref<Script> scr = _get_category_script(info);
1812
if (scr.is_valid()) {
1813
StringName script_name = EditorNode::get_editor_data().script_class_get_name(scr->get_path());
1814
if (script_name != StringName()) {
1815
label = script_name;
1816
}
1817
}
1818
if (label.is_empty()) {
1819
label = info.name;
1820
}
1821
_update_icon();
1822
}
1823
1824
void EditorInspectorCategory::set_doc_class_name(const String &p_name) {
1825
doc_class_name = p_name;
1826
}
1827
1828
Size2 EditorInspectorCategory::get_minimum_size() const {
1829
Size2 ms;
1830
if (theme_cache.bold_font.is_valid()) {
1831
ms.height = theme_cache.bold_font->get_height(theme_cache.bold_font_size);
1832
}
1833
if (icon.is_valid()) {
1834
ms.height = MAX(theme_cache.class_icon_size, ms.height);
1835
}
1836
ms.height += theme_cache.vertical_separation;
1837
1838
if (theme_cache.background.is_valid()) {
1839
ms.height += theme_cache.background->get_content_margin(SIDE_TOP) + theme_cache.background->get_content_margin(SIDE_BOTTOM);
1840
}
1841
1842
return ms;
1843
}
1844
1845
void EditorInspectorCategory::_handle_menu_option(int p_option) {
1846
switch (p_option) {
1847
case MENU_OPEN_DOCS: {
1848
ScriptEditor::get_singleton()->goto_help("class:" + doc_class_name);
1849
EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
1850
} break;
1851
1852
case MENU_UNFAVORITE_ALL: {
1853
emit_signal(SNAME("unfavorite_all"));
1854
} break;
1855
}
1856
}
1857
1858
void EditorInspectorCategory::_popup_context_menu(const Point2i &p_position) {
1859
if (!is_favorite && doc_class_name.is_empty()) {
1860
return;
1861
}
1862
1863
if (menu == nullptr) {
1864
menu = memnew(PopupMenu);
1865
1866
if (is_favorite) {
1867
menu->add_item(TTRC("Unfavorite All"), MENU_UNFAVORITE_ALL);
1868
} else {
1869
menu->add_item(TTRC("Open Documentation"), MENU_OPEN_DOCS);
1870
menu->set_item_disabled(-1, !EditorHelp::get_doc_data()->class_list.has(doc_class_name));
1871
}
1872
1873
menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorInspectorCategory::_handle_menu_option));
1874
add_child(menu);
1875
}
1876
1877
if (menu_icon_dirty) {
1878
if (is_favorite) {
1879
menu->set_item_icon(menu->get_item_index(MENU_UNFAVORITE_ALL), theme_cache.icon_unfavorite);
1880
} else {
1881
menu->set_item_icon(menu->get_item_index(MENU_OPEN_DOCS), theme_cache.icon_help);
1882
}
1883
menu_icon_dirty = false;
1884
}
1885
1886
menu->set_position(p_position);
1887
menu->reset_size();
1888
menu->popup();
1889
}
1890
1891
void EditorInspectorCategory::_update_icon() {
1892
if (is_favorite) {
1893
icon = theme_cache.icon_favorites;
1894
return;
1895
}
1896
1897
icon = Ref<Texture2D>();
1898
1899
Ref<Script> scr = _get_category_script(info);
1900
if (scr.is_valid()) {
1901
StringName script_name = EditorNode::get_editor_data().script_class_get_name(scr->get_path());
1902
if (script_name == StringName()) {
1903
icon = EditorNode::get_singleton()->get_object_icon(scr.ptr());
1904
} else {
1905
icon = EditorNode::get_singleton()->get_class_icon(script_name);
1906
}
1907
}
1908
if (icon.is_null() && !info.name.is_empty()) {
1909
icon = EditorNode::get_singleton()->get_class_icon(info.name);
1910
}
1911
}
1912
1913
void EditorInspectorCategory::_theme_changed() {
1914
// This needs to be done via the signal, as it's fired before the minimum since is updated.
1915
EditorInspector::initialize_category_theme(theme_cache, this);
1916
menu_icon_dirty = true;
1917
_update_icon();
1918
}
1919
1920
void EditorInspectorCategory::gui_input(const Ref<InputEvent> &p_event) {
1921
const Ref<InputEventMouseButton> &mb = p_event;
1922
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
1923
_popup_context_menu(get_screen_position() + mb->get_position());
1924
}
1925
}
1926
1927
EditorInspectorCategory::EditorInspectorCategory() {
1928
set_focus_mode(FOCUS_ACCESSIBILITY);
1929
}
1930
1931
////////////////////////////////////////////////
1932
////////////////////////////////////////////////
1933
1934
void EditorInspectorSection::_test_unfold() {
1935
if (!vbox_added) {
1936
add_child(vbox);
1937
move_child(vbox, 0);
1938
vbox_added = true;
1939
}
1940
}
1941
1942
Ref<Texture2D> EditorInspectorSection::_get_arrow() {
1943
Ref<Texture2D> arrow;
1944
if (foldable) {
1945
if (object->editor_is_section_unfolded(section)) {
1946
arrow = theme_cache.arrow;
1947
} else {
1948
if (is_layout_rtl()) {
1949
arrow = theme_cache.arrow_collapsed_mirrored;
1950
} else {
1951
arrow = theme_cache.arrow_collapsed;
1952
}
1953
}
1954
}
1955
return arrow;
1956
}
1957
1958
Ref<Texture2D> EditorInspectorSection::_get_checkbox() {
1959
Ref<Texture2D> checkbox;
1960
1961
if (checkable) {
1962
if (checked) {
1963
checkbox = theme_cache.icon_gui_checked;
1964
} else {
1965
checkbox = theme_cache.icon_gui_unchecked;
1966
}
1967
}
1968
1969
return checkbox;
1970
}
1971
1972
int EditorInspectorSection::_get_header_height() {
1973
int header_height = theme_cache.bold_font->get_height(theme_cache.bold_font_size);
1974
Ref<Texture2D> arrow = _get_arrow();
1975
if (arrow.is_valid()) {
1976
header_height = MAX(header_height, arrow->get_height());
1977
}
1978
header_height += theme_cache.vertical_separation;
1979
1980
return header_height;
1981
}
1982
1983
void EditorInspectorSection::_notification(int p_what) {
1984
switch (p_what) {
1985
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
1986
RID ae = get_accessibility_element();
1987
ERR_FAIL_COND(ae.is_null());
1988
1989
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
1990
1991
DisplayServer::get_singleton()->accessibility_update_set_name(ae, vformat(TTR("Section: %s"), label));
1992
DisplayServer::get_singleton()->accessibility_update_set_value(ae, vformat(TTR("Section: %s"), label));
1993
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_COLLAPSE, callable_mp(this, &EditorInspectorSection::_accessibility_action_collapse));
1994
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_EXPAND, callable_mp(this, &EditorInspectorSection::_accessibility_action_expand));
1995
} break;
1996
1997
case NOTIFICATION_THEME_CHANGED: {
1998
EditorInspector::initialize_section_theme(theme_cache, this);
1999
2000
bg_color = theme_cache.prop_subsection;
2001
bg_color.a /= level;
2002
} break;
2003
2004
case NOTIFICATION_SORT_CHILDREN: {
2005
if (!vbox_added) {
2006
return;
2007
}
2008
2009
int inspector_margin = theme_cache.inspector_margin;
2010
if (indent_depth > 0 && theme_cache.indent_size > 0) {
2011
inspector_margin += indent_depth * theme_cache.indent_size;
2012
}
2013
if (indent_depth > 0 && theme_cache.indent_box.is_valid()) {
2014
inspector_margin += theme_cache.indent_box->get_margin(SIDE_LEFT) + theme_cache.indent_box->get_margin(SIDE_RIGHT);
2015
}
2016
2017
Size2 size = get_size() - Vector2(inspector_margin, 0);
2018
int header_height = _get_header_height();
2019
Vector2 offset = Vector2(is_layout_rtl() ? 0 : inspector_margin, header_height);
2020
for (int i = 0; i < get_child_count(); i++) {
2021
Control *c = as_sortable_control(get_child(i));
2022
if (!c) {
2023
continue;
2024
}
2025
fit_child_in_rect(c, Rect2(offset, size));
2026
}
2027
} break;
2028
2029
case NOTIFICATION_DRAW: {
2030
int section_indent = 0;
2031
int section_indent_size = theme_cache.indent_size;
2032
if (indent_depth > 0 && section_indent_size > 0) {
2033
section_indent = indent_depth * section_indent_size;
2034
}
2035
Ref<StyleBoxFlat> section_indent_style = theme_cache.indent_box;
2036
if (indent_depth > 0 && section_indent_style.is_valid()) {
2037
section_indent += section_indent_style->get_margin(SIDE_LEFT) + section_indent_style->get_margin(SIDE_RIGHT);
2038
}
2039
2040
int header_width = get_size().width - section_indent;
2041
int header_offset_x = 0.0;
2042
bool rtl = is_layout_rtl();
2043
if (!rtl) {
2044
header_offset_x += section_indent;
2045
}
2046
2047
bool can_click_unfold = vbox->get_child_count(false) != 0 && !(checkable && !checked && !checkbox_only);
2048
2049
// Draw header area.
2050
int header_height = _get_header_height();
2051
Rect2 header_rect = Rect2(Vector2(header_offset_x, 0.0), Vector2(header_width, header_height));
2052
Color c = bg_color;
2053
c.a *= 0.4;
2054
if (header_hover) {
2055
c = c.lightened(Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT) ? -0.05 : 0.2);
2056
}
2057
draw_rect(header_rect, c);
2058
2059
// Draw header title, folding arrow and count of revertable properties.
2060
{
2061
int outer_margin = Math::round(2 * EDSCALE);
2062
2063
int margin_start = section_indent + outer_margin;
2064
int margin_end = outer_margin;
2065
2066
// - Arrow.
2067
Ref<Texture2D> arrow = _get_arrow();
2068
if (arrow.is_valid()) {
2069
Point2 arrow_position;
2070
if (rtl) {
2071
arrow_position.x = get_size().width - (margin_start + arrow->get_width());
2072
} else {
2073
arrow_position.x = margin_start;
2074
}
2075
arrow_position.y = (header_height - arrow->get_height()) / 2;
2076
if (can_click_unfold) {
2077
draw_texture(arrow, arrow_position, Color(1, 1, 1, header_hover ? 1.0 : 0.85));
2078
}
2079
margin_start += arrow->get_width() + theme_cache.horizontal_separation;
2080
}
2081
2082
Ref<Font> font = theme_cache.bold_font;
2083
int font_size = theme_cache.bold_font_size;
2084
Color font_color = theme_cache.font_color;
2085
2086
Ref<Font> light_font = theme_cache.light_font;
2087
int light_font_size = theme_cache.light_font_size;
2088
2089
// - Keying
2090
Ref<Texture2D> key = theme_cache.icon_gui_animation_key;
2091
if (keying && key.is_valid()) {
2092
Point2 key_position;
2093
key_position.x = (rtl ? margin_end : (get_size().width - key->get_width() - margin_end)) - theme_cache.key_padding_size / 2;
2094
keying_rect = Rect2(key_position.x - theme_cache.key_padding_size / 2, 0, key->get_width() + theme_cache.key_padding_size, header_height);
2095
2096
Color key_color(1, 1, 1);
2097
if (keying_hover) {
2098
key_color.r *= 1.2;
2099
key_color.g *= 1.2;
2100
key_color.b *= 1.2;
2101
2102
Ref<StyleBox> sb_hover = theme_cache.key_hover;
2103
draw_style_box(sb_hover, keying_rect);
2104
}
2105
key_position.y = (header_height - key->get_height()) / 2;
2106
2107
draw_texture(key, key_position, key_color);
2108
margin_end += keying_rect.size.width + theme_cache.horizontal_separation;
2109
} else {
2110
keying_rect = Rect2();
2111
}
2112
2113
// - Checkbox.
2114
Ref<Texture2D> checkbox = _get_checkbox();
2115
if (checkbox.is_valid()) {
2116
const String checkbox_text = TTR("On");
2117
Size2 label_size = light_font->get_string_size(checkbox_text, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size);
2118
Point2 checkbox_position;
2119
Point2 label_position;
2120
if (rtl) {
2121
label_position.x = margin_end;
2122
checkbox_position.x = margin_end + label_size.width + 2 * EDSCALE;
2123
} else {
2124
label_position.x = get_size().width - (margin_end + label_size.width);
2125
checkbox_position.x = label_position.x - checkbox->get_width() - 2 * EDSCALE;
2126
}
2127
checkbox_position.y = (header_height - checkbox->get_height()) / 2;
2128
label_position.y = light_font->get_ascent(light_font_size) + (header_height - label_size.height) / 2.0;
2129
2130
check_rect = Rect2(checkbox_position.x, 0, checkbox->get_width() + label_size.width + 2 * EDSCALE, header_height);
2131
2132
Color check_font_color = font_color;
2133
Color checkbox_color(1, 1, 1);
2134
if (check_hover) {
2135
checkbox_color.r *= 1.2;
2136
checkbox_color.g *= 1.2;
2137
checkbox_color.b *= 1.2;
2138
check_font_color = checked ? theme_cache.font_hover_pressed_color : theme_cache.font_hover_color;
2139
} else if (checked) {
2140
check_font_color = theme_cache.font_pressed_color;
2141
}
2142
2143
draw_texture(checkbox, checkbox_position, checkbox_color);
2144
draw_string(light_font, label_position, checkbox_text, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, check_font_color, TextServer::JUSTIFICATION_NONE);
2145
margin_end += label_size.width + checkbox->get_width() + 6 * EDSCALE;
2146
} else {
2147
check_rect = Rect2();
2148
}
2149
2150
int available = get_size().width - (margin_start + margin_end);
2151
2152
// - Count of revertable properties.
2153
String num_revertable_str;
2154
int num_revertable_width = 0;
2155
2156
bool folded = (foldable || !checkbox_only) && !vbox->is_visible();
2157
if (folded && revertable_properties.size()) {
2158
int label_width = theme_cache.bold_font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, available, theme_cache.bold_font_size, TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_CONSTRAIN_ELLIPSIS).x;
2159
2160
// Can we fit the long version of the revertable count text?
2161
num_revertable_str = vformat(TTRN("(%d change)", "(%d changes)", revertable_properties.size()), revertable_properties.size());
2162
num_revertable_width = light_font->get_string_size(num_revertable_str, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, TextServer::JUSTIFICATION_NONE).x;
2163
if (label_width + outer_margin + num_revertable_width > available) {
2164
// We'll have to use the short version.
2165
num_revertable_str = vformat("(%d)", revertable_properties.size());
2166
num_revertable_width = light_font->get_string_size(num_revertable_str, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, TextServer::JUSTIFICATION_NONE).x;
2167
}
2168
2169
float text_offset_y = light_font->get_ascent(light_font_size) + (header_height - light_font->get_height(light_font_size)) / 2;
2170
Point2 text_offset = Point2(margin_end, text_offset_y).round();
2171
if (!rtl) {
2172
text_offset.x = get_size().width - (text_offset.x + num_revertable_width);
2173
}
2174
draw_string(light_font, text_offset, num_revertable_str, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, theme_cache.font_disabled_color, TextServer::JUSTIFICATION_NONE);
2175
margin_end += num_revertable_width + outer_margin;
2176
available -= num_revertable_width + outer_margin;
2177
}
2178
2179
// - Label.
2180
float text_offset_y = font->get_ascent(font_size) + (header_height - font->get_height(font_size)) / 2;
2181
Point2 text_offset = Point2(margin_start, text_offset_y).round();
2182
if (rtl) {
2183
text_offset.x = margin_end;
2184
}
2185
if (object->has_method("_get_property_warning") && !String(object->call("_get_property_warning", related_enable_property)).is_empty()) {
2186
font_color = theme_cache.warning_color;
2187
}
2188
const Color string_color = header_hover ? theme_cache.font_hover_mono_color : font_color;
2189
HorizontalAlignment text_align = rtl ? HORIZONTAL_ALIGNMENT_RIGHT : HORIZONTAL_ALIGNMENT_LEFT;
2190
draw_string(font, text_offset, label, text_align, available, font_size, string_color, TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_CONSTRAIN_ELLIPSIS);
2191
}
2192
2193
// Draw section indentation.
2194
if (section_indent_style.is_valid() && section_indent > 0) {
2195
Rect2 indent_rect = Rect2(Vector2(), Vector2(indent_depth * section_indent_size, get_size().height));
2196
if (rtl) {
2197
indent_rect.position.x = get_size().width - section_indent + section_indent_style->get_margin(SIDE_RIGHT);
2198
} else {
2199
indent_rect.position.x = section_indent_style->get_margin(SIDE_LEFT);
2200
}
2201
draw_style_box(section_indent_style, indent_rect);
2202
}
2203
} break;
2204
2205
case NOTIFICATION_DRAG_BEGIN: {
2206
dropping_for_unfold = true;
2207
} break;
2208
2209
case NOTIFICATION_DRAG_END: {
2210
dropping_for_unfold = false;
2211
} break;
2212
2213
case NOTIFICATION_MOUSE_ENTER: {
2214
if (dropping_for_unfold) {
2215
dropping_unfold_timer->start();
2216
}
2217
queue_redraw();
2218
} break;
2219
2220
case NOTIFICATION_MOUSE_EXIT_SELF:
2221
case NOTIFICATION_MOUSE_EXIT: {
2222
if (dropping_for_unfold) {
2223
dropping_unfold_timer->stop();
2224
}
2225
2226
if (header_hover || check_hover || keying_hover) {
2227
header_hover = false;
2228
check_hover = false;
2229
keying_hover = false;
2230
queue_redraw();
2231
}
2232
} break;
2233
2234
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
2235
if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor")) {
2236
dropping_unfold_timer->set_wait_time(EDITOR_GET("interface/editor/dragging_hover_wait_seconds"));
2237
}
2238
} break;
2239
}
2240
}
2241
2242
Size2 EditorInspectorSection::get_minimum_size() const {
2243
Size2 ms;
2244
for (int i = 0; i < get_child_count(); i++) {
2245
Control *c = as_sortable_control(get_child(i));
2246
if (!c) {
2247
continue;
2248
}
2249
Size2 minsize = c->get_combined_minimum_size();
2250
ms = ms.max(minsize);
2251
}
2252
2253
if (theme_cache.font.is_valid()) {
2254
ms.height += theme_cache.font->get_height(theme_cache.font_size) + theme_cache.vertical_separation;
2255
ms.width += theme_cache.inspector_margin;
2256
}
2257
2258
if (indent_depth > 0 && theme_cache.indent_size > 0) {
2259
ms.width += indent_depth * theme_cache.indent_size;
2260
}
2261
if (indent_depth > 0 && theme_cache.indent_box.is_valid()) {
2262
ms.width += theme_cache.indent_box->get_margin(SIDE_LEFT) + theme_cache.indent_box->get_margin(SIDE_RIGHT);
2263
}
2264
2265
return ms;
2266
}
2267
2268
EditorInspector *EditorInspectorSection::_get_parent_inspector() const {
2269
Node *parent = get_parent();
2270
while (parent) {
2271
EditorInspector *ei = Object::cast_to<EditorInspector>(parent);
2272
if (ei) {
2273
return ei;
2274
}
2275
parent = parent->get_parent();
2276
}
2277
return nullptr;
2278
}
2279
2280
Control *EditorInspectorSection::make_custom_tooltip(const String &p_text) const {
2281
if (!checkable) {
2282
return Container::make_custom_tooltip(p_text);
2283
}
2284
2285
String symbol;
2286
String prologue;
2287
2288
if (object->has_method("_get_property_warning")) {
2289
const String custom_warning = object->call("_get_property_warning", related_enable_property);
2290
if (!custom_warning.is_empty()) {
2291
prologue = "[b][color=" + theme_cache.warning_color.to_html(false) + "]" + custom_warning + "[/color][/b]";
2292
}
2293
}
2294
2295
symbol = p_text;
2296
2297
const EditorInspector *inspector = _get_parent_inspector();
2298
if (inspector) {
2299
const String custom_description = inspector->get_custom_property_description(p_text);
2300
if (!custom_description.is_empty()) {
2301
if (!prologue.is_empty()) {
2302
prologue += '\n';
2303
}
2304
prologue += custom_description;
2305
}
2306
}
2307
2308
if (!symbol.is_empty() || !prologue.is_empty()) {
2309
return EditorHelpBitTooltip::make_tooltip(const_cast<EditorInspectorSection *>(this), symbol, prologue);
2310
}
2311
2312
return nullptr;
2313
}
2314
2315
void EditorInspectorSection::setup(const String &p_section, const String &p_label, Object *p_object, const Color &p_bg_color, bool p_foldable, int p_indent_depth, int p_level) {
2316
section = p_section;
2317
label = p_label;
2318
object = p_object;
2319
bg_color = p_bg_color;
2320
foldable = p_foldable;
2321
indent_depth = p_indent_depth;
2322
level = p_level;
2323
2324
_test_unfold();
2325
2326
if (foldable) {
2327
if (object->editor_is_section_unfolded(section)) {
2328
vbox->show();
2329
} else {
2330
vbox->hide();
2331
}
2332
}
2333
}
2334
2335
void EditorInspectorSection::gui_input(const Ref<InputEvent> &p_event) {
2336
ERR_FAIL_COND(p_event.is_null());
2337
bool can_click_unfold = vbox->get_child_count(false) != 0 && !(!checkbox_only && checkable && !checked);
2338
2339
Ref<InputEventMouseMotion> mm = p_event;
2340
if (mm.is_valid()) {
2341
Vector2 mpos = mm->get_position();
2342
2343
bool new_check_hover = check_rect.has_point(mpos);
2344
if (new_check_hover != check_hover) {
2345
check_hover = new_check_hover;
2346
queue_redraw();
2347
}
2348
2349
bool new_keying_hover = keying_rect.has_point(mpos);
2350
if (new_keying_hover != keying_hover) {
2351
keying_hover = new_keying_hover;
2352
queue_redraw();
2353
}
2354
2355
bool new_header_hover = foldable && can_click_unfold && (get_local_mouse_position().y < _get_header_height());
2356
if (new_header_hover != header_hover) {
2357
header_hover = new_header_hover;
2358
queue_redraw();
2359
}
2360
}
2361
2362
Ref<InputEventKey> k = p_event;
2363
if (k.is_valid() && k->is_pressed()) {
2364
if (foldable && can_click_unfold && k->is_action("ui_accept", true)) {
2365
accept_event();
2366
2367
bool should_unfold = !object->editor_is_section_unfolded(section);
2368
if (should_unfold) {
2369
unfold();
2370
} else {
2371
fold();
2372
}
2373
}
2374
}
2375
2376
Ref<InputEventMouseButton> mb = p_event;
2377
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
2378
Vector2 pos = mb->get_position();
2379
2380
if (object->editor_is_section_unfolded(section)) {
2381
int header_height = _get_header_height();
2382
2383
if (pos.y >= header_height) {
2384
return;
2385
}
2386
}
2387
2388
accept_event();
2389
2390
if (checkable && check_rect.has_point(pos)) {
2391
checked = !checked;
2392
emit_signal(SNAME("section_toggled_by_user"), related_enable_property, checked);
2393
if (checked) {
2394
unfold();
2395
} else if (!checkbox_only) {
2396
vbox->hide();
2397
}
2398
} else if (keying && keying_rect.has_point(pos)) {
2399
emit_signal(SNAME("property_keyed"), related_enable_property, false);
2400
} else if (foldable) {
2401
bool should_unfold = can_click_unfold && !object->editor_is_section_unfolded(section);
2402
if (should_unfold) {
2403
unfold();
2404
} else {
2405
fold();
2406
}
2407
}
2408
} else if (mb.is_valid() && !mb->is_pressed()) {
2409
queue_redraw();
2410
}
2411
}
2412
2413
String EditorInspectorSection::get_section() const {
2414
return section;
2415
}
2416
2417
VBoxContainer *EditorInspectorSection::get_vbox() {
2418
return vbox;
2419
}
2420
2421
void EditorInspectorSection::_accessibility_action_collapse(const Variant &p_data) {
2422
fold();
2423
}
2424
2425
void EditorInspectorSection::_accessibility_action_expand(const Variant &p_data) {
2426
unfold();
2427
}
2428
2429
void EditorInspectorSection::unfold() {
2430
if ((!foldable && !checkable) || (!checkbox_only && checkable && !checked)) {
2431
return;
2432
}
2433
2434
_test_unfold();
2435
2436
if (foldable) {
2437
object->editor_set_section_unfold(section, true);
2438
}
2439
2440
vbox->show();
2441
queue_redraw();
2442
}
2443
2444
void EditorInspectorSection::fold() {
2445
if (!foldable || !vbox_added) {
2446
return;
2447
}
2448
2449
object->editor_set_section_unfold(section, false);
2450
vbox->hide();
2451
queue_redraw();
2452
}
2453
2454
void EditorInspectorSection::set_bg_color(const Color &p_bg_color) {
2455
bg_color = p_bg_color;
2456
queue_redraw();
2457
}
2458
2459
void EditorInspectorSection::set_keying(bool p_keying) {
2460
if (keying == (checkable && p_keying)) {
2461
return;
2462
}
2463
2464
keying = checkable && p_keying;
2465
if (checkable) {
2466
queue_redraw();
2467
}
2468
}
2469
2470
void EditorInspectorSection::reset_timer() {
2471
if (dropping_for_unfold && !dropping_unfold_timer->is_stopped()) {
2472
dropping_unfold_timer->start();
2473
}
2474
}
2475
2476
void EditorInspectorSection::set_checkable(const String &p_related_check_property, bool p_checkbox_only, bool p_checked) {
2477
if (checkable == !p_related_check_property.is_empty()) {
2478
return;
2479
}
2480
2481
checkbox_only = p_checkbox_only;
2482
checkable = !p_related_check_property.is_empty();
2483
checked = p_checked;
2484
related_enable_property = p_related_check_property;
2485
2486
if (InspectorDock::get_singleton()) {
2487
if (checkable) {
2488
InspectorDock::get_inspector_singleton()->connect("property_edited", callable_mp(this, &EditorInspectorSection::_property_edited));
2489
} else {
2490
InspectorDock::get_inspector_singleton()->disconnect("property_edited", callable_mp(this, &EditorInspectorSection::_property_edited));
2491
}
2492
}
2493
2494
if (!checkbox_only && checkable && !checked) {
2495
vbox->hide();
2496
}
2497
2498
queue_redraw();
2499
}
2500
2501
void EditorInspectorSection::set_checked(bool p_checked) {
2502
if (checked == p_checked) {
2503
return;
2504
}
2505
2506
checked = p_checked;
2507
if (!checkbox_only && checkable && !checked) {
2508
vbox->hide();
2509
} else if (!checkbox_only) {
2510
unfold();
2511
}
2512
2513
queue_redraw();
2514
}
2515
2516
bool EditorInspectorSection::has_revertable_properties() const {
2517
return !revertable_properties.is_empty();
2518
}
2519
2520
void EditorInspectorSection::property_can_revert_changed(const String &p_path, bool p_can_revert) {
2521
bool had_revertable_properties = has_revertable_properties();
2522
if (p_can_revert) {
2523
revertable_properties.insert(p_path);
2524
} else {
2525
revertable_properties.erase(p_path);
2526
}
2527
if (has_revertable_properties() != had_revertable_properties) {
2528
queue_redraw();
2529
}
2530
}
2531
2532
void EditorInspectorSection::_property_edited(const String &p_property) {
2533
if (!related_enable_property.is_empty() && p_property == related_enable_property) {
2534
update_property();
2535
}
2536
}
2537
2538
void EditorInspectorSection::update_property() {
2539
if (!checkable) {
2540
return;
2541
}
2542
2543
bool valid = false;
2544
Variant value_checked = object->get(related_enable_property, &valid);
2545
2546
if (valid) {
2547
set_checked(value_checked.operator bool());
2548
}
2549
}
2550
2551
void EditorInspectorSection::_bind_methods() {
2552
ClassDB::bind_method(D_METHOD("setup", "section", "label", "object", "bg_color", "foldable", "indent_depth", "level"), &EditorInspectorSection::setup, DEFVAL(0), DEFVAL(1));
2553
ClassDB::bind_method(D_METHOD("get_vbox"), &EditorInspectorSection::get_vbox);
2554
ClassDB::bind_method(D_METHOD("unfold"), &EditorInspectorSection::unfold);
2555
ClassDB::bind_method(D_METHOD("fold"), &EditorInspectorSection::fold);
2556
2557
ADD_SIGNAL(MethodInfo("section_toggled_by_user", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "value")));
2558
ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING_NAME, "property")));
2559
}
2560
2561
EditorInspectorSection::EditorInspectorSection() {
2562
set_focus_mode(FOCUS_ACCESSIBILITY);
2563
2564
vbox = memnew(VBoxContainer);
2565
vbox->set_theme_type_variation(SNAME("EditorPropertyContainer"));
2566
2567
dropping_unfold_timer = memnew(Timer);
2568
dropping_unfold_timer->set_wait_time(EDITOR_GET("interface/editor/dragging_hover_wait_seconds"));
2569
dropping_unfold_timer->set_one_shot(true);
2570
add_child(dropping_unfold_timer);
2571
dropping_unfold_timer->connect("timeout", callable_mp(this, &EditorInspectorSection::unfold));
2572
}
2573
2574
EditorInspectorSection::~EditorInspectorSection() {
2575
if (!vbox_added) {
2576
memdelete(vbox);
2577
}
2578
2579
if (checkable && InspectorDock::get_singleton()) {
2580
InspectorDock::get_inspector_singleton()->disconnect("property_edited", callable_mp(this, &EditorInspectorSection::_property_edited));
2581
}
2582
}
2583
2584
////////////////////////////////////////////////
2585
////////////////////////////////////////////////
2586
2587
int EditorInspectorArray::_get_array_count() {
2588
if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
2589
List<PropertyInfo> object_property_list;
2590
object->get_property_list(&object_property_list);
2591
return _extract_properties_as_array(object_property_list).size();
2592
} else if (mode == MODE_USE_COUNT_PROPERTY) {
2593
bool valid;
2594
int count_val = object->get(count_property, &valid);
2595
ERR_FAIL_COND_V_MSG(!valid, 0, vformat("%s is not a valid property to be used as array count.", count_property));
2596
return count_val;
2597
}
2598
return 0;
2599
}
2600
2601
void EditorInspectorArray::_add_button_pressed() {
2602
_move_element(-1, -1);
2603
}
2604
2605
void EditorInspectorArray::_paginator_page_changed(int p_page) {
2606
emit_signal("page_change_request", p_page);
2607
}
2608
2609
void EditorInspectorArray::_rmb_popup_id_pressed(int p_id) {
2610
switch (p_id) {
2611
case OPTION_MOVE_UP:
2612
if (popup_array_index_pressed > 0) {
2613
_move_element(popup_array_index_pressed, popup_array_index_pressed - 1);
2614
}
2615
break;
2616
case OPTION_MOVE_DOWN:
2617
if (popup_array_index_pressed < count - 1) {
2618
_move_element(popup_array_index_pressed, popup_array_index_pressed + 2);
2619
}
2620
break;
2621
case OPTION_NEW_BEFORE:
2622
_move_element(-1, popup_array_index_pressed);
2623
break;
2624
case OPTION_NEW_AFTER:
2625
_move_element(-1, popup_array_index_pressed + 1);
2626
break;
2627
case OPTION_REMOVE:
2628
_move_element(popup_array_index_pressed, -1);
2629
break;
2630
case OPTION_CLEAR_ARRAY:
2631
_clear_array();
2632
break;
2633
case OPTION_RESIZE_ARRAY:
2634
new_size_spin_box->set_value(count);
2635
resize_dialog->get_ok_button()->set_disabled(true);
2636
resize_dialog->popup_centered(Size2(250, 0) * EDSCALE);
2637
new_size_spin_box->get_line_edit()->grab_focus();
2638
new_size_spin_box->get_line_edit()->select_all();
2639
break;
2640
default:
2641
break;
2642
}
2643
}
2644
2645
void EditorInspectorArray::_control_dropping_draw() {
2646
int drop_position = _drop_position();
2647
2648
if (dropping && drop_position >= 0) {
2649
Vector2 from;
2650
Vector2 to;
2651
if (drop_position < elements_vbox->get_child_count()) {
2652
Transform2D xform = Object::cast_to<Control>(elements_vbox->get_child(drop_position))->get_transform();
2653
from = xform.xform(Vector2());
2654
to = xform.xform(Vector2(elements_vbox->get_size().x, 0));
2655
} else {
2656
Control *child = Object::cast_to<Control>(elements_vbox->get_child(drop_position - 1));
2657
Transform2D xform = child->get_transform();
2658
from = xform.xform(Vector2(0, child->get_size().y));
2659
to = xform.xform(Vector2(elements_vbox->get_size().x, child->get_size().y));
2660
}
2661
Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
2662
control_dropping->draw_line(from, to, color, 2);
2663
}
2664
}
2665
2666
void EditorInspectorArray::_vbox_visibility_changed() {
2667
control_dropping->set_visible(vbox->is_visible_in_tree());
2668
}
2669
2670
void EditorInspectorArray::_panel_draw(int p_index) {
2671
ERR_FAIL_INDEX(p_index, (int)array_elements.size());
2672
2673
Ref<StyleBox> style = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles));
2674
if (style.is_null()) {
2675
return;
2676
}
2677
if (array_elements[p_index].panel->has_focus(true)) {
2678
array_elements[p_index].panel->draw_style_box(style, Rect2(Vector2(), array_elements[p_index].panel->get_size()));
2679
}
2680
}
2681
2682
void EditorInspectorArray::_panel_gui_focus(int p_index) {
2683
array_elements[p_index].panel->queue_redraw();
2684
selected = p_index;
2685
}
2686
2687
void EditorInspectorArray::_panel_gui_unfocus(int p_index) {
2688
array_elements[p_index].panel->queue_redraw();
2689
if (selected == p_index) {
2690
selected = -1;
2691
}
2692
}
2693
2694
void EditorInspectorArray::_panel_gui_input(Ref<InputEvent> p_event, int p_index) {
2695
ERR_FAIL_INDEX(p_index, (int)array_elements.size());
2696
2697
if (read_only) {
2698
return;
2699
}
2700
2701
Ref<InputEventKey> key_ref = p_event;
2702
if (key_ref.is_valid()) {
2703
const InputEventKey &key = **key_ref;
2704
2705
if (array_elements[p_index].panel->has_focus() && key.is_pressed() && key.get_keycode() == Key::KEY_DELETE) {
2706
_move_element(begin_array_index + p_index, -1);
2707
array_elements[p_index].panel->accept_event();
2708
}
2709
}
2710
2711
Ref<InputEventMouseButton> mb = p_event;
2712
if (mb.is_valid()) {
2713
if (movable && mb->get_button_index() == MouseButton::RIGHT) {
2714
array_elements[p_index].panel->accept_event();
2715
popup_array_index_pressed = begin_array_index + p_index;
2716
rmb_popup->set_item_disabled(OPTION_MOVE_UP, popup_array_index_pressed == 0);
2717
rmb_popup->set_item_disabled(OPTION_MOVE_DOWN, popup_array_index_pressed == count - 1);
2718
rmb_popup->set_position(array_elements[p_index].panel->get_screen_position() + mb->get_position());
2719
rmb_popup->reset_size();
2720
rmb_popup->popup();
2721
}
2722
}
2723
}
2724
2725
void EditorInspectorArray::show_menu(int p_index, const Vector2 &p_offset) {
2726
popup_array_index_pressed = begin_array_index + p_index;
2727
rmb_popup->set_item_disabled(OPTION_MOVE_UP, popup_array_index_pressed == 0);
2728
rmb_popup->set_item_disabled(OPTION_MOVE_DOWN, popup_array_index_pressed == count - 1);
2729
rmb_popup->set_position(get_screen_position() + p_offset);
2730
rmb_popup->reset_size();
2731
rmb_popup->popup();
2732
}
2733
2734
void EditorInspectorArray::_move_element(int p_element_index, int p_to_pos) {
2735
String action_name;
2736
if (p_element_index < 0) {
2737
action_name = vformat(TTR("Add element to property array with prefix %s."), array_element_prefix);
2738
} else if (p_to_pos < 0) {
2739
action_name = vformat(TTR("Remove element %d from property array with prefix %s."), p_element_index, array_element_prefix);
2740
} else {
2741
action_name = vformat(TTR("Move element %d to position %d in property array with prefix %s."), p_element_index, p_to_pos, array_element_prefix);
2742
}
2743
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
2744
undo_redo->create_action(action_name);
2745
if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
2746
// Call the function.
2747
Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
2748
if (move_function.is_valid()) {
2749
move_function.call(undo_redo, object, array_element_prefix, p_element_index, p_to_pos);
2750
} else {
2751
WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
2752
}
2753
} else if (mode == MODE_USE_COUNT_PROPERTY) {
2754
ERR_FAIL_COND(p_to_pos < -1 || p_to_pos > count);
2755
2756
if (!swap_method.is_empty()) {
2757
ERR_FAIL_COND(!object->has_method(swap_method));
2758
2759
// Swap method was provided, use it.
2760
if (p_element_index < 0) {
2761
// Add an element at position
2762
undo_redo->add_do_property(object, count_property, count + 1);
2763
if (p_to_pos >= 0) {
2764
for (int i = count; i > p_to_pos; i--) {
2765
undo_redo->add_do_method(object, swap_method, i, i - 1);
2766
}
2767
for (int i = p_to_pos; i < count; i++) {
2768
undo_redo->add_undo_method(object, swap_method, i, i + 1);
2769
}
2770
}
2771
undo_redo->add_undo_property(object, count_property, count);
2772
2773
} else if (p_to_pos < 0) {
2774
if (count > 0) {
2775
// Remove element at position
2776
undo_redo->add_undo_property(object, count_property, count);
2777
2778
List<PropertyInfo> object_property_list;
2779
object->get_property_list(&object_property_list);
2780
2781
for (int i = p_element_index; i < count - 1; i++) {
2782
undo_redo->add_do_method(object, swap_method, i, i + 1);
2783
}
2784
2785
for (int i = count; i > p_element_index; i--) {
2786
undo_redo->add_undo_method(object, swap_method, i, i - 1);
2787
}
2788
2789
String erase_prefix = String(array_element_prefix) + itos(p_element_index);
2790
2791
for (const PropertyInfo &E : object_property_list) {
2792
if (E.name.begins_with(erase_prefix)) {
2793
undo_redo->add_undo_property(object, E.name, object->get(E.name));
2794
}
2795
}
2796
2797
undo_redo->add_do_property(object, count_property, count - 1);
2798
}
2799
} else {
2800
if (p_to_pos > p_element_index) {
2801
p_to_pos--;
2802
}
2803
2804
if (p_to_pos < p_element_index) {
2805
for (int i = p_element_index; i > p_to_pos; i--) {
2806
undo_redo->add_do_method(object, swap_method, i, i - 1);
2807
}
2808
for (int i = p_to_pos; i < p_element_index; i++) {
2809
undo_redo->add_undo_method(object, swap_method, i, i + 1);
2810
}
2811
} else if (p_to_pos > p_element_index) {
2812
for (int i = p_element_index; i < p_to_pos; i++) {
2813
undo_redo->add_do_method(object, swap_method, i, i + 1);
2814
}
2815
2816
for (int i = p_to_pos; i > p_element_index; i--) {
2817
undo_redo->add_undo_method(object, swap_method, i, i - 1);
2818
}
2819
}
2820
}
2821
} else {
2822
// Use standard properties.
2823
List<PropertyInfo> object_property_list;
2824
object->get_property_list(&object_property_list);
2825
2826
Array properties_as_array = _extract_properties_as_array(object_property_list);
2827
properties_as_array.resize(count);
2828
2829
// For undoing things
2830
undo_redo->add_undo_property(object, count_property, properties_as_array.size());
2831
for (int i = 0; i < (int)properties_as_array.size(); i++) {
2832
Dictionary d = Dictionary(properties_as_array[i]);
2833
for (const KeyValue<Variant, Variant> &kv : d) {
2834
undo_redo->add_undo_property(object, vformat(kv.key, i), kv.value);
2835
}
2836
}
2837
2838
if (p_element_index < 0) {
2839
// Add an element.
2840
properties_as_array.insert(p_to_pos < 0 ? properties_as_array.size() : p_to_pos, Dictionary());
2841
} else if (p_to_pos < 0) {
2842
// Delete the element.
2843
properties_as_array.remove_at(p_element_index);
2844
} else {
2845
// Move the element.
2846
properties_as_array.insert(p_to_pos, properties_as_array[p_element_index].duplicate());
2847
properties_as_array.remove_at(p_to_pos < p_element_index ? p_element_index + 1 : p_element_index);
2848
}
2849
2850
// Change the array size then set the properties.
2851
undo_redo->add_do_property(object, count_property, properties_as_array.size());
2852
for (int i = 0; i < (int)properties_as_array.size(); i++) {
2853
Dictionary d = properties_as_array[i];
2854
for (const KeyValue<Variant, Variant> &kv : d) {
2855
undo_redo->add_do_property(object, vformat(kv.key, i), kv.value);
2856
}
2857
}
2858
}
2859
}
2860
undo_redo->commit_action();
2861
2862
// Handle page change and update counts.
2863
if (p_element_index < 0) {
2864
int added_index = p_to_pos < 0 ? count : p_to_pos;
2865
emit_signal(SNAME("page_change_request"), added_index / page_length);
2866
count += 1;
2867
} else if (p_to_pos < 0) {
2868
count -= 1;
2869
if (page == max_page && (MAX(0, count - 1) / page_length != max_page)) {
2870
emit_signal(SNAME("page_change_request"), max_page - 1);
2871
}
2872
} else if (p_to_pos == begin_array_index - 1) {
2873
emit_signal(SNAME("page_change_request"), page - 1);
2874
} else if (p_to_pos > end_array_index) {
2875
emit_signal(SNAME("page_change_request"), page + 1);
2876
}
2877
begin_array_index = page * page_length;
2878
end_array_index = MIN(count, (page + 1) * page_length);
2879
max_page = MAX(0, count - 1) / page_length;
2880
}
2881
2882
void EditorInspectorArray::_clear_array() {
2883
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
2884
undo_redo->create_action(vformat(TTR("Clear Property Array with Prefix %s"), array_element_prefix));
2885
if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
2886
for (int i = count - 1; i >= 0; i--) {
2887
// Call the function.
2888
Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
2889
if (move_function.is_valid()) {
2890
move_function.call(undo_redo, object, array_element_prefix, i, -1);
2891
} else {
2892
WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
2893
}
2894
}
2895
} else if (mode == MODE_USE_COUNT_PROPERTY) {
2896
List<PropertyInfo> object_property_list;
2897
object->get_property_list(&object_property_list);
2898
2899
Array properties_as_array = _extract_properties_as_array(object_property_list);
2900
properties_as_array.resize(count);
2901
2902
// For undoing things
2903
undo_redo->add_undo_property(object, count_property, count);
2904
for (int i = 0; i < (int)properties_as_array.size(); i++) {
2905
Dictionary d = Dictionary(properties_as_array[i]);
2906
for (const KeyValue<Variant, Variant> &kv : d) {
2907
undo_redo->add_undo_property(object, vformat(kv.key, i), kv.value);
2908
}
2909
}
2910
2911
// Change the array size then set the properties.
2912
undo_redo->add_do_property(object, count_property, 0);
2913
}
2914
undo_redo->commit_action();
2915
2916
// Handle page change and update counts.
2917
emit_signal(SNAME("page_change_request"), 0);
2918
count = 0;
2919
begin_array_index = 0;
2920
end_array_index = 0;
2921
max_page = 0;
2922
}
2923
2924
void EditorInspectorArray::_resize_array(int p_size) {
2925
ERR_FAIL_COND(p_size < 0);
2926
if (p_size == count) {
2927
return;
2928
}
2929
2930
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
2931
undo_redo->create_action(vformat(TTR("Resize Property Array with Prefix %s"), array_element_prefix));
2932
if (p_size > count) {
2933
if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
2934
for (int i = count; i < p_size; i++) {
2935
// Call the function.
2936
Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
2937
if (move_function.is_valid()) {
2938
move_function.call(undo_redo, object, array_element_prefix, -1, -1);
2939
} else {
2940
WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
2941
}
2942
}
2943
} else if (mode == MODE_USE_COUNT_PROPERTY) {
2944
undo_redo->add_undo_property(object, count_property, count);
2945
undo_redo->add_do_property(object, count_property, p_size);
2946
}
2947
} else {
2948
if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
2949
for (int i = count - 1; i > p_size - 1; i--) {
2950
// Call the function.
2951
Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
2952
if (move_function.is_valid()) {
2953
move_function.call(undo_redo, object, array_element_prefix, i, -1);
2954
} else {
2955
WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
2956
}
2957
}
2958
} else if (mode == MODE_USE_COUNT_PROPERTY) {
2959
List<PropertyInfo> object_property_list;
2960
object->get_property_list(&object_property_list);
2961
2962
Array properties_as_array = _extract_properties_as_array(object_property_list);
2963
properties_as_array.resize(count);
2964
2965
// For undoing things
2966
undo_redo->add_undo_property(object, count_property, count);
2967
for (int i = count - 1; i > p_size - 1; i--) {
2968
Dictionary d = Dictionary(properties_as_array[i]);
2969
for (const KeyValue<Variant, Variant> &kv : d) {
2970
undo_redo->add_undo_property(object, vformat(kv.key, i), kv.value);
2971
}
2972
}
2973
2974
// Change the array size then set the properties.
2975
undo_redo->add_do_property(object, count_property, p_size);
2976
}
2977
}
2978
undo_redo->commit_action();
2979
2980
// Handle page change and update counts.
2981
emit_signal(SNAME("page_change_request"), 0);
2982
/*
2983
count = 0;
2984
begin_array_index = 0;
2985
end_array_index = 0;
2986
max_page = 0;
2987
*/
2988
}
2989
2990
Array EditorInspectorArray::_extract_properties_as_array(const List<PropertyInfo> &p_list) {
2991
Array output;
2992
2993
for (const PropertyInfo &pi : p_list) {
2994
if (!(pi.usage & PROPERTY_USAGE_EDITOR)) {
2995
continue;
2996
}
2997
2998
if (pi.name.begins_with(array_element_prefix)) {
2999
String str = pi.name.trim_prefix(array_element_prefix);
3000
3001
int to_char_index = 0;
3002
while (to_char_index < str.length()) {
3003
if (!is_digit(str[to_char_index])) {
3004
break;
3005
}
3006
to_char_index++;
3007
}
3008
if (to_char_index > 0) {
3009
int array_index = str.left(to_char_index).to_int();
3010
Error error = OK;
3011
if (array_index >= output.size()) {
3012
error = output.resize(array_index + 1);
3013
}
3014
if (error == OK) {
3015
String format_string = String(array_element_prefix) + "%d" + str.substr(to_char_index);
3016
Dictionary dict = output[array_index];
3017
dict[format_string] = object->get(pi.name);
3018
output[array_index] = dict;
3019
} else {
3020
WARN_PRINT(vformat("Array element %s has an index too high. Array allocation failed.", pi.name));
3021
}
3022
}
3023
}
3024
}
3025
return output;
3026
}
3027
3028
int EditorInspectorArray::_drop_position() const {
3029
for (int i = 0; i < (int)array_elements.size(); i++) {
3030
const ArrayElement &ae = array_elements[i];
3031
3032
Size2 size = ae.panel->get_size();
3033
Vector2 mp = ae.panel->get_local_mouse_position();
3034
3035
if (Rect2(Vector2(), size).has_point(mp)) {
3036
if (mp.y < size.y / 2) {
3037
return i;
3038
} else {
3039
return i + 1;
3040
}
3041
}
3042
}
3043
return -1;
3044
}
3045
3046
void EditorInspectorArray::_resize_dialog_confirmed() {
3047
if (int(new_size_spin_box->get_value()) == count) {
3048
return;
3049
}
3050
3051
resize_dialog->hide();
3052
_resize_array(int(new_size_spin_box->get_value()));
3053
}
3054
3055
void EditorInspectorArray::_new_size_spin_box_value_changed(float p_value) {
3056
resize_dialog->get_ok_button()->set_disabled(int(p_value) == count);
3057
}
3058
3059
void EditorInspectorArray::_new_size_spin_box_text_submitted(const String &p_text) {
3060
_resize_dialog_confirmed();
3061
}
3062
3063
void EditorInspectorArray::_setup() {
3064
// Setup counts.
3065
count = _get_array_count();
3066
begin_array_index = page * page_length;
3067
end_array_index = MIN(count, (page + 1) * page_length);
3068
max_page = MAX(0, count - 1) / page_length;
3069
array_elements.resize(MAX(0, end_array_index - begin_array_index));
3070
if (page < 0 || page > max_page) {
3071
WARN_PRINT(vformat("Invalid page number %d", page));
3072
page = CLAMP(page, 0, max_page);
3073
}
3074
3075
Ref<Font> numbers_font;
3076
int numbers_min_w = 0;
3077
bool unresizable = is_const || read_only;
3078
3079
if (numbered) {
3080
numbers_font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
3081
int digits_found = count;
3082
String test;
3083
while (digits_found) {
3084
test += "8";
3085
digits_found /= 10;
3086
}
3087
numbers_min_w = numbers_font->get_string_size(test).width;
3088
}
3089
3090
for (int i = 0; i < (int)array_elements.size(); i++) {
3091
ArrayElement &ae = array_elements[i];
3092
3093
// Panel and its hbox.
3094
ae.panel = memnew(ArrayPanelContainer);
3095
ae.panel->set_focus_mode(FOCUS_ALL);
3096
ae.panel->set_mouse_filter(MOUSE_FILTER_PASS);
3097
SET_DRAG_FORWARDING_GCD(ae.panel, EditorInspectorArray);
3098
3099
int element_position = begin_array_index + i;
3100
String ae_name = vformat(TTR("Element %d: %s%d*"), element_position, array_element_prefix, element_position);
3101
3102
ae.panel->set_meta("index", element_position);
3103
ae.panel->set_meta("name", ae_name);
3104
ae.panel->set_meta("element", this);
3105
ae.panel->set_tooltip_text(ae_name);
3106
ae.panel->connect(SceneStringName(focus_entered), callable_mp(this, &EditorInspectorArray::_panel_gui_focus).bind(i));
3107
ae.panel->connect(SceneStringName(focus_exited), callable_mp(this, &EditorInspectorArray::_panel_gui_unfocus).bind(i));
3108
ae.panel->connect(SceneStringName(draw), callable_mp(this, &EditorInspectorArray::_panel_draw).bind(i));
3109
ae.panel->connect(SceneStringName(gui_input), callable_mp(this, &EditorInspectorArray::_panel_gui_input).bind(i));
3110
ae.panel->add_theme_style_override(SceneStringName(panel), i % 2 ? odd_style : even_style);
3111
elements_vbox->add_child(ae.panel);
3112
3113
ae.margin = memnew(MarginContainer);
3114
ae.margin->set_mouse_filter(MOUSE_FILTER_PASS);
3115
if (is_inside_tree()) {
3116
Size2 min_size = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles))->get_minimum_size();
3117
ae.margin->begin_bulk_theme_override();
3118
ae.margin->add_theme_constant_override("margin_left", min_size.x / 2);
3119
ae.margin->add_theme_constant_override("margin_top", min_size.y / 2);
3120
ae.margin->add_theme_constant_override("margin_right", min_size.x / 2);
3121
ae.margin->add_theme_constant_override("margin_bottom", min_size.y / 2);
3122
ae.margin->end_bulk_theme_override();
3123
}
3124
ae.panel->add_child(ae.margin);
3125
3126
ae.hbox = memnew(HBoxContainer);
3127
ae.hbox->set_h_size_flags(SIZE_EXPAND_FILL);
3128
ae.hbox->set_v_size_flags(SIZE_EXPAND_FILL);
3129
ae.margin->add_child(ae.hbox);
3130
3131
// Move button.
3132
if (movable) {
3133
VBoxContainer *move_vbox = memnew(VBoxContainer);
3134
move_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
3135
move_vbox->set_alignment(BoxContainer::ALIGNMENT_CENTER);
3136
ae.hbox->add_child(move_vbox);
3137
3138
if (element_position > 0) {
3139
ae.move_up = memnew(Button);
3140
ae.move_up->set_accessibility_name(TTRC("Move Up"));
3141
ae.move_up->set_button_icon(get_editor_theme_icon(SNAME("MoveUp")));
3142
ae.move_up->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_move_element).bind(element_position, element_position - 1));
3143
move_vbox->add_child(ae.move_up);
3144
}
3145
3146
ae.move_texture_rect = memnew(TextureRect);
3147
ae.move_texture_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
3148
ae.move_texture_rect->set_default_cursor_shape(Control::CURSOR_MOVE);
3149
3150
if (is_inside_tree()) {
3151
ae.move_texture_rect->set_texture(get_editor_theme_icon(SNAME("TripleBar")));
3152
}
3153
move_vbox->add_child(ae.move_texture_rect);
3154
3155
if (element_position < count - 1) {
3156
ae.move_down = memnew(Button);
3157
ae.move_down->set_accessibility_name(TTRC("Move Down"));
3158
ae.move_down->set_button_icon(get_editor_theme_icon(SNAME("MoveDown")));
3159
ae.move_down->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_move_element).bind(element_position, element_position + 2));
3160
move_vbox->add_child(ae.move_down);
3161
}
3162
}
3163
3164
if (numbered) {
3165
ae.number = memnew(Label);
3166
ae.number->add_theme_font_override(SceneStringName(font), numbers_font);
3167
ae.number->set_custom_minimum_size(Size2(numbers_min_w, 0));
3168
ae.number->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
3169
ae.number->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
3170
ae.number->set_text(itos(element_position));
3171
ae.hbox->add_child(ae.number);
3172
}
3173
3174
// Right vbox.
3175
ae.vbox = memnew(VBoxContainer);
3176
ae.vbox->set_h_size_flags(SIZE_EXPAND_FILL);
3177
ae.vbox->set_v_size_flags(SIZE_EXPAND_FILL);
3178
ae.hbox->add_child(ae.vbox);
3179
3180
if (!unresizable) {
3181
ae.erase = memnew(Button);
3182
ae.erase->set_accessibility_name(TTRC("Remove"));
3183
ae.erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
3184
ae.erase->set_v_size_flags(SIZE_SHRINK_CENTER);
3185
ae.erase->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_remove_item).bind(element_position));
3186
ae.hbox->add_child(ae.erase);
3187
}
3188
}
3189
3190
// Hide/show the add button.
3191
add_button->set_visible(page == max_page && !unresizable);
3192
3193
// Add paginator if there's more than 1 page.
3194
if (max_page > 0) {
3195
EditorPaginator *paginator = memnew(EditorPaginator);
3196
paginator->update(page, max_page);
3197
paginator->connect("page_changed", callable_mp(this, &EditorInspectorArray::_paginator_page_changed));
3198
vbox->add_child(paginator);
3199
}
3200
}
3201
3202
void EditorInspectorArray::_remove_item(int p_index) {
3203
_move_element(p_index, -1);
3204
}
3205
3206
Variant EditorInspectorArray::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
3207
if (!movable) {
3208
return Variant();
3209
}
3210
int index = p_from->get_meta("index");
3211
Dictionary dict;
3212
dict["type"] = "property_array_element";
3213
dict["property_array_prefix"] = array_element_prefix;
3214
dict["index"] = index;
3215
3216
return dict;
3217
}
3218
3219
void EditorInspectorArray::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
3220
Dictionary dict = p_data;
3221
3222
int to_drop = dict["index"];
3223
int drop_position = (p_point == Vector2(Math::INF, Math::INF)) ? selected : _drop_position();
3224
if (drop_position < 0) {
3225
return;
3226
}
3227
_move_element(to_drop, begin_array_index + drop_position);
3228
}
3229
3230
bool EditorInspectorArray::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
3231
if (!movable || read_only) {
3232
return false;
3233
}
3234
// First, update drawing.
3235
control_dropping->queue_redraw();
3236
3237
if (p_data.get_type() != Variant::DICTIONARY) {
3238
return false;
3239
}
3240
Dictionary dict = p_data;
3241
int drop_position = (p_point == Vector2(Math::INF, Math::INF)) ? selected : _drop_position();
3242
if (!dict.has("type") || dict["type"] != "property_array_element" || String(dict["property_array_prefix"]) != array_element_prefix || drop_position < 0) {
3243
return false;
3244
}
3245
3246
// Check in dropping at the given index does indeed move the item.
3247
int moved_array_index = (int)dict["index"];
3248
int drop_array_index = begin_array_index + drop_position;
3249
3250
return drop_array_index != moved_array_index && drop_array_index - 1 != moved_array_index;
3251
}
3252
3253
void ArrayPanelContainer::_accessibility_action_menu(const Variant &p_data) {
3254
EditorInspectorArray *el = Object::cast_to<EditorInspectorArray>(get_meta("element"));
3255
if (el) {
3256
int index = get_meta("index");
3257
el->show_menu(index, Vector2());
3258
}
3259
}
3260
3261
void ArrayPanelContainer::_notification(int p_what) {
3262
switch (p_what) {
3263
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
3264
RID ae = get_accessibility_element();
3265
ERR_FAIL_COND(ae.is_null());
3266
3267
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
3268
3269
DisplayServer::get_singleton()->accessibility_update_set_name(ae, get_meta("text"));
3270
DisplayServer::get_singleton()->accessibility_update_set_value(ae, get_meta("text"));
3271
3272
DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_MENU);
3273
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &ArrayPanelContainer::_accessibility_action_menu));
3274
} break;
3275
}
3276
}
3277
3278
ArrayPanelContainer::ArrayPanelContainer() {
3279
set_focus_mode(FOCUS_ACCESSIBILITY);
3280
}
3281
3282
void EditorInspectorArray::_notification(int p_what) {
3283
switch (p_what) {
3284
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
3285
RID ae = get_accessibility_element();
3286
ERR_FAIL_COND(ae.is_null());
3287
3288
DisplayServer::get_singleton()->accessibility_update_set_name(ae, vformat(TTR("Array: %s"), get_label()));
3289
DisplayServer::get_singleton()->accessibility_update_set_value(ae, vformat(TTR("Array: %s"), get_label()));
3290
} break;
3291
3292
case NOTIFICATION_THEME_CHANGED: {
3293
Color color = get_theme_color(SNAME("bg"), SNAME("EditorInspectorArray"));
3294
odd_style->set_bg_color(color.darkened(-0.1));
3295
even_style->set_bg_color(color.darkened(0.1));
3296
3297
for (ArrayElement &ae : array_elements) {
3298
if (ae.move_texture_rect) {
3299
ae.move_texture_rect->set_texture(get_editor_theme_icon(SNAME("TripleBar")));
3300
}
3301
if (ae.move_up) {
3302
ae.move_up->set_button_icon(get_editor_theme_icon(SNAME("MoveUp")));
3303
}
3304
if (ae.move_down) {
3305
ae.move_down->set_button_icon(get_editor_theme_icon(SNAME("MoveDown")));
3306
}
3307
Size2 min_size = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles))->get_minimum_size();
3308
ae.margin->begin_bulk_theme_override();
3309
ae.margin->add_theme_constant_override("margin_left", min_size.x / 2);
3310
ae.margin->add_theme_constant_override("margin_top", min_size.y / 2);
3311
ae.margin->add_theme_constant_override("margin_right", min_size.x / 2);
3312
ae.margin->add_theme_constant_override("margin_bottom", min_size.y / 2);
3313
ae.margin->end_bulk_theme_override();
3314
3315
if (ae.erase) {
3316
ae.erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
3317
}
3318
}
3319
} break;
3320
3321
case NOTIFICATION_DRAG_BEGIN: {
3322
Dictionary dict = get_viewport()->gui_get_drag_data();
3323
if (dict.has("type") && dict["type"] == "property_array_element" && String(dict["property_array_prefix"]) == array_element_prefix) {
3324
dropping = true;
3325
control_dropping->queue_redraw();
3326
}
3327
} break;
3328
3329
case NOTIFICATION_DRAG_END: {
3330
if (dropping) {
3331
dropping = false;
3332
control_dropping->queue_redraw();
3333
}
3334
} break;
3335
}
3336
}
3337
3338
void EditorInspectorArray::_bind_methods() {
3339
ADD_SIGNAL(MethodInfo("page_change_request"));
3340
}
3341
3342
void EditorInspectorArray::setup_with_move_element_function(Object *p_object, const String &p_label, const StringName &p_array_element_prefix, int p_page, const Color &p_bg_color, bool p_foldable, bool p_movable, bool p_is_const, bool p_numbered, int p_page_length, const String &p_add_item_text) {
3343
count_property = "";
3344
mode = MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION;
3345
array_element_prefix = p_array_element_prefix;
3346
page = p_page;
3347
movable = p_movable;
3348
is_const = p_is_const;
3349
page_length = p_page_length;
3350
numbered = p_numbered;
3351
3352
EditorInspectorSection::setup(String(p_array_element_prefix) + "_array", p_label, p_object, p_bg_color, p_foldable, 0);
3353
3354
_setup();
3355
}
3356
3357
void EditorInspectorArray::setup_with_count_property(Object *p_object, const String &p_label, const StringName &p_count_property, const StringName &p_array_element_prefix, int p_page, const Color &p_bg_color, bool p_foldable, bool p_movable, bool p_is_const, bool p_numbered, int p_page_length, const String &p_add_item_text, const String &p_swap_method) {
3358
count_property = p_count_property;
3359
mode = MODE_USE_COUNT_PROPERTY;
3360
array_element_prefix = p_array_element_prefix;
3361
page = p_page;
3362
movable = p_movable;
3363
is_const = p_is_const;
3364
page_length = p_page_length;
3365
numbered = p_numbered;
3366
swap_method = p_swap_method;
3367
3368
add_button->set_text(p_add_item_text);
3369
EditorInspectorSection::setup(String(count_property) + "_array", p_label, p_object, p_bg_color, p_foldable, 0);
3370
3371
_setup();
3372
}
3373
3374
VBoxContainer *EditorInspectorArray::get_vbox(int p_index) {
3375
if (p_index >= begin_array_index && p_index < end_array_index) {
3376
return array_elements[p_index - begin_array_index].vbox;
3377
} else if (p_index < 0) {
3378
return vbox;
3379
} else {
3380
return nullptr;
3381
}
3382
}
3383
3384
EditorInspectorArray::EditorInspectorArray(bool p_read_only) {
3385
read_only = p_read_only;
3386
3387
odd_style.instantiate();
3388
even_style.instantiate();
3389
3390
rmb_popup = memnew(PopupMenu);
3391
rmb_popup->set_accessibility_name(TTRC("Move"));
3392
rmb_popup->add_item(TTR("Move Up"), OPTION_MOVE_UP);
3393
rmb_popup->add_item(TTR("Move Down"), OPTION_MOVE_DOWN);
3394
rmb_popup->add_separator();
3395
rmb_popup->add_item(TTR("Insert New Before"), OPTION_NEW_BEFORE);
3396
rmb_popup->add_item(TTR("Insert New After"), OPTION_NEW_AFTER);
3397
rmb_popup->add_separator();
3398
rmb_popup->add_item(TTR("Remove"), OPTION_REMOVE);
3399
rmb_popup->add_separator();
3400
rmb_popup->add_item(TTR("Clear Array"), OPTION_CLEAR_ARRAY);
3401
rmb_popup->add_item(TTR("Resize Array..."), OPTION_RESIZE_ARRAY);
3402
rmb_popup->connect(SceneStringName(id_pressed), callable_mp(this, &EditorInspectorArray::_rmb_popup_id_pressed));
3403
add_child(rmb_popup);
3404
3405
elements_vbox = memnew(VBoxContainer);
3406
elements_vbox->add_theme_constant_override("separation", 0);
3407
vbox->add_child(elements_vbox);
3408
3409
add_button = memnew(EditorInspectorActionButton(TTRC("Add Element"), SNAME("Add")));
3410
add_button->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_add_button_pressed));
3411
add_button->set_disabled(read_only);
3412
vbox->add_child(add_button);
3413
3414
control_dropping = memnew(Control);
3415
control_dropping->connect(SceneStringName(draw), callable_mp(this, &EditorInspectorArray::_control_dropping_draw));
3416
control_dropping->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
3417
add_child(control_dropping);
3418
3419
resize_dialog = memnew(AcceptDialog);
3420
resize_dialog->set_title(TTRC("Resize Array"));
3421
resize_dialog->add_cancel_button();
3422
resize_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorInspectorArray::_resize_dialog_confirmed));
3423
add_child(resize_dialog);
3424
3425
VBoxContainer *resize_dialog_vbox = memnew(VBoxContainer);
3426
resize_dialog->add_child(resize_dialog_vbox);
3427
3428
new_size_spin_box = memnew(SpinBox);
3429
new_size_spin_box->set_accessibility_name(TTRC("New Size:"));
3430
new_size_spin_box->set_max(16384);
3431
new_size_spin_box->connect(SceneStringName(value_changed), callable_mp(this, &EditorInspectorArray::_new_size_spin_box_value_changed));
3432
new_size_spin_box->get_line_edit()->connect(SceneStringName(text_submitted), callable_mp(this, &EditorInspectorArray::_new_size_spin_box_text_submitted));
3433
new_size_spin_box->set_editable(!read_only);
3434
resize_dialog_vbox->add_margin_child(TTRC("New Size:"), new_size_spin_box);
3435
3436
vbox->connect(SceneStringName(visibility_changed), callable_mp(this, &EditorInspectorArray::_vbox_visibility_changed));
3437
}
3438
3439
////////////////////////////////////////////////
3440
////////////////////////////////////////////////
3441
3442
void EditorPaginator::_first_page_button_pressed() {
3443
emit_signal("page_changed", 0);
3444
}
3445
3446
void EditorPaginator::_prev_page_button_pressed() {
3447
emit_signal("page_changed", MAX(0, page - 1));
3448
}
3449
3450
void EditorPaginator::_page_line_edit_text_submitted(const String &p_text) {
3451
if (p_text.is_valid_int()) {
3452
int new_page = p_text.to_int() - 1;
3453
new_page = MIN(MAX(0, new_page), max_page);
3454
page_line_edit->set_text(Variant(new_page));
3455
emit_signal("page_changed", new_page);
3456
} else {
3457
page_line_edit->set_text(Variant(page));
3458
}
3459
}
3460
3461
void EditorPaginator::_next_page_button_pressed() {
3462
emit_signal("page_changed", MIN(max_page, page + 1));
3463
}
3464
3465
void EditorPaginator::_last_page_button_pressed() {
3466
emit_signal("page_changed", max_page);
3467
}
3468
3469
void EditorPaginator::update(int p_page, int p_max_page) {
3470
page = p_page;
3471
max_page = p_max_page;
3472
3473
// Update buttons.
3474
first_page_button->set_disabled(page == 0);
3475
prev_page_button->set_disabled(page == 0);
3476
next_page_button->set_disabled(page == max_page);
3477
last_page_button->set_disabled(page == max_page);
3478
3479
// Update page number and page count.
3480
page_line_edit->set_text(vformat("%d", page + 1));
3481
page_count_label->set_text(vformat("/ %d", max_page + 1));
3482
}
3483
3484
void EditorPaginator::_notification(int p_what) {
3485
switch (p_what) {
3486
case NOTIFICATION_THEME_CHANGED: {
3487
first_page_button->set_button_icon(get_editor_theme_icon(SNAME("PageFirst")));
3488
prev_page_button->set_button_icon(get_editor_theme_icon(SNAME("PagePrevious")));
3489
next_page_button->set_button_icon(get_editor_theme_icon(SNAME("PageNext")));
3490
last_page_button->set_button_icon(get_editor_theme_icon(SNAME("PageLast")));
3491
} break;
3492
}
3493
}
3494
3495
void EditorPaginator::_bind_methods() {
3496
ADD_SIGNAL(MethodInfo("page_changed", PropertyInfo(Variant::INT, "page")));
3497
}
3498
3499
EditorPaginator::EditorPaginator() {
3500
set_h_size_flags(SIZE_EXPAND_FILL);
3501
set_alignment(ALIGNMENT_CENTER);
3502
3503
first_page_button = memnew(Button);
3504
first_page_button->set_accessibility_name(TTRC("First Page"));
3505
first_page_button->set_flat(true);
3506
first_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_first_page_button_pressed));
3507
add_child(first_page_button);
3508
3509
prev_page_button = memnew(Button);
3510
prev_page_button->set_accessibility_name(TTRC("Previous Page"));
3511
prev_page_button->set_flat(true);
3512
prev_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_prev_page_button_pressed));
3513
add_child(prev_page_button);
3514
3515
page_line_edit = memnew(LineEdit);
3516
page_line_edit->set_accessibility_name(TTRC("Page Number"));
3517
page_line_edit->connect(SceneStringName(text_submitted), callable_mp(this, &EditorPaginator::_page_line_edit_text_submitted));
3518
page_line_edit->add_theme_constant_override("minimum_character_width", 2);
3519
add_child(page_line_edit);
3520
3521
page_count_label = memnew(Label);
3522
page_count_label->set_focus_mode(FOCUS_ACCESSIBILITY);
3523
add_child(page_count_label);
3524
3525
next_page_button = memnew(Button);
3526
prev_page_button->set_accessibility_name(TTRC("Next Page"));
3527
next_page_button->set_flat(true);
3528
next_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_next_page_button_pressed));
3529
add_child(next_page_button);
3530
3531
last_page_button = memnew(Button);
3532
last_page_button->set_accessibility_name(TTRC("Last Page"));
3533
last_page_button->set_flat(true);
3534
last_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_last_page_button_pressed));
3535
add_child(last_page_button);
3536
}
3537
3538
////////////////////////////////////////////////
3539
////////////////////////////////////////////////
3540
3541
Ref<EditorInspectorPlugin> EditorInspector::inspector_plugins[MAX_PLUGINS];
3542
int EditorInspector::inspector_plugin_count = 0;
3543
3544
EditorProperty *EditorInspector::instantiate_property_editor(Object *p_object, const Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, const uint32_t p_usage, const bool p_wide) {
3545
for (int i = inspector_plugin_count - 1; i >= 0; i--) {
3546
if (!inspector_plugins[i]->can_handle(p_object)) {
3547
continue;
3548
}
3549
3550
inspector_plugins[i]->parse_property(p_object, p_type, p_path, p_hint, p_hint_text, p_usage, p_wide);
3551
if (inspector_plugins[i]->added_editors.size()) {
3552
for (List<EditorInspectorPlugin::AddedEditor>::Element *E = inspector_plugins[i]->added_editors.front()->next(); E; E = E->next()) { //only keep first one
3553
memdelete(E->get().property_editor);
3554
}
3555
3556
EditorProperty *prop = Object::cast_to<EditorProperty>(inspector_plugins[i]->added_editors.front()->get().property_editor);
3557
if (prop) {
3558
inspector_plugins[i]->added_editors.clear();
3559
return prop;
3560
} else {
3561
memdelete(inspector_plugins[i]->added_editors.front()->get().property_editor);
3562
inspector_plugins[i]->added_editors.clear();
3563
}
3564
}
3565
}
3566
return nullptr;
3567
}
3568
3569
void EditorInspector::initialize_section_theme(EditorInspectorSection::ThemeCache &p_cache, Control *p_control) {
3570
EditorInspector *parent_inspector = _get_control_parent_inspector(p_control);
3571
if (parent_inspector && parent_inspector != p_control) {
3572
p_cache = parent_inspector->section_theme_cache;
3573
return;
3574
}
3575
3576
p_cache.horizontal_separation = p_control->get_theme_constant(SNAME("h_separation"), SNAME("EditorInspectorSection"));
3577
p_cache.vertical_separation = p_control->get_theme_constant(SNAME("separation"), SNAME("EditorPropertyContainer"));
3578
p_cache.inspector_margin = p_control->get_theme_constant(SNAME("inspector_margin"), EditorStringName(Editor));
3579
p_cache.indent_size = p_control->get_theme_constant(SNAME("indent_size"), SNAME("EditorInspectorSection"));
3580
p_cache.key_padding_size = int(EDITOR_GET("interface/theme/base_spacing")) * 2;
3581
3582
p_cache.warning_color = p_control->get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
3583
p_cache.prop_subsection = p_control->get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor));
3584
p_cache.font_color = p_control->get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
3585
p_cache.font_disabled_color = p_control->get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor));
3586
p_cache.font_hover_color = p_control->get_theme_color(SNAME("font_hover_color"), EditorStringName(Editor));
3587
p_cache.font_pressed_color = p_control->get_theme_color(SNAME("font_pressed_color"), EditorStringName(Editor));
3588
p_cache.font_hover_pressed_color = p_control->get_theme_color(SNAME("font_hover_pressed_color"), EditorStringName(Editor));
3589
p_cache.font_hover_mono_color = p_control->get_theme_color(SNAME("mono_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.925);
3590
3591
p_cache.font = p_control->get_theme_font(SceneStringName(font), SNAME("Tree"));
3592
p_cache.font_size = p_control->get_theme_font_size(SceneStringName(font_size), SNAME("Tree"));
3593
p_cache.bold_font = p_control->get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
3594
p_cache.bold_font_size = p_control->get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
3595
p_cache.light_font = p_control->get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
3596
p_cache.light_font_size = p_control->get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
3597
3598
p_cache.arrow = p_control->get_theme_icon(SNAME("arrow"), SNAME("Tree"));
3599
p_cache.arrow_collapsed = p_control->get_theme_icon(SNAME("arrow_collapsed"), SNAME("Tree"));
3600
p_cache.arrow_collapsed_mirrored = p_control->get_theme_icon(SNAME("arrow_collapsed_mirrored"), SNAME("Tree"));
3601
p_cache.icon_gui_checked = p_control->get_editor_theme_icon(SNAME("GuiChecked"));
3602
p_cache.icon_gui_unchecked = p_control->get_editor_theme_icon(SNAME("GuiUnchecked"));
3603
p_cache.icon_gui_animation_key = p_control->get_editor_theme_icon(SNAME("Key"));
3604
3605
p_cache.indent_box = p_control->get_theme_stylebox(SNAME("indent_box"), SNAME("EditorInspectorSection"));
3606
p_cache.key_hover = p_control->get_theme_stylebox(SceneStringName(hover), SceneStringName(FlatButton));
3607
}
3608
3609
void EditorInspector::initialize_category_theme(EditorInspectorCategory::ThemeCache &p_cache, Control *p_control) {
3610
EditorInspector *parent_inspector = _get_control_parent_inspector(p_control);
3611
if (parent_inspector && parent_inspector != p_control) {
3612
p_cache = parent_inspector->category_theme_cache;
3613
return;
3614
}
3615
3616
p_cache.horizontal_separation = p_control->get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
3617
p_cache.vertical_separation = p_control->get_theme_constant(SNAME("separation"), SNAME("EditorPropertyContainer"));
3618
p_cache.class_icon_size = p_control->get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
3619
3620
p_cache.font_color = p_control->get_theme_color(SceneStringName(font_color), SNAME("Tree"));
3621
3622
p_cache.bold_font = p_control->get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
3623
p_cache.bold_font_size = p_control->get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
3624
3625
p_cache.icon_favorites = p_control->get_editor_theme_icon(SNAME("Favorites"));
3626
p_cache.icon_unfavorite = p_control->get_editor_theme_icon(SNAME("Unfavorite"));
3627
p_cache.icon_help = p_control->get_editor_theme_icon(SNAME("Help"));
3628
3629
p_cache.background = p_control->get_theme_stylebox(SNAME("bg"), SNAME("EditorInspectorCategory"));
3630
}
3631
3632
void EditorInspector::initialize_property_theme(EditorProperty::ThemeCache &p_cache, Control *p_control) {
3633
EditorInspector *parent_inspector = _get_control_parent_inspector(p_control);
3634
if (parent_inspector && parent_inspector != p_control) {
3635
p_cache = parent_inspector->property_theme_cache;
3636
return;
3637
}
3638
3639
p_cache.font = p_control->get_theme_font(SceneStringName(font), SNAME("Tree"));
3640
3641
p_cache.background = p_control->get_theme_stylebox(SNAME("bg"), SNAME("EditorProperty"));
3642
p_cache.background_selected = p_control->get_theme_stylebox(SNAME("bg_selected"), SNAME("EditorProperty"));
3643
p_cache.child_background = p_control->get_theme_stylebox(SNAME("child_bg"), SNAME("EditorProperty"));
3644
p_cache.hover = p_control->get_theme_stylebox(SceneStringName(hover), SceneStringName(FlatButton));
3645
3646
p_cache.key_icon = p_control->get_editor_theme_icon(SNAME("Key"));
3647
p_cache.key_next_icon = p_control->get_editor_theme_icon(SNAME("KeyNext"));
3648
p_cache.delete_icon = p_control->get_editor_theme_icon(SNAME("Close"));
3649
p_cache.checked_icon = p_control->get_editor_theme_icon(SNAME("GuiChecked"));
3650
p_cache.unchecked_icon = p_control->get_editor_theme_icon(SNAME("GuiUnchecked"));
3651
p_cache.revert_icon = p_control->get_editor_theme_icon(SNAME("ReloadSmall"));
3652
p_cache.pin_icon = p_control->get_editor_theme_icon(SNAME("Pin"));
3653
p_cache.copy_icon = p_control->get_editor_theme_icon(SNAME("ActionCopy"));
3654
p_cache.copy_node_path_icon = p_control->get_editor_theme_icon(SNAME("CopyNodePath"));
3655
p_cache.paste_icon = p_control->get_editor_theme_icon(SNAME("ActionPaste"));
3656
p_cache.unfavorite_icon = p_control->get_editor_theme_icon(SNAME("Unfavorite"));
3657
p_cache.favorite_icon = p_control->get_editor_theme_icon(SNAME("Favorites"));
3658
p_cache.override_icon = p_control->get_editor_theme_icon(SNAME("Override"));
3659
p_cache.remove_icon = p_control->get_editor_theme_icon(SNAME("Remove"));
3660
p_cache.help_icon = p_control->get_editor_theme_icon(SNAME("Help"));
3661
3662
p_cache.font_size = p_control->get_theme_font_size(SceneStringName(font_size), SNAME("Tree"));
3663
p_cache.font_offset = p_control->get_theme_constant(SNAME("font_offset"), SNAME("EditorProperty"));
3664
p_cache.horizontal_separation = p_control->get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
3665
p_cache.vertical_separation = p_control->get_theme_constant(SNAME("separation"), SNAME("EditorPropertyContainer"));
3666
p_cache.padding = int(EDITOR_GET("interface/theme/base_spacing")) * 2;
3667
p_cache.inspector_property_height = p_control->get_theme_constant(SNAME("inspector_property_height"), EditorStringName(Editor));
3668
3669
p_cache.property_color = p_control->get_theme_color(SNAME("property_color"), SNAME("EditorProperty"));
3670
p_cache.readonly_property_color = p_control->get_theme_color(SNAME("readonly_color"), SNAME("EditorProperty"));
3671
p_cache.warning_color = p_control->get_theme_color(SNAME("warning_color"), SNAME("EditorProperty"));
3672
p_cache.readonly_warning_color = p_control->get_theme_color(SNAME("readonly_warning_color"), SNAME("EditorProperty"));
3673
p_cache.property_color_x = p_control->get_theme_color(SNAME("property_color_x"), EditorStringName(Editor));
3674
p_cache.property_color_y = p_control->get_theme_color(SNAME("property_color_y"), EditorStringName(Editor));
3675
p_cache.property_color_z = p_control->get_theme_color(SNAME("property_color_z"), EditorStringName(Editor));
3676
p_cache.property_color_w = p_control->get_theme_color(SNAME("property_color_w"), EditorStringName(Editor));
3677
p_cache.sub_inspector_property_color = p_control->get_theme_color(SNAME("sub_inspector_property_color"), EditorStringName(EditorStyles));
3678
3679
if (p_control == parent_inspector) {
3680
// Only initialize for the inspector, as stand-alone properties won't need it.
3681
for (int i = 0; i <= 16; i++) {
3682
p_cache.sub_inspector_background[i] = p_control->get_theme_stylebox("sub_inspector_property_bg" + itos(i), EditorStringName(EditorStyles));
3683
}
3684
}
3685
}
3686
3687
void EditorInspector::add_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
3688
ERR_FAIL_COND(inspector_plugin_count == MAX_PLUGINS);
3689
3690
for (int i = 0; i < inspector_plugin_count; i++) {
3691
if (inspector_plugins[i] == p_plugin) {
3692
return; //already exists
3693
}
3694
}
3695
inspector_plugins[inspector_plugin_count++] = p_plugin;
3696
}
3697
3698
void EditorInspector::remove_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
3699
int idx = -1;
3700
for (int i = 0; i < inspector_plugin_count; i++) {
3701
if (inspector_plugins[i] == p_plugin) {
3702
idx = i;
3703
break;
3704
}
3705
}
3706
3707
ERR_FAIL_COND_MSG(idx == -1, "Trying to remove nonexistent inspector plugin.");
3708
for (int i = idx; i < inspector_plugin_count - 1; i++) {
3709
inspector_plugins[i] = inspector_plugins[i + 1];
3710
}
3711
inspector_plugins[inspector_plugin_count - 1] = Ref<EditorInspectorPlugin>();
3712
3713
inspector_plugin_count--;
3714
}
3715
3716
void EditorInspector::cleanup_plugins() {
3717
for (int i = 0; i < inspector_plugin_count; i++) {
3718
inspector_plugins[i].unref();
3719
}
3720
inspector_plugin_count = 0;
3721
}
3722
3723
bool EditorInspector::is_main_editor_inspector() const {
3724
return InspectorDock::get_singleton() && InspectorDock::get_inspector_singleton() == this;
3725
}
3726
3727
String EditorInspector::get_selected_path() const {
3728
return property_selected;
3729
}
3730
3731
void EditorInspector::_parse_added_editors(VBoxContainer *p_current_vbox, EditorInspectorSection *p_section, Ref<EditorInspectorPlugin> p_plugin) {
3732
for (const EditorInspectorPlugin::AddedEditor &F : p_plugin->added_editors) {
3733
EditorProperty *ep = Object::cast_to<EditorProperty>(F.property_editor);
3734
3735
if (ep && !F.properties.is_empty() && current_favorites.has(F.properties[0])) {
3736
ep->favorited = true;
3737
favorites_vbox->add_child(F.property_editor);
3738
} else {
3739
p_current_vbox->add_child(F.property_editor);
3740
}
3741
3742
if (ep) {
3743
ep->object = object;
3744
ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed).bind(false));
3745
ep->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
3746
ep->connect("property_deleted", callable_mp(this, &EditorInspector::_property_deleted), CONNECT_DEFERRED);
3747
ep->connect("property_keyed_with_value", callable_mp(this, &EditorInspector::_property_keyed_with_value));
3748
ep->connect("property_checked", callable_mp(this, &EditorInspector::_property_checked));
3749
ep->connect("property_favorited", callable_mp(this, &EditorInspector::_set_property_favorited), CONNECT_DEFERRED);
3750
ep->connect("property_pinned", callable_mp(this, &EditorInspector::_property_pinned));
3751
ep->connect("selected", callable_mp(this, &EditorInspector::_property_selected));
3752
ep->connect("multiple_properties_changed", callable_mp(this, &EditorInspector::_multiple_properties_changed));
3753
ep->connect("resource_selected", callable_mp(get_root_inspector(), &EditorInspector::_resource_selected), CONNECT_DEFERRED);
3754
ep->connect("object_id_selected", callable_mp(this, &EditorInspector::_object_id_selected), CONNECT_DEFERRED);
3755
3756
if (F.properties.size()) {
3757
if (F.properties.size() == 1) {
3758
//since it's one, associate:
3759
ep->property = F.properties[0];
3760
ep->property_path = property_prefix + F.properties[0];
3761
ep->property_usage = 0;
3762
}
3763
3764
if (!F.label.is_empty()) {
3765
ep->set_label(F.label);
3766
}
3767
3768
for (int i = 0; i < F.properties.size(); i++) {
3769
String prop = F.properties[i];
3770
3771
if (!editor_property_map.has(prop)) {
3772
editor_property_map[prop] = List<EditorProperty *>();
3773
}
3774
editor_property_map[prop].push_back(ep);
3775
}
3776
}
3777
3778
Node *section_search = p_section;
3779
while (section_search) {
3780
EditorInspectorSection *section = Object::cast_to<EditorInspectorSection>(section_search);
3781
if (section) {
3782
ep->connect("property_can_revert_changed", callable_mp(section, &EditorInspectorSection::property_can_revert_changed));
3783
}
3784
section_search = section_search->get_parent();
3785
if (Object::cast_to<EditorInspector>(section_search)) {
3786
// Skip sub-resource inspectors.
3787
break;
3788
}
3789
}
3790
3791
ep->set_read_only(read_only);
3792
ep->update_property();
3793
ep->_update_flags();
3794
ep->update_editor_property_status();
3795
ep->set_deletable(deletable_properties);
3796
ep->update_cache();
3797
}
3798
}
3799
p_plugin->added_editors.clear();
3800
}
3801
3802
bool EditorInspector::_is_property_disabled_by_feature_profile(const StringName &p_property) {
3803
if (!EditorFeatureProfileManager::get_singleton()) {
3804
return false;
3805
}
3806
3807
Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
3808
if (profile.is_null()) {
3809
return false;
3810
}
3811
3812
StringName class_name = object->get_class();
3813
3814
while (class_name != StringName()) {
3815
if (profile->is_class_property_disabled(class_name, p_property)) {
3816
return true;
3817
}
3818
if (profile->is_class_disabled(class_name)) {
3819
//won't see properties of a disabled class
3820
return true;
3821
}
3822
class_name = ClassDB::get_parent_class(class_name);
3823
}
3824
3825
return false;
3826
}
3827
3828
void EditorInspector::_add_section_in_tree(EditorInspectorSection *p_section, VBoxContainer *p_current_vbox) {
3829
// Place adjacent sections in their own vbox with a theme-specific separation.
3830
VBoxContainer *container = nullptr;
3831
if (p_current_vbox->get_child_count() > 0) {
3832
Node *last_child = p_current_vbox->get_child(-1);
3833
container = Object::cast_to<VBoxContainer>(last_child);
3834
}
3835
if (!container) {
3836
container = memnew(VBoxContainer);
3837
container->set_theme_type_variation(SNAME("EditorSectionContainer"));
3838
p_current_vbox->add_child(container);
3839
}
3840
container->add_child(p_section);
3841
}
3842
3843
void EditorInspector::update_tree() {
3844
if (!object) {
3845
return;
3846
}
3847
3848
bool root_inspector_was_following_focus = get_root_inspector()->is_following_focus();
3849
if (root_inspector_was_following_focus) {
3850
// Temporarily disable focus following on the root inspector to avoid jumping while the inspector is updating.
3851
get_root_inspector()->set_follow_focus(false);
3852
}
3853
3854
// Store currently selected and focused elements to restore after the update.
3855
// TODO: Can be useful to store more context for the focusable, such as the caret position in LineEdit.
3856
StringName current_selected = property_selected;
3857
int current_focusable = -1;
3858
3859
if (property_focusable != -1) {
3860
// Check that focusable is actually focusable.
3861
bool restore_focus = false;
3862
Control *focused = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
3863
if (focused) {
3864
Node *parent = focused->get_parent();
3865
while (parent) {
3866
EditorInspector *inspector = Object::cast_to<EditorInspector>(parent);
3867
if (inspector) {
3868
restore_focus = inspector == this; // May be owned by another inspector.
3869
break; // Exit after the first inspector is found, since there may be nested ones.
3870
}
3871
parent = parent->get_parent();
3872
}
3873
}
3874
3875
if (restore_focus) {
3876
current_focusable = property_focusable;
3877
}
3878
}
3879
3880
// The call here is for the edited object that has not changed, but the tree needs to be updated (for example, the object's property list has been modified).
3881
// Since the edited object has not changed, there is no need to hide the plugin at this time.
3882
_clear(false);
3883
3884
List<Ref<EditorInspectorPlugin>> valid_plugins;
3885
3886
for (int i = inspector_plugin_count - 1; i >= 0; i--) { //start by last, so lastly added can override newly added
3887
if (!inspector_plugins[i]->can_handle(object)) {
3888
continue;
3889
}
3890
valid_plugins.push_back(inspector_plugins[i]);
3891
}
3892
3893
// Decide if properties should be drawn with the warning color (yellow),
3894
// or if the whole object should be considered read-only.
3895
bool draw_warning = false;
3896
bool all_read_only = false;
3897
if (is_inside_tree() && EditorNode::get_singleton()) {
3898
if (object->has_method("_is_read_only")) {
3899
all_read_only = object->call("_is_read_only");
3900
}
3901
3902
Node *nod = Object::cast_to<Node>(object);
3903
Node *es = EditorNode::get_singleton()->get_edited_scene();
3904
if (nod && es != nod && nod->get_owner() != es) {
3905
// Draw in warning color edited nodes that are not in the currently edited scene,
3906
// as changes may be lost in the future.
3907
draw_warning = true;
3908
} else {
3909
if (!all_read_only) {
3910
Resource *res = Object::cast_to<Resource>(object);
3911
if (res) {
3912
all_read_only = EditorNode::get_singleton()->is_resource_read_only(res);
3913
}
3914
}
3915
}
3916
}
3917
3918
String filter = search_box ? search_box->get_text() : "";
3919
String group;
3920
String group_base;
3921
EditorInspectorSection *group_togglable_property = nullptr;
3922
String subgroup;
3923
String subgroup_base;
3924
EditorInspectorSection *subgroup_togglable_property = nullptr;
3925
int section_depth = 0;
3926
bool disable_favorite = false;
3927
VBoxContainer *category_vbox = nullptr;
3928
3929
List<PropertyInfo> plist;
3930
object->get_property_list(&plist, true);
3931
3932
HashMap<VBoxContainer *, HashMap<String, VBoxContainer *>> vbox_per_path;
3933
HashMap<String, EditorInspectorArray *> editor_inspector_array_per_prefix;
3934
HashMap<String, HashMap<String, LocalVector<EditorProperty *>>> favorites_to_add;
3935
HashMap<String, EditorInspectorSection *> togglable_editor_inspector_sections;
3936
3937
const Color sscolor = theme_cache.prop_subsection;
3938
bool sub_inspectors_enabled = EDITOR_GET("interface/inspector/open_resources_in_current_inspector");
3939
3940
if (!valid_plugins.is_empty()) {
3941
for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
3942
ped->parse_begin(object);
3943
_parse_added_editors(begin_vbox, nullptr, ped);
3944
}
3945
3946
// Show if any of the editors were added to the beginning.
3947
if (begin_vbox->get_child_count() > 0) {
3948
begin_vbox->show();
3949
}
3950
}
3951
3952
StringName doc_name;
3953
3954
// Get the lists of editors for properties.
3955
for (List<PropertyInfo>::Element *E_property = plist.front(); E_property; E_property = E_property->next()) {
3956
PropertyInfo &p = E_property->get();
3957
3958
if (p.usage & PROPERTY_USAGE_SUBGROUP) {
3959
// Setup a property sub-group.
3960
subgroup = p.name;
3961
subgroup_togglable_property = nullptr;
3962
3963
subgroup_base = p.hint_string.get_slicec(',', 0);
3964
if (p.hint_string.get_slice_count(",") > 1) {
3965
section_depth = p.hint_string.get_slicec(',', 1).to_int();
3966
} else {
3967
section_depth = 0;
3968
}
3969
3970
continue;
3971
3972
} else if (p.usage & PROPERTY_USAGE_GROUP) {
3973
// Setup a property group.
3974
group = p.name;
3975
group_togglable_property = nullptr;
3976
3977
group_base = p.hint_string.get_slicec(',', 0);
3978
if (p.hint_string.get_slice_count(",") > 1) {
3979
section_depth = p.hint_string.get_slicec(',', 1).to_int();
3980
} else {
3981
section_depth = 0;
3982
}
3983
3984
subgroup = "";
3985
subgroup_base = "";
3986
subgroup_togglable_property = nullptr;
3987
3988
continue;
3989
3990
} else if (p.usage & PROPERTY_USAGE_CATEGORY) {
3991
// Setup a property category.
3992
group = "";
3993
group_base = "";
3994
group_togglable_property = nullptr;
3995
subgroup = "";
3996
subgroup_base = "";
3997
subgroup_togglable_property = nullptr;
3998
section_depth = 0;
3999
disable_favorite = false;
4000
4001
vbox_per_path.clear();
4002
editor_inspector_array_per_prefix.clear();
4003
4004
// `hint_script` should contain a native class name or a script path.
4005
// Otherwise the category was probably added via `@export_category` or `_get_property_list()`.
4006
const bool is_custom_category = p.hint_string.is_empty();
4007
4008
// Iterate over remaining properties. If no properties in category, skip the category.
4009
List<PropertyInfo>::Element *N = E_property->next();
4010
bool valid = true;
4011
while (N) {
4012
if (!N->get().name.begins_with("metadata/_") && N->get().usage & PROPERTY_USAGE_EDITOR &&
4013
(!filter.is_empty() || !restrict_to_basic || (N->get().usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
4014
break;
4015
}
4016
// Treat custom categories as second-level ones. Do not skip a normal category if it is followed by a custom one.
4017
// Skip in the other 3 cases (normal -> normal, custom -> custom, custom -> normal).
4018
if ((N->get().usage & PROPERTY_USAGE_CATEGORY) && (is_custom_category || !N->get().hint_string.is_empty())) {
4019
valid = false;
4020
break;
4021
}
4022
N = N->next();
4023
}
4024
if (!valid) {
4025
continue; // Empty, ignore it.
4026
}
4027
4028
String category_tooltip;
4029
4030
// Do not add an icon, do not change the current class (`doc_name`) for custom categories.
4031
if (is_custom_category) {
4032
category_tooltip = p.name;
4033
} else {
4034
doc_name = p.name;
4035
4036
// Use category's owner script to update some of its information.
4037
if (!EditorNode::get_editor_data().is_type_recognized(p.name) && ResourceLoader::exists(p.hint_string, "Script")) {
4038
Ref<Script> scr = ResourceLoader::load(p.hint_string, "Script");
4039
if (scr.is_valid()) {
4040
doc_name = scr->get_doc_class_name();
4041
4042
// Property favorites aren't compatible with built-in scripts.
4043
if (scr->is_built_in()) {
4044
disable_favorite = true;
4045
}
4046
}
4047
}
4048
4049
if (use_doc_hints) {
4050
// `|` separators used in `EditorHelpBit`.
4051
category_tooltip = "class|" + doc_name + "|";
4052
}
4053
}
4054
4055
if ((is_custom_category && !show_custom_categories) || (!is_custom_category && !show_standard_categories)) {
4056
continue;
4057
}
4058
4059
// Hide the "MultiNodeEdit" category for MultiNodeEdit.
4060
if (Object::cast_to<MultiNodeEdit>(object) && p.name == "MultiNodeEdit") {
4061
continue;
4062
}
4063
4064
// Create an EditorInspectorCategory and add it to the inspector.
4065
EditorInspectorCategory *category = memnew(EditorInspectorCategory);
4066
category->set_property_info(p);
4067
main_vbox->add_child(category);
4068
category_vbox = nullptr; // Reset.
4069
4070
// Set the category info.
4071
category->set_tooltip_text(category_tooltip);
4072
if (!is_custom_category) {
4073
category->set_doc_class_name(doc_name);
4074
}
4075
4076
// Add editors at the start of a category.
4077
for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
4078
ped->parse_category(object, p.name);
4079
_parse_added_editors(main_vbox, nullptr, ped);
4080
}
4081
4082
continue;
4083
4084
} else if (p.name.begins_with("metadata/_") || !(p.usage & PROPERTY_USAGE_EDITOR) || _is_property_disabled_by_feature_profile(p.name) ||
4085
(filter.is_empty() && restrict_to_basic && !(p.usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
4086
// Ignore properties that are not supposed to be in the inspector.
4087
continue;
4088
}
4089
4090
if (p.name == "script") {
4091
// Script should go into its own category.
4092
category_vbox = nullptr;
4093
}
4094
4095
if (p.usage & PROPERTY_USAGE_HIGH_END_GFX && RS::get_singleton()->is_low_end()) {
4096
// Do not show this property in low end gfx.
4097
continue;
4098
}
4099
4100
if (p.name == "script" && (hide_script || bool(object->call("_hide_script_from_inspector")))) {
4101
// Hide script variables from inspector if required.
4102
continue;
4103
}
4104
4105
if (p.name.begins_with("metadata/") && bool(object->call(SNAME("_hide_metadata_from_inspector")))) {
4106
// Hide metadata from inspector if required.
4107
continue;
4108
}
4109
4110
// Get the path for property.
4111
String path = p.name;
4112
4113
// First check if we have an array that fits the prefix.
4114
String array_prefix = "";
4115
int array_index = -1;
4116
for (KeyValue<String, EditorInspectorArray *> &E : editor_inspector_array_per_prefix) {
4117
if (p.name.begins_with(E.key) && E.key.length() > array_prefix.length()) {
4118
array_prefix = E.key;
4119
}
4120
}
4121
4122
if (!array_prefix.is_empty()) {
4123
// If we have an array element, find the according index in array.
4124
String str = p.name.trim_prefix(array_prefix);
4125
int to_char_index = 0;
4126
while (to_char_index < str.length()) {
4127
if (!is_digit(str[to_char_index])) {
4128
break;
4129
}
4130
to_char_index++;
4131
}
4132
if (to_char_index > 0) {
4133
array_index = str.left(to_char_index).to_int();
4134
} else {
4135
array_prefix = "";
4136
}
4137
}
4138
4139
// Don't allow to favorite array items.
4140
if (!disable_favorite) {
4141
disable_favorite = !array_prefix.is_empty();
4142
}
4143
4144
if (!array_prefix.is_empty()) {
4145
path = path.trim_prefix(array_prefix);
4146
int char_index = path.find_char('/');
4147
if (char_index >= 0) {
4148
path = path.right(-char_index - 1);
4149
} else {
4150
path = vformat(TTR("Element %s"), array_index);
4151
}
4152
} else {
4153
// Check if we exit or not a subgroup. If there is a prefix, remove it from the property label string.
4154
if (!subgroup.is_empty() && !subgroup_base.is_empty()) {
4155
if (path.begins_with(subgroup_base)) {
4156
path = path.trim_prefix(subgroup_base);
4157
} else if (subgroup_base.begins_with(path)) {
4158
// Keep it, this is used pretty often.
4159
} else {
4160
subgroup = ""; // The prefix changed, we are no longer in the subgroup.
4161
subgroup_togglable_property = nullptr;
4162
}
4163
}
4164
4165
// Check if we exit or not a group. If there is a prefix, remove it from the property label string.
4166
if (!group.is_empty() && !group_base.is_empty() && subgroup.is_empty()) {
4167
if (path.begins_with(group_base)) {
4168
path = path.trim_prefix(group_base);
4169
} else if (group_base.begins_with(path)) {
4170
// Keep it, this is used pretty often.
4171
} else {
4172
group = ""; // The prefix changed, we are no longer in the group.
4173
group_togglable_property = nullptr;
4174
subgroup = "";
4175
subgroup_togglable_property = nullptr;
4176
}
4177
}
4178
4179
// Add the group and subgroup to the path.
4180
if (!subgroup.is_empty()) {
4181
path = subgroup + "/" + path;
4182
}
4183
if (!group.is_empty()) {
4184
path = group + "/" + path;
4185
}
4186
}
4187
4188
// Get the property label's string.
4189
String name_override = (path.contains_char('/')) ? path.substr(path.rfind_char('/') + 1) : path;
4190
String feature_tag;
4191
{
4192
const int dot = name_override.find_char('.');
4193
if (dot != -1) {
4194
feature_tag = name_override.substr(dot);
4195
name_override = name_override.substr(0, dot);
4196
}
4197
}
4198
name_override = name_override.uri_decode();
4199
4200
// Don't localize script variables.
4201
EditorPropertyNameProcessor::Style name_style = property_name_style;
4202
if ((p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) && name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
4203
name_style = EditorPropertyNameProcessor::STYLE_CAPITALIZED;
4204
}
4205
const String property_label_string = EditorPropertyNameProcessor::get_singleton()->process_name(name_override, name_style, p.name, doc_name) + feature_tag;
4206
4207
// Remove the property from the path.
4208
int idx = path.rfind_char('/');
4209
if (idx > -1) {
4210
path = path.left(idx);
4211
} else {
4212
path = "";
4213
}
4214
4215
// Ignore properties that do not fit the filter.
4216
bool sub_inspector_use_filter = false;
4217
if (use_filter && !filter.is_empty()) {
4218
const String property_path = property_prefix + (path.is_empty() ? "" : path + "/") + name_override;
4219
if (!_property_path_matches(property_path, filter, property_name_style)) {
4220
if (!sub_inspectors_enabled || p.hint != PROPERTY_HINT_RESOURCE_TYPE) {
4221
continue;
4222
}
4223
4224
Ref<Resource> res = object->get(p.name);
4225
if (res.is_null()) {
4226
continue;
4227
}
4228
4229
// Check if the sub-resource has any properties that match the filter.
4230
if (!_resource_properties_matches(res, filter)) {
4231
continue;
4232
}
4233
4234
sub_inspector_use_filter = true;
4235
}
4236
}
4237
4238
// Recreate the category vbox if it was reset.
4239
if (category_vbox == nullptr) {
4240
category_vbox = memnew(VBoxContainer);
4241
category_vbox->set_theme_type_variation(SNAME("EditorPropertyContainer"));
4242
category_vbox->hide();
4243
main_vbox->add_child(category_vbox);
4244
}
4245
4246
// Find the correct section/vbox to add the property editor to.
4247
VBoxContainer *root_vbox = array_prefix.is_empty() ? category_vbox : editor_inspector_array_per_prefix[array_prefix]->get_vbox(array_index);
4248
if (!root_vbox) {
4249
continue;
4250
}
4251
category_vbox->show();
4252
4253
if (!vbox_per_path.has(root_vbox)) {
4254
vbox_per_path[root_vbox] = HashMap<String, VBoxContainer *>();
4255
vbox_per_path[root_vbox][""] = root_vbox;
4256
}
4257
4258
VBoxContainer *current_vbox = root_vbox;
4259
String acc_path = "";
4260
int level = 1;
4261
4262
Vector<String> components = path.split("/");
4263
for (int i = 0; i < components.size(); i++) {
4264
const String &component = components[i];
4265
acc_path += (i > 0) ? "/" + component : component;
4266
4267
if (!vbox_per_path[root_vbox].has(acc_path)) {
4268
// If the section does not exists, create it.
4269
EditorInspectorSection *section = memnew(EditorInspectorSection);
4270
get_root_inspector()->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(section, &EditorInspectorSection::reset_timer).unbind(1));
4271
_add_section_in_tree(section, current_vbox);
4272
sections.push_back(section);
4273
4274
String label;
4275
String tooltip;
4276
4277
// Don't localize groups for script variables.
4278
EditorPropertyNameProcessor::Style section_name_style = property_name_style;
4279
if ((p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) && section_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
4280
section_name_style = EditorPropertyNameProcessor::STYLE_CAPITALIZED;
4281
}
4282
4283
// Only process group label if this is not the group or subgroup.
4284
if ((i == 0 && component == group) || (i == 1 && component == subgroup)) {
4285
if (section_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
4286
label = EditorPropertyNameProcessor::get_singleton()->translate_group_name(component);
4287
tooltip = component;
4288
} else {
4289
label = component;
4290
tooltip = EditorPropertyNameProcessor::get_singleton()->translate_group_name(component);
4291
}
4292
} else {
4293
label = EditorPropertyNameProcessor::get_singleton()->process_name(component, section_name_style, p.name, doc_name);
4294
tooltip = EditorPropertyNameProcessor::get_singleton()->process_name(component, EditorPropertyNameProcessor::get_tooltip_style(section_name_style), p.name, doc_name);
4295
}
4296
4297
Color c = sscolor;
4298
c.a /= level;
4299
section->setup(acc_path, label, object, c, use_folding, section_depth, level);
4300
section->set_tooltip_text(tooltip);
4301
4302
section->connect("section_toggled_by_user", callable_mp(this, &EditorInspector::_section_toggled_by_user));
4303
section->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
4304
4305
// Add editors at the start of a group.
4306
for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
4307
ped->parse_group(object, path);
4308
_parse_added_editors(section->get_vbox(), section, ped);
4309
}
4310
4311
vbox_per_path[root_vbox][acc_path] = section->get_vbox();
4312
}
4313
4314
current_vbox = vbox_per_path[root_vbox][acc_path];
4315
level = (MIN(level + 1, 4));
4316
}
4317
4318
// If we did not find a section to add the property to, add it to the category vbox instead (the category vbox handles margins correctly).
4319
if (current_vbox == main_vbox) {
4320
category_vbox->show();
4321
current_vbox = category_vbox;
4322
}
4323
4324
// Check if the property is an array counter, if so create a dedicated array editor for the array.
4325
if (p.usage & PROPERTY_USAGE_ARRAY) {
4326
EditorInspectorArray *editor_inspector_array = nullptr;
4327
StringName array_element_prefix;
4328
Color c = sscolor;
4329
c.a /= level;
4330
4331
Vector<String> class_name_components = String(p.class_name).split(",");
4332
4333
int page_size = 5;
4334
bool movable = true;
4335
bool is_const = false;
4336
bool numbered = false;
4337
bool foldable = use_folding;
4338
String add_button_text = TTRC("Add Element");
4339
String swap_method;
4340
for (int i = (p.type == Variant::NIL ? 1 : 2); i < class_name_components.size(); i++) {
4341
if (class_name_components[i].begins_with("page_size") && class_name_components[i].get_slice_count("=") == 2) {
4342
page_size = class_name_components[i].get_slicec('=', 1).to_int();
4343
} else if (class_name_components[i].begins_with("add_button_text") && class_name_components[i].get_slice_count("=") == 2) {
4344
add_button_text = class_name_components[i].get_slicec('=', 1).strip_edges();
4345
} else if (class_name_components[i] == "static") {
4346
movable = false;
4347
} else if (class_name_components[i] == "const") {
4348
is_const = true;
4349
} else if (class_name_components[i] == "numbered") {
4350
numbered = true;
4351
} else if (class_name_components[i] == "unfoldable") {
4352
foldable = false;
4353
} else if (class_name_components[i].begins_with("swap_method") && class_name_components[i].get_slice_count("=") == 2) {
4354
swap_method = class_name_components[i].get_slicec('=', 1).strip_edges();
4355
}
4356
}
4357
4358
if (p.type == Variant::NIL) {
4359
// Setup the array to use a method to create/move/delete elements.
4360
array_element_prefix = class_name_components[0];
4361
editor_inspector_array = memnew(EditorInspectorArray(all_read_only));
4362
4363
String array_label = path.contains_char('/') ? path.substr(path.rfind_char('/') + 1) : path;
4364
array_label = EditorPropertyNameProcessor::get_singleton()->process_name(property_label_string, property_name_style, p.name, doc_name);
4365
int page = per_array_page.has(array_element_prefix) ? per_array_page[array_element_prefix] : 0;
4366
editor_inspector_array->setup_with_move_element_function(object, array_label, array_element_prefix, page, c, use_folding);
4367
editor_inspector_array->connect("page_change_request", callable_mp(this, &EditorInspector::_page_change_request).bind(array_element_prefix));
4368
} else if (p.type == Variant::INT) {
4369
// Setup the array to use the count property and built-in functions to create/move/delete elements.
4370
if (class_name_components.size() >= 2) {
4371
array_element_prefix = class_name_components[1];
4372
editor_inspector_array = memnew(EditorInspectorArray(all_read_only));
4373
int page = per_array_page.has(array_element_prefix) ? per_array_page[array_element_prefix] : 0;
4374
4375
editor_inspector_array->setup_with_count_property(object, class_name_components[0], p.name, array_element_prefix, page, c, foldable, movable, is_const, numbered, page_size, add_button_text, swap_method);
4376
editor_inspector_array->connect("page_change_request", callable_mp(this, &EditorInspector::_page_change_request).bind(array_element_prefix));
4377
}
4378
}
4379
4380
if (editor_inspector_array) {
4381
_add_section_in_tree(editor_inspector_array, current_vbox);
4382
editor_inspector_array_per_prefix[array_element_prefix] = editor_inspector_array;
4383
}
4384
4385
continue;
4386
}
4387
4388
// Checkable and checked properties.
4389
bool checkable = false;
4390
bool checked = false;
4391
if (p.usage & PROPERTY_USAGE_CHECKABLE) {
4392
checkable = true;
4393
checked = p.usage & PROPERTY_USAGE_CHECKED;
4394
}
4395
4396
bool property_read_only = (p.usage & PROPERTY_USAGE_READ_ONLY) || read_only;
4397
4398
// Mark properties that would require an editor restart (mostly when editing editor settings).
4399
if (p.usage & PROPERTY_USAGE_RESTART_IF_CHANGED) {
4400
restart_request_props.insert(p.name);
4401
}
4402
4403
String doc_path;
4404
String theme_item_name;
4405
String doc_tooltip_text;
4406
4407
// Build the doc hint, to use as tooltip.
4408
if (use_doc_hints) {
4409
StringName classname = doc_name;
4410
if (!object_class.is_empty()) {
4411
classname = object_class;
4412
} else if (Object::cast_to<MultiNodeEdit>(object)) {
4413
classname = Object::cast_to<MultiNodeEdit>(object)->get_edited_class_name();
4414
} else if (classname == "") {
4415
classname = object->get_class_name();
4416
Resource *res = Object::cast_to<Resource>(object);
4417
if (res && !res->get_script().is_null()) {
4418
// Grab the script of this resource to get the evaluated script class.
4419
Ref<Script> scr = res->get_script();
4420
if (scr.is_valid()) {
4421
Vector<DocData::ClassDoc> docs = scr->get_documentation();
4422
if (!docs.is_empty()) {
4423
// The documentation of a GDScript's main class is at the end of the array.
4424
// Hacky because this isn't necessarily always guaranteed.
4425
classname = docs[docs.size() - 1].name;
4426
}
4427
}
4428
}
4429
}
4430
4431
StringName propname = property_prefix + p.name;
4432
for (const KeyValue<String, String> &E : doc_property_class_remaps) {
4433
if (property_prefix.begins_with(E.key)) {
4434
propname = property_prefix.trim_prefix(E.key) + p.name;
4435
classname = E.value;
4436
break;
4437
}
4438
}
4439
4440
bool found = false;
4441
4442
// Small hack for theme_overrides. They are listed under Control, but come from another class.
4443
if (classname == "Control" && p.name.begins_with("theme_override_")) {
4444
classname = get_edited_object()->get_class();
4445
}
4446
4447
// Search for the doc path in the cache.
4448
HashMap<StringName, HashMap<StringName, DocCacheInfo>>::Iterator E = doc_cache.find(classname);
4449
if (E) {
4450
HashMap<StringName, DocCacheInfo>::Iterator F = E->value.find(propname);
4451
if (F) {
4452
found = true;
4453
doc_path = F->value.doc_path;
4454
theme_item_name = F->value.theme_item_name;
4455
}
4456
}
4457
4458
if (!found) {
4459
DocTools *dd = EditorHelp::get_doc_data();
4460
// Do not cache the doc path information of scripts.
4461
bool is_native_class = ClassDB::class_exists(classname);
4462
4463
HashMap<String, DocData::ClassDoc>::ConstIterator F = dd->class_list.find(classname);
4464
while (F) {
4465
Vector<String> slices = propname.operator String().split("/");
4466
// Check if it's a theme item first.
4467
if (slices.size() == 2 && slices[0].begins_with("theme_override_")) {
4468
for (int i = 0; i < F->value.theme_properties.size(); i++) {
4469
String doc_path_current = "class_theme_item:" + F->value.name + ":" + F->value.theme_properties[i].name;
4470
if (F->value.theme_properties[i].name == slices[1]) {
4471
doc_path = doc_path_current;
4472
theme_item_name = F->value.theme_properties[i].name;
4473
}
4474
}
4475
} else {
4476
for (int i = 0; i < F->value.properties.size(); i++) {
4477
String doc_path_current = "class_property:" + F->value.name + ":" + F->value.properties[i].name;
4478
if (F->value.properties[i].name == propname.operator String()) {
4479
doc_path = doc_path_current;
4480
}
4481
}
4482
}
4483
4484
if (is_native_class) {
4485
DocCacheInfo cache_info;
4486
cache_info.doc_path = doc_path;
4487
cache_info.theme_item_name = theme_item_name;
4488
doc_cache[classname][propname] = cache_info;
4489
}
4490
4491
if (!doc_path.is_empty() || F->value.inherits.is_empty()) {
4492
break;
4493
}
4494
// Couldn't find the doc path in the class itself, try its super class.
4495
F = dd->class_list.find(F->value.inherits);
4496
}
4497
}
4498
4499
// `|` separators used in `EditorHelpBit`.
4500
if (theme_item_name.is_empty()) {
4501
if (p.name.contains("shader_parameter/")) {
4502
doc_tooltip_text = "property|" + p.class_name + "|" + property_prefix + propname;
4503
} else if (p.usage & PROPERTY_USAGE_INTERNAL) {
4504
doc_tooltip_text = "internal_property|" + classname + "|" + propname;
4505
} else {
4506
doc_tooltip_text = "property|" + classname + "|" + propname;
4507
}
4508
} else {
4509
doc_tooltip_text = "theme_item|" + classname + "|" + theme_item_name;
4510
}
4511
}
4512
4513
// Only used for boolean types. Makes the section header a checkable group and adds tooltips.
4514
if (p.hint == PROPERTY_HINT_GROUP_ENABLE) {
4515
if (p.type == Variant::BOOL && (p.name.begins_with(group_base) || p.name.begins_with(subgroup_base))) {
4516
EditorInspectorSection *last_created_section = Object::cast_to<EditorInspectorSection>(current_vbox->get_parent());
4517
if (last_created_section) {
4518
bool valid = false;
4519
Variant value_checked = object->get(p.name, &valid);
4520
4521
if (valid) {
4522
last_created_section->set_checkable(p.name, p.hint_string == "checkbox_only", value_checked.operator bool());
4523
last_created_section->set_keying(keying);
4524
4525
if (p.name.begins_with(group_base)) {
4526
group_togglable_property = last_created_section;
4527
} else {
4528
subgroup_togglable_property = last_created_section;
4529
}
4530
4531
if (use_doc_hints) {
4532
last_created_section->set_tooltip_text(doc_tooltip_text);
4533
}
4534
continue;
4535
}
4536
}
4537
} else {
4538
ERR_PRINT("PROPERTY_HINT_GROUP_ENABLE can only be used on boolean types and must have the same prefix as the group.");
4539
}
4540
}
4541
4542
Vector<EditorInspectorPlugin::AddedEditor> editors;
4543
Vector<EditorInspectorPlugin::AddedEditor> late_editors;
4544
4545
// Search for the inspector plugin that will handle the properties. Then add the correct property editor to it.
4546
for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
4547
bool exclusive = ped->parse_property(object, p.type, p.name, p.hint, p.hint_string, p.usage, wide_editors);
4548
4549
for (const EditorInspectorPlugin::AddedEditor &F : ped->added_editors) {
4550
if (F.add_to_end) {
4551
late_editors.push_back(F);
4552
} else {
4553
editors.push_back(F);
4554
}
4555
}
4556
4557
ped->added_editors.clear();
4558
4559
if (exclusive) {
4560
break;
4561
}
4562
}
4563
4564
editors.append_array(late_editors);
4565
4566
const Node *node = Object::cast_to<Node>(object);
4567
4568
Vector<SceneState::PackState> sstack;
4569
if (node != nullptr) {
4570
const Node *es = EditorNode::get_singleton()->get_edited_scene();
4571
sstack = PropertyUtils::get_node_states_stack(node, es);
4572
}
4573
4574
for (int i = 0; i < editors.size(); i++) {
4575
EditorProperty *ep = Object::cast_to<EditorProperty>(editors[i].property_editor);
4576
const Vector<String> &properties = editors[i].properties;
4577
4578
if (ep) {
4579
// Set all this before the control gets the ENTER_TREE notification.
4580
ep->object = object;
4581
4582
if (properties.size()) {
4583
if (properties.size() == 1) {
4584
// Since it's one, associate:
4585
ep->property = properties[0];
4586
ep->property_path = property_prefix + properties[0];
4587
ep->property_usage = p.usage;
4588
// And set label?
4589
}
4590
if (!editors[i].label.is_empty()) {
4591
ep->set_label(editors[i].label);
4592
} else {
4593
// Use the existing one.
4594
ep->set_label(property_label_string);
4595
}
4596
4597
for (int j = 0; j < properties.size(); j++) {
4598
String prop = properties[j];
4599
4600
if (!editor_property_map.has(prop)) {
4601
editor_property_map[prop] = List<EditorProperty *>();
4602
}
4603
editor_property_map[prop].push_back(ep);
4604
}
4605
}
4606
4607
if (sub_inspector_use_filter) {
4608
EditorPropertyResource *epr = Object::cast_to<EditorPropertyResource>(ep);
4609
if (epr) {
4610
epr->set_use_filter(true);
4611
}
4612
}
4613
4614
if (p.name.begins_with("metadata/")) {
4615
Variant _default = Variant();
4616
if (node != nullptr) {
4617
_default = PropertyUtils::get_property_default_value(node, p.name, nullptr, &sstack, false, nullptr, nullptr);
4618
}
4619
ep->set_deletable(_default == Variant());
4620
} else {
4621
ep->set_deletable(deletable_properties);
4622
}
4623
4624
ep->set_draw_warning(draw_warning);
4625
ep->set_use_folding(use_folding);
4626
ep->set_favoritable(can_favorite && !disable_favorite && !ep->is_deletable());
4627
ep->set_checkable(checkable);
4628
ep->set_checked(checked);
4629
ep->set_keying(keying);
4630
ep->set_read_only(property_read_only || all_read_only);
4631
}
4632
4633
if (ep && ep->is_favoritable() && current_favorites.has(p.name)) {
4634
ep->favorited = true;
4635
favorites_to_add[group][subgroup].push_back(ep);
4636
4637
if (group_togglable_property) {
4638
togglable_editor_inspector_sections[group] = group_togglable_property;
4639
}
4640
if (subgroup_togglable_property) {
4641
togglable_editor_inspector_sections[group + "/" + subgroup] = subgroup_togglable_property;
4642
}
4643
} else {
4644
current_vbox->add_child(editors[i].property_editor);
4645
4646
if (ep) {
4647
Node *section_search = current_vbox->get_parent();
4648
while (section_search) {
4649
EditorInspectorSection *section = Object::cast_to<EditorInspectorSection>(section_search);
4650
if (section) {
4651
ep->connect("property_can_revert_changed", callable_mp(section, &EditorInspectorSection::property_can_revert_changed));
4652
}
4653
section_search = section_search->get_parent();
4654
if (Object::cast_to<EditorInspector>(section_search)) {
4655
// Skip sub-resource inspectors.
4656
break;
4657
}
4658
}
4659
}
4660
}
4661
4662
if (ep) {
4663
// Eventually, set other properties/signals after the property editor got added to the tree.
4664
bool update_all = (p.usage & PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED);
4665
ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed).bind(update_all));
4666
ep->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
4667
ep->connect("property_deleted", callable_mp(this, &EditorInspector::_property_deleted), CONNECT_DEFERRED);
4668
ep->connect("property_keyed_with_value", callable_mp(this, &EditorInspector::_property_keyed_with_value));
4669
ep->connect("property_favorited", callable_mp(this, &EditorInspector::_set_property_favorited), CONNECT_DEFERRED);
4670
ep->connect("property_checked", callable_mp(this, &EditorInspector::_property_checked));
4671
ep->connect("property_pinned", callable_mp(this, &EditorInspector::_property_pinned));
4672
ep->connect("selected", callable_mp(this, &EditorInspector::_property_selected));
4673
ep->connect("multiple_properties_changed", callable_mp(this, &EditorInspector::_multiple_properties_changed));
4674
ep->connect("resource_selected", callable_mp(get_root_inspector(), &EditorInspector::_resource_selected), CONNECT_DEFERRED);
4675
ep->connect("object_id_selected", callable_mp(this, &EditorInspector::_object_id_selected), CONNECT_DEFERRED);
4676
4677
ep->set_tooltip_text(doc_tooltip_text);
4678
ep->has_doc_tooltip = use_doc_hints;
4679
ep->set_doc_path(doc_path);
4680
ep->set_internal(p.usage & PROPERTY_USAGE_INTERNAL);
4681
4682
// If this property is favorited, it won't be in the tree yet. So don't do this setup right now.
4683
if (ep->is_inside_tree()) {
4684
ep->update_property();
4685
ep->_update_flags();
4686
ep->update_editor_property_status();
4687
ep->update_cache();
4688
4689
if (current_selected && ep->property == current_selected) {
4690
ep->select(current_focusable);
4691
}
4692
}
4693
}
4694
}
4695
}
4696
4697
if (!current_favorites.is_empty()) {
4698
favorites_section->show();
4699
4700
// Organize the favorited properties in their sections, to keep context and differentiate from others with the same name.
4701
bool is_localized = property_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED;
4702
for (const KeyValue<String, HashMap<String, LocalVector<EditorProperty *>>> &KV : favorites_to_add) {
4703
String section_name = KV.key;
4704
String label;
4705
String tooltip;
4706
VBoxContainer *parent_vbox = favorites_vbox;
4707
if (!section_name.is_empty()) {
4708
favorites_groups_vbox->show();
4709
4710
if (is_localized) {
4711
label = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
4712
tooltip = section_name;
4713
} else {
4714
label = section_name;
4715
tooltip = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
4716
}
4717
4718
EditorInspectorSection *section = memnew(EditorInspectorSection);
4719
get_root_inspector()->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(section, &EditorInspectorSection::reset_timer).unbind(1));
4720
favorites_groups_vbox->add_child(section);
4721
parent_vbox = section->get_vbox();
4722
section->setup("", section_name, object, sscolor, false);
4723
section->set_tooltip_text(tooltip);
4724
4725
if (togglable_editor_inspector_sections.has(section_name)) {
4726
EditorInspectorSection *corresponding_section = togglable_editor_inspector_sections.get(section_name);
4727
4728
bool valid = false;
4729
Variant value_checked = object->get(corresponding_section->related_enable_property, &valid);
4730
if (valid) {
4731
section->section = corresponding_section->section;
4732
section->set_checkable(corresponding_section->related_enable_property, corresponding_section->checkbox_only, value_checked.operator bool());
4733
section->set_keying(keying);
4734
if (use_doc_hints) {
4735
section->set_tooltip_text(corresponding_section->get_tooltip_text());
4736
}
4737
4738
section->connect("section_toggled_by_user", callable_mp(this, &EditorInspector::_section_toggled_by_user));
4739
section->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
4740
sections.push_back(section);
4741
}
4742
}
4743
}
4744
4745
for (const KeyValue<String, LocalVector<EditorProperty *>> &KV2 : KV.value) {
4746
section_name = KV2.key;
4747
VBoxContainer *vbox = parent_vbox;
4748
if (!section_name.is_empty()) {
4749
if (is_localized) {
4750
label = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
4751
tooltip = section_name;
4752
} else {
4753
label = section_name;
4754
tooltip = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
4755
}
4756
4757
EditorInspectorSection *section = memnew(EditorInspectorSection);
4758
get_root_inspector()->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(section, &EditorInspectorSection::reset_timer).unbind(1));
4759
vbox->add_child(section);
4760
vbox = section->get_vbox();
4761
section->setup("", section_name, object, sscolor, false);
4762
section->set_tooltip_text(tooltip);
4763
4764
if (togglable_editor_inspector_sections.has(KV.key + "/" + section_name)) {
4765
EditorInspectorSection *corresponding_section = togglable_editor_inspector_sections.get(KV.key + "/" + section_name);
4766
4767
bool valid = false;
4768
Variant value_checked = object->get(corresponding_section->related_enable_property, &valid);
4769
if (valid) {
4770
section->section = corresponding_section->section;
4771
section->set_checkable(corresponding_section->related_enable_property, corresponding_section->checkbox_only, value_checked.operator bool());
4772
section->set_keying(keying);
4773
if (use_doc_hints) {
4774
section->set_tooltip_text(corresponding_section->get_tooltip_text());
4775
}
4776
4777
section->connect("section_toggled_by_user", callable_mp(this, &EditorInspector::_section_toggled_by_user));
4778
section->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
4779
sections.push_back(section);
4780
}
4781
}
4782
}
4783
4784
for (EditorProperty *ep : KV2.value) {
4785
vbox->add_child(ep);
4786
4787
Node *section_search = vbox->get_parent();
4788
while (section_search) {
4789
EditorInspectorSection *section = Object::cast_to<EditorInspectorSection>(section_search);
4790
if (section) {
4791
ep->connect("property_can_revert_changed", callable_mp(section, &EditorInspectorSection::property_can_revert_changed));
4792
}
4793
section_search = section_search->get_parent();
4794
if (Object::cast_to<EditorInspector>(section_search)) {
4795
// Skip sub-resource inspectors.
4796
break;
4797
}
4798
}
4799
4800
// Now that it's inside the tree, do the setup.
4801
ep->update_property();
4802
ep->_update_flags();
4803
ep->update_editor_property_status();
4804
ep->update_cache();
4805
4806
if (current_selected && ep->property == current_selected) {
4807
ep->select(current_focusable);
4808
}
4809
}
4810
}
4811
4812
if (favorites_vbox->get_child_count() > 0) {
4813
favorites_vbox->show();
4814
}
4815
}
4816
4817
// Show a separator if there's no category to clearly divide the properties.
4818
favorites_separator->hide();
4819
if (main_vbox->get_child_count() > 0) {
4820
EditorInspectorCategory *category = Object::cast_to<EditorInspectorCategory>(main_vbox->get_child(0));
4821
if (!category) {
4822
favorites_separator->show();
4823
}
4824
}
4825
4826
// Clean up empty sections.
4827
for (List<EditorInspectorSection *>::Element *I = sections.back(); I;) {
4828
EditorInspectorSection *section = I->get();
4829
I = I->prev(); // Note: Advance before erasing element.
4830
if (section->get_vbox()->get_child_count() == 0) {
4831
sections.erase(section);
4832
vbox_per_path[main_vbox].erase(section->get_section());
4833
memdelete(section);
4834
}
4835
}
4836
}
4837
4838
if (!hide_metadata && !object->call("_hide_metadata_from_inspector")) {
4839
// Add 4px of spacing between the "Add Metadata" button and the content above it.
4840
Control *spacer = memnew(Control);
4841
spacer->set_custom_minimum_size(Size2(0, 4) * EDSCALE);
4842
main_vbox->add_child(spacer);
4843
4844
Button *add_md = memnew(EditorInspectorActionButton(TTRC("Add Metadata"), SNAME("Add")));
4845
add_md->connect(SceneStringName(pressed), callable_mp(this, &EditorInspector::_show_add_meta_dialog));
4846
main_vbox->add_child(add_md);
4847
if (all_read_only) {
4848
add_md->set_disabled(true);
4849
}
4850
}
4851
4852
// Get the lists of to add at the end.
4853
for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
4854
ped->parse_end(object);
4855
_parse_added_editors(main_vbox, nullptr, ped);
4856
}
4857
4858
if (is_main_editor_inspector()) {
4859
// Updating inspector might invalidate some editing owners.
4860
EditorNode::get_singleton()->hide_unused_editors();
4861
}
4862
4863
if (root_inspector_was_following_focus) {
4864
get_root_inspector()->set_follow_focus(true);
4865
}
4866
}
4867
4868
void EditorInspector::update_property(const String &p_prop) {
4869
if (!editor_property_map.has(p_prop)) {
4870
return;
4871
}
4872
4873
for (EditorProperty *E : editor_property_map[p_prop]) {
4874
E->update_property();
4875
E->update_editor_property_status();
4876
E->update_cache();
4877
}
4878
4879
for (EditorInspectorSection *S : sections) {
4880
if (S->is_checkable()) {
4881
S->_property_edited(p_prop);
4882
}
4883
}
4884
}
4885
4886
void EditorInspector::_clear(bool p_hide_plugins) {
4887
begin_vbox->hide();
4888
while (begin_vbox->get_child_count()) {
4889
memdelete(begin_vbox->get_child(0));
4890
}
4891
4892
favorites_section->hide();
4893
favorites_vbox->hide();
4894
while (favorites_vbox->get_child_count()) {
4895
memdelete(favorites_vbox->get_child(0));
4896
}
4897
favorites_groups_vbox->hide();
4898
while (favorites_groups_vbox->get_child_count()) {
4899
memdelete(favorites_groups_vbox->get_child(0));
4900
}
4901
4902
while (main_vbox->get_child_count()) {
4903
memdelete(main_vbox->get_child(0));
4904
}
4905
4906
property_selected = StringName();
4907
property_focusable = -1;
4908
editor_property_map.clear();
4909
sections.clear();
4910
pending.clear();
4911
restart_request_props.clear();
4912
4913
if (p_hide_plugins && is_main_editor_inspector()) {
4914
EditorNode::get_singleton()->hide_unused_editors(this);
4915
}
4916
}
4917
4918
Object *EditorInspector::get_edited_object() {
4919
return object;
4920
}
4921
4922
Object *EditorInspector::get_next_edited_object() {
4923
return next_object;
4924
}
4925
4926
void EditorInspector::edit(Object *p_object) {
4927
if (object == p_object) {
4928
return;
4929
}
4930
4931
next_object = p_object; // Some plugins need to know the next edited object when clearing the inspector.
4932
if (object) {
4933
if (likely(Variant(object).get_validated_object())) {
4934
object->disconnect(CoreStringName(property_list_changed), callable_mp(this, &EditorInspector::_changed_callback));
4935
}
4936
_clear();
4937
}
4938
per_array_page.clear();
4939
4940
object = p_object;
4941
4942
if (object) {
4943
update_scroll_request = 0; //reset
4944
if (scroll_cache.has(object->get_instance_id())) { //if exists, set something else
4945
update_scroll_request = scroll_cache[object->get_instance_id()]; //done this way because wait until full size is accommodated
4946
}
4947
object->connect(CoreStringName(property_list_changed), callable_mp(this, &EditorInspector::_changed_callback));
4948
4949
can_favorite = Object::cast_to<Node>(object) || Object::cast_to<Resource>(object);
4950
_update_current_favorites();
4951
4952
update_tree();
4953
}
4954
4955
// Keep it available until the end so it works with both main and sub inspectors.
4956
next_object = nullptr;
4957
4958
emit_signal(SNAME("edited_object_changed"));
4959
}
4960
4961
void EditorInspector::set_keying(bool p_active) {
4962
if (keying == p_active) {
4963
return;
4964
}
4965
keying = p_active;
4966
_keying_changed();
4967
}
4968
4969
void EditorInspector::_keying_changed() {
4970
for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
4971
for (EditorProperty *E : F.value) {
4972
if (E) {
4973
E->set_keying(keying);
4974
}
4975
}
4976
}
4977
4978
for (EditorInspectorSection *S : sections) {
4979
S->set_keying(keying);
4980
}
4981
}
4982
4983
void EditorInspector::set_read_only(bool p_read_only) {
4984
if (p_read_only == read_only) {
4985
return;
4986
}
4987
read_only = p_read_only;
4988
update_tree();
4989
}
4990
4991
EditorPropertyNameProcessor::Style EditorInspector::get_property_name_style() const {
4992
return property_name_style;
4993
}
4994
4995
void EditorInspector::set_property_name_style(EditorPropertyNameProcessor::Style p_style) {
4996
if (property_name_style == p_style) {
4997
return;
4998
}
4999
property_name_style = p_style;
5000
update_tree();
5001
}
5002
5003
void EditorInspector::set_use_settings_name_style(bool p_enable) {
5004
if (use_settings_name_style == p_enable) {
5005
return;
5006
}
5007
use_settings_name_style = p_enable;
5008
if (use_settings_name_style) {
5009
set_property_name_style(EditorPropertyNameProcessor::get_singleton()->get_settings_style());
5010
}
5011
}
5012
5013
void EditorInspector::set_autoclear(bool p_enable) {
5014
autoclear = p_enable;
5015
}
5016
5017
void EditorInspector::set_show_categories(bool p_show_standard, bool p_show_custom) {
5018
show_standard_categories = p_show_standard;
5019
show_custom_categories = p_show_custom;
5020
update_tree();
5021
}
5022
5023
void EditorInspector::set_use_doc_hints(bool p_enable) {
5024
use_doc_hints = p_enable;
5025
update_tree();
5026
}
5027
5028
void EditorInspector::set_hide_script(bool p_hide) {
5029
hide_script = p_hide;
5030
update_tree();
5031
}
5032
5033
void EditorInspector::set_hide_metadata(bool p_hide) {
5034
hide_metadata = p_hide;
5035
update_tree();
5036
}
5037
5038
void EditorInspector::set_use_filter(bool p_use) {
5039
use_filter = p_use;
5040
update_tree();
5041
}
5042
5043
void EditorInspector::register_text_enter(Node *p_line_edit) {
5044
search_box = Object::cast_to<LineEdit>(p_line_edit);
5045
if (search_box) {
5046
search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorInspector::update_tree).unbind(1));
5047
}
5048
}
5049
5050
void EditorInspector::set_use_folding(bool p_use_folding, bool p_update_tree) {
5051
use_folding = p_use_folding;
5052
5053
if (p_update_tree) {
5054
update_tree();
5055
}
5056
}
5057
5058
bool EditorInspector::is_using_folding() {
5059
return use_folding;
5060
}
5061
5062
void EditorInspector::collapse_all_folding() {
5063
for (EditorInspectorSection *E : sections) {
5064
E->fold();
5065
}
5066
5067
for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
5068
for (EditorProperty *E : F.value) {
5069
E->collapse_all_folding();
5070
}
5071
}
5072
}
5073
5074
void EditorInspector::expand_all_folding() {
5075
for (EditorInspectorSection *E : sections) {
5076
E->unfold();
5077
}
5078
for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
5079
for (EditorProperty *E : F.value) {
5080
E->expand_all_folding();
5081
}
5082
}
5083
}
5084
5085
void EditorInspector::expand_revertable() {
5086
HashSet<EditorInspectorSection *> sections_to_unfold[2];
5087
for (EditorInspectorSection *E : sections) {
5088
if (E->has_revertable_properties()) {
5089
sections_to_unfold[0].insert(E);
5090
}
5091
}
5092
5093
// Climb up the hierarchy doing double buffering with the sets.
5094
int a = 0;
5095
int b = 1;
5096
while (sections_to_unfold[a].size()) {
5097
for (EditorInspectorSection *E : sections_to_unfold[a]) {
5098
E->unfold();
5099
5100
Node *n = E->get_parent();
5101
while (n) {
5102
if (Object::cast_to<EditorInspector>(n)) {
5103
break;
5104
}
5105
if (Object::cast_to<EditorInspectorSection>(n) && !sections_to_unfold[a].has((EditorInspectorSection *)n)) {
5106
sections_to_unfold[b].insert((EditorInspectorSection *)n);
5107
}
5108
n = n->get_parent();
5109
}
5110
}
5111
5112
sections_to_unfold[a].clear();
5113
SWAP(a, b);
5114
}
5115
5116
for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
5117
for (EditorProperty *E : F.value) {
5118
E->expand_revertable();
5119
}
5120
}
5121
}
5122
5123
void EditorInspector::set_scroll_offset(int p_offset) {
5124
// This can be called before the container finishes sorting its children, so defer it.
5125
callable_mp((ScrollContainer *)this, &ScrollContainer::set_v_scroll).call_deferred(p_offset);
5126
}
5127
5128
int EditorInspector::get_scroll_offset() const {
5129
return get_v_scroll();
5130
}
5131
5132
void EditorInspector::set_use_wide_editors(bool p_enable) {
5133
wide_editors = p_enable;
5134
}
5135
5136
void EditorInspector::set_root_inspector(EditorInspector *p_root_inspector) {
5137
root_inspector = p_root_inspector;
5138
// Only the root inspector should follow focus.
5139
set_follow_focus(false);
5140
}
5141
5142
void EditorInspector::_section_toggled_by_user(const String &p_path, bool p_value) {
5143
_property_changed(p_path, p_value);
5144
}
5145
5146
void EditorInspector::set_use_deletable_properties(bool p_enabled) {
5147
deletable_properties = p_enabled;
5148
}
5149
5150
void EditorInspector::_page_change_request(int p_new_page, const StringName &p_array_prefix) {
5151
int prev_page = per_array_page.has(p_array_prefix) ? per_array_page[p_array_prefix] : 0;
5152
int new_page = MAX(0, p_new_page);
5153
if (new_page != prev_page) {
5154
per_array_page[p_array_prefix] = new_page;
5155
update_tree_pending = true;
5156
}
5157
}
5158
5159
void EditorInspector::_edit_request_change(Object *p_object, const String &p_property) {
5160
if (object != p_object) { //may be undoing/redoing for a non edited object, so ignore
5161
return;
5162
}
5163
5164
if (changing) {
5165
return;
5166
}
5167
5168
if (p_property.is_empty()) {
5169
update_tree_pending = true;
5170
} else {
5171
pending.insert(p_property);
5172
}
5173
}
5174
5175
void EditorInspector::_edit_set(const String &p_name, const Variant &p_value, bool p_refresh_all, const String &p_changed_field) {
5176
if (autoclear && editor_property_map.has(p_name)) {
5177
for (EditorProperty *E : editor_property_map[p_name]) {
5178
if (E->is_checkable()) {
5179
E->set_checked(true);
5180
}
5181
}
5182
}
5183
5184
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
5185
if (!undo_redo || bool(object->call("_dont_undo_redo"))) {
5186
object->set(p_name, p_value);
5187
if (p_refresh_all) {
5188
_edit_request_change(object, "");
5189
} else {
5190
_edit_request_change(object, p_name);
5191
}
5192
5193
emit_signal(_prop_edited, p_name);
5194
} else if (Object::cast_to<MultiNodeEdit>(object)) {
5195
Object::cast_to<MultiNodeEdit>(object)->set_property_field(p_name, p_value, p_changed_field);
5196
_edit_request_change(object, p_name);
5197
emit_signal(_prop_edited, p_name);
5198
} else if (Object::cast_to<EditorDebuggerRemoteObjects>(object)) {
5199
Object::cast_to<EditorDebuggerRemoteObjects>(object)->set_property_field(p_name, p_value, p_changed_field);
5200
_edit_request_change(object, p_name);
5201
emit_signal(_prop_edited, p_name);
5202
} else {
5203
undo_redo->create_action(vformat(TTR("Set %s"), p_name), UndoRedo::MERGE_ENDS, nullptr, false, mark_unsaved);
5204
undo_redo->add_do_property(object, p_name, p_value);
5205
bool valid = false;
5206
Variant value = object->get(p_name, &valid);
5207
Variant::Type type = p_value.get_type();
5208
if (valid) {
5209
if (Object::cast_to<Control>(object) && (p_name == "anchors_preset" || p_name == "layout_mode")) {
5210
undo_redo->add_undo_method(object, "_edit_set_state", Object::cast_to<Control>(object)->_edit_get_state());
5211
} else {
5212
undo_redo->add_undo_property(object, p_name, value);
5213
}
5214
Node *N = Object::cast_to<Node>(object);
5215
bool double_counting = Object::cast_to<Node>(p_value) == N || Object::cast_to<Node>(value) == N;
5216
if (N && !double_counting && (type == Variant::OBJECT || type == Variant::ARRAY || type == Variant::DICTIONARY) && value != p_value) {
5217
undo_redo->add_do_method(EditorNode::get_singleton(), "update_node_reference", value, N, true);
5218
undo_redo->add_do_method(EditorNode::get_singleton(), "update_node_reference", p_value, N, false);
5219
// Perhaps an inefficient way of updating the resource count.
5220
// We could go in depth and check which Resource values changed/got removed and which ones stayed the same, but this is more readable at the moment.
5221
undo_redo->add_undo_method(EditorNode::get_singleton(), "update_node_reference", p_value, N, true);
5222
undo_redo->add_undo_method(EditorNode::get_singleton(), "update_node_reference", value, N, false);
5223
}
5224
}
5225
5226
List<StringName> linked_properties;
5227
ClassDB::get_linked_properties_info(object->get_class_name(), p_name, &linked_properties);
5228
5229
for (const StringName &linked_prop : linked_properties) {
5230
valid = false;
5231
Variant undo_value = object->get(linked_prop, &valid);
5232
if (valid) {
5233
undo_redo->add_undo_property(object, linked_prop, undo_value);
5234
}
5235
}
5236
5237
PackedStringArray linked_properties_dynamic = object->call("_get_linked_undo_properties", p_name, p_value);
5238
for (int i = 0; i < linked_properties_dynamic.size(); i++) {
5239
valid = false;
5240
Variant undo_value = object->get(linked_properties_dynamic[i], &valid);
5241
if (valid) {
5242
undo_redo->add_undo_property(object, linked_properties_dynamic[i], undo_value);
5243
}
5244
}
5245
5246
Variant v_undo_redo = undo_redo;
5247
Variant v_object = object;
5248
Variant v_name = p_name;
5249
const Vector<Callable> &callbacks = EditorNode::get_editor_data().get_undo_redo_inspector_hook_callback();
5250
for (int i = 0; i < callbacks.size(); i++) {
5251
const Callable &callback = callbacks[i];
5252
5253
const Variant *p_arguments[] = { &v_undo_redo, &v_object, &v_name, &p_value };
5254
Variant return_value;
5255
Callable::CallError call_error;
5256
5257
callback.callp(p_arguments, 4, return_value, call_error);
5258
if (call_error.error != Callable::CallError::CALL_OK) {
5259
ERR_PRINT("Invalid UndoRedo callback.");
5260
}
5261
}
5262
5263
if (p_refresh_all) {
5264
undo_redo->add_do_method(this, "_edit_request_change", object, "");
5265
undo_redo->add_undo_method(this, "_edit_request_change", object, "");
5266
} else {
5267
undo_redo->add_do_method(this, "_edit_request_change", object, p_name);
5268
undo_redo->add_undo_method(this, "_edit_request_change", object, p_name);
5269
}
5270
5271
Resource *r = Object::cast_to<Resource>(object);
5272
5273
if (r) {
5274
//Setting a Subresource. Since there's possibly multiple Nodes referencing 'r', we need to link them to the Subresource.
5275
List<Node *> shared_nodes = EditorNode::get_singleton()->get_resource_node_list(r);
5276
for (Node *N : shared_nodes) {
5277
if ((type == Variant::OBJECT || type == Variant::ARRAY || type == Variant::DICTIONARY) && value != p_value) {
5278
undo_redo->add_do_method(EditorNode::get_singleton(), "update_node_reference", value, N, true);
5279
undo_redo->add_do_method(EditorNode::get_singleton(), "update_node_reference", p_value, N, false);
5280
// Perhaps an inefficient way of updating the resource count.
5281
// We could go in depth and check which Resource values changed/got removed and which ones stayed the same, but this is more readable at the moment.
5282
undo_redo->add_undo_method(EditorNode::get_singleton(), "update_node_reference", p_value, N, true);
5283
undo_redo->add_undo_method(EditorNode::get_singleton(), "update_node_reference", value, N, false);
5284
}
5285
}
5286
if (String(p_name) == "resource_local_to_scene") {
5287
bool prev = object->get(p_name);
5288
bool next = p_value;
5289
if (next) {
5290
undo_redo->add_do_method(r, "setup_local_to_scene");
5291
}
5292
if (prev) {
5293
undo_redo->add_undo_method(r, "setup_local_to_scene");
5294
}
5295
}
5296
}
5297
undo_redo->add_do_method(this, "emit_signal", _prop_edited, p_name);
5298
undo_redo->add_undo_method(this, "emit_signal", _prop_edited, p_name);
5299
undo_redo->commit_action();
5300
}
5301
5302
if (editor_property_map.has(p_name)) {
5303
for (EditorProperty *E : editor_property_map[p_name]) {
5304
E->update_editor_property_status();
5305
}
5306
}
5307
}
5308
5309
void EditorInspector::_property_changed(const String &p_path, const Variant &p_value, const String &p_name, bool p_changing, bool p_update_all) {
5310
// The "changing" variable must be true for properties that trigger events as typing occurs,
5311
// like "text_changed" signal. E.g. text property of Label, Button, RichTextLabel, etc.
5312
if (p_changing) {
5313
changing++;
5314
}
5315
5316
_edit_set(p_path, p_value, p_update_all, p_name);
5317
5318
if (p_changing) {
5319
changing--;
5320
}
5321
5322
if (restart_request_props.has(p_path)) {
5323
emit_signal(SNAME("restart_requested"));
5324
}
5325
}
5326
5327
void EditorInspector::_multiple_properties_changed(const Vector<String> &p_paths, const Array &p_values, bool p_changing) {
5328
ERR_FAIL_COND(p_paths.is_empty() || p_values.is_empty());
5329
ERR_FAIL_COND(p_paths.size() != p_values.size());
5330
String names;
5331
for (int i = 0; i < p_paths.size(); i++) {
5332
if (i > 0) {
5333
names += ",";
5334
}
5335
names += p_paths[i];
5336
}
5337
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
5338
// TRANSLATORS: This is describing a change to multiple properties at once. The parameter is a list of property names.
5339
undo_redo->create_action(vformat(TTR("Set Multiple: %s"), names), UndoRedo::MERGE_ENDS);
5340
for (int i = 0; i < p_paths.size(); i++) {
5341
_edit_set(p_paths[i], p_values[i], false, "");
5342
if (restart_request_props.has(p_paths[i])) {
5343
emit_signal(SNAME("restart_requested"));
5344
}
5345
}
5346
if (p_changing) {
5347
changing++;
5348
}
5349
undo_redo->commit_action();
5350
if (p_changing) {
5351
changing--;
5352
}
5353
}
5354
5355
void EditorInspector::_property_keyed(const String &p_path, bool p_advance) {
5356
if (!object) {
5357
return;
5358
}
5359
5360
// The second parameter could be null, causing the event to fire with less arguments, so use the pointer call which preserves it.
5361
const Variant args[3] = { p_path, object->get(p_path), p_advance };
5362
const Variant *argp[3] = { &args[0], &args[1], &args[2] };
5363
emit_signalp(SNAME("property_keyed"), argp, 3);
5364
}
5365
5366
void EditorInspector::_property_deleted(const String &p_path) {
5367
if (!object) {
5368
return;
5369
}
5370
5371
if (p_path.begins_with("metadata/")) {
5372
String name = p_path.replace_first("metadata/", "");
5373
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
5374
undo_redo->create_action(vformat(TTR("Remove metadata %s"), name));
5375
undo_redo->add_do_method(object, "remove_meta", name);
5376
undo_redo->add_undo_method(object, "set_meta", name, object->get_meta(name));
5377
undo_redo->commit_action();
5378
}
5379
5380
if (restart_request_props.has(p_path)) {
5381
emit_signal(SNAME("restart_requested"));
5382
}
5383
emit_signal(SNAME("property_deleted"), p_path);
5384
}
5385
5386
void EditorInspector::_property_keyed_with_value(const String &p_path, const Variant &p_value, bool p_advance) {
5387
if (!object) {
5388
return;
5389
}
5390
5391
// The second parameter could be null, causing the event to fire with less arguments, so use the pointer call which preserves it.
5392
const Variant args[3] = { p_path, p_value, p_advance };
5393
const Variant *argp[3] = { &args[0], &args[1], &args[2] };
5394
emit_signalp(SNAME("property_keyed"), argp, 3);
5395
}
5396
5397
void EditorInspector::_property_checked(const String &p_path, bool p_checked) {
5398
if (!object) {
5399
return;
5400
}
5401
5402
//property checked
5403
if (autoclear) {
5404
if (!p_checked) {
5405
_edit_set(p_path, Variant(), false, "");
5406
} else {
5407
Variant to_create;
5408
Control *control = Object::cast_to<Control>(object);
5409
if (control && p_path.begins_with("theme_override_")) {
5410
to_create = control->get_used_theme_item(p_path);
5411
} else {
5412
List<PropertyInfo> pinfo;
5413
object->get_property_list(&pinfo);
5414
for (const PropertyInfo &E : pinfo) {
5415
if (E.name == p_path) {
5416
Callable::CallError ce;
5417
Variant::construct(E.type, to_create, nullptr, 0, ce);
5418
break;
5419
}
5420
}
5421
}
5422
_edit_set(p_path, to_create, false, "");
5423
}
5424
5425
if (editor_property_map.has(p_path)) {
5426
for (EditorProperty *E : editor_property_map[p_path]) {
5427
E->set_checked(p_checked);
5428
E->update_property();
5429
E->update_editor_property_status();
5430
E->update_cache();
5431
}
5432
}
5433
} else {
5434
emit_signal(SNAME("property_toggled"), p_path, p_checked);
5435
}
5436
}
5437
5438
void EditorInspector::_property_pinned(const String &p_path, bool p_pinned) {
5439
if (!object) {
5440
return;
5441
}
5442
5443
Node *node = Object::cast_to<Node>(object);
5444
ERR_FAIL_NULL(node);
5445
5446
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
5447
undo_redo->create_action(vformat(p_pinned ? TTR("Pinned %s") : TTR("Unpinned %s"), p_path));
5448
undo_redo->add_do_method(node, "_set_property_pinned", p_path, p_pinned);
5449
undo_redo->add_undo_method(node, "_set_property_pinned", p_path, !p_pinned);
5450
if (editor_property_map.has(p_path)) {
5451
for (List<EditorProperty *>::Element *E = editor_property_map[p_path].front(); E; E = E->next()) {
5452
undo_redo->add_do_method(E->get(), "_update_editor_property_status");
5453
undo_redo->add_undo_method(E->get(), "_update_editor_property_status");
5454
}
5455
}
5456
undo_redo->commit_action();
5457
}
5458
5459
void EditorInspector::_property_selected(const String &p_path, int p_focusable) {
5460
property_selected = p_path;
5461
property_focusable = p_focusable;
5462
// Deselect the others.
5463
for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
5464
if (F.key == property_selected) {
5465
continue;
5466
}
5467
for (EditorProperty *E : F.value) {
5468
if (E->is_selected()) {
5469
E->deselect();
5470
}
5471
}
5472
}
5473
5474
emit_signal(SNAME("property_selected"), p_path);
5475
}
5476
5477
void EditorInspector::_object_id_selected(const String &p_path, ObjectID p_id) {
5478
emit_signal(SNAME("object_id_selected"), p_id);
5479
}
5480
5481
void EditorInspector::_resource_selected(const String &p_path, Ref<Resource> p_resource) {
5482
emit_signal(SNAME("resource_selected"), p_resource, p_path);
5483
}
5484
5485
void EditorInspector::_node_removed(Node *p_node) {
5486
if (p_node == object) {
5487
edit(nullptr);
5488
}
5489
}
5490
5491
void EditorInspector::_update_current_favorites() {
5492
current_favorites.clear();
5493
if (!can_favorite) {
5494
return;
5495
}
5496
5497
HashMap<String, PackedStringArray> favorites = EditorSettings::get_singleton()->get_favorite_properties();
5498
5499
// Fetch script properties.
5500
Ref<Script> scr = object->get_script();
5501
if (scr.is_valid()) {
5502
List<PropertyInfo> plist;
5503
// FIXME: Only properties from a saved script will be available, unsaved ones will be ignored.
5504
// Can cause a little wonkiness, while nothing serious, would be nice to find a way to get
5505
// unsaved ones without needing to get the entire property list of an object.
5506
scr->get_script_property_list(&plist);
5507
5508
String path;
5509
HashMap<String, LocalVector<String>> props;
5510
5511
for (PropertyInfo &p : plist) {
5512
if (p.usage & PROPERTY_USAGE_CATEGORY) {
5513
path = favorites.has(p.hint_string) ? p.hint_string : String();
5514
} else if (p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE && !path.is_empty()) {
5515
props[path].push_back(p.name);
5516
}
5517
}
5518
5519
// Add favorited properties while removing invalid ones.
5520
bool invalid_props = false;
5521
for (const KeyValue<String, LocalVector<String>> &KV : props) {
5522
path = KV.key;
5523
for (int i = 0; i < favorites[path].size(); i++) {
5524
String prop = favorites[path][i];
5525
if (KV.value.has(prop)) {
5526
current_favorites.append(prop);
5527
} else {
5528
invalid_props = true;
5529
favorites[path].erase(prop);
5530
i--;
5531
}
5532
}
5533
5534
if (favorites[path].is_empty()) {
5535
favorites.erase(path);
5536
}
5537
}
5538
5539
if (invalid_props) {
5540
EditorSettings::get_singleton()->set_favorite_properties(favorites);
5541
}
5542
}
5543
5544
// Fetch built-in properties.
5545
StringName class_name = object->get_class_name();
5546
for (const KeyValue<String, PackedStringArray> &KV : favorites) {
5547
if (ClassDB::is_parent_class(class_name, KV.key)) {
5548
current_favorites.append_array(KV.value);
5549
}
5550
}
5551
}
5552
5553
void EditorInspector::_set_property_favorited(const String &p_path, bool p_favorited) {
5554
if (!object) {
5555
return;
5556
}
5557
5558
StringName validate_name = object->get_class_name();
5559
StringName class_name;
5560
5561
String theme_property;
5562
if (p_path.begins_with("theme_override_")) {
5563
theme_property = p_path.get_slicec('/', 1);
5564
}
5565
5566
while (!validate_name.is_empty()) {
5567
class_name = validate_name;
5568
5569
if (!theme_property.is_empty()) { // Deal with theme properties.
5570
bool found = false;
5571
HashMap<String, DocData::ClassDoc>::ConstIterator F = EditorHelp::get_doc_data()->class_list.find(class_name);
5572
if (F) {
5573
for (const DocData::ThemeItemDoc &prop : F->value.theme_properties) {
5574
if (prop.name == theme_property) {
5575
found = true;
5576
break;
5577
}
5578
}
5579
}
5580
5581
if (found) {
5582
break;
5583
}
5584
} else if (ClassDB::has_property(class_name, p_path, true)) { // Check if the property is built-in.
5585
break;
5586
}
5587
5588
validate_name = ClassDB::get_parent_class_nocheck(class_name);
5589
}
5590
5591
// "script" isn't a real property, so a hack is necessary.
5592
if (validate_name.is_empty() && p_path != "script") {
5593
class_name = "";
5594
}
5595
5596
if (class_name.is_empty()) {
5597
// Check if it's part of a script.
5598
Ref<Script> scr = object->get_script();
5599
if (scr.is_valid()) {
5600
List<PropertyInfo> plist;
5601
scr->get_script_property_list(&plist);
5602
5603
String path;
5604
for (PropertyInfo &p : plist) {
5605
if (p.usage & PROPERTY_USAGE_CATEGORY) {
5606
path = p.hint_string;
5607
} else if (p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE && p.name == p_path) {
5608
class_name = path;
5609
break;
5610
}
5611
}
5612
}
5613
5614
ERR_FAIL_COND_MSG(class_name.is_empty(), "Can't favorite invalid property. If said property was from a script and recently renamed, try saving it first.");
5615
}
5616
5617
HashMap<String, PackedStringArray> favorites = EditorSettings::get_singleton()->get_favorite_properties();
5618
if (p_favorited) {
5619
current_favorites.append(p_path);
5620
favorites[class_name].append(p_path);
5621
} else {
5622
current_favorites.erase(p_path);
5623
5624
if (favorites.has(class_name) && favorites[class_name].has(p_path)) {
5625
if (favorites[class_name].size() > 1) {
5626
favorites[class_name].erase(p_path);
5627
} else {
5628
favorites.erase(class_name);
5629
}
5630
}
5631
}
5632
EditorSettings::get_singleton()->set_favorite_properties(favorites);
5633
5634
update_tree();
5635
}
5636
5637
void EditorInspector::_clear_current_favorites() {
5638
current_favorites.clear();
5639
5640
HashMap<String, PackedStringArray> favorites = EditorSettings::get_singleton()->get_favorite_properties();
5641
5642
Ref<Script> scr = object->get_script();
5643
if (scr.is_valid()) {
5644
List<PropertyInfo> plist;
5645
scr->get_script_property_list(&plist);
5646
5647
for (PropertyInfo &p : plist) {
5648
if (p.usage & PROPERTY_USAGE_CATEGORY && favorites.has(p.hint_string)) {
5649
favorites.erase(p.hint_string);
5650
}
5651
}
5652
}
5653
5654
StringName class_name = object->get_class_name();
5655
while (class_name) {
5656
if (favorites.has(class_name)) {
5657
favorites.erase(class_name);
5658
}
5659
5660
class_name = ClassDB::get_parent_class(class_name);
5661
}
5662
5663
EditorSettings::get_singleton()->set_favorite_properties(favorites);
5664
update_tree();
5665
}
5666
5667
void EditorInspector::_notification(int p_what) {
5668
switch (p_what) {
5669
case NOTIFICATION_TRANSLATION_CHANGED: {
5670
if (property_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
5671
update_tree_pending = true;
5672
}
5673
} break;
5674
5675
case NOTIFICATION_THEME_CHANGED: {
5676
if (updating_theme) {
5677
break;
5678
}
5679
5680
theme_cache.prop_subsection = get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor));
5681
5682
initialize_section_theme(section_theme_cache, this);
5683
initialize_category_theme(category_theme_cache, this);
5684
initialize_property_theme(property_theme_cache, this);
5685
} break;
5686
5687
case NOTIFICATION_READY: {
5688
if (EditorFeatureProfileManager::get_singleton()) {
5689
EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &EditorInspector::_feature_profile_changed));
5690
}
5691
set_process(is_visible_in_tree());
5692
if (!is_sub_inspector()) {
5693
get_tree()->connect("node_removed", callable_mp(this, &EditorInspector::_node_removed));
5694
}
5695
} break;
5696
5697
case NOTIFICATION_PREDELETE: {
5698
if (!is_sub_inspector() && is_inside_tree()) {
5699
get_tree()->disconnect("node_removed", callable_mp(this, &EditorInspector::_node_removed));
5700
}
5701
edit(nullptr);
5702
} break;
5703
5704
case NOTIFICATION_VISIBILITY_CHANGED: {
5705
set_process(is_visible_in_tree());
5706
} break;
5707
5708
case NOTIFICATION_PROCESS: {
5709
if (update_scroll_request >= 0) {
5710
callable_mp((Range *)get_v_scroll_bar(), &Range::set_value).call_deferred(update_scroll_request);
5711
update_scroll_request = -1;
5712
}
5713
if (update_tree_pending) {
5714
refresh_countdown = float(EDITOR_GET("docks/property_editor/auto_refresh_interval"));
5715
} else if (refresh_countdown > 0) {
5716
refresh_countdown -= get_process_delta_time();
5717
if (refresh_countdown <= 0) {
5718
for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
5719
for (EditorProperty *E : F.value) {
5720
if (E && !E->is_cache_valid()) {
5721
E->update_property();
5722
E->update_editor_property_status();
5723
E->update_cache();
5724
}
5725
}
5726
}
5727
5728
for (EditorInspectorSection *S : sections) {
5729
S->update_property();
5730
}
5731
5732
refresh_countdown = float(EDITOR_GET("docks/property_editor/auto_refresh_interval"));
5733
}
5734
}
5735
5736
changing++;
5737
5738
if (update_tree_pending) {
5739
update_tree();
5740
update_tree_pending = false;
5741
} else {
5742
for (const StringName &prop : pending) {
5743
if (editor_property_map.has(prop)) {
5744
for (EditorProperty *E : editor_property_map[prop]) {
5745
E->update_property();
5746
E->update_editor_property_status();
5747
E->update_cache();
5748
}
5749
}
5750
}
5751
5752
for (EditorInspectorSection *S : sections) {
5753
S->update_property();
5754
}
5755
}
5756
5757
pending.clear();
5758
5759
changing--;
5760
} break;
5761
5762
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
5763
if (use_settings_name_style && EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/localize_settings")) {
5764
EditorPropertyNameProcessor::Style style = EditorPropertyNameProcessor::get_settings_style();
5765
if (property_name_style != style) {
5766
property_name_style = style;
5767
update_tree_pending = true;
5768
}
5769
}
5770
5771
if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/inspector")) {
5772
update_tree_pending = true;
5773
}
5774
} break;
5775
}
5776
}
5777
5778
void EditorInspector::_changed_callback() {
5779
//this is called when property change is notified via notify_property_list_changed()
5780
if (object != nullptr) {
5781
_update_current_favorites();
5782
_edit_request_change(object, String());
5783
}
5784
}
5785
5786
void EditorInspector::_vscroll_changed(double p_offset) {
5787
if (update_scroll_request >= 0) { //waiting, do nothing
5788
return;
5789
}
5790
5791
if (object) {
5792
scroll_cache[object->get_instance_id()] = p_offset;
5793
}
5794
}
5795
5796
void EditorInspector::set_property_prefix(const String &p_prefix) {
5797
property_prefix = p_prefix;
5798
}
5799
5800
String EditorInspector::get_property_prefix() const {
5801
return property_prefix;
5802
}
5803
5804
void EditorInspector::add_custom_property_description(const String &p_class, const String &p_property, const String &p_description) {
5805
const String key = vformat("property|%s|%s", p_class, p_property);
5806
custom_property_descriptions[key] = p_description;
5807
}
5808
5809
String EditorInspector::get_custom_property_description(const String &p_property) const {
5810
HashMap<String, String>::ConstIterator E = custom_property_descriptions.find(p_property);
5811
if (E) {
5812
return E->value;
5813
}
5814
return "";
5815
}
5816
5817
void EditorInspector::remap_doc_property_class(const String &p_property_prefix, const String &p_class) {
5818
doc_property_class_remaps[p_property_prefix] = p_class;
5819
}
5820
5821
void EditorInspector::set_object_class(const String &p_class) {
5822
object_class = p_class;
5823
}
5824
5825
String EditorInspector::get_object_class() const {
5826
return object_class;
5827
}
5828
5829
void EditorInspector::_feature_profile_changed() {
5830
update_tree();
5831
}
5832
5833
void EditorInspector::set_restrict_to_basic_settings(bool p_restrict) {
5834
restrict_to_basic = p_restrict;
5835
update_tree();
5836
}
5837
5838
void EditorInspector::set_property_clipboard(const Variant &p_value) {
5839
property_clipboard = p_value;
5840
}
5841
5842
Variant EditorInspector::get_property_clipboard() {
5843
return property_clipboard;
5844
}
5845
5846
void EditorInspector::_show_add_meta_dialog() {
5847
if (!add_meta_dialog) {
5848
add_meta_dialog = memnew(AddMetadataDialog());
5849
add_meta_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorInspector::_add_meta_confirm));
5850
add_child(add_meta_dialog);
5851
}
5852
5853
StringName dialog_title;
5854
Node *node = Object::cast_to<Node>(object);
5855
// If object is derived from Node use node name, if derived from Resource use classname.
5856
dialog_title = node ? node->get_name() : StringName(object->get_class());
5857
5858
List<StringName> existing_meta_keys;
5859
object->get_meta_list(&existing_meta_keys);
5860
add_meta_dialog->open(dialog_title, existing_meta_keys);
5861
}
5862
5863
void EditorInspector::_add_meta_confirm() {
5864
// Ensure metadata is unfolded when adding a new metadata.
5865
object->editor_set_section_unfold("metadata", true);
5866
5867
String name = add_meta_dialog->get_meta_name();
5868
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
5869
undo_redo->create_action(vformat(TTR("Add metadata %s"), name));
5870
undo_redo->add_do_method(object, "set_meta", name, add_meta_dialog->get_meta_defval());
5871
undo_redo->add_undo_method(object, "remove_meta", name);
5872
undo_redo->commit_action();
5873
}
5874
5875
EditorInspector *EditorInspector::_get_control_parent_inspector(Control *p_control) {
5876
{
5877
EditorInspector *inspector = Object::cast_to<EditorInspector>(p_control);
5878
if (inspector) {
5879
return inspector;
5880
}
5881
}
5882
5883
Control *parent = p_control->get_parent_control();
5884
while (parent) {
5885
EditorInspector *inspector = Object::cast_to<EditorInspector>(parent);
5886
if (inspector) {
5887
return inspector;
5888
}
5889
parent = parent->get_parent_control();
5890
}
5891
return nullptr;
5892
}
5893
5894
void EditorInspector::_bind_methods() {
5895
ClassDB::bind_method(D_METHOD("edit", "object"), &EditorInspector::edit);
5896
ClassDB::bind_method("_edit_request_change", &EditorInspector::_edit_request_change);
5897
ClassDB::bind_method("get_selected_path", &EditorInspector::get_selected_path);
5898
ClassDB::bind_method("get_edited_object", &EditorInspector::get_edited_object);
5899
5900
ClassDB::bind_static_method("EditorInspector", D_METHOD("instantiate_property_editor", "object", "type", "path", "hint", "hint_text", "usage", "wide"), &EditorInspector::instantiate_property_editor, DEFVAL(false));
5901
5902
ADD_SIGNAL(MethodInfo("property_selected", PropertyInfo(Variant::STRING, "property")));
5903
ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), PropertyInfo(Variant::BOOL, "advance")));
5904
ADD_SIGNAL(MethodInfo("property_deleted", PropertyInfo(Variant::STRING, "property")));
5905
ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, Resource::get_class_static()), PropertyInfo(Variant::STRING, "path")));
5906
ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::INT, "id")));
5907
ADD_SIGNAL(MethodInfo("property_edited", PropertyInfo(Variant::STRING, "property")));
5908
ADD_SIGNAL(MethodInfo("property_toggled", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::BOOL, "checked")));
5909
ADD_SIGNAL(MethodInfo("edited_object_changed"));
5910
ADD_SIGNAL(MethodInfo("restart_requested"));
5911
}
5912
5913
EditorInspector::EditorInspector() {
5914
object = nullptr;
5915
5916
base_vbox = memnew(VBoxContainer);
5917
base_vbox->set_theme_type_variation(SNAME("EditorInspectorContainer"));
5918
base_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
5919
add_child(base_vbox);
5920
5921
begin_vbox = memnew(VBoxContainer);
5922
begin_vbox->set_theme_type_variation(SNAME("EditorInspectorContainer"));
5923
base_vbox->add_child(begin_vbox);
5924
begin_vbox->hide();
5925
5926
favorites_section = memnew(VBoxContainer);
5927
favorites_section->set_theme_type_variation(SNAME("EditorInspectorContainer"));
5928
base_vbox->add_child(favorites_section);
5929
favorites_section->hide();
5930
5931
EditorInspectorCategory *favorites_category = memnew(EditorInspectorCategory);
5932
favorites_category->set_as_favorite();
5933
favorites_category->connect("unfavorite_all", callable_mp(this, &EditorInspector::_clear_current_favorites));
5934
favorites_section->add_child(favorites_category);
5935
5936
favorites_vbox = memnew(VBoxContainer);
5937
favorites_vbox->set_theme_type_variation(SNAME("EditorPropertyContainer"));
5938
favorites_section->add_child(favorites_vbox);
5939
favorites_vbox->hide();
5940
favorites_groups_vbox = memnew(VBoxContainer);
5941
favorites_groups_vbox->set_theme_type_variation(SNAME("EditorInspectorContainer"));
5942
favorites_section->add_child(favorites_groups_vbox);
5943
favorites_groups_vbox->hide();
5944
5945
favorites_separator = memnew(HSeparator);
5946
favorites_section->add_child(favorites_separator);
5947
favorites_separator->hide();
5948
5949
main_vbox = memnew(VBoxContainer);
5950
main_vbox->set_theme_type_variation(SNAME("EditorInspectorContainer"));
5951
base_vbox->add_child(main_vbox);
5952
5953
set_horizontal_scroll_mode(SCROLL_MODE_DISABLED);
5954
set_follow_focus(true);
5955
5956
changing = 0;
5957
search_box = nullptr;
5958
_prop_edited = "property_edited";
5959
set_process(false);
5960
set_focus_mode(FocusMode::FOCUS_ALL);
5961
property_focusable = -1;
5962
5963
get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(this, &EditorInspector::_vscroll_changed));
5964
update_scroll_request = -1;
5965
if (EditorSettings::get_singleton()) {
5966
refresh_countdown = float(EDITOR_GET("docks/property_editor/auto_refresh_interval"));
5967
} else {
5968
//used when class is created by the docgen to dump default values of everything bindable, editorsettings may not be created
5969
refresh_countdown = 0.33;
5970
}
5971
5972
ED_SHORTCUT("property_editor/copy_value", TTRC("Copy Value"), KeyModifierMask::CMD_OR_CTRL | Key::C);
5973
ED_SHORTCUT("property_editor/paste_value", TTRC("Paste Value"), KeyModifierMask::CMD_OR_CTRL | Key::V);
5974
ED_SHORTCUT("property_editor/copy_property_path", TTRC("Copy Property Path"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::C);
5975
5976
// `use_settings_name_style` is true by default, set the name style accordingly.
5977
set_property_name_style(EditorPropertyNameProcessor::get_singleton()->get_settings_style());
5978
5979
set_draw_focus_border(true);
5980
set_scroll_on_drag_hover(true);
5981
}
5982
5983