1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/**************************************************************************/
/* multi_node_edit.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "multi_node_edit.h"
#include "core/math/math_fieldwise.h"
#include "editor/editor_node.h"
#include "editor/editor_undo_redo_manager.h"
bool MultiNodeEdit::_set(const StringName &p_name, const Variant &p_value) {
return _set_impl(p_name, p_value, "");
}
bool MultiNodeEdit::_set_impl(const StringName &p_name, const Variant &p_value, const String &p_field) {
Node *es = EditorNode::get_singleton()->get_edited_scene();
if (!es) {
return false;
}
String name = p_name;
if (name == "scripts") { // Script set is intercepted at object level (check Variant Object::get()), so use a different name.
name = "script";
}
Node *node_path_target = nullptr;
if (p_value.get_type() == Variant::NODE_PATH && p_value != NodePath()) {
node_path_target = es->get_node(p_value);
}
Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
ur->create_action(vformat(TTR("Set %s on %d nodes"), name, get_node_count()), UndoRedo::MERGE_ENDS);
for (const NodePath &E : nodes) {
Node *n = es->get_node_or_null(E);
if (!n) {
continue;
}
if (p_value.get_type() == Variant::NODE_PATH) {
NodePath path;
if (node_path_target) {
path = n->get_path_to(node_path_target);
}
ur->add_do_property(n, name, path);
} else {
Variant new_value;
if (p_field.is_empty()) {
// whole value
new_value = p_value;
} else {
// only one field
new_value = fieldwise_assign(n->get(name), p_value, p_field);
}
ur->add_do_property(n, name, new_value);
}
ur->add_undo_property(n, name, n->get(name));
}
ur->commit_action();
return true;
}
bool MultiNodeEdit::_get(const StringName &p_name, Variant &r_ret) const {
Node *es = EditorNode::get_singleton()->get_edited_scene();
if (!es) {
return false;
}
String name = p_name;
if (name == "scripts") { // Script set is intercepted at object level (check Variant Object::get()), so use a different name.
name = "script";
}
for (const NodePath &E : nodes) {
const Node *n = es->get_node_or_null(E);
if (!n) {
continue;
}
bool found;
r_ret = n->get(name, &found);
if (found) {
return true;
}
}
return false;
}
void MultiNodeEdit::_get_property_list(List<PropertyInfo> *p_list) const {
HashMap<String, PLData> usage;
Node *es = EditorNode::get_singleton()->get_edited_scene();
if (!es) {
return;
}
int nc = 0;
List<PLData *> data_list;
for (const NodePath &E : nodes) {
Node *n = es->get_node_or_null(E);
if (!n) {
continue;
}
List<PropertyInfo> plist;
n->get_property_list(&plist, true);
for (const PropertyInfo &F : plist) {
if (F.name == "script") {
continue; // Added later manually, since this is intercepted before being set (check Variant Object::get()).
}
if (!usage.has(F.name)) {
PLData pld;
pld.uses = 0;
pld.info = F;
usage[F.name] = pld;
data_list.push_back(usage.getptr(F.name));
}
// Make sure only properties with the same exact PropertyInfo data will appear.
if (usage[F.name].info == F) {
usage[F.name].uses++;
}
}
nc++;
}
for (const PLData *E : data_list) {
if (nc == E->uses) {
p_list->push_back(E->info);
}
}
p_list->push_back(PropertyInfo(Variant::OBJECT, "scripts", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
}
String MultiNodeEdit::_get_editor_name() const {
return vformat(TTR("%s (%d Selected)"), get_edited_class_name(), get_node_count());
}
bool MultiNodeEdit::_property_can_revert(const StringName &p_name) const {
Node *es = EditorNode::get_singleton()->get_edited_scene();
if (!es) {
return false;
}
if (ClassDB::has_property(get_edited_class_name(), p_name)) {
StringName class_name;
for (const NodePath &E : nodes) {
Node *node = es->get_node_or_null(E);
if (!node) {
continue;
}
class_name = node->get_class_name();
}
Variant default_value = ClassDB::class_get_default_property_value(class_name, p_name);
for (const NodePath &E : nodes) {
Node *node = es->get_node_or_null(E);
if (!node) {
continue;
}
if (node->get(p_name) != default_value) {
// A node that doesn't have the default value has been found, so show the revert button.
return true;
}
}
return false;
}
// Don't show the revert button if the edited class doesn't have the property.
return false;
}
bool MultiNodeEdit::_property_get_revert(const StringName &p_name, Variant &r_property) const {
Node *es = EditorNode::get_singleton()->get_edited_scene();
if (!es) {
return false;
}
for (const NodePath &E : nodes) {
Node *node = es->get_node_or_null(E);
if (!node) {
continue;
}
r_property = ClassDB::class_get_default_property_value(node->get_class_name(), p_name);
return true;
}
return false;
}
void MultiNodeEdit::add_node(const NodePath &p_node) {
nodes.push_back(p_node);
}
int MultiNodeEdit::get_node_count() const {
return nodes.size();
}
NodePath MultiNodeEdit::get_node(int p_index) const {
ERR_FAIL_INDEX_V(p_index, nodes.size(), NodePath());
return nodes[p_index];
}
StringName MultiNodeEdit::get_edited_class_name() const {
Node *es = EditorNode::get_singleton()->get_edited_scene();
if (!es) {
return SNAME("Node");
}
// Get the class name of the first node.
StringName class_name;
for (const NodePath &E : nodes) {
Node *node = es->get_node_or_null(E);
if (!node) {
continue;
}
class_name = node->get_class_name();
break;
}
if (class_name == StringName()) {
return SNAME("Node");
}
bool check_again = true;
while (check_again) {
check_again = false;
if (class_name == SNAME("Node") || class_name == StringName()) {
// All nodes inherit from Node, so no need to continue checking.
return SNAME("Node");
}
// Check that all nodes inherit from class_name.
for (const NodePath &E : nodes) {
Node *node = es->get_node_or_null(E);
if (!node) {
continue;
}
const StringName node_class_name = node->get_class_name();
if (class_name == node_class_name || ClassDB::is_parent_class(node_class_name, class_name)) {
// class_name is the same or a parent of the node's class.
continue;
}
// class_name is not a parent of the node's class, so check again with the parent class.
class_name = ClassDB::get_parent_class(class_name);
check_again = true;
break;
}
}
return class_name;
}
void MultiNodeEdit::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
_set_impl(p_property, p_value, p_field);
}
void MultiNodeEdit::_bind_methods() {
ClassDB::bind_method("_hide_script_from_inspector", &MultiNodeEdit::_hide_script_from_inspector);
ClassDB::bind_method("_hide_metadata_from_inspector", &MultiNodeEdit::_hide_metadata_from_inspector);
ClassDB::bind_method("_get_editor_name", &MultiNodeEdit::_get_editor_name);
}
MultiNodeEdit::MultiNodeEdit() {
}
|