dictionary_property_edit.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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(const Variant &p_old_key, const Variant &p_new_key) {
  39. // TODO: Set key of a dictionary is not allowed yet
  40. }
  41. void DictionaryPropertyEdit::_set_value(const Variant &p_key, const Variant &p_value) {
  42. Dictionary dict = get_dictionary();
  43. dict[p_key] = p_value;
  44. Object *o = ObjectDB::get_instance(obj);
  45. if (!o) {
  46. return;
  47. }
  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. }
  55. Variant dict = o->get(property);
  56. if (dict.get_type() != Variant::DICTIONARY) {
  57. return Dictionary();
  58. }
  59. return dict;
  60. }
  61. void DictionaryPropertyEdit::_get_property_list(List<PropertyInfo> *p_list) const {
  62. Dictionary dict = get_dictionary();
  63. Array keys = dict.keys();
  64. keys.sort();
  65. for (int i = 0; i < keys.size(); i++) {
  66. String index = itos(i);
  67. const Variant &key = keys[i];
  68. PropertyInfo pi(key.get_type(), index + ": key");
  69. p_list->push_back(pi);
  70. const Variant &value = dict[key];
  71. pi = PropertyInfo(value.get_type(), index + ": value");
  72. p_list->push_back(pi);
  73. }
  74. }
  75. void DictionaryPropertyEdit::edit(Object *p_obj, const StringName &p_prop) {
  76. property = p_prop;
  77. obj = p_obj->get_instance_id();
  78. }
  79. Node *DictionaryPropertyEdit::get_node() {
  80. Object *o = ObjectDB::get_instance(obj);
  81. if (!o) {
  82. return nullptr;
  83. }
  84. return cast_to<Node>(o);
  85. }
  86. bool DictionaryPropertyEdit::_dont_undo_redo() {
  87. return true;
  88. }
  89. void DictionaryPropertyEdit::_bind_methods() {
  90. ClassDB::bind_method(D_METHOD("_set_key"), &DictionaryPropertyEdit::_set_key);
  91. ClassDB::bind_method(D_METHOD("_set_value"), &DictionaryPropertyEdit::_set_value);
  92. ClassDB::bind_method(D_METHOD("_notif_change"), &DictionaryPropertyEdit::_notif_change);
  93. ClassDB::bind_method(D_METHOD("_notif_changev"), &DictionaryPropertyEdit::_notif_changev);
  94. ClassDB::bind_method(D_METHOD("_dont_undo_redo"), &DictionaryPropertyEdit::_dont_undo_redo);
  95. }
  96. bool DictionaryPropertyEdit::_set(const StringName &p_name, const Variant &p_value) {
  97. Dictionary dict = get_dictionary();
  98. Array keys = dict.keys();
  99. keys.sort();
  100. String pn = p_name;
  101. int slash = pn.find(": ");
  102. if (slash != -1 && pn.length() > slash) {
  103. String type = pn.substr(slash + 2, pn.length());
  104. int index = pn.substr(0, slash).to_int();
  105. if (type == "key" && index < keys.size()) {
  106. const Variant &key = keys[index];
  107. UndoRedo *ur = EditorNode::get_undo_redo();
  108. ur->create_action(TTR("Change Dictionary Key"));
  109. ur->add_do_method(this, "_set_key", key, p_value);
  110. ur->add_undo_method(this, "_set_key", p_value, key);
  111. ur->add_do_method(this, "_notif_changev", p_name);
  112. ur->add_undo_method(this, "_notif_changev", p_name);
  113. ur->commit_action();
  114. return true;
  115. } else if (type == "value" && index < keys.size()) {
  116. const Variant &key = keys[index];
  117. if (dict.has(key)) {
  118. Variant value = dict[key];
  119. UndoRedo *ur = EditorNode::get_undo_redo();
  120. ur->create_action(TTR("Change Dictionary Value"));
  121. ur->add_do_method(this, "_set_value", key, p_value);
  122. ur->add_undo_method(this, "_set_value", key, value);
  123. ur->add_do_method(this, "_notif_changev", p_name);
  124. ur->add_undo_method(this, "_notif_changev", p_name);
  125. ur->commit_action();
  126. return true;
  127. }
  128. }
  129. }
  130. return false;
  131. }
  132. bool DictionaryPropertyEdit::_get(const StringName &p_name, Variant &r_ret) const {
  133. Dictionary dict = get_dictionary();
  134. Array keys = dict.keys();
  135. keys.sort();
  136. String pn = p_name;
  137. int slash = pn.find(": ");
  138. if (slash != -1 && pn.length() > slash) {
  139. String type = pn.substr(slash + 2, pn.length());
  140. int index = pn.substr(0, slash).to_int();
  141. if (type == "key" && index < keys.size()) {
  142. r_ret = keys[index];
  143. return true;
  144. } else if (type == "value" && index < keys.size()) {
  145. const Variant &key = keys[index];
  146. if (dict.has(key)) {
  147. r_ret = dict[key];
  148. return true;
  149. }
  150. }
  151. }
  152. return false;
  153. }
  154. DictionaryPropertyEdit::DictionaryPropertyEdit() {
  155. obj = 0;
  156. }