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