dictionary_property_edit.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*************************************************************************/
  2. /* dictionary_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. #include "dictionary_property_edit.h"
  31. #include "editor_node.h"
  32. void DictionaryPropertyEdit::_notif_change() {
  33. _change_notify();
  34. }
  35. void DictionaryPropertyEdit::_notif_changev(const String &p_v) {
  36. _change_notify(p_v.utf8().get_data());
  37. }
  38. void DictionaryPropertyEdit::_set_key(int p_idx, const Variant &p_new_key) {
  39. // Set key of a dictionary is not allowd
  40. return;
  41. }
  42. void DictionaryPropertyEdit::_set_value(const Variant &p_key, const Variant &p_value) {
  43. Dictionary dict = get_dictionary();
  44. dict[p_key] = p_value;
  45. Object *o = ObjectDB::get_instance(obj);
  46. if (!o)
  47. return;
  48. o->set(property, dict);
  49. }
  50. Variant DictionaryPropertyEdit::get_dictionary() const {
  51. Object *o = ObjectDB::get_instance(obj);
  52. if (!o)
  53. return Dictionary();
  54. Variant dict = o->get(property);
  55. if (dict.get_type() != Variant::DICTIONARY)
  56. return Dictionary();
  57. return dict;
  58. }
  59. void DictionaryPropertyEdit::_get_property_list(List<PropertyInfo> *p_list) const {
  60. Dictionary dict = get_dictionary();
  61. const Array &keys = dict.keys();
  62. for (int i = 0; i < keys.size(); i++) {
  63. String index = itos(i);
  64. const Variant &key = keys[i];
  65. PropertyInfo pi(key.get_type(), index + "/key");
  66. p_list->push_back(pi);
  67. const Variant &value = dict[key];
  68. pi = PropertyInfo(value.get_type(), index + "/value");
  69. p_list->push_back(pi);
  70. }
  71. }
  72. void DictionaryPropertyEdit::edit(Object *p_obj, const StringName &p_prop) {
  73. property = p_prop;
  74. obj = p_obj->get_instance_ID();
  75. }
  76. Node *DictionaryPropertyEdit::get_node() {
  77. Object *o = ObjectDB::get_instance(obj);
  78. if (!o)
  79. return NULL;
  80. return o->cast_to<Node>();
  81. }
  82. void DictionaryPropertyEdit::_bind_methods() {
  83. ObjectTypeDB::bind_method(_MD("_set_key"), &DictionaryPropertyEdit::_set_key);
  84. ObjectTypeDB::bind_method(_MD("_set_value"), &DictionaryPropertyEdit::_set_value);
  85. ObjectTypeDB::bind_method(_MD("_notif_change"), &DictionaryPropertyEdit::_notif_change);
  86. ObjectTypeDB::bind_method(_MD("_notif_changev"), &DictionaryPropertyEdit::_notif_changev);
  87. }
  88. bool DictionaryPropertyEdit::_set(const StringName &p_name, const Variant &p_value) {
  89. Dictionary dict = get_dictionary();
  90. const Array &keys = dict.keys();
  91. String pn = p_name;
  92. int slash = pn.find("/");
  93. if (slash != -1 && pn.length() > slash) {
  94. String type = pn.substr(slash + 1, pn.length());
  95. int index = pn.substr(0, slash).to_int();
  96. if (type == "key" && index < keys.size()) {
  97. const Variant &key = keys[index];
  98. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  99. ur->create_action(TTR("Change Dictionary Key"));
  100. ur->add_do_method(this, "_set_key", index, p_value);
  101. ur->add_undo_method(this, "_set_key", index, key);
  102. ur->add_do_method(this, "_notif_changev", p_name);
  103. ur->add_undo_method(this, "_notif_changev", p_name);
  104. ur->commit_action();
  105. return true;
  106. } else if (type == "value" && index < keys.size()) {
  107. const Variant &key = keys[index];
  108. if (dict.has(key)) {
  109. Variant value = dict[key];
  110. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  111. ur->create_action(TTR("Change Dictionary Value"));
  112. ur->add_do_method(this, "_set_value", key, p_value);
  113. ur->add_undo_method(this, "_set_value", key, value);
  114. ur->add_do_method(this, "_notif_changev", p_name);
  115. ur->add_undo_method(this, "_notif_changev", p_name);
  116. ur->commit_action();
  117. return true;
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123. bool DictionaryPropertyEdit::_get(const StringName &p_name, Variant &r_ret) const {
  124. Dictionary dict = get_dictionary();
  125. const Array &keys = dict.keys();
  126. String pn = p_name;
  127. int slash = pn.find("/");
  128. if (slash != -1 && pn.length() > slash) {
  129. String type = pn.substr(slash + 1, pn.length());
  130. int index = pn.substr(0, slash).to_int();
  131. if (type == "key" && index < keys.size()) {
  132. r_ret = keys[index];
  133. return true;
  134. } else if (type == "value" && index < keys.size()) {
  135. const Variant &key = keys[index];
  136. if (dict.has(key)) {
  137. r_ret = dict[key];
  138. return true;
  139. }
  140. }
  141. }
  142. return false;
  143. }
  144. DictionaryPropertyEdit::DictionaryPropertyEdit() {
  145. obj = 0;
  146. }