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