Debian Codegraph @

editor/editor_data.h

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