array_property_edit.cpp 9.5 KB

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