dictionary_property_edit.cpp 6.0 KB

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