editor_data.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/list.h"
  33. #include "core/pair.h"
  34. #include "core/undo_redo.h"
  35. #include "editor/editor_plugin.h"
  36. #include "editor/plugins/script_editor_plugin.h"
  37. #include "scene/resources/texture.h"
  38. class EditorHistory {
  39. enum {
  40. HISTORY_MAX = 64
  41. };
  42. struct Obj {
  43. REF ref;
  44. ObjectID object;
  45. String property;
  46. bool inspector_only;
  47. };
  48. struct History {
  49. Vector<Obj> path;
  50. int level;
  51. };
  52. friend class EditorData;
  53. Vector<History> history;
  54. int current;
  55. //Vector<EditorPlugin*> editor_plugins;
  56. struct PropertyData {
  57. String name;
  58. Variant value;
  59. };
  60. void _add_object(ObjectID p_object, const String &p_property, int p_level_change, bool p_inspector_only = false);
  61. public:
  62. void cleanup_history();
  63. bool is_at_beginning() const;
  64. bool is_at_end() const;
  65. void add_object_inspector_only(ObjectID p_object);
  66. void add_object(ObjectID p_object);
  67. void add_object(ObjectID p_object, const String &p_subprop);
  68. void add_object(ObjectID p_object, int p_relevel);
  69. int get_history_len();
  70. int get_history_pos();
  71. ObjectID get_history_obj(int p_obj) const;
  72. bool is_history_obj_inspector_only(int p_obj) const;
  73. bool next();
  74. bool previous();
  75. ObjectID get_current();
  76. bool is_current_inspector_only() const;
  77. int get_path_size() const;
  78. ObjectID get_path_object(int p_index) const;
  79. String get_path_property(int p_index) const;
  80. void clear();
  81. EditorHistory();
  82. };
  83. class EditorSelection;
  84. class EditorData {
  85. public:
  86. struct CustomType {
  87. String name;
  88. Ref<Script> script;
  89. Ref<Texture> icon;
  90. };
  91. struct EditedScene {
  92. Node *root;
  93. String path;
  94. uint64_t file_modified_time = 0;
  95. Dictionary editor_states;
  96. List<Node *> selection;
  97. Vector<EditorHistory::History> history_stored;
  98. int history_current;
  99. Dictionary custom_state;
  100. uint64_t version;
  101. NodePath live_edit_root;
  102. };
  103. private:
  104. Vector<EditorPlugin *> editor_plugins;
  105. struct PropertyData {
  106. String name;
  107. Variant value;
  108. };
  109. Map<String, Vector<CustomType>> custom_types;
  110. List<PropertyData> clipboard;
  111. UndoRedo undo_redo;
  112. Vector<EditedScene> edited_scene;
  113. int current_edited_scene;
  114. bool _find_updated_instances(Node *p_root, Node *p_node, Set<String> &checked_paths);
  115. HashMap<StringName, String> _script_class_icon_paths;
  116. HashMap<String, StringName> _script_class_file_to_path;
  117. public:
  118. EditorPlugin *get_editor(Object *p_object);
  119. EditorPlugin *get_subeditor(Object *p_object);
  120. Vector<EditorPlugin *> get_subeditors(Object *p_object);
  121. EditorPlugin *get_editor(String p_name);
  122. void copy_object_params(Object *p_object);
  123. void paste_object_params(Object *p_object);
  124. Dictionary get_editor_states() const;
  125. Dictionary get_scene_editor_states(int p_idx) const;
  126. void set_editor_states(const Dictionary &p_states);
  127. void get_editor_breakpoints(List<String> *p_breakpoints);
  128. void clear_editor_states();
  129. void save_editor_external_data();
  130. void apply_changes_in_editors();
  131. void add_editor_plugin(EditorPlugin *p_plugin);
  132. void remove_editor_plugin(EditorPlugin *p_plugin);
  133. int get_editor_plugin_count() const;
  134. EditorPlugin *get_editor_plugin(int p_idx);
  135. UndoRedo &get_undo_redo();
  136. void save_editor_global_states();
  137. void restore_editor_global_states();
  138. void add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture> &p_icon);
  139. Variant instance_custom_type(const String &p_type, const String &p_inherits);
  140. void remove_custom_type(const String &p_type);
  141. const Map<String, Vector<CustomType>> &get_custom_types() const { return custom_types; }
  142. int add_edited_scene(int p_at_pos);
  143. void move_edited_scene_index(int p_idx, int p_to_idx);
  144. void remove_scene(int p_idx);
  145. void set_edited_scene(int p_idx);
  146. void set_edited_scene_root(Node *p_root);
  147. int get_edited_scene() const;
  148. Node *get_edited_scene_root(int p_idx = -1);
  149. int get_edited_scene_count() const;
  150. Vector<EditedScene> get_edited_scenes() const;
  151. String get_scene_title(int p_idx, bool p_always_strip_extension = false) const;
  152. String get_scene_path(int p_idx) const;
  153. String get_scene_type(int p_idx) const;
  154. void set_scene_path(int p_idx, const String &p_path);
  155. Ref<Script> get_scene_root_script(int p_idx) const;
  156. void set_edited_scene_version(uint64_t version, int p_scene_idx = -1);
  157. uint64_t get_edited_scene_version() const;
  158. uint64_t get_scene_version(int p_idx) const;
  159. void set_scene_modified_time(int p_idx, uint64_t p_time);
  160. uint64_t get_scene_modified_time(int p_idx) const;
  161. void clear_edited_scenes();
  162. void set_edited_scene_live_edit_root(const NodePath &p_root);
  163. NodePath get_edited_scene_live_edit_root();
  164. bool check_and_update_scene(int p_idx);
  165. void move_edited_scene_to_index(int p_idx);
  166. bool call_build();
  167. void set_plugin_window_layout(Ref<ConfigFile> p_layout);
  168. void get_plugin_window_layout(Ref<ConfigFile> p_layout);
  169. void save_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history, const Dictionary &p_custom);
  170. Dictionary restore_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history);
  171. void notify_edited_scene_changed();
  172. void notify_resource_saved(const Ref<Resource> &p_resource);
  173. bool script_class_is_parent(const String &p_class, const String &p_inherits);
  174. StringName script_class_get_base(const String &p_class) const;
  175. Variant script_class_instance(const String &p_class);
  176. Ref<Script> script_class_load_script(const String &p_class) const;
  177. StringName script_class_get_name(const String &p_path) const;
  178. void script_class_set_name(const String &p_path, const StringName &p_class);
  179. String script_class_get_icon_path(const String &p_class) const;
  180. void script_class_set_icon_path(const String &p_class, const String &p_icon_path);
  181. void script_class_clear_icon_paths() { _script_class_icon_paths.clear(); }
  182. void script_class_save_icon_paths();
  183. void script_class_load_icon_paths();
  184. EditorData();
  185. };
  186. class EditorSelection : public Object {
  187. GDCLASS(EditorSelection, Object);
  188. private:
  189. Map<Node *, Object *> selection;
  190. bool emitted;
  191. bool changed;
  192. bool nl_changed;
  193. void _node_removed(Node *p_node);
  194. List<Object *> editor_plugins;
  195. List<Node *> selected_node_list;
  196. void _update_nl();
  197. Array _get_transformable_selected_nodes();
  198. void _emit_change();
  199. protected:
  200. static void _bind_methods();
  201. public:
  202. Array get_selected_nodes();
  203. void add_node(Node *p_node);
  204. void remove_node(Node *p_node);
  205. bool is_selected(Node *) const;
  206. template <class T>
  207. T *get_node_editor_data(Node *p_node) {
  208. if (!selection.has(p_node)) {
  209. return nullptr;
  210. }
  211. return Object::cast_to<T>(selection[p_node]);
  212. }
  213. void add_editor_plugin(Object *p_object);
  214. void update();
  215. void clear();
  216. List<Node *> &get_selected_node_list();
  217. List<Node *> get_full_selected_node_list();
  218. Map<Node *, Object *> &get_selection() { return selection; }
  219. EditorSelection();
  220. ~EditorSelection();
  221. };
  222. #endif // EDITOR_DATA_H