editor_properties_array_dict.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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_property, Variant p_value, const String &p_name, bool p_changing) {
  126. if (p_property.begins_with("indices")) {
  127. int idx = p_property.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_screen_rect();
  141. change_type->set_as_minsize();
  142. change_type->set_position(rect.position + rect.size - Vector2(change_type->get_contents_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. Callable::CallError ce;
  152. Variant::construct(Variant::Type(p_index), value, nullptr, 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 StringName &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::PACKED_BYTE_ARRAY: {
  174. arrtype = "PackedByteArray";
  175. } break;
  176. case Variant::PACKED_INT32_ARRAY: {
  177. arrtype = "PackedInt32Array";
  178. } break;
  179. case Variant::PACKED_FLOAT32_ARRAY: {
  180. arrtype = "PackedFloat32Array";
  181. } break;
  182. case Variant::PACKED_INT64_ARRAY: {
  183. arrtype = "PackedInt64Array";
  184. } break;
  185. case Variant::PACKED_FLOAT64_ARRAY: {
  186. arrtype = "PackedFloat64Array";
  187. } break;
  188. case Variant::PACKED_STRING_ARRAY: {
  189. arrtype = "PackedStringArray";
  190. } break;
  191. case Variant::PACKED_VECTOR2_ARRAY: {
  192. arrtype = "PackedVector2Array";
  193. } break;
  194. case Variant::PACKED_VECTOR3_ARRAY: {
  195. arrtype = "PackedVector3Array";
  196. } break;
  197. case Variant::PACKED_COLOR_ARRAY: {
  198. arrtype = "PackedColorArray";
  199. } break;
  200. default: {
  201. }
  202. }
  203. if (array.get_type() == Variant::NIL) {
  204. edit->set_text(String("(Nil) ") + arrtype);
  205. edit->set_pressed(false);
  206. if (vbox) {
  207. set_bottom_editor(nullptr);
  208. memdelete(vbox);
  209. vbox = nullptr;
  210. }
  211. return;
  212. }
  213. edit->set_text(arrtype + " (size " + itos(array.call("size")) + ")");
  214. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  215. if (edit->is_pressed() != unfolded) {
  216. edit->set_pressed(unfolded);
  217. }
  218. if (unfolded) {
  219. updating = true;
  220. if (!vbox) {
  221. vbox = memnew(VBoxContainer);
  222. add_child(vbox);
  223. set_bottom_editor(vbox);
  224. HBoxContainer *hbc = memnew(HBoxContainer);
  225. vbox->add_child(hbc);
  226. Label *label = memnew(Label(TTR("Size: ")));
  227. label->set_h_size_flags(SIZE_EXPAND_FILL);
  228. hbc->add_child(label);
  229. length = memnew(EditorSpinSlider);
  230. length->set_step(1);
  231. length->set_max(1000000);
  232. length->set_h_size_flags(SIZE_EXPAND_FILL);
  233. hbc->add_child(length);
  234. length->connect("value_changed", callable_mp(this, &EditorPropertyArray::_length_changed));
  235. page_hb = memnew(HBoxContainer);
  236. vbox->add_child(page_hb);
  237. label = memnew(Label(TTR("Page: ")));
  238. label->set_h_size_flags(SIZE_EXPAND_FILL);
  239. page_hb->add_child(label);
  240. page = memnew(EditorSpinSlider);
  241. page->set_step(1);
  242. page_hb->add_child(page);
  243. page->set_h_size_flags(SIZE_EXPAND_FILL);
  244. page->connect("value_changed", callable_mp(this, &EditorPropertyArray::_page_changed));
  245. } else {
  246. //bye bye children of the box
  247. while (vbox->get_child_count() > 2) {
  248. vbox->get_child(2)->queue_delete(); // button still needed after pressed is called
  249. vbox->remove_child(vbox->get_child(2));
  250. }
  251. }
  252. int len = array.call("size");
  253. length->set_value(len);
  254. int pages = MAX(0, len - 1) / page_len + 1;
  255. page->set_max(pages);
  256. page_idx = MIN(page_idx, pages - 1);
  257. page->set_value(page_idx);
  258. page_hb->set_visible(pages > 1);
  259. int offset = page_idx * page_len;
  260. int amount = MIN(len - offset, page_len);
  261. if (array.get_type() == Variant::ARRAY) {
  262. array = array.call("duplicate");
  263. }
  264. object->set_array(array);
  265. for (int i = 0; i < amount; i++) {
  266. String prop_name = "indices/" + itos(i + offset);
  267. EditorProperty *prop = nullptr;
  268. Variant value = array.get(i + offset);
  269. Variant::Type value_type = value.get_type();
  270. if (value_type == Variant::NIL && subtype != Variant::NIL) {
  271. value_type = subtype;
  272. }
  273. if (value_type == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(value)) {
  274. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  275. editor->setup("Object");
  276. prop = editor;
  277. } else {
  278. prop = EditorInspector::instantiate_property_editor(nullptr, value_type, "", subtype_hint, subtype_hint_string, 0);
  279. }
  280. prop->set_object_and_property(object.ptr(), prop_name);
  281. prop->set_label(itos(i + offset));
  282. prop->set_selectable(false);
  283. prop->connect("property_changed", callable_mp(this, &EditorPropertyArray::_property_changed));
  284. prop->connect("object_id_selected", callable_mp(this, &EditorPropertyArray::_object_id_selected));
  285. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  286. HBoxContainer *hb = memnew(HBoxContainer);
  287. vbox->add_child(hb);
  288. hb->add_child(prop);
  289. bool is_untyped_array = array.get_type() == Variant::ARRAY && subtype == Variant::NIL;
  290. if (is_untyped_array) {
  291. Button *edit = memnew(Button);
  292. edit->set_icon(get_theme_icon("Edit", "EditorIcons"));
  293. hb->add_child(edit);
  294. edit->connect("pressed", callable_mp(this, &EditorPropertyArray::_change_type), varray(edit, i + offset));
  295. } else {
  296. Button *remove = memnew(Button);
  297. remove->set_icon(get_theme_icon("Remove", "EditorIcons"));
  298. remove->connect("pressed", callable_mp(this, &EditorPropertyArray::_remove_pressed), varray(i + offset));
  299. hb->add_child(remove);
  300. }
  301. prop->update_property();
  302. }
  303. updating = false;
  304. } else {
  305. if (vbox) {
  306. set_bottom_editor(nullptr);
  307. memdelete(vbox);
  308. vbox = nullptr;
  309. }
  310. }
  311. }
  312. void EditorPropertyArray::_remove_pressed(int p_index) {
  313. Variant array = object->get_array();
  314. array.call("remove", p_index);
  315. if (array.get_type() == Variant::ARRAY) {
  316. array = array.call("duplicate");
  317. }
  318. emit_changed(get_edited_property(), array, "", false);
  319. object->set_array(array);
  320. update_property();
  321. }
  322. void EditorPropertyArray::_button_draw() {
  323. if (dropping) {
  324. Color color = get_theme_color("accent_color", "Editor");
  325. edit->draw_rect(Rect2(Point2(), edit->get_size()), color, false);
  326. }
  327. }
  328. bool EditorPropertyArray::_is_drop_valid(const Dictionary &p_drag_data) const {
  329. String allowed_type = Variant::get_type_name(subtype);
  330. Dictionary drag_data = p_drag_data;
  331. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  332. Vector<String> files = drag_data["files"];
  333. for (int i = 0; i < files.size(); i++) {
  334. String file = files[i];
  335. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  336. for (int j = 0; j < allowed_type.get_slice_count(","); j++) {
  337. String at = allowed_type.get_slice(",", j).strip_edges();
  338. // Fail if one of the files is not of allowed type
  339. if (!ClassDB::is_parent_class(ftype, at)) {
  340. return false;
  341. }
  342. }
  343. }
  344. // If no files fail, drop is valid
  345. return true;
  346. }
  347. return false;
  348. }
  349. bool EditorPropertyArray::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  350. return _is_drop_valid(p_data);
  351. }
  352. void EditorPropertyArray::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  353. ERR_FAIL_COND(!_is_drop_valid(p_data));
  354. Dictionary drag_data = p_data;
  355. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  356. Vector<String> files = drag_data["files"];
  357. Variant array = object->get_array();
  358. // Handle the case where array is not initialised yet
  359. if (!array.is_array()) {
  360. Callable::CallError ce;
  361. Variant::construct(array_type, array, nullptr, 0, ce);
  362. }
  363. // Loop the file array and add to existing array
  364. for (int i = 0; i < files.size(); i++) {
  365. String file = files[i];
  366. RES res = ResourceLoader::load(file);
  367. if (res.is_valid()) {
  368. array.call("push_back", res);
  369. }
  370. }
  371. if (array.get_type() == Variant::ARRAY) {
  372. array = array.call("duplicate");
  373. }
  374. emit_changed(get_edited_property(), array, "", false);
  375. object->set_array(array);
  376. update_property();
  377. }
  378. }
  379. void EditorPropertyArray::_notification(int p_what) {
  380. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  381. if (is_visible_in_tree()) {
  382. if (_is_drop_valid(get_viewport()->gui_get_drag_data())) {
  383. dropping = true;
  384. edit->update();
  385. }
  386. }
  387. }
  388. if (p_what == NOTIFICATION_DRAG_END) {
  389. if (dropping) {
  390. dropping = false;
  391. edit->update();
  392. }
  393. }
  394. }
  395. void EditorPropertyArray::_edit_pressed() {
  396. Variant array = get_edited_object()->get(get_edited_property());
  397. if (!array.is_array()) {
  398. Callable::CallError ce;
  399. Variant::construct(array_type, array, nullptr, 0, ce);
  400. get_edited_object()->set(get_edited_property(), array);
  401. }
  402. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  403. update_property();
  404. }
  405. void EditorPropertyArray::_page_changed(double p_page) {
  406. if (updating) {
  407. return;
  408. }
  409. page_idx = p_page;
  410. update_property();
  411. }
  412. void EditorPropertyArray::_length_changed(double p_page) {
  413. if (updating) {
  414. return;
  415. }
  416. Variant array = object->get_array();
  417. int previous_size = array.call("size");
  418. array.call("resize", int(p_page));
  419. if (array.get_type() == Variant::ARRAY) {
  420. if (subtype != Variant::NIL) {
  421. int size = array.call("size");
  422. for (int i = previous_size; i < size; i++) {
  423. if (array.get(i).get_type() == Variant::NIL) {
  424. Callable::CallError ce;
  425. Variant r;
  426. Variant::construct(subtype, r, nullptr, 0, ce);
  427. array.set(i, r);
  428. }
  429. }
  430. }
  431. array = array.call("duplicate"); //dupe, so undo/redo works better
  432. } else {
  433. int size = array.call("size");
  434. // Pool*Array don't initialize their elements, have to do it manually
  435. for (int i = previous_size; i < size; i++) {
  436. Callable::CallError ce;
  437. Variant r;
  438. Variant::construct(array.get(i).get_type(), r, nullptr, 0, ce);
  439. array.set(i, r);
  440. }
  441. }
  442. emit_changed(get_edited_property(), array, "", false);
  443. object->set_array(array);
  444. update_property();
  445. }
  446. void EditorPropertyArray::setup(Variant::Type p_array_type, const String &p_hint_string) {
  447. array_type = p_array_type;
  448. if (array_type == Variant::ARRAY && !p_hint_string.is_empty()) {
  449. int hint_subtype_separator = p_hint_string.find(":");
  450. if (hint_subtype_separator >= 0) {
  451. String subtype_string = p_hint_string.substr(0, hint_subtype_separator);
  452. int slash_pos = subtype_string.find("/");
  453. if (slash_pos >= 0) {
  454. subtype_hint = PropertyHint(subtype_string.substr(slash_pos + 1, subtype_string.size() - slash_pos - 1).to_int());
  455. subtype_string = subtype_string.substr(0, slash_pos);
  456. }
  457. subtype_hint_string = p_hint_string.substr(hint_subtype_separator + 1, p_hint_string.size() - hint_subtype_separator - 1);
  458. subtype = Variant::Type(subtype_string.to_int());
  459. }
  460. }
  461. }
  462. void EditorPropertyArray::_bind_methods() {
  463. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &EditorPropertyArray::can_drop_data_fw);
  464. ClassDB::bind_method(D_METHOD("drop_data_fw"), &EditorPropertyArray::drop_data_fw);
  465. }
  466. EditorPropertyArray::EditorPropertyArray() {
  467. object.instance();
  468. page_len = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  469. edit = memnew(Button);
  470. edit->set_flat(true);
  471. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  472. edit->set_clip_text(true);
  473. edit->connect("pressed", callable_mp(this, &EditorPropertyArray::_edit_pressed));
  474. edit->set_toggle_mode(true);
  475. edit->set_drag_forwarding(this);
  476. edit->connect("draw", callable_mp(this, &EditorPropertyArray::_button_draw));
  477. add_child(edit);
  478. add_focusable(edit);
  479. vbox = nullptr;
  480. page = nullptr;
  481. length = nullptr;
  482. updating = false;
  483. change_type = memnew(PopupMenu);
  484. add_child(change_type);
  485. change_type->connect("id_pressed", callable_mp(this, &EditorPropertyArray::_change_type_menu));
  486. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  487. String type = Variant::get_type_name(Variant::Type(i));
  488. change_type->add_item(type, i);
  489. }
  490. change_type->add_separator();
  491. change_type->add_item(TTR("Remove Item"), Variant::VARIANT_MAX);
  492. changing_type_idx = -1;
  493. subtype = Variant::NIL;
  494. subtype_hint = PROPERTY_HINT_NONE;
  495. subtype_hint_string = "";
  496. dropping = false;
  497. }
  498. ///////////////////// DICTIONARY ///////////////////////////
  499. void EditorPropertyDictionary::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
  500. if (p_property == "new_item_key") {
  501. object->set_new_item_key(p_value);
  502. } else if (p_property == "new_item_value") {
  503. object->set_new_item_value(p_value);
  504. } else if (p_property.begins_with("indices")) {
  505. int idx = p_property.get_slice("/", 1).to_int();
  506. Dictionary dict = object->get_dict();
  507. Variant key = dict.get_key_at_index(idx);
  508. dict[key] = p_value;
  509. emit_changed(get_edited_property(), dict, "", true);
  510. dict = dict.duplicate(); //dupe, so undo/redo works better
  511. object->set_dict(dict);
  512. }
  513. }
  514. void EditorPropertyDictionary::_change_type(Object *p_button, int p_index) {
  515. Button *button = Object::cast_to<Button>(p_button);
  516. Rect2 rect = button->get_screen_rect();
  517. change_type->set_as_minsize();
  518. change_type->set_position(rect.position + rect.size - Vector2(change_type->get_contents_minimum_size().x, 0));
  519. change_type->popup();
  520. changing_type_idx = p_index;
  521. }
  522. void EditorPropertyDictionary::_add_key_value() {
  523. // Do not allow nil as valid key. I experienced errors with this
  524. if (object->get_new_item_key().get_type() == Variant::NIL) {
  525. return;
  526. }
  527. Dictionary dict = object->get_dict();
  528. dict[object->get_new_item_key()] = object->get_new_item_value();
  529. object->set_new_item_key(Variant());
  530. object->set_new_item_value(Variant());
  531. emit_changed(get_edited_property(), dict, "", false);
  532. dict = dict.duplicate(); //dupe, so undo/redo works better
  533. object->set_dict(dict);
  534. update_property();
  535. }
  536. void EditorPropertyDictionary::_change_type_menu(int p_index) {
  537. if (changing_type_idx < 0) {
  538. Variant value;
  539. Callable::CallError ce;
  540. Variant::construct(Variant::Type(p_index), value, nullptr, 0, ce);
  541. if (changing_type_idx == -1) {
  542. object->set_new_item_key(value);
  543. } else {
  544. object->set_new_item_value(value);
  545. }
  546. update_property();
  547. return;
  548. }
  549. Dictionary dict = object->get_dict();
  550. if (p_index < Variant::VARIANT_MAX) {
  551. Variant value;
  552. Callable::CallError ce;
  553. Variant::construct(Variant::Type(p_index), value, nullptr, 0, ce);
  554. Variant key = dict.get_key_at_index(changing_type_idx);
  555. dict[key] = value;
  556. } else {
  557. Variant key = dict.get_key_at_index(changing_type_idx);
  558. dict.erase(key);
  559. }
  560. emit_changed(get_edited_property(), dict, "", false);
  561. dict = dict.duplicate(); //dupe, so undo/redo works better
  562. object->set_dict(dict);
  563. update_property();
  564. }
  565. void EditorPropertyDictionary::update_property() {
  566. Variant updated_val = get_edited_object()->get(get_edited_property());
  567. if (updated_val.get_type() == Variant::NIL) {
  568. edit->set_text("Dictionary (Nil)"); //This provides symmetry with the array property.
  569. edit->set_pressed(false);
  570. if (vbox) {
  571. set_bottom_editor(nullptr);
  572. memdelete(vbox);
  573. vbox = nullptr;
  574. }
  575. return;
  576. }
  577. Dictionary dict = updated_val;
  578. edit->set_text("Dictionary (size " + itos(dict.size()) + ")");
  579. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  580. if (edit->is_pressed() != unfolded) {
  581. edit->set_pressed(unfolded);
  582. }
  583. if (unfolded) {
  584. updating = true;
  585. if (!vbox) {
  586. vbox = memnew(VBoxContainer);
  587. add_child(vbox);
  588. set_bottom_editor(vbox);
  589. page_hb = memnew(HBoxContainer);
  590. vbox->add_child(page_hb);
  591. Label *label = memnew(Label(TTR("Page: ")));
  592. label->set_h_size_flags(SIZE_EXPAND_FILL);
  593. page_hb->add_child(label);
  594. page = memnew(EditorSpinSlider);
  595. page->set_step(1);
  596. page_hb->add_child(page);
  597. page->set_h_size_flags(SIZE_EXPAND_FILL);
  598. page->connect("value_changed", callable_mp(this, &EditorPropertyDictionary::_page_changed));
  599. } else {
  600. // Queue children for deletion, deleting immediately might cause errors.
  601. for (int i = 1; i < vbox->get_child_count(); i++) {
  602. vbox->get_child(i)->queue_delete();
  603. }
  604. }
  605. int len = dict.size();
  606. int pages = MAX(0, len - 1) / page_len + 1;
  607. page->set_max(pages);
  608. page_idx = MIN(page_idx, pages - 1);
  609. page->set_value(page_idx);
  610. page_hb->set_visible(pages > 1);
  611. int offset = page_idx * page_len;
  612. int amount = MIN(len - offset, page_len);
  613. dict = dict.duplicate();
  614. object->set_dict(dict);
  615. VBoxContainer *add_vbox = nullptr;
  616. for (int i = 0; i < amount + 2; i++) {
  617. String prop_name;
  618. Variant key;
  619. Variant value;
  620. if (i < amount) {
  621. prop_name = "indices/" + itos(i + offset);
  622. key = dict.get_key_at_index(i + offset);
  623. value = dict.get_value_at_index(i + offset);
  624. } else if (i == amount) {
  625. prop_name = "new_item_key";
  626. value = object->get_new_item_key();
  627. } else if (i == amount + 1) {
  628. prop_name = "new_item_value";
  629. value = object->get_new_item_value();
  630. }
  631. EditorProperty *prop = nullptr;
  632. switch (value.get_type()) {
  633. case Variant::NIL: {
  634. prop = memnew(EditorPropertyNil);
  635. } break;
  636. // atomic types
  637. case Variant::BOOL: {
  638. prop = memnew(EditorPropertyCheck);
  639. } break;
  640. case Variant::INT: {
  641. EditorPropertyInteger *editor = memnew(EditorPropertyInteger);
  642. editor->setup(-100000, 100000, 1, true, true);
  643. prop = editor;
  644. } break;
  645. case Variant::FLOAT: {
  646. EditorPropertyFloat *editor = memnew(EditorPropertyFloat);
  647. editor->setup(-100000, 100000, 0.001, true, false, true, true);
  648. prop = editor;
  649. } break;
  650. case Variant::STRING: {
  651. prop = memnew(EditorPropertyText);
  652. } break;
  653. // math types
  654. case Variant::VECTOR2: {
  655. EditorPropertyVector2 *editor = memnew(EditorPropertyVector2);
  656. editor->setup(-100000, 100000, 0.001, true);
  657. prop = editor;
  658. } break;
  659. case Variant::VECTOR2I: {
  660. EditorPropertyVector2i *editor = memnew(EditorPropertyVector2i);
  661. editor->setup(-100000, 100000, true);
  662. prop = editor;
  663. } break;
  664. case Variant::RECT2: {
  665. EditorPropertyRect2 *editor = memnew(EditorPropertyRect2);
  666. editor->setup(-100000, 100000, 0.001, true);
  667. prop = editor;
  668. } break;
  669. case Variant::RECT2I: {
  670. EditorPropertyRect2i *editor = memnew(EditorPropertyRect2i);
  671. editor->setup(-100000, 100000, true);
  672. prop = editor;
  673. } break;
  674. case Variant::VECTOR3: {
  675. EditorPropertyVector3 *editor = memnew(EditorPropertyVector3);
  676. editor->setup(-100000, 100000, 0.001, true);
  677. prop = editor;
  678. } break;
  679. case Variant::VECTOR3I: {
  680. EditorPropertyVector3i *editor = memnew(EditorPropertyVector3i);
  681. editor->setup(-100000, 100000, true);
  682. prop = editor;
  683. } break;
  684. case Variant::TRANSFORM2D: {
  685. EditorPropertyTransform2D *editor = memnew(EditorPropertyTransform2D);
  686. editor->setup(-100000, 100000, 0.001, true);
  687. prop = editor;
  688. } break;
  689. case Variant::PLANE: {
  690. EditorPropertyPlane *editor = memnew(EditorPropertyPlane);
  691. editor->setup(-100000, 100000, 0.001, true);
  692. prop = editor;
  693. } break;
  694. case Variant::QUAT: {
  695. EditorPropertyQuat *editor = memnew(EditorPropertyQuat);
  696. editor->setup(-100000, 100000, 0.001, true);
  697. prop = editor;
  698. } break;
  699. case Variant::AABB: {
  700. EditorPropertyAABB *editor = memnew(EditorPropertyAABB);
  701. editor->setup(-100000, 100000, 0.001, true);
  702. prop = editor;
  703. } break;
  704. case Variant::BASIS: {
  705. EditorPropertyBasis *editor = memnew(EditorPropertyBasis);
  706. editor->setup(-100000, 100000, 0.001, true);
  707. prop = editor;
  708. } break;
  709. case Variant::TRANSFORM: {
  710. EditorPropertyTransform *editor = memnew(EditorPropertyTransform);
  711. editor->setup(-100000, 100000, 0.001, true);
  712. prop = editor;
  713. } break;
  714. // misc types
  715. case Variant::COLOR: {
  716. prop = memnew(EditorPropertyColor);
  717. } break;
  718. case Variant::STRING_NAME: {
  719. EditorPropertyText *ept = memnew(EditorPropertyText);
  720. ept->set_string_name(true);
  721. prop = ept;
  722. } break;
  723. case Variant::NODE_PATH: {
  724. prop = memnew(EditorPropertyNodePath);
  725. } break;
  726. case Variant::RID: {
  727. prop = memnew(EditorPropertyRID);
  728. } break;
  729. case Variant::OBJECT: {
  730. if (Object::cast_to<EncodedObjectAsID>(value)) {
  731. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  732. editor->setup("Object");
  733. prop = editor;
  734. } else {
  735. EditorPropertyResource *editor = memnew(EditorPropertyResource);
  736. editor->setup("Resource");
  737. prop = editor;
  738. }
  739. } break;
  740. case Variant::DICTIONARY: {
  741. prop = memnew(EditorPropertyDictionary);
  742. } break;
  743. case Variant::ARRAY: {
  744. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  745. editor->setup(Variant::ARRAY);
  746. prop = editor;
  747. } break;
  748. // arrays
  749. case Variant::PACKED_BYTE_ARRAY: {
  750. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  751. editor->setup(Variant::PACKED_BYTE_ARRAY);
  752. prop = editor;
  753. } break;
  754. case Variant::PACKED_INT32_ARRAY: {
  755. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  756. editor->setup(Variant::PACKED_INT32_ARRAY);
  757. prop = editor;
  758. } break;
  759. case Variant::PACKED_FLOAT32_ARRAY: {
  760. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  761. editor->setup(Variant::PACKED_FLOAT32_ARRAY);
  762. prop = editor;
  763. } break;
  764. case Variant::PACKED_INT64_ARRAY: {
  765. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  766. editor->setup(Variant::PACKED_INT64_ARRAY);
  767. prop = editor;
  768. } break;
  769. case Variant::PACKED_FLOAT64_ARRAY: {
  770. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  771. editor->setup(Variant::PACKED_FLOAT64_ARRAY);
  772. prop = editor;
  773. } break;
  774. case Variant::PACKED_STRING_ARRAY: {
  775. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  776. editor->setup(Variant::PACKED_STRING_ARRAY);
  777. prop = editor;
  778. } break;
  779. case Variant::PACKED_VECTOR2_ARRAY: {
  780. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  781. editor->setup(Variant::PACKED_VECTOR2_ARRAY);
  782. prop = editor;
  783. } break;
  784. case Variant::PACKED_VECTOR3_ARRAY: {
  785. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  786. editor->setup(Variant::PACKED_VECTOR3_ARRAY);
  787. prop = editor;
  788. } break;
  789. case Variant::PACKED_COLOR_ARRAY: {
  790. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  791. editor->setup(Variant::PACKED_COLOR_ARRAY);
  792. prop = editor;
  793. } break;
  794. default: {
  795. }
  796. }
  797. if (i == amount) {
  798. PanelContainer *pc = memnew(PanelContainer);
  799. vbox->add_child(pc);
  800. Ref<StyleBoxFlat> flat;
  801. flat.instance();
  802. for (int j = 0; j < 4; j++) {
  803. flat->set_default_margin(Side(j), 2 * EDSCALE);
  804. }
  805. flat->set_bg_color(get_theme_color("prop_subsection", "Editor"));
  806. pc->add_theme_style_override("panel", flat);
  807. add_vbox = memnew(VBoxContainer);
  808. pc->add_child(add_vbox);
  809. }
  810. prop->set_object_and_property(object.ptr(), prop_name);
  811. int change_index = 0;
  812. if (i < amount) {
  813. String cs = key.get_construct_string();
  814. prop->set_label(key.get_construct_string());
  815. prop->set_tooltip(cs);
  816. change_index = i + offset;
  817. } else if (i == amount) {
  818. prop->set_label(TTR("New Key:"));
  819. change_index = -1;
  820. } else if (i == amount + 1) {
  821. prop->set_label(TTR("New Value:"));
  822. change_index = -2;
  823. }
  824. prop->set_selectable(false);
  825. prop->connect("property_changed", callable_mp(this, &EditorPropertyDictionary::_property_changed));
  826. prop->connect("object_id_selected", callable_mp(this, &EditorPropertyDictionary::_object_id_selected));
  827. HBoxContainer *hb = memnew(HBoxContainer);
  828. if (add_vbox) {
  829. add_vbox->add_child(hb);
  830. } else {
  831. vbox->add_child(hb);
  832. }
  833. hb->add_child(prop);
  834. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  835. Button *edit = memnew(Button);
  836. edit->set_icon(get_theme_icon("Edit", "EditorIcons"));
  837. hb->add_child(edit);
  838. edit->connect("pressed", callable_mp(this, &EditorPropertyDictionary::_change_type), varray(edit, change_index));
  839. prop->update_property();
  840. if (i == amount + 1) {
  841. Button *butt_add_item = memnew(Button);
  842. butt_add_item->set_text(TTR("Add Key/Value Pair"));
  843. butt_add_item->connect("pressed", callable_mp(this, &EditorPropertyDictionary::_add_key_value));
  844. add_vbox->add_child(butt_add_item);
  845. }
  846. }
  847. updating = false;
  848. } else {
  849. if (vbox) {
  850. set_bottom_editor(nullptr);
  851. memdelete(vbox);
  852. vbox = nullptr;
  853. }
  854. }
  855. }
  856. void EditorPropertyDictionary::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  857. emit_signal("object_id_selected", p_property, p_id);
  858. }
  859. void EditorPropertyDictionary::_notification(int p_what) {
  860. }
  861. void EditorPropertyDictionary::_edit_pressed() {
  862. Variant prop_val = get_edited_object()->get(get_edited_property());
  863. if (prop_val.get_type() == Variant::NIL) {
  864. Callable::CallError ce;
  865. Variant::construct(Variant::DICTIONARY, prop_val, nullptr, 0, ce);
  866. get_edited_object()->set(get_edited_property(), prop_val);
  867. }
  868. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  869. update_property();
  870. }
  871. void EditorPropertyDictionary::_page_changed(double p_page) {
  872. if (updating) {
  873. return;
  874. }
  875. page_idx = p_page;
  876. update_property();
  877. }
  878. void EditorPropertyDictionary::_bind_methods() {
  879. }
  880. EditorPropertyDictionary::EditorPropertyDictionary() {
  881. object.instance();
  882. page_len = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  883. edit = memnew(Button);
  884. edit->set_flat(true);
  885. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  886. edit->set_clip_text(true);
  887. edit->connect("pressed", callable_mp(this, &EditorPropertyDictionary::_edit_pressed));
  888. edit->set_toggle_mode(true);
  889. add_child(edit);
  890. add_focusable(edit);
  891. vbox = nullptr;
  892. page = nullptr;
  893. updating = false;
  894. change_type = memnew(PopupMenu);
  895. add_child(change_type);
  896. change_type->connect("id_pressed", callable_mp(this, &EditorPropertyDictionary::_change_type_menu));
  897. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  898. String type = Variant::get_type_name(Variant::Type(i));
  899. change_type->add_item(type, i);
  900. }
  901. change_type->add_separator();
  902. change_type->add_item(TTR("Remove Item"), Variant::VARIANT_MAX);
  903. changing_type_idx = -1;
  904. }