Debian Codegraph @

editor/inspector/multi_node_edit.cpp

raw symbols SCIP
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, bool p_undo_redo) {
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 if (p_undo_redo) {
63 ur->create_action(vformat(TTR("Set %s on %d nodes"), name, get_node_count()), UndoRedo::MERGE_ENDS);
64 }
65
66 for (const NodePath &E : nodes) {
67 Node *n = es->get_node_or_null(E);
68 if (!n) {
69 continue;
70 }
71
72 Variant new_value;
73 if (p_value.get_type() == Variant::NODE_PATH) {
74 NodePath path;
75 if (node_path_target) {
76 path = n->get_path_to(node_path_target);
77 }
78
79 if (p_undo_redo) {
80 ur->add_do_property(n, name, path);
81 } else {
82 n->set(name, path);
83 }
84 } else {
85 if (p_field.is_empty()) {
86 // whole value
87 new_value = p_value;
88 } else {
89 // only one field
90 new_value = fieldwise_assign(n->get(name), p_value, p_field);
91 }
92
93 if (p_undo_redo) {
94 ur->add_do_property(n, name, new_value);
95 } else {
96 n->set(name, new_value);
97 }
98 }
99
100 if (p_undo_redo) {
101 ur->add_undo_property(n, name, n->get(name));
102 Variant old_value = n->get(p_name);
103 Variant::Type type = old_value.get_type();
104 if ((type == Variant::OBJECT || type == Variant::ARRAY || type == Variant::DICTIONARY) && old_value != new_value) {
105 ur->add_do_method(EditorNode::get_singleton(), "update_node_reference", old_value, n, true);
106 ur->add_do_method(EditorNode::get_singleton(), "update_node_reference", new_value, n, false);
107 // Perhaps an inefficient way of updating the resource count.
108 // We could go in depth and check which Resource values changed/got removed and which ones stayed the same, but this is more readable at the moment.
109 ur->add_undo_method(EditorNode::get_singleton(), "update_node_reference", new_value, n, true);
110 ur->add_undo_method(EditorNode::get_singleton(), "update_node_reference", old_value, n, false);
111 }
112 }
113 }
114
115 if (p_undo_redo) {
116 ur->commit_action();
117 }
118 return true;
119 }
120
121 bool MultiNodeEdit::_get(const StringName &p_name, Variant &r_ret) const {
122 Node *es = EditorNode::get_singleton()->get_edited_scene();
123 if (!es) {
124 return false;
125 }
126
127 String name = p_name;
128 if (name == "scripts") { // Script set is intercepted at object level (check Variant Object::get()), so use a different name.
129 name = "script";
130 } else if (name.begins_with("Metadata/")) {
131 name = name.replace_first("Metadata/", "metadata/");
132 }
133
134 for (const NodePath &E : nodes) {
135 const Node *n = es->get_node_or_null(E);
136 if (!n) {
137 continue;
138 }
139
140 bool found;
141 r_ret = n->get(name, &found);
142 if (found) {
143 return true;
144 }
145 }
146
147 return false;
148 }
149
150 void MultiNodeEdit::_get_property_list(List<PropertyInfo> *p_list) const {
151 HashMap<String, PLData> usage;
152
153 Node *es = EditorNode::get_singleton()->get_edited_scene();
154 if (!es) {
155 return;
156 }
157
158 int nc = 0;
159
160 List<PLData *> data_list;
161
162 for (const NodePath &E : nodes) {
163 Node *n = es->get_node_or_null(E);
164 if (!n) {
165 continue;
166 }
167
168 List<PropertyInfo> plist;
169 n->get_property_list(&plist, true);
170
171 for (PropertyInfo F : plist) {
172 if (F.name == "script") {
173 continue; // Added later manually, since this is intercepted before being set (check Variant Object::get()).
174 } else if (F.name.begins_with("metadata/")) {
175 F.name = F.name.replace_first("metadata/", "Metadata/"); // Trick to not get actual metadata edited from MultiNodeEdit.
176 }
177
178 PLData *usage_data = usage.getptr(F.name);
179 if (!usage_data) {
180 PLData pld;
181 pld.uses = 0;
182 pld.info = F;
183 pld.info.name = F.name;
184 HashMap<String, MultiNodeEdit::PLData>::Iterator I = usage.insert(F.name, pld);
185 usage_data = &I->value;
186 data_list.push_back(usage_data);
187 }
188
189 // Make sure only properties with the same exact PropertyInfo data will appear.
190 if (usage_data->info == F) {
191 usage_data->uses++;
192 }
193 }
194
195 nc++;
196 }
197
198 for (const PLData *E : data_list) {
199 if (nc == E->uses) {
200 p_list->push_back(E->info);
201 }
202 }
203
204 p_list->push_back(PropertyInfo(Variant::OBJECT, "scripts", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
205 }
206
207 String MultiNodeEdit::_get_editor_name() const {
208 return vformat(TTR("%s (%d Selected)"), get_edited_class_name(), get_node_count());
209 }
210
211 bool MultiNodeEdit::_property_can_revert(const StringName &p_name) const {
212 Node *es = EditorNode::get_singleton()->get_edited_scene();
213 if (!es) {
214 return false;
215 }
216
217 if (ClassDB::has_property(get_edited_class_name(), p_name)) {
218 for (const NodePath &E : nodes) {
219 Node *node = es->get_node_or_null(E);
220 if (node) {
221 return true;
222 }
223 }
224
225 return false;
226 }
227
228 // Don't show the revert button if the edited class doesn't have the property.
229 return false;
230 }
231
232 bool MultiNodeEdit::_property_get_revert(const StringName &p_name, Variant &r_property) const {
233 Node *es = EditorNode::get_singleton()->get_edited_scene();
234 if (!es) {
235 return false;
236 }
237
238 for (const NodePath &E : nodes) {
239 Node *node = es->get_node_or_null(E);
240 if (!node) {
241 continue;
242 }
243
244 r_property = ClassDB::class_get_default_property_value(node->get_class_name(), p_name);
245 return true;
246 }
247
248 return false;
249 }
250
251 void MultiNodeEdit::_queue_notify_property_list_changed() {
252 if (notify_property_list_changed_pending) {
253 return;
254 }
255 notify_property_list_changed_pending = true;
256 callable_mp(this, &MultiNodeEdit::_notify_property_list_changed).call_deferred();
257 }
258
259 void MultiNodeEdit::_notify_property_list_changed() {
260 notify_property_list_changed_pending = false;
261 notify_property_list_changed();
262 }
263
264 void MultiNodeEdit::add_node(const NodePath &p_node) {
265 nodes.push_back(p_node);
266
267 Node *es = EditorNode::get_singleton()->get_edited_scene();
268 if (es) {
269 Node *node = es->get_node_or_null(p_node);
270 if (node) {
271 node->connect(CoreStringName(property_list_changed), callable_mp(this, &MultiNodeEdit::_queue_notify_property_list_changed));
272 }
273 }
274 }
275
276 int MultiNodeEdit::get_node_count() const {
277 return nodes.size();
278 }
279
280 NodePath MultiNodeEdit::get_node(int p_index) const {
281 ERR_FAIL_INDEX_V(p_index, get_node_count(), NodePath());
282 return nodes[p_index];
283 }
284
285 StringName MultiNodeEdit::get_edited_class_name() const {
286 Node *es = EditorNode::get_singleton()->get_edited_scene();
287 if (!es) {
288 return SNAME("Node");
289 }
290
291 // Get the class name of the first node.
292 StringName class_name;
293 for (const NodePath &E : nodes) {
294 Node *node = es->get_node_or_null(E);
295 if (!node) {
296 continue;
297 }
298
299 class_name = node->get_class_name();
300 break;
301 }
302
303 if (class_name == StringName()) {
304 return SNAME("Node");
305 }
306
307 bool check_again = true;
308 while (check_again) {
309 check_again = false;
310
311 if (class_name == SNAME("Node") || class_name == StringName()) {
312 // All nodes inherit from Node, so no need to continue checking.
313 return SNAME("Node");
314 }
315
316 // Check that all nodes inherit from class_name.
317 for (const NodePath &E : nodes) {
318 Node *node = es->get_node_or_null(E);
319 if (!node) {
320 continue;
321 }
322
323 const StringName node_class_name = node->get_class_name();
324 if (class_name == node_class_name || ClassDB::is_parent_class(node_class_name, class_name)) {
325 // class_name is the same or a parent of the node's class.
326 continue;
327 }
328
329 // class_name is not a parent of the node's class, so check again with the parent class.
330 class_name = ClassDB::get_parent_class(class_name);
331 check_again = true;
332 break;
333 }
334 }
335
336 return class_name;
337 }
338
339 void MultiNodeEdit::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
340 // Ignore the field with arrays and dictionaries, as they are passed whole when edited.
341 Variant::Type type = p_value.get_type();
342 if (type == Variant::ARRAY || type == Variant::DICTIONARY) {
343 _set_impl(p_property, p_value, "");
344 } else {
345 _set_impl(p_property, p_value, p_field);
346 }
347 }
348
349 void MultiNodeEdit::_bind_methods() {
350 ClassDB::bind_method("_hide_script_from_inspector", &MultiNodeEdit::_hide_script_from_inspector);
351 ClassDB::bind_method("_hide_metadata_from_inspector", &MultiNodeEdit::_hide_metadata_from_inspector);
352 ClassDB::bind_method("_get_editor_name", &MultiNodeEdit::_get_editor_name);
353 }