1/**************************************************************************/
2/* editor_inspector.h */
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#ifndef EDITOR_INSPECTOR_H
32#define EDITOR_INSPECTOR_H
33
34#include "editor_property_name_processor.h"
35#include "scene/gui/box_container.h"
36#include "scene/gui/scroll_container.h"
37
38class AcceptDialog;
39class Button;
40class ConfirmationDialog;
41class EditorInspector;
42class EditorValidationPanel;
43class LineEdit;
44class OptionButton;
45class PanelContainer;
46class PopupMenu;
47class SpinBox;
48class StyleBoxFlat;
49class TextureRect;
50
51class EditorPropertyRevert {
52public:
53 static Variant get_property_revert_value(Object *p_object, const StringName &p_property, bool *r_is_valid);
54 static bool can_property_revert(Object *p_object, const StringName &p_property, const Variant *p_custom_current_value = nullptr);
55};
56
57class EditorProperty : public Container {
58 GDCLASS(EditorProperty, Container);
59
60public:
61 enum MenuItems {
62 MENU_COPY_VALUE,
63 MENU_PASTE_VALUE,
64 MENU_COPY_PROPERTY_PATH,
65 MENU_PIN_VALUE,
66 MENU_OPEN_DOCUMENTATION,
67 };
68
69private:
70 String label;
71 int text_size;
72 friend class EditorInspector;
73 Object *object = nullptr;
74 StringName property;
75 String property_path;
76 String doc_path;
77
78 int property_usage;
79
80 bool read_only = false;
81 bool checkable = false;
82 bool checked = false;
83 bool draw_warning = false;
84 bool draw_prop_warning = false;
85 bool keying = false;
86 bool deletable = false;
87
88 Rect2 right_child_rect;
89 Rect2 bottom_child_rect;
90
91 Rect2 keying_rect;
92 bool keying_hover = false;
93 Rect2 revert_rect;
94 bool revert_hover = false;
95 Rect2 check_rect;
96 bool check_hover = false;
97 Rect2 delete_rect;
98 bool delete_hover = false;
99
100 bool can_revert = false;
101 bool can_pin = false;
102 bool pin_hidden = false;
103 bool pinned = false;
104
105 bool use_folding = false;
106 bool draw_top_bg = true;
107
108 void _update_popup();
109 void _focusable_focused(int p_index);
110
111 bool selectable = true;
112 bool selected = false;
113 int selected_focusable;
114
115 float split_ratio;
116
117 Vector<Control *> focusables;
118 Control *label_reference = nullptr;
119 Control *bottom_editor = nullptr;
120 PopupMenu *menu = nullptr;
121
122 HashMap<StringName, Variant> cache;
123
124 GDVIRTUAL0(_update_property)
125 GDVIRTUAL1(_set_read_only, bool)
126
127 void _update_pin_flags();
128
129protected:
130 void _notification(int p_what);
131 static void _bind_methods();
132 virtual void _set_read_only(bool p_read_only);
133
134 virtual void gui_input(const Ref<InputEvent> &p_event) override;
135 virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
136 const Color *_get_property_colors();
137
138 virtual Variant _get_cache_value(const StringName &p_prop, bool &r_valid) const;
139 virtual StringName _get_revert_property() const;
140
141public:
142 void emit_changed(const StringName &p_property, const Variant &p_value, const StringName &p_field = StringName(), bool p_changing = false);
143
144 virtual Size2 get_minimum_size() const override;
145
146 void set_label(const String &p_label);
147 String get_label() const;
148
149 void set_read_only(bool p_read_only);
150 bool is_read_only() const;
151
152 Object *get_edited_object();
153 StringName get_edited_property() const;
154 inline Variant get_edited_property_value() const { return object->get(property); }
155 EditorInspector *get_parent_inspector() const;
156
157 void set_doc_path(const String &p_doc_path);
158
159 virtual void update_property();
160 void update_editor_property_status();
161
162 virtual bool use_keying_next() const;
163
164 void set_checkable(bool p_checkable);
165 bool is_checkable() const;
166
167 void set_checked(bool p_checked);
168 bool is_checked() const;
169
170 void set_draw_warning(bool p_draw_warning);
171 bool is_draw_warning() const;
172
173 void set_keying(bool p_keying);
174 bool is_keying() const;
175
176 void set_deletable(bool p_enable);
177 bool is_deletable() const;
178 void add_focusable(Control *p_control);
179 void select(int p_focusable = -1);
180 void deselect();
181 bool is_selected() const;
182
183 void set_label_reference(Control *p_control);
184 void set_bottom_editor(Control *p_control);
185
186 void set_use_folding(bool p_use_folding);
187 bool is_using_folding() const;
188
189 virtual void expand_all_folding();
190 virtual void collapse_all_folding();
191 virtual void expand_revertable();
192
193 virtual Variant get_drag_data(const Point2 &p_point) override;
194 virtual void update_cache();
195 virtual bool is_cache_valid() const;
196
197 void set_selectable(bool p_selectable);
198 bool is_selectable() const;
199
200 void set_name_split_ratio(float p_ratio);
201 float get_name_split_ratio() const;
202
203 void set_object_and_property(Object *p_object, const StringName &p_property);
204 virtual Control *make_custom_tooltip(const String &p_text) const override;
205
206 void set_draw_top_bg(bool p_draw) { draw_top_bg = p_draw; }
207
208 bool can_revert_to_default() const { return can_revert; }
209
210 void menu_option(int p_option);
211
212 EditorProperty();
213};
214
215class EditorInspectorPlugin : public RefCounted {
216 GDCLASS(EditorInspectorPlugin, RefCounted);
217
218public:
219 friend class EditorInspector;
220 struct AddedEditor {
221 Control *property_editor = nullptr;
222 Vector<String> properties;
223 String label;
224 bool add_to_end = false;
225 };
226
227 List<AddedEditor> added_editors;
228
229protected:
230 static void _bind_methods();
231
232 GDVIRTUAL1RC(bool, _can_handle, Object *)
233 GDVIRTUAL1(_parse_begin, Object *)
234 GDVIRTUAL2(_parse_category, Object *, String)
235 GDVIRTUAL2(_parse_group, Object *, String)
236 GDVIRTUAL7R(bool, _parse_property, Object *, Variant::Type, String, PropertyHint, String, BitField<PropertyUsageFlags>, bool)
237 GDVIRTUAL1(_parse_end, Object *)
238
239public:
240 void add_custom_control(Control *control);
241 void add_property_editor(const String &p_for_property, Control *p_prop, bool p_add_to_end = false);
242 void add_property_editor_for_multiple_properties(const String &p_label, const Vector<String> &p_properties, Control *p_prop);
243
244 virtual bool can_handle(Object *p_object);
245 virtual void parse_begin(Object *p_object);
246 virtual void parse_category(Object *p_object, const String &p_category);
247 virtual void parse_group(Object *p_object, const String &p_group);
248 virtual bool 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 = false);
249 virtual void parse_end(Object *p_object);
250};
251
252class EditorInspectorCategory : public Control {
253 GDCLASS(EditorInspectorCategory, Control);
254
255 friend class EditorInspector;
256
257 // Right-click context menu options.
258 enum ClassMenuOption {
259 MENU_OPEN_DOCS,
260 };
261
262 Ref<Texture2D> icon;
263 String label;
264 String doc_class_name;
265 PopupMenu *menu = nullptr;
266
267 void _handle_menu_option(int p_option);
268
269protected:
270 void _notification(int p_what);
271 virtual void gui_input(const Ref<InputEvent> &p_event) override;
272
273public:
274 virtual Size2 get_minimum_size() const override;
275 virtual Control *make_custom_tooltip(const String &p_text) const override;
276
277 EditorInspectorCategory();
278};
279
280class EditorInspectorSection : public Container {
281 GDCLASS(EditorInspectorSection, Container);
282
283 String label;
284 String section;
285 bool vbox_added = false; // Optimization.
286 Color bg_color;
287 bool foldable = false;
288 int indent_depth = 0;
289
290 Timer *dropping_unfold_timer = nullptr;
291 bool dropping = false;
292 bool dropping_for_unfold = false;
293
294 HashSet<StringName> revertable_properties;
295
296 void _test_unfold();
297 int _get_header_height();
298 Ref<Texture2D> _get_arrow();
299
300protected:
301 Object *object = nullptr;
302 VBoxContainer *vbox = nullptr;
303
304 void _notification(int p_what);
305 static void _bind_methods();
306 virtual void gui_input(const Ref<InputEvent> &p_event) override;
307
308public:
309 virtual Size2 get_minimum_size() const override;
310
311 void setup(const String &p_section, const String &p_label, Object *p_object, const Color &p_bg_color, bool p_foldable, int p_indent_depth = 0);
312 VBoxContainer *get_vbox();
313 void unfold();
314 void fold();
315 void set_bg_color(const Color &p_bg_color);
316
317 bool has_revertable_properties() const;
318 void property_can_revert_changed(const String &p_path, bool p_can_revert);
319
320 EditorInspectorSection();
321 ~EditorInspectorSection();
322};
323
324class EditorInspectorArray : public EditorInspectorSection {
325 GDCLASS(EditorInspectorArray, EditorInspectorSection);
326
327 enum Mode {
328 MODE_NONE,
329 MODE_USE_COUNT_PROPERTY,
330 MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION,
331 } mode;
332 StringName count_property;
333 StringName array_element_prefix;
334 String swap_method;
335
336 int count = 0;
337
338 VBoxContainer *elements_vbox = nullptr;
339
340 Control *control_dropping = nullptr;
341 bool dropping = false;
342
343 Button *add_button = nullptr;
344
345 AcceptDialog *resize_dialog = nullptr;
346 SpinBox *new_size_spin_box = nullptr;
347
348 // Pagination.
349 int page_length = 5;
350 int page = 0;
351 int max_page = 0;
352 int begin_array_index = 0;
353 int end_array_index = 0;
354
355 bool read_only = false;
356 bool movable = true;
357 bool numbered = false;
358
359 enum MenuOptions {
360 OPTION_MOVE_UP = 0,
361 OPTION_MOVE_DOWN,
362 OPTION_NEW_BEFORE,
363 OPTION_NEW_AFTER,
364 OPTION_REMOVE,
365 OPTION_CLEAR_ARRAY,
366 OPTION_RESIZE_ARRAY,
367 };
368 int popup_array_index_pressed = -1;
369 PopupMenu *rmb_popup = nullptr;
370
371 struct ArrayElement {
372 PanelContainer *panel = nullptr;
373 MarginContainer *margin = nullptr;
374 HBoxContainer *hbox = nullptr;
375 Button *move_up = nullptr;
376 TextureRect *move_texture_rect = nullptr;
377 Button *move_down = nullptr;
378 Label *number = nullptr;
379 VBoxContainer *vbox = nullptr;
380 Button *erase = nullptr;
381 };
382 LocalVector<ArrayElement> array_elements;
383
384 Ref<StyleBoxFlat> odd_style;
385 Ref<StyleBoxFlat> even_style;
386
387 int _get_array_count();
388 void _add_button_pressed();
389 void _paginator_page_changed(int p_page);
390
391 void _rmb_popup_id_pressed(int p_id);
392
393 void _control_dropping_draw();
394
395 void _vbox_visibility_changed();
396
397 void _panel_draw(int p_index);
398 void _panel_gui_input(Ref<InputEvent> p_event, int p_index);
399 void _move_element(int p_element_index, int p_to_pos);
400 void _clear_array();
401 void _resize_array(int p_size);
402 Array _extract_properties_as_array(const List<PropertyInfo> &p_list);
403 int _drop_position() const;
404
405 void _new_size_spin_box_value_changed(float p_value);
406 void _new_size_spin_box_text_submitted(String p_text);
407 void _resize_dialog_confirmed();
408
409 void _update_elements_visibility();
410 void _setup();
411
412 Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
413 void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
414 bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
415
416 void _remove_item(int p_index);
417
418protected:
419 void _notification(int p_what);
420 static void _bind_methods();
421
422public:
423 void setup_with_move_element_function(Object *p_object, String p_label, const StringName &p_array_element_prefix, int p_page, const Color &p_bg_color, bool p_foldable, bool p_movable = true, bool p_numbered = false, int p_page_length = 5, const String &p_add_item_text = "");
424 void setup_with_count_property(Object *p_object, String p_label, const StringName &p_count_property, const StringName &p_array_element_prefix, int p_page, const Color &p_bg_color, bool p_foldable, bool p_movable = true, bool p_numbered = false, int p_page_length = 5, const String &p_add_item_text = "", const String &p_swap_method = "");
425 VBoxContainer *get_vbox(int p_index);
426
427 EditorInspectorArray(bool p_read_only);
428};
429
430class EditorPaginator : public HBoxContainer {
431 GDCLASS(EditorPaginator, HBoxContainer);
432
433 int page = 0;
434 int max_page = 0;
435 Button *first_page_button = nullptr;
436 Button *prev_page_button = nullptr;
437 LineEdit *page_line_edit = nullptr;
438 Label *page_count_label = nullptr;
439 Button *next_page_button = nullptr;
440 Button *last_page_button = nullptr;
441
442 void _first_page_button_pressed();
443 void _prev_page_button_pressed();
444 void _page_line_edit_text_submitted(String p_text);
445 void _next_page_button_pressed();
446 void _last_page_button_pressed();
447
448protected:
449 void _notification(int p_what);
450 static void _bind_methods();
451
452public:
453 void update(int p_page, int p_max_page);
454
455 EditorPaginator();
456};
457
458class EditorInspector : public ScrollContainer {
459 GDCLASS(EditorInspector, ScrollContainer);
460
461 enum {
462 MAX_PLUGINS = 1024
463 };
464 static Ref<EditorInspectorPlugin> inspector_plugins[MAX_PLUGINS];
465 static int inspector_plugin_count;
466
467 VBoxContainer *main_vbox = nullptr;
468
469 // Map used to cache the instantiated editors.
470 HashMap<StringName, List<EditorProperty *>> editor_property_map;
471 List<EditorInspectorSection *> sections;
472 HashSet<StringName> pending;
473
474 void _clear(bool p_hide_plugins = true);
475 Object *object = nullptr;
476
477 //
478
479 LineEdit *search_box = nullptr;
480 bool show_categories = false;
481 bool hide_script = true;
482 bool hide_metadata = true;
483 bool use_doc_hints = false;
484 EditorPropertyNameProcessor::Style property_name_style = EditorPropertyNameProcessor::STYLE_CAPITALIZED;
485 bool use_settings_name_style = true;
486 bool use_filter = false;
487 bool autoclear = false;
488 bool use_folding = false;
489 int changing;
490 bool update_all_pending = false;
491 bool read_only = false;
492 bool keying = false;
493 bool sub_inspector = false;
494 bool wide_editors = false;
495 bool deletable_properties = false;
496
497 float refresh_countdown;
498 bool update_tree_pending = false;
499 StringName _prop_edited;
500 StringName property_selected;
501 int property_focusable;
502 int update_scroll_request;
503
504 struct PropertyDocInfo {
505 String description;
506 String path;
507 };
508
509 HashMap<StringName, HashMap<StringName, PropertyDocInfo>> doc_info_cache;
510 HashMap<StringName, String> class_descr_cache;
511 HashSet<StringName> restart_request_props;
512
513 HashMap<ObjectID, int> scroll_cache;
514
515 String property_prefix; // Used for sectioned inspector.
516 String object_class;
517 Variant property_clipboard;
518
519 bool restrict_to_basic = false;
520
521 void _edit_set(const String &p_name, const Variant &p_value, bool p_refresh_all, const String &p_changed_field);
522
523 void _property_changed(const String &p_path, const Variant &p_value, const String &p_name = "", bool p_changing = false, bool p_update_all = false);
524 void _multiple_properties_changed(Vector<String> p_paths, Array p_values, bool p_changing = false);
525 void _property_keyed(const String &p_path, bool p_advance);
526 void _property_keyed_with_value(const String &p_path, const Variant &p_value, bool p_advance);
527 void _property_deleted(const String &p_path);
528 void _property_checked(const String &p_path, bool p_checked);
529 void _property_pinned(const String &p_path, bool p_pinned);
530 bool _property_path_matches(const String &p_property_path, const String &p_filter, EditorPropertyNameProcessor::Style p_style);
531
532 void _resource_selected(const String &p_path, Ref<Resource> p_resource);
533 void _property_selected(const String &p_path, int p_focusable);
534 void _object_id_selected(const String &p_path, ObjectID p_id);
535
536 void _node_removed(Node *p_node);
537
538 HashMap<StringName, int> per_array_page;
539 void _page_change_request(int p_new_page, const StringName &p_array_prefix);
540
541 void _changed_callback();
542 void _edit_request_change(Object *p_object, const String &p_prop);
543
544 void _keying_changed();
545
546 void _filter_changed(const String &p_text);
547 void _parse_added_editors(VBoxContainer *current_vbox, EditorInspectorSection *p_section, Ref<EditorInspectorPlugin> ped);
548
549 void _vscroll_changed(double);
550
551 void _feature_profile_changed();
552
553 bool _is_property_disabled_by_feature_profile(const StringName &p_property);
554
555 void _update_inspector_bg();
556
557 ConfirmationDialog *add_meta_dialog = nullptr;
558 LineEdit *add_meta_name = nullptr;
559 OptionButton *add_meta_type = nullptr;
560 EditorValidationPanel *validation_panel = nullptr;
561
562 void _add_meta_confirm();
563 void _show_add_meta_dialog();
564 void _check_meta_name();
565
566protected:
567 static void _bind_methods();
568 void _notification(int p_what);
569
570public:
571 static void add_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin);
572 static void remove_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin);
573 static void cleanup_plugins();
574 static Button *create_inspector_action_button(const String &p_text);
575
576 static EditorProperty *instantiate_property_editor(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const uint32_t p_usage, const bool p_wide = false);
577
578 bool is_main_editor_inspector() const;
579 String get_selected_path() const;
580
581 void update_tree();
582 void update_property(const String &p_prop);
583 void edit(Object *p_object);
584 Object *get_edited_object();
585
586 void set_keying(bool p_active);
587 void set_read_only(bool p_read_only);
588
589 EditorPropertyNameProcessor::Style get_property_name_style() const;
590 void set_property_name_style(EditorPropertyNameProcessor::Style p_style);
591
592 // If true, the inspector will update its property name style according to the current editor settings.
593 void set_use_settings_name_style(bool p_enable);
594
595 void set_autoclear(bool p_enable);
596
597 void set_show_categories(bool p_show);
598 void set_use_doc_hints(bool p_enable);
599 void set_hide_script(bool p_hide);
600 void set_hide_metadata(bool p_hide);
601
602 void set_use_filter(bool p_use);
603 void register_text_enter(Node *p_line_edit);
604
605 void set_use_folding(bool p_use_folding, bool p_update_tree = true);
606 bool is_using_folding();
607
608 void collapse_all_folding();
609 void expand_all_folding();
610 void expand_revertable();
611
612 void set_scroll_offset(int p_offset);
613 int get_scroll_offset() const;
614
615 void set_property_prefix(const String &p_prefix);
616 String get_property_prefix() const;
617
618 void set_object_class(const String &p_class);
619 String get_object_class() const;
620
621 void set_use_wide_editors(bool p_enable);
622 void set_sub_inspector(bool p_enable);
623 bool is_sub_inspector() const { return sub_inspector; }
624
625 void set_use_deletable_properties(bool p_enabled);
626
627 void set_restrict_to_basic_settings(bool p_restrict);
628 void set_property_clipboard(const Variant &p_value);
629 Variant get_property_clipboard() const;
630
631 EditorInspector();
632};
633
634#endif // EDITOR_INSPECTOR_H
635