Debian Codegraph @

editor/inspector/editor_properties.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_properties.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_properties.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/input/input_map.h"
35 #include "core/string/translation_server.h"
36 #include "editor/docks/inspector_dock.h"
37 #include "editor/docks/scene_tree_dock.h"
38 #include "editor/editor_node.h"
39 #include "editor/editor_string_names.h"
40 #include "editor/gui/create_dialog.h"
41 #include "editor/gui/editor_file_dialog.h"
42 #include "editor/gui/editor_spin_slider.h"
43 #include "editor/gui/editor_variant_type_selectors.h"
44 #include "editor/inspector/editor_properties_array_dict.h"
45 #include "editor/inspector/editor_properties_vector.h"
46 #include "editor/inspector/editor_resource_picker.h"
47 #include "editor/inspector/property_selector.h"
48 #include "editor/scene/scene_tree_editor.h"
49 #include "editor/script/script_editor_plugin.h"
50 #include "editor/settings/editor_settings.h"
51 #include "editor/settings/project_settings_editor.h"
52 #include "editor/themes/editor_scale.h"
53 #include "scene/2d/gpu_particles_2d.h"
54 #include "scene/3d/fog_volume.h"
55 #include "scene/3d/gpu_particles_3d.h"
56 #include "scene/gui/color_picker.h"
57 #include "scene/gui/grid_container.h"
58 #include "scene/gui/text_edit.h"
59 #include "scene/main/window.h"
60 #include "scene/resources/font.h"
61 #include "scene/resources/mesh.h"
62 #include "scene/resources/visual_shader_nodes.h"
63
64 ///////////////////// NIL /////////////////////////
65
66 void EditorPropertyNil::update_property() {
67 }
68
69 EditorPropertyNil::EditorPropertyNil() {
70 Label *prop_label = memnew(Label);
71 prop_label->set_text("<null>");
72 add_child(prop_label);
73 }
74
75 //////////////////// VARIANT ///////////////////////
76
77 void EditorPropertyVariant::_change_type(int p_to_type) {
78 new_type = Variant::Type(p_to_type);
79
80 Variant zero;
81 Callable::CallError ce;
82 Variant::construct(new_type, zero, nullptr, 0, ce);
83 emit_changed(get_edited_property(), zero);
84 }
85
86 void EditorPropertyVariant::_popup_edit_menu() {
87 if (change_type == nullptr) {
88 change_type = memnew(EditorVariantTypePopupMenu(false));
89 change_type->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyVariant::_change_type));
90 content->add_child(change_type);
91 }
92
93 Rect2 rect = edit_button->get_screen_rect();
94 change_type->set_position(rect.get_end() - Vector2(change_type->get_contents_minimum_size().x, 0));
95 change_type->popup();
96 }
97
98 void EditorPropertyVariant::_set_read_only(bool p_read_only) {
99 edit_button->set_disabled(p_read_only);
100 if (sub_property) {
101 sub_property->set_read_only(p_read_only);
102 }
103 }
104
105 void EditorPropertyVariant::_notification(int p_what) {
106 if (p_what == NOTIFICATION_THEME_CHANGED) {
107 edit_button->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
108 }
109 }
110
111 void EditorPropertyVariant::update_property() {
112 const Variant &value = get_edited_property_value();
113 if (new_type == Variant::VARIANT_MAX) {
114 new_type = value.get_type();
115 }
116
117 if (new_type != current_type) {
118 current_type = new_type;
119
120 if (sub_property) {
121 memdelete(sub_property);
122 sub_property = nullptr;
123 }
124
125 if (current_type == Variant::OBJECT) {
126 sub_property = EditorInspector::instantiate_property_editor(nullptr, current_type, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource", PROPERTY_USAGE_NONE);
127 } else {
128 sub_property = EditorInspector::instantiate_property_editor(nullptr, current_type, "", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE);
129 }
130 ERR_FAIL_NULL(sub_property);
131
132 sub_property->set_object_and_property(get_edited_object(), get_edited_property());
133 sub_property->set_name_split_ratio(0);
134 sub_property->set_selectable(false);
135 sub_property->set_use_folding(is_using_folding());
136 sub_property->set_read_only(is_read_only());
137 sub_property->set_h_size_flags(SIZE_EXPAND_FILL);
138 sub_property->connect(SNAME("property_changed"), callable_mp((EditorProperty *)this, &EditorProperty::emit_changed));
139 content->add_child(sub_property);
140 content->move_child(sub_property, 0);
141 sub_property->update_property();
142 } else if (sub_property) {
143 sub_property->update_property();
144 }
145 new_type = Variant::VARIANT_MAX;
146 }
147
148 EditorPropertyVariant::EditorPropertyVariant() {
149 content = memnew(HBoxContainer);
150 add_child(content);
151
152 edit_button = memnew(Button);
153 edit_button->set_flat(true);
154 edit_button->set_theme_type_variation(SNAME("EditorInspectorButton"));
155 edit_button->set_accessibility_name(TTRC("Edit"));
156 edit_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyVariant::_popup_edit_menu));
157 content->add_child(edit_button);
158 }
159
160 ///////////////////// TEXT /////////////////////////
161
162 void EditorPropertyText::_notification(int p_what) {
163 switch (p_what) {
164 case NOTIFICATION_THEME_CHANGED: {
165 _update_theme();
166 } break;
167 }
168 }
169
170 void EditorPropertyText::_set_read_only(bool p_read_only) {
171 text->set_editable(!p_read_only);
172 }
173
174 void EditorPropertyText::_update_theme() {
175 Ref<Font> font;
176 int font_size;
177
178 if (monospaced) {
179 font = get_theme_font(SNAME("source"), EditorStringName(EditorFonts));
180 font_size = get_theme_font_size(SNAME("source_size"), EditorStringName(EditorFonts));
181 } else {
182 font = get_theme_font(SceneStringName(font), SNAME("LineEdit"));
183 font_size = get_theme_font_size(SceneStringName(font_size), SNAME("LineEdit"));
184 }
185
186 text->add_theme_font_override(SceneStringName(font), font);
187 text->add_theme_font_size_override(SceneStringName(font_size), font_size);
188 }
189
190 void EditorPropertyText::_text_submitted(const String &p_string) {
191 if (updating) {
192 return;
193 }
194
195 if (text->has_focus()) {
196 _text_changed(p_string);
197 }
198 }
199
200 void EditorPropertyText::_text_changed(const String &p_string) {
201 if (updating) {
202 return;
203 }
204
205 // Set tooltip so that the full text is displayed in a tooltip if hovered.
206 // This is useful when using a narrow inspector, as the text can be trimmed otherwise.
207 if (text->is_secret()) {
208 text->set_tooltip_text(get_tooltip_string(text->get_placeholder()));
209 } else {
210 text->set_tooltip_text(get_tooltip_string(text->get_text()));
211 }
212
213 if (string_name) {
214 emit_changed(get_edited_property(), StringName(p_string));
215 } else {
216 emit_changed(get_edited_property(), p_string);
217 }
218 }
219
220 void EditorPropertyText::update_property() {
221 String s = get_edited_property_value();
222 updating = true;
223 if (text->get_text() != s) {
224 int caret = text->get_caret_column();
225 text->set_text(s);
226 if (text->is_secret()) {
227 text->set_tooltip_text(get_tooltip_string(text->get_placeholder()));
228 } else {
229 text->set_tooltip_text(get_tooltip_string(s));
230 }
231 text->set_caret_column(caret);
232 }
233 text->set_editable(!is_read_only());
234 updating = false;
235 }
236
237 void EditorPropertyText::set_string_name(bool p_enabled) {
238 string_name = p_enabled;
239 if (p_enabled) {
240 Label *prefix = memnew(Label("&"));
241 prefix->set_tooltip_text("StringName");
242 prefix->set_mouse_filter(MOUSE_FILTER_STOP);
243 text->get_parent()->add_child(prefix);
244 text->get_parent()->move_child(prefix, 0);
245 }
246 }
247
248 void EditorPropertyText::set_secret(bool p_enabled) {
249 text->set_secret(p_enabled);
250 }
251
252 void EditorPropertyText::set_placeholder(const String &p_string) {
253 text->set_placeholder(p_string);
254 }
255
256 void EditorPropertyText::set_monospaced(bool p_monospaced) {
257 if (p_monospaced == monospaced) {
258 return;
259 }
260 monospaced = p_monospaced;
261 _update_theme();
262 }
263
264 EditorPropertyText::EditorPropertyText() {
265 HBoxContainer *hb = memnew(HBoxContainer);
266 add_child(hb);
267
268 text = memnew(LineEdit);
269 text->set_h_size_flags(SIZE_EXPAND_FILL);
270 text->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Prevents translating placeholder.
271 hb->add_child(text);
272 add_focusable(text);
273 text->connect(SceneStringName(text_changed), callable_mp(this, &EditorPropertyText::_text_changed));
274 text->connect(SceneStringName(text_submitted), callable_mp(this, &EditorPropertyText::_text_submitted));
275 }
276
277 ///////////////////// MULTILINE TEXT /////////////////////////
278
279 void EditorPropertyMultilineText::_set_read_only(bool p_read_only) {
280 text->set_editable(!p_read_only);
281 open_big_text->set_disabled(p_read_only);
282 }
283
284 void EditorPropertyMultilineText::_big_text_changed() {
285 text->set_text(big_text->get_text());
286 // Set tooltip so that the full text is displayed in a tooltip if hovered.
287 // This is useful when using a narrow inspector, as the text can be trimmed otherwise.
288 text->set_tooltip_text(get_tooltip_string(big_text->get_text()));
289 emit_changed(get_edited_property(), big_text->get_text(), "", true);
290 }
291
292 void EditorPropertyMultilineText::_text_changed() {
293 text->set_tooltip_text(get_tooltip_string(text->get_text()));
294 emit_changed(get_edited_property(), text->get_text(), "", true);
295 }
296
297 void EditorPropertyMultilineText::_open_big_text() {
298 if (!big_text_dialog) {
299 big_text = memnew(TextEdit);
300 if (expression) {
301 big_text->set_syntax_highlighter(text->get_syntax_highlighter());
302 }
303 big_text->connect(SceneStringName(text_changed), callable_mp(this, &EditorPropertyMultilineText::_big_text_changed));
304 big_text->set_line_wrapping_mode(wrap_lines
305 ? TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY
306 : TextEdit::LineWrappingMode::LINE_WRAPPING_NONE);
307 big_text_dialog = memnew(AcceptDialog);
308 big_text_dialog->add_child(big_text);
309 big_text_dialog->set_title(TTR("Edit Text:"));
310 add_child(big_text_dialog);
311 }
312
313 big_text_dialog->popup_centered_clamped(Size2(1000, 900) * EDSCALE, 0.8);
314 big_text->set_text(text->get_text());
315 big_text->grab_focus();
316
317 _update_theme();
318 }
319
320 void EditorPropertyMultilineText::update_property() {
321 String t = get_edited_property_value();
322 if (text->get_text() != t) {
323 text->set_text(t);
324 text->set_tooltip_text(get_tooltip_string(t));
325 if (big_text && big_text->is_visible_in_tree()) {
326 big_text->set_text(t);
327 }
328 }
329 }
330
331 void EditorPropertyMultilineText::_update_theme() {
332 Ref<Texture2D> df = get_editor_theme_icon(SNAME("DistractionFree"));
333 open_big_text->set_button_icon(df);
334
335 Ref<Font> font;
336 int font_size;
337 if (expression) {
338 font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts));
339 font_size = get_theme_font_size(SNAME("expression_size"), EditorStringName(EditorFonts));
340 } else {
341 // Non expression.
342 if (monospaced) {
343 font = get_theme_font(SNAME("source"), EditorStringName(EditorFonts));
344 font_size = get_theme_font_size(SNAME("source_size"), EditorStringName(EditorFonts));
345 } else {
346 font = get_theme_font(SceneStringName(font), SNAME("TextEdit"));
347 font_size = get_theme_font_size(SceneStringName(font_size), SNAME("TextEdit"));
348 }
349 }
350 text->add_theme_font_override(SceneStringName(font), font);
351 text->add_theme_font_size_override(SceneStringName(font_size), font_size);
352 text->set_line_wrapping_mode(wrap_lines
353 ? TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY
354 : TextEdit::LineWrappingMode::LINE_WRAPPING_NONE);
355 if (big_text) {
356 big_text->add_theme_font_override(SceneStringName(font), font);
357 big_text->add_theme_font_size_override(SceneStringName(font_size), font_size);
358 big_text->set_line_wrapping_mode(wrap_lines
359 ? TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY
360 : TextEdit::LineWrappingMode::LINE_WRAPPING_NONE);
361 }
362
363 text->set_custom_minimum_size(Vector2(0, font->get_height(font_size) * 6));
364 }
365
366 void EditorPropertyMultilineText::_notification(int p_what) {
367 switch (p_what) {
368 case NOTIFICATION_THEME_CHANGED: {
369 _update_theme();
370 } break;
371 }
372 }
373
374 void EditorPropertyMultilineText::EditorPropertyMultilineText::set_monospaced(bool p_monospaced) {
375 if (p_monospaced == monospaced) {
376 return;
377 }
378 monospaced = p_monospaced;
379 _update_theme();
380 }
381
382 bool EditorPropertyMultilineText::EditorPropertyMultilineText::get_monospaced() {
383 return monospaced;
384 }
385
386 void EditorPropertyMultilineText::EditorPropertyMultilineText::set_wrap_lines(bool p_wrap_lines) {
387 if (p_wrap_lines == wrap_lines) {
388 return;
389 }
390 wrap_lines = p_wrap_lines;
391 _update_theme();
392 }
393
394 bool EditorPropertyMultilineText::EditorPropertyMultilineText::get_wrap_lines() {
395 return wrap_lines;
396 }
397
398 EditorPropertyMultilineText::EditorPropertyMultilineText(bool p_expression) :
399 expression(p_expression) {
400 HBoxContainer *hb = memnew(HBoxContainer);
401 hb->add_theme_constant_override("separation", 0);
402 add_child(hb);
403 set_bottom_editor(hb);
404 text = memnew(TextEdit);
405 text->connect(SceneStringName(text_changed), callable_mp(this, &EditorPropertyMultilineText::_text_changed));
406 text->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
407 add_focusable(text);
408 hb->add_child(text);
409 text->set_h_size_flags(SIZE_EXPAND_FILL);
410 open_big_text = memnew(Button);
411 open_big_text->set_accessibility_name(TTRC("Open Text Edit Dialog"));
412 open_big_text->set_flat(true);
413 open_big_text->set_theme_type_variation(SNAME("EditorInspectorButton"));
414 open_big_text->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyMultilineText::_open_big_text));
415 hb->add_child(open_big_text);
416 big_text_dialog = nullptr;
417 big_text = nullptr;
418
419 if (expression) {
420 Ref<EditorStandardSyntaxHighlighter> highlighter;
421 highlighter.instantiate();
422 text->set_syntax_highlighter(highlighter);
423 }
424 }
425
426 ///////////////////// TEXT ENUM /////////////////////////
427
428 void EditorPropertyTextEnum::_set_read_only(bool p_read_only) {
429 option_button->set_disabled(p_read_only);
430 edit_button->set_disabled(p_read_only);
431 }
432
433 void EditorPropertyTextEnum::_emit_changed_value(const String &p_string) {
434 if (string_name) {
435 emit_changed(get_edited_property(), StringName(p_string));
436 } else {
437 emit_changed(get_edited_property(), p_string);
438 }
439 }
440
441 void EditorPropertyTextEnum::_option_selected(int p_which) {
442 _emit_changed_value(option_button->get_item_metadata(p_which));
443 }
444
445 void EditorPropertyTextEnum::_edit_custom_value() {
446 default_layout->hide();
447 edit_custom_layout->show();
448 custom_value_edit->grab_focus(true);
449 }
450
451 void EditorPropertyTextEnum::_custom_value_submitted(const String &p_value) {
452 edit_custom_layout->hide();
453 default_layout->show();
454
455 _emit_changed_value(p_value.strip_edges());
456 }
457
458 void EditorPropertyTextEnum::_custom_value_accepted() {
459 String new_value = custom_value_edit->get_text().strip_edges();
460 _custom_value_submitted(new_value);
461 }
462
463 void EditorPropertyTextEnum::_custom_value_canceled() {
464 custom_value_edit->set_text(get_edited_property_value());
465
466 edit_custom_layout->hide();
467 default_layout->show();
468 }
469
470 void EditorPropertyTextEnum::update_property() {
471 String current_value = get_edited_property_value();
472 int default_option = options.find(current_value);
473
474 // The list can change in the loose mode.
475 if (loose_mode) {
476 custom_value_edit->set_text(current_value);
477 option_button->clear();
478
479 // Manually entered value.
480 if (default_option < 0 && !current_value.is_empty()) {
481 option_button->add_item(current_value, options.size() + 1001);
482 option_button->set_item_metadata(-1, current_value);
483 option_button->select(0);
484
485 option_button->add_separator();
486 }
487
488 // Add an explicit empty value for clearing the property.
489 option_button->add_item("", options.size() + 1000);
490 option_button->set_item_metadata(-1, String());
491
492 for (int i = 0; i < options.size(); i++) {
493 option_button->add_item(option_names[i], i);
494 option_button->set_item_metadata(-1, options[i]);
495 if (options[i] == current_value) {
496 option_button->select(option_button->get_item_count() - 1);
497 }
498 }
499 } else {
500 option_button->select(default_option);
501 if (default_option < 0) {
502 option_button->set_text(current_value);
503 }
504 }
505 }
506
507 void EditorPropertyTextEnum::setup(const Vector<String> &p_options, const Vector<String> &p_option_names, bool p_string_name, bool p_loose_mode) {
508 ERR_FAIL_COND(!p_option_names.is_empty() && p_option_names.size() != p_options.size());
509
510 string_name = p_string_name;
511 loose_mode = p_loose_mode;
512
513 options = p_options;
514 if (p_option_names.is_empty()) {
515 option_names = p_options;
516 } else {
517 option_names = p_option_names;
518 }
519
520 if (loose_mode) {
521 // Add an explicit empty value for clearing the property in the loose mode.
522 option_button->add_item("", options.size() + 1000);
523 option_button->set_item_metadata(-1, String());
524 }
525
526 for (int i = 0; i < options.size(); i++) {
527 option_button->add_item(option_names[i], i);
528 option_button->set_item_metadata(-1, options[i]);
529 }
530
531 if (loose_mode) {
532 edit_button->show();
533 }
534 }
535
536 void EditorPropertyTextEnum::_notification(int p_what) {
537 switch (p_what) {
538 case NOTIFICATION_THEME_CHANGED: {
539 edit_button->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
540 accept_button->set_button_icon(get_editor_theme_icon(SNAME("ImportCheck")));
541 cancel_button->set_button_icon(get_editor_theme_icon(SNAME("ImportFail")));
542 } break;
543 }
544 }
545
546 EditorPropertyTextEnum::EditorPropertyTextEnum() {
547 HBoxContainer *hb = memnew(HBoxContainer);
548 add_child(hb);
549
550 default_layout = memnew(HBoxContainer);
551 default_layout->set_h_size_flags(SIZE_EXPAND_FILL);
552 hb->add_child(default_layout);
553
554 edit_custom_layout = memnew(HBoxContainer);
555 edit_custom_layout->set_h_size_flags(SIZE_EXPAND_FILL);
556 edit_custom_layout->hide();
557 hb->add_child(edit_custom_layout);
558
559 option_button = memnew(OptionButton);
560 option_button->set_accessibility_name(TTRC("Enum Options"));
561 option_button->set_h_size_flags(SIZE_EXPAND_FILL);
562 option_button->set_clip_text(true);
563 option_button->set_flat(true);
564 option_button->set_theme_type_variation(SNAME("EditorInspectorButton"));
565 option_button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
566 default_layout->add_child(option_button);
567 option_button->connect(SceneStringName(item_selected), callable_mp(this, &EditorPropertyTextEnum::_option_selected));
568
569 edit_button = memnew(Button);
570 edit_button->set_accessibility_name(TTRC("Edit"));
571 edit_button->set_flat(true);
572 edit_button->set_theme_type_variation(SNAME("EditorInspectorButton"));
573 edit_button->hide();
574 default_layout->add_child(edit_button);
575 edit_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyTextEnum::_edit_custom_value));
576
577 custom_value_edit = memnew(LineEdit);
578 custom_value_edit->set_accessibility_name(TTRC("Custom Value"));
579 custom_value_edit->set_h_size_flags(SIZE_EXPAND_FILL);
580 edit_custom_layout->add_child(custom_value_edit);
581 custom_value_edit->connect(SceneStringName(text_submitted), callable_mp(this, &EditorPropertyTextEnum::_custom_value_submitted));
582
583 accept_button = memnew(Button);
584 accept_button->set_accessibility_name(TTRC("Accept Custom Value Edit"));
585 accept_button->set_flat(true);
586 accept_button->set_theme_type_variation(SNAME("EditorInspectorButton"));
587 edit_custom_layout->add_child(accept_button);
588 accept_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyTextEnum::_custom_value_accepted));
589
590 cancel_button = memnew(Button);
591 cancel_button->set_accessibility_name(TTRC("Cancel Custom Value Edit"));
592 cancel_button->set_flat(true);
593 cancel_button->set_theme_type_variation(SNAME("EditorInspectorButton"));
594 edit_custom_layout->add_child(cancel_button);
595 cancel_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyTextEnum::_custom_value_canceled));
596
597 add_focusable(option_button);
598 add_focusable(edit_button);
599 add_focusable(custom_value_edit);
600 add_focusable(accept_button);
601 add_focusable(cancel_button);
602 }
603
604 //////////////////// LOCALE ////////////////////////
605
606 void EditorPropertyLocale::_locale_selected(const String &p_locale) {
607 emit_changed(get_edited_property(), p_locale);
608 update_property();
609 }
610
611 void EditorPropertyLocale::_locale_pressed() {
612 if (!dialog) {
613 dialog = memnew(EditorLocaleDialog);
614 dialog->connect("locale_selected", callable_mp(this, &EditorPropertyLocale::_locale_selected));
615 add_child(dialog);
616 }
617
618 String locale_code = get_edited_property_value();
619 dialog->set_locale(locale_code);
620 dialog->popup_locale_dialog();
621 }
622
623 void EditorPropertyLocale::update_property() {
624 String locale_code = get_edited_property_value();
625 locale->set_text(locale_code);
626 locale->set_tooltip_text(locale_code);
627 }
628
629 void EditorPropertyLocale::setup(const String &p_hint_text) {
630 }
631
632 void EditorPropertyLocale::_notification(int p_what) {
633 switch (p_what) {
634 case NOTIFICATION_THEME_CHANGED: {
635 locale_edit->set_button_icon(get_editor_theme_icon(SNAME("Translation")));
636 } break;
637 }
638 }
639
640 void EditorPropertyLocale::_locale_focus_exited() {
641 _locale_selected(locale->get_text());
642 }
643
644 EditorPropertyLocale::EditorPropertyLocale() {
645 HBoxContainer *locale_hb = memnew(HBoxContainer);
646 add_child(locale_hb);
647 locale = memnew(LineEdit);
648 locale->set_accessibility_name(TTRC("Locale"));
649 locale_hb->add_child(locale);
650 locale->connect(SceneStringName(text_submitted), callable_mp(this, &EditorPropertyLocale::_locale_selected));
651 locale->connect(SceneStringName(focus_exited), callable_mp(this, &EditorPropertyLocale::_locale_focus_exited));
652 locale->set_h_size_flags(SIZE_EXPAND_FILL);
653
654 locale_edit = memnew(Button);
655 locale_edit->set_accessibility_name(TTRC("Edit"));
656 locale_edit->set_clip_text(true);
657 locale_edit->set_theme_type_variation(SNAME("EditorInspectorButton"));
658 locale_hb->add_child(locale_edit);
659 add_focusable(locale);
660 dialog = nullptr;
661 locale_edit->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyLocale::_locale_pressed));
662 }
663
664 ///////////////////// PATH /////////////////////////
665
666 void EditorPropertyPath::_set_read_only(bool p_read_only) {
667 path->set_editable(!p_read_only);
668 path_edit->set_disabled(p_read_only);
669 }
670
671 void EditorPropertyPath::_path_selected(const String &p_path) {
672 String full_path = p_path;
673
674 if (enable_uid) {
675 const ResourceUID::ID id = ResourceLoader::get_resource_uid(full_path);
676 if (id != ResourceUID::INVALID_ID) {
677 full_path = ResourceUID::get_singleton()->id_to_text(id);
678 }
679 }
680
681 emit_changed(get_edited_property(), full_path);
682 update_property();
683 }
684
685 String EditorPropertyPath::_get_path_text(bool p_allow_uid) {
686 String full_path = get_edited_property_value();
687 if (!p_allow_uid && full_path.begins_with("uid://")) {
688 full_path = ResourceUID::uid_to_path(full_path);
689 }
690
691 return full_path;
692 }
693
694 void EditorPropertyPath::_path_pressed() {
695 if (!dialog) {
696 dialog = memnew(EditorFileDialog);
697 dialog->connect("file_selected", callable_mp(this, &EditorPropertyPath::_path_selected));
698 dialog->connect("dir_selected", callable_mp(this, &EditorPropertyPath::_path_selected));
699 add_child(dialog);
700 }
701
702 String full_path = _get_path_text();
703
704 dialog->clear_filters();
705
706 if (global) {
707 dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
708 } else {
709 dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
710 }
711
712 if (folder) {
713 dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR);
714 dialog->set_current_dir(full_path);
715 } else {
716 dialog->set_file_mode(save_mode ? EditorFileDialog::FILE_MODE_SAVE_FILE : EditorFileDialog::FILE_MODE_OPEN_FILE);
717 for (int i = 0; i < extensions.size(); i++) {
718 String e = extensions[i].strip_edges();
719 if (!e.is_empty()) {
720 dialog->add_filter(extensions[i].strip_edges());
721 }
722 }
723 dialog->set_current_path(full_path);
724 }
725
726 dialog->popup_file_dialog();
727 }
728
729 void EditorPropertyPath::update_property() {
730 String full_path = _get_path_text(display_uid);
731 path->set_text(full_path);
732 path->set_tooltip_text(full_path);
733
734 toggle_uid->set_visible(get_edited_property_value().operator String().begins_with("uid://"));
735 }
736
737 void EditorPropertyPath::setup(const Vector<String> &p_extensions, bool p_folder, bool p_global, bool p_enable_uid) {
738 extensions = p_extensions;
739 folder = p_folder;
740 global = p_global;
741 enable_uid = p_enable_uid;
742 }
743
744 void EditorPropertyPath::set_save_mode() {
745 save_mode = true;
746 }
747
748 void EditorPropertyPath::_notification(int p_what) {
749 switch (p_what) {
750 case NOTIFICATION_THEME_CHANGED: {
751 if (folder) {
752 path_edit->set_button_icon(get_editor_theme_icon(SNAME("FolderBrowse")));
753 } else {
754 path_edit->set_button_icon(get_editor_theme_icon(SNAME("FileBrowse")));
755 }
756 _update_uid_icon();
757 } break;
758 }
759 }
760
761 void EditorPropertyPath::_path_focus_exited() {
762 _path_selected(path->get_text());
763 }
764
765 void EditorPropertyPath::_toggle_uid_display() {
766 display_uid = !display_uid;
767 _update_uid_icon();
768 update_property();
769 }
770
771 void EditorPropertyPath::_update_uid_icon() {
772 toggle_uid->set_button_icon(get_editor_theme_icon(display_uid ? SNAME("UID") : SNAME("NodePath")));
773 }
774
775 void EditorPropertyPath::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
776 const Dictionary drag_data = p_data;
777 if (!drag_data.has("type")) {
778 return;
779 }
780 if (String(drag_data["type"]) != "files") {
781 return;
782 }
783 const Vector<String> filesPaths = drag_data["files"];
784 if (filesPaths.is_empty()) {
785 return;
786 }
787
788 _path_selected(filesPaths[0]);
789 }
790
791 bool EditorPropertyPath::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
792 const Dictionary drag_data = p_data;
793 if (!drag_data.has("type")) {
794 return false;
795 }
796 if (String(drag_data["type"]) != "files") {
797 return false;
798 }
799 const Vector<String> filesPaths = drag_data["files"];
800 if (filesPaths.is_empty()) {
801 return false;
802 }
803
804 for (const String &extension : extensions) {
805 if (filesPaths[0].ends_with(extension.substr(1))) {
806 return true;
807 }
808 }
809
810 return false;
811 }
812
813 EditorPropertyPath::EditorPropertyPath() {
814 HBoxContainer *path_hb = memnew(HBoxContainer);
815 add_child(path_hb);
816 path = memnew(LineEdit);
817 path->set_accessibility_name(TTRC("Path"));
818 SET_DRAG_FORWARDING_CDU(path, EditorPropertyPath);
819 path->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
820 path_hb->add_child(path);
821 path->connect(SceneStringName(text_submitted), callable_mp(this, &EditorPropertyPath::_path_selected));
822 path->connect(SceneStringName(focus_exited), callable_mp(this, &EditorPropertyPath::_path_focus_exited));
823 path->set_h_size_flags(SIZE_EXPAND_FILL);
824
825 toggle_uid = memnew(Button);
826 toggle_uid->set_accessibility_name(TTRC("Toggle Display UID"));
827 toggle_uid->set_tooltip_text(TTRC("Toggles displaying between path and UID.\nThe UID is the actual value of this property."));
828 toggle_uid->set_pressed(false);
829 toggle_uid->set_theme_type_variation(SNAME("EditorInspectorButton"));
830 path_hb->add_child(toggle_uid);
831 add_focusable(toggle_uid);
832 toggle_uid->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyPath::_toggle_uid_display));
833
834 path_edit = memnew(Button);
835 path_edit->set_theme_type_variation(SNAME("EditorInspectorButton"));
836 path_edit->set_accessibility_name(TTRC("Edit"));
837 path_hb->add_child(path_edit);
838 add_focusable(path);
839 path_edit->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyPath::_path_pressed));
840 }
841
842 ///////////////////// CLASS NAME /////////////////////////
843
844 void EditorPropertyClassName::_set_read_only(bool p_read_only) {
845 property->set_disabled(p_read_only);
846 }
847
848 void EditorPropertyClassName::setup(const String &p_base_type, const String &p_selected_type) {
849 base_type = p_base_type;
850 dialog->set_base_type(base_type);
851 selected_type = p_selected_type;
852 property->set_text(selected_type);
853 }
854
855 void EditorPropertyClassName::update_property() {
856 String s = get_edited_property_value();
857 property->set_text(s);
858 selected_type = s;
859 }
860
861 void EditorPropertyClassName::_property_selected() {
862 dialog->popup_create(true, true, get_edited_property_value(), get_edited_property());
863 }
864
865 void EditorPropertyClassName::_dialog_created() {
866 selected_type = dialog->get_selected_type();
867 emit_changed(get_edited_property(), selected_type);
868 update_property();
869 }
870
871 EditorPropertyClassName::EditorPropertyClassName() {
872 property = memnew(Button);
873 property->set_clip_text(true);
874 property->set_theme_type_variation(SNAME("EditorInspectorButton"));
875 add_child(property);
876 add_focusable(property);
877 property->set_text(selected_type);
878 property->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyClassName::_property_selected));
879 dialog = memnew(CreateDialog);
880 dialog->set_base_type(base_type);
881 dialog->connect("create", callable_mp(this, &EditorPropertyClassName::_dialog_created));
882 add_child(dialog);
883 }
884
885 ///////////////////// CHECK /////////////////////////
886
887 void EditorPropertyCheck::_set_read_only(bool p_read_only) {
888 checkbox->set_disabled(p_read_only);
889 }
890
891 void EditorPropertyCheck::_checkbox_pressed() {
892 emit_changed(get_edited_property(), checkbox->is_pressed());
893 }
894
895 void EditorPropertyCheck::update_property() {
896 bool c = get_edited_property_value();
897 checkbox->set_pressed(c);
898 checkbox->set_disabled(is_read_only());
899 }
900
901 EditorPropertyCheck::EditorPropertyCheck() {
902 checkbox = memnew(CheckBox);
903 checkbox->set_text(TTR("On"));
904 add_child(checkbox);
905 add_focusable(checkbox);
906 checkbox->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyCheck::_checkbox_pressed));
907 }
908
909 ///////////////////// ENUM /////////////////////////
910
911 void EditorPropertyEnum::_set_read_only(bool p_read_only) {
912 options->set_disabled(p_read_only);
913 }
914
915 void EditorPropertyEnum::_option_selected(int p_which) {
916 int64_t val = options->get_item_metadata(p_which);
917 emit_changed(get_edited_property(), val);
918 }
919
920 void EditorPropertyEnum::update_property() {
921 Variant current = get_edited_property_value();
922 if (current.get_type() == Variant::NIL) {
923 options->select(-1);
924 options->set_text("<null>");
925 return;
926 }
927
928 int64_t which = current;
929 for (int i = 0; i < options->get_item_count(); i++) {
930 if (which == (int64_t)options->get_item_metadata(i)) {
931 options->select(i);
932 return;
933 }
934 }
935 options->select(-1);
936 options->set_text(itos(which));
937 }
938
939 void EditorPropertyEnum::setup(const Vector<String> &p_options) {
940 options->clear();
941 HashMap<int64_t, Vector<String>> items;
942 int64_t current_val = 0;
943 for (const String &option : p_options) {
944 if (option.get_slice_count(":") != 1) {
945 current_val = option.get_slicec(':', 1).to_int();
946 }
947 items[current_val].push_back(option.get_slicec(':', 0));
948 current_val += 1;
949 }
950
951 for (const KeyValue<int64_t, Vector<String>> &K : items) {
952 options->add_item(String(", ").join(K.value));
953 options->set_item_metadata(-1, K.key);
954 }
955 }
956
957 void EditorPropertyEnum::set_option_button_clip(bool p_enable) {
958 options->set_clip_text(p_enable);
959 }
960
961 OptionButton *EditorPropertyEnum::get_option_button() {
962 return options;
963 }
964
965 EditorPropertyEnum::EditorPropertyEnum() {
966 options = memnew(OptionButton);
967 options->set_clip_text(true);
968 options->set_flat(true);
969 options->set_theme_type_variation(SNAME("EditorInspectorButton"));
970 options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
971 add_child(options);
972 add_focusable(options);
973 options->connect(SceneStringName(item_selected), callable_mp(this, &EditorPropertyEnum::_option_selected));
974 }
975
976 ///////////////////// FLAGS /////////////////////////
977
978 void EditorPropertyFlags::_set_read_only(bool p_read_only) {
979 for (CheckBox *check : flags) {
980 check->set_disabled(p_read_only);
981 }
982 }
983
984 void EditorPropertyFlags::_flag_toggled(int p_index) {
985 uint32_t value = get_edited_property_value();
986 if (flags[p_index]->is_pressed()) {
987 value |= flag_values[p_index];
988 } else {
989 value &= ~flag_values[p_index];
990 }
991
992 emit_changed(get_edited_property(), value);
993 }
994
995 void EditorPropertyFlags::update_property() {
996 uint32_t value = get_edited_property_value();
997
998 for (int i = 0; i < flags.size(); i++) {
999 flags[i]->set_pressed((value & flag_values[i]) == flag_values[i]);
1000 }
1001 }
1002
1003 void EditorPropertyFlags::setup(const Vector<String> &p_options) {
1004 ERR_FAIL_COND(flags.size());
1005
1006 bool first = true;
1007 uint32_t current_val;
1008 for (int i = 0; i < p_options.size(); i++) {
1009 // An empty option is not considered a "flag".
1010 String option = p_options[i].strip_edges();
1011 if (option.is_empty()) {
1012 continue;
1013 }
1014 const int flag_index = flags.size(); // Index of the next element (added by the code below).
1015
1016 // Value for a flag can be explicitly overridden.
1017 Vector<String> text_split = option.split(":");
1018 if (text_split.size() != 1) {
1019 current_val = text_split[1].to_int();
1020 } else {
1021 current_val = 1u << i;
1022 }
1023 flag_values.push_back(current_val);
1024
1025 // Create a CheckBox for the current flag.
1026 CheckBox *cb = memnew(CheckBox);
1027 cb->set_text(text_split[0]);
1028 cb->set_clip_text(true);
1029 cb->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyFlags::_flag_toggled).bind(flag_index));
1030 add_focusable(cb);
1031 vbox->add_child(cb);
1032 flags.push_back(cb);
1033
1034 // Can't use `i == 0` because we want to find the first none-empty option.
1035 if (first) {
1036 set_label_reference(cb);
1037 first = false;
1038 }
1039 }
1040 }
1041
1042 EditorPropertyFlags::EditorPropertyFlags() {
1043 vbox = memnew(VBoxContainer);
1044 vbox->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1045 add_child(vbox);
1046 }
1047
1048 ///////////////////// LAYERS /////////////////////////
1049
1050 void EditorPropertyLayersGrid::_rename_pressed(int p_menu) {
1051 // Show rename popup for active layer.
1052 ERR_FAIL_INDEX(renamed_layer_index, names.size());
1053 String name = names[renamed_layer_index];
1054 rename_dialog->set_title(vformat(TTR("Renaming layer %d:"), renamed_layer_index + 1));
1055 rename_dialog_text->set_text(name);
1056 rename_dialog_text->select(0, name.length());
1057 rename_dialog->popup_centered(Size2(300, 80) * EDSCALE);
1058 rename_dialog_text->grab_focus();
1059 }
1060
1061 void EditorPropertyLayersGrid::_rename_operation_confirm() {
1062 String new_name = rename_dialog_text->get_text().strip_edges();
1063 if (new_name.length() == 0) {
1064 EditorNode::get_singleton()->show_warning(TTR("No name provided."));
1065 return;
1066 } else if (new_name.contains_char('/') || new_name.contains_char('\\') || new_name.contains_char(':')) {
1067 EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters."));
1068 return;
1069 }
1070 names.set(renamed_layer_index, new_name);
1071 tooltips.set(renamed_layer_index, new_name + "\n" + vformat(TTR("Bit %d, value %d"), renamed_layer_index, 1u << renamed_layer_index));
1072 emit_signal(SNAME("rename_confirmed"), renamed_layer_index, new_name);
1073 }
1074
1075 EditorPropertyLayersGrid::EditorPropertyLayersGrid() {
1076 rename_dialog = memnew(ConfirmationDialog);
1077 VBoxContainer *rename_dialog_vb = memnew(VBoxContainer);
1078 rename_dialog->add_child(rename_dialog_vb);
1079 rename_dialog_text = memnew(LineEdit);
1080 rename_dialog_vb->add_margin_child(TTR("Name:"), rename_dialog_text);
1081 rename_dialog->set_ok_button_text(TTR("Rename"));
1082 add_child(rename_dialog);
1083 rename_dialog->register_text_enter(rename_dialog_text);
1084 rename_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorPropertyLayersGrid::_rename_operation_confirm));
1085 layer_rename = memnew(PopupMenu);
1086 layer_rename->add_item(TTR("Rename layer"), 0);
1087 add_child(layer_rename);
1088 layer_rename->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyLayersGrid::_rename_pressed));
1089 }
1090
1091 Size2 EditorPropertyLayersGrid::get_grid_size() const {
1092 Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
1093 int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
1094 return Vector2(0, font->get_height(font_size) * 3);
1095 }
1096
1097 void EditorPropertyLayersGrid::set_read_only(bool p_read_only) {
1098 read_only = p_read_only;
1099 }
1100
1101 Size2 EditorPropertyLayersGrid::get_minimum_size() const {
1102 Size2 min_size = get_grid_size();
1103
1104 // Add extra rows when expanded.
1105 if (expanded) {
1106 const int bsize = (min_size.height * 80 / 100) / 2;
1107 for (int i = 0; i < expansion_rows; ++i) {
1108 min_size.y += 2 * (bsize + 1) + 3;
1109 }
1110 }
1111
1112 return min_size;
1113 }
1114
1115 String EditorPropertyLayersGrid::get_tooltip(const Point2 &p_pos) const {
1116 for (int i = 0; i < flag_rects.size(); i++) {
1117 if (i < tooltips.size() && flag_rects[i].has_point(p_pos)) {
1118 return tooltips[i];
1119 }
1120 }
1121 return String();
1122 }
1123
1124 void EditorPropertyLayersGrid::_update_hovered(const Vector2 &p_position) {
1125 bool expand_was_hovered = expand_hovered;
1126 expand_hovered = expand_rect.has_point(p_position);
1127 if (expand_hovered != expand_was_hovered) {
1128 queue_redraw();
1129 }
1130
1131 if (!expand_hovered) {
1132 for (int i = 0; i < flag_rects.size(); i++) {
1133 if (flag_rects[i].has_point(p_position)) {
1134 // Used to highlight the hovered flag in the layers grid.
1135 hovered_index = i;
1136 queue_redraw();
1137 return;
1138 }
1139 }
1140 }
1141
1142 // Remove highlight when no square is hovered.
1143 if (hovered_index != HOVERED_INDEX_NONE) {
1144 hovered_index = HOVERED_INDEX_NONE;
1145 queue_redraw();
1146 }
1147 }
1148
1149 void EditorPropertyLayersGrid::_on_hover_exit() {
1150 if (expand_hovered) {
1151 expand_hovered = false;
1152 queue_redraw();
1153 }
1154 if (hovered_index != HOVERED_INDEX_NONE) {
1155 hovered_index = HOVERED_INDEX_NONE;
1156 queue_redraw();
1157 }
1158 if (dragging) {
1159 dragging = false;
1160 }
1161 }
1162
1163 void EditorPropertyLayersGrid::_update_flag(bool p_replace) {
1164 if (hovered_index != HOVERED_INDEX_NONE) {
1165 // Toggle the flag.
1166 // We base our choice on the hovered flag, so that it always matches the hovered flag.
1167 if (p_replace) {
1168 // Replace all flags with the hovered flag ("solo mode"),
1169 // instead of toggling the hovered flags while preserving other flags' state.
1170 if (value == 1u << hovered_index) {
1171 // If the flag is already enabled, enable all other items and disable the current flag.
1172 // This allows for quicker toggling.
1173 value = ~value;
1174 } else {
1175 value = 1u << hovered_index;
1176 }
1177 } else {
1178 value ^= 1u << hovered_index;
1179 }
1180
1181 emit_signal(SNAME("flag_changed"), value);
1182 queue_redraw();
1183 } else if (expand_hovered) {
1184 expanded = !expanded;
1185 update_minimum_size();
1186 queue_redraw();
1187 }
1188 }
1189
1190 void EditorPropertyLayersGrid::gui_input(const Ref<InputEvent> &p_ev) {
1191 if (read_only) {
1192 return;
1193 }
1194 const Ref<InputEventMouseMotion> mm = p_ev;
1195 if (mm.is_valid()) {
1196 _update_hovered(mm->get_position());
1197 if (dragging && hovered_index != HOVERED_INDEX_NONE && dragging_value_to_set != bool(value & (1u << hovered_index))) {
1198 value ^= 1u << hovered_index;
1199 emit_signal(SNAME("flag_changed"), value);
1200 queue_redraw();
1201 }
1202 return;
1203 }
1204
1205 const Ref<InputEventMouseButton> mb = p_ev;
1206 if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
1207 _update_hovered(mb->get_position());
1208 bool replace_mode = mb->is_command_or_control_pressed();
1209 _update_flag(replace_mode);
1210 if (!replace_mode && hovered_index != HOVERED_INDEX_NONE) {
1211 dragging = true;
1212 dragging_value_to_set = bool(value & (1u << hovered_index));
1213 }
1214 }
1215 if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
1216 dragging = false;
1217 }
1218 if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
1219 if (hovered_index != HOVERED_INDEX_NONE) {
1220 renamed_layer_index = hovered_index;
1221 layer_rename->set_position(get_screen_position() + mb->get_position());
1222 layer_rename->reset_size();
1223 layer_rename->popup();
1224 }
1225 }
1226 }
1227
1228 void EditorPropertyLayersGrid::_notification(int p_what) {
1229 switch (p_what) {
1230 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
1231 RID ae = get_accessibility_element();
1232 ERR_FAIL_COND(ae.is_null());
1233
1234 //TODO
1235 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_STATIC_TEXT);
1236 DisplayServer::get_singleton()->accessibility_update_set_value(ae, TTR(vformat("The %s is not accessible at this time.", "Layers grid property editor")));
1237 } break;
1238
1239 case NOTIFICATION_DRAW: {
1240 Size2 grid_size = get_grid_size();
1241 grid_size.x = get_size().x;
1242
1243 flag_rects.clear();
1244
1245 int prev_expansion_rows = expansion_rows;
1246 expansion_rows = 0;
1247
1248 const int bsize = (grid_size.height * 80 / 100) / 2;
1249 const int h = bsize * 2 + 1;
1250
1251 Color color = get_theme_color(read_only ? SNAME("highlight_disabled_color") : SNAME("highlight_color"), EditorStringName(Editor));
1252
1253 Color text_color = get_theme_color(read_only ? SNAME("font_disabled_color") : SceneStringName(font_color), EditorStringName(Editor));
1254 text_color.a *= 0.5;
1255
1256 Color text_color_on = get_theme_color(read_only ? SNAME("font_disabled_color") : SNAME("font_hover_color"), EditorStringName(Editor));
1257 text_color_on.a *= 0.7;
1258
1259 const int vofs = (grid_size.height - h) / 2;
1260
1261 uint32_t layer_index = 0;
1262
1263 Point2 arrow_pos;
1264
1265 Point2 block_ofs(4, vofs);
1266
1267 while (true) {
1268 Point2 ofs = block_ofs;
1269
1270 for (int i = 0; i < 2; i++) {
1271 for (int j = 0; j < layer_group_size; j++) {
1272 const bool on = value & (1u << layer_index);
1273 Rect2 rect2 = Rect2(ofs, Size2(bsize, bsize));
1274
1275 color.a = on ? 0.6 : 0.2;
1276 if (layer_index == hovered_index) {
1277 // Add visual feedback when hovering a flag.
1278 color.a += 0.15;
1279 }
1280
1281 draw_rect(rect2, color);
1282 flag_rects.push_back(rect2);
1283
1284 Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
1285 int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
1286 Vector2 offset;
1287 offset.y = rect2.size.y * 0.75;
1288
1289 draw_string(font, rect2.position + offset, itos(layer_index + 1), HORIZONTAL_ALIGNMENT_CENTER, rect2.size.x, font_size, on ? text_color_on : text_color);
1290
1291 ofs.x += bsize + 1;
1292
1293 ++layer_index;
1294 }
1295
1296 ofs.x = block_ofs.x;
1297 ofs.y += bsize + 1;
1298 }
1299
1300 if (layer_index >= layer_count) {
1301 if (!flag_rects.is_empty() && (expansion_rows == 0)) {
1302 const Rect2 &last_rect = flag_rects[flag_rects.size() - 1];
1303 arrow_pos = last_rect.get_end();
1304 }
1305 break;
1306 }
1307
1308 int block_size_x = layer_group_size * (bsize + 1);
1309 block_ofs.x += block_size_x + 3;
1310
1311 if (block_ofs.x + block_size_x + 12 > grid_size.width) {
1312 // Keep last valid cell position for the expansion icon.
1313 if (!flag_rects.is_empty() && (expansion_rows == 0)) {
1314 const Rect2 &last_rect = flag_rects[flag_rects.size() - 1];
1315 arrow_pos = last_rect.get_end();
1316 }
1317 ++expansion_rows;
1318
1319 if (expanded) {
1320 // Expand grid to next line.
1321 block_ofs.x = 4;
1322 block_ofs.y += 2 * (bsize + 1) + 3;
1323 } else {
1324 // Skip remaining blocks.
1325 break;
1326 }
1327 }
1328 }
1329
1330 if ((expansion_rows != prev_expansion_rows) && expanded) {
1331 update_minimum_size();
1332 }
1333
1334 if ((expansion_rows == 0) && (layer_index == layer_count)) {
1335 // Whole grid was drawn, no need for expansion icon.
1336 break;
1337 }
1338
1339 Ref<Texture2D> arrow = get_theme_icon(SNAME("arrow"), SNAME("Tree"));
1340 ERR_FAIL_COND(arrow.is_null());
1341
1342 Color arrow_color = get_theme_color(SNAME("highlight_color"), EditorStringName(Editor));
1343 arrow_color.a = expand_hovered ? 1.0 : 0.6;
1344
1345 arrow_pos.x += 2.0;
1346 arrow_pos.y -= arrow->get_height();
1347
1348 Rect2 arrow_draw_rect(arrow_pos, arrow->get_size());
1349 expand_rect = arrow_draw_rect;
1350 if (expanded) {
1351 arrow_draw_rect.size.y *= -1.0; // Flip arrow vertically when expanded.
1352 }
1353
1354 RID ci = get_canvas_item();
1355 arrow->draw_rect(ci, arrow_draw_rect, false, arrow_color);
1356
1357 } break;
1358
1359 case NOTIFICATION_MOUSE_EXIT: {
1360 _on_hover_exit();
1361 } break;
1362 }
1363 }
1364
1365 void EditorPropertyLayersGrid::set_flag(uint32_t p_flag) {
1366 value = p_flag;
1367 queue_redraw();
1368 }
1369
1370 void EditorPropertyLayersGrid::_bind_methods() {
1371 ADD_SIGNAL(MethodInfo("flag_changed", PropertyInfo(Variant::INT, "flag")));
1372 ADD_SIGNAL(MethodInfo("rename_confirmed", PropertyInfo(Variant::INT, "layer_id"), PropertyInfo(Variant::STRING, "new_name")));
1373 }
1374
1375 void EditorPropertyLayers::_notification(int p_what) {
1376 switch (p_what) {
1377 case NOTIFICATION_THEME_CHANGED: {
1378 button->set_texture_normal(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
1379 button->set_texture_pressed(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
1380 button->set_texture_disabled(get_editor_theme_icon(SNAME("GuiTabMenu")));
1381 } break;
1382 }
1383 }
1384
1385 void EditorPropertyLayers::_set_read_only(bool p_read_only) {
1386 button->set_disabled(p_read_only);
1387 grid->set_read_only(p_read_only);
1388 }
1389
1390 void EditorPropertyLayers::_grid_changed(uint32_t p_grid) {
1391 emit_changed(get_edited_property(), p_grid);
1392 }
1393
1394 void EditorPropertyLayers::update_property() {
1395 uint32_t value = get_edited_property_value();
1396
1397 grid->set_flag(value);
1398 }
1399
1400 void EditorPropertyLayers::setup(LayerType p_layer_type) {
1401 layer_type = p_layer_type;
1402 int layer_group_size = 0;
1403 int layer_count = 0;
1404 switch (p_layer_type) {
1405 case LAYER_RENDER_2D: {
1406 basename = "layer_names/2d_render";
1407 layer_group_size = 5;
1408 layer_count = 20;
1409 } break;
1410
1411 case LAYER_PHYSICS_2D: {
1412 basename = "layer_names/2d_physics";
1413 layer_group_size = 4;
1414 layer_count = 32;
1415 } break;
1416
1417 case LAYER_NAVIGATION_2D: {
1418 basename = "layer_names/2d_navigation";
1419 layer_group_size = 4;
1420 layer_count = 32;
1421 } break;
1422
1423 case LAYER_RENDER_3D: {
1424 basename = "layer_names/3d_render";
1425 layer_group_size = 5;
1426 layer_count = 20;
1427 } break;
1428
1429 case LAYER_PHYSICS_3D: {
1430 basename = "layer_names/3d_physics";
1431 layer_group_size = 4;
1432 layer_count = 32;
1433 } break;
1434
1435 case LAYER_NAVIGATION_3D: {
1436 basename = "layer_names/3d_navigation";
1437 layer_group_size = 4;
1438 layer_count = 32;
1439 } break;
1440
1441 case LAYER_AVOIDANCE: {
1442 basename = "layer_names/avoidance";
1443 layer_group_size = 4;
1444 layer_count = 32;
1445 } break;
1446 }
1447
1448 Vector<String> names;
1449 Vector<String> tooltips;
1450 for (int i = 0; i < layer_count; i++) {
1451 String name;
1452
1453 if (ProjectSettings::get_singleton()->has_setting(basename + vformat("/layer_%d", i + 1))) {
1454 name = GLOBAL_GET(basename + vformat("/layer_%d", i + 1));
1455 }
1456
1457 if (name.is_empty()) {
1458 name = vformat(TTR("Layer %d"), i + 1);
1459 }
1460
1461 names.push_back(name);
1462 tooltips.push_back(name + "\n" + vformat(TTR("Bit %d, value %d"), i, 1u << i));
1463 }
1464
1465 grid->names = names;
1466 grid->tooltips = tooltips;
1467 grid->layer_group_size = layer_group_size;
1468 grid->layer_count = layer_count;
1469 }
1470
1471 void EditorPropertyLayers::set_layer_name(int p_index, const String &p_name) {
1472 const String property_name = basename + vformat("/layer_%d", p_index + 1);
1473 if (ProjectSettings::get_singleton()->has_setting(property_name)) {
1474 ProjectSettings::get_singleton()->set(property_name, p_name);
1475 ProjectSettings::get_singleton()->save();
1476 }
1477 }
1478
1479 String EditorPropertyLayers::get_layer_name(int p_index) const {
1480 const String property_name = basename + vformat("/layer_%d", p_index + 1);
1481 if (ProjectSettings::get_singleton()->has_setting(property_name)) {
1482 return GLOBAL_GET(property_name);
1483 }
1484 return String();
1485 }
1486
1487 void EditorPropertyLayers::_button_pressed() {
1488 int layer_count = grid->layer_count;
1489 layers->clear();
1490 for (int i = 0; i < layer_count; i++) {
1491 const String name = get_layer_name(i);
1492 if (name.is_empty()) {
1493 continue;
1494 }
1495 layers->add_check_item(name, i);
1496 int idx = layers->get_item_index(i);
1497 layers->set_item_checked(idx, grid->value & (1u << i));
1498 }
1499
1500 if (layers->get_item_count() == 0) {
1501 layers->add_item(TTR("No Named Layers"));
1502 layers->set_item_disabled(0, true);
1503 }
1504 layers->add_separator();
1505 layers->add_icon_item(get_editor_theme_icon("Edit"), TTR("Edit Layer Names"), grid->layer_count);
1506
1507 Rect2 gp = button->get_screen_rect();
1508 layers->reset_size();
1509 Vector2 popup_pos = gp.position - Vector2(layers->get_contents_minimum_size().x, 0);
1510 layers->set_position(popup_pos);
1511 layers->popup();
1512 }
1513
1514 void EditorPropertyLayers::_menu_pressed(int p_menu) {
1515 if (uint32_t(p_menu) == grid->layer_count) {
1516 ProjectSettingsEditor::get_singleton()->popup_project_settings(true);
1517 ProjectSettingsEditor::get_singleton()->set_general_page(basename);
1518 } else {
1519 grid->value ^= 1u << p_menu;
1520 grid->queue_redraw();
1521 layers->set_item_checked(layers->get_item_index(p_menu), grid->value & (1u << p_menu));
1522 _grid_changed(grid->value);
1523 }
1524 }
1525
1526 void EditorPropertyLayers::_refresh_names() {
1527 setup(layer_type);
1528 }
1529
1530 EditorPropertyLayers::EditorPropertyLayers() {
1531 HBoxContainer *hb = memnew(HBoxContainer);
1532 hb->set_clip_contents(true);
1533 add_child(hb);
1534 grid = memnew(EditorPropertyLayersGrid);
1535 grid->connect("flag_changed", callable_mp(this, &EditorPropertyLayers::_grid_changed));
1536 grid->connect("rename_confirmed", callable_mp(this, &EditorPropertyLayers::set_layer_name));
1537 grid->set_h_size_flags(SIZE_EXPAND_FILL);
1538 hb->add_child(grid);
1539
1540 button = memnew(TextureButton);
1541 button->set_accessibility_name(TTRC("Layers"));
1542 button->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED);
1543 button->set_toggle_mode(true);
1544 button->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyLayers::_button_pressed));
1545 hb->add_child(button);
1546
1547 set_bottom_editor(hb);
1548
1549 layers = memnew(PopupMenu);
1550 add_child(layers);
1551 layers->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1552 layers->set_hide_on_checkable_item_selection(false);
1553 layers->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyLayers::_menu_pressed));
1554 layers->connect("popup_hide", callable_mp((BaseButton *)button, &BaseButton::set_pressed).bind(false));
1555 ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &EditorPropertyLayers::_refresh_names));
1556 }
1557
1558 ///////////////////// INT /////////////////////////
1559
1560 void EditorPropertyInteger::_set_read_only(bool p_read_only) {
1561 spin->set_read_only(p_read_only);
1562 }
1563
1564 void EditorPropertyInteger::_value_changed(int64_t val) {
1565 emit_changed(get_edited_property(), val);
1566 }
1567
1568 void EditorPropertyInteger::update_property() {
1569 int64_t val = get_edited_property_display_value();
1570 spin->set_value_no_signal(val);
1571 #ifdef DEBUG_ENABLED
1572 // If spin (currently EditorSplinSlider : Range) is changed so that it can use int64_t, then the below warning wouldn't be a problem.
1573 if (val != (int64_t)(double)(val)) {
1574 WARN_PRINT("Cannot reliably represent '" + itos(val) + "' in the inspector, value is too large.");
1575 }
1576 #endif
1577 }
1578
1579 void EditorPropertyInteger::setup(const EditorPropertyRangeHint &p_range_hint) {
1580 spin->set_min(p_range_hint.min);
1581 spin->set_max(p_range_hint.max);
1582 spin->set_step(Math::round(p_range_hint.step));
1583 if (p_range_hint.hide_control) {
1584 spin->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);
1585 } else {
1586 spin->set_control_state(p_range_hint.prefer_slider ? EditorSpinSlider::CONTROL_STATE_PREFER_SLIDER : EditorSpinSlider::CONTROL_STATE_DEFAULT);
1587 }
1588 spin->set_allow_greater(p_range_hint.or_greater);
1589 spin->set_allow_lesser(p_range_hint.or_less);
1590 spin->set_suffix(p_range_hint.suffix);
1591 }
1592
1593 EditorPropertyInteger::EditorPropertyInteger() {
1594 spin = memnew(EditorSpinSlider);
1595 spin->set_flat(true);
1596 spin->set_editing_integer(true);
1597 add_child(spin);
1598 add_focusable(spin);
1599 spin->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyInteger::_value_changed));
1600 }
1601
1602 ///////////////////// OBJECT ID /////////////////////////
1603
1604 void EditorPropertyObjectID::_set_read_only(bool p_read_only) {
1605 edit->set_disabled(p_read_only);
1606 }
1607
1608 void EditorPropertyObjectID::_edit_pressed() {
1609 emit_signal(SNAME("object_id_selected"), get_edited_property(), get_edited_property_value());
1610 }
1611
1612 void EditorPropertyObjectID::update_property() {
1613 String type = base_type;
1614 if (type.is_empty()) {
1615 type = "Object";
1616 }
1617
1618 ObjectID id = get_edited_property_value();
1619 if (id.is_valid()) {
1620 edit->set_text(type + " ID: " + uitos(id));
1621 edit->set_tooltip_text(type + " ID: " + uitos(id));
1622 edit->set_disabled(false);
1623 edit->set_button_icon(EditorNode::get_singleton()->get_class_icon(type));
1624 } else {
1625 edit->set_text(TTR("<empty>"));
1626 edit->set_tooltip_text("");
1627 edit->set_disabled(true);
1628 edit->set_button_icon(Ref<Texture2D>());
1629 }
1630 }
1631
1632 void EditorPropertyObjectID::setup(const String &p_base_type) {
1633 base_type = p_base_type;
1634 }
1635
1636 EditorPropertyObjectID::EditorPropertyObjectID() {
1637 edit = memnew(Button);
1638 edit->set_theme_type_variation(SNAME("EditorInspectorButton"));
1639 edit->set_accessibility_name(TTRC("Edit"));
1640 add_child(edit);
1641 add_focusable(edit);
1642 edit->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
1643 edit->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyObjectID::_edit_pressed));
1644 }
1645
1646 ///////////////////// SIGNAL /////////////////////////
1647
1648 void EditorPropertySignal::_edit_pressed() {
1649 Signal signal = get_edited_property_value();
1650 emit_signal(SNAME("object_id_selected"), get_edited_property(), signal.get_object_id());
1651 }
1652
1653 void EditorPropertySignal::update_property() {
1654 String type = base_type;
1655
1656 Signal signal = get_edited_property_value();
1657
1658 edit->set_text("Signal: " + signal.get_name());
1659 edit->set_disabled(false);
1660 edit->set_button_icon(get_editor_theme_icon(SNAME("Signals")));
1661 }
1662
1663 EditorPropertySignal::EditorPropertySignal() {
1664 edit = memnew(Button);
1665 edit->set_theme_type_variation(SNAME("EditorInspectorButton"));
1666 edit->set_accessibility_name(TTRC("Edit"));
1667 add_child(edit);
1668 add_focusable(edit);
1669 edit->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertySignal::_edit_pressed));
1670 }
1671
1672 ///////////////////// CALLABLE /////////////////////////
1673
1674 void EditorPropertyCallable::update_property() {
1675 String type = base_type;
1676
1677 Callable callable = get_edited_property_value();
1678
1679 edit->set_text("Callable");
1680 edit->set_disabled(true);
1681 edit->set_button_icon(get_editor_theme_icon(SNAME("Callable")));
1682 }
1683
1684 EditorPropertyCallable::EditorPropertyCallable() {
1685 edit = memnew(Button);
1686 edit->set_theme_type_variation(SNAME("EditorInspectorButton"));
1687 edit->set_accessibility_name(TTRC("Edit"));
1688 add_child(edit);
1689 add_focusable(edit);
1690 }
1691
1692 ///////////////////// FLOAT /////////////////////////
1693
1694 void EditorPropertyFloat::_set_read_only(bool p_read_only) {
1695 spin->set_read_only(p_read_only);
1696 }
1697
1698 void EditorPropertyFloat::_value_changed(double val) {
1699 if (radians_as_degrees) {
1700 val = Math::deg_to_rad(val);
1701 }
1702 emit_changed(get_edited_property(), val);
1703 }
1704
1705 void EditorPropertyFloat::update_property() {
1706 double val = get_edited_property_value();
1707 if (radians_as_degrees) {
1708 val = Math::rad_to_deg(val);
1709 }
1710 spin->set_value_no_signal(val);
1711 }
1712
1713 void EditorPropertyFloat::setup(const EditorPropertyRangeHint &p_range_hint) {
1714 radians_as_degrees = p_range_hint.radians_as_degrees;
1715 spin->set_min(p_range_hint.min);
1716 spin->set_max(p_range_hint.max);
1717 spin->set_step(p_range_hint.step);
1718 if (p_range_hint.hide_control) {
1719 spin->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);
1720 }
1721 spin->set_exp_ratio(p_range_hint.exp_range);
1722 spin->set_allow_greater(p_range_hint.or_greater);
1723 spin->set_allow_lesser(p_range_hint.or_less);
1724 spin->set_suffix(p_range_hint.suffix);
1725 }
1726
1727 EditorPropertyFloat::EditorPropertyFloat() {
1728 spin = memnew(EditorSpinSlider);
1729 spin->set_flat(true);
1730 add_child(spin);
1731 add_focusable(spin);
1732 spin->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyFloat::_value_changed));
1733 }
1734
1735 ///////////////////// EASING /////////////////////////
1736
1737 void EditorPropertyEasing::_set_read_only(bool p_read_only) {
1738 spin->set_read_only(p_read_only);
1739 }
1740
1741 void EditorPropertyEasing::_drag_easing(const Ref<InputEvent> &p_ev) {
1742 if (is_read_only()) {
1743 return;
1744 }
1745 const Ref<InputEventMouseButton> mb = p_ev;
1746 if (mb.is_valid()) {
1747 if (mb->is_double_click() && mb->get_button_index() == MouseButton::LEFT) {
1748 _setup_spin();
1749 }
1750
1751 if (mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
1752 preset->set_position(easing_draw->get_screen_position() + mb->get_position());
1753 preset->reset_size();
1754 preset->popup();
1755
1756 // Ensure the easing doesn't appear as being dragged
1757 dragging = false;
1758 easing_draw->queue_redraw();
1759 }
1760
1761 if (mb->get_button_index() == MouseButton::LEFT) {
1762 dragging = mb->is_pressed();
1763 // Update to display the correct dragging color
1764 easing_draw->queue_redraw();
1765 }
1766 }
1767
1768 const Ref<InputEventMouseMotion> mm = p_ev;
1769
1770 if (dragging && mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
1771 float rel = mm->get_relative().x;
1772 if (rel == 0) {
1773 return;
1774 }
1775
1776 if (flip) {
1777 rel = -rel;
1778 }
1779
1780 float val = get_edited_property_value();
1781 bool sg = val < 0;
1782 val = Math::abs(val);
1783
1784 val = Math::log(val) / Math::log((float)2.0);
1785 // Logarithmic space.
1786 val += rel * 0.05;
1787
1788 val = Math::pow(2.0f, val);
1789 if (sg) {
1790 val = -val;
1791 }
1792
1793 // 0 is a singularity, but both positive and negative values
1794 // are otherwise allowed. Enforce 0+ as workaround.
1795 if (Math::is_zero_approx(val)) {
1796 val = 0.00001;
1797 }
1798
1799 // Limit to a reasonable value to prevent the curve going into infinity,
1800 // which can cause crashes and other issues.
1801 val = CLAMP(val, -1'000'000, 1'000'000);
1802
1803 emit_changed(get_edited_property(), val);
1804 }
1805 }
1806
1807 void EditorPropertyEasing::_draw_easing() {
1808 RID ci = easing_draw->get_canvas_item();
1809
1810 Size2 s = easing_draw->get_size();
1811
1812 const int point_count = 48;
1813
1814 const float exp = get_edited_property_value();
1815
1816 const Ref<Font> f = get_theme_font(SceneStringName(font), SNAME("Label"));
1817 int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
1818 const Color font_color = get_theme_color(is_read_only() ? SNAME("font_uneditable_color") : SceneStringName(font_color), SNAME("LineEdit"));
1819 Color line_color;
1820 if (dragging) {
1821 line_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
1822 } else {
1823 line_color = get_theme_color(is_read_only() ? SNAME("font_uneditable_color") : SceneStringName(font_color), SNAME("LineEdit")) * Color(1, 1, 1, 0.9);
1824 }
1825
1826 Vector<Point2> points;
1827 for (int i = 0; i <= point_count; i++) {
1828 float ifl = i / float(point_count);
1829
1830 const float h = 1.0 - Math::ease(ifl, exp);
1831
1832 if (flip) {
1833 ifl = 1.0 - ifl;
1834 }
1835
1836 points.push_back(Point2(ifl * s.width, h * s.height));
1837 }
1838
1839 easing_draw->draw_polyline(points, line_color, 1.0, true);
1840 // Draw more decimals for small numbers since higher precision is usually required for fine adjustments.
1841 int decimals;
1842 if (Math::abs(exp) < 0.1 - CMP_EPSILON) {
1843 decimals = 4;
1844 } else if (Math::abs(exp) < 1 - CMP_EPSILON) {
1845 decimals = 3;
1846 } else if (Math::abs(exp) < 10 - CMP_EPSILON) {
1847 decimals = 2;
1848 } else {
1849 decimals = 1;
1850 }
1851
1852 const String &formatted = TranslationServer::get_singleton()->format_number(rtos(exp).pad_decimals(decimals), _get_locale());
1853 f->draw_string(ci, Point2(10, 10 + f->get_ascent(font_size)), formatted, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color);
1854 }
1855
1856 void EditorPropertyEasing::update_property() {
1857 float val = get_edited_property_value();
1858 spin->set_value_no_signal(val);
1859
1860 easing_draw->queue_redraw();
1861 }
1862
1863 void EditorPropertyEasing::_set_preset(int p_preset) {
1864 static const float preset_value[EASING_MAX] = { 0.0, 1.0, 2.0, 0.5, -2.0, -0.5 };
1865
1866 emit_changed(get_edited_property(), preset_value[p_preset]);
1867 }
1868
1869 void EditorPropertyEasing::_setup_spin() {
1870 spin->setup_and_show();
1871 spin->get_line_edit()->set_text(TranslationServer::get_singleton()->format_number(rtos(get_edited_property_value()), _get_locale()));
1872 spin->show();
1873 }
1874
1875 void EditorPropertyEasing::_spin_value_changed(double p_value) {
1876 // Limit to a reasonable value to prevent the curve going into infinity,
1877 // which can cause crashes and other issues.
1878 p_value = CLAMP(p_value, -1'000'000, 1'000'000);
1879
1880 if (positive_only) {
1881 // Force a positive or zero value if a negative value was manually entered by double-clicking.
1882 p_value = MAX(0.0, p_value);
1883 }
1884
1885 emit_changed(get_edited_property(), p_value);
1886 _spin_focus_exited();
1887 }
1888
1889 void EditorPropertyEasing::_spin_focus_exited() {
1890 spin->hide();
1891 // Ensure the easing doesn't appear as being dragged
1892 dragging = false;
1893 easing_draw->queue_redraw();
1894 }
1895
1896 void EditorPropertyEasing::setup(bool p_positive_only, bool p_flip) {
1897 flip = p_flip;
1898 positive_only = p_positive_only;
1899
1900 // Names need translation context, so they are set in NOTIFICATION_TRANSLATION_CHANGED.
1901 preset->add_item("", EASING_LINEAR);
1902 preset->add_item("", EASING_IN);
1903 preset->add_item("", EASING_OUT);
1904 preset->add_item("", EASING_ZERO);
1905 if (!positive_only) {
1906 preset->add_item("", EASING_IN_OUT);
1907 preset->add_item("", EASING_OUT_IN);
1908 }
1909 }
1910
1911 void EditorPropertyEasing::_notification(int p_what) {
1912 switch (p_what) {
1913 case NOTIFICATION_THEME_CHANGED: {
1914 preset->set_item_icon(preset->get_item_index(EASING_LINEAR), get_editor_theme_icon(SNAME("CurveLinear")));
1915 preset->set_item_icon(preset->get_item_index(EASING_IN), get_editor_theme_icon(SNAME("CurveIn")));
1916 preset->set_item_icon(preset->get_item_index(EASING_OUT), get_editor_theme_icon(SNAME("CurveOut")));
1917 preset->set_item_icon(preset->get_item_index(EASING_ZERO), get_editor_theme_icon(SNAME("CurveConstant")));
1918 if (!positive_only) {
1919 preset->set_item_icon(preset->get_item_index(EASING_IN_OUT), get_editor_theme_icon(SNAME("CurveInOut")));
1920 preset->set_item_icon(preset->get_item_index(EASING_OUT_IN), get_editor_theme_icon(SNAME("CurveOutIn")));
1921 }
1922 easing_draw->set_custom_minimum_size(Size2(0, get_theme_font(SceneStringName(font), SNAME("Label"))->get_height(get_theme_font_size(SceneStringName(font_size), SNAME("Label"))) * 2));
1923 } break;
1924
1925 case NOTIFICATION_TRANSLATION_CHANGED: {
1926 preset->set_item_text(preset->get_item_index(EASING_LINEAR), TTR("Linear", "Ease Type"));
1927 preset->set_item_text(preset->get_item_index(EASING_IN), TTR("Ease In", "Ease Type"));
1928 preset->set_item_text(preset->get_item_index(EASING_OUT), TTR("Ease Out", "Ease Type"));
1929 preset->set_item_text(preset->get_item_index(EASING_ZERO), TTR("Zero", "Ease Type"));
1930 if (!positive_only) {
1931 preset->set_item_text(preset->get_item_index(EASING_IN_OUT), TTR("Ease In-Out", "Ease Type"));
1932 preset->set_item_text(preset->get_item_index(EASING_OUT_IN), TTR("Ease Out-In", "Ease Type"));
1933 }
1934 } break;
1935 }
1936 }
1937
1938 EditorPropertyEasing::EditorPropertyEasing() {
1939 easing_draw = memnew(Control);
1940 easing_draw->connect(SceneStringName(draw), callable_mp(this, &EditorPropertyEasing::_draw_easing));
1941 easing_draw->connect(SceneStringName(gui_input), callable_mp(this, &EditorPropertyEasing::_drag_easing));
1942 easing_draw->set_default_cursor_shape(Control::CURSOR_MOVE);
1943 add_child(easing_draw);
1944
1945 preset = memnew(PopupMenu);
1946 preset->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1947 add_child(preset);
1948 preset->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyEasing::_set_preset));
1949
1950 spin = memnew(EditorSpinSlider);
1951 spin->set_flat(true);
1952 spin->set_min(-100);
1953 spin->set_max(100);
1954 spin->set_step(0);
1955 spin->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);
1956 spin->set_allow_lesser(true);
1957 spin->set_allow_greater(true);
1958 spin->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyEasing::_spin_value_changed));
1959 spin->get_line_edit()->connect(SceneStringName(focus_exited), callable_mp(this, &EditorPropertyEasing::_spin_focus_exited));
1960 spin->hide();
1961 add_child(spin);
1962 }
1963
1964 ///////////////////// RECT2 /////////////////////////
1965
1966 void EditorPropertyRect2::_set_read_only(bool p_read_only) {
1967 for (int i = 0; i < 4; i++) {
1968 spin[i]->set_read_only(p_read_only);
1969 }
1970 }
1971
1972 void EditorPropertyRect2::_value_changed(double val, const String &p_name) {
1973 Rect2 r2;
1974 r2.position.x = spin[0]->get_value();
1975 r2.position.y = spin[1]->get_value();
1976 r2.size.x = spin[2]->get_value();
1977 r2.size.y = spin[3]->get_value();
1978 emit_changed(get_edited_property(), r2, p_name);
1979 }
1980
1981 void EditorPropertyRect2::update_property() {
1982 Rect2 val = get_edited_property_value();
1983 spin[0]->set_value_no_signal(val.position.x);
1984 spin[1]->set_value_no_signal(val.position.y);
1985 spin[2]->set_value_no_signal(val.size.x);
1986 spin[3]->set_value_no_signal(val.size.y);
1987 }
1988
1989 void EditorPropertyRect2::_notification(int p_what) {
1990 switch (p_what) {
1991 case NOTIFICATION_THEME_CHANGED: {
1992 const Color *colors = _get_property_colors();
1993 for (int i = 0; i < 4; i++) {
1994 spin[i]->add_theme_color_override("label_color", colors[i % 2]);
1995 }
1996 } break;
1997 }
1998 }
1999
2000 void EditorPropertyRect2::setup(const EditorPropertyRangeHint &p_range_hint) {
2001 for (int i = 0; i < 4; i++) {
2002 spin[i]->set_min(p_range_hint.min);
2003 spin[i]->set_max(p_range_hint.max);
2004 spin[i]->set_step(p_range_hint.step);
2005 if (p_range_hint.hide_control) {
2006 spin[i]->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);
2007 }
2008 spin[i]->set_allow_greater(true);
2009 spin[i]->set_allow_lesser(true);
2010 spin[i]->set_suffix(p_range_hint.suffix);
2011 }
2012 }
2013
2014 EditorPropertyRect2::EditorPropertyRect2(bool p_force_wide) {
2015 bool horizontal = p_force_wide || bool(EDITOR_GET("interface/inspector/horizontal_vector_types_editing"));
2016 bool grid = false;
2017 BoxContainer *bc;
2018
2019 if (p_force_wide) {
2020 bc = memnew(HBoxContainer);
2021 add_child(bc);
2022 } else if (horizontal) {
2023 bc = memnew(VBoxContainer);
2024 add_child(bc);
2025 set_bottom_editor(bc);
2026
2027 bc->add_child(memnew(HBoxContainer));
2028 bc->add_child(memnew(HBoxContainer));
2029 grid = true;
2030 } else {
2031 bc = memnew(VBoxContainer);
2032 add_child(bc);
2033 }
2034
2035 static const char *desc[4] = { "x", "y", "w", "h" };
2036 for (int i = 0; i < 4; i++) {
2037 spin[i] = memnew(EditorSpinSlider);
2038 spin[i]->set_label(desc[i]);
2039 spin[i]->set_accessibility_name(desc[i]);
2040 spin[i]->set_flat(true);
2041
2042 if (grid) {
2043 bc->get_child(i / 2)->add_child(spin[i]);
2044 } else {
2045 bc->add_child(spin[i]);
2046 }
2047
2048 add_focusable(spin[i]);
2049 spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyRect2::_value_changed).bind(desc[i]));
2050 if (horizontal) {
2051 spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
2052 }
2053 }
2054
2055 if (!horizontal) {
2056 set_label_reference(spin[0]); //show text and buttons around this
2057 }
2058 }
2059
2060 ///////////////////// RECT2i /////////////////////////
2061
2062 void EditorPropertyRect2i::_set_read_only(bool p_read_only) {
2063 for (int i = 0; i < 4; i++) {
2064 spin[i]->set_read_only(p_read_only);
2065 }
2066 }
2067
2068 void EditorPropertyRect2i::_value_changed(double val, const String &p_name) {
2069 Rect2i r2;
2070 r2.position.x = spin[0]->get_value();
2071 r2.position.y = spin[1]->get_value();
2072 r2.size.x = spin[2]->get_value();
2073 r2.size.y = spin[3]->get_value();
2074 emit_changed(get_edited_property(), r2, p_name);
2075 }
2076
2077 void EditorPropertyRect2i::update_property() {
2078 Rect2i val = get_edited_property_value();
2079 spin[0]->set_value_no_signal(val.position.x);
2080 spin[1]->set_value_no_signal(val.position.y);
2081 spin[2]->set_value_no_signal(val.size.x);
2082 spin[3]->set_value_no_signal(val.size.y);
2083 }
2084
2085 void EditorPropertyRect2i::_notification(int p_what) {
2086 switch (p_what) {
2087 case NOTIFICATION_THEME_CHANGED: {
2088 const Color *colors = _get_property_colors();
2089 for (int i = 0; i < 4; i++) {
2090 spin[i]->add_theme_color_override("label_color", colors[i % 2]);
2091 }
2092 } break;
2093 }
2094 }
2095
2096 void EditorPropertyRect2i::setup(const EditorPropertyRangeHint &p_range_hint) {
2097 for (int i = 0; i < 4; i++) {
2098 spin[i]->set_min(p_range_hint.min);
2099 spin[i]->set_max(p_range_hint.max);
2100 spin[i]->set_step(1);
2101 spin[i]->set_allow_greater(true);
2102 spin[i]->set_allow_lesser(true);
2103 spin[i]->set_suffix(p_range_hint.suffix);
2104 spin[i]->set_editing_integer(true);
2105 }
2106 }
2107
2108 EditorPropertyRect2i::EditorPropertyRect2i(bool p_force_wide) {
2109 bool horizontal = p_force_wide || bool(EDITOR_GET("interface/inspector/horizontal_vector_types_editing"));
2110 bool grid = false;
2111 BoxContainer *bc;
2112
2113 if (p_force_wide) {
2114 bc = memnew(HBoxContainer);
2115 add_child(bc);
2116 } else if (horizontal) {
2117 bc = memnew(VBoxContainer);
2118 add_child(bc);
2119 set_bottom_editor(bc);
2120
2121 bc->add_child(memnew(HBoxContainer));
2122 bc->add_child(memnew(HBoxContainer));
2123 grid = true;
2124 } else {
2125 bc = memnew(VBoxContainer);
2126 add_child(bc);
2127 }
2128
2129 static const char *desc[4] = { "x", "y", "w", "h" };
2130 for (int i = 0; i < 4; i++) {
2131 spin[i] = memnew(EditorSpinSlider);
2132 spin[i]->set_label(desc[i]);
2133 spin[i]->set_accessibility_name(desc[i]);
2134 spin[i]->set_flat(true);
2135
2136 if (grid) {
2137 bc->get_child(i / 2)->add_child(spin[i]);
2138 } else {
2139 bc->add_child(spin[i]);
2140 }
2141
2142 add_focusable(spin[i]);
2143 spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyRect2i::_value_changed).bind(desc[i]));
2144 if (horizontal) {
2145 spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
2146 }
2147 }
2148
2149 if (!horizontal) {
2150 set_label_reference(spin[0]); //show text and buttons around this
2151 }
2152 }
2153
2154 ///////////////////// PLANE /////////////////////////
2155
2156 void EditorPropertyPlane::_set_read_only(bool p_read_only) {
2157 for (int i = 0; i < 4; i++) {
2158 spin[i]->set_read_only(p_read_only);
2159 }
2160 }
2161
2162 void EditorPropertyPlane::_value_changed(double val, const String &p_name) {
2163 Plane p;
2164 p.normal.x = spin[0]->get_value();
2165 p.normal.y = spin[1]->get_value();
2166 p.normal.z = spin[2]->get_value();
2167 p.d = spin[3]->get_value();
2168 emit_changed(get_edited_property(), p, p_name);
2169 }
2170
2171 void EditorPropertyPlane::update_property() {
2172 Plane val = get_edited_property_value();
2173 spin[0]->set_value_no_signal(val.normal.x);
2174 spin[1]->set_value_no_signal(val.normal.y);
2175 spin[2]->set_value_no_signal(val.normal.z);
2176 spin[3]->set_value_no_signal(val.d);
2177 }
2178
2179 void EditorPropertyPlane::_notification(int p_what) {
2180 switch (p_what) {
2181 case NOTIFICATION_THEME_CHANGED: {
2182 const Color *colors = _get_property_colors();
2183 for (int i = 0; i < 4; i++) {
2184 spin[i]->add_theme_color_override("label_color", colors[i]);
2185 }
2186 } break;
2187 }
2188 }
2189
2190 void EditorPropertyPlane::setup(const EditorPropertyRangeHint &p_range_hint) {
2191 for (int i = 0; i < 4; i++) {
2192 spin[i]->set_min(p_range_hint.min);
2193 spin[i]->set_max(p_range_hint.max);
2194 spin[i]->set_step(p_range_hint.step);
2195 if (p_range_hint.hide_control) {
2196 spin[i]->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);
2197 }
2198 spin[i]->set_allow_greater(true);
2199 spin[i]->set_allow_lesser(true);
2200 }
2201 spin[3]->set_suffix(p_range_hint.suffix);
2202 }
2203
2204 EditorPropertyPlane::EditorPropertyPlane(bool p_force_wide) {
2205 bool horizontal = p_force_wide || bool(EDITOR_GET("interface/inspector/horizontal_vector_types_editing"));
2206
2207 BoxContainer *bc;
2208
2209 if (p_force_wide) {
2210 bc = memnew(HBoxContainer);
2211 add_child(bc);
2212 } else if (horizontal) {
2213 bc = memnew(HBoxContainer);
2214 add_child(bc);
2215 set_bottom_editor(bc);
2216 } else {
2217 bc = memnew(VBoxContainer);
2218 add_child(bc);
2219 }
2220
2221 static const char *desc[4] = { "x", "y", "z", "d" };
2222 for (int i = 0; i < 4; i++) {
2223 spin[i] = memnew(EditorSpinSlider);
2224 spin[i]->set_flat(true);
2225 spin[i]->set_label(desc[i]);
2226 spin[i]->set_accessibility_name(desc[i]);
2227 bc->add_child(spin[i]);
2228 add_focusable(spin[i]);
2229 spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyPlane::_value_changed).bind(desc[i]));
2230 if (horizontal) {
2231 spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
2232 }
2233 }
2234
2235 if (!horizontal) {
2236 set_label_reference(spin[0]); //show text and buttons around this
2237 }
2238 }
2239
2240 ///////////////////// QUATERNION /////////////////////////
2241
2242 void EditorPropertyQuaternion::_set_read_only(bool p_read_only) {
2243 for (int i = 0; i < 4; i++) {
2244 spin[i]->set_read_only(p_read_only);
2245 }
2246 for (int i = 0; i < 3; i++) {
2247 euler[i]->set_read_only(p_read_only);
2248 }
2249 }
2250
2251 void EditorPropertyQuaternion::_edit_custom_value() {
2252 if (edit_button->is_pressed()) {
2253 edit_custom_bc->show();
2254 for (int i = 0; i < 3; i++) {
2255 euler[i]->grab_focus();
2256 }
2257 } else {
2258 edit_custom_bc->hide();
2259 for (int i = 0; i < 4; i++) {
2260 spin[i]->grab_focus();
2261 }
2262 }
2263 update_property();
2264 }
2265
2266 void EditorPropertyQuaternion::_custom_value_changed(double val) {
2267 edit_euler.x = euler[0]->get_value();
2268 edit_euler.y = euler[1]->get_value();
2269 edit_euler.z = euler[2]->get_value();
2270
2271 Vector3 v;
2272 v.x = Math::deg_to_rad(edit_euler.x);
2273 v.y = Math::deg_to_rad(edit_euler.y);
2274 v.z = Math::deg_to_rad(edit_euler.z);
2275
2276 Quaternion temp_q = Quaternion::from_euler(v);
2277 spin[0]->set_value_no_signal(temp_q.x);
2278 spin[1]->set_value_no_signal(temp_q.y);
2279 spin[2]->set_value_no_signal(temp_q.z);
2280 spin[3]->set_value_no_signal(temp_q.w);
2281 _value_changed(-1, "");
2282 }
2283
2284 void EditorPropertyQuaternion::_value_changed(double val, const String &p_name) {
2285 Quaternion p;
2286 p.x = spin[0]->get_value();
2287 p.y = spin[1]->get_value();
2288 p.z = spin[2]->get_value();
2289 p.w = spin[3]->get_value();
2290
2291 emit_changed(get_edited_property(), p, p_name);
2292 }
2293
2294 bool EditorPropertyQuaternion::is_grabbing_euler() {
2295 bool is_grabbing = false;
2296 for (int i = 0; i < 3; i++) {
2297 is_grabbing |= euler[i]->is_grabbing();
2298 }
2299 return is_grabbing;
2300 }
2301
2302 void EditorPropertyQuaternion::update_property() {
2303 Quaternion val = get_edited_property_value();
2304 spin[0]->set_value_no_signal(val.x);
2305 spin[1]->set_value_no_signal(val.y);
2306 spin[2]->set_value_no_signal(val.z);
2307 spin[3]->set_value_no_signal(val.w);
2308 if (!is_grabbing_euler()) {
2309 Vector3 v = val.normalized().get_euler();
2310 edit_euler.x = Math::rad_to_deg(v.x);
2311 edit_euler.y = Math::rad_to_deg(v.y);
2312 edit_euler.z = Math::rad_to_deg(v.z);
2313 euler[0]->set_value_no_signal(edit_euler.x);
2314 euler[1]->set_value_no_signal(edit_euler.y);
2315 euler[2]->set_value_no_signal(edit_euler.z);
2316 }
2317 }
2318
2319 void EditorPropertyQuaternion::_warning_pressed() {
2320 warning_dialog->popup_centered();
2321 }
2322
2323 void EditorPropertyQuaternion::_notification(int p_what) {
2324 switch (p_what) {
2325 case NOTIFICATION_THEME_CHANGED: {
2326 const Color *colors = _get_property_colors();
2327 for (int i = 0; i < 4; i++) {
2328 spin[i]->add_theme_color_override("label_color", colors[i]);
2329 }
2330 for (int i = 0; i < 3; i++) {
2331 euler[i]->add_theme_color_override("label_color", colors[i]);
2332 }
2333 edit_button->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
2334 euler_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("property_color"), SNAME("EditorProperty")));
2335 warning->set_button_icon(get_editor_theme_icon(SNAME("NodeWarning")));
2336 warning->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
2337 } break;
2338 }
2339 }
2340
2341 void EditorPropertyQuaternion::setup(const EditorPropertyRangeHint &p_range_hint, bool p_hide_editor) {
2342 for (int i = 0; i < 4; i++) {
2343 spin[i]->set_min(p_range_hint.min);
2344 spin[i]->set_max(p_range_hint.max);
2345 spin[i]->set_step(p_range_hint.step);
2346 if (p_range_hint.hide_control) {
2347 spin[i]->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);
2348 }
2349 spin[i]->set_allow_greater(true);
2350 spin[i]->set_allow_lesser(true);
2351 // Quaternion is inherently unitless, however someone may want to use it as
2352 // a generic way to store 4 values, so we'll still respect the suffix.
2353 spin[i]->set_suffix(p_range_hint.suffix);
2354 }
2355
2356 for (int i = 0; i < 3; i++) {
2357 euler[i]->set_min(-360);
2358 euler[i]->set_max(360);
2359 euler[i]->set_step(0.1);
2360 euler[i]->set_allow_greater(true);
2361 euler[i]->set_allow_lesser(true);
2362 euler[i]->set_suffix(U"\u00B0");
2363 }
2364
2365 if (p_hide_editor) {
2366 edit_button->hide();
2367 }
2368 }
2369
2370 EditorPropertyQuaternion::EditorPropertyQuaternion() {
2371 bool horizontal = EDITOR_GET("interface/inspector/horizontal_vector_types_editing");
2372
2373 VBoxContainer *bc = memnew(VBoxContainer);
2374 edit_custom_bc = memnew(VBoxContainer);
2375 BoxContainer *edit_custom_layout;
2376 if (horizontal) {
2377 default_layout = memnew(HBoxContainer);
2378 edit_custom_layout = memnew(HBoxContainer);
2379 set_bottom_editor(bc);
2380 } else {
2381 default_layout = memnew(VBoxContainer);
2382 edit_custom_layout = memnew(VBoxContainer);
2383 }
2384 edit_custom_bc->hide();
2385 add_child(bc);
2386 edit_custom_bc->set_h_size_flags(SIZE_EXPAND_FILL);
2387 default_layout->set_h_size_flags(SIZE_EXPAND_FILL);
2388 edit_custom_layout->set_h_size_flags(SIZE_EXPAND_FILL);
2389 bc->add_child(default_layout);
2390 bc->add_child(edit_custom_bc);
2391
2392 static const char *desc[4] = { "x", "y", "z", "w" };
2393 for (int i = 0; i < 4; i++) {
2394 spin[i] = memnew(EditorSpinSlider);
2395 spin[i]->set_flat(true);
2396 spin[i]->set_label(desc[i]);
2397 spin[i]->set_accessibility_name(desc[i]);
2398 default_layout->add_child(spin[i]);
2399 add_focusable(spin[i]);
2400 spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyQuaternion::_value_changed).bind(desc[i]));
2401 if (horizontal) {
2402 spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
2403 }
2404 }
2405
2406 warning = memnew(Button);
2407 warning->set_text(TTR("Temporary Euler may be changed implicitly!"));
2408 warning->set_clip_text(true);
2409 warning->set_theme_type_variation(SNAME("EditorInspectorButton"));
2410 warning->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyQuaternion::_warning_pressed));
2411 warning_dialog = memnew(AcceptDialog);
2412 add_child(warning_dialog);
2413 warning_dialog->set_text(TTR("Temporary Euler will not be stored in the object with the original value. Instead, it will be stored as Quaternion with irreversible conversion.\nThis is due to the fact that the result of Euler->Quaternion can be determined uniquely, but the result of Quaternion->Euler can be multi-existent."));
2414
2415 euler_label = memnew(Label);
2416 euler_label->set_text(TTR("Temporary Euler"));
2417
2418 edit_custom_bc->add_child(warning);
2419 edit_custom_bc->add_child(edit_custom_layout);
2420 edit_custom_layout->add_child(euler_label);
2421
2422 for (int i = 0; i < 3; i++) {
2423 euler[i] = memnew(EditorSpinSlider);
2424 euler[i]->set_flat(true);
2425 euler[i]->set_label(desc[i]);
2426 euler[i]->set_accessibility_name(vformat(TTR("Temporary Euler %s"), desc[i]));
2427 edit_custom_layout->add_child(euler[i]);
2428 add_focusable(euler[i]);
2429 euler[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyQuaternion::_custom_value_changed));
2430 if (horizontal) {
2431 euler[i]->set_h_size_flags(SIZE_EXPAND_FILL);
2432 }
2433 }
2434
2435 edit_button = memnew(Button);
2436 edit_button->set_accessibility_name(TTRC("Edit"));
2437 edit_button->set_flat(true);
2438 edit_button->set_toggle_mode(true);
2439 edit_button->set_theme_type_variation(SNAME("EditorInspectorButton"));
2440 default_layout->add_child(edit_button);
2441 edit_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyQuaternion::_edit_custom_value));
2442
2443 add_focusable(edit_button);
2444
2445 if (!horizontal) {
2446 set_label_reference(spin[0]); //show text and buttons around this
2447 }
2448 }
2449
2450 ///////////////////// AABB /////////////////////////
2451
2452 void EditorPropertyAABB::_set_read_only(bool p_read_only) {
2453 for (int i = 0; i < 6; i++) {
2454 spin[i]->set_read_only(p_read_only);
2455 }
2456 }
2457
2458 void EditorPropertyAABB::_value_changed(double val, const String &p_name) {
2459 AABB p;
2460 p.position.x = spin[0]->get_value();
2461 p.position.y = spin[1]->get_value();
2462 p.position.z = spin[2]->get_value();
2463 p.size.x = spin[3]->get_value();
2464 p.size.y = spin[4]->get_value();
2465 p.size.z = spin[5]->get_value();
2466 emit_changed(get_edited_property(), p, p_name);
2467 }
2468
2469 void EditorPropertyAABB::update_property() {
2470 AABB val = get_edited_property_value();
2471 spin[0]->set_value_no_signal(val.position.x);
2472 spin[1]->set_value_no_signal(val.position.y);
2473 spin[2]->set_value_no_signal(val.position.z);
2474 spin[3]->set_value_no_signal(val.size.x);
2475 spin[4]->set_value_no_signal(val.size.y);
2476 spin[5]->set_value_no_signal(val.size.z);
2477 }
2478
2479 void EditorPropertyAABB::_notification(int p_what) {
2480 switch (p_what) {
2481 case NOTIFICATION_THEME_CHANGED: {
2482 const Color *colors = _get_property_colors();
2483 for (int i = 0; i < 6; i++) {
2484 spin[i]->add_theme_color_override("label_color", colors[i % 3]);
2485 }
2486 } break;
2487 }
2488 }
2489
2490 void EditorPropertyAABB::setup(const EditorPropertyRangeHint &p_range_hint) {
2491 for (int i = 0; i < 6; i++) {
2492 spin[i]->set_min(p_range_hint.min);
2493 spin[i]->set_max(p_range_hint.max);
2494 spin[i]->set_step(p_range_hint.step);
2495 if (p_range_hint.hide_control) {
2496 spin[i]->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);
2497 }
2498 spin[i]->set_allow_greater(true);
2499 spin[i]->set_allow_lesser(true);
2500 spin[i]->set_suffix(p_range_hint.suffix);
2501 }
2502 }
2503
2504 EditorPropertyAABB::EditorPropertyAABB() {
2505 GridContainer *g = memnew(GridContainer);
2506 g->set_columns(3);
2507 add_child(g);
2508
2509 static const char *desc[6] = { "x", "y", "z", "w", "h", "d" };
2510 for (int i = 0; i < 6; i++) {
2511 spin[i] = memnew(EditorSpinSlider);
2512 spin[i]->set_label(desc[i]);
2513 spin[i]->set_accessibility_name(desc[i]);
2514 spin[i]->set_flat(true);
2515
2516 g->add_child(spin[i]);
2517 spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
2518 add_focusable(spin[i]);
2519 spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyAABB::_value_changed).bind(desc[i]));
2520 }
2521 set_bottom_editor(g);
2522 }
2523
2524 ///////////////////// TRANSFORM2D /////////////////////////
2525
2526 void EditorPropertyTransform2D::_set_read_only(bool p_read_only) {
2527 for (int i = 0; i < 6; i++) {
2528 spin[i]->set_read_only(p_read_only);
2529 }
2530 }
2531
2532 void EditorPropertyTransform2D::_value_changed(double val, const String &p_name) {
2533 Transform2D p;
2534 p[0][0] = spin[0]->get_value();
2535 p[1][0] = spin[1]->get_value();
2536 p[2][0] = spin[2]->get_value();
2537 p[0][1] = spin[3]->get_value();
2538 p[1][1] = spin[4]->get_value();
2539 p[2][1] = spin[5]->get_value();
2540
2541 emit_changed(get_edited_property(), p, p_name);
2542 }
2543
2544 void EditorPropertyTransform2D::update_property() {
2545 Transform2D val = get_edited_property_value();
2546 spin[0]->set_value_no_signal(val[0][0]);
2547 spin[1]->set_value_no_signal(val[1][0]);
2548 spin[2]->set_value_no_signal(val[2][0]);
2549 spin[3]->set_value_no_signal(val[0][1]);
2550 spin[4]->set_value_no_signal(val[1][1]);
2551 spin[5]->set_value_no_signal(val[2][1]);
2552 }
2553
2554 void EditorPropertyTransform2D::_notification(int p_what) {
2555 switch (p_what) {
2556 case NOTIFICATION_THEME_CHANGED: {
2557 const Color *colors = _get_property_colors();
2558 for (int i = 0; i < 6; i++) {
2559 // For Transform2D, use the 4th color (cyan) for the origin vector.
2560 if (i % 3 == 2) {
2561 spin[i]->add_theme_color_override("label_color", colors[3]);
2562 } else {
2563 spin[i]->add_theme_color_override("label_color", colors[i % 3]);
2564 }
2565 }
2566 } break;
2567 }
2568 }
2569
2570 void EditorPropertyTransform2D::setup(const EditorPropertyRangeHint &p_range_hint) {
2571 for (int i = 0; i < 6; i++) {
2572 spin[i]->set_min(p_range_hint.min);
2573 spin[i]->set_max(p_range_hint.max);
2574 spin[i]->set_step(p_range_hint.step);
2575 if (p_range_hint.hide_control) {
2576 spin[i]->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);
2577 }
2578 spin[i]->set_allow_greater(true);
2579 spin[i]->set_allow_lesser(true);
2580 if (i % 3 == 2) {
2581 spin[i]->set_suffix(p_range_hint.suffix);
2582 }
2583 }
2584 }
2585
2586 EditorPropertyTransform2D::EditorPropertyTransform2D(bool p_include_origin) {
2587 GridContainer *g = memnew(GridContainer);
2588 g->set_columns(p_include_origin ? 3 : 2);
2589 add_child(g);
2590
2591 static const char *desc[6] = { "xx", "xy", "xo", "yx", "yy", "yo" };
2592 for (int i = 0; i < 6; i++) {
2593 spin[i] = memnew(EditorSpinSlider);
2594 spin[i]->set_label(desc[i]);
2595 spin[i]->set_accessibility_name(desc[i]);
2596 spin[i]->set_flat(true);
2597 if (p_include_origin || i % 3 != 2) {
2598 g->add_child(spin[i]);
2599 }
2600 spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
2601 add_focusable(spin[i]);
2602 spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyTransform2D::_value_changed).bind(desc[i]));
2603 }
2604 set_bottom_editor(g);
2605 }
2606
2607 ///////////////////// BASIS /////////////////////////
2608
2609 void EditorPropertyBasis::_set_read_only(bool p_read_only) {
2610 for (int i = 0; i < 9; i++) {
2611 spin[i]->set_read_only(p_read_only);
2612 }
2613 }
2614
2615 void EditorPropertyBasis::_value_changed(double val, const String &p_name) {
2616 Basis p;
2617 p[0][0] = spin[0]->get_value();
2618 p[0][1] = spin[1]->get_value();
2619 p[0][2] = spin[2]->get_value();
2620 p[1][0] = spin[3]->get_value();
2621 p[1][1] = spin[4]->get_value();
2622 p[1][2] = spin[5]->get_value();
2623 p[2][0] = spin[6]->get_value();
2624 p[2][1] = spin[7]->get_value();
2625 p[2][2] = spin[8]->get_value();
2626
2627 emit_changed(get_edited_property(), p, p_name);
2628 }
2629
2630 void EditorPropertyBasis::update_property() {
2631 Basis val = get_edited_property_value();
2632 spin[0]->set_value_no_signal(val[0][0]);
2633 spin[1]->set_value_no_signal(val[0][1]);
2634 spin[2]->set_value_no_signal(val[0][2]);
2635 spin[3]->set_value_no_signal(val[1][0]);
2636 spin[4]->set_value_no_signal(val[1][1]);
2637 spin[5]->set_value_no_signal(val[1][2]);
2638 spin[6]->set_value_no_signal(val[2][0]);
2639 spin[7]->set_value_no_signal(val[2][1]);
2640 spin[8]->set_value_no_signal(val[2][2]);
2641 }
2642
2643 void EditorPropertyBasis::_notification(int p_what) {
2644 switch (p_what) {
2645 case NOTIFICATION_THEME_CHANGED: {
2646 const Color *colors = _get_property_colors();
2647 for (int i = 0; i < 9; i++) {
2648 spin[i]->add_theme_color_override("label_color", colors[i % 3]);
2649 }
2650 } break;
2651 }
2652 }
2653
2654 void EditorPropertyBasis::setup(const EditorPropertyRangeHint &p_range_hint) {
2655 for (int i = 0; i < 9; i++) {
2656 spin[i]->set_min(p_range_hint.min);
2657 spin[i]->set_max(p_range_hint.max);
2658 spin[i]->set_step(p_range_hint.step);
2659 if (p_range_hint.hide_control) {
2660 spin[i]->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);
2661 }
2662 spin[i]->set_allow_greater(true);
2663 spin[i]->set_allow_lesser(true);
2664 // Basis is inherently unitless, however someone may want to use it as
2665 // a generic way to store 9 values, so we'll still respect the suffix.
2666 spin[i]->set_suffix(p_range_hint.suffix);
2667 }
2668 }
2669
2670 EditorPropertyBasis::EditorPropertyBasis() {
2671 GridContainer *g = memnew(GridContainer);
2672 g->set_columns(3);
2673 add_child(g);
2674
2675 static const char *desc[9] = { "xx", "xy", "xz", "yx", "yy", "yz", "zx", "zy", "zz" };
2676 for (int i = 0; i < 9; i++) {
2677 spin[i] = memnew(EditorSpinSlider);
2678 spin[i]->set_label(desc[i]);
2679 spin[i]->set_accessibility_name(desc[i]);
2680 spin[i]->set_flat(true);
2681 g->add_child(spin[i]);
2682 spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
2683 add_focusable(spin[i]);
2684 spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyBasis::_value_changed).bind(desc[i]));
2685 }
2686 set_bottom_editor(g);
2687 }
2688
2689 ///////////////////// TRANSFORM3D /////////////////////////
2690
2691 void EditorPropertyTransform3D::_set_read_only(bool p_read_only) {
2692 for (int i = 0; i < 12; i++) {
2693 spin[i]->set_read_only(p_read_only);
2694 }
2695 }
2696
2697 void EditorPropertyTransform3D::_value_changed(double val, const String &p_name) {
2698 Transform3D p;
2699 p.basis[0][0] = spin[0]->get_value();
2700 p.basis[0][1] = spin[1]->get_value();
2701 p.basis[0][2] = spin[2]->get_value();
2702 p.origin[0] = spin[3]->get_value();
2703 p.basis[1][0] = spin[4]->get_value();
2704 p.basis[1][1] = spin[5]->get_value();
2705 p.basis[1][2] = spin[6]->get_value();
2706 p.origin[1] = spin[7]->get_value();
2707 p.basis[2][0] = spin[8]->get_value();
2708 p.basis[2][1] = spin[9]->get_value();
2709 p.basis[2][2] = spin[10]->get_value();
2710 p.origin[2] = spin[11]->get_value();
2711
2712 emit_changed(get_edited_property(), p, p_name);
2713 }
2714
2715 void EditorPropertyTransform3D::update_property() {
2716 update_using_transform(get_edited_property_value());
2717 }
2718
2719 void EditorPropertyTransform3D::update_using_transform(Transform3D p_transform) {
2720 spin[0]->set_value_no_signal(p_transform.basis[0][0]);
2721 spin[1]->set_value_no_signal(p_transform.basis[0][1]);
2722 spin[2]->set_value_no_signal(p_transform.basis[0][2]);
2723 spin[3]->set_value_no_signal(p_transform.origin[0]);
2724 spin[4]->set_value_no_signal(p_transform.basis[1][0]);
2725 spin[5]->set_value_no_signal(p_transform.basis[1][1]);
2726 spin[6]->set_value_no_signal(p_transform.basis[1][2]);
2727 spin[7]->set_value_no_signal(p_transform.origin[1]);
2728 spin[8]->set_value_no_signal(p_transform.basis[2][0]);
2729 spin[9]->set_value_no_signal(p_transform.basis[2][1]);
2730 spin[10]->set_value_no_signal(p_transform.basis[2][2]);
2731 spin[11]->set_value_no_signal(p_transform.origin[2]);
2732 }
2733
2734 void EditorPropertyTransform3D::_notification(int p_what) {
2735 switch (p_what) {
2736 case NOTIFICATION_THEME_CHANGED: {
2737 const Color *colors = _get_property_colors();
2738 for (int i = 0; i < 12; i++) {
2739 spin[i]->add_theme_color_override("label_color", colors[i % 4]);
2740 }
2741 } break;
2742 }
2743 }
2744
2745 void EditorPropertyTransform3D::setup(const EditorPropertyRangeHint &p_range_hint) {
2746 for (int i = 0; i < 12; i++) {
2747 spin[i]->set_min(p_range_hint.min);
2748 spin[i]->set_max(p_range_hint.max);
2749 spin[i]->set_step(p_range_hint.step);
2750 if (p_range_hint.hide_control) {
2751 spin[i]->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);
2752 }
2753 spin[i]->set_allow_greater(true);
2754 spin[i]->set_allow_lesser(true);
2755 if (i % 4 == 3) {
2756 spin[i]->set_suffix(p_range_hint.suffix);
2757 }
2758 }
2759 }
2760
2761 EditorPropertyTransform3D::EditorPropertyTransform3D() {
2762 GridContainer *g = memnew(GridContainer);
2763 g->set_columns(4);
2764 add_child(g);
2765
2766 static const char *desc[12] = { "xx", "xy", "xz", "xo", "yx", "yy", "yz", "yo", "zx", "zy", "zz", "zo" };
2767 for (int i = 0; i < 12; i++) {
2768 spin[i] = memnew(EditorSpinSlider);
2769 spin[i]->set_label(desc[i]);
2770 spin[i]->set_accessibility_name(desc[i]);
2771 spin[i]->set_flat(true);
2772 g->add_child(spin[i]);
2773 spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
2774 add_focusable(spin[i]);
2775 spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyTransform3D::_value_changed).bind(desc[i]));
2776 }
2777 set_bottom_editor(g);
2778 }
2779
2780 ///////////////////// PROJECTION /////////////////////////
2781
2782 void EditorPropertyProjection::_set_read_only(bool p_read_only) {
2783 for (int i = 0; i < 12; i++) {
2784 spin[i]->set_read_only(p_read_only);
2785 }
2786 }
2787
2788 void EditorPropertyProjection::_value_changed(double val, const String &p_name) {
2789 Projection p;
2790 p.columns[0][0] = spin[0]->get_value();
2791 p.columns[0][1] = spin[1]->get_value();
2792 p.columns[0][2] = spin[2]->get_value();
2793 p.columns[0][3] = spin[3]->get_value();
2794 p.columns[1][0] = spin[4]->get_value();
2795 p.columns[1][1] = spin[5]->get_value();
2796 p.columns[1][2] = spin[6]->get_value();
2797 p.columns[1][3] = spin[7]->get_value();
2798 p.columns[2][0] = spin[8]->get_value();
2799 p.columns[2][1] = spin[9]->get_value();
2800 p.columns[2][2] = spin[10]->get_value();
2801 p.columns[2][3] = spin[11]->get_value();
2802 p.columns[3][0] = spin[12]->get_value();
2803 p.columns[3][1] = spin[13]->get_value();
2804 p.columns[3][2] = spin[14]->get_value();
2805 p.columns[3][3] = spin[15]->get_value();
2806
2807 emit_changed(get_edited_property(), p, p_name);
2808 }
2809
2810 void EditorPropertyProjection::update_property() {
2811 update_using_transform(get_edited_property_value());
2812 }
2813
2814 void EditorPropertyProjection::update_using_transform(Projection p_transform) {
2815 spin[0]->set_value_no_signal(p_transform.columns[0][0]);
2816 spin[1]->set_value_no_signal(p_transform.columns[0][1]);
2817 spin[2]->set_value_no_signal(p_transform.columns[0][2]);
2818 spin[3]->set_value_no_signal(p_transform.columns[0][3]);
2819 spin[4]->set_value_no_signal(p_transform.columns[1][0]);
2820 spin[5]->set_value_no_signal(p_transform.columns[1][1]);
2821 spin[6]->set_value_no_signal(p_transform.columns[1][2]);
2822 spin[7]->set_value_no_signal(p_transform.columns[1][3]);
2823 spin[8]->set_value_no_signal(p_transform.columns[2][0]);
2824 spin[9]->set_value_no_signal(p_transform.columns[2][1]);
2825 spin[10]->set_value_no_signal(p_transform.columns[2][2]);
2826 spin[11]->set_value_no_signal(p_transform.columns[2][3]);
2827 spin[12]->set_value_no_signal(p_transform.columns[3][0]);
2828 spin[13]->set_value_no_signal(p_transform.columns[3][1]);
2829 spin[14]->set_value_no_signal(p_transform.columns[3][2]);
2830 spin[15]->set_value_no_signal(p_transform.columns[3][3]);
2831 }
2832
2833 void EditorPropertyProjection::_notification(int p_what) {
2834 switch (p_what) {
2835 case NOTIFICATION_THEME_CHANGED: {
2836 const Color *colors = _get_property_colors();
2837 for (int i = 0; i < 16; i++) {
2838 spin[i]->add_theme_color_override("label_color", colors[i % 4]);
2839 }
2840 } break;
2841 }
2842 }
2843
2844 void EditorPropertyProjection::setup(const EditorPropertyRangeHint &p_range_hint) {
2845 for (int i = 0; i < 16; i++) {
2846 spin[i]->set_min(p_range_hint.min);
2847 spin[i]->set_max(p_range_hint.max);
2848 spin[i]->set_step(p_range_hint.step);
2849 if (p_range_hint.hide_control) {
2850 spin[i]->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);
2851 }
2852 spin[i]->set_allow_greater(true);
2853 spin[i]->set_allow_lesser(true);
2854 if (i % 4 == 3) {
2855 spin[i]->set_suffix(p_range_hint.suffix);
2856 }
2857 }
2858 }
2859
2860 EditorPropertyProjection::EditorPropertyProjection() {
2861 GridContainer *g = memnew(GridContainer);
2862 g->set_columns(4);
2863 add_child(g);
2864
2865 static const char *desc[16] = { "xx", "xy", "xz", "xw", "yx", "yy", "yz", "yw", "zx", "zy", "zz", "zw", "wx", "wy", "wz", "ww" };
2866 for (int i = 0; i < 16; i++) {
2867 spin[i] = memnew(EditorSpinSlider);
2868 spin[i]->set_label(desc[i]);
2869 spin[i]->set_accessibility_name(desc[i]);
2870 spin[i]->set_flat(true);
2871 g->add_child(spin[i]);
2872 spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
2873 add_focusable(spin[i]);
2874 spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyProjection::_value_changed).bind(desc[i]));
2875 }
2876 set_bottom_editor(g);
2877 }
2878 ////////////// COLOR PICKER //////////////////////
2879
2880 void EditorPropertyColor::_set_read_only(bool p_read_only) {
2881 picker->set_disabled(p_read_only);
2882 }
2883
2884 void EditorPropertyColor::_color_changed(const Color &p_color) {
2885 if (!live_changes_enabled) {
2886 return;
2887 }
2888
2889 // Cancel the color change if the current color is identical to the new one.
2890 if (((Color)get_edited_property_value()).is_equal_approx(p_color)) {
2891 return;
2892 }
2893
2894 // Preview color change, bypassing undo/redo.
2895 get_edited_object()->set(get_edited_property(), p_color);
2896 }
2897
2898 void EditorPropertyColor::_picker_created() {
2899 picker->get_popup()->connect("about_to_popup", callable_mp(this, &EditorPropertyColor::_popup_opening));
2900 picker->connect("popup_closed", callable_mp(this, &EditorPropertyColor::_popup_closed), CONNECT_DEFERRED);
2901 }
2902
2903 void EditorPropertyColor::_popup_opening() {
2904 if (EditorNode::get_singleton()) {
2905 EditorNode::get_singleton()->setup_color_picker(picker->get_picker());
2906 }
2907 last_color = picker->get_pick_color();
2908 was_checked = !is_checkable() || is_checked();
2909 }
2910
2911 void EditorPropertyColor::_popup_closed() {
2912 get_edited_object()->set(get_edited_property(), was_checked ? Variant(last_color) : Variant());
2913 if (!picker->get_pick_color().is_equal_approx(last_color)) {
2914 emit_changed(get_edited_property(), picker->get_pick_color(), "", false);
2915 }
2916 }
2917
2918 void EditorPropertyColor::update_property() {
2919 picker->set_pick_color(get_edited_property_display_value());
2920 const Color color = picker->get_pick_color();
2921
2922 // Add a tooltip to display each channel's values without having to click the ColorPickerButton
2923 if (picker->is_editing_alpha()) {
2924 picker->set_tooltip_text(vformat(
2925 "R: %s\nG: %s\nB: %s\nA: %s",
2926 rtos(color.r).pad_decimals(2),
2927 rtos(color.g).pad_decimals(2),
2928 rtos(color.b).pad_decimals(2),
2929 rtos(color.a).pad_decimals(2)));
2930 } else {
2931 picker->set_tooltip_text(vformat(
2932 "R: %s\nG: %s\nB: %s",
2933 rtos(color.r).pad_decimals(2),
2934 rtos(color.g).pad_decimals(2),
2935 rtos(color.b).pad_decimals(2)));
2936 }
2937 }
2938
2939 void EditorPropertyColor::setup(bool p_show_alpha) {
2940 picker->set_edit_alpha(p_show_alpha);
2941 }
2942
2943 void EditorPropertyColor::set_live_changes_enabled(bool p_enabled) {
2944 live_changes_enabled = p_enabled;
2945 }
2946
2947 EditorPropertyColor::EditorPropertyColor() {
2948 picker = memnew(ColorPickerButton);
2949 add_child(picker);
2950 picker->set_flat(true);
2951 picker->set_theme_type_variation(SNAME("EditorInspectorButton"));
2952 picker->connect("color_changed", callable_mp(this, &EditorPropertyColor::_color_changed));
2953 picker->connect("picker_created", callable_mp(this, &EditorPropertyColor::_picker_created), CONNECT_ONE_SHOT);
2954 }
2955
2956 ////////////// NODE PATH //////////////////////
2957
2958 void EditorPropertyNodePath::_set_read_only(bool p_read_only) {
2959 assign->set_disabled(p_read_only);
2960 menu->set_disabled(p_read_only);
2961 }
2962
2963 Variant EditorPropertyNodePath::_get_cache_value(const StringName &p_prop, bool &r_valid) const {
2964 if (p_prop == get_edited_property()) {
2965 r_valid = true;
2966 return const_cast<EditorPropertyNodePath *>(this)->get_edited_object()->get(get_edited_property(), &r_valid);
2967 }
2968 return Variant();
2969 }
2970
2971 void EditorPropertyNodePath::_node_selected(const NodePath &p_path, bool p_absolute) {
2972 NodePath path = p_path;
2973 Node *base_node = get_base_node();
2974
2975 if (!base_node && Object::cast_to<RefCounted>(get_edited_object())) {
2976 Node *to_node = get_node(p_path);
2977 ERR_FAIL_NULL(to_node);
2978 path = get_tree()->get_edited_scene_root()->get_path_to(to_node);
2979 }
2980
2981 if (p_absolute && base_node) { // for AnimationTrackKeyEdit
2982 path = base_node->get_path().rel_path_to(p_path);
2983 }
2984
2985 if (editing_node) {
2986 if (!base_node) {
2987 emit_changed(get_edited_property(), get_tree()->get_edited_scene_root()->get_node(path));
2988 } else {
2989 emit_changed(get_edited_property(), base_node->get_node(path));
2990 }
2991 } else {
2992 emit_changed(get_edited_property(), path);
2993 }
2994 update_property();
2995 }
2996
2997 void EditorPropertyNodePath::_node_assign() {
2998 if (!scene_tree) {
2999 scene_tree = memnew(SceneTreeDialog);
3000 scene_tree->get_scene_tree()->set_show_enabled_subscene(true);
3001 scene_tree->set_valid_types(valid_types);
3002 add_child(scene_tree);
3003 scene_tree->connect("selected", callable_mp(this, &EditorPropertyNodePath::_node_selected).bind(true));
3004 }
3005
3006 Variant val = get_edited_property_value();
3007 Node *n = nullptr;
3008 if (val.get_type() == Variant::Type::NODE_PATH) {
3009 Node *base_node = get_base_node();
3010 n = base_node == nullptr ? nullptr : base_node->get_node_or_null(val);
3011 } else {
3012 n = Object::cast_to<Node>(val);
3013 }
3014 scene_tree->popup_scenetree_dialog(n, get_base_node());
3015 }
3016
3017 void EditorPropertyNodePath::_assign_draw() {
3018 if (dropping) {
3019 Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
3020 assign->draw_rect(Rect2(Point2(), assign->get_size()), color, false);
3021 }
3022 }
3023
3024 void EditorPropertyNodePath::_update_menu() {
3025 const NodePath &np = _get_node_path();
3026
3027 menu->get_popup()->set_item_disabled(ACTION_CLEAR, np.is_empty());
3028 menu->get_popup()->set_item_disabled(ACTION_COPY, np.is_empty());
3029
3030 Node *edited_node = Object::cast_to<Node>(get_edited_object());
3031 menu->get_popup()->set_item_disabled(ACTION_SELECT, !edited_node || !edited_node->has_node(np));
3032 }
3033
3034 void EditorPropertyNodePath::_menu_option(int p_idx) {
3035 switch (p_idx) {
3036 case ACTION_CLEAR: {
3037 if (editing_node) {
3038 emit_changed(get_edited_property(), Variant());
3039 } else {
3040 emit_changed(get_edited_property(), NodePath());
3041 }
3042 update_property();
3043 } break;
3044
3045 case ACTION_COPY: {
3046 DisplayServer::get_singleton()->clipboard_set(String(_get_node_path()));
3047 } break;
3048
3049 case ACTION_EDIT: {
3050 assign->hide();
3051 menu->hide();
3052
3053 const NodePath &np = _get_node_path();
3054 edit->set_text(String(np));
3055 edit->show();
3056 callable_mp((Control *)edit, &Control::grab_focus).call_deferred(false);
3057 } break;
3058
3059 case ACTION_SELECT: {
3060 const Node *edited_node = get_base_node();
3061 ERR_FAIL_NULL(edited_node);
3062
3063 const NodePath &np = _get_node_path();
3064 Node *target_node = edited_node->get_node_or_null(np);
3065 ERR_FAIL_NULL(target_node);
3066
3067 SceneTreeDock::get_singleton()->set_selected(target_node);
3068 } break;
3069 }
3070 }
3071
3072 void EditorPropertyNodePath::_accept_text() {
3073 _text_submitted(edit->get_text());
3074 }
3075
3076 void EditorPropertyNodePath::_text_submitted(const String &p_text) {
3077 NodePath np = p_text;
3078 _node_selected(np, false);
3079 edit->hide();
3080 assign->show();
3081 menu->show();
3082 }
3083
3084 const NodePath EditorPropertyNodePath::_get_node_path() const {
3085 const Node *base_node = const_cast<EditorPropertyNodePath *>(this)->get_base_node();
3086
3087 Variant val = get_edited_property_value();
3088 Node *n = Object::cast_to<Node>(val);
3089 if (n) {
3090 if (!n->is_inside_tree()) {
3091 return NodePath();
3092 }
3093 if (base_node) {
3094 return base_node->get_path_to(n);
3095 } else {
3096 return get_tree()->get_edited_scene_root()->get_path_to(n);
3097 }
3098 } else {
3099 return val;
3100 }
3101 }
3102
3103 bool EditorPropertyNodePath::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
3104 return !is_read_only() && is_drop_valid(p_data);
3105 }
3106
3107 void EditorPropertyNodePath::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
3108 ERR_FAIL_COND(!is_drop_valid(p_data));
3109 Dictionary data_dict = p_data;
3110 Array nodes = data_dict["nodes"];
3111 Node *node = get_tree()->get_edited_scene_root()->get_node(nodes[0]);
3112
3113 if (node) {
3114 _node_selected(node->get_path());
3115 }
3116 }
3117
3118 bool EditorPropertyNodePath::is_drop_valid(const Dictionary &p_drag_data) const {
3119 if (!p_drag_data.has("type") || p_drag_data["type"] != "nodes") {
3120 return false;
3121 }
3122 Array nodes = p_drag_data["nodes"];
3123 if (nodes.size() != 1) {
3124 return false;
3125 }
3126
3127 Object *data_root = p_drag_data.get("scene_root", (Object *)nullptr);
3128 if (data_root && get_tree()->get_edited_scene_root() != data_root) {
3129 return false;
3130 }
3131
3132 Node *dropped_node = get_tree()->get_edited_scene_root()->get_node(nodes[0]);
3133 ERR_FAIL_NULL_V(dropped_node, false);
3134
3135 if (valid_types.is_empty()) {
3136 // No type requirements specified so any type is valid.
3137 return true;
3138 }
3139
3140 for (const StringName &E : valid_types) {
3141 if (dropped_node->is_class(E) ||
3142 EditorNode::get_singleton()->is_object_of_custom_type(dropped_node, E)) {
3143 return true;
3144 } else {
3145 Ref<Script> dropped_node_script = dropped_node->get_script();
3146 while (dropped_node_script.is_valid()) {
3147 if (dropped_node_script->get_path() == E) {
3148 return true;
3149 }
3150 dropped_node_script = dropped_node_script->get_base_script();
3151 }
3152 }
3153 }
3154
3155 return false;
3156 }
3157
3158 void EditorPropertyNodePath::update_property() {
3159 const Node *base_node = get_base_node();
3160 const NodePath &p = _get_node_path();
3161 assign->set_tooltip_text(String(p));
3162
3163 if (p.is_empty()) {
3164 assign->set_button_icon(Ref<Texture2D>());
3165 assign->set_text(TTR("Assign..."));
3166 assign->set_flat(false);
3167 return;
3168 }
3169 assign->set_flat(true);
3170
3171 if (!base_node || !base_node->has_node(p)) {
3172 assign->set_button_icon(Ref<Texture2D>());
3173 assign->set_text(String(p));
3174 return;
3175 }
3176
3177 const Node *target_node = base_node->get_node(p);
3178 ERR_FAIL_NULL(target_node);
3179
3180 if (String(target_node->get_name()).contains_char('@')) {
3181 assign->set_button_icon(Ref<Texture2D>());
3182 assign->set_text(String(p));
3183 return;
3184 }
3185
3186 assign->set_text(target_node->get_name());
3187 assign->set_button_icon(EditorNode::get_singleton()->get_object_icon(target_node));
3188 }
3189
3190 void EditorPropertyNodePath::setup(const Vector<StringName> &p_valid_types, bool p_use_path_from_scene_root, bool p_editing_node) {
3191 valid_types = p_valid_types;
3192 editing_node = p_editing_node;
3193 use_path_from_scene_root = p_use_path_from_scene_root;
3194 }
3195
3196 void EditorPropertyNodePath::_notification(int p_what) {
3197 switch (p_what) {
3198 case NOTIFICATION_THEME_CHANGED: {
3199 menu->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
3200 menu->get_popup()->set_item_icon(ACTION_CLEAR, get_editor_theme_icon(SNAME("Clear")));
3201 menu->get_popup()->set_item_icon(ACTION_COPY, get_editor_theme_icon(SNAME("ActionCopy")));
3202 menu->get_popup()->set_item_icon(ACTION_EDIT, get_editor_theme_icon(SNAME("Edit")));
3203 menu->get_popup()->set_item_icon(ACTION_SELECT, get_editor_theme_icon(SNAME("ExternalLink")));
3204
3205 // Use a constant width for the icon to avoid sizing issues or blurry icons.
3206 assign->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
3207 } break;
3208
3209 case NOTIFICATION_DRAG_BEGIN: {
3210 if (!is_read_only() && is_drop_valid(get_viewport()->gui_get_drag_data())) {
3211 dropping = true;
3212 assign->queue_redraw();
3213 }
3214 } break;
3215
3216 case NOTIFICATION_DRAG_END: {
3217 if (dropping) {
3218 dropping = false;
3219 assign->queue_redraw();
3220 }
3221 } break;
3222 }
3223 }
3224
3225 Node *EditorPropertyNodePath::get_base_node() {
3226 Node *base_node = Object::cast_to<Node>(get_edited_object());
3227
3228 // For proxy objects, specifies the node to which the path is relative.
3229 if (!base_node && get_edited_object()->has_meta("__base_node_relative")) {
3230 base_node = Object::cast_to<Node>(get_edited_object()->get_meta("__base_node_relative"));
3231 }
3232
3233 if (!base_node) {
3234 base_node = Object::cast_to<Node>(InspectorDock::get_inspector_singleton()->get_edited_object());
3235 }
3236 if (!base_node) {
3237 // Try a base node within history.
3238 if (EditorNode::get_singleton()->get_editor_selection_history()->get_path_size() > 0) {
3239 Object *base = ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_selection_history()->get_path_object(0));
3240 if (base) {
3241 base_node = Object::cast_to<Node>(base);
3242 }
3243 }
3244 }
3245
3246 if (!use_path_from_scene_root) {
3247 return base_node;
3248 }
3249
3250 if (get_edited_object()->has_method("get_root_path")) {
3251 return Object::cast_to<Node>(get_edited_object()->call("get_root_path"));
3252 }
3253
3254 if (!base_node) {
3255 return nullptr; // Editing external resources.
3256 }
3257
3258 if (base_node->is_instance()) {
3259 return base_node; // Known scene root.
3260 }
3261
3262 base_node = base_node->get_owner();
3263 if (base_node) {
3264 return base_node; // Node in known scene.
3265 }
3266
3267 return get_tree()->get_edited_scene_root(); // Treat as a node in the main scene.
3268 }
3269
3270 EditorPropertyNodePath::EditorPropertyNodePath() {
3271 HBoxContainer *hbc = memnew(HBoxContainer);
3272 hbc->add_theme_constant_override("separation", 0);
3273 add_child(hbc);
3274 assign = memnew(Button);
3275 assign->set_accessibility_name(TTRC("Assign Node"));
3276 assign->set_flat(true);
3277 assign->set_theme_type_variation(SNAME("EditorInspectorButton"));
3278 assign->set_h_size_flags(SIZE_EXPAND_FILL);
3279 assign->set_clip_text(true);
3280 assign->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
3281 assign->set_expand_icon(true);
3282 assign->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyNodePath::_node_assign));
3283 assign->connect(SceneStringName(draw), callable_mp(this, &EditorPropertyNodePath::_assign_draw));
3284 SET_DRAG_FORWARDING_CD(assign, EditorPropertyNodePath);
3285 hbc->add_child(assign);
3286
3287 menu = memnew(MenuButton);
3288 menu->set_flat(true);
3289 menu->connect(SNAME("about_to_popup"), callable_mp(this, &EditorPropertyNodePath::_update_menu));
3290 hbc->add_child(menu);
3291
3292 menu->get_popup()->add_item(TTR("Clear"), ACTION_CLEAR);
3293 menu->get_popup()->add_item(TTR("Copy as Text"), ACTION_COPY);
3294 menu->get_popup()->add_item(TTR("Edit"), ACTION_EDIT);
3295 menu->get_popup()->add_item(TTR("Show Node in Tree"), ACTION_SELECT);
3296 menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyNodePath::_menu_option));
3297
3298 edit = memnew(LineEdit);
3299 edit->set_accessibility_name(TTRC("Node Path"));
3300 edit->set_h_size_flags(SIZE_EXPAND_FILL);
3301 edit->hide();
3302 edit->connect(SceneStringName(focus_exited), callable_mp(this, &EditorPropertyNodePath::_accept_text));
3303 edit->connect(SceneStringName(text_submitted), callable_mp(this, &EditorPropertyNodePath::_text_submitted));
3304 hbc->add_child(edit);
3305 }
3306
3307 ///////////////////// RID /////////////////////////
3308
3309 void EditorPropertyRID::update_property() {
3310 RID rid = get_edited_property_value();
3311 if (rid.is_valid()) {
3312 uint64_t id = rid.get_id();
3313 label->set_text("RID: " + uitos(id));
3314 } else {
3315 label->set_text(TTR("Invalid RID"));
3316 }
3317 }
3318
3319 EditorPropertyRID::EditorPropertyRID() {
3320 label = memnew(Label);
3321 add_child(label);
3322 }
3323
3324 ////////////// RESOURCE //////////////////////
3325
3326 void EditorPropertyResource::_set_read_only(bool p_read_only) {
3327 resource_picker->set_editable(!p_read_only);
3328 }
3329
3330 void EditorPropertyResource::_resource_selected(const Ref<Resource> &p_resource, bool p_inspect) {
3331 if (p_resource->is_built_in() && !p_resource->get_path().is_empty()) {
3332 String parent = p_resource->get_path().get_slice("::", 0);
3333 List<String> extensions;
3334 ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
3335
3336 if (p_inspect) {
3337 if (extensions.find(parent.get_extension()) && (!EditorNode::get_singleton()->get_edited_scene() || EditorNode::get_singleton()->get_edited_scene()->get_scene_file_path() != parent)) {
3338 // If the resource belongs to another (non-imported) scene, edit it in that scene instead.
3339 if (!FileAccess::exists(parent + ".import")) {
3340 callable_mp(EditorNode::get_singleton(), &EditorNode::edit_foreign_resource).call_deferred(p_resource);
3341 return;
3342 }
3343 }
3344 }
3345 }
3346
3347 if (!p_inspect && use_sub_inspector) {
3348 bool unfold = !get_edited_object()->editor_is_section_unfolded(get_edited_property());
3349 get_edited_object()->editor_set_section_unfold(get_edited_property(), unfold);
3350 update_property();
3351 } else if (!is_checkable() || is_checked()) {
3352 emit_signal(SNAME("resource_selected"), get_edited_property(), p_resource);
3353 }
3354 }
3355
3356 void EditorPropertyResource::_resource_changed(const Ref<Resource> &p_resource) {
3357 Resource *r = Object::cast_to<Resource>(get_edited_object());
3358 if (r) {
3359 // Check for recursive setting of resource
3360 HashSet<Resource *> resources_found;
3361 resources_found.insert(r);
3362 bool found = EditorNode::find_recursive_resources(p_resource, resources_found);
3363 if (found) {
3364 callable_mp(EditorNode::get_singleton(), &EditorNode::show_warning).call_deferred(TTR("Recursion detected, unable to assign resource to property."), TTR("Warning!"));
3365 emit_changed(get_edited_property(), Ref<Resource>());
3366 update_property();
3367 return;
3368 }
3369 }
3370
3371 if (p_resource.is_valid() && p_resource->is_local_to_scene()) {
3372 // Attempting to configure the local scene.
3373 Node *local_scene = _get_base_node();
3374 if (local_scene) {
3375 HashMap<Ref<Resource>, Ref<Resource>> remap;
3376 p_resource->configure_for_local_scene(local_scene, remap);
3377 } else {
3378 WARN_PRINT("You are attempting to assign a local-to-scene resource outside the scene.");
3379 }
3380 }
3381
3382 // The bool is_script applies only to an object's main script.
3383 // Changing the value of Script-type exported variables of the main script should not trigger saving/reloading properties.
3384 bool is_script = false;
3385 Ref<Script> s = p_resource;
3386 if (get_edited_object() && s.is_valid() && get_edited_property() == CoreStringName(script)) {
3387 is_script = true;
3388 InspectorDock::get_singleton()->store_script_properties(get_edited_object());
3389 s->call("set_instance_base_type", get_edited_object()->get_class());
3390 }
3391
3392 // Prevent the creation of invalid ViewportTextures when possible.
3393 Ref<ViewportTexture> vpt = p_resource;
3394 if (vpt.is_valid()) {
3395 r = Object::cast_to<Resource>(get_edited_object());
3396 if (Object::cast_to<VisualShaderNodeTexture>(r)) {
3397 EditorNode::get_singleton()->show_warning(TTR("Can't create a ViewportTexture in a Texture2D node because the texture will not be bound to a scene.\nUse a Texture2DParameter node instead and set the texture in the \"Shader Parameters\" tab."));
3398 emit_changed(get_edited_property(), Ref<Resource>());
3399 update_property();
3400 return;
3401 }
3402
3403 if (r && r->get_path().is_resource_file()) {
3404 EditorNode::get_singleton()->show_warning(TTR("Can't create a ViewportTexture on resources saved as a file.\nResource needs to belong to a scene."));
3405 emit_changed(get_edited_property(), Ref<Resource>());
3406 update_property();
3407 return;
3408 }
3409
3410 if (r && !r->is_local_to_scene()) {
3411 EditorNode::get_singleton()->show_warning(TTR("Can't create a ViewportTexture on this resource because it's not set as local to scene.\nPlease switch on the 'local to scene' property on it (and all resources containing it up to a node)."));
3412 emit_changed(get_edited_property(), Ref<Resource>());
3413 update_property();
3414 return;
3415 }
3416 }
3417
3418 emit_changed(get_edited_property(), p_resource);
3419 update_property();
3420
3421 if (is_script) {
3422 // Restore properties if script was changed.
3423 InspectorDock::get_singleton()->apply_script_properties(get_edited_object());
3424 }
3425
3426 // Automatically suggest setting up the path for a ViewportTexture.
3427 if (vpt.is_valid() && vpt->get_viewport_path_in_scene().is_empty()) {
3428 if (!scene_tree) {
3429 scene_tree = memnew(SceneTreeDialog);
3430 scene_tree->set_title(TTR("Pick a Viewport"));
3431
3432 Vector<StringName> valid_types;
3433 valid_types.push_back("Viewport");
3434 scene_tree->set_valid_types(valid_types);
3435 scene_tree->get_scene_tree()->set_show_enabled_subscene(true);
3436
3437 add_child(scene_tree);
3438 scene_tree->connect("selected", callable_mp(this, &EditorPropertyResource::_viewport_selected));
3439 }
3440 scene_tree->popup_scenetree_dialog();
3441 }
3442 }
3443
3444 void EditorPropertyResource::_sub_inspector_property_keyed(const String &p_property, const Variant &p_value, bool p_advance) {
3445 // The second parameter could be null, causing the event to fire with less arguments, so use the pointer call which preserves it.
3446 const Variant args[3] = { String(get_edited_property()) + ":" + p_property, p_value, p_advance };
3447 const Variant *argp[3] = { &args[0], &args[1], &args[2] };
3448 emit_signalp(SNAME("property_keyed_with_value"), argp, 3);
3449 }
3450
3451 void EditorPropertyResource::_sub_inspector_resource_selected(const Ref<Resource> &p_resource, const String &p_property) {
3452 emit_signal(SNAME("resource_selected"), String(get_edited_property()) + ":" + p_property, p_resource);
3453 }
3454
3455 void EditorPropertyResource::_sub_inspector_object_id_selected(int p_id) {
3456 emit_signal(SNAME("object_id_selected"), get_edited_property(), p_id);
3457 }
3458
3459 void EditorPropertyResource::_open_editor_pressed() {
3460 Ref<Resource> res = get_edited_property_value();
3461 if (res.is_valid()) {
3462 EditorNode::get_singleton()->edit_item(res.ptr(), this);
3463 }
3464 }
3465
3466 void EditorPropertyResource::_update_preferred_shader() {
3467 Node *parent = get_parent();
3468 EditorProperty *parent_property = nullptr;
3469
3470 while (parent && !parent_property) {
3471 parent_property = Object::cast_to<EditorProperty>(parent);
3472 parent = parent->get_parent();
3473 }
3474
3475 if (parent_property) {
3476 EditorShaderPicker *shader_picker = Object::cast_to<EditorShaderPicker>(resource_picker);
3477 Object *ed_object = parent_property->get_edited_object();
3478 const StringName &ed_property = parent_property->get_edited_property();
3479
3480 // Set preferred shader based on edited parent type.
3481 if ((Object::cast_to<GPUParticles2D>(ed_object) || Object::cast_to<GPUParticles3D>(ed_object)) && ed_property == SNAME("process_material")) {
3482 shader_picker->set_preferred_mode(Shader::MODE_PARTICLES);
3483 } else if (Object::cast_to<FogVolume>(ed_object)) {
3484 shader_picker->set_preferred_mode(Shader::MODE_FOG);
3485 } else if (Object::cast_to<CanvasItem>(ed_object)) {
3486 shader_picker->set_preferred_mode(Shader::MODE_CANVAS_ITEM);
3487 } else if (Object::cast_to<Node3D>(ed_object) || Object::cast_to<Mesh>(ed_object)) {
3488 shader_picker->set_preferred_mode(Shader::MODE_SPATIAL);
3489 } else if (Object::cast_to<Sky>(ed_object)) {
3490 shader_picker->set_preferred_mode(Shader::MODE_SKY);
3491 }
3492 }
3493 }
3494
3495 bool EditorPropertyResource::_should_stop_editing() const {
3496 return !resource_picker->is_toggle_pressed();
3497 }
3498
3499 Node *EditorPropertyResource::_get_base_node() {
3500 Node *base_node = Object::cast_to<Node>(get_edited_object());
3501
3502 if (!base_node) {
3503 base_node = Object::cast_to<Node>(InspectorDock::get_inspector_singleton()->get_edited_object());
3504 }
3505
3506 if (!base_node) {
3507 // Try a base node within history.
3508 if (EditorNode::get_singleton()->get_editor_selection_history()->get_path_size() > 0) {
3509 Object *base = ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_selection_history()->get_path_object(0));
3510 if (base) {
3511 base_node = Object::cast_to<Node>(base);
3512 }
3513 }
3514 }
3515
3516 if (!base_node) {
3517 return nullptr; // Editing external resources.
3518 }
3519
3520 if (!base_node->get_scene_file_path().is_empty()) {
3521 return base_node; // Known scene root.
3522 }
3523
3524 base_node = base_node->get_owner();
3525 if (base_node) {
3526 return base_node; // Node in known scene.
3527 }
3528
3529 return get_tree()->get_edited_scene_root(); // Treat as a node in the main scene.
3530 }
3531
3532 void EditorPropertyResource::_viewport_selected(const NodePath &p_path) {
3533 Node *to_node = get_node(p_path);
3534 if (!Object::cast_to<Viewport>(to_node)) {
3535 EditorNode::get_singleton()->show_warning(TTR("Selected node is not a Viewport!"));
3536 return;
3537 }
3538
3539 Ref<ViewportTexture> vt = get_edited_property_value();
3540 ERR_FAIL_COND(vt.is_null());
3541
3542 Node *local_scene = _get_base_node();
3543 ERR_FAIL_NULL(local_scene);
3544 vt->set_viewport_path_in_scene(local_scene->get_path_to(to_node));
3545
3546 emit_changed(get_edited_property(), vt);
3547 update_property();
3548 }
3549
3550 void EditorPropertyResource::setup(Object *p_object, const String &p_path, const String &p_base_type) {
3551 if (resource_picker) {
3552 memdelete(resource_picker);
3553 resource_picker = nullptr;
3554 }
3555
3556 if (p_path == "script" && p_base_type == "Script" && Object::cast_to<Node>(p_object)) {
3557 EditorScriptPicker *script_picker = memnew(EditorScriptPicker);
3558 script_picker->set_script_owner(Object::cast_to<Node>(p_object));
3559 resource_picker = script_picker;
3560 } else if (p_path == "shader" && p_base_type == "Shader" && Object::cast_to<ShaderMaterial>(p_object)) {
3561 EditorShaderPicker *shader_picker = memnew(EditorShaderPicker);
3562 shader_picker->set_edited_material(Object::cast_to<ShaderMaterial>(p_object));
3563 resource_picker = shader_picker;
3564 connect(SceneStringName(ready), callable_mp(this, &EditorPropertyResource::_update_preferred_shader));
3565 } else if (ClassDB::is_parent_class(p_base_type, "AudioStream")) {
3566 EditorAudioStreamPicker *astream_picker = memnew(EditorAudioStreamPicker);
3567 resource_picker = astream_picker;
3568 } else {
3569 resource_picker = memnew(EditorResourcePicker);
3570 }
3571
3572 resource_picker->set_base_type(p_base_type);
3573 resource_picker->set_resource_owner(p_object);
3574 resource_picker->set_property_path(p_path);
3575 resource_picker->set_editable(true);
3576 resource_picker->set_h_size_flags(SIZE_EXPAND_FILL);
3577 add_child(resource_picker);
3578
3579 resource_picker->connect("resource_selected", callable_mp(this, &EditorPropertyResource::_resource_selected));
3580 resource_picker->connect("resource_changed", callable_mp(this, &EditorPropertyResource::_resource_changed));
3581
3582 for (int i = 0; i < resource_picker->get_child_count(); i++) {
3583 Button *b = Object::cast_to<Button>(resource_picker->get_child(i));
3584 if (b) {
3585 add_focusable(b);
3586 }
3587 }
3588 }
3589
3590 void EditorPropertyResource::update_property() {
3591 Ref<Resource> res = get_edited_property_display_value();
3592
3593 if (use_sub_inspector) {
3594 if (res.is_valid() != resource_picker->is_toggle_mode()) {
3595 resource_picker->set_toggle_mode(res.is_valid());
3596 }
3597
3598 if (res.is_valid() && get_edited_object()->editor_is_section_unfolded(get_edited_property())) {
3599 if (!sub_inspector) {
3600 sub_inspector = memnew(EditorInspector);
3601 sub_inspector->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
3602 sub_inspector->set_use_doc_hints(true);
3603
3604 EditorInspector *parent_inspector = get_parent_inspector();
3605 if (parent_inspector) {
3606 sub_inspector->set_root_inspector(parent_inspector->get_root_inspector());
3607 sub_inspector->register_text_enter(parent_inspector->search_box);
3608 }
3609
3610 sub_inspector->set_property_name_style(InspectorDock::get_singleton()->get_property_name_style());
3611
3612 sub_inspector->connect("property_keyed", callable_mp(this, &EditorPropertyResource::_sub_inspector_property_keyed));
3613 sub_inspector->connect("resource_selected", callable_mp(this, &EditorPropertyResource::_sub_inspector_resource_selected));
3614 sub_inspector->connect("object_id_selected", callable_mp(this, &EditorPropertyResource::_sub_inspector_object_id_selected));
3615 sub_inspector->set_keying(is_keying());
3616 sub_inspector->set_read_only(is_read_only());
3617 sub_inspector->set_use_folding(is_using_folding());
3618
3619 sub_inspector->set_draw_focus_border(false);
3620 sub_inspector->set_focus_mode(FocusMode::FOCUS_NONE);
3621
3622 sub_inspector->set_use_filter(use_filter);
3623
3624 add_child(sub_inspector);
3625 set_bottom_editor(sub_inspector);
3626
3627 resource_picker->set_toggle_pressed(true);
3628
3629 Array editor_list;
3630 for (int i = 0; i < EditorNode::get_editor_data().get_editor_plugin_count(); i++) {
3631 EditorPlugin *ep = EditorNode::get_editor_data().get_editor_plugin(i);
3632 if (ep->handles(res.ptr())) {
3633 editor_list.push_back(ep);
3634 }
3635 }
3636
3637 if (!editor_list.is_empty()) {
3638 // Open editor directly.
3639 _open_editor_pressed();
3640 opened_editor = true;
3641 }
3642 }
3643
3644 sub_inspector->set_read_only(is_checkable() && !is_checked());
3645
3646 if (res.ptr() != sub_inspector->get_edited_object()) {
3647 sub_inspector->edit(res.ptr());
3648 _update_property_bg();
3649 }
3650
3651 } else if (sub_inspector) {
3652 set_bottom_editor(nullptr);
3653 memdelete(sub_inspector);
3654 sub_inspector = nullptr;
3655
3656 if (opened_editor) {
3657 EditorNode::get_singleton()->hide_unused_editors();
3658 opened_editor = false;
3659 }
3660 }
3661 }
3662
3663 resource_picker->set_edited_resource_no_check(res);
3664 const Ref<Resource> &real_res = get_edited_property_value();
3665 resource_picker->set_force_allow_unique(real_res.is_null() && res.is_valid());
3666 }
3667
3668 void EditorPropertyResource::collapse_all_folding() {
3669 if (sub_inspector) {
3670 sub_inspector->collapse_all_folding();
3671 }
3672 }
3673
3674 void EditorPropertyResource::expand_all_folding() {
3675 if (sub_inspector) {
3676 sub_inspector->expand_all_folding();
3677 }
3678 }
3679
3680 void EditorPropertyResource::expand_revertable() {
3681 if (sub_inspector) {
3682 sub_inspector->expand_revertable();
3683 }
3684 }
3685
3686 void EditorPropertyResource::set_use_sub_inspector(bool p_enable) {
3687 use_sub_inspector = p_enable;
3688 }
3689
3690 void EditorPropertyResource::set_use_filter(bool p_use) {
3691 use_filter = p_use;
3692 if (sub_inspector) {
3693 update_property();
3694 }
3695 }
3696
3697 void EditorPropertyResource::fold_resource() {
3698 bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
3699 if (unfolded) {
3700 resource_picker->set_toggle_pressed(false);
3701 get_edited_object()->editor_set_section_unfold(get_edited_property(), false);
3702 update_property();
3703 }
3704 }
3705
3706 void EditorPropertyResource::set_keying(bool p_keying) {
3707 EditorProperty::set_keying(p_keying);
3708 if (sub_inspector) {
3709 sub_inspector->set_keying(p_keying);
3710 }
3711 }
3712
3713 bool EditorPropertyResource::is_colored(ColorationMode p_mode) {
3714 switch (p_mode) {
3715 case COLORATION_CONTAINER_RESOURCE:
3716 return sub_inspector != nullptr;
3717 case COLORATION_RESOURCE:
3718 return true;
3719 case COLORATION_EXTERNAL:
3720 if (sub_inspector) {
3721 Resource *edited_resource = Object::cast_to<Resource>(sub_inspector->get_edited_object());
3722 return edited_resource && !edited_resource->is_built_in();
3723 }
3724 break;
3725 }
3726 return false;
3727 }
3728
3729 void EditorPropertyResource::_notification(int p_what) {
3730 switch (p_what) {
3731 case NOTIFICATION_EXIT_TREE: {
3732 const EditorInspector *ei = get_parent_inspector();
3733 const EditorInspector *main_ei = InspectorDock::get_inspector_singleton();
3734 if (ei && main_ei && ei != main_ei && !main_ei->is_ancestor_of(ei)) {
3735 fold_resource();
3736 }
3737 } break;
3738 }
3739 }
3740
3741 void EditorPropertyResource::_bind_methods() {
3742 ClassDB::bind_method(D_METHOD("_should_stop_editing"), &EditorPropertyResource::_should_stop_editing);
3743 }
3744
3745 EditorPropertyResource::EditorPropertyResource() {
3746 use_sub_inspector = bool(EDITOR_GET("interface/inspector/open_resources_in_current_inspector"));
3747 has_borders = true;
3748 }
3749
3750 ////////////// DEFAULT PLUGIN //////////////////////
3751
3752 bool EditorInspectorDefaultPlugin::can_handle(Object *p_object) {
3753 return true; // Can handle everything.
3754 }
3755
3756 bool EditorInspectorDefaultPlugin::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) {
3757 Control *editor = EditorInspectorDefaultPlugin::get_editor_for_property(p_object, p_type, p_path, p_hint, p_hint_text, p_usage, p_wide);
3758 if (editor) {
3759 add_property_editor(p_path, editor);
3760 }
3761 return false;
3762 }
3763
3764 static EditorPropertyRangeHint _parse_range_hint(PropertyHint p_hint, const String &p_hint_text, double p_default_step, bool is_int = false) {
3765 EditorPropertyRangeHint hint;
3766 hint.step = p_default_step;
3767 if (is_int) {
3768 hint.hide_control = false; // Always show controls for ints, unless specified in hint range.
3769 }
3770 Vector<String> slices = p_hint_text.split(",");
3771 if (p_hint == PROPERTY_HINT_RANGE) {
3772 ERR_FAIL_COND_V_MSG(slices.size() < 2, hint,
3773 vformat("Invalid PROPERTY_HINT_RANGE with hint \"%s\": Missing required min and/or max values.", p_hint_text));
3774
3775 hint.or_greater = false; // If using ranged, assume false by default.
3776 hint.or_less = false;
3777
3778 hint.min = slices[0].to_float();
3779 hint.max = slices[1].to_float();
3780
3781 if (slices.size() >= 3 && slices[2].is_valid_float()) {
3782 // Step is optional, could be something else if not a number.
3783 hint.step = slices[2].to_float();
3784 }
3785 hint.hide_control = false;
3786 for (int i = 2; i < slices.size(); i++) {
3787 String slice = slices[i].strip_edges();
3788 if (slice == "or_greater") {
3789 hint.or_greater = true;
3790 } else if (slice == "or_less") {
3791 hint.or_less = true;
3792 } else if (slice == "prefer_slider") {
3793 hint.prefer_slider = true;
3794 } else if (slice == "hide_control") {
3795 hint.hide_control = true;
3796 #ifndef DISABLE_DEPRECATED
3797 } else if (slice == "hide_slider") {
3798 hint.hide_control = true;
3799 #endif
3800 } else if (slice == "exp") {
3801 hint.exp_range = true;
3802 }
3803 }
3804 }
3805 bool degrees = false;
3806 for (int i = 0; i < slices.size(); i++) {
3807 String slice = slices[i].strip_edges();
3808 if (slice == "radians_as_degrees"
3809 #ifndef DISABLE_DEPRECATED
3810 || slice == "radians"
3811 #endif // DISABLE_DEPRECATED
3812 ) {
3813 hint.radians_as_degrees = true;
3814 } else if (slice == "degrees") {
3815 degrees = true;
3816 } else if (slice.begins_with("suffix:")) {
3817 hint.suffix = " " + slice.replace_first("suffix:", "").strip_edges();
3818 }
3819 }
3820
3821 if ((hint.radians_as_degrees || degrees) && hint.suffix.is_empty()) {
3822 hint.suffix = U"\u00B0";
3823 }
3824
3825 ERR_FAIL_COND_V_MSG(hint.step == 0, hint,
3826 vformat("Invalid PROPERTY_HINT_RANGE with hint \"%s\": Step cannot be 0.", p_hint_text));
3827
3828 return hint;
3829 }
3830
3831 static EditorProperty *get_input_action_editor(const String &p_hint_text, bool is_string_name) {
3832 // TODO: Should probably use a better editor GUI with a search bar.
3833 // Said GUI could also handle showing builtin options, requiring 1 less hint.
3834 EditorPropertyTextEnum *editor = memnew(EditorPropertyTextEnum);
3835 Vector<String> options;
3836 Vector<String> builtin_options;
3837 List<PropertyInfo> pinfo;
3838 ProjectSettings::get_singleton()->get_property_list(&pinfo);
3839 Vector<String> hints = p_hint_text.remove_char(' ').split(",", false);
3840
3841 HashMap<String, List<Ref<InputEvent>>> builtins = InputMap::get_singleton()->get_builtins();
3842 bool show_builtin = hints.has("show_builtin");
3843
3844 for (const PropertyInfo &pi : pinfo) {
3845 if (!pi.name.begins_with("input/")) {
3846 continue;
3847 }
3848
3849 const String action_name = pi.name.get_slicec('/', 1);
3850 if (builtins.has(action_name)) {
3851 if (show_builtin) {
3852 builtin_options.append(action_name);
3853 }
3854 } else {
3855 options.append(action_name);
3856 }
3857 }
3858 options.append_array(builtin_options);
3859 editor->setup(options, Vector<String>(), is_string_name, hints.has("loose_mode"));
3860 return editor;
3861 }
3862
3863 EditorProperty *EditorInspectorDefaultPlugin::get_editor_for_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) {
3864 double default_float_step = EDITOR_GET("interface/inspector/default_float_step");
3865
3866 switch (p_type) {
3867 // atomic types
3868 case Variant::NIL: {
3869 if (p_usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
3870 return memnew(EditorPropertyVariant);
3871 } else {
3872 return memnew(EditorPropertyNil);
3873 }
3874 } break;
3875 case Variant::BOOL: {
3876 EditorPropertyCheck *editor = memnew(EditorPropertyCheck);
3877 return editor;
3878 } break;
3879 case Variant::INT: {
3880 if (p_hint == PROPERTY_HINT_ENUM) {
3881 EditorPropertyEnum *editor = memnew(EditorPropertyEnum);
3882 Vector<String> options = p_hint_text.split(",");
3883 editor->setup(options);
3884 return editor;
3885
3886 } else if (p_hint == PROPERTY_HINT_FLAGS) {
3887 EditorPropertyFlags *editor = memnew(EditorPropertyFlags);
3888 Vector<String> options = p_hint_text.split(",");
3889 editor->setup(options);
3890 return editor;
3891
3892 } else if (p_hint == PROPERTY_HINT_LAYERS_2D_PHYSICS ||
3893 p_hint == PROPERTY_HINT_LAYERS_2D_RENDER ||
3894 p_hint == PROPERTY_HINT_LAYERS_2D_NAVIGATION ||
3895 p_hint == PROPERTY_HINT_LAYERS_3D_PHYSICS ||
3896 p_hint == PROPERTY_HINT_LAYERS_3D_RENDER ||
3897 p_hint == PROPERTY_HINT_LAYERS_3D_NAVIGATION ||
3898 p_hint == PROPERTY_HINT_LAYERS_AVOIDANCE) {
3899 EditorPropertyLayers::LayerType lt = EditorPropertyLayers::LAYER_RENDER_2D;
3900 switch (p_hint) {
3901 case PROPERTY_HINT_LAYERS_2D_RENDER:
3902 lt = EditorPropertyLayers::LAYER_RENDER_2D;
3903 break;
3904 case PROPERTY_HINT_LAYERS_2D_PHYSICS:
3905 lt = EditorPropertyLayers::LAYER_PHYSICS_2D;
3906 break;
3907 case PROPERTY_HINT_LAYERS_2D_NAVIGATION:
3908 lt = EditorPropertyLayers::LAYER_NAVIGATION_2D;
3909 break;
3910 case PROPERTY_HINT_LAYERS_3D_RENDER:
3911 lt = EditorPropertyLayers::LAYER_RENDER_3D;
3912 break;
3913 case PROPERTY_HINT_LAYERS_3D_PHYSICS:
3914 lt = EditorPropertyLayers::LAYER_PHYSICS_3D;
3915 break;
3916 case PROPERTY_HINT_LAYERS_3D_NAVIGATION:
3917 lt = EditorPropertyLayers::LAYER_NAVIGATION_3D;
3918 break;
3919 case PROPERTY_HINT_LAYERS_AVOIDANCE:
3920 lt = EditorPropertyLayers::LAYER_AVOIDANCE;
3921 break;
3922 default: {
3923 } //compiler could be smarter here and realize this can't happen
3924 }
3925 EditorPropertyLayers *editor = memnew(EditorPropertyLayers);
3926 editor->setup(lt);
3927 return editor;
3928 } else if (p_hint == PROPERTY_HINT_OBJECT_ID) {
3929 EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
3930 editor->setup(p_hint_text);
3931 return editor;
3932
3933 } else {
3934 EditorPropertyInteger *editor = memnew(EditorPropertyInteger);
3935 editor->setup(_parse_range_hint(p_hint, p_hint_text, 1, true));
3936 return editor;
3937 }
3938 } break;
3939 case Variant::FLOAT: {
3940 if (p_hint == PROPERTY_HINT_EXP_EASING) {
3941 EditorPropertyEasing *editor = memnew(EditorPropertyEasing);
3942 bool positive_only = false;
3943 bool flip = false;
3944 const Vector<String> hints = p_hint_text.split(",");
3945 for (int i = 0; i < hints.size(); i++) {
3946 const String hint = hints[i].strip_edges();
3947 if (hint == "attenuation") {
3948 flip = true;
3949 }
3950 if (hint == "positive_only") {
3951 positive_only = true;
3952 }
3953 }
3954
3955 editor->setup(positive_only, flip);
3956 return editor;
3957
3958 } else {
3959 EditorPropertyFloat *editor = memnew(EditorPropertyFloat);
3960 editor->setup(_parse_range_hint(p_hint, p_hint_text, default_float_step));
3961 return editor;
3962 }
3963 } break;
3964 case Variant::STRING: {
3965 if (p_hint == PROPERTY_HINT_ENUM || p_hint == PROPERTY_HINT_ENUM_SUGGESTION) {
3966 EditorPropertyTextEnum *editor = memnew(EditorPropertyTextEnum);
3967 Vector<String> options;
3968 Vector<String> option_names;
3969 if (p_hint_text.begins_with(";")) {
3970 // This is not supported officially. Only for `interface/editor/editor_language`.
3971 for (const String &option : p_hint_text.split(";", false)) {
3972 options.append(option.get_slicec('/', 0));
3973 option_names.append(option.get_slicec('/', 1));
3974 }
3975 } else {
3976 options = p_hint_text.split(",", false);
3977 }
3978 editor->setup(options, option_names, false, (p_hint == PROPERTY_HINT_ENUM_SUGGESTION));
3979 return editor;
3980 } else if (p_hint == PROPERTY_HINT_INPUT_NAME) {
3981 return get_input_action_editor(p_hint_text, false);
3982 } else if (p_hint == PROPERTY_HINT_MULTILINE_TEXT) {
3983 Vector<String> options = p_hint_text.split(",", false);
3984 EditorPropertyMultilineText *editor = memnew(EditorPropertyMultilineText(false));
3985 if (options.has("monospace")) {
3986 editor->set_monospaced(true);
3987 }
3988 if (options.has("no_wrap")) {
3989 editor->set_wrap_lines(false);
3990 }
3991 return editor;
3992 } else if (p_hint == PROPERTY_HINT_EXPRESSION) {
3993 EditorPropertyMultilineText *editor = memnew(EditorPropertyMultilineText(true));
3994 return editor;
3995 } else if (p_hint == PROPERTY_HINT_TYPE_STRING) {
3996 EditorPropertyClassName *editor = memnew(EditorPropertyClassName);
3997 editor->setup(p_hint_text, p_hint_text);
3998 return editor;
3999 } else if (p_hint == PROPERTY_HINT_LOCALE_ID) {
4000 EditorPropertyLocale *editor = memnew(EditorPropertyLocale);
4001 editor->setup(p_hint_text);
4002 return editor;
4003 } else if (p_hint == PROPERTY_HINT_DIR || p_hint == PROPERTY_HINT_FILE || p_hint == PROPERTY_HINT_SAVE_FILE || p_hint == PROPERTY_HINT_GLOBAL_SAVE_FILE || p_hint == PROPERTY_HINT_GLOBAL_DIR || p_hint == PROPERTY_HINT_GLOBAL_FILE || p_hint == PROPERTY_HINT_FILE_PATH) {
4004 Vector<String> extensions = p_hint_text.split(",");
4005 bool global = p_hint == PROPERTY_HINT_GLOBAL_DIR || p_hint == PROPERTY_HINT_GLOBAL_FILE || p_hint == PROPERTY_HINT_GLOBAL_SAVE_FILE;
4006 bool folder = p_hint == PROPERTY_HINT_DIR || p_hint == PROPERTY_HINT_GLOBAL_DIR;
4007 bool save = p_hint == PROPERTY_HINT_SAVE_FILE || p_hint == PROPERTY_HINT_GLOBAL_SAVE_FILE;
4008 bool enable_uid = p_hint == PROPERTY_HINT_FILE;
4009 EditorPropertyPath *editor = memnew(EditorPropertyPath);
4010 editor->setup(extensions, folder, global, enable_uid);
4011 if (save) {
4012 editor->set_save_mode();
4013 }
4014 return editor;
4015 } else {
4016 EditorPropertyText *editor = memnew(EditorPropertyText);
4017
4018 Vector<String> hints = p_hint_text.split(",");
4019 if (hints.has("monospace")) {
4020 editor->set_monospaced(true);
4021 }
4022
4023 if (p_hint == PROPERTY_HINT_PLACEHOLDER_TEXT) {
4024 editor->set_placeholder(p_hint_text);
4025 } else if (p_hint == PROPERTY_HINT_PASSWORD) {
4026 editor->set_secret(true);
4027 editor->set_placeholder(p_hint_text);
4028 }
4029 return editor;
4030 }
4031 } break;
4032
4033 // math types
4034
4035 case Variant::VECTOR2: {
4036 EditorPropertyVector2 *editor = memnew(EditorPropertyVector2(p_wide));
4037 editor->setup(_parse_range_hint(p_hint, p_hint_text, default_float_step), p_hint == PROPERTY_HINT_LINK);
4038 return editor;
4039
4040 } break;
4041 case Variant::VECTOR2I: {
4042 EditorPropertyVector2i *editor = memnew(EditorPropertyVector2i(p_wide));
4043 EditorPropertyRangeHint hint = _parse_range_hint(p_hint, p_hint_text, 1, true);
4044 hint.step = Math::round(hint.step);
4045 editor->setup(hint, p_hint == PROPERTY_HINT_LINK, true);
4046 return editor;
4047
4048 } break;
4049 case Variant::RECT2: {
4050 EditorPropertyRect2 *editor = memnew(EditorPropertyRect2(p_wide));
4051 editor->setup(_parse_range_hint(p_hint, p_hint_text, default_float_step));
4052 return editor;
4053 } break;
4054 case Variant::RECT2I: {
4055 EditorPropertyRect2i *editor = memnew(EditorPropertyRect2i(p_wide));
4056 editor->setup(_parse_range_hint(p_hint, p_hint_text, 1, true));
4057 return editor;
4058 } break;
4059 case Variant::VECTOR3: {
4060 EditorPropertyVector3 *editor = memnew(EditorPropertyVector3(p_wide));
4061 editor->setup(_parse_range_hint(p_hint, p_hint_text, default_float_step), p_hint == PROPERTY_HINT_LINK);
4062 return editor;
4063
4064 } break;
4065 case Variant::VECTOR3I: {
4066 EditorPropertyVector3i *editor = memnew(EditorPropertyVector3i(p_wide));
4067 EditorPropertyRangeHint hint = _parse_range_hint(p_hint, p_hint_text, 1, true);
4068 hint.step = Math::round(hint.step);
4069 editor->setup(hint, p_hint == PROPERTY_HINT_LINK, true);
4070 return editor;
4071
4072 } break;
4073 case Variant::VECTOR4: {
4074 EditorPropertyVector4 *editor = memnew(EditorPropertyVector4);
4075 editor->setup(_parse_range_hint(p_hint, p_hint_text, default_float_step), p_hint == PROPERTY_HINT_LINK);
4076 return editor;
4077
4078 } break;
4079 case Variant::VECTOR4I: {
4080 EditorPropertyVector4i *editor = memnew(EditorPropertyVector4i);
4081 EditorPropertyRangeHint hint = _parse_range_hint(p_hint, p_hint_text, 1, true);
4082 hint.step = Math::round(hint.step);
4083 editor->setup(hint, p_hint == PROPERTY_HINT_LINK, true);
4084 return editor;
4085
4086 } break;
4087 case Variant::TRANSFORM2D: {
4088 EditorPropertyTransform2D *editor = memnew(EditorPropertyTransform2D);
4089 editor->setup(_parse_range_hint(p_hint, p_hint_text, default_float_step));
4090 return editor;
4091 } break;
4092 case Variant::PLANE: {
4093 EditorPropertyPlane *editor = memnew(EditorPropertyPlane(p_wide));
4094 editor->setup(_parse_range_hint(p_hint, p_hint_text, default_float_step));
4095 return editor;
4096 } break;
4097 case Variant::QUATERNION: {
4098 EditorPropertyQuaternion *editor = memnew(EditorPropertyQuaternion);
4099 // Quaternions are almost never used for human-readable values that need stepifying,
4100 // so we should be more precise with their step, as much as the float precision allows.
4101 #ifdef REAL_T_IS_DOUBLE
4102 constexpr double QUATERNION_STEP = 1e-14;
4103 #else
4104 constexpr double QUATERNION_STEP = 1e-6;
4105 #endif
4106 editor->setup(_parse_range_hint(p_hint, p_hint_text, QUATERNION_STEP), p_hint == PROPERTY_HINT_HIDE_QUATERNION_EDIT);
4107 return editor;
4108 } break;
4109 case Variant::AABB: {
4110 EditorPropertyAABB *editor = memnew(EditorPropertyAABB);
4111 editor->setup(_parse_range_hint(p_hint, p_hint_text, default_float_step));
4112 return editor;
4113 } break;
4114 case Variant::BASIS: {
4115 EditorPropertyBasis *editor = memnew(EditorPropertyBasis);
4116 EditorPropertyRangeHint hint = _parse_range_hint(p_hint, p_hint_text, default_float_step);
4117 editor->setup(_parse_range_hint(p_hint, p_hint_text, default_float_step));
4118 return editor;
4119 } break;
4120 case Variant::TRANSFORM3D: {
4121 EditorPropertyTransform3D *editor = memnew(EditorPropertyTransform3D);
4122 editor->setup(_parse_range_hint(p_hint, p_hint_text, default_float_step));
4123 return editor;
4124
4125 } break;
4126 case Variant::PROJECTION: {
4127 EditorPropertyProjection *editor = memnew(EditorPropertyProjection);
4128 editor->setup(_parse_range_hint(p_hint, p_hint_text, default_float_step));
4129 return editor;
4130
4131 } break;
4132
4133 // misc types
4134 case Variant::COLOR: {
4135 EditorPropertyColor *editor = memnew(EditorPropertyColor);
4136 editor->setup(p_hint != PROPERTY_HINT_COLOR_NO_ALPHA);
4137 return editor;
4138 } break;
4139 case Variant::STRING_NAME: {
4140 if (p_hint == PROPERTY_HINT_ENUM || p_hint == PROPERTY_HINT_ENUM_SUGGESTION) {
4141 EditorPropertyTextEnum *editor = memnew(EditorPropertyTextEnum);
4142 Vector<String> options = p_hint_text.split(",", false);
4143 editor->setup(options, Vector<String>(), true, (p_hint == PROPERTY_HINT_ENUM_SUGGESTION));
4144 return editor;
4145 } else if (p_hint == PROPERTY_HINT_INPUT_NAME) {
4146 return get_input_action_editor(p_hint_text, true);
4147 } else {
4148 EditorPropertyText *editor = memnew(EditorPropertyText);
4149 if (p_hint == PROPERTY_HINT_PLACEHOLDER_TEXT) {
4150 editor->set_placeholder(p_hint_text);
4151 } else if (p_hint == PROPERTY_HINT_PASSWORD) {
4152 editor->set_secret(true);
4153 editor->set_placeholder(p_hint_text);
4154 }
4155 editor->set_string_name(true);
4156 return editor;
4157 }
4158 } break;
4159 case Variant::NODE_PATH: {
4160 EditorPropertyNodePath *editor = memnew(EditorPropertyNodePath);
4161 if (p_hint == PROPERTY_HINT_NODE_PATH_VALID_TYPES && !p_hint_text.is_empty()) {
4162 Vector<String> types = p_hint_text.split(",", false);
4163 Vector<StringName> sn = Variant(types); //convert via variant
4164 editor->setup(sn, (p_usage & PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT));
4165 }
4166 return editor;
4167
4168 } break;
4169 case Variant::RID: {
4170 EditorPropertyRID *editor = memnew(EditorPropertyRID);
4171 return editor;
4172 } break;
4173 case Variant::OBJECT: {
4174 if (p_hint == PROPERTY_HINT_NODE_TYPE) {
4175 EditorPropertyNodePath *editor = memnew(EditorPropertyNodePath);
4176 Vector<String> types = p_hint_text.split(",", false);
4177 Vector<StringName> sn = Variant(types); //convert via variant
4178 editor->setup(sn, false, true);
4179 return editor;
4180 } else {
4181 EditorPropertyResource *editor = memnew(EditorPropertyResource);
4182 editor->setup(p_object, p_path, p_hint == PROPERTY_HINT_RESOURCE_TYPE ? p_hint_text : "Resource");
4183
4184 if (p_hint == PROPERTY_HINT_RESOURCE_TYPE) {
4185 const PackedStringArray open_in_new_inspector = EDITOR_GET("interface/inspector/resources_to_open_in_new_inspector");
4186
4187 for (const String &type : open_in_new_inspector) {
4188 for (int j = 0; j < p_hint_text.get_slice_count(","); j++) {
4189 const String inherits = p_hint_text.get_slicec(',', j);
4190 if (ClassDB::is_parent_class(inherits, type)) {
4191 editor->set_use_sub_inspector(false);
4192 }
4193 }
4194 }
4195 }
4196
4197 return editor;
4198 }
4199
4200 } break;
4201 case Variant::CALLABLE: {
4202 EditorPropertyCallable *editor = memnew(EditorPropertyCallable);
4203 return editor;
4204 } break;
4205 case Variant::SIGNAL: {
4206 EditorPropertySignal *editor = memnew(EditorPropertySignal);
4207 return editor;
4208 } break;
4209 case Variant::DICTIONARY: {
4210 if (p_hint == PROPERTY_HINT_LOCALIZABLE_STRING) {
4211 EditorPropertyLocalizableString *editor = memnew(EditorPropertyLocalizableString);
4212 return editor;
4213 } else {
4214 EditorPropertyDictionary *editor = memnew(EditorPropertyDictionary);
4215 editor->setup(p_hint, p_hint_text);
4216 return editor;
4217 }
4218 } break;
4219 case Variant::ARRAY: {
4220 EditorPropertyArray *editor = memnew(EditorPropertyArray);
4221 editor->setup(Variant::ARRAY, p_hint_text);
4222 return editor;
4223 } break;
4224 case Variant::PACKED_BYTE_ARRAY: {
4225 EditorPropertyArray *editor = memnew(EditorPropertyArray);
4226 editor->setup(Variant::PACKED_BYTE_ARRAY, p_hint_text);
4227 return editor;
4228 } break;
4229 case Variant::PACKED_INT32_ARRAY: {
4230 EditorPropertyArray *editor = memnew(EditorPropertyArray);
4231 editor->setup(Variant::PACKED_INT32_ARRAY, p_hint_text);
4232 return editor;
4233 } break;
4234 case Variant::PACKED_INT64_ARRAY: {
4235 EditorPropertyArray *editor = memnew(EditorPropertyArray);
4236 editor->setup(Variant::PACKED_INT64_ARRAY, p_hint_text);
4237 return editor;
4238 } break;
4239 case Variant::PACKED_FLOAT32_ARRAY: {
4240 EditorPropertyArray *editor = memnew(EditorPropertyArray);
4241 editor->setup(Variant::PACKED_FLOAT32_ARRAY, p_hint_text);
4242 return editor;
4243 } break;
4244 case Variant::PACKED_FLOAT64_ARRAY: {
4245 EditorPropertyArray *editor = memnew(EditorPropertyArray);
4246 editor->setup(Variant::PACKED_FLOAT64_ARRAY, p_hint_text);
4247 return editor;
4248 } break;
4249 case Variant::PACKED_STRING_ARRAY: {
4250 EditorPropertyArray *editor = memnew(EditorPropertyArray);
4251 editor->setup(Variant::PACKED_STRING_ARRAY, p_hint_text);
4252 return editor;
4253 } break;
4254 case Variant::PACKED_VECTOR2_ARRAY: {
4255 EditorPropertyArray *editor = memnew(EditorPropertyArray);
4256 editor->setup(Variant::PACKED_VECTOR2_ARRAY, p_hint_text);
4257 return editor;
4258 } break;
4259 case Variant::PACKED_VECTOR3_ARRAY: {
4260 EditorPropertyArray *editor = memnew(EditorPropertyArray);
4261 editor->setup(Variant::PACKED_VECTOR3_ARRAY, p_hint_text);
4262 return editor;
4263 } break;
4264 case Variant::PACKED_COLOR_ARRAY: {
4265 EditorPropertyArray *editor = memnew(EditorPropertyArray);
4266 editor->setup(Variant::PACKED_COLOR_ARRAY, p_hint_text);
4267 return editor;
4268 } break;
4269 case Variant::PACKED_VECTOR4_ARRAY: {
4270 EditorPropertyArray *editor = memnew(EditorPropertyArray);
4271 editor->setup(Variant::PACKED_VECTOR4_ARRAY, p_hint_text);
4272 return editor;
4273 } break;
4274 default: {
4275 }
4276 }
4277
4278 return nullptr;
4279 }