array_property_edit.cpp 9.4 KB

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