array_property_edit.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*************************************************************************/
  2. /* array_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 "array_property_edit.h"
  31. #include "editor_node.h"
  32. #define ITEMS_PER_PAGE 100
  33. Variant ArrayPropertyEdit::get_array() const {
  34. Object *o = ObjectDB::get_instance(obj);
  35. if (!o)
  36. return Array();
  37. Variant arr = o->get(property);
  38. if (!arr.is_array()) {
  39. Variant::CallError ce;
  40. arr = Variant::construct(default_type, NULL, 0, ce);
  41. }
  42. return arr;
  43. }
  44. void ArrayPropertyEdit::_notif_change() {
  45. _change_notify();
  46. }
  47. void ArrayPropertyEdit::_notif_changev(const String &p_v) {
  48. _change_notify(p_v.utf8().get_data());
  49. }
  50. void ArrayPropertyEdit::_set_size(int p_size) {
  51. Variant arr = get_array();
  52. arr.call("resize", p_size);
  53. Object *o = ObjectDB::get_instance(obj);
  54. if (!o)
  55. return;
  56. o->set(property, arr);
  57. }
  58. void ArrayPropertyEdit::_set_value(int p_idx, const Variant &p_value) {
  59. Variant arr = get_array();
  60. arr.set(p_idx, p_value);
  61. Object *o = ObjectDB::get_instance(obj);
  62. if (!o)
  63. return;
  64. o->set(property, arr);
  65. }
  66. bool ArrayPropertyEdit::_set(const StringName &p_name, const Variant &p_value) {
  67. String pn = p_name;
  68. if (pn.begins_with("array/")) {
  69. if (pn == "array/size") {
  70. Variant arr = get_array();
  71. int size = arr.call("size");
  72. int newsize = p_value;
  73. if (newsize == size)
  74. return true;
  75. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  76. ur->create_action(TTR("Resize Array"));
  77. ur->add_do_method(this, "_set_size", newsize);
  78. ur->add_undo_method(this, "_set_size", size);
  79. if (newsize < size) {
  80. for (int i = newsize; i < size; i++) {
  81. ur->add_undo_method(this, "_set_value", i, arr.get(i));
  82. }
  83. } else if (newsize > size && size) {
  84. Variant init;
  85. Variant::CallError ce;
  86. init = Variant::construct(arr.get(size - 1).get_type(), NULL, 0, ce);
  87. for (int i = size; i < newsize; i++) {
  88. ur->add_do_method(this, "_set_value", i, init);
  89. }
  90. }
  91. ur->add_do_method(this, "_notif_change");
  92. ur->add_undo_method(this, "_notif_change");
  93. ur->commit_action();
  94. return true;
  95. }
  96. if (pn == "array/page") {
  97. page = p_value;
  98. _change_notify();
  99. return true;
  100. }
  101. } else if (pn.begins_with("indices")) {
  102. if (pn.find("_") != -1) {
  103. //type
  104. int idx = pn.get_slicec('/', 1).get_slicec('_', 0).to_int();
  105. int type = p_value;
  106. Variant arr = get_array();
  107. Variant value = arr.get(idx);
  108. if (value.get_type() != type && type >= 0 && type < Variant::VARIANT_MAX) {
  109. Variant::CallError ce;
  110. Variant new_value = Variant::construct(Variant::Type(type), NULL, 0, ce);
  111. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  112. ur->create_action(TTR("Change Array Value Type"));
  113. ur->add_do_method(this, "_set_value", idx, new_value);
  114. ur->add_undo_method(this, "_set_value", idx, value);
  115. ur->add_do_method(this, "_notif_change");
  116. ur->add_undo_method(this, "_notif_change");
  117. ur->commit_action();
  118. }
  119. return true;
  120. } else {
  121. int idx = pn.get_slicec('/', 1).to_int();
  122. Variant arr = get_array();
  123. Variant value = arr.get(idx);
  124. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  125. ur->create_action(TTR("Change Array Value"));
  126. ur->add_do_method(this, "_set_value", idx, p_value);
  127. ur->add_undo_method(this, "_set_value", idx, value);
  128. ur->add_do_method(this, "_notif_changev", p_name);
  129. ur->add_undo_method(this, "_notif_changev", p_name);
  130. ur->commit_action();
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. bool ArrayPropertyEdit::_get(const StringName &p_name, Variant &r_ret) const {
  137. Variant arr = get_array();
  138. //int size = arr.call("size");
  139. String pn = p_name;
  140. if (pn.begins_with("array/")) {
  141. if (pn == "array/size") {
  142. r_ret = arr.call("size");
  143. return true;
  144. }
  145. if (pn == "array/page") {
  146. r_ret = page;
  147. return true;
  148. }
  149. } else if (pn.begins_with("indices")) {
  150. if (pn.find("_") != -1) {
  151. //type
  152. int idx = pn.get_slicec('/', 1).get_slicec('_', 0).to_int();
  153. bool valid;
  154. r_ret = arr.get(idx, &valid);
  155. if (valid)
  156. r_ret = r_ret.get_type();
  157. return valid;
  158. } else {
  159. int idx = pn.get_slicec('/', 1).to_int();
  160. bool valid;
  161. r_ret = arr.get(idx, &valid);
  162. return valid;
  163. }
  164. }
  165. return false;
  166. }
  167. void ArrayPropertyEdit::_get_property_list(List<PropertyInfo> *p_list) const {
  168. Variant arr = get_array();
  169. int size = arr.call("size");
  170. p_list->push_back(PropertyInfo(Variant::INT, "array/size", PROPERTY_HINT_RANGE, "0,100000,1"));
  171. int pages = size / ITEMS_PER_PAGE;
  172. if (pages > 0)
  173. p_list->push_back(PropertyInfo(Variant::INT, "array/page", PROPERTY_HINT_RANGE, "0," + itos(pages) + ",1"));
  174. int offset = page * ITEMS_PER_PAGE;
  175. int items = MIN(size - offset, ITEMS_PER_PAGE);
  176. for (int i = 0; i < items; i++) {
  177. Variant v = arr.get(i + offset);
  178. if (arr.get_type() == Variant::ARRAY) {
  179. p_list->push_back(PropertyInfo(Variant::INT, "indices/" + itos(i + offset) + "_type", PROPERTY_HINT_ENUM, vtypes));
  180. }
  181. if (arr.get_type() != Variant::ARRAY || v.get_type() != Variant::NIL) {
  182. PropertyInfo pi(v.get_type(), "indices/" + itos(i + offset));
  183. if (v.get_type() == Variant::OBJECT) {
  184. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  185. pi.hint_string = "Resource";
  186. }
  187. p_list->push_back(pi);
  188. }
  189. }
  190. }
  191. void ArrayPropertyEdit::edit(Object *p_obj, const StringName &p_prop, Variant::Type p_deftype) {
  192. page = 0;
  193. property = p_prop;
  194. obj = p_obj->get_instance_ID();
  195. default_type = p_deftype;
  196. }
  197. Node *ArrayPropertyEdit::get_node() {
  198. Object *o = ObjectDB::get_instance(obj);
  199. if (!o)
  200. return NULL;
  201. return o->cast_to<Node>();
  202. }
  203. void ArrayPropertyEdit::_bind_methods() {
  204. ObjectTypeDB::bind_method(_MD("_set_size"), &ArrayPropertyEdit::_set_size);
  205. ObjectTypeDB::bind_method(_MD("_set_value"), &ArrayPropertyEdit::_set_value);
  206. ObjectTypeDB::bind_method(_MD("_notif_change"), &ArrayPropertyEdit::_notif_change);
  207. ObjectTypeDB::bind_method(_MD("_notif_changev"), &ArrayPropertyEdit::_notif_changev);
  208. }
  209. ArrayPropertyEdit::ArrayPropertyEdit() {
  210. page = 0;
  211. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  212. if (i > 0)
  213. vtypes += ",";
  214. vtypes += Variant::get_type_name(Variant::Type(i));
  215. }
  216. default_type = Variant::NIL;
  217. }