2017-10-17 16:39:31 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* dictionary_property_edit.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2018-01-05 00:50:27 +01:00
|
|
|
/* https://godotengine.org */
|
2017-10-17 16:39:31 +02:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-10-17 16:39:31 +02: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 00:50:27 +01:00
|
|
|
|
2017-10-17 16:39:31 +02:00
|
|
|
#include "dictionary_property_edit.h"
|
|
|
|
#include "editor_node.h"
|
|
|
|
|
|
|
|
void DictionaryPropertyEdit::_notif_change() {
|
2021-02-10 21:18:45 +01:00
|
|
|
notify_property_list_changed();
|
2017-10-17 16:39:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DictionaryPropertyEdit::_set_key(const Variant &p_old_key, const Variant &p_new_key) {
|
2018-02-21 17:30:55 +01:00
|
|
|
// TODO: Set key of a dictionary is not allowed yet
|
2017-10-17 16:39:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DictionaryPropertyEdit::_set_value(const Variant &p_key, const Variant &p_value) {
|
|
|
|
Dictionary dict = get_dictionary();
|
|
|
|
dict[p_key] = p_value;
|
|
|
|
Object *o = ObjectDB::get_instance(obj);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!o) {
|
2017-10-17 16:39:31 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-17 16:39:31 +02:00
|
|
|
|
|
|
|
o->set(property, dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant DictionaryPropertyEdit::get_dictionary() const {
|
|
|
|
Object *o = ObjectDB::get_instance(obj);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!o) {
|
2017-10-17 16:39:31 +02:00
|
|
|
return Dictionary();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-17 16:39:31 +02:00
|
|
|
Variant dict = o->get(property);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (dict.get_type() != Variant::DICTIONARY) {
|
2017-10-17 16:39:31 +02:00
|
|
|
return Dictionary();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-17 16:39:31 +02:00
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DictionaryPropertyEdit::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
Dictionary dict = get_dictionary();
|
|
|
|
|
|
|
|
Array keys = dict.keys();
|
|
|
|
keys.sort();
|
|
|
|
|
|
|
|
for (int i = 0; i < keys.size(); i++) {
|
|
|
|
String index = itos(i);
|
|
|
|
|
|
|
|
const Variant &key = keys[i];
|
|
|
|
PropertyInfo pi(key.get_type(), index + ": key");
|
|
|
|
p_list->push_back(pi);
|
|
|
|
|
|
|
|
const Variant &value = dict[key];
|
|
|
|
pi = PropertyInfo(value.get_type(), index + ": value");
|
|
|
|
p_list->push_back(pi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DictionaryPropertyEdit::edit(Object *p_obj, const StringName &p_prop) {
|
|
|
|
property = p_prop;
|
|
|
|
obj = p_obj->get_instance_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *DictionaryPropertyEdit::get_node() {
|
|
|
|
Object *o = ObjectDB::get_instance(obj);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!o) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-17 16:39:31 +02:00
|
|
|
|
|
|
|
return cast_to<Node>(o);
|
|
|
|
}
|
|
|
|
|
2019-02-14 14:19:03 +01:00
|
|
|
bool DictionaryPropertyEdit::_dont_undo_redo() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-17 16:39:31 +02:00
|
|
|
void DictionaryPropertyEdit::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_key"), &DictionaryPropertyEdit::_set_key);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_value"), &DictionaryPropertyEdit::_set_value);
|
|
|
|
ClassDB::bind_method(D_METHOD("_notif_change"), &DictionaryPropertyEdit::_notif_change);
|
2019-02-14 14:19:03 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_dont_undo_redo"), &DictionaryPropertyEdit::_dont_undo_redo);
|
2017-10-17 16:39:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DictionaryPropertyEdit::_set(const StringName &p_name, const Variant &p_value) {
|
|
|
|
Dictionary dict = get_dictionary();
|
|
|
|
Array keys = dict.keys();
|
|
|
|
keys.sort();
|
|
|
|
|
|
|
|
String pn = p_name;
|
|
|
|
int slash = pn.find(": ");
|
|
|
|
if (slash != -1 && pn.length() > slash) {
|
|
|
|
String type = pn.substr(slash + 2, pn.length());
|
|
|
|
int index = pn.substr(0, slash).to_int();
|
|
|
|
if (type == "key" && index < keys.size()) {
|
|
|
|
const Variant &key = keys[index];
|
2019-06-26 15:08:25 +02:00
|
|
|
UndoRedo *ur = EditorNode::get_undo_redo();
|
2017-10-17 16:39:31 +02:00
|
|
|
|
|
|
|
ur->create_action(TTR("Change Dictionary Key"));
|
|
|
|
ur->add_do_method(this, "_set_key", key, p_value);
|
|
|
|
ur->add_undo_method(this, "_set_key", p_value, key);
|
|
|
|
ur->commit_action();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else if (type == "value" && index < keys.size()) {
|
|
|
|
const Variant &key = keys[index];
|
|
|
|
if (dict.has(key)) {
|
|
|
|
Variant value = dict[key];
|
2019-06-26 15:08:25 +02:00
|
|
|
UndoRedo *ur = EditorNode::get_undo_redo();
|
2017-10-17 16:39:31 +02:00
|
|
|
|
|
|
|
ur->create_action(TTR("Change Dictionary Value"));
|
|
|
|
ur->add_do_method(this, "_set_value", key, p_value);
|
|
|
|
ur->add_undo_method(this, "_set_value", key, value);
|
|
|
|
ur->commit_action();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DictionaryPropertyEdit::_get(const StringName &p_name, Variant &r_ret) const {
|
|
|
|
Dictionary dict = get_dictionary();
|
|
|
|
Array keys = dict.keys();
|
|
|
|
keys.sort();
|
|
|
|
|
|
|
|
String pn = p_name;
|
|
|
|
int slash = pn.find(": ");
|
|
|
|
|
|
|
|
if (slash != -1 && pn.length() > slash) {
|
|
|
|
String type = pn.substr(slash + 2, pn.length());
|
|
|
|
int index = pn.substr(0, slash).to_int();
|
|
|
|
|
|
|
|
if (type == "key" && index < keys.size()) {
|
|
|
|
r_ret = keys[index];
|
|
|
|
return true;
|
|
|
|
} else if (type == "value" && index < keys.size()) {
|
|
|
|
const Variant &key = keys[index];
|
|
|
|
if (dict.has(key)) {
|
|
|
|
r_ret = dict[key];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DictionaryPropertyEdit::DictionaryPropertyEdit() {
|
|
|
|
}
|