2016-06-18 20:46:12 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* array_property_edit.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 20:46:12 +08:00
|
|
|
/*************************************************************************/
|
2020-01-01 18:16:22 +08:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 20:46:12 +08:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2015-08-26 07:45:51 +08:00
|
|
|
#include "array_property_edit.h"
|
|
|
|
|
2018-11-22 09:10:27 +08:00
|
|
|
#include "core/io/marshalls.h"
|
2015-08-26 07:45:51 +08:00
|
|
|
#include "editor_node.h"
|
|
|
|
|
|
|
|
#define ITEMS_PER_PAGE 100
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Variant ArrayPropertyEdit::get_array() const {
|
|
|
|
Object *o = ObjectDB::get_instance(obj);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!o) {
|
2015-08-26 07:45:51 +08:00
|
|
|
return Array();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
Variant arr = o->get(property);
|
2015-08-26 10:09:41 +08:00
|
|
|
if (!arr.is_array()) {
|
2020-02-20 03:27:19 +08:00
|
|
|
Callable::CallError ce;
|
2020-04-02 07:20:12 +08:00
|
|
|
arr = Variant::construct(default_type, nullptr, 0, ce);
|
2015-08-26 10:09:41 +08:00
|
|
|
}
|
|
|
|
return arr;
|
2015-08-26 07:45:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArrayPropertyEdit::_notif_change() {
|
|
|
|
_change_notify();
|
|
|
|
}
|
2020-05-14 20:29:06 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ArrayPropertyEdit::_notif_changev(const String &p_v) {
|
2015-08-26 07:45:51 +08:00
|
|
|
_change_notify(p_v.utf8().get_data());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArrayPropertyEdit::_set_size(int p_size) {
|
|
|
|
Variant arr = get_array();
|
2017-03-05 23:44:50 +08:00
|
|
|
arr.call("resize", p_size);
|
|
|
|
Object *o = ObjectDB::get_instance(obj);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!o) {
|
2015-08-26 07:45:51 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-08-26 07:45:51 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
o->set(property, arr);
|
2015-08-26 07:45:51 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ArrayPropertyEdit::_set_value(int p_idx, const Variant &p_value) {
|
2015-08-26 07:45:51 +08:00
|
|
|
Variant arr = get_array();
|
2017-03-05 23:44:50 +08:00
|
|
|
arr.set(p_idx, p_value);
|
|
|
|
Object *o = ObjectDB::get_instance(obj);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!o) {
|
2015-08-26 07:45:51 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-08-26 07:45:51 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
o->set(property, arr);
|
2015-08-26 07:45:51 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool ArrayPropertyEdit::_set(const StringName &p_name, const Variant &p_value) {
|
|
|
|
String pn = p_name;
|
2015-08-26 07:45:51 +08:00
|
|
|
|
|
|
|
if (pn.begins_with("array/")) {
|
2017-03-05 23:44:50 +08:00
|
|
|
if (pn == "array/size") {
|
2015-08-26 07:45:51 +08:00
|
|
|
Variant arr = get_array();
|
|
|
|
int size = arr.call("size");
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int newsize = p_value;
|
2020-05-14 22:41:43 +08:00
|
|
|
if (newsize == size) {
|
2015-08-26 07:45:51 +08:00
|
|
|
return true;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-08-26 07:45:51 +08:00
|
|
|
|
2019-06-26 21:08:25 +08:00
|
|
|
UndoRedo *ur = EditorNode::get_undo_redo();
|
2016-05-04 09:25:37 +08:00
|
|
|
ur->create_action(TTR("Resize Array"));
|
2017-03-05 23:44:50 +08:00
|
|
|
ur->add_do_method(this, "_set_size", newsize);
|
|
|
|
ur->add_undo_method(this, "_set_size", size);
|
|
|
|
if (newsize < size) {
|
|
|
|
for (int i = newsize; i < size; i++) {
|
|
|
|
ur->add_undo_method(this, "_set_value", i, arr.get(i));
|
2015-08-26 07:45:51 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
} else if (newsize > size) {
|
2016-01-01 04:46:07 +08:00
|
|
|
Variant init;
|
2020-02-20 03:27:19 +08:00
|
|
|
Callable::CallError ce;
|
2016-10-26 19:38:41 +08:00
|
|
|
Variant::Type new_type = subtype;
|
2017-03-05 23:44:50 +08:00
|
|
|
if (new_type == Variant::NIL && size) {
|
|
|
|
new_type = arr.get(size - 1).get_type();
|
2016-10-26 19:38:41 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
if (new_type != Variant::NIL) {
|
2020-04-02 07:20:12 +08:00
|
|
|
init = Variant::construct(new_type, nullptr, 0, ce);
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = size; i < newsize; i++) {
|
|
|
|
ur->add_do_method(this, "_set_value", i, init);
|
2016-10-26 19:38:41 +08:00
|
|
|
}
|
2016-01-01 04:46:07 +08:00
|
|
|
}
|
2015-08-26 07:45:51 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
ur->add_do_method(this, "_notif_change");
|
|
|
|
ur->add_undo_method(this, "_notif_change");
|
2015-08-26 07:45:51 +08:00
|
|
|
ur->commit_action();
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
if (pn == "array/page") {
|
|
|
|
page = p_value;
|
2015-08-26 07:45:51 +08:00
|
|
|
_change_notify();
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-01 04:56:51 +08:00
|
|
|
|
2015-08-26 07:45:51 +08:00
|
|
|
} else if (pn.begins_with("indices")) {
|
2017-03-05 23:44:50 +08:00
|
|
|
if (pn.find("_") != -1) {
|
2015-08-26 07:45:51 +08:00
|
|
|
//type
|
2017-03-05 23:44:50 +08:00
|
|
|
int idx = pn.get_slicec('/', 1).get_slicec('_', 0).to_int();
|
2015-08-26 07:45:51 +08:00
|
|
|
|
|
|
|
int type = p_value;
|
|
|
|
|
|
|
|
Variant arr = get_array();
|
|
|
|
|
|
|
|
Variant value = arr.get(idx);
|
2017-03-05 23:44:50 +08:00
|
|
|
if (value.get_type() != type && type >= 0 && type < Variant::VARIANT_MAX) {
|
2020-02-20 03:27:19 +08:00
|
|
|
Callable::CallError ce;
|
2020-04-02 07:20:12 +08:00
|
|
|
Variant new_value = Variant::construct(Variant::Type(type), nullptr, 0, ce);
|
2019-06-26 21:08:25 +08:00
|
|
|
UndoRedo *ur = EditorNode::get_undo_redo();
|
2015-08-26 07:45:51 +08:00
|
|
|
|
2016-05-04 09:25:37 +08:00
|
|
|
ur->create_action(TTR("Change Array Value Type"));
|
2017-03-05 23:44:50 +08:00
|
|
|
ur->add_do_method(this, "_set_value", idx, new_value);
|
|
|
|
ur->add_undo_method(this, "_set_value", idx, value);
|
|
|
|
ur->add_do_method(this, "_notif_change");
|
|
|
|
ur->add_undo_method(this, "_notif_change");
|
2015-08-26 07:45:51 +08:00
|
|
|
ur->commit_action();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
2017-03-05 23:44:50 +08:00
|
|
|
int idx = pn.get_slicec('/', 1).to_int();
|
2015-08-26 07:45:51 +08:00
|
|
|
Variant arr = get_array();
|
|
|
|
|
|
|
|
Variant value = arr.get(idx);
|
2019-06-26 21:08:25 +08:00
|
|
|
UndoRedo *ur = EditorNode::get_undo_redo();
|
2015-08-26 07:45:51 +08:00
|
|
|
|
2016-05-04 09:25:37 +08:00
|
|
|
ur->create_action(TTR("Change Array Value"));
|
2017-03-05 23:44:50 +08:00
|
|
|
ur->add_do_method(this, "_set_value", idx, p_value);
|
|
|
|
ur->add_undo_method(this, "_set_value", idx, value);
|
|
|
|
ur->add_do_method(this, "_notif_changev", p_name);
|
|
|
|
ur->add_undo_method(this, "_notif_changev", p_name);
|
2015-08-26 07:45:51 +08:00
|
|
|
ur->commit_action();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool ArrayPropertyEdit::_get(const StringName &p_name, Variant &r_ret) const {
|
2015-08-26 07:45:51 +08:00
|
|
|
Variant arr = get_array();
|
|
|
|
//int size = arr.call("size");
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
String pn = p_name;
|
2015-08-26 07:45:51 +08:00
|
|
|
if (pn.begins_with("array/")) {
|
2017-03-05 23:44:50 +08:00
|
|
|
if (pn == "array/size") {
|
|
|
|
r_ret = arr.call("size");
|
2015-08-26 07:45:51 +08:00
|
|
|
return true;
|
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
if (pn == "array/page") {
|
|
|
|
r_ret = page;
|
2015-08-26 07:45:51 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (pn.begins_with("indices")) {
|
2017-03-05 23:44:50 +08:00
|
|
|
if (pn.find("_") != -1) {
|
2015-08-26 07:45:51 +08:00
|
|
|
//type
|
2017-03-05 23:44:50 +08:00
|
|
|
int idx = pn.get_slicec('/', 1).get_slicec('_', 0).to_int();
|
2015-08-26 07:45:51 +08:00
|
|
|
bool valid;
|
2017-03-05 23:44:50 +08:00
|
|
|
r_ret = arr.get(idx, &valid);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (valid) {
|
2017-03-05 23:44:50 +08:00
|
|
|
r_ret = r_ret.get_type();
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-08-26 07:45:51 +08:00
|
|
|
return valid;
|
|
|
|
|
|
|
|
} else {
|
2017-03-05 23:44:50 +08:00
|
|
|
int idx = pn.get_slicec('/', 1).to_int();
|
2015-08-26 07:45:51 +08:00
|
|
|
bool valid;
|
2017-03-05 23:44:50 +08:00
|
|
|
r_ret = arr.get(idx, &valid);
|
2018-11-22 09:10:27 +08:00
|
|
|
|
|
|
|
if (r_ret.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(r_ret)) {
|
|
|
|
r_ret = Object::cast_to<EncodedObjectAsID>(r_ret)->get_object_id();
|
|
|
|
}
|
|
|
|
|
2015-08-26 07:45:51 +08:00
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ArrayPropertyEdit::_get_property_list(List<PropertyInfo> *p_list) const {
|
2015-08-26 07:45:51 +08:00
|
|
|
Variant arr = get_array();
|
|
|
|
int size = arr.call("size");
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "array/size", PROPERTY_HINT_RANGE, "0,100000,1"));
|
|
|
|
int pages = size / ITEMS_PER_PAGE;
|
2020-05-14 22:41:43 +08:00
|
|
|
if (pages > 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "array/page", PROPERTY_HINT_RANGE, "0," + itos(pages) + ",1"));
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-08-26 07:45:51 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int offset = page * ITEMS_PER_PAGE;
|
2015-08-26 07:45:51 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int items = MIN(size - offset, ITEMS_PER_PAGE);
|
2015-08-26 07:45:51 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < items; i++) {
|
|
|
|
Variant v = arr.get(i + offset);
|
|
|
|
bool is_typed = arr.get_type() != Variant::ARRAY || subtype != Variant::NIL;
|
2015-08-26 07:45:51 +08:00
|
|
|
|
2016-10-26 19:38:41 +08:00
|
|
|
if (!is_typed) {
|
2017-03-05 23:44:50 +08:00
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "indices/" + itos(i + offset) + "_type", PROPERTY_HINT_ENUM, vtypes));
|
2015-08-26 07:45:51 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
|
2018-11-22 09:10:27 +08:00
|
|
|
if (v.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(v)) {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "indices/" + itos(i + offset), PROPERTY_HINT_OBJECT_ID, "Object"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (is_typed || v.get_type() != Variant::NIL) {
|
|
|
|
PropertyInfo pi(v.get_type(), "indices/" + itos(i + offset));
|
|
|
|
if (subtype != Variant::NIL) {
|
2016-10-26 19:38:41 +08:00
|
|
|
pi.type = Variant::Type(subtype);
|
|
|
|
pi.hint = PropertyHint(subtype_hint);
|
|
|
|
pi.hint_string = subtype_hint_string;
|
2017-03-05 23:44:50 +08:00
|
|
|
} else if (v.get_type() == Variant::OBJECT) {
|
|
|
|
pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
|
|
|
|
pi.hint_string = "Resource";
|
2015-08-26 07:45:51 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
|
2015-08-26 07:45:51 +08:00
|
|
|
p_list->push_back(pi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ArrayPropertyEdit::edit(Object *p_obj, const StringName &p_prop, const String &p_hint_string, Variant::Type p_deftype) {
|
|
|
|
page = 0;
|
|
|
|
property = p_prop;
|
2017-08-07 18:17:31 +08:00
|
|
|
obj = p_obj->get_instance_id();
|
2017-03-05 23:44:50 +08:00
|
|
|
default_type = p_deftype;
|
2015-08-26 07:45:51 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (!p_hint_string.empty()) {
|
2019-10-31 20:40:58 +08:00
|
|
|
int hint_subtype_separator = p_hint_string.find(":");
|
|
|
|
if (hint_subtype_separator >= 0) {
|
|
|
|
String subtype_string = p_hint_string.substr(0, hint_subtype_separator);
|
2017-03-05 23:44:50 +08:00
|
|
|
|
2016-10-26 19:38:41 +08:00
|
|
|
int slash_pos = subtype_string.find("/");
|
2017-03-05 23:44:50 +08:00
|
|
|
if (slash_pos >= 0) {
|
|
|
|
subtype_hint = PropertyHint(subtype_string.substr(slash_pos + 1, subtype_string.size() - slash_pos - 1).to_int());
|
|
|
|
subtype_string = subtype_string.substr(0, slash_pos);
|
2016-10-26 19:38:41 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
|
2019-10-31 20:40:58 +08:00
|
|
|
subtype_hint_string = p_hint_string.substr(hint_subtype_separator + 1, p_hint_string.size() - hint_subtype_separator - 1);
|
2017-03-05 23:44:50 +08:00
|
|
|
subtype = Variant::Type(subtype_string.to_int());
|
2016-10-26 19:38:41 +08:00
|
|
|
}
|
|
|
|
}
|
2015-08-26 07:45:51 +08:00
|
|
|
}
|
|
|
|
|
2015-12-13 22:42:29 +08:00
|
|
|
Node *ArrayPropertyEdit::get_node() {
|
2017-08-25 04:58:51 +08:00
|
|
|
return Object::cast_to<Node>(ObjectDB::get_instance(obj));
|
2015-12-13 22:42:29 +08:00
|
|
|
}
|
|
|
|
|
2019-02-14 21:19:03 +08:00
|
|
|
bool ArrayPropertyEdit::_dont_undo_redo() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-26 07:45:51 +08:00
|
|
|
void ArrayPropertyEdit::_bind_methods() {
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_size"), &ArrayPropertyEdit::_set_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_value"), &ArrayPropertyEdit::_set_value);
|
|
|
|
ClassDB::bind_method(D_METHOD("_notif_change"), &ArrayPropertyEdit::_notif_change);
|
|
|
|
ClassDB::bind_method(D_METHOD("_notif_changev"), &ArrayPropertyEdit::_notif_changev);
|
2019-02-14 21:19:03 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("_dont_undo_redo"), &ArrayPropertyEdit::_dont_undo_redo);
|
2015-08-26 07:45:51 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ArrayPropertyEdit::ArrayPropertyEdit() {
|
|
|
|
page = 0;
|
|
|
|
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (i > 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
vtypes += ",";
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
vtypes += Variant::get_type_name(Variant::Type(i));
|
2015-08-26 07:45:51 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
default_type = Variant::NIL;
|
|
|
|
subtype = Variant::NIL;
|
|
|
|
subtype_hint = PROPERTY_HINT_NONE;
|
|
|
|
subtype_hint_string = "";
|
2015-08-26 07:45:51 +08:00
|
|
|
}
|