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