2
0

array_property_edit.cpp 9.5 KB

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