Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/inspector/multi_node_edit.cpp
9903 views
1
/**************************************************************************/
2
/* multi_node_edit.cpp */
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
#include "multi_node_edit.h"
32
33
#include "core/math/math_fieldwise.h"
34
#include "editor/editor_node.h"
35
#include "editor/editor_undo_redo_manager.h"
36
37
bool MultiNodeEdit::_set(const StringName &p_name, const Variant &p_value) {
38
return _set_impl(p_name, p_value, "");
39
}
40
41
bool MultiNodeEdit::_set_impl(const StringName &p_name, const Variant &p_value, const String &p_field) {
42
Node *es = EditorNode::get_singleton()->get_edited_scene();
43
if (!es) {
44
return false;
45
}
46
47
String name = p_name;
48
49
if (name == "scripts") { // Script set is intercepted at object level (check Variant Object::get()), so use a different name.
50
name = "script";
51
} else if (name.begins_with("Metadata/")) {
52
name = name.replace_first("Metadata/", "metadata/");
53
}
54
55
Node *node_path_target = nullptr;
56
if (p_value.get_type() == Variant::NODE_PATH && p_value != NodePath()) {
57
node_path_target = es->get_node(p_value);
58
}
59
60
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
61
62
ur->create_action(vformat(TTR("Set %s on %d nodes"), name, get_node_count()), UndoRedo::MERGE_ENDS);
63
for (const NodePath &E : nodes) {
64
Node *n = es->get_node_or_null(E);
65
if (!n) {
66
continue;
67
}
68
69
if (p_value.get_type() == Variant::NODE_PATH) {
70
NodePath path;
71
if (node_path_target) {
72
path = n->get_path_to(node_path_target);
73
}
74
ur->add_do_property(n, name, path);
75
} else {
76
Variant new_value;
77
if (p_field.is_empty()) {
78
// whole value
79
new_value = p_value;
80
} else {
81
// only one field
82
new_value = fieldwise_assign(n->get(name), p_value, p_field);
83
}
84
ur->add_do_property(n, name, new_value);
85
}
86
87
ur->add_undo_property(n, name, n->get(name));
88
}
89
90
ur->commit_action();
91
return true;
92
}
93
94
bool MultiNodeEdit::_get(const StringName &p_name, Variant &r_ret) const {
95
Node *es = EditorNode::get_singleton()->get_edited_scene();
96
if (!es) {
97
return false;
98
}
99
100
String name = p_name;
101
if (name == "scripts") { // Script set is intercepted at object level (check Variant Object::get()), so use a different name.
102
name = "script";
103
} else if (name.begins_with("Metadata/")) {
104
name = name.replace_first("Metadata/", "metadata/");
105
}
106
107
for (const NodePath &E : nodes) {
108
const Node *n = es->get_node_or_null(E);
109
if (!n) {
110
continue;
111
}
112
113
bool found;
114
r_ret = n->get(name, &found);
115
if (found) {
116
return true;
117
}
118
}
119
120
return false;
121
}
122
123
void MultiNodeEdit::_get_property_list(List<PropertyInfo> *p_list) const {
124
HashMap<String, PLData> usage;
125
126
Node *es = EditorNode::get_singleton()->get_edited_scene();
127
if (!es) {
128
return;
129
}
130
131
int nc = 0;
132
133
List<PLData *> data_list;
134
135
for (const NodePath &E : nodes) {
136
Node *n = es->get_node_or_null(E);
137
if (!n) {
138
continue;
139
}
140
141
List<PropertyInfo> plist;
142
n->get_property_list(&plist, true);
143
144
for (PropertyInfo F : plist) {
145
if (F.name == "script") {
146
continue; // Added later manually, since this is intercepted before being set (check Variant Object::get()).
147
} else if (F.name.begins_with("metadata/")) {
148
F.name = F.name.replace_first("metadata/", "Metadata/"); // Trick to not get actual metadata edited from MultiNodeEdit.
149
}
150
151
if (!usage.has(F.name)) {
152
PLData pld;
153
pld.uses = 0;
154
pld.info = F;
155
pld.info.name = F.name;
156
usage[F.name] = pld;
157
data_list.push_back(usage.getptr(F.name));
158
}
159
160
// Make sure only properties with the same exact PropertyInfo data will appear.
161
if (usage[F.name].info == F) {
162
usage[F.name].uses++;
163
}
164
}
165
166
nc++;
167
}
168
169
for (const PLData *E : data_list) {
170
if (nc == E->uses) {
171
p_list->push_back(E->info);
172
}
173
}
174
175
p_list->push_back(PropertyInfo(Variant::OBJECT, "scripts", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
176
}
177
178
String MultiNodeEdit::_get_editor_name() const {
179
return vformat(TTR("%s (%d Selected)"), get_edited_class_name(), get_node_count());
180
}
181
182
bool MultiNodeEdit::_property_can_revert(const StringName &p_name) const {
183
Node *es = EditorNode::get_singleton()->get_edited_scene();
184
if (!es) {
185
return false;
186
}
187
188
if (ClassDB::has_property(get_edited_class_name(), p_name)) {
189
for (const NodePath &E : nodes) {
190
Node *node = es->get_node_or_null(E);
191
if (node) {
192
return true;
193
}
194
}
195
196
return false;
197
}
198
199
// Don't show the revert button if the edited class doesn't have the property.
200
return false;
201
}
202
203
bool MultiNodeEdit::_property_get_revert(const StringName &p_name, Variant &r_property) const {
204
Node *es = EditorNode::get_singleton()->get_edited_scene();
205
if (!es) {
206
return false;
207
}
208
209
for (const NodePath &E : nodes) {
210
Node *node = es->get_node_or_null(E);
211
if (!node) {
212
continue;
213
}
214
215
r_property = ClassDB::class_get_default_property_value(node->get_class_name(), p_name);
216
return true;
217
}
218
219
return false;
220
}
221
222
void MultiNodeEdit::_queue_notify_property_list_changed() {
223
if (notify_property_list_changed_pending) {
224
return;
225
}
226
notify_property_list_changed_pending = true;
227
callable_mp(this, &MultiNodeEdit::_notify_property_list_changed).call_deferred();
228
}
229
230
void MultiNodeEdit::_notify_property_list_changed() {
231
notify_property_list_changed_pending = false;
232
notify_property_list_changed();
233
}
234
235
void MultiNodeEdit::add_node(const NodePath &p_node) {
236
nodes.push_back(p_node);
237
238
Node *es = EditorNode::get_singleton()->get_edited_scene();
239
if (es) {
240
Node *node = es->get_node_or_null(p_node);
241
if (node) {
242
node->connect(CoreStringName(property_list_changed), callable_mp(this, &MultiNodeEdit::_queue_notify_property_list_changed));
243
}
244
}
245
}
246
247
int MultiNodeEdit::get_node_count() const {
248
return nodes.size();
249
}
250
251
NodePath MultiNodeEdit::get_node(int p_index) const {
252
ERR_FAIL_INDEX_V(p_index, get_node_count(), NodePath());
253
return nodes[p_index];
254
}
255
256
StringName MultiNodeEdit::get_edited_class_name() const {
257
Node *es = EditorNode::get_singleton()->get_edited_scene();
258
if (!es) {
259
return SNAME("Node");
260
}
261
262
// Get the class name of the first node.
263
StringName class_name;
264
for (const NodePath &E : nodes) {
265
Node *node = es->get_node_or_null(E);
266
if (!node) {
267
continue;
268
}
269
270
class_name = node->get_class_name();
271
break;
272
}
273
274
if (class_name == StringName()) {
275
return SNAME("Node");
276
}
277
278
bool check_again = true;
279
while (check_again) {
280
check_again = false;
281
282
if (class_name == SNAME("Node") || class_name == StringName()) {
283
// All nodes inherit from Node, so no need to continue checking.
284
return SNAME("Node");
285
}
286
287
// Check that all nodes inherit from class_name.
288
for (const NodePath &E : nodes) {
289
Node *node = es->get_node_or_null(E);
290
if (!node) {
291
continue;
292
}
293
294
const StringName node_class_name = node->get_class_name();
295
if (class_name == node_class_name || ClassDB::is_parent_class(node_class_name, class_name)) {
296
// class_name is the same or a parent of the node's class.
297
continue;
298
}
299
300
// class_name is not a parent of the node's class, so check again with the parent class.
301
class_name = ClassDB::get_parent_class(class_name);
302
check_again = true;
303
break;
304
}
305
}
306
307
return class_name;
308
}
309
310
void MultiNodeEdit::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
311
_set_impl(p_property, p_value, p_field);
312
}
313
314
void MultiNodeEdit::_bind_methods() {
315
ClassDB::bind_method("_hide_script_from_inspector", &MultiNodeEdit::_hide_script_from_inspector);
316
ClassDB::bind_method("_hide_metadata_from_inspector", &MultiNodeEdit::_hide_metadata_from_inspector);
317
ClassDB::bind_method("_get_editor_name", &MultiNodeEdit::_get_editor_name);
318
}
319
320