Debian Codegraph @

editor/inspector/editor_inspector.cpp

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