1 /*************************************************************************/
2 /* array_property_edit.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 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
31 #include "array_property_edit.h"
32
33 #include "core/io/marshalls.h"
34 #include "editor_node.h"
35
36 #define ITEMS_PER_PAGE 100
37
get_array() const38 Variant ArrayPropertyEdit::get_array() const {
39
40 Object *o = ObjectDB::get_instance(obj);
41 if (!o)
42 return Array();
43 Variant arr = o->get(property);
44 if (!arr.is_array()) {
45 Variant::CallError ce;
46 arr = Variant::construct(default_type, NULL, 0, ce);
47 }
48 return arr;
49 }
50
_notif_change()51 void ArrayPropertyEdit::_notif_change() {
52 _change_notify();
53 }
_notif_changev(const String & p_v)54 void ArrayPropertyEdit::_notif_changev(const String &p_v) {
55
56 _change_notify(p_v.utf8().get_data());
57 }
58
_set_size(int p_size)59 void ArrayPropertyEdit::_set_size(int p_size) {
60
61 Variant arr = get_array();
62 arr.call("resize", p_size);
63 Object *o = ObjectDB::get_instance(obj);
64 if (!o)
65 return;
66
67 o->set(property, arr);
68 }
69
_set_value(int p_idx,const Variant & p_value)70 void ArrayPropertyEdit::_set_value(int p_idx, const Variant &p_value) {
71
72 Variant arr = get_array();
73 arr.set(p_idx, p_value);
74 Object *o = ObjectDB::get_instance(obj);
75 if (!o)
76 return;
77
78 o->set(property, arr);
79 }
80
_set(const StringName & p_name,const Variant & p_value)81 bool ArrayPropertyEdit::_set(const StringName &p_name, const Variant &p_value) {
82
83 String pn = p_name;
84
85 if (pn.begins_with("array/")) {
86
87 if (pn == "array/size") {
88
89 Variant arr = get_array();
90 int size = arr.call("size");
91
92 int newsize = p_value;
93 if (newsize == size)
94 return true;
95
96 UndoRedo *ur = EditorNode::get_undo_redo();
97 ur->create_action(TTR("Resize Array"));
98 ur->add_do_method(this, "_set_size", newsize);
99 ur->add_undo_method(this, "_set_size", size);
100 if (newsize < size) {
101 for (int i = newsize; i < size; i++) {
102 ur->add_undo_method(this, "_set_value", i, arr.get(i));
103 }
104 } else if (newsize > size) {
105
106 Variant init;
107 Variant::CallError ce;
108 Variant::Type new_type = subtype;
109 if (new_type == Variant::NIL && size) {
110 new_type = arr.get(size - 1).get_type();
111 }
112 if (new_type != Variant::NIL) {
113 init = Variant::construct(new_type, NULL, 0, ce);
114 for (int i = size; i < newsize; i++) {
115 ur->add_do_method(this, "_set_value", i, init);
116 }
117 }
118 }
119 ur->add_do_method(this, "_notif_change");
120 ur->add_undo_method(this, "_notif_change");
121 ur->commit_action();
122 return true;
123 }
124 if (pn == "array/page") {
125 page = p_value;
126 _change_notify();
127 return true;
128 }
129
130 } else if (pn.begins_with("indices")) {
131
132 if (pn.find("_") != -1) {
133 //type
134 int idx = pn.get_slicec('/', 1).get_slicec('_', 0).to_int();
135
136 int type = p_value;
137
138 Variant arr = get_array();
139
140 Variant value = arr.get(idx);
141 if (value.get_type() != type && type >= 0 && type < Variant::VARIANT_MAX) {
142 Variant::CallError ce;
143 Variant new_value = Variant::construct(Variant::Type(type), NULL, 0, ce);
144 UndoRedo *ur = EditorNode::get_undo_redo();
145
146 ur->create_action(TTR("Change Array Value Type"));
147 ur->add_do_method(this, "_set_value", idx, new_value);
148 ur->add_undo_method(this, "_set_value", idx, value);
149 ur->add_do_method(this, "_notif_change");
150 ur->add_undo_method(this, "_notif_change");
151 ur->commit_action();
152 }
153 return true;
154
155 } else {
156 int idx = pn.get_slicec('/', 1).to_int();
157 Variant arr = get_array();
158
159 Variant value = arr.get(idx);
160 UndoRedo *ur = EditorNode::get_undo_redo();
161
162 ur->create_action(TTR("Change Array Value"));
163 ur->add_do_method(this, "_set_value", idx, p_value);
164 ur->add_undo_method(this, "_set_value", idx, value);
165 ur->add_do_method(this, "_notif_changev", p_name);
166 ur->add_undo_method(this, "_notif_changev", p_name);
167 ur->commit_action();
168 return true;
169 }
170 }
171
172 return false;
173 }
174
_get(const StringName & p_name,Variant & r_ret) const175 bool ArrayPropertyEdit::_get(const StringName &p_name, Variant &r_ret) const {
176
177 Variant arr = get_array();
178 //int size = arr.call("size");
179
180 String pn = p_name;
181 if (pn.begins_with("array/")) {
182
183 if (pn == "array/size") {
184 r_ret = arr.call("size");
185 return true;
186 }
187 if (pn == "array/page") {
188 r_ret = page;
189 return true;
190 }
191 } else if (pn.begins_with("indices")) {
192
193 if (pn.find("_") != -1) {
194 //type
195 int idx = pn.get_slicec('/', 1).get_slicec('_', 0).to_int();
196 bool valid;
197 r_ret = arr.get(idx, &valid);
198 if (valid)
199 r_ret = r_ret.get_type();
200 return valid;
201
202 } else {
203 int idx = pn.get_slicec('/', 1).to_int();
204 bool valid;
205 r_ret = arr.get(idx, &valid);
206
207 if (r_ret.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(r_ret)) {
208 r_ret = Object::cast_to<EncodedObjectAsID>(r_ret)->get_object_id();
209 }
210
211 return valid;
212 }
213 }
214
215 return false;
216 }
217
_get_property_list(List<PropertyInfo> * p_list) const218 void ArrayPropertyEdit::_get_property_list(List<PropertyInfo> *p_list) const {
219
220 Variant arr = get_array();
221 int size = arr.call("size");
222
223 p_list->push_back(PropertyInfo(Variant::INT, "array/size", PROPERTY_HINT_RANGE, "0,100000,1"));
224 int pages = size / ITEMS_PER_PAGE;
225 if (pages > 0)
226 p_list->push_back(PropertyInfo(Variant::INT, "array/page", PROPERTY_HINT_RANGE, "0," + itos(pages) + ",1"));
227
228 int offset = page * ITEMS_PER_PAGE;
229
230 int items = MIN(size - offset, ITEMS_PER_PAGE);
231
232 for (int i = 0; i < items; i++) {
233
234 Variant v = arr.get(i + offset);
235 bool is_typed = arr.get_type() != Variant::ARRAY || subtype != Variant::NIL;
236
237 if (!is_typed) {
238 p_list->push_back(PropertyInfo(Variant::INT, "indices/" + itos(i + offset) + "_type", PROPERTY_HINT_ENUM, vtypes));
239 }
240
241 if (v.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(v)) {
242 p_list->push_back(PropertyInfo(Variant::INT, "indices/" + itos(i + offset), PROPERTY_HINT_OBJECT_ID, "Object"));
243 continue;
244 }
245
246 if (is_typed || v.get_type() != Variant::NIL) {
247 PropertyInfo pi(v.get_type(), "indices/" + itos(i + offset));
248 if (subtype != Variant::NIL) {
249 pi.type = Variant::Type(subtype);
250 pi.hint = PropertyHint(subtype_hint);
251 pi.hint_string = subtype_hint_string;
252 } else if (v.get_type() == Variant::OBJECT) {
253 pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
254 pi.hint_string = "Resource";
255 }
256
257 p_list->push_back(pi);
258 }
259 }
260 }
261
edit(Object * p_obj,const StringName & p_prop,const String & p_hint_string,Variant::Type p_deftype)262 void ArrayPropertyEdit::edit(Object *p_obj, const StringName &p_prop, const String &p_hint_string, Variant::Type p_deftype) {
263
264 page = 0;
265 property = p_prop;
266 obj = p_obj->get_instance_id();
267 default_type = p_deftype;
268
269 if (!p_hint_string.empty()) {
270 int hint_subtype_separator = p_hint_string.find(":");
271 if (hint_subtype_separator >= 0) {
272 String subtype_string = p_hint_string.substr(0, hint_subtype_separator);
273
274 int slash_pos = subtype_string.find("/");
275 if (slash_pos >= 0) {
276 subtype_hint = PropertyHint(subtype_string.substr(slash_pos + 1, subtype_string.size() - slash_pos - 1).to_int());
277 subtype_string = subtype_string.substr(0, slash_pos);
278 }
279
280 subtype_hint_string = p_hint_string.substr(hint_subtype_separator + 1, p_hint_string.size() - hint_subtype_separator - 1);
281 subtype = Variant::Type(subtype_string.to_int());
282 }
283 }
284 }
285
get_node()286 Node *ArrayPropertyEdit::get_node() {
287
288 return Object::cast_to<Node>(ObjectDB::get_instance(obj));
289 }
290
_dont_undo_redo()291 bool ArrayPropertyEdit::_dont_undo_redo() {
292 return true;
293 }
294
_bind_methods()295 void ArrayPropertyEdit::_bind_methods() {
296
297 ClassDB::bind_method(D_METHOD("_set_size"), &ArrayPropertyEdit::_set_size);
298 ClassDB::bind_method(D_METHOD("_set_value"), &ArrayPropertyEdit::_set_value);
299 ClassDB::bind_method(D_METHOD("_notif_change"), &ArrayPropertyEdit::_notif_change);
300 ClassDB::bind_method(D_METHOD("_notif_changev"), &ArrayPropertyEdit::_notif_changev);
301 ClassDB::bind_method(D_METHOD("_dont_undo_redo"), &ArrayPropertyEdit::_dont_undo_redo);
302 }
303
ArrayPropertyEdit()304 ArrayPropertyEdit::ArrayPropertyEdit() {
305 page = 0;
306 for (int i = 0; i < Variant::VARIANT_MAX; i++) {
307
308 if (i > 0)
309 vtypes += ",";
310 vtypes += Variant::get_type_name(Variant::Type(i));
311 }
312 default_type = Variant::NIL;
313 subtype = Variant::NIL;
314 subtype_hint = PROPERTY_HINT_NONE;
315 subtype_hint_string = "";
316 }
317