editor_properties_array_dict.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*************************************************************************/
  2. /* editor_properties_array_dict.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 "editor_properties_array_dict.h"
  31. #include "core/io/marshalls.h"
  32. #include "editor/editor_scale.h"
  33. #include "editor_properties.h"
  34. bool EditorPropertyArrayObject::_set(const StringName &p_name, const Variant &p_value) {
  35. String pn = p_name;
  36. if (pn.begins_with("indices")) {
  37. int idx = pn.get_slicec('/', 1).to_int();
  38. array.set(idx, p_value);
  39. return true;
  40. }
  41. return false;
  42. }
  43. bool EditorPropertyArrayObject::_get(const StringName &p_name, Variant &r_ret) const {
  44. String pn = p_name;
  45. if (pn.begins_with("indices")) {
  46. int idx = pn.get_slicec('/', 1).to_int();
  47. bool valid;
  48. r_ret = array.get(idx, &valid);
  49. if (r_ret.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(r_ret)) {
  50. r_ret = Object::cast_to<EncodedObjectAsID>(r_ret)->get_object_id();
  51. }
  52. return valid;
  53. }
  54. return false;
  55. }
  56. void EditorPropertyArrayObject::set_array(const Variant &p_array) {
  57. array = p_array;
  58. }
  59. Variant EditorPropertyArrayObject::get_array() {
  60. return array;
  61. }
  62. EditorPropertyArrayObject::EditorPropertyArrayObject() {
  63. }
  64. ///////////////////
  65. bool EditorPropertyDictionaryObject::_set(const StringName &p_name, const Variant &p_value) {
  66. String pn = p_name;
  67. if (pn == "new_item_key") {
  68. new_item_key = p_value;
  69. return true;
  70. }
  71. if (pn == "new_item_value") {
  72. new_item_value = p_value;
  73. return true;
  74. }
  75. if (pn.begins_with("indices")) {
  76. int idx = pn.get_slicec('/', 1).to_int();
  77. Variant key = dict.get_key_at_index(idx);
  78. dict[key] = p_value;
  79. return true;
  80. }
  81. return false;
  82. }
  83. bool EditorPropertyDictionaryObject::_get(const StringName &p_name, Variant &r_ret) const {
  84. String pn = p_name;
  85. if (pn == "new_item_key") {
  86. r_ret = new_item_key;
  87. return true;
  88. }
  89. if (pn == "new_item_value") {
  90. r_ret = new_item_value;
  91. return true;
  92. }
  93. if (pn.begins_with("indices")) {
  94. int idx = pn.get_slicec('/', 1).to_int();
  95. Variant key = dict.get_key_at_index(idx);
  96. r_ret = dict[key];
  97. if (r_ret.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(r_ret)) {
  98. r_ret = Object::cast_to<EncodedObjectAsID>(r_ret)->get_object_id();
  99. }
  100. return true;
  101. }
  102. return false;
  103. }
  104. void EditorPropertyDictionaryObject::set_dict(const Dictionary &p_dict) {
  105. dict = p_dict;
  106. }
  107. Dictionary EditorPropertyDictionaryObject::get_dict() {
  108. return dict;
  109. }
  110. void EditorPropertyDictionaryObject::set_new_item_key(const Variant &p_new_item) {
  111. new_item_key = p_new_item;
  112. }
  113. Variant EditorPropertyDictionaryObject::get_new_item_key() {
  114. return new_item_key;
  115. }
  116. void EditorPropertyDictionaryObject::set_new_item_value(const Variant &p_new_item) {
  117. new_item_value = p_new_item;
  118. }
  119. Variant EditorPropertyDictionaryObject::get_new_item_value() {
  120. return new_item_value;
  121. }
  122. EditorPropertyDictionaryObject::EditorPropertyDictionaryObject() {
  123. }
  124. ///////////////////// ARRAY ///////////////////////////
  125. void EditorPropertyArray::_property_changed(const String &p_prop, Variant p_value, const String &p_name, bool changing) {
  126. if (p_prop.begins_with("indices")) {
  127. int idx = p_prop.get_slice("/", 1).to_int();
  128. Variant array = object->get_array();
  129. array.set(idx, p_value);
  130. emit_changed(get_edited_property(), array, "", true);
  131. if (array.get_type() == Variant::ARRAY) {
  132. array = array.call("duplicate"); //dupe, so undo/redo works better
  133. }
  134. object->set_array(array);
  135. }
  136. }
  137. void EditorPropertyArray::_change_type(Object *p_button, int p_index) {
  138. Button *button = Object::cast_to<Button>(p_button);
  139. Rect2 rect = button->get_global_rect();
  140. change_type->set_as_minsize();
  141. change_type->set_global_position(rect.position + rect.size - Vector2(change_type->get_combined_minimum_size().x, 0));
  142. change_type->popup();
  143. changing_type_idx = p_index;
  144. }
  145. void EditorPropertyArray::_change_type_menu(int p_index) {
  146. Variant value;
  147. Variant::CallError ce;
  148. value = Variant::construct(Variant::Type(p_index), NULL, 0, ce);
  149. Variant array = object->get_array();
  150. array.set(changing_type_idx, value);
  151. emit_changed(get_edited_property(), array, "", true);
  152. if (array.get_type() == Variant::ARRAY) {
  153. array = array.call("duplicate"); //dupe, so undo/redo works better
  154. }
  155. object->set_array(array);
  156. update_property();
  157. }
  158. void EditorPropertyArray::_object_id_selected(const String &p_property, ObjectID p_id) {
  159. emit_signal("object_id_selected", p_property, p_id);
  160. }
  161. void EditorPropertyArray::update_property() {
  162. Variant array = get_edited_object()->get(get_edited_property());
  163. String arrtype = "";
  164. switch (array_type) {
  165. case Variant::ARRAY: {
  166. arrtype = "Array";
  167. } break;
  168. // arrays
  169. case Variant::POOL_BYTE_ARRAY: {
  170. arrtype = "PoolByteArray";
  171. } break;
  172. case Variant::POOL_INT_ARRAY: {
  173. arrtype = "PoolIntArray";
  174. } break;
  175. case Variant::POOL_REAL_ARRAY: {
  176. arrtype = "PoolFloatArray";
  177. } break;
  178. case Variant::POOL_STRING_ARRAY: {
  179. arrtype = "PoolStringArray";
  180. } break;
  181. case Variant::POOL_VECTOR2_ARRAY: {
  182. arrtype = "PoolVector2Array";
  183. } break;
  184. case Variant::POOL_VECTOR3_ARRAY: {
  185. arrtype = "PoolVector3Array";
  186. } break;
  187. case Variant::POOL_COLOR_ARRAY: {
  188. arrtype = "PoolColorArray";
  189. } break;
  190. default: {}
  191. }
  192. if (array.get_type() == Variant::NIL) {
  193. edit->set_text(String("(Nil) ") + arrtype);
  194. edit->set_pressed(false);
  195. if (vbox) {
  196. memdelete(vbox);
  197. }
  198. return;
  199. }
  200. edit->set_text(arrtype + " (size " + itos(array.call("size")) + ")");
  201. #ifdef TOOLS_ENABLED
  202. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  203. if (edit->is_pressed() != unfolded) {
  204. edit->set_pressed(unfolded);
  205. }
  206. if (unfolded) {
  207. updating = true;
  208. if (!vbox) {
  209. vbox = memnew(VBoxContainer);
  210. add_child(vbox);
  211. set_bottom_editor(vbox);
  212. HBoxContainer *hbc = memnew(HBoxContainer);
  213. vbox->add_child(hbc);
  214. Label *label = memnew(Label(TTR("Size: ")));
  215. label->set_h_size_flags(SIZE_EXPAND_FILL);
  216. hbc->add_child(label);
  217. length = memnew(EditorSpinSlider);
  218. length->set_step(1);
  219. length->set_max(1000000);
  220. length->set_h_size_flags(SIZE_EXPAND_FILL);
  221. hbc->add_child(length);
  222. length->connect("value_changed", this, "_length_changed");
  223. page_hb = memnew(HBoxContainer);
  224. vbox->add_child(page_hb);
  225. label = memnew(Label(TTR("Page: ")));
  226. label->set_h_size_flags(SIZE_EXPAND_FILL);
  227. page_hb->add_child(label);
  228. page = memnew(EditorSpinSlider);
  229. page->set_step(1);
  230. page_hb->add_child(page);
  231. page->set_h_size_flags(SIZE_EXPAND_FILL);
  232. page->connect("value_changed", this, "_page_changed");
  233. } else {
  234. //bye bye children of the box
  235. while (vbox->get_child_count() > 2) {
  236. memdelete(vbox->get_child(2));
  237. }
  238. }
  239. int len = array.call("size");
  240. length->set_value(len);
  241. int pages = MAX(0, len - 1) / page_len + 1;
  242. page->set_max(pages);
  243. page_idx = MIN(page_idx, pages - 1);
  244. page->set_value(page_idx);
  245. page_hb->set_visible(pages > 1);
  246. int offset = page_idx * page_len;
  247. int amount = MIN(len - offset, page_len);
  248. if (array.get_type() == Variant::ARRAY) {
  249. array = array.call("duplicate");
  250. }
  251. object->set_array(array);
  252. for (int i = 0; i < amount; i++) {
  253. String prop_name = "indices/" + itos(i + offset);
  254. EditorProperty *prop = NULL;
  255. Variant value = array.get(i + offset);
  256. Variant::Type value_type = value.get_type();
  257. if (value_type == Variant::NIL && subtype != Variant::NIL) {
  258. value_type = subtype;
  259. }
  260. if (value_type == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(value)) {
  261. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  262. editor->setup("Object");
  263. prop = editor;
  264. } else {
  265. prop = EditorInspector::instantiate_property_editor(NULL, value_type, "", subtype_hint, subtype_hint_string, 0);
  266. }
  267. prop->set_object_and_property(object.ptr(), prop_name);
  268. prop->set_label(itos(i + offset));
  269. prop->set_selectable(false);
  270. prop->connect("property_changed", this, "_property_changed");
  271. prop->connect("object_id_selected", this, "_object_id_selected");
  272. if (array.get_type() == Variant::ARRAY) {
  273. HBoxContainer *hb = memnew(HBoxContainer);
  274. vbox->add_child(hb);
  275. hb->add_child(prop);
  276. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  277. if (subtype == Variant::NIL) {
  278. Button *edit = memnew(Button);
  279. edit->set_icon(get_icon("Edit", "EditorIcons"));
  280. hb->add_child(edit);
  281. edit->connect("pressed", this, "_change_type", varray(edit, i + offset));
  282. }
  283. } else {
  284. vbox->add_child(prop);
  285. }
  286. prop->update_property();
  287. }
  288. updating = false;
  289. } else {
  290. if (vbox) {
  291. set_bottom_editor(NULL);
  292. memdelete(vbox);
  293. vbox = NULL;
  294. }
  295. }
  296. #endif
  297. }
  298. void EditorPropertyArray::_notification(int p_what) {
  299. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  300. }
  301. }
  302. void EditorPropertyArray::_edit_pressed() {
  303. Variant array = get_edited_object()->get(get_edited_property());
  304. if (!array.is_array()) {
  305. Variant::CallError ce;
  306. array = Variant::construct(array_type, NULL, 0, ce);
  307. get_edited_object()->set(get_edited_property(), array);
  308. }
  309. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  310. update_property();
  311. }
  312. void EditorPropertyArray::_page_changed(double p_page) {
  313. if (updating)
  314. return;
  315. page_idx = p_page;
  316. update_property();
  317. }
  318. void EditorPropertyArray::_length_changed(double p_page) {
  319. if (updating)
  320. return;
  321. Variant array = object->get_array();
  322. int previous_size = array.call("size");
  323. array.call("resize", int(p_page));
  324. if (array.get_type() == Variant::ARRAY) {
  325. if (subtype != Variant::NIL) {
  326. int size = array.call("size");
  327. for (int i = previous_size; i < size; i++) {
  328. if (array.get(i).get_type() == Variant::NIL) {
  329. Variant::CallError ce;
  330. array.set(i, Variant::construct(subtype, NULL, 0, ce));
  331. }
  332. }
  333. }
  334. array = array.call("duplicate"); //dupe, so undo/redo works better
  335. } else {
  336. int size = array.call("size");
  337. // Pool*Array don't initialize their elements, have to do it manually
  338. for (int i = previous_size; i < size; i++) {
  339. Variant::CallError ce;
  340. array.set(i, Variant::construct(array.get(i).get_type(), NULL, 0, ce));
  341. }
  342. }
  343. emit_changed(get_edited_property(), array, "", false);
  344. object->set_array(array);
  345. update_property();
  346. }
  347. void EditorPropertyArray::setup(Variant::Type p_array_type, const String &p_hint_string) {
  348. array_type = p_array_type;
  349. if (array_type == Variant::ARRAY && !p_hint_string.empty()) {
  350. int hint_subtype_seperator = p_hint_string.find(":");
  351. if (hint_subtype_seperator >= 0) {
  352. String subtype_string = p_hint_string.substr(0, hint_subtype_seperator);
  353. int slash_pos = subtype_string.find("/");
  354. if (slash_pos >= 0) {
  355. subtype_hint = PropertyHint(subtype_string.substr(slash_pos + 1, subtype_string.size() - slash_pos - 1).to_int());
  356. subtype_string = subtype_string.substr(0, slash_pos);
  357. }
  358. subtype_hint_string = p_hint_string.substr(hint_subtype_seperator + 1, p_hint_string.size() - hint_subtype_seperator - 1);
  359. subtype = Variant::Type(subtype_string.to_int());
  360. }
  361. }
  362. }
  363. void EditorPropertyArray::_bind_methods() {
  364. ClassDB::bind_method("_edit_pressed", &EditorPropertyArray::_edit_pressed);
  365. ClassDB::bind_method("_page_changed", &EditorPropertyArray::_page_changed);
  366. ClassDB::bind_method("_length_changed", &EditorPropertyArray::_length_changed);
  367. ClassDB::bind_method("_property_changed", &EditorPropertyArray::_property_changed, DEFVAL(String()), DEFVAL(false));
  368. ClassDB::bind_method("_change_type", &EditorPropertyArray::_change_type);
  369. ClassDB::bind_method("_change_type_menu", &EditorPropertyArray::_change_type_menu);
  370. ClassDB::bind_method("_object_id_selected", &EditorPropertyArray::_object_id_selected);
  371. }
  372. EditorPropertyArray::EditorPropertyArray() {
  373. object.instance();
  374. page_idx = 0;
  375. page_len = 10;
  376. edit = memnew(Button);
  377. edit->set_flat(true);
  378. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  379. edit->set_clip_text(true);
  380. edit->connect("pressed", this, "_edit_pressed");
  381. edit->set_toggle_mode(true);
  382. add_child(edit);
  383. add_focusable(edit);
  384. vbox = NULL;
  385. page = NULL;
  386. length = NULL;
  387. updating = false;
  388. change_type = memnew(PopupMenu);
  389. add_child(change_type);
  390. change_type->connect("id_pressed", this, "_change_type_menu");
  391. changing_type_idx = -1;
  392. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  393. String type = Variant::get_type_name(Variant::Type(i));
  394. change_type->add_item(type, i);
  395. }
  396. changing_type_idx = -1;
  397. subtype = Variant::NIL;
  398. subtype_hint = PROPERTY_HINT_NONE;
  399. subtype_hint_string = "";
  400. }
  401. ///////////////////// DICTIONARY ///////////////////////////
  402. void EditorPropertyDictionary::_property_changed(const String &p_prop, Variant p_value, const String &p_name, bool changing) {
  403. if (p_prop == "new_item_key") {
  404. object->set_new_item_key(p_value);
  405. } else if (p_prop == "new_item_value") {
  406. object->set_new_item_value(p_value);
  407. } else if (p_prop.begins_with("indices")) {
  408. int idx = p_prop.get_slice("/", 1).to_int();
  409. Dictionary dict = object->get_dict();
  410. Variant key = dict.get_key_at_index(idx);
  411. dict[key] = p_value;
  412. emit_changed(get_edited_property(), dict, "", true);
  413. dict = dict.duplicate(); //dupe, so undo/redo works better
  414. object->set_dict(dict);
  415. }
  416. }
  417. void EditorPropertyDictionary::_change_type(Object *p_button, int p_index) {
  418. Button *button = Object::cast_to<Button>(p_button);
  419. Rect2 rect = button->get_global_rect();
  420. change_type->set_as_minsize();
  421. change_type->set_global_position(rect.position + rect.size - Vector2(change_type->get_combined_minimum_size().x, 0));
  422. change_type->popup();
  423. changing_type_idx = p_index;
  424. }
  425. void EditorPropertyDictionary::_add_key_value() {
  426. // Do not allow nil as valid key. I experienced errors with this
  427. if (object->get_new_item_key().get_type() == Variant::NIL) {
  428. return;
  429. }
  430. Dictionary dict = object->get_dict();
  431. dict[object->get_new_item_key()] = object->get_new_item_value();
  432. object->set_new_item_key(Variant());
  433. object->set_new_item_value(Variant());
  434. emit_changed(get_edited_property(), dict, "", false);
  435. dict = dict.duplicate(); //dupe, so undo/redo works better
  436. object->set_dict(dict);
  437. update_property();
  438. }
  439. void EditorPropertyDictionary::_change_type_menu(int p_index) {
  440. if (changing_type_idx < 0) {
  441. Variant value;
  442. Variant::CallError ce;
  443. value = Variant::construct(Variant::Type(p_index), NULL, 0, ce);
  444. if (changing_type_idx == -1) {
  445. object->set_new_item_key(value);
  446. } else {
  447. object->set_new_item_value(value);
  448. }
  449. update_property();
  450. return;
  451. }
  452. Dictionary dict = object->get_dict();
  453. if (p_index < Variant::VARIANT_MAX) {
  454. Variant value;
  455. Variant::CallError ce;
  456. value = Variant::construct(Variant::Type(p_index), NULL, 0, ce);
  457. Variant key = dict.get_key_at_index(changing_type_idx);
  458. dict[key] = value;
  459. } else {
  460. Variant key = dict.get_key_at_index(changing_type_idx);
  461. dict.erase(key);
  462. }
  463. emit_changed(get_edited_property(), dict, "", false);
  464. dict = dict.duplicate(); //dupe, so undo/redo works better
  465. object->set_dict(dict);
  466. update_property();
  467. }
  468. void EditorPropertyDictionary::update_property() {
  469. Variant updated_val = get_edited_object()->get(get_edited_property());
  470. if (updated_val.get_type() == Variant::NIL) {
  471. edit->set_text("Dictionary (Nil)"); //This provides symmetry with the array property.
  472. edit->set_pressed(false);
  473. if (vbox) {
  474. memdelete(vbox);
  475. }
  476. return;
  477. }
  478. Dictionary dict = updated_val;
  479. edit->set_text("Dictionary (size " + itos(dict.size()) + ")");
  480. #ifdef TOOLS_ENABLED
  481. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  482. if (edit->is_pressed() != unfolded) {
  483. edit->set_pressed(unfolded);
  484. }
  485. if (unfolded) {
  486. updating = true;
  487. if (!vbox) {
  488. vbox = memnew(VBoxContainer);
  489. add_child(vbox);
  490. set_bottom_editor(vbox);
  491. page_hb = memnew(HBoxContainer);
  492. vbox->add_child(page_hb);
  493. Label *label = memnew(Label(TTR("Page: ")));
  494. label->set_h_size_flags(SIZE_EXPAND_FILL);
  495. page_hb->add_child(label);
  496. page = memnew(EditorSpinSlider);
  497. page->set_step(1);
  498. page_hb->add_child(page);
  499. page->set_h_size_flags(SIZE_EXPAND_FILL);
  500. page->connect("value_changed", this, "_page_changed");
  501. } else {
  502. // Queue childs for deletion, delete immediately might cause errors.
  503. for (int i = 1; i < vbox->get_child_count(); i++) {
  504. vbox->get_child(i)->queue_delete();
  505. }
  506. }
  507. int len = dict.size();
  508. int pages = MAX(0, len - 1) / page_len + 1;
  509. page->set_max(pages);
  510. page_idx = MIN(page_idx, pages - 1);
  511. page->set_value(page_idx);
  512. page_hb->set_visible(pages > 1);
  513. int offset = page_idx * page_len;
  514. int amount = MIN(len - offset, page_len);
  515. dict = dict.duplicate();
  516. object->set_dict(dict);
  517. VBoxContainer *add_vbox = NULL;
  518. for (int i = 0; i < amount + 2; i++) {
  519. String prop_name;
  520. Variant key;
  521. Variant value;
  522. if (i < amount) {
  523. prop_name = "indices/" + itos(i + offset);
  524. key = dict.get_key_at_index(i + offset);
  525. value = dict.get_value_at_index(i + offset);
  526. } else if (i == amount) {
  527. prop_name = "new_item_key";
  528. value = object->get_new_item_key();
  529. } else if (i == amount + 1) {
  530. prop_name = "new_item_value";
  531. value = object->get_new_item_value();
  532. }
  533. EditorProperty *prop = NULL;
  534. switch (value.get_type()) {
  535. case Variant::NIL: {
  536. prop = memnew(EditorPropertyNil);
  537. } break;
  538. // atomic types
  539. case Variant::BOOL: {
  540. prop = memnew(EditorPropertyCheck);
  541. } break;
  542. case Variant::INT: {
  543. EditorPropertyInteger *editor = memnew(EditorPropertyInteger);
  544. editor->setup(-100000, 100000, 1, true, true);
  545. prop = editor;
  546. } break;
  547. case Variant::REAL: {
  548. EditorPropertyFloat *editor = memnew(EditorPropertyFloat);
  549. editor->setup(-100000, 100000, 0.001, true, false, true, true);
  550. prop = editor;
  551. } break;
  552. case Variant::STRING: {
  553. prop = memnew(EditorPropertyText);
  554. } break;
  555. // math types
  556. case Variant::VECTOR2: {
  557. EditorPropertyVector2 *editor = memnew(EditorPropertyVector2);
  558. editor->setup(-100000, 100000, 0.001, true);
  559. prop = editor;
  560. } break;
  561. case Variant::RECT2: {
  562. EditorPropertyRect2 *editor = memnew(EditorPropertyRect2);
  563. editor->setup(-100000, 100000, 0.001, true);
  564. prop = editor;
  565. } break;
  566. case Variant::VECTOR3: {
  567. EditorPropertyVector3 *editor = memnew(EditorPropertyVector3);
  568. editor->setup(-100000, 100000, 0.001, true);
  569. prop = editor;
  570. } break;
  571. case Variant::TRANSFORM2D: {
  572. EditorPropertyTransform2D *editor = memnew(EditorPropertyTransform2D);
  573. editor->setup(-100000, 100000, 0.001, true);
  574. prop = editor;
  575. } break;
  576. case Variant::PLANE: {
  577. EditorPropertyPlane *editor = memnew(EditorPropertyPlane);
  578. editor->setup(-100000, 100000, 0.001, true);
  579. prop = editor;
  580. } break;
  581. case Variant::QUAT: {
  582. EditorPropertyQuat *editor = memnew(EditorPropertyQuat);
  583. editor->setup(-100000, 100000, 0.001, true);
  584. prop = editor;
  585. } break;
  586. case Variant::AABB: {
  587. EditorPropertyAABB *editor = memnew(EditorPropertyAABB);
  588. editor->setup(-100000, 100000, 0.001, true);
  589. prop = editor;
  590. } break;
  591. case Variant::BASIS: {
  592. EditorPropertyBasis *editor = memnew(EditorPropertyBasis);
  593. editor->setup(-100000, 100000, 0.001, true);
  594. prop = editor;
  595. } break;
  596. case Variant::TRANSFORM: {
  597. EditorPropertyTransform *editor = memnew(EditorPropertyTransform);
  598. editor->setup(-100000, 100000, 0.001, true);
  599. prop = editor;
  600. } break;
  601. // misc types
  602. case Variant::COLOR: {
  603. prop = memnew(EditorPropertyColor);
  604. } break;
  605. case Variant::NODE_PATH: {
  606. prop = memnew(EditorPropertyNodePath);
  607. } break;
  608. case Variant::_RID: {
  609. prop = memnew(EditorPropertyNil);
  610. } break;
  611. case Variant::OBJECT: {
  612. if (Object::cast_to<EncodedObjectAsID>(value)) {
  613. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  614. editor->setup("Object");
  615. prop = editor;
  616. } else {
  617. EditorPropertyResource *editor = memnew(EditorPropertyResource);
  618. editor->setup("Resource");
  619. prop = editor;
  620. }
  621. } break;
  622. case Variant::DICTIONARY: {
  623. prop = memnew(EditorPropertyDictionary);
  624. } break;
  625. case Variant::ARRAY: {
  626. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  627. editor->setup(Variant::ARRAY);
  628. prop = editor;
  629. } break;
  630. // arrays
  631. case Variant::POOL_BYTE_ARRAY: {
  632. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  633. editor->setup(Variant::POOL_BYTE_ARRAY);
  634. prop = editor;
  635. } break;
  636. case Variant::POOL_INT_ARRAY: {
  637. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  638. editor->setup(Variant::POOL_INT_ARRAY);
  639. prop = editor;
  640. } break;
  641. case Variant::POOL_REAL_ARRAY: {
  642. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  643. editor->setup(Variant::POOL_REAL_ARRAY);
  644. prop = editor;
  645. } break;
  646. case Variant::POOL_STRING_ARRAY: {
  647. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  648. editor->setup(Variant::POOL_STRING_ARRAY);
  649. prop = editor;
  650. } break;
  651. case Variant::POOL_VECTOR2_ARRAY: {
  652. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  653. editor->setup(Variant::POOL_VECTOR2_ARRAY);
  654. prop = editor;
  655. } break;
  656. case Variant::POOL_VECTOR3_ARRAY: {
  657. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  658. editor->setup(Variant::POOL_VECTOR3_ARRAY);
  659. prop = editor;
  660. } break;
  661. case Variant::POOL_COLOR_ARRAY: {
  662. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  663. editor->setup(Variant::POOL_COLOR_ARRAY);
  664. prop = editor;
  665. } break;
  666. default: {}
  667. }
  668. if (i == amount) {
  669. PanelContainer *pc = memnew(PanelContainer);
  670. vbox->add_child(pc);
  671. Ref<StyleBoxFlat> flat;
  672. flat.instance();
  673. for (int j = 0; j < 4; j++) {
  674. flat->set_default_margin(Margin(j), 2 * EDSCALE);
  675. }
  676. flat->set_bg_color(get_color("prop_subsection", "Editor"));
  677. pc->add_style_override("panel", flat);
  678. add_vbox = memnew(VBoxContainer);
  679. pc->add_child(add_vbox);
  680. }
  681. prop->set_object_and_property(object.ptr(), prop_name);
  682. int change_index = 0;
  683. if (i < amount) {
  684. String cs = key.get_construct_string();
  685. prop->set_label(key.get_construct_string());
  686. prop->set_tooltip(cs);
  687. change_index = i + offset;
  688. } else if (i == amount) {
  689. prop->set_label(TTR("New Key:"));
  690. change_index = -1;
  691. } else if (i == amount + 1) {
  692. prop->set_label(TTR("New Value:"));
  693. change_index = -2;
  694. }
  695. prop->set_selectable(false);
  696. prop->connect("property_changed", this, "_property_changed");
  697. prop->connect("object_id_selected", this, "_object_id_selected");
  698. HBoxContainer *hb = memnew(HBoxContainer);
  699. if (add_vbox) {
  700. add_vbox->add_child(hb);
  701. } else {
  702. vbox->add_child(hb);
  703. }
  704. hb->add_child(prop);
  705. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  706. Button *edit = memnew(Button);
  707. edit->set_icon(get_icon("Edit", "EditorIcons"));
  708. hb->add_child(edit);
  709. edit->connect("pressed", this, "_change_type", varray(edit, change_index));
  710. prop->update_property();
  711. if (i == amount + 1) {
  712. Button *butt_add_item = memnew(Button);
  713. butt_add_item->set_text(TTR("Add Key/Value Pair"));
  714. butt_add_item->connect("pressed", this, "_add_key_value");
  715. add_vbox->add_child(butt_add_item);
  716. }
  717. }
  718. updating = false;
  719. } else {
  720. if (vbox) {
  721. set_bottom_editor(NULL);
  722. memdelete(vbox);
  723. vbox = NULL;
  724. }
  725. }
  726. #endif
  727. }
  728. void EditorPropertyDictionary::_object_id_selected(const String &p_property, ObjectID p_id) {
  729. emit_signal("object_id_selected", p_property, p_id);
  730. }
  731. void EditorPropertyDictionary::_notification(int p_what) {
  732. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  733. }
  734. }
  735. void EditorPropertyDictionary::_edit_pressed() {
  736. Variant prop_val = get_edited_object()->get(get_edited_property());
  737. if (prop_val.get_type() == Variant::NIL) {
  738. Variant::CallError ce;
  739. prop_val = Variant::construct(Variant::DICTIONARY, NULL, 0, ce);
  740. get_edited_object()->set(get_edited_property(), prop_val);
  741. }
  742. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  743. update_property();
  744. }
  745. void EditorPropertyDictionary::_page_changed(double p_page) {
  746. if (updating)
  747. return;
  748. page_idx = p_page;
  749. update_property();
  750. }
  751. void EditorPropertyDictionary::_bind_methods() {
  752. ClassDB::bind_method("_edit_pressed", &EditorPropertyDictionary::_edit_pressed);
  753. ClassDB::bind_method("_page_changed", &EditorPropertyDictionary::_page_changed);
  754. ClassDB::bind_method("_property_changed", &EditorPropertyDictionary::_property_changed, DEFVAL(String()), DEFVAL(false));
  755. ClassDB::bind_method("_change_type", &EditorPropertyDictionary::_change_type);
  756. ClassDB::bind_method("_change_type_menu", &EditorPropertyDictionary::_change_type_menu);
  757. ClassDB::bind_method("_add_key_value", &EditorPropertyDictionary::_add_key_value);
  758. ClassDB::bind_method("_object_id_selected", &EditorPropertyDictionary::_object_id_selected);
  759. }
  760. EditorPropertyDictionary::EditorPropertyDictionary() {
  761. object.instance();
  762. page_idx = 0;
  763. page_len = 10;
  764. edit = memnew(Button);
  765. edit->set_flat(true);
  766. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  767. edit->set_clip_text(true);
  768. edit->connect("pressed", this, "_edit_pressed");
  769. edit->set_toggle_mode(true);
  770. add_child(edit);
  771. add_focusable(edit);
  772. vbox = NULL;
  773. page = NULL;
  774. updating = false;
  775. change_type = memnew(PopupMenu);
  776. add_child(change_type);
  777. change_type->connect("id_pressed", this, "_change_type_menu");
  778. changing_type_idx = -1;
  779. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  780. String type = Variant::get_type_name(Variant::Type(i));
  781. change_type->add_item(type, i);
  782. }
  783. change_type->add_separator();
  784. change_type->add_item(TTR("Remove Item"), Variant::VARIANT_MAX);
  785. changing_type_idx = -1;
  786. }