1 /*************************************************************************/
2 /* multi_node_edit.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
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 #include "multi_node_edit.h"
31 #include "core/helper/math_fieldwise.h"
32 #include "editor_node.h"
33
_set(const StringName & p_name,const Variant & p_value)34 bool MultiNodeEdit::_set(const StringName &p_name, const Variant &p_value) {
35
36 return _set_impl(p_name, p_value, "");
37 }
38
_set_impl(const StringName & p_name,const Variant & p_value,const String & p_field)39 bool MultiNodeEdit::_set_impl(const StringName &p_name, const Variant &p_value, const String &p_field) {
40
41 Node *es = EditorNode::get_singleton()->get_edited_scene();
42 if (!es)
43 return false;
44
45 String name = p_name;
46
47 if (name == "scripts/script") { // script/script set is intercepted at object level (check Variant Object::get() ) ,so use a different name
48 name = "script/script";
49 }
50
51 UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
52
53 ur->create_action(TTR("MultiNode Set") + " " + String(name));
54 for (const List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
55
56 if (!es->has_node(E->get()))
57 continue;
58
59 Node *n = es->get_node(E->get());
60 if (!n)
61 continue;
62
63 if (p_value.get_type() == Variant::NODE_PATH) {
64 Node *tonode = n->get_node(p_value);
65 NodePath p_path = n->get_path_to(tonode);
66 ur->add_do_property(n, name, p_path);
67 } else {
68 Variant new_value;
69 if (p_field == "") {
70 // whole value
71 new_value = p_value;
72 } else {
73 // only one field
74 new_value = fieldwise_assign(n->get(name), p_value, p_field);
75 }
76 ur->add_do_property(n, name, new_value);
77 }
78
79 ur->add_undo_property(n, name, n->get(name));
80 }
81 ur->add_do_method(EditorNode::get_singleton()->get_property_editor(), "refresh");
82 ur->add_undo_method(EditorNode::get_singleton()->get_property_editor(), "refresh");
83
84 ur->commit_action();
85 return true;
86 }
87
_get(const StringName & p_name,Variant & r_ret) const88 bool MultiNodeEdit::_get(const StringName &p_name, Variant &r_ret) const {
89
90 Node *es = EditorNode::get_singleton()->get_edited_scene();
91 if (!es)
92 return false;
93
94 String name = p_name;
95 if (name == "scripts/script") { // script/script set is intercepted at object level (check Variant Object::get() ) ,so use a different name
96 name = "script/script";
97 }
98
99 for (const List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
100
101 if (!es->has_node(E->get()))
102 continue;
103
104 const Node *n = es->get_node(E->get());
105 if (!n)
106 continue;
107
108 bool found;
109 r_ret = n->get(name, &found);
110 if (found)
111 return true;
112 }
113
114 return false;
115 }
116
_get_property_list(List<PropertyInfo> * p_list) const117 void MultiNodeEdit::_get_property_list(List<PropertyInfo> *p_list) const {
118
119 HashMap<String, PLData> usage;
120
121 Node *es = EditorNode::get_singleton()->get_edited_scene();
122 if (!es)
123 return;
124
125 int nc = 0;
126
127 List<PLData *> datas;
128
129 for (const List<NodePath>::Element *E = nodes.front(); E; E = E->next()) {
130
131 if (!es->has_node(E->get()))
132 continue;
133
134 Node *n = es->get_node(E->get());
135 if (!n)
136 continue;
137
138 List<PropertyInfo> plist;
139 n->get_property_list(&plist, true);
140
141 for (List<PropertyInfo>::Element *F = plist.front(); F; F = F->next()) {
142
143 if (F->get().name == "script/script")
144 continue; //added later manually, since this is intercepted before being set (check Variant Object::get() )
145 if (!usage.has(F->get().name)) {
146 PLData pld;
147 pld.uses = 0;
148 pld.info = F->get();
149 usage[F->get().name] = pld;
150 datas.push_back(usage.getptr(F->get().name));
151 }
152
153 usage[F->get().name].uses++;
154 }
155
156 nc++;
157 }
158
159 for (List<PLData *>::Element *E = datas.front(); E; E = E->next()) {
160
161 if (nc == E->get()->uses) {
162 p_list->push_back(E->get()->info);
163 }
164 }
165
166 p_list->push_back(PropertyInfo(Variant::OBJECT, "scripts/script", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
167 }
168
clear_nodes()169 void MultiNodeEdit::clear_nodes() {
170
171 nodes.clear();
172 }
173
add_node(const NodePath & p_node)174 void MultiNodeEdit::add_node(const NodePath &p_node) {
175
176 nodes.push_back(p_node);
177 }
178
set_property_field(const StringName & p_property,const Variant & p_value,const String & p_field)179 void MultiNodeEdit::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
180
181 _set_impl(p_property, p_value, p_field);
182 }
183
MultiNodeEdit()184 MultiNodeEdit::MultiNodeEdit() {
185 }
186