editor_inspector.h 19 KB

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