editor_inspector.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*************************************************************************/
  2. /* editor_inspector.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef EDITOR_INSPECTOR_H
  31. #define EDITOR_INSPECTOR_H
  32. #include "editor_property_name_processor.h"
  33. #include "scene/gui/box_container.h"
  34. #include "scene/gui/button.h"
  35. #include "scene/gui/dialogs.h"
  36. #include "scene/gui/line_edit.h"
  37. #include "scene/gui/option_button.h"
  38. #include "scene/gui/panel_container.h"
  39. #include "scene/gui/scroll_container.h"
  40. #include "scene/gui/texture_rect.h"
  41. class UndoRedo;
  42. class EditorPropertyRevert {
  43. public:
  44. static bool get_instantiated_node_original_property(Node *p_node, const StringName &p_prop, Variant &value, bool p_check_class_default = true);
  45. static bool is_node_property_different(Node *p_node, const Variant &p_current, const Variant &p_orig);
  46. static bool is_property_value_different(const Variant &p_a, const Variant &p_b);
  47. static Variant get_property_revert_value(Object *p_object, const StringName &p_property, bool *r_is_valid);
  48. static bool can_property_revert(Object *p_object, const StringName &p_property);
  49. };
  50. class EditorProperty : public Container {
  51. GDCLASS(EditorProperty, Container);
  52. public:
  53. enum MenuItems {
  54. MENU_COPY_PROPERTY,
  55. MENU_PASTE_PROPERTY,
  56. MENU_COPY_PROPERTY_PATH,
  57. MENU_PIN_VALUE,
  58. };
  59. private:
  60. String label;
  61. int text_size;
  62. friend class EditorInspector;
  63. Object *object = nullptr;
  64. StringName property;
  65. String property_path;
  66. int property_usage;
  67. bool read_only = false;
  68. bool checkable = false;
  69. bool checked = false;
  70. bool draw_warning = false;
  71. bool keying = false;
  72. bool deletable = false;
  73. Rect2 right_child_rect;
  74. Rect2 bottom_child_rect;
  75. Rect2 keying_rect;
  76. bool keying_hover = false;
  77. Rect2 revert_rect;
  78. bool revert_hover = false;
  79. Rect2 check_rect;
  80. bool check_hover = false;
  81. Rect2 delete_rect;
  82. bool delete_hover = false;
  83. bool can_revert = false;
  84. bool can_pin = false;
  85. bool pin_hidden = false;
  86. bool pinned = false;
  87. bool use_folding = false;
  88. bool draw_top_bg = true;
  89. void _update_popup();
  90. void _focusable_focused(int p_index);
  91. bool selectable = true;
  92. bool selected = false;
  93. int selected_focusable;
  94. float split_ratio;
  95. Vector<Control *> focusables;
  96. Control *label_reference = nullptr;
  97. Control *bottom_editor = nullptr;
  98. PopupMenu *menu = nullptr;
  99. mutable String tooltip_text;
  100. Map<StringName, Variant> cache;
  101. GDVIRTUAL0(_update_property)
  102. void _update_pin_flags();
  103. protected:
  104. void _notification(int p_what);
  105. static void _bind_methods();
  106. virtual void _set_read_only(bool p_read_only);
  107. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  108. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  109. const Color *_get_property_colors();
  110. public:
  111. void emit_changed(const StringName &p_property, const Variant &p_value, const StringName &p_field = StringName(), bool p_changing = false);
  112. virtual Size2 get_minimum_size() const override;
  113. void set_label(const String &p_label);
  114. String get_label() const;
  115. void set_read_only(bool p_read_only);
  116. bool is_read_only() const;
  117. Object *get_edited_object();
  118. StringName get_edited_property();
  119. virtual void update_property();
  120. void update_revert_and_pin_status();
  121. virtual bool use_keying_next() const;
  122. void set_checkable(bool p_checkable);
  123. bool is_checkable() const;
  124. void set_checked(bool p_checked);
  125. bool is_checked() const;
  126. void set_draw_warning(bool p_draw_warning);
  127. bool is_draw_warning() const;
  128. void set_keying(bool p_keying);
  129. bool is_keying() const;
  130. void set_deletable(bool p_enable);
  131. bool is_deletable() const;
  132. void add_focusable(Control *p_control);
  133. void select(int p_focusable = -1);
  134. void deselect();
  135. bool is_selected() const;
  136. void set_label_reference(Control *p_control);
  137. void set_bottom_editor(Control *p_control);
  138. void set_use_folding(bool p_use_folding);
  139. bool is_using_folding() const;
  140. virtual void expand_all_folding();
  141. virtual void collapse_all_folding();
  142. virtual Variant get_drag_data(const Point2 &p_point) override;
  143. virtual void update_cache();
  144. virtual bool is_cache_valid() const;
  145. void set_selectable(bool p_selectable);
  146. bool is_selectable() const;
  147. void set_name_split_ratio(float p_ratio);
  148. float get_name_split_ratio() const;
  149. void set_object_and_property(Object *p_object, const StringName &p_property);
  150. virtual Control *make_custom_tooltip(const String &p_text) const override;
  151. String get_tooltip_text() const;
  152. void set_draw_top_bg(bool p_draw) { draw_top_bg = p_draw; }
  153. bool can_revert_to_default() const { return can_revert; }
  154. void menu_option(int p_option);
  155. EditorProperty();
  156. };
  157. class EditorInspectorPlugin : public RefCounted {
  158. GDCLASS(EditorInspectorPlugin, RefCounted);
  159. friend class EditorInspector;
  160. struct AddedEditor {
  161. Control *property_editor = nullptr;
  162. Vector<String> properties;
  163. String label;
  164. };
  165. List<AddedEditor> added_editors;
  166. protected:
  167. static void _bind_methods();
  168. GDVIRTUAL1RC(bool, _can_handle, Variant)
  169. GDVIRTUAL1(_parse_begin, Object *)
  170. GDVIRTUAL2(_parse_category, Object *, String)
  171. GDVIRTUAL2(_parse_group, Object *, String)
  172. GDVIRTUAL7R(bool, _parse_property, Object *, int, String, int, String, int, bool)
  173. GDVIRTUAL1(_parse_end, Object *)
  174. public:
  175. void add_custom_control(Control *control);
  176. void add_property_editor(const String &p_for_property, Control *p_prop);
  177. void add_property_editor_for_multiple_properties(const String &p_label, const Vector<String> &p_properties, Control *p_prop);
  178. virtual bool can_handle(Object *p_object);
  179. virtual void parse_begin(Object *p_object);
  180. virtual void parse_category(Object *p_object, const String &p_category);
  181. virtual void parse_group(Object *p_object, const String &p_group);
  182. 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 uint32_t p_usage, const bool p_wide = false);
  183. virtual void parse_end(Object *p_object);
  184. };
  185. class EditorInspectorCategory : public Control {
  186. GDCLASS(EditorInspectorCategory, Control);
  187. friend class EditorInspector;
  188. Ref<Texture2D> icon;
  189. String label;
  190. mutable String tooltip_text;
  191. protected:
  192. void _notification(int p_what);
  193. static void _bind_methods();
  194. public:
  195. virtual Size2 get_minimum_size() const override;
  196. virtual Control *make_custom_tooltip(const String &p_text) const override;
  197. String get_tooltip_text() const;
  198. EditorInspectorCategory();
  199. };
  200. class EditorInspectorSection : public Container {
  201. GDCLASS(EditorInspectorSection, Container);
  202. String label;
  203. String section;
  204. bool vbox_added = false; // Optimization.
  205. Color bg_color;
  206. bool foldable = false;
  207. int indent_depth = 0;
  208. Timer *dropping_unfold_timer = nullptr;
  209. bool dropping = false;
  210. void _test_unfold();
  211. protected:
  212. Object *object = nullptr;
  213. VBoxContainer *vbox = nullptr;
  214. void _notification(int p_what);
  215. static void _bind_methods();
  216. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  217. public:
  218. virtual Size2 get_minimum_size() const override;
  219. 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);
  220. VBoxContainer *get_vbox();
  221. void unfold();
  222. void fold();
  223. EditorInspectorSection();
  224. ~EditorInspectorSection();
  225. };
  226. class EditorInspectorArray : public EditorInspectorSection {
  227. GDCLASS(EditorInspectorArray, EditorInspectorSection);
  228. UndoRedo *undo_redo = nullptr;
  229. enum Mode {
  230. MODE_NONE,
  231. MODE_USE_COUNT_PROPERTY,
  232. MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION,
  233. } mode;
  234. StringName count_property;
  235. StringName array_element_prefix;
  236. int count = 0;
  237. VBoxContainer *elements_vbox = nullptr;
  238. Control *control_dropping = nullptr;
  239. bool dropping = false;
  240. Button *add_button = nullptr;
  241. AcceptDialog *resize_dialog = nullptr;
  242. int new_size = 0;
  243. LineEdit *new_size_line_edit = nullptr;
  244. // Pagination
  245. int page_length = 5;
  246. int page = 0;
  247. int max_page = 0;
  248. int begin_array_index = 0;
  249. int end_array_index = 0;
  250. enum MenuOptions {
  251. OPTION_MOVE_UP = 0,
  252. OPTION_MOVE_DOWN,
  253. OPTION_NEW_BEFORE,
  254. OPTION_NEW_AFTER,
  255. OPTION_REMOVE,
  256. OPTION_CLEAR_ARRAY,
  257. OPTION_RESIZE_ARRAY,
  258. };
  259. int popup_array_index_pressed = -1;
  260. PopupMenu *rmb_popup = nullptr;
  261. struct ArrayElement {
  262. PanelContainer *panel = nullptr;
  263. MarginContainer *margin = nullptr;
  264. HBoxContainer *hbox = nullptr;
  265. TextureRect *move_texture_rect = nullptr;
  266. VBoxContainer *vbox = nullptr;
  267. };
  268. LocalVector<ArrayElement> array_elements;
  269. Ref<StyleBoxFlat> odd_style;
  270. Ref<StyleBoxFlat> even_style;
  271. int _get_array_count();
  272. void _add_button_pressed();
  273. void _paginator_page_changed(int p_page);
  274. void _rmb_popup_id_pressed(int p_id);
  275. void _control_dropping_draw();
  276. void _vbox_visibility_changed();
  277. void _panel_draw(int p_index);
  278. void _panel_gui_input(Ref<InputEvent> p_event, int p_index);
  279. void _move_element(int p_element_index, int p_to_pos);
  280. void _clear_array();
  281. void _resize_array(int p_size);
  282. Array _extract_properties_as_array(const List<PropertyInfo> &p_list);
  283. int _drop_position() const;
  284. void _new_size_line_edit_text_changed(String p_text);
  285. void _new_size_line_edit_text_submitted(String p_text);
  286. void _resize_dialog_confirmed();
  287. void _update_elements_visibility();
  288. void _setup();
  289. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  290. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  291. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  292. protected:
  293. void _notification(int p_what);
  294. static void _bind_methods();
  295. public:
  296. void set_undo_redo(UndoRedo *p_undo_redo);
  297. 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);
  298. 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);
  299. VBoxContainer *get_vbox(int p_index);
  300. EditorInspectorArray();
  301. };
  302. class EditorPaginator : public HBoxContainer {
  303. GDCLASS(EditorPaginator, HBoxContainer);
  304. int page = 0;
  305. int max_page = 0;
  306. Button *first_page_button = nullptr;
  307. Button *prev_page_button = nullptr;
  308. LineEdit *page_line_edit = nullptr;
  309. Label *page_count_label = nullptr;
  310. Button *next_page_button = nullptr;
  311. Button *last_page_button = nullptr;
  312. void _first_page_button_pressed();
  313. void _prev_page_button_pressed();
  314. void _page_line_edit_text_submitted(String p_text);
  315. void _next_page_button_pressed();
  316. void _last_page_button_pressed();
  317. protected:
  318. void _notification(int p_what);
  319. static void _bind_methods();
  320. public:
  321. void update(int p_page, int p_max_page);
  322. EditorPaginator();
  323. };
  324. class EditorInspector : public ScrollContainer {
  325. GDCLASS(EditorInspector, ScrollContainer);
  326. UndoRedo *undo_redo = nullptr;
  327. enum {
  328. MAX_PLUGINS = 1024
  329. };
  330. static Ref<EditorInspectorPlugin> inspector_plugins[MAX_PLUGINS];
  331. static int inspector_plugin_count;
  332. VBoxContainer *main_vbox = nullptr;
  333. //map use to cache the instantiated editors
  334. Map<StringName, List<EditorProperty *>> editor_property_map;
  335. List<EditorInspectorSection *> sections;
  336. Set<StringName> pending;
  337. void _clear();
  338. Object *object = nullptr;
  339. //
  340. LineEdit *search_box = nullptr;
  341. bool show_categories = false;
  342. bool hide_script = true;
  343. bool hide_metadata = true;
  344. bool use_doc_hints = false;
  345. EditorPropertyNameProcessor::Style property_name_style = EditorPropertyNameProcessor::STYLE_CAPITALIZED;
  346. bool use_filter = false;
  347. bool autoclear = false;
  348. bool use_folding = false;
  349. int changing;
  350. bool update_all_pending = false;
  351. bool read_only = false;
  352. bool keying = false;
  353. bool sub_inspector = false;
  354. bool wide_editors = false;
  355. bool deletable_properties = false;
  356. float refresh_countdown;
  357. bool update_tree_pending = false;
  358. StringName _prop_edited;
  359. StringName property_selected;
  360. int property_focusable;
  361. int update_scroll_request;
  362. Map<StringName, Map<StringName, String>> descr_cache;
  363. Map<StringName, String> class_descr_cache;
  364. Set<StringName> restart_request_props;
  365. Map<ObjectID, int> scroll_cache;
  366. String property_prefix; //used for sectioned inspector
  367. String object_class;
  368. Variant property_clipboard;
  369. bool restrict_to_basic = false;
  370. void _edit_set(const String &p_name, const Variant &p_value, bool p_refresh_all, const String &p_changed_field);
  371. void _property_changed(const String &p_path, const Variant &p_value, const String &p_name = "", bool p_changing = false, bool p_update_all = false);
  372. void _multiple_properties_changed(Vector<String> p_paths, Array p_values, bool p_changing = false);
  373. void _property_keyed(const String &p_path, bool p_advance);
  374. void _property_keyed_with_value(const String &p_path, const Variant &p_value, bool p_advance);
  375. void _property_deleted(const String &p_path);
  376. void _property_checked(const String &p_path, bool p_checked);
  377. void _property_pinned(const String &p_path, bool p_pinned);
  378. void _resource_selected(const String &p_path, Ref<Resource> p_resource);
  379. void _property_selected(const String &p_path, int p_focusable);
  380. void _object_id_selected(const String &p_path, ObjectID p_id);
  381. void _node_removed(Node *p_node);
  382. Map<StringName, int> per_array_page;
  383. void _page_change_request(int p_new_page, const StringName &p_array_prefix);
  384. void _changed_callback();
  385. void _edit_request_change(Object *p_object, const String &p_prop);
  386. void _filter_changed(const String &p_text);
  387. void _parse_added_editors(VBoxContainer *current_vbox, Ref<EditorInspectorPlugin> ped);
  388. void _vscroll_changed(double);
  389. void _feature_profile_changed();
  390. void _update_script_class_properties(const Object &p_object, List<PropertyInfo> &r_list) const;
  391. bool _is_property_disabled_by_feature_profile(const StringName &p_property);
  392. void _update_inspector_bg();
  393. ConfirmationDialog *add_meta_dialog = nullptr;
  394. LineEdit *add_meta_name = nullptr;
  395. OptionButton *add_meta_type = nullptr;
  396. Label *add_meta_error = nullptr;
  397. void _add_meta_confirm();
  398. void _show_add_meta_dialog();
  399. void _check_meta_name(String name);
  400. protected:
  401. static void _bind_methods();
  402. void _notification(int p_what);
  403. public:
  404. static void add_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin);
  405. static void remove_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin);
  406. static void cleanup_plugins();
  407. 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);
  408. void set_undo_redo(UndoRedo *p_undo_redo);
  409. String get_selected_path() const;
  410. void update_tree();
  411. void update_property(const String &p_prop);
  412. void edit(Object *p_object);
  413. Object *get_edited_object();
  414. void set_keying(bool p_active);
  415. void set_read_only(bool p_read_only);
  416. EditorPropertyNameProcessor::Style get_property_name_style() const;
  417. void set_property_name_style(EditorPropertyNameProcessor::Style p_style);
  418. void set_autoclear(bool p_enable);
  419. void set_show_categories(bool p_show);
  420. void set_use_doc_hints(bool p_enable);
  421. void set_hide_script(bool p_hide);
  422. void set_hide_metadata(bool p_hide);
  423. void set_use_filter(bool p_use);
  424. void register_text_enter(Node *p_line_edit);
  425. void set_use_folding(bool p_enable);
  426. bool is_using_folding();
  427. void collapse_all_folding();
  428. void expand_all_folding();
  429. void set_scroll_offset(int p_offset);
  430. int get_scroll_offset() const;
  431. void set_property_prefix(const String &p_prefix);
  432. String get_property_prefix() const;
  433. void set_object_class(const String &p_class);
  434. String get_object_class() const;
  435. void set_use_wide_editors(bool p_enable);
  436. void set_sub_inspector(bool p_enable);
  437. bool is_sub_inspector() const { return sub_inspector; }
  438. void set_use_deletable_properties(bool p_enabled);
  439. void set_restrict_to_basic_settings(bool p_restrict);
  440. void set_property_clipboard(const Variant &p_value);
  441. Variant get_property_clipboard() const;
  442. EditorInspector();
  443. };
  444. #endif // INSPECTOR_H