editor_properties_array_dict.cpp 28 KB

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