editor_inspector.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 "scene/gui/box_container.h"
  33. #include "scene/gui/line_edit.h"
  34. #include "scene/gui/scroll_container.h"
  35. class UndoRedo;
  36. class EditorPropertyRevert {
  37. public:
  38. static bool may_node_be_in_instance(Node *p_node);
  39. static bool get_instanced_node_original_property(Node *p_node, const StringName &p_prop, Variant &value, bool p_check_class_default = true);
  40. static bool is_node_property_different(Node *p_node, const Variant &p_current, const Variant &p_orig);
  41. static bool is_property_value_different(const Variant &p_a, const Variant &p_b);
  42. static Variant get_property_revert_value(Object *p_object, const StringName &p_property);
  43. static bool can_property_revert(Object *p_object, const StringName &p_property);
  44. };
  45. class EditorProperty : public Container {
  46. GDCLASS(EditorProperty, Container);
  47. private:
  48. String label;
  49. int text_size;
  50. friend class EditorInspector;
  51. Object *object;
  52. StringName property;
  53. int property_usage;
  54. bool read_only;
  55. bool checkable;
  56. bool checked;
  57. bool draw_red;
  58. bool keying;
  59. Rect2 right_child_rect;
  60. Rect2 bottom_child_rect;
  61. Rect2 keying_rect;
  62. bool keying_hover;
  63. Rect2 revert_rect;
  64. bool revert_hover;
  65. Rect2 check_rect;
  66. bool check_hover;
  67. bool can_revert;
  68. bool use_folding;
  69. bool draw_top_bg;
  70. bool _is_property_different(const Variant &p_current, const Variant &p_orig);
  71. bool _get_instanced_node_original_property(const StringName &p_prop, Variant &value);
  72. void _focusable_focused(int p_index);
  73. bool selectable;
  74. bool selected;
  75. int selected_focusable;
  76. float split_ratio;
  77. Vector<Control *> focusables;
  78. Control *label_reference;
  79. Control *bottom_editor;
  80. mutable String tooltip_text;
  81. protected:
  82. void _notification(int p_what);
  83. static void _bind_methods();
  84. void _gui_input(const Ref<InputEvent> &p_event);
  85. public:
  86. void emit_changed(const StringName &p_property, const Variant &p_value, const StringName &p_field = StringName(), bool p_changing = false);
  87. virtual Size2 get_minimum_size() const;
  88. void set_label(const String &p_label);
  89. String get_label() const;
  90. void set_read_only(bool p_read_only);
  91. bool is_read_only() const;
  92. Object *get_edited_object();
  93. StringName get_edited_property();
  94. virtual void update_property();
  95. void update_reload_status();
  96. virtual bool use_keying_next() const;
  97. void set_checkable(bool p_checkable);
  98. bool is_checkable() const;
  99. void set_checked(bool p_checked);
  100. bool is_checked() const;
  101. void set_draw_red(bool p_draw_red);
  102. bool is_draw_red() const;
  103. void set_keying(bool p_keying);
  104. bool is_keying() const;
  105. void add_focusable(Control *p_control);
  106. void select(int p_focusable = -1);
  107. void deselect();
  108. bool is_selected() const;
  109. void set_label_reference(Control *p_control);
  110. void set_bottom_editor(Control *p_control);
  111. void set_use_folding(bool p_use_folding);
  112. bool is_using_folding() const;
  113. virtual void expand_all_folding();
  114. virtual void collapse_all_folding();
  115. virtual Variant get_drag_data(const Point2 &p_point);
  116. void set_selectable(bool p_selectable);
  117. bool is_selectable() const;
  118. void set_name_split_ratio(float p_ratio);
  119. float get_name_split_ratio() const;
  120. void set_object_and_property(Object *p_object, const StringName &p_property);
  121. virtual Control *make_custom_tooltip(const String &p_text) const;
  122. String get_tooltip_text() const;
  123. void set_draw_top_bg(bool p_draw) { draw_top_bg = p_draw; }
  124. bool can_revert_to_default() const { return can_revert; }
  125. EditorProperty();
  126. };
  127. class EditorInspectorPlugin : public Reference {
  128. GDCLASS(EditorInspectorPlugin, Reference);
  129. friend class EditorInspector;
  130. struct AddedEditor {
  131. Control *property_editor;
  132. Vector<String> properties;
  133. String label;
  134. };
  135. List<AddedEditor> added_editors;
  136. protected:
  137. static void _bind_methods();
  138. public:
  139. void add_custom_control(Control *control);
  140. void add_property_editor(const String &p_for_property, Control *p_prop);
  141. void add_property_editor_for_multiple_properties(const String &p_label, const Vector<String> &p_properties, Control *p_prop);
  142. virtual bool can_handle(Object *p_object);
  143. virtual void parse_begin(Object *p_object);
  144. virtual void parse_category(Object *p_object, const String &p_parse_category);
  145. virtual bool parse_property(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage);
  146. virtual void parse_end();
  147. };
  148. class EditorInspectorCategory : public Control {
  149. GDCLASS(EditorInspectorCategory, Control);
  150. friend class EditorInspector;
  151. Ref<Texture> icon;
  152. String label;
  153. Color bg_color;
  154. mutable String tooltip_text;
  155. protected:
  156. void _notification(int p_what);
  157. static void _bind_methods();
  158. public:
  159. virtual Size2 get_minimum_size() const;
  160. virtual Control *make_custom_tooltip(const String &p_text) const;
  161. String get_tooltip_text() const;
  162. EditorInspectorCategory();
  163. };
  164. class EditorInspectorSection : public Container {
  165. GDCLASS(EditorInspectorSection, Container);
  166. String label;
  167. String section;
  168. Object *object;
  169. VBoxContainer *vbox;
  170. bool vbox_added; //optimization
  171. Color bg_color;
  172. bool foldable;
  173. void _test_unfold();
  174. protected:
  175. void _notification(int p_what);
  176. static void _bind_methods();
  177. void _gui_input(const Ref<InputEvent> &p_event);
  178. public:
  179. virtual Size2 get_minimum_size() const;
  180. void setup(const String &p_section, const String &p_label, Object *p_object, const Color &p_bg_color, bool p_foldable);
  181. VBoxContainer *get_vbox();
  182. void unfold();
  183. void fold();
  184. Object *get_edited_object();
  185. EditorInspectorSection();
  186. ~EditorInspectorSection();
  187. };
  188. class EditorInspector : public ScrollContainer {
  189. GDCLASS(EditorInspector, ScrollContainer);
  190. UndoRedo *undo_redo;
  191. enum {
  192. MAX_PLUGINS = 1024
  193. };
  194. static Ref<EditorInspectorPlugin> inspector_plugins[MAX_PLUGINS];
  195. static int inspector_plugin_count;
  196. VBoxContainer *main_vbox;
  197. //map use to cache the instanced editors
  198. Map<StringName, List<EditorProperty *>> editor_property_map;
  199. List<EditorInspectorSection *> sections;
  200. Set<StringName> pending;
  201. void _clear();
  202. Object *object;
  203. //
  204. LineEdit *search_box;
  205. bool show_categories;
  206. bool hide_script;
  207. bool use_doc_hints;
  208. bool capitalize_paths;
  209. bool use_filter;
  210. bool autoclear;
  211. bool use_folding;
  212. int changing;
  213. bool update_all_pending;
  214. bool read_only;
  215. bool keying;
  216. bool sub_inspector;
  217. float refresh_countdown;
  218. bool update_tree_pending;
  219. StringName _prop_edited;
  220. StringName property_selected;
  221. int property_focusable;
  222. int update_scroll_request;
  223. Map<StringName, Map<StringName, String>> descr_cache;
  224. Map<StringName, String> class_descr_cache;
  225. Set<StringName> restart_request_props;
  226. Map<ObjectID, int> scroll_cache;
  227. String property_prefix; //used for sectioned inspector
  228. String object_class;
  229. void _edit_set(const String &p_name, const Variant &p_value, bool p_refresh_all, const String &p_changed_field);
  230. void _property_changed(const String &p_path, const Variant &p_value, const String &p_name = "", bool changing = false);
  231. void _property_changed_update_all(const String &p_path, const Variant &p_value, const String &p_name = "", bool p_changing = false);
  232. void _multiple_properties_changed(Vector<String> p_paths, Array p_values);
  233. void _property_keyed(const String &p_path, bool p_advance);
  234. void _property_keyed_with_value(const String &p_path, const Variant &p_value, bool p_advance);
  235. void _property_checked(const String &p_path, bool p_checked);
  236. void _resource_selected(const String &p_path, RES p_resource);
  237. void _property_selected(const String &p_path, int p_focusable);
  238. void _object_id_selected(const String &p_path, ObjectID p_id);
  239. void _node_removed(Node *p_node);
  240. void _changed_callback(Object *p_changed, const char *p_prop);
  241. void _edit_request_change(Object *p_object, const String &p_prop);
  242. void _filter_changed(const String &p_text);
  243. void _parse_added_editors(VBoxContainer *current_vbox, Ref<EditorInspectorPlugin> ped);
  244. void _vscroll_changed(double);
  245. void _feature_profile_changed();
  246. bool _is_property_disabled_by_feature_profile(const StringName &p_property);
  247. void _update_inspector_bg();
  248. protected:
  249. static void _bind_methods();
  250. void _notification(int p_what);
  251. public:
  252. static void add_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin);
  253. static void remove_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin);
  254. static void cleanup_plugins();
  255. static EditorProperty *instantiate_property_editor(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage);
  256. void set_undo_redo(UndoRedo *p_undo_redo);
  257. String get_selected_path() const;
  258. void update_tree();
  259. void update_property(const String &p_prop);
  260. void refresh();
  261. void edit(Object *p_object);
  262. Object *get_edited_object();
  263. void set_keying(bool p_active);
  264. void set_read_only(bool p_read_only);
  265. bool is_capitalize_paths_enabled() const;
  266. void set_enable_capitalize_paths(bool p_capitalize);
  267. void set_autoclear(bool p_enable);
  268. void set_show_categories(bool p_show);
  269. void set_use_doc_hints(bool p_enable);
  270. void set_hide_script(bool p_hide);
  271. void set_use_filter(bool p_use);
  272. void register_text_enter(Node *p_line_edit);
  273. void set_use_folding(bool p_enable);
  274. bool is_using_folding();
  275. void collapse_all_folding();
  276. void expand_all_folding();
  277. void set_scroll_offset(int p_offset);
  278. int get_scroll_offset() const;
  279. void set_property_prefix(const String &p_prefix);
  280. String get_property_prefix() const;
  281. void set_object_class(const String &p_class);
  282. String get_object_class() const;
  283. void set_sub_inspector(bool p_enable);
  284. bool is_sub_inspector() const { return sub_inspector; }
  285. EditorInspector();
  286. };
  287. #endif // INSPECTOR_H