array_property_edit.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /**************************************************************************/
  2. /* array_property_edit.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "core/io/marshalls.h"
  32. #include "editor_node.h"
  33. #define ITEMS_PER_PAGE 100
  34. Variant ArrayPropertyEdit::get_array() const {
  35. Object *o = ObjectDB::get_instance(obj);
  36. if (!o) {
  37. return Array();
  38. }
  39. Variant arr = o->get(property);
  40. if (!arr.is_array()) {
  41. Variant::CallError ce;
  42. arr = Variant::construct(default_type, nullptr, 0, ce);
  43. }
  44. return arr;
  45. }
  46. void ArrayPropertyEdit::_notif_change() {
  47. _change_notify();
  48. }
  49. void ArrayPropertyEdit::_notif_changev(const String &p_v) {
  50. _change_notify(p_v.utf8().get_data());
  51. }
  52. void ArrayPropertyEdit::_set_size(int p_size) {
  53. Variant arr = get_array();
  54. arr.call("resize", p_size);
  55. Object *o = ObjectDB::get_instance(obj);
  56. if (!o) {
  57. return;
  58. }
  59. o->set(property, arr);
  60. }
  61. void ArrayPropertyEdit::_set_value(int p_idx, const Variant &p_value) {
  62. Variant arr = get_array();
  63. arr.set(p_idx, p_value);
  64. Object *o = ObjectDB::get_instance(obj);
  65. if (!o) {
  66. return;
  67. }
  68. o->set(property, arr);
  69. }
  70. bool ArrayPropertyEdit::_set(const StringName &p_name, const Variant &p_value) {
  71. String pn = p_name;
  72. if (pn.begins_with("array/")) {
  73. if (pn == "array/size") {
  74. Variant arr = get_array();
  75. int size = arr.call("size");
  76. int newsize = p_value;
  77. if (newsize == size) {
  78. return true;
  79. }
  80. UndoRedo *ur = EditorNode::get_undo_redo();
  81. ur->create_action(TTR("Resize Array"));
  82. ur->add_do_method(this, "_set_size", newsize);
  83. ur->add_undo_method(this, "_set_size", size);
  84. if (newsize < size) {
  85. for (int i = newsize; i < size; i++) {
  86. ur->add_undo_method(this, "_set_value", i, arr.get(i));
  87. }
  88. } else if (newsize > size) {
  89. Variant init;
  90. Variant::CallError ce;
  91. Variant::Type new_type = subtype;
  92. if (new_type == Variant::NIL && size) {
  93. new_type = arr.get(size - 1).get_type();
  94. }
  95. if (new_type != Variant::NIL) {
  96. init = Variant::construct(new_type, nullptr, 0, ce);
  97. for (int i = size; i < newsize; i++) {
  98. ur->add_do_method(this, "_set_value", i, init);
  99. }
  100. }
  101. }
  102. ur->add_do_method(this, "_notif_change");
  103. ur->add_undo_method(this, "_notif_change");
  104. ur->commit_action();
  105. return true;
  106. }
  107. if (pn == "array/page") {
  108. page = p_value;
  109. _change_notify();
  110. return true;
  111. }
  112. } else if (pn.begins_with("indices")) {
  113. if (pn.find("_") != -1) {
  114. //type
  115. int idx = pn.get_slicec('/', 1).get_slicec('_', 0).to_int();
  116. int type = p_value;
  117. Variant arr = get_array();
  118. Variant value = arr.get(idx);
  119. if (value.get_type() != type && type >= 0 && type < Variant::VARIANT_MAX) {
  120. Variant::CallError ce;
  121. Variant new_value = Variant::construct(Variant::Type(type), nullptr, 0, ce);
  122. UndoRedo *ur = EditorNode::get_undo_redo();
  123. ur->create_action(TTR("Change Array Value Type"));
  124. ur->add_do_method(this, "_set_value", idx, new_value);
  125. ur->add_undo_method(this, "_set_value", idx, value);
  126. ur->add_do_method(this, "_notif_change");
  127. ur->add_undo_method(this, "_notif_change");
  128. ur->commit_action();
  129. }
  130. return true;
  131. } else {
  132. int idx = pn.get_slicec('/', 1).to_int();
  133. Variant arr = get_array();
  134. Variant value = arr.get(idx);
  135. UndoRedo *ur = EditorNode::get_undo_redo();
  136. ur->create_action(TTR("Change Array Value"));
  137. ur->add_do_method(this, "_set_value", idx, p_value);
  138. ur->add_undo_method(this, "_set_value", idx, value);
  139. ur->add_do_method(this, "_notif_changev", p_name);
  140. ur->add_undo_method(this, "_notif_changev", p_name);
  141. ur->commit_action();
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. bool ArrayPropertyEdit::_get(const StringName &p_name, Variant &r_ret) const {
  148. Variant arr = get_array();
  149. //int size = arr.call("size");
  150. String pn = p_name;
  151. if (pn.begins_with("array/")) {
  152. if (pn == "array/size") {
  153. r_ret = arr.call("size");
  154. return true;
  155. }
  156. if (pn == "array/page") {
  157. r_ret = page;
  158. return true;
  159. }
  160. } else if (pn.begins_with("indices")) {
  161. if (pn.find("_") != -1) {
  162. //type
  163. int idx = pn.get_slicec('/', 1).get_slicec('_', 0).to_int();
  164. bool valid;
  165. r_ret = arr.get(idx, &valid);
  166. if (valid) {
  167. r_ret = r_ret.get_type();
  168. }
  169. return valid;
  170. } else {
  171. int idx = pn.get_slicec('/', 1).to_int();
  172. bool valid;
  173. r_ret = arr.get(idx, &valid);
  174. if (r_ret.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(r_ret)) {
  175. r_ret = Object::cast_to<EncodedObjectAsID>(r_ret)->get_object_id();
  176. }
  177. return valid;
  178. }
  179. }
  180. return false;
  181. }
  182. void ArrayPropertyEdit::_get_property_list(List<PropertyInfo> *p_list) const {
  183. Variant arr = get_array();
  184. int size = arr.call("size");
  185. p_list->push_back(PropertyInfo(Variant::INT, "array/size", PROPERTY_HINT_RANGE, "0,100000,1"));
  186. int pages = size / ITEMS_PER_PAGE;
  187. if (pages > 0) {
  188. p_list->push_back(PropertyInfo(Variant::INT, "array/page", PROPERTY_HINT_RANGE, "0," + itos(pages) + ",1"));
  189. }
  190. int offset = page * ITEMS_PER_PAGE;
  191. int items = MIN(size - offset, ITEMS_PER_PAGE);
  192. for (int i = 0; i < items; i++) {
  193. Variant v = arr.get(i + offset);
  194. bool is_typed = arr.get_type() != Variant::ARRAY || subtype != Variant::NIL;
  195. if (!is_typed) {
  196. p_list->push_back(PropertyInfo(Variant::INT, "indices/" + itos(i + offset) + "_type", PROPERTY_HINT_ENUM, vtypes));
  197. }
  198. if (v.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(v)) {
  199. p_list->push_back(PropertyInfo(Variant::INT, "indices/" + itos(i + offset), PROPERTY_HINT_OBJECT_ID, "Object"));
  200. continue;
  201. }
  202. if (is_typed || v.get_type() != Variant::NIL) {
  203. PropertyInfo pi(v.get_type(), "indices/" + itos(i + offset));
  204. if (subtype != Variant::NIL) {
  205. pi.type = Variant::Type(subtype);
  206. pi.hint = PropertyHint(subtype_hint);
  207. pi.hint_string = subtype_hint_string;
  208. } else if (v.get_type() == Variant::OBJECT) {
  209. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  210. pi.hint_string = "Resource";
  211. }
  212. p_list->push_back(pi);
  213. }
  214. }
  215. }
  216. void ArrayPropertyEdit::edit(Object *p_obj, const StringName &p_prop, const String &p_hint_string, Variant::Type p_deftype) {
  217. page = 0;
  218. property = p_prop;
  219. obj = p_obj->get_instance_id();
  220. default_type = p_deftype;
  221. if (!p_hint_string.empty()) {
  222. int hint_subtype_separator = p_hint_string.find(":");
  223. if (hint_subtype_separator >= 0) {
  224. String subtype_string = p_hint_string.substr(0, hint_subtype_separator);
  225. int slash_pos = subtype_string.find("/");
  226. if (slash_pos >= 0) {
  227. subtype_hint = PropertyHint(subtype_string.substr(slash_pos + 1, subtype_string.size() - slash_pos - 1).to_int());
  228. subtype_string = subtype_string.substr(0, slash_pos);
  229. }
  230. subtype_hint_string = p_hint_string.substr(hint_subtype_separator + 1, p_hint_string.size() - hint_subtype_separator - 1);
  231. subtype = Variant::Type(subtype_string.to_int());
  232. }
  233. }
  234. }
  235. Node *ArrayPropertyEdit::get_node() {
  236. return Object::cast_to<Node>(ObjectDB::get_instance(obj));
  237. }
  238. bool ArrayPropertyEdit::_dont_undo_redo() {
  239. return true;
  240. }
  241. void ArrayPropertyEdit::_bind_methods() {
  242. ClassDB::bind_method(D_METHOD("_set_size"), &ArrayPropertyEdit::_set_size);
  243. ClassDB::bind_method(D_METHOD("_set_value"), &ArrayPropertyEdit::_set_value);
  244. ClassDB::bind_method(D_METHOD("_notif_change"), &ArrayPropertyEdit::_notif_change);
  245. ClassDB::bind_method(D_METHOD("_notif_changev"), &ArrayPropertyEdit::_notif_changev);
  246. ClassDB::bind_method(D_METHOD("_dont_undo_redo"), &ArrayPropertyEdit::_dont_undo_redo);
  247. }
  248. ArrayPropertyEdit::ArrayPropertyEdit() {
  249. page = 0;
  250. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  251. if (i > 0) {
  252. vtypes += ",";
  253. }
  254. vtypes += Variant::get_type_name(Variant::Type(i));
  255. }
  256. default_type = Variant::NIL;
  257. subtype = Variant::NIL;
  258. subtype_hint = PROPERTY_HINT_NONE;
  259. subtype_hint_string = "";
  260. }