| 
									
										
										
										
											2017-10-17 22:39:31 +08: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 22:39:31 +08:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-01-01 11:16:22 +01:00
										 |  |  | /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2017-10-17 22:39:31 +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 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 22:39:31 +08:00
										 |  |  | #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(const Variant &p_old_key, const Variant &p_new_key) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 11:30:55 -05:00
										 |  |  | 	// TODO: Set key of a dictionary is not allowed yet
 | 
					
						
							| 
									
										
										
										
											2017-10-17 22:39:31 +08: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); | 
					
						
							|  |  |  | 	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(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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); | 
					
						
							|  |  |  | 	if (!o) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return cast_to<Node>(o); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-14 10:19:03 -03:00
										 |  |  | bool DictionaryPropertyEdit::_dont_undo_redo() { | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 22:39:31 +08: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); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("_notif_changev"), &DictionaryPropertyEdit::_notif_changev); | 
					
						
							| 
									
										
										
										
											2019-02-14 10:19:03 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("_dont_undo_redo"), &DictionaryPropertyEdit::_dont_undo_redo); | 
					
						
							| 
									
										
										
										
											2017-10-17 22:39:31 +08: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 22:39:31 +08: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->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]; | 
					
						
							| 
									
										
										
										
											2019-06-26 15:08:25 +02:00
										 |  |  | 				UndoRedo *ur = EditorNode::get_undo_redo(); | 
					
						
							| 
									
										
										
										
											2017-10-17 22:39:31 +08: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->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(); | 
					
						
							|  |  |  | 	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() { | 
					
						
							|  |  |  | } |