editor_data.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**************************************************************************/
  2. /* editor_data.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_DATA_H
  31. #define EDITOR_DATA_H
  32. #include "core/templates/list.h"
  33. #include "scene/resources/texture.h"
  34. class ConfigFile;
  35. class EditorPlugin;
  36. class EditorUndoRedoManager;
  37. class PopupMenu;
  38. /**
  39. * Stores the history of objects which have been selected for editing in the Editor & the Inspector.
  40. *
  41. * Used in the editor to set & access the currently edited object, as well as the history of objects which have been edited.
  42. */
  43. class EditorSelectionHistory {
  44. // Stores the object & property (if relevant).
  45. struct _Object {
  46. Ref<RefCounted> ref;
  47. ObjectID object;
  48. String property;
  49. bool inspector_only = false;
  50. };
  51. // Represents the selection of an object for editing.
  52. struct HistoryElement {
  53. // The sub-resources of the parent object (first in the path) that have been edited.
  54. // For example, Node2D -> nested resource -> nested resource, if edited each individually in their own inspector.
  55. Vector<_Object> path;
  56. // The current point in the path. This is always equal to the last item in the path - it is never decremented.
  57. int level = 0;
  58. };
  59. friend class EditorData;
  60. Vector<HistoryElement> history;
  61. int current_elem_idx; // The current history element being edited.
  62. public:
  63. void cleanup_history();
  64. bool is_at_beginning() const;
  65. bool is_at_end() const;
  66. // Adds an object to the selection history. A property name can be passed if the target is a subresource of the given object.
  67. // If the object should not change the main screen plugin, it can be set as inspector only.
  68. void add_object(ObjectID p_object, const String &p_property = String(), bool p_inspector_only = false);
  69. void replace_object(ObjectID p_old_object, ObjectID p_new_object);
  70. int get_history_len();
  71. int get_history_pos();
  72. // Gets an object from the history. The most recent object would be the object with p_obj = get_history_len() - 1.
  73. ObjectID get_history_obj(int p_obj) const;
  74. bool next();
  75. bool previous();
  76. ObjectID get_current();
  77. bool is_current_inspector_only() const;
  78. // Gets the size of the path of the current history item.
  79. int get_path_size() const;
  80. // Gets the object of the current history item, if valid.
  81. ObjectID get_path_object(int p_index) const;
  82. // Gets the property of the current history item.
  83. String get_path_property(int p_index) const;
  84. void clear();
  85. EditorSelectionHistory();
  86. };
  87. class EditorSelection;
  88. class EditorData {
  89. public:
  90. struct CustomType {
  91. String name;
  92. Ref<Script> script;
  93. Ref<Texture2D> icon;
  94. };
  95. struct EditedScene {
  96. Node *root = nullptr;
  97. String path;
  98. uint64_t file_modified_time = 0;
  99. Dictionary editor_states;
  100. List<Node *> selection;
  101. Vector<EditorSelectionHistory::HistoryElement> history_stored;
  102. int history_current = 0;
  103. Dictionary custom_state;
  104. NodePath live_edit_root;
  105. int history_id = 0;
  106. uint64_t last_checked_version = 0;
  107. };
  108. private:
  109. Vector<EditorPlugin *> editor_plugins;
  110. HashMap<StringName, EditorPlugin *> extension_editor_plugins;
  111. struct PropertyData {
  112. String name;
  113. Variant value;
  114. };
  115. HashMap<String, Vector<CustomType>> custom_types;
  116. List<PropertyData> clipboard;
  117. EditorUndoRedoManager *undo_redo_manager;
  118. Vector<Callable> undo_redo_callbacks;
  119. HashMap<StringName, Callable> move_element_functions;
  120. Vector<EditedScene> edited_scene;
  121. int current_edited_scene = -1;
  122. int last_created_scene = 1;
  123. bool _find_updated_instances(Node *p_root, Node *p_node, HashSet<String> &checked_paths);
  124. HashMap<StringName, String> _script_class_icon_paths;
  125. HashMap<String, StringName> _script_class_file_to_path;
  126. HashMap<Ref<Script>, Ref<Texture>> _script_icon_cache;
  127. Ref<Texture2D> _load_script_icon(const String &p_path) const;
  128. public:
  129. EditorPlugin *get_handling_main_editor(Object *p_object);
  130. Vector<EditorPlugin *> get_handling_sub_editors(Object *p_object);
  131. EditorPlugin *get_editor_by_name(const String &p_name);
  132. void copy_object_params(Object *p_object);
  133. void paste_object_params(Object *p_object);
  134. Dictionary get_editor_plugin_states() const;
  135. Dictionary get_scene_editor_states(int p_idx) const;
  136. void set_editor_plugin_states(const Dictionary &p_states);
  137. void get_editor_breakpoints(List<String> *p_breakpoints);
  138. void clear_editor_states();
  139. void save_editor_external_data();
  140. void apply_changes_in_editors();
  141. void add_editor_plugin(EditorPlugin *p_plugin);
  142. void remove_editor_plugin(EditorPlugin *p_plugin);
  143. int get_editor_plugin_count() const;
  144. EditorPlugin *get_editor_plugin(int p_idx);
  145. void add_extension_editor_plugin(const StringName &p_class_name, EditorPlugin *p_plugin);
  146. void remove_extension_editor_plugin(const StringName &p_class_name);
  147. bool has_extension_editor_plugin(const StringName &p_class_name);
  148. EditorPlugin *get_extension_editor_plugin(const StringName &p_class_name);
  149. void add_undo_redo_inspector_hook_callback(Callable p_callable); // Callbacks should have this signature: void (Object* undo_redo, Object *modified_object, String property, Variant new_value)
  150. void remove_undo_redo_inspector_hook_callback(Callable p_callable);
  151. const Vector<Callable> get_undo_redo_inspector_hook_callback();
  152. void add_move_array_element_function(const StringName &p_class, Callable p_callable); // Function should have this signature: void (Object* undo_redo, Object *modified_object, String array_prefix, int element_index, int new_position)
  153. void remove_move_array_element_function(const StringName &p_class);
  154. Callable get_move_array_element_function(const StringName &p_class) const;
  155. void save_editor_global_states();
  156. void add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture2D> &p_icon);
  157. Variant instantiate_custom_type(const String &p_type, const String &p_inherits);
  158. void remove_custom_type(const String &p_type);
  159. const HashMap<String, Vector<CustomType>> &get_custom_types() const { return custom_types; }
  160. const CustomType *get_custom_type_by_name(const String &p_name) const;
  161. const CustomType *get_custom_type_by_path(const String &p_path) const;
  162. bool is_type_recognized(const String &p_type) const;
  163. void instantiate_object_properties(Object *p_object);
  164. int add_edited_scene(int p_at_pos);
  165. void move_edited_scene_index(int p_idx, int p_to_idx);
  166. void remove_scene(int p_idx);
  167. void set_edited_scene(int p_idx);
  168. void set_edited_scene_root(Node *p_root);
  169. int get_edited_scene() const;
  170. int get_edited_scene_from_path(const String &p_path) const;
  171. Node *get_edited_scene_root(int p_idx = -1);
  172. int get_edited_scene_count() const;
  173. Vector<EditedScene> get_edited_scenes() const;
  174. String get_scene_title(int p_idx, bool p_always_strip_extension = false) const;
  175. String get_scene_path(int p_idx) const;
  176. String get_scene_type(int p_idx) const;
  177. void set_scene_path(int p_idx, const String &p_path);
  178. Ref<Script> get_scene_root_script(int p_idx) const;
  179. void set_scene_modified_time(int p_idx, uint64_t p_time);
  180. uint64_t get_scene_modified_time(int p_idx) const;
  181. void clear_edited_scenes();
  182. void set_edited_scene_live_edit_root(const NodePath &p_root);
  183. NodePath get_edited_scene_live_edit_root();
  184. bool check_and_update_scene(int p_idx);
  185. void move_edited_scene_to_index(int p_idx);
  186. bool call_build();
  187. void set_scene_as_saved(int p_idx);
  188. bool is_scene_changed(int p_idx);
  189. int get_scene_history_id_from_path(const String &p_path) const;
  190. int get_current_edited_scene_history_id() const;
  191. int get_scene_history_id(int p_idx) const;
  192. void set_plugin_window_layout(Ref<ConfigFile> p_layout);
  193. void get_plugin_window_layout(Ref<ConfigFile> p_layout);
  194. void save_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history, const Dictionary &p_custom);
  195. Dictionary restore_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history);
  196. void notify_edited_scene_changed();
  197. void notify_resource_saved(const Ref<Resource> &p_resource);
  198. void notify_scene_saved(const String &p_path);
  199. bool script_class_is_parent(const String &p_class, const String &p_inherits);
  200. StringName script_class_get_base(const String &p_class) const;
  201. Variant script_class_instance(const String &p_class);
  202. Ref<Script> script_class_load_script(const String &p_class) const;
  203. StringName script_class_get_name(const String &p_path) const;
  204. void script_class_set_name(const String &p_path, const StringName &p_class);
  205. String script_class_get_icon_path(const String &p_class) const;
  206. void script_class_set_icon_path(const String &p_class, const String &p_icon_path);
  207. void script_class_clear_icon_paths() { _script_class_icon_paths.clear(); }
  208. void script_class_save_icon_paths();
  209. void script_class_load_icon_paths();
  210. Ref<Texture2D> extension_class_get_icon(const String &p_class) const;
  211. Ref<Texture2D> get_script_icon(const Ref<Script> &p_script);
  212. void clear_script_icon_cache();
  213. EditorData();
  214. ~EditorData();
  215. };
  216. /**
  217. * Stores and provides access to the nodes currently selected in the editor.
  218. *
  219. * This provides a central location for storing "selected" nodes, as a selection can be triggered from multiple places,
  220. * such as the SceneTreeDock or a main screen editor plugin (e.g. CanvasItemEditor).
  221. */
  222. class EditorSelection : public Object {
  223. GDCLASS(EditorSelection, Object);
  224. // Contains the selected nodes and corresponding metadata.
  225. // Metadata objects come from calling _get_editor_data on the editor_plugins, passing the selected node.
  226. HashMap<Node *, Object *> selection;
  227. // Tracks whether the selection change signal has been emitted.
  228. // Prevents multiple signals being called in one frame.
  229. bool emitted = false;
  230. bool changed = false;
  231. bool node_list_changed = false;
  232. void _node_removed(Node *p_node);
  233. // Editor plugins which are related to selection.
  234. List<Object *> editor_plugins;
  235. List<Node *> selected_node_list;
  236. void _update_node_list();
  237. TypedArray<Node> _get_transformable_selected_nodes();
  238. void _emit_change();
  239. protected:
  240. static void _bind_methods();
  241. public:
  242. void add_node(Node *p_node);
  243. void remove_node(Node *p_node);
  244. bool is_selected(Node *p_node) const;
  245. template <typename T>
  246. T *get_node_editor_data(Node *p_node) {
  247. if (!selection.has(p_node)) {
  248. return nullptr;
  249. }
  250. return Object::cast_to<T>(selection[p_node]);
  251. }
  252. // Adds an editor plugin which can provide metadata for selected nodes.
  253. void add_editor_plugin(Object *p_object);
  254. void update();
  255. void clear();
  256. // Returns all the selected nodes.
  257. TypedArray<Node> get_selected_nodes();
  258. // Returns only the top level selected nodes.
  259. // That is, if the selection includes some node and a child of that node, only the parent is returned.
  260. List<Node *> &get_selected_node_list();
  261. // Returns all the selected nodes (list version of "get_selected_nodes").
  262. List<Node *> get_full_selected_node_list();
  263. // Returns the map of selected objects and their metadata.
  264. HashMap<Node *, Object *> &get_selection() { return selection; }
  265. EditorSelection();
  266. ~EditorSelection();
  267. };
  268. #endif // EDITOR_DATA_H