0.2.0-dev Download ZIP
341 lines ·13.5 KB Raw
| 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 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include "core/templates/list.h" |
| 34 | #include "scene/main/node.h" |
| 35 | #include "scene/resources/texture.h" |
| 36 | |
| 37 | class ConfigFile; |
| 38 | class EditorPlugin; |
| 39 | class EditorUndoRedoManager; |
| 40 | class PopupMenu; |
| 41 | class PackedScene; |
| 42 | |
| 43 | /** |
| 44 | * Stores the history of objects which have been selected for editing in the Editor & the Inspector. |
| 45 | * |
| 46 | * Used in the editor to set & access the currently edited object, as well as the history of objects which have been edited. |
| 47 | */ |
| 48 | class EditorSelectionHistory { |
| 49 | // Stores the object & property (if relevant). |
| 50 | struct _Object { |
| 51 | Ref<RefCounted> ref; |
| 52 | ObjectID object; |
| 53 | String property; |
| 54 | bool inspector_only = false; |
| 55 | }; |
| 56 | |
| 57 | // Represents the selection of an object for editing. |
| 58 | struct HistoryElement { |
| 59 | // The sub-resources of the parent object (first in the path) that have been edited. |
| 60 | // For example, Node2D -> nested resource -> nested resource, if edited each individually in their own inspector. |
| 61 | Vector<_Object> path; |
| 62 | // The current point in the path. This is always equal to the last item in the path - it is never decremented. |
| 63 | int level = 0; |
| 64 | }; |
| 65 | friend class EditorData; |
| 66 | |
| 67 | Vector<HistoryElement> history; |
| 68 | int current_elem_idx; // The current history element being edited. |
| 69 | |
| 70 | public: |
| 71 | void cleanup_history(); |
| 72 | |
| 73 | bool is_at_beginning() const; |
| 74 | bool is_at_end() const; |
| 75 | |
| 76 | // Adds an object to the selection history. A property name can be passed if the target is a subresource of the given object. |
| 77 | // If the object should not change the main screen plugin, it can be set as inspector only. |
| 78 | void add_object(ObjectID p_object, const String &p_property = String(), bool p_inspector_only = false); |
| 79 | void replace_object(ObjectID p_old_object, ObjectID p_new_object); |
| 80 | |
| 81 | int get_history_len(); |
| 82 | int get_history_pos(); |
| 83 | |
| 84 | // Gets an object from the history. The most recent object would be the object with p_obj = get_history_len() - 1. |
| 85 | ObjectID get_history_obj(int p_obj) const; |
| 86 | |
| 87 | bool next(); |
| 88 | bool previous(); |
| 89 | ObjectID get_current(); |
| 90 | bool is_current_inspector_only() const; |
| 91 | |
| 92 | // Gets the size of the path of the current history item. |
| 93 | int get_path_size() const; |
| 94 | // Gets the object of the current history item, if valid. |
| 95 | ObjectID get_path_object(int p_index) const; |
| 96 | // Gets the property of the current history item. |
| 97 | String get_path_property(int p_index) const; |
| 98 | |
| 99 | void clear(); |
| 100 | |
| 101 | EditorSelectionHistory(); |
| 102 | }; |
| 103 | |
| 104 | class EditorSelection; |
| 105 | |
| 106 | class EditorData { |
| 107 | public: |
| 108 | struct CustomType { |
| 109 | String name; |
| 110 | Ref<Script> script; |
| 111 | Ref<Texture2D> icon; |
| 112 | }; |
| 113 | |
| 114 | struct EditedScene { |
| 115 | Node *root = nullptr; |
| 116 | Ref<PackedScene> scene; |
| 117 | String path; |
| 118 | uint64_t file_modified_time = 0; |
| 119 | Dictionary editor_states; |
| 120 | List<Node *> selection; |
| 121 | Vector<EditorSelectionHistory::HistoryElement> history_stored; |
| 122 | int history_current = 0; |
| 123 | Dictionary custom_state; |
| 124 | NodePath live_edit_root; |
| 125 | int history_id = 0; |
| 126 | uint64_t last_checked_version = 0; |
| 127 | uint64_t time_opened = 0; |
| 128 | }; |
| 129 | |
| 130 | private: |
| 131 | Vector<EditorPlugin *> editor_plugins; |
| 132 | HashMap<StringName, EditorPlugin *> extension_editor_plugins; |
| 133 | |
| 134 | struct PropertyData { |
| 135 | String name; |
| 136 | Variant value; |
| 137 | }; |
| 138 | HashMap<String, Vector<CustomType>> custom_types; |
| 139 | |
| 140 | List<PropertyData> clipboard; |
| 141 | EditorUndoRedoManager *undo_redo_manager; |
| 142 | Vector<Callable> undo_redo_callbacks; |
| 143 | HashMap<StringName, Callable> move_element_functions; |
| 144 | |
| 145 | Vector<EditedScene> edited_scene; |
| 146 | int current_edited_scene = -1; |
| 147 | int last_created_scene = 1; |
| 148 | |
| 149 | bool _find_updated_instances(Node *p_root, Node *p_node, HashSet<String> &checked_paths); |
| 150 | |
| 151 | HashMap<StringName, String> _script_class_icon_paths; |
| 152 | HashMap<String, StringName> _script_class_file_to_path; |
| 153 | HashMap<String, Ref<Texture2D>> _script_icon_cache; |
| 154 | |
| 155 | Ref<Texture2D> _load_script_icon(const String &p_path) const; |
| 156 | |
| 157 | public: |
| 158 | Vector<EditorPlugin *> get_handling_sub_editors(Object *p_object); |
| 159 | EditorPlugin *get_editor_by_name(const String &p_name); |
| 160 | |
| 161 | void copy_object_params(Object *p_object); |
| 162 | void paste_object_params(Object *p_object); |
| 163 | |
| 164 | Dictionary get_editor_plugin_states() const; |
| 165 | Dictionary get_scene_editor_states(int p_idx) const; |
| 166 | Dictionary get_scene_editor_states_with_selection(int p_idx) const; |
| 167 | void set_editor_plugin_states(const Dictionary &p_states); |
| 168 | void get_editor_breakpoints(List<String> *p_breakpoints); |
| 169 | void clear_editor_states(); |
| 170 | void save_editor_external_data(); |
| 171 | void apply_changes_in_editors(); |
| 172 | |
| 173 | void add_editor_plugin(EditorPlugin *p_plugin); |
| 174 | void remove_editor_plugin(EditorPlugin *p_plugin); |
| 175 | |
| 176 | int get_editor_plugin_count() const; |
| 177 | EditorPlugin *get_editor_plugin(int p_idx); |
| 178 | |
| 179 | void add_extension_editor_plugin(const StringName &p_class_name, EditorPlugin *p_plugin); |
| 180 | void remove_extension_editor_plugin(const StringName &p_class_name); |
| 181 | bool has_extension_editor_plugin(const StringName &p_class_name); |
| 182 | EditorPlugin *get_extension_editor_plugin(const StringName &p_class_name); |
| 183 | |
| 184 | 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) |
| 185 | void remove_undo_redo_inspector_hook_callback(Callable p_callable); |
| 186 | const Vector<Callable> get_undo_redo_inspector_hook_callback(); |
| 187 | |
| 188 | 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) |
| 189 | void remove_move_array_element_function(const StringName &p_class); |
| 190 | Callable get_move_array_element_function(const StringName &p_class) const; |
| 191 | |
| 192 | void add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture2D> &p_icon); |
| 193 | Variant instantiate_custom_type(const String &p_type, const String &p_inherits); |
| 194 | void remove_custom_type(const String &p_type); |
| 195 | const HashMap<String, Vector<CustomType>> &get_custom_types() const { return custom_types; } |
| 196 | const CustomType *get_custom_type_by_name(const String &p_name) const; |
| 197 | const CustomType *get_custom_type_by_path(const String &p_path) const; |
| 198 | bool is_type_recognized(const String &p_type) const; |
| 199 | |
| 200 | void instantiate_object_properties(Object *p_object); |
| 201 | |
| 202 | int add_edited_scene(int p_at_pos); |
| 203 | void remove_scene(int p_idx); |
| 204 | void set_scene_root(int p_idx, Node *p_root); |
| 205 | void set_scene_resource(int p_idx, const Ref<PackedScene> &p_scene); |
| 206 | void set_edited_scene(int p_idx); |
| 207 | void set_edited_scene_root(Node *p_root); |
| 208 | int get_edited_scene() const; |
| 209 | int get_edited_scene_from_path(const String &p_path) const; |
| 210 | Node *get_edited_scene_root(int p_idx = -1); |
| 211 | int get_edited_scene_count() const; |
| 212 | Vector<EditedScene> get_edited_scenes() const; |
| 213 | |
| 214 | String get_scene_title(int p_idx, bool p_always_strip_extension = false) const; |
| 215 | String get_scene_path(int p_idx) const; |
| 216 | String get_scene_type(int p_idx) const; |
| 217 | void set_scene_path(int p_idx, const String &p_path); |
| 218 | Ref<Script> get_scene_root_script(int p_idx) const; |
| 219 | uint64_t get_scene_time_opened(int p_idx) const; |
| 220 | void set_scene_modified_time(int p_idx, uint64_t p_time); |
| 221 | uint64_t get_scene_modified_time(int p_idx) const; |
| 222 | void clear_edited_scenes(); |
| 223 | void set_edited_scene_live_edit_root(const NodePath &p_root); |
| 224 | NodePath get_edited_scene_live_edit_root(); |
| 225 | bool check_and_update_scene(int p_idx); |
| 226 | bool reload_scene_from_memory(int p_idx, bool p_mark_unsaved); |
| 227 | void move_scene_to_index(int p_idx, int p_to_idx); |
| 228 | void move_edited_scene_to_index(int p_idx); |
| 229 | |
| 230 | bool call_build(); |
| 231 | |
| 232 | void set_scene_as_saved(int p_idx); |
| 233 | bool is_scene_changed(int p_idx); |
| 234 | |
| 235 | int get_scene_history_id_from_path(const String &p_path) const; |
| 236 | int get_current_edited_scene_history_id() const; |
| 237 | int get_scene_history_id(int p_idx) const; |
| 238 | |
| 239 | void set_plugin_window_layout(Ref<ConfigFile> p_layout); |
| 240 | void get_plugin_window_layout(Ref<ConfigFile> p_layout); |
| 241 | |
| 242 | void save_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history, const Dictionary &p_custom); |
| 243 | Dictionary restore_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history); |
| 244 | void notify_edited_scene_changed(); |
| 245 | void notify_resource_saved(const Ref<Resource> &p_resource); |
| 246 | void notify_scene_saved(const String &p_path); |
| 247 | void load_editor_plugin_states_from_config(const Ref<ConfigFile> &p_config_file, int p_idx); |
| 248 | |
| 249 | bool script_class_is_parent(const String &p_class, const String &p_inherits); |
| 250 | Variant script_class_instance(const String &p_class); |
| 251 | |
| 252 | Ref<Script> script_class_load_script(const String &p_class) const; |
| 253 | |
| 254 | StringName script_class_get_name(const String &p_path) const; |
| 255 | void script_class_set_name(const String &p_path, const StringName &p_class); |
| 256 | |
| 257 | String script_class_get_icon_path(const String &p_class, bool *r_valid = nullptr) const; |
| 258 | void script_class_set_icon_path(const String &p_class, const String &p_icon_path); |
| 259 | void script_class_clear_icon_paths() { _script_class_icon_paths.clear(); } |
| 260 | void script_class_save_global_classes(); |
| 261 | void script_class_load_icon_paths(); |
| 262 | |
| 263 | Ref<Texture2D> extension_class_get_icon(const String &p_class) const; |
| 264 | |
| 265 | Ref<Texture2D> get_script_icon(const String &p_script_path); |
| 266 | void clear_script_icon_cache(); |
| 267 | |
| 268 | EditorData(); |
| 269 | ~EditorData(); |
| 270 | }; |
| 271 | |
| 272 | /** |
| 273 | * Stores and provides access to the nodes currently selected in the editor. |
| 274 | * |
| 275 | * This provides a central location for storing "selected" nodes, as a selection can be triggered from multiple places, |
| 276 | * such as the SceneTreeDock or a main screen editor plugin (e.g. CanvasItemEditor). |
| 277 | */ |
| 278 | class EditorSelection : public Object { |
| 279 | GDCLASS(EditorSelection, Object); |
| 280 | |
| 281 | // Contains the selected nodes and corresponding metadata. |
| 282 | // Metadata objects come from calling _get_editor_data on the editor_plugins, passing the selected node. |
| 283 | HashMap<ObjectID, Object *> selection; |
| 284 | |
| 285 | // Tracks whether the selection change signal has been emitted. |
| 286 | // Prevents multiple signals being called in one frame. |
| 287 | bool emitted = false; |
| 288 | |
| 289 | bool changed = false; |
| 290 | bool node_list_changed = false; |
| 291 | |
| 292 | void _node_removed(Node *p_node); |
| 293 | |
| 294 | // Editor plugins which are related to selection. |
| 295 | List<Object *> editor_plugins; |
| 296 | LocalVector<ObjectID> top_selected_node_list; |
| 297 | |
| 298 | void _update_node_list(); |
| 299 | void _emit_change(); |
| 300 | |
| 301 | protected: |
| 302 | static void _bind_methods(); |
| 303 | |
| 304 | public: |
| 305 | void add_node(Node *p_node); |
| 306 | void remove_node(Node *p_node); |
| 307 | bool is_selected(Node *p_node) const; |
| 308 | |
| 309 | template <typename T> |
| 310 | T *get_node_editor_data(Node *p_node) { |
| 311 | if (!p_node) { |
| 312 | return nullptr; |
| 313 | } |
| 314 | ObjectID nid = p_node->get_instance_id(); |
| 315 | if (!selection.has(nid)) { |
| 316 | return nullptr; |
| 317 | } |
| 318 | return Object::cast_to<T>(selection[nid]); |
| 319 | } |
| 320 | |
| 321 | // Adds an editor plugin which can provide metadata for selected nodes. |
| 322 | void add_editor_plugin(Object *p_object); |
| 323 | |
| 324 | void update(bool p_deferred = true); |
| 325 | void clear(); |
| 326 | |
| 327 | // Returns only the top-level selected nodes (i.e. excludes any selected node whose parent is also selected). |
| 328 | // The first top-level node selected by the user is at the front of the list (i.e. not sorted in scene tree order). |
| 329 | List<Node *> get_top_selected_node_list(); |
| 330 | // Same as get_top_selected_node_list() but returns a TypedArray for binding to scripts. |
| 331 | TypedArray<Node> get_top_selected_nodes(); |
| 332 | // Returns all selected nodes (list version of "get_selected_nodes"). |
| 333 | // The first node selected by the user is at the front of the list (i.e. not sorted in scene tree order). |
| 334 | List<Node *> get_full_selected_node_list(); |
| 335 | // Same as get_full_selected_node_list() but returns a TypedArray for binding to scripts. |
| 336 | TypedArray<Node> get_selected_nodes(); |
| 337 | // Returns the map of all selected nodes and their metadata. |
| 338 | HashMap<ObjectID, Object *> &get_selection() { return selection; } |
| 339 | |
| 340 | ~EditorSelection(); |
| 341 | }; |