godot/editor/dictionary_property_edit.cpp

188 lines
6.1 KiB
C++

/*************************************************************************/
/* dictionary_property_edit.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* 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. */
/*************************************************************************/
#include "dictionary_property_edit.h"
#include "editor_node.h"
void DictionaryPropertyEdit::_notif_change() {
_change_notify();
}
void DictionaryPropertyEdit::_notif_changev(const String &p_v) {
_change_notify(p_v.utf8().get_data());
}
void DictionaryPropertyEdit::_set_key(int p_idx, const Variant &p_new_key) {
// Set key of a dictionary is not allowd
return;
}
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);
if (!o)
return;
o->set(property, dict);
}
Variant DictionaryPropertyEdit::get_dictionary() const {
Object *o = ObjectDB::get_instance(obj);
if (!o)
return Dictionary();
Variant dict = o->get(property);
if (dict.get_type() != Variant::DICTIONARY)
return Dictionary();
return dict;
}
void DictionaryPropertyEdit::_get_property_list(List<PropertyInfo> *p_list) const {
Dictionary dict = get_dictionary();
const Array &keys = dict.keys();
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);
if (!o)
return NULL;
return o->cast_to<Node>();
}
void DictionaryPropertyEdit::_bind_methods() {
ObjectTypeDB::bind_method(_MD("_set_key"), &DictionaryPropertyEdit::_set_key);
ObjectTypeDB::bind_method(_MD("_set_value"), &DictionaryPropertyEdit::_set_value);
ObjectTypeDB::bind_method(_MD("_notif_change"), &DictionaryPropertyEdit::_notif_change);
ObjectTypeDB::bind_method(_MD("_notif_changev"), &DictionaryPropertyEdit::_notif_changev);
}
bool DictionaryPropertyEdit::_set(const StringName &p_name, const Variant &p_value) {
Dictionary dict = get_dictionary();
const Array &keys = dict.keys();
String pn = p_name;
int slash = pn.find("/");
if (slash != -1 && pn.length() > slash) {
String type = pn.substr(slash + 1, pn.length());
int index = pn.substr(0, slash).to_int();
if (type == "key" && index < keys.size()) {
const Variant &key = keys[index];
UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
ur->create_action(TTR("Change Dictionary Key"));
ur->add_do_method(this, "_set_key", index, p_value);
ur->add_undo_method(this, "_set_key", index, key);
ur->add_do_method(this, "_notif_changev", p_name);
ur->add_undo_method(this, "_notif_changev", p_name);
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];
UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
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->add_do_method(this, "_notif_changev", p_name);
ur->add_undo_method(this, "_notif_changev", p_name);
ur->commit_action();
return true;
}
}
}
return false;
}
bool DictionaryPropertyEdit::_get(const StringName &p_name, Variant &r_ret) const {
Dictionary dict = get_dictionary();
const Array &keys = dict.keys();
String pn = p_name;
int slash = pn.find("/");
if (slash != -1 && pn.length() > slash) {
String type = pn.substr(slash + 1, 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() {
obj = 0;
}