editor_properties_array_dict.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/input/input.h"
  32. #include "core/io/marshalls.h"
  33. #include "editor/editor_file_system.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_properties.h"
  36. #include "editor/editor_properties_vector.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/gui/editor_spin_slider.h"
  40. #include "editor/gui/editor_variant_type_selectors.h"
  41. #include "editor/inspector_dock.h"
  42. #include "editor/themes/editor_scale.h"
  43. #include "scene/gui/button.h"
  44. #include "scene/gui/margin_container.h"
  45. bool EditorPropertyArrayObject::_set(const StringName &p_name, const Variant &p_value) {
  46. String name = p_name;
  47. if (!name.begins_with("indices")) {
  48. return false;
  49. }
  50. int index;
  51. if (name.begins_with("metadata/")) {
  52. index = name.get_slicec('/', 2).to_int();
  53. } else {
  54. index = name.get_slicec('/', 1).to_int();
  55. }
  56. array.set(index, p_value);
  57. return true;
  58. }
  59. bool EditorPropertyArrayObject::_get(const StringName &p_name, Variant &r_ret) const {
  60. String name = p_name;
  61. if (!name.begins_with("indices")) {
  62. return false;
  63. }
  64. int index;
  65. if (name.begins_with("metadata/")) {
  66. index = name.get_slicec('/', 2).to_int();
  67. } else {
  68. index = name.get_slicec('/', 1).to_int();
  69. }
  70. bool valid;
  71. r_ret = array.get(index, &valid);
  72. if (r_ret.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(r_ret)) {
  73. r_ret = Object::cast_to<EncodedObjectAsID>(r_ret)->get_object_id();
  74. }
  75. return valid;
  76. }
  77. void EditorPropertyArrayObject::set_array(const Variant &p_array) {
  78. array = p_array;
  79. }
  80. Variant EditorPropertyArrayObject::get_array() {
  81. return array;
  82. }
  83. ///////////////////
  84. bool EditorPropertyDictionaryObject::_set(const StringName &p_name, const Variant &p_value) {
  85. String name = p_name;
  86. if (name == "new_item_key") {
  87. new_item_key = p_value;
  88. return true;
  89. }
  90. if (name == "new_item_value") {
  91. new_item_value = p_value;
  92. return true;
  93. }
  94. if (name.begins_with("indices")) {
  95. dict = dict.duplicate();
  96. int index = name.get_slicec('/', 1).to_int();
  97. Variant key = dict.get_key_at_index(index);
  98. dict[key] = p_value;
  99. return true;
  100. }
  101. return false;
  102. }
  103. bool EditorPropertyDictionaryObject::_get(const StringName &p_name, Variant &r_ret) const {
  104. if (!get_by_property_name(p_name, r_ret)) {
  105. return false;
  106. }
  107. if (r_ret.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(r_ret)) {
  108. r_ret = Object::cast_to<EncodedObjectAsID>(r_ret)->get_object_id();
  109. }
  110. return true;
  111. }
  112. bool EditorPropertyDictionaryObject::get_by_property_name(const String &p_name, Variant &r_ret) const {
  113. String name = p_name;
  114. if (name == "new_item_key") {
  115. r_ret = new_item_key;
  116. return true;
  117. }
  118. if (name == "new_item_value") {
  119. r_ret = new_item_value;
  120. return true;
  121. }
  122. if (name == "new_item_key_name") {
  123. r_ret = TTR("New Key:");
  124. return true;
  125. }
  126. if (name == "new_item_value_name") {
  127. r_ret = TTR("New Value:");
  128. return true;
  129. }
  130. if (name.begins_with("indices")) {
  131. int index = name.get_slicec('/', 1).to_int();
  132. Variant key = dict.get_key_at_index(index);
  133. r_ret = dict[key];
  134. return true;
  135. }
  136. if (name.begins_with("keys")) {
  137. int index = name.get_slicec('/', 1).to_int();
  138. Variant key = dict.get_key_at_index(index);
  139. r_ret = key;
  140. return true;
  141. }
  142. return false;
  143. }
  144. void EditorPropertyDictionaryObject::set_dict(const Dictionary &p_dict) {
  145. dict = p_dict;
  146. }
  147. Dictionary EditorPropertyDictionaryObject::get_dict() {
  148. return dict;
  149. }
  150. void EditorPropertyDictionaryObject::set_new_item_key(const Variant &p_new_item) {
  151. new_item_key = p_new_item;
  152. }
  153. Variant EditorPropertyDictionaryObject::get_new_item_key() {
  154. return new_item_key;
  155. }
  156. void EditorPropertyDictionaryObject::set_new_item_value(const Variant &p_new_item) {
  157. new_item_value = p_new_item;
  158. }
  159. Variant EditorPropertyDictionaryObject::get_new_item_value() {
  160. return new_item_value;
  161. }
  162. String EditorPropertyDictionaryObject::get_property_name_for_index(int p_index) {
  163. switch (p_index) {
  164. case NEW_KEY_INDEX:
  165. return "new_item_key";
  166. case NEW_VALUE_INDEX:
  167. return "new_item_value";
  168. default:
  169. return "indices/" + itos(p_index);
  170. }
  171. }
  172. String EditorPropertyDictionaryObject::get_key_name_for_index(int p_index) {
  173. switch (p_index) {
  174. case NEW_KEY_INDEX:
  175. return "new_item_key_name";
  176. case NEW_VALUE_INDEX:
  177. return "new_item_value_name";
  178. default:
  179. return "keys/" + itos(p_index);
  180. }
  181. }
  182. String EditorPropertyDictionaryObject::get_label_for_index(int p_index) {
  183. switch (p_index) {
  184. case NEW_KEY_INDEX:
  185. return TTR("New Key:");
  186. break;
  187. case NEW_VALUE_INDEX:
  188. return TTR("New Value:");
  189. break;
  190. default:
  191. return dict.get_key_at_index(p_index).get_construct_string();
  192. break;
  193. }
  194. }
  195. ///////////////////// ARRAY ///////////////////////////
  196. void EditorPropertyArray::initialize_array(Variant &p_array) {
  197. if (array_type == Variant::ARRAY && subtype != Variant::NIL) {
  198. Array array;
  199. StringName subtype_class;
  200. Ref<Script> subtype_script;
  201. if (subtype == Variant::OBJECT && !subtype_hint_string.is_empty()) {
  202. if (ClassDB::class_exists(subtype_hint_string)) {
  203. subtype_class = subtype_hint_string;
  204. }
  205. }
  206. array.set_typed(subtype, subtype_class, subtype_script);
  207. p_array = array;
  208. } else {
  209. VariantInternal::initialize(&p_array, array_type);
  210. }
  211. }
  212. void EditorPropertyArray::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
  213. if (!p_property.begins_with("indices")) {
  214. return;
  215. }
  216. if (p_value.get_type() == Variant::OBJECT && p_value.is_null()) {
  217. p_value = Variant(); // `EditorResourcePicker` resets to `Ref<Resource>()`. See GH-82716.
  218. }
  219. int index = p_property.get_slicec('/', 1).to_int();
  220. Variant array = object->get_array().duplicate();
  221. array.set(index, p_value);
  222. emit_changed(get_edited_property(), array, p_name, p_changing);
  223. if (p_changing) {
  224. object->set_array(array);
  225. }
  226. }
  227. void EditorPropertyArray::_change_type(Object *p_button, int p_slot_index) {
  228. Button *button = Object::cast_to<Button>(p_button);
  229. changing_type_index = p_slot_index;
  230. Rect2 rect = button->get_screen_rect();
  231. change_type->reset_size();
  232. change_type->set_position(rect.get_end() - Vector2(change_type->get_contents_minimum_size().x, 0));
  233. change_type->popup();
  234. }
  235. void EditorPropertyArray::_change_type_menu(int p_index) {
  236. if (p_index == Variant::VARIANT_MAX) {
  237. _remove_pressed(changing_type_index);
  238. return;
  239. }
  240. ERR_FAIL_COND_MSG(
  241. changing_type_index == EditorPropertyArrayObject::NOT_CHANGING_TYPE,
  242. "Tried to change type of an array item, but no item was selected.");
  243. Variant value;
  244. VariantInternal::initialize(&value, Variant::Type(p_index));
  245. Variant array = object->get_array().duplicate();
  246. array.set(slots[changing_type_index].index, value);
  247. emit_changed(get_edited_property(), array);
  248. }
  249. void EditorPropertyArray::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  250. emit_signal(SNAME("object_id_selected"), p_property, p_id);
  251. }
  252. void EditorPropertyArray::_create_new_property_slot() {
  253. int idx = slots.size();
  254. HBoxContainer *hbox = memnew(HBoxContainer);
  255. Button *reorder_button = memnew(Button);
  256. reorder_button->set_accessibility_name(TTRC("Reorder"));
  257. reorder_button->set_button_icon(get_editor_theme_icon(SNAME("TripleBar")));
  258. reorder_button->set_default_cursor_shape(Control::CURSOR_MOVE);
  259. reorder_button->set_disabled(is_read_only());
  260. reorder_button->connect(SceneStringName(gui_input), callable_mp(this, &EditorPropertyArray::_reorder_button_gui_input));
  261. reorder_button->connect(SNAME("button_up"), callable_mp(this, &EditorPropertyArray::_reorder_button_up));
  262. reorder_button->connect(SNAME("button_down"), callable_mp(this, &EditorPropertyArray::_reorder_button_down).bind(idx));
  263. hbox->add_child(reorder_button);
  264. EditorProperty *prop = memnew(EditorPropertyNil);
  265. hbox->add_child(prop);
  266. bool is_untyped_array = object->get_array().get_type() == Variant::ARRAY && subtype == Variant::NIL;
  267. if (is_untyped_array) {
  268. Button *edit_btn = memnew(Button);
  269. edit_btn->set_accessibility_name(TTRC("Edit"));
  270. edit_btn->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  271. edit_btn->set_disabled(is_read_only());
  272. edit_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyArray::_change_type).bind(edit_btn, idx));
  273. hbox->add_child(edit_btn);
  274. } else {
  275. Button *remove_btn = memnew(Button);
  276. remove_btn->set_accessibility_name(TTRC("Remove"));
  277. remove_btn->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  278. remove_btn->set_disabled(is_read_only());
  279. remove_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyArray::_remove_pressed).bind(idx));
  280. hbox->add_child(remove_btn);
  281. }
  282. property_vbox->add_child(hbox);
  283. Slot slot;
  284. slot.prop = prop;
  285. slot.object = object;
  286. slot.container = hbox;
  287. slot.reorder_button = reorder_button;
  288. slot.set_index(idx + page_index * page_length);
  289. slots.push_back(slot);
  290. }
  291. void EditorPropertyArray::set_preview_value(bool p_preview_value) {
  292. preview_value = p_preview_value;
  293. }
  294. void EditorPropertyArray::update_property() {
  295. Variant array = get_edited_property_value();
  296. String array_type_name = Variant::get_type_name(array_type);
  297. String array_sub_type_name;
  298. if (array_type == Variant::ARRAY && subtype != Variant::NIL) {
  299. String type_name;
  300. if (subtype == Variant::OBJECT && (subtype_hint == PROPERTY_HINT_RESOURCE_TYPE || subtype_hint == PROPERTY_HINT_NODE_TYPE)) {
  301. type_name = subtype_hint_string;
  302. } else {
  303. type_name = Variant::get_type_name(subtype);
  304. }
  305. if (preview_value) {
  306. array_sub_type_name = vformat("[%s] ", type_name);
  307. } else {
  308. array_type_name = vformat("%s[%s]", array_type_name, type_name);
  309. }
  310. }
  311. if (!array.is_array()) {
  312. if (preview_value) {
  313. edit->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  314. edit->set_button_icon(get_editor_theme_icon(SNAME("Nil")));
  315. edit->set_text(array_type_name);
  316. } else {
  317. edit->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  318. edit->set_button_icon(Ref<Texture2D>());
  319. edit->set_text(vformat(TTR("(Nil) %s"), array_type_name));
  320. }
  321. edit->set_pressed(false);
  322. if (container) {
  323. set_bottom_editor(nullptr);
  324. memdelete(container);
  325. button_add_item = nullptr;
  326. container = nullptr;
  327. slots.clear();
  328. }
  329. return;
  330. }
  331. object->set_array(array);
  332. int size = array.call("size");
  333. int max_page = MAX(0, size - 1) / page_length;
  334. if (page_index > max_page) {
  335. _page_changed(max_page);
  336. }
  337. if (preview_value) {
  338. String ctr_str = array.get_construct_string().trim_prefix(array_type_name + "(").trim_suffix(")").remove_char('\n');
  339. if (array_type == Variant::ARRAY && subtype != Variant::NIL) {
  340. int type_end = ctr_str.find("](");
  341. if (type_end > 0) {
  342. ctr_str = ctr_str.substr(type_end + 2);
  343. }
  344. }
  345. edit->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  346. edit->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  347. edit->set_button_icon(get_editor_theme_icon(array_type_name));
  348. edit->set_text(vformat("%s%s", array_sub_type_name, ctr_str));
  349. edit->set_tooltip_text(vformat(TTR("%s%s (size %d)"), array_type_name, array_sub_type_name, size));
  350. } else {
  351. edit->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  352. edit->set_button_icon(Ref<Texture2D>());
  353. edit->set_text(vformat(TTR("%s (size %d)"), array_type_name, size));
  354. }
  355. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  356. if (edit->is_pressed() != unfolded) {
  357. edit->set_pressed(unfolded);
  358. }
  359. if (unfolded) {
  360. updating = true;
  361. if (!container) {
  362. container = memnew(PanelContainer);
  363. add_child(container);
  364. set_bottom_editor(container);
  365. VBoxContainer *vbox = memnew(VBoxContainer);
  366. container->add_child(vbox);
  367. HBoxContainer *hbox = memnew(HBoxContainer);
  368. vbox->add_child(hbox);
  369. Label *size_label = memnew(Label(TTR("Size:")));
  370. size_label->set_h_size_flags(SIZE_EXPAND_FILL);
  371. hbox->add_child(size_label);
  372. size_slider = memnew(EditorSpinSlider);
  373. size_slider->set_step(1);
  374. size_slider->set_max(INT32_MAX);
  375. size_slider->set_editing_integer(true);
  376. size_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  377. size_slider->set_read_only(is_read_only());
  378. size_slider->set_accessibility_name(TTRC("Size"));
  379. size_slider->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyArray::_length_changed));
  380. hbox->add_child(size_slider);
  381. property_vbox = memnew(VBoxContainer);
  382. property_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  383. vbox->add_child(property_vbox);
  384. button_add_item = EditorInspector::create_inspector_action_button(TTR("Add Element"));
  385. button_add_item->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  386. button_add_item->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyArray::_add_element));
  387. button_add_item->connect(SceneStringName(draw), callable_mp(this, &EditorPropertyArray::_button_add_item_draw));
  388. SET_DRAG_FORWARDING_CD(button_add_item, EditorPropertyArray);
  389. button_add_item->set_disabled(is_read_only());
  390. button_add_item->set_accessibility_name(TTRC("Add"));
  391. vbox->add_child(button_add_item);
  392. paginator = memnew(EditorPaginator);
  393. paginator->connect("page_changed", callable_mp(this, &EditorPropertyArray::_page_changed));
  394. vbox->add_child(paginator);
  395. for (int i = 0; i < page_length; i++) {
  396. _create_new_property_slot();
  397. }
  398. }
  399. size_slider->set_value(size);
  400. property_vbox->set_visible(size > 0);
  401. button_add_item->set_visible(page_index == max_page);
  402. paginator->update(page_index, max_page);
  403. paginator->set_visible(max_page > 0);
  404. for (Slot &slot : slots) {
  405. bool slot_visible = &slot != &reorder_slot && slot.index < size;
  406. slot.container->set_visible(slot_visible);
  407. // If not visible no need to update it
  408. if (!slot_visible) {
  409. continue;
  410. }
  411. int idx = slot.index;
  412. Variant::Type value_type = subtype;
  413. if (value_type == Variant::NIL) {
  414. value_type = array.get(idx).get_type();
  415. }
  416. // Check if the editor property needs to be updated.
  417. bool value_as_id = Object::cast_to<EncodedObjectAsID>(array.get(idx));
  418. if (value_type != slot.type || (value_type == Variant::OBJECT && (value_as_id != slot.as_id))) {
  419. slot.as_id = value_as_id;
  420. slot.type = value_type;
  421. EditorProperty *new_prop = nullptr;
  422. if (value_type == Variant::OBJECT && value_as_id) {
  423. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  424. editor->setup("Object");
  425. new_prop = editor;
  426. } else {
  427. new_prop = EditorInspector::instantiate_property_editor(this, value_type, "", subtype_hint, subtype_hint_string, PROPERTY_USAGE_NONE);
  428. }
  429. new_prop->set_selectable(false);
  430. new_prop->set_use_folding(is_using_folding());
  431. new_prop->connect(SNAME("property_changed"), callable_mp(this, &EditorPropertyArray::_property_changed));
  432. new_prop->connect(SNAME("object_id_selected"), callable_mp(this, &EditorPropertyArray::_object_id_selected));
  433. new_prop->set_h_size_flags(SIZE_EXPAND_FILL);
  434. new_prop->set_read_only(is_read_only());
  435. slot.prop->add_sibling(new_prop, false);
  436. slot.prop->queue_free();
  437. slot.prop = new_prop;
  438. slot.set_index(idx);
  439. }
  440. if (slot.index == changing_type_index) {
  441. callable_mp(slot.prop, &EditorProperty::grab_focus).call_deferred(0);
  442. changing_type_index = EditorPropertyArrayObject::NOT_CHANGING_TYPE;
  443. }
  444. slot.prop->update_property();
  445. }
  446. updating = false;
  447. } else {
  448. if (container) {
  449. set_bottom_editor(nullptr);
  450. memdelete(container);
  451. button_add_item = nullptr;
  452. container = nullptr;
  453. slots.clear();
  454. }
  455. }
  456. }
  457. void EditorPropertyArray::_remove_pressed(int p_slot_index) {
  458. Variant array = object->get_array().duplicate();
  459. array.call("remove_at", slots[p_slot_index].index);
  460. emit_changed(get_edited_property(), array);
  461. }
  462. void EditorPropertyArray::_button_draw() {
  463. if (dropping) {
  464. Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  465. edit->draw_rect(Rect2(Point2(), edit->get_size()), color, false);
  466. }
  467. }
  468. void EditorPropertyArray::_button_add_item_draw() {
  469. if (dropping) {
  470. Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  471. button_add_item->draw_rect(Rect2(Point2(), button_add_item->get_size()), color, false);
  472. }
  473. }
  474. bool EditorPropertyArray::_is_drop_valid(const Dictionary &p_drag_data) const {
  475. if (is_read_only()) {
  476. return false;
  477. }
  478. String allowed_type = Variant::get_type_name(subtype);
  479. // When the subtype is of type Object, an additional subtype may be specified in the hint string
  480. // (e.g. Resource, Texture2D, ShaderMaterial, etc). We want the allowed type to be that, not just "Object".
  481. if (subtype == Variant::OBJECT && !subtype_hint_string.is_empty()) {
  482. allowed_type = subtype_hint_string;
  483. }
  484. Dictionary drag_data = p_drag_data;
  485. const String drop_type = drag_data.get("type", "");
  486. if (drop_type == "files") {
  487. PackedStringArray files = drag_data["files"];
  488. for (const String &file : files) {
  489. int idx_in_dir;
  490. EditorFileSystemDirectory const *dir = EditorFileSystem::get_singleton()->find_file(file, &idx_in_dir);
  491. if (!dir) {
  492. return false;
  493. }
  494. StringName ftype = dir->get_file_type(idx_in_dir);
  495. String script_class = dir->get_file_resource_script_class(idx_in_dir);
  496. for (String at : allowed_type.split(",", false)) {
  497. at = at.strip_edges();
  498. // Fail if one of the files is not of allowed type.
  499. if (!ClassDB::is_parent_class(ftype, at) && !EditorNode::get_editor_data().script_class_is_parent(script_class, at)) {
  500. return false;
  501. }
  502. }
  503. }
  504. // If no files fail, drop is valid.
  505. return true;
  506. }
  507. if (drop_type == "resource") {
  508. Ref<Resource> res = drag_data["resource"];
  509. if (res.is_null()) {
  510. return false;
  511. }
  512. String res_type = res->get_class();
  513. StringName script_class;
  514. if (res->get_script()) {
  515. script_class = EditorNode::get_singleton()->get_object_custom_type_name(res->get_script());
  516. }
  517. for (String at : allowed_type.split(",", false)) {
  518. at = at.strip_edges();
  519. if (ClassDB::is_parent_class(res_type, at) || EditorNode::get_editor_data().script_class_is_parent(script_class, at)) {
  520. return true;
  521. }
  522. }
  523. return false;
  524. }
  525. if (drop_type == "nodes") {
  526. Array node_paths = drag_data["nodes"];
  527. PackedStringArray allowed_subtype_array;
  528. if (allowed_type == "NodePath") {
  529. if (subtype_hint_string == "NodePath") {
  530. return true;
  531. } else {
  532. for (String ast : subtype_hint_string.split(",", false)) {
  533. ast = ast.strip_edges();
  534. allowed_subtype_array.append(ast);
  535. }
  536. }
  537. }
  538. bool is_drop_allowed = true;
  539. for (int i = 0; i < node_paths.size(); i++) {
  540. const Node *dropped_node = get_node_or_null(node_paths[i]);
  541. ERR_FAIL_NULL_V_MSG(dropped_node, false, "Could not get the dropped node by its path.");
  542. if (allowed_type != "NodePath") {
  543. if (!ClassDB::is_parent_class(dropped_node->get_class_name(), allowed_type) &&
  544. !EditorNode::get_singleton()->is_object_of_custom_type(dropped_node, allowed_type)) {
  545. // Fail if one of the nodes is not of allowed type.
  546. return false;
  547. }
  548. }
  549. // The array of NodePaths is restricted to specific types using @export_node_path().
  550. if (allowed_type == "NodePath" && subtype_hint_string != "NodePath") {
  551. if (!allowed_subtype_array.has(dropped_node->get_class_name())) {
  552. // The dropped node type was not found in the allowed subtype array, we must check if it inherits one of them.
  553. for (const String &ast : allowed_subtype_array) {
  554. if (ClassDB::is_parent_class(dropped_node->get_class_name(), ast) ||
  555. EditorNode::get_singleton()->is_object_of_custom_type(dropped_node, ast)) {
  556. is_drop_allowed = true;
  557. break;
  558. } else {
  559. is_drop_allowed = false;
  560. }
  561. }
  562. if (!is_drop_allowed) {
  563. break;
  564. }
  565. }
  566. }
  567. }
  568. return is_drop_allowed;
  569. }
  570. return false;
  571. }
  572. bool EditorPropertyArray::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  573. return _is_drop_valid(p_data);
  574. }
  575. void EditorPropertyArray::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  576. Dictionary drag_data = p_data;
  577. const String drop_type = drag_data.get("type", "");
  578. Variant array = object->get_array();
  579. // Handle the case where array is not initialized yet.
  580. if (!array.is_array()) {
  581. initialize_array(array);
  582. } else {
  583. array = array.duplicate();
  584. }
  585. if (drop_type == "files") {
  586. PackedStringArray files = drag_data["files"];
  587. // Loop the file array and add to existing array.
  588. for (int i = 0; i < files.size(); i++) {
  589. const String &file = files[i];
  590. Ref<Resource> res = ResourceLoader::load(file);
  591. if (res.is_valid()) {
  592. array.call("push_back", res);
  593. }
  594. }
  595. emit_changed(get_edited_property(), array);
  596. }
  597. if (drop_type == "resource") {
  598. Ref<Resource> res = drag_data["resource"];
  599. if (res.is_valid()) {
  600. array.call("push_back", res);
  601. emit_changed(get_edited_property(), array);
  602. }
  603. }
  604. if (drop_type == "nodes") {
  605. Array node_paths = drag_data["nodes"];
  606. Node *base_node = get_base_node();
  607. for (int i = 0; i < node_paths.size(); i++) {
  608. const NodePath &path = node_paths[i];
  609. if (subtype == Variant::OBJECT) {
  610. array.call("push_back", get_node(path));
  611. } else if (subtype == Variant::NODE_PATH) {
  612. array.call("push_back", base_node->get_path().rel_path_to(path));
  613. }
  614. }
  615. emit_changed(get_edited_property(), array);
  616. }
  617. }
  618. Node *EditorPropertyArray::get_base_node() {
  619. Node *base_node = Object::cast_to<Node>(InspectorDock::get_inspector_singleton()->get_edited_object());
  620. if (!base_node) {
  621. base_node = get_tree()->get_edited_scene_root();
  622. }
  623. return base_node;
  624. }
  625. void EditorPropertyArray::_notification(int p_what) {
  626. switch (p_what) {
  627. case NOTIFICATION_THEME_CHANGED: {
  628. if (button_add_item) {
  629. button_add_item->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  630. }
  631. } break;
  632. case NOTIFICATION_DRAG_BEGIN: {
  633. if (is_visible_in_tree()) {
  634. if (_is_drop_valid(get_viewport()->gui_get_drag_data())) {
  635. dropping = true;
  636. edit->queue_redraw();
  637. if (button_add_item) {
  638. button_add_item->queue_redraw();
  639. }
  640. }
  641. }
  642. } break;
  643. case NOTIFICATION_DRAG_END: {
  644. if (dropping) {
  645. dropping = false;
  646. edit->queue_redraw();
  647. if (button_add_item) {
  648. button_add_item->queue_redraw();
  649. }
  650. }
  651. } break;
  652. }
  653. }
  654. void EditorPropertyArray::_edit_pressed() {
  655. Variant array = get_edited_property_value();
  656. if (!array.is_array() && edit->is_pressed()) {
  657. initialize_array(array);
  658. emit_changed(get_edited_property(), array);
  659. }
  660. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  661. update_property();
  662. }
  663. void EditorPropertyArray::_page_changed(int p_page) {
  664. if (updating) {
  665. return;
  666. }
  667. page_index = p_page;
  668. int i = p_page * page_length;
  669. if (reorder_slot.index < 0) {
  670. for (Slot &slot : slots) {
  671. slot.set_index(i);
  672. i++;
  673. }
  674. } else {
  675. int reorder_from_page = reorder_slot.index / page_length;
  676. if (reorder_from_page < p_page) {
  677. i++;
  678. }
  679. for (Slot &slot : slots) {
  680. if (slot.index != reorder_slot.index) {
  681. slot.set_index(i);
  682. i++;
  683. } else if (i == reorder_slot.index) {
  684. i++;
  685. }
  686. }
  687. }
  688. update_property();
  689. }
  690. void EditorPropertyArray::_length_changed(double p_page) {
  691. if (updating) {
  692. return;
  693. }
  694. Variant array = object->get_array().duplicate();
  695. array.call("resize", int(p_page));
  696. emit_changed(get_edited_property(), array);
  697. }
  698. void EditorPropertyArray::_add_element() {
  699. _length_changed(double(object->get_array().call("size")) + 1.0);
  700. }
  701. void EditorPropertyArray::setup(Variant::Type p_array_type, const String &p_hint_string) {
  702. array_type = p_array_type;
  703. // The format of p_hint_string is:
  704. // subType/subTypeHint:nextSubtype ... etc.
  705. if (!p_hint_string.is_empty()) {
  706. int hint_subtype_separator = p_hint_string.find_char(':');
  707. if (hint_subtype_separator >= 0) {
  708. String subtype_string = p_hint_string.substr(0, hint_subtype_separator);
  709. int slash_pos = subtype_string.find_char('/');
  710. if (slash_pos >= 0) {
  711. subtype_hint = PropertyHint(subtype_string.substr(slash_pos + 1).to_int());
  712. subtype_string = subtype_string.substr(0, slash_pos);
  713. }
  714. subtype_hint_string = p_hint_string.substr(hint_subtype_separator + 1);
  715. subtype = Variant::Type(subtype_string.to_int());
  716. } else {
  717. subtype = Variant::get_type_by_name(p_hint_string);
  718. if (subtype == Variant::VARIANT_MAX) {
  719. subtype = Variant::OBJECT;
  720. subtype_hint = PROPERTY_HINT_RESOURCE_TYPE;
  721. subtype_hint_string = p_hint_string;
  722. }
  723. }
  724. }
  725. }
  726. void EditorPropertyArray::_reorder_button_gui_input(const Ref<InputEvent> &p_event) {
  727. if (reorder_slot.index < 0 || is_read_only()) {
  728. return;
  729. }
  730. Ref<InputEventMouseMotion> mm = p_event;
  731. if (mm.is_valid()) {
  732. Variant array = object->get_array();
  733. int size = array.call("size");
  734. // Cumulate the mouse delta, many small changes (dragging slowly) should result in reordering at some point.
  735. reorder_mouse_y_delta += mm->get_relative().y;
  736. // Reordering is done by moving the dragged element by +1/-1 index at a time based on the cumulated mouse delta so if
  737. // already at the array bounds make sure to ignore the remaining out of bounds drag (by resetting the cumulated delta).
  738. if ((reorder_to_index == 0 && reorder_mouse_y_delta < 0.0f) || (reorder_to_index == size - 1 && reorder_mouse_y_delta > 0.0f)) {
  739. reorder_mouse_y_delta = 0.0f;
  740. return;
  741. }
  742. float required_y_distance = 20.0f * EDSCALE;
  743. if (Math::abs(reorder_mouse_y_delta) > required_y_distance) {
  744. int direction = reorder_mouse_y_delta > 0.0f ? 1 : -1;
  745. reorder_mouse_y_delta -= required_y_distance * direction;
  746. reorder_to_index += direction;
  747. property_vbox->move_child(reorder_slot.container, reorder_to_index % page_length);
  748. if ((direction < 0 && reorder_to_index % page_length == page_length - 1) || (direction > 0 && reorder_to_index % page_length == 0)) {
  749. // Automatically move to the next/previous page.
  750. _page_changed(page_index + direction);
  751. }
  752. // Ensure the moving element is visible in the root inspector.
  753. EditorInspector *parent_inspector = get_parent_inspector();
  754. if (parent_inspector) {
  755. // Defer to prevent moving elements from not displaying properly, especially near borders.
  756. callable_mp((ScrollContainer *)parent_inspector->get_root_inspector(), &ScrollContainer::ensure_control_visible).call_deferred(reorder_slot.container);
  757. }
  758. }
  759. }
  760. }
  761. void EditorPropertyArray::_reorder_button_down(int p_slot_index) {
  762. if (is_read_only()) {
  763. return;
  764. }
  765. reorder_slot = slots[p_slot_index];
  766. reorder_to_index = reorder_slot.index;
  767. // Ideally it'd to be able to show the mouse but I had issues with
  768. // Control's `mouse_exit()`/`mouse_entered()` signals not getting called.
  769. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  770. }
  771. void EditorPropertyArray::_reorder_button_up() {
  772. if (is_read_only()) {
  773. return;
  774. }
  775. if (reorder_slot.index != reorder_to_index) {
  776. // Move the element.
  777. Variant array = object->get_array().duplicate();
  778. property_vbox->move_child(reorder_slot.container, reorder_slot.index % page_length);
  779. Variant value_to_move = array.get(reorder_slot.index);
  780. array.call("remove_at", reorder_slot.index);
  781. array.call("insert", reorder_to_index, value_to_move);
  782. slots[reorder_to_index % page_length].reorder_button->grab_focus();
  783. emit_changed(get_edited_property(), array);
  784. }
  785. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  786. ERR_FAIL_NULL(reorder_slot.reorder_button);
  787. reorder_slot.reorder_button->warp_mouse(reorder_slot.reorder_button->get_size() / 2.0f);
  788. reorder_to_index = -1;
  789. reorder_mouse_y_delta = 0.0f;
  790. reorder_slot = Slot();
  791. _page_changed(page_index);
  792. }
  793. bool EditorPropertyArray::is_colored(ColorationMode p_mode) {
  794. return p_mode == COLORATION_CONTAINER_RESOURCE;
  795. }
  796. EditorPropertyArray::EditorPropertyArray() {
  797. object.instantiate();
  798. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  799. edit = memnew(Button);
  800. edit->set_accessibility_name(TTRC("Edit"));
  801. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  802. edit->set_clip_text(true);
  803. edit->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyArray::_edit_pressed));
  804. edit->set_toggle_mode(true);
  805. SET_DRAG_FORWARDING_CD(edit, EditorPropertyArray);
  806. edit->connect(SceneStringName(draw), callable_mp(this, &EditorPropertyArray::_button_draw));
  807. add_child(edit);
  808. add_focusable(edit);
  809. change_type = memnew(EditorVariantTypePopupMenu(true));
  810. add_child(change_type);
  811. change_type->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyArray::_change_type_menu));
  812. changing_type_index = -1;
  813. subtype = Variant::NIL;
  814. subtype_hint = PROPERTY_HINT_NONE;
  815. subtype_hint_string = "";
  816. has_borders = true;
  817. }
  818. ///////////////////// DICTIONARY ///////////////////////////
  819. void EditorPropertyDictionary::initialize_dictionary(Variant &p_dictionary) {
  820. if (key_subtype != Variant::NIL || value_subtype != Variant::NIL) {
  821. Dictionary dict;
  822. StringName key_subtype_class;
  823. Ref<Script> key_subtype_script;
  824. if (key_subtype == Variant::OBJECT && !key_subtype_hint_string.is_empty() && ClassDB::class_exists(key_subtype_hint_string)) {
  825. key_subtype_class = key_subtype_hint_string;
  826. }
  827. StringName value_subtype_class;
  828. Ref<Script> value_subtype_script;
  829. if (value_subtype == Variant::OBJECT && !value_subtype_hint_string.is_empty() && ClassDB::class_exists(value_subtype_hint_string)) {
  830. value_subtype_class = value_subtype_hint_string;
  831. }
  832. dict.set_typed(key_subtype, key_subtype_class, key_subtype_script, value_subtype, value_subtype_class, value_subtype_script);
  833. p_dictionary = dict;
  834. } else {
  835. VariantInternal::initialize(&p_dictionary, Variant::DICTIONARY);
  836. }
  837. }
  838. void EditorPropertyDictionary::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
  839. if (p_value.get_type() == Variant::OBJECT && p_value.is_null()) {
  840. p_value = Variant(); // `EditorResourcePicker` resets to `Ref<Resource>()`. See GH-82716.
  841. }
  842. object->set(p_property, p_value);
  843. bool new_item_or_key = !p_property.begins_with("indices");
  844. emit_changed(get_edited_property(), object->get_dict(), p_name, p_changing || new_item_or_key);
  845. if (new_item_or_key) {
  846. update_property();
  847. }
  848. }
  849. void EditorPropertyDictionary::_change_type(Object *p_button, int p_slot_index) {
  850. Button *button = Object::cast_to<Button>(p_button);
  851. int index = slots[p_slot_index].index;
  852. Rect2 rect = button->get_screen_rect();
  853. change_type->set_item_disabled(change_type->get_item_index(Variant::VARIANT_MAX), index < 0);
  854. change_type->reset_size();
  855. change_type->set_position(rect.get_end() - Vector2(change_type->get_contents_minimum_size().x, 0));
  856. change_type->popup();
  857. changing_type_index = index;
  858. }
  859. void EditorPropertyDictionary::_add_key_value() {
  860. // Do not allow nil as valid key. I experienced errors with this
  861. if (object->get_new_item_key().get_type() == Variant::NIL) {
  862. return;
  863. }
  864. Dictionary dict = object->get_dict().duplicate();
  865. Variant new_key = object->get_new_item_key();
  866. Variant new_value = object->get_new_item_value();
  867. dict[new_key] = new_value;
  868. Variant::Type type = new_key.get_type();
  869. new_key.zero();
  870. VariantInternal::initialize(&new_key, type);
  871. object->set_new_item_key(new_key);
  872. type = new_value.get_type();
  873. new_value.zero();
  874. VariantInternal::initialize(&new_value, type);
  875. object->set_new_item_value(new_value);
  876. object->set_dict(dict);
  877. slots[(dict.size() - 1) % page_length].update_prop_or_index();
  878. emit_changed(get_edited_property(), dict);
  879. }
  880. void EditorPropertyDictionary::_create_new_property_slot(int p_idx) {
  881. HBoxContainer *hbox = memnew(HBoxContainer);
  882. EditorProperty *prop_key = nullptr;
  883. if (p_idx != EditorPropertyDictionaryObject::NEW_KEY_INDEX && p_idx != EditorPropertyDictionaryObject::NEW_VALUE_INDEX) {
  884. prop_key = memnew(EditorPropertyNil);
  885. hbox->add_child(prop_key);
  886. }
  887. EditorProperty *prop = memnew(EditorPropertyNil);
  888. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  889. if (p_idx != EditorPropertyDictionaryObject::NEW_KEY_INDEX && p_idx != EditorPropertyDictionaryObject::NEW_VALUE_INDEX) {
  890. prop->set_draw_label(false);
  891. }
  892. hbox->add_child(prop);
  893. bool use_key = p_idx == EditorPropertyDictionaryObject::NEW_KEY_INDEX;
  894. bool is_untyped_dict = (use_key ? key_subtype : value_subtype) == Variant::NIL;
  895. if (is_untyped_dict) {
  896. Button *edit_btn = memnew(Button);
  897. edit_btn->set_accessibility_name(TTRC("Edit"));
  898. edit_btn->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  899. edit_btn->set_disabled(is_read_only());
  900. edit_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyDictionary::_change_type).bind(edit_btn, slots.size()));
  901. hbox->add_child(edit_btn);
  902. } else if (p_idx >= 0) {
  903. Button *remove_btn = memnew(Button);
  904. remove_btn->set_accessibility_name(TTRC("Remove"));
  905. remove_btn->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  906. remove_btn->set_disabled(is_read_only());
  907. remove_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyDictionary::_remove_pressed).bind(slots.size()));
  908. hbox->add_child(remove_btn);
  909. }
  910. if (add_panel) {
  911. add_panel->get_child(0)->add_child(hbox);
  912. } else {
  913. property_vbox->add_child(hbox);
  914. }
  915. Slot slot;
  916. slot.prop = prop;
  917. slot.prop_key = prop_key;
  918. slot.object = object;
  919. slot.container = hbox;
  920. int index = p_idx + (p_idx >= 0 ? page_index * page_length : 0);
  921. slot.set_index(index);
  922. slots.push_back(slot);
  923. }
  924. void EditorPropertyDictionary::_change_type_menu(int p_index) {
  925. ERR_FAIL_COND_MSG(
  926. changing_type_index == EditorPropertyDictionaryObject::NOT_CHANGING_TYPE,
  927. "Tried to change the type of a dict key or value, but nothing was selected.");
  928. Variant value;
  929. switch (changing_type_index) {
  930. case EditorPropertyDictionaryObject::NEW_KEY_INDEX:
  931. case EditorPropertyDictionaryObject::NEW_VALUE_INDEX:
  932. VariantInternal::initialize(&value, Variant::Type(p_index));
  933. if (changing_type_index == EditorPropertyDictionaryObject::NEW_KEY_INDEX) {
  934. object->set_new_item_key(value);
  935. } else {
  936. object->set_new_item_value(value);
  937. }
  938. update_property();
  939. break;
  940. default:
  941. Dictionary dict = object->get_dict().duplicate();
  942. Variant key = dict.get_key_at_index(changing_type_index);
  943. if (p_index < Variant::VARIANT_MAX) {
  944. VariantInternal::initialize(&value, Variant::Type(p_index));
  945. dict[key] = value;
  946. } else {
  947. dict.erase(key);
  948. object->set_dict(dict);
  949. for (Slot &slot : slots) {
  950. slot.update_prop_or_index();
  951. }
  952. }
  953. emit_changed(get_edited_property(), dict);
  954. }
  955. }
  956. void EditorPropertyDictionary::setup(PropertyHint p_hint, const String &p_hint_string) {
  957. PackedStringArray types = p_hint_string.split(";");
  958. if (types.size() > 0 && !types[0].is_empty()) {
  959. String key = types[0];
  960. int hint_key_subtype_separator = key.find_char(':');
  961. if (hint_key_subtype_separator >= 0) {
  962. String key_subtype_string = key.substr(0, hint_key_subtype_separator);
  963. int slash_pos = key_subtype_string.find_char('/');
  964. if (slash_pos >= 0) {
  965. key_subtype_hint = PropertyHint(key_subtype_string.substr(slash_pos + 1).to_int());
  966. key_subtype_string = key_subtype_string.substr(0, slash_pos);
  967. }
  968. key_subtype_hint_string = key.substr(hint_key_subtype_separator + 1);
  969. key_subtype = Variant::Type(key_subtype_string.to_int());
  970. } else {
  971. key_subtype = Variant::get_type_by_name(key);
  972. if (key_subtype == Variant::VARIANT_MAX) {
  973. key_subtype = Variant::OBJECT;
  974. key_subtype_hint = PROPERTY_HINT_RESOURCE_TYPE;
  975. key_subtype_hint_string = key;
  976. }
  977. }
  978. Variant new_key = object->get_new_item_key();
  979. VariantInternal::initialize(&new_key, key_subtype);
  980. object->set_new_item_key(new_key);
  981. }
  982. if (types.size() > 1 && !types[1].is_empty()) {
  983. String value = types[1];
  984. int hint_value_subtype_separator = value.find_char(':');
  985. if (hint_value_subtype_separator >= 0) {
  986. String value_subtype_string = value.substr(0, hint_value_subtype_separator);
  987. int slash_pos = value_subtype_string.find_char('/');
  988. if (slash_pos >= 0) {
  989. value_subtype_hint = PropertyHint(value_subtype_string.substr(slash_pos + 1).to_int());
  990. value_subtype_string = value_subtype_string.substr(0, slash_pos);
  991. }
  992. value_subtype_hint_string = value.substr(hint_value_subtype_separator + 1);
  993. value_subtype = Variant::Type(value_subtype_string.to_int());
  994. } else {
  995. value_subtype = Variant::get_type_by_name(value);
  996. if (value_subtype == Variant::VARIANT_MAX) {
  997. value_subtype = Variant::OBJECT;
  998. value_subtype_hint = PROPERTY_HINT_RESOURCE_TYPE;
  999. value_subtype_hint_string = value;
  1000. }
  1001. }
  1002. Variant new_value = object->get_new_item_value();
  1003. VariantInternal::initialize(&new_value, value_subtype);
  1004. object->set_new_item_value(new_value);
  1005. }
  1006. }
  1007. void EditorPropertyDictionary::set_preview_value(bool p_preview_value) {
  1008. preview_value = p_preview_value;
  1009. }
  1010. void EditorPropertyDictionary::update_property() {
  1011. Variant updated_val = get_edited_property_value();
  1012. String dict_type_name = "Dictionary";
  1013. String dict_sub_type_name;
  1014. if (key_subtype != Variant::NIL || value_subtype != Variant::NIL) {
  1015. String key_subtype_name = "Variant";
  1016. if (key_subtype == Variant::OBJECT && (key_subtype_hint == PROPERTY_HINT_RESOURCE_TYPE || key_subtype_hint == PROPERTY_HINT_NODE_TYPE)) {
  1017. key_subtype_name = key_subtype_hint_string;
  1018. } else if (key_subtype != Variant::NIL) {
  1019. key_subtype_name = Variant::get_type_name(key_subtype);
  1020. }
  1021. String value_subtype_name = "Variant";
  1022. if (value_subtype == Variant::OBJECT && (value_subtype_hint == PROPERTY_HINT_RESOURCE_TYPE || value_subtype_hint == PROPERTY_HINT_NODE_TYPE)) {
  1023. value_subtype_name = value_subtype_hint_string;
  1024. } else if (value_subtype != Variant::NIL) {
  1025. value_subtype_name = Variant::get_type_name(value_subtype);
  1026. }
  1027. if (preview_value) {
  1028. dict_sub_type_name = vformat("[%s, %s] ", key_subtype_name, value_subtype_name);
  1029. } else {
  1030. dict_type_name += vformat("[%s, %s]", key_subtype_name, value_subtype_name);
  1031. }
  1032. }
  1033. if (updated_val.get_type() != Variant::DICTIONARY) {
  1034. if (preview_value) {
  1035. edit->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  1036. edit->set_button_icon(get_editor_theme_icon(SNAME("Nil")));
  1037. edit->set_text(dict_type_name);
  1038. } else {
  1039. edit->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1040. edit->set_button_icon(Ref<Texture2D>());
  1041. edit->set_text(vformat(TTR("(Nil) %s"), dict_type_name));
  1042. }
  1043. edit->set_pressed(false);
  1044. if (container) {
  1045. set_bottom_editor(nullptr);
  1046. memdelete(container);
  1047. button_add_item = nullptr;
  1048. container = nullptr;
  1049. add_panel = nullptr;
  1050. slots.clear();
  1051. }
  1052. return;
  1053. }
  1054. Dictionary dict = updated_val;
  1055. object->set_dict(updated_val);
  1056. if (preview_value) {
  1057. String ctr_str = updated_val.get_construct_string().remove_char('\n');
  1058. if (key_subtype != Variant::NIL || value_subtype != Variant::NIL) {
  1059. int type_end = ctr_str.find("](");
  1060. if (type_end > 0) {
  1061. ctr_str = ctr_str.substr(type_end + 2).trim_suffix(")");
  1062. }
  1063. }
  1064. edit->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  1065. edit->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  1066. edit->set_button_icon(get_editor_theme_icon(dict_type_name));
  1067. edit->set_text(vformat("%s%s", dict_sub_type_name, ctr_str));
  1068. edit->set_tooltip_text(vformat(TTR("%s%s (size %d)"), dict_type_name, dict_sub_type_name, dict.size()));
  1069. } else {
  1070. edit->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1071. edit->set_button_icon(Ref<Texture2D>());
  1072. edit->set_text(vformat(TTR("%s (size %d)"), dict_type_name, dict.size()));
  1073. }
  1074. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  1075. if (edit->is_pressed() != unfolded) {
  1076. edit->set_pressed(unfolded);
  1077. }
  1078. if (unfolded) {
  1079. updating = true;
  1080. if (!container) {
  1081. container = memnew(PanelContainer);
  1082. add_child(container);
  1083. set_bottom_editor(container);
  1084. VBoxContainer *vbox = memnew(VBoxContainer);
  1085. container->add_child(vbox);
  1086. property_vbox = memnew(VBoxContainer);
  1087. property_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1088. vbox->add_child(property_vbox);
  1089. paginator = memnew(EditorPaginator);
  1090. paginator->connect("page_changed", callable_mp(this, &EditorPropertyDictionary::_page_changed));
  1091. vbox->add_child(paginator);
  1092. for (int i = 0; i < page_length; i++) {
  1093. _create_new_property_slot(slots.size());
  1094. }
  1095. add_panel = memnew(PanelContainer);
  1096. property_vbox->add_child(add_panel);
  1097. add_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("DictionaryAddItem")));
  1098. VBoxContainer *add_vbox = memnew(VBoxContainer);
  1099. add_panel->add_child(add_vbox);
  1100. _create_new_property_slot(EditorPropertyDictionaryObject::NEW_KEY_INDEX);
  1101. _create_new_property_slot(EditorPropertyDictionaryObject::NEW_VALUE_INDEX);
  1102. button_add_item = EditorInspector::create_inspector_action_button(TTR("Add Key/Value Pair"));
  1103. button_add_item->set_button_icon(get_theme_icon(SNAME("Add"), EditorStringName(EditorIcons)));
  1104. button_add_item->set_disabled(is_read_only());
  1105. button_add_item->set_accessibility_name(TTRC("Add"));
  1106. button_add_item->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyDictionary::_add_key_value));
  1107. add_vbox->add_child(button_add_item);
  1108. }
  1109. int size = dict.size();
  1110. int max_page = MAX(0, size - 1) / page_length;
  1111. if (page_index > max_page) {
  1112. _page_changed(max_page);
  1113. }
  1114. paginator->update(page_index, max_page);
  1115. paginator->set_visible(max_page > 0);
  1116. add_panel->set_visible(page_index == max_page);
  1117. for (Slot &slot : slots) {
  1118. bool slot_visible = slot.index < size;
  1119. slot.container->set_visible(slot_visible);
  1120. // If not visible no need to update it.
  1121. if (!slot_visible) {
  1122. continue;
  1123. }
  1124. // Check if the editor property key needs to be updated.
  1125. if (slot.prop_key) {
  1126. Variant key;
  1127. object->get_by_property_name(slot.key_name, key);
  1128. Variant::Type key_type = key.get_type();
  1129. bool key_as_id = Object::cast_to<EncodedObjectAsID>(key);
  1130. if (key_type != slot.key_type || (key_type == Variant::OBJECT && key_as_id != slot.key_as_id)) {
  1131. slot.key_as_id = key_as_id;
  1132. slot.key_type = key_type;
  1133. EditorProperty *new_prop = nullptr;
  1134. if (key_type == Variant::OBJECT && key_as_id) {
  1135. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  1136. editor->setup("Object");
  1137. new_prop = editor;
  1138. } else {
  1139. new_prop = EditorInspector::instantiate_property_editor(this, key_type, "", key_subtype_hint, key_subtype_hint_string, PROPERTY_USAGE_NONE);
  1140. }
  1141. new_prop->set_read_only(true);
  1142. new_prop->set_selectable(false);
  1143. new_prop->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  1144. new_prop->set_draw_background(false);
  1145. new_prop->set_use_folding(is_using_folding());
  1146. new_prop->set_h_size_flags(SIZE_EXPAND_FILL);
  1147. new_prop->set_draw_label(false);
  1148. EditorPropertyArray *arr_prop = Object::cast_to<EditorPropertyArray>(new_prop);
  1149. if (arr_prop) {
  1150. arr_prop->set_preview_value(true);
  1151. }
  1152. EditorPropertyDictionary *dict_prop = Object::cast_to<EditorPropertyDictionary>(new_prop);
  1153. if (dict_prop) {
  1154. dict_prop->set_preview_value(true);
  1155. }
  1156. slot.set_key_prop(new_prop);
  1157. }
  1158. }
  1159. Variant value;
  1160. object->get_by_property_name(slot.prop_name, value);
  1161. Variant::Type value_type;
  1162. if (dict.is_typed_value() && value_subtype != Variant::NIL && slot.prop_key) {
  1163. value_type = value_subtype;
  1164. } else {
  1165. value_type = value.get_type();
  1166. }
  1167. // Check if the editor property needs to be updated.
  1168. bool value_as_id = Object::cast_to<EncodedObjectAsID>(value);
  1169. if (value_type != slot.type || (value_type == Variant::OBJECT && value_as_id != slot.as_id)) {
  1170. slot.as_id = value_as_id;
  1171. slot.type = value_type;
  1172. EditorProperty *new_prop = nullptr;
  1173. if (value_type == Variant::OBJECT && value_as_id) {
  1174. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  1175. editor->setup("Object");
  1176. new_prop = editor;
  1177. } else {
  1178. bool use_key = slot.index == EditorPropertyDictionaryObject::NEW_KEY_INDEX;
  1179. new_prop = EditorInspector::instantiate_property_editor(this, value_type, "", use_key ? key_subtype_hint : value_subtype_hint,
  1180. use_key ? key_subtype_hint_string : value_subtype_hint_string, PROPERTY_USAGE_NONE);
  1181. }
  1182. new_prop->set_selectable(false);
  1183. new_prop->set_use_folding(is_using_folding());
  1184. new_prop->connect(SNAME("property_changed"), callable_mp(this, &EditorPropertyDictionary::_property_changed));
  1185. new_prop->connect(SNAME("object_id_selected"), callable_mp(this, &EditorPropertyDictionary::_object_id_selected));
  1186. new_prop->set_h_size_flags(SIZE_EXPAND_FILL);
  1187. if (slot.index != EditorPropertyDictionaryObject::NEW_KEY_INDEX && slot.index != EditorPropertyDictionaryObject::NEW_VALUE_INDEX) {
  1188. new_prop->set_draw_label(false);
  1189. }
  1190. new_prop->set_read_only(is_read_only());
  1191. slot.set_prop(new_prop);
  1192. } else if (slot.index != EditorPropertyDictionaryObject::NEW_KEY_INDEX && slot.index != EditorPropertyDictionaryObject::NEW_VALUE_INDEX) {
  1193. Variant key = dict.get_key_at_index(slot.index);
  1194. String cs = key.get_construct_string();
  1195. slot.prop->set_tooltip_text(cs);
  1196. }
  1197. // We need to grab the focus of the property that is being changed, even if the type didn't actually changed.
  1198. // Otherwise, focus will stay on the change type button, which is not very user friendly.
  1199. if (changing_type_index == slot.index) {
  1200. callable_mp(slot.prop, &EditorProperty::grab_focus).call_deferred(0);
  1201. changing_type_index = EditorPropertyDictionaryObject::NOT_CHANGING_TYPE; // Reset to avoid grabbing focus again.
  1202. }
  1203. slot.prop->update_property();
  1204. if (slot.prop_key) {
  1205. slot.prop_key->update_property();
  1206. }
  1207. }
  1208. updating = false;
  1209. } else {
  1210. if (container) {
  1211. set_bottom_editor(nullptr);
  1212. memdelete(container);
  1213. button_add_item = nullptr;
  1214. container = nullptr;
  1215. add_panel = nullptr;
  1216. slots.clear();
  1217. }
  1218. }
  1219. }
  1220. void EditorPropertyDictionary::_remove_pressed(int p_slot_index) {
  1221. Dictionary dict = object->get_dict().duplicate();
  1222. int index = slots[p_slot_index].index;
  1223. dict.erase(dict.get_key_at_index(index));
  1224. emit_changed(get_edited_property(), dict);
  1225. }
  1226. void EditorPropertyDictionary::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  1227. emit_signal(SNAME("object_id_selected"), p_property, p_id);
  1228. }
  1229. void EditorPropertyDictionary::_notification(int p_what) {
  1230. switch (p_what) {
  1231. case NOTIFICATION_THEME_CHANGED: {
  1232. if (button_add_item) {
  1233. button_add_item->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  1234. add_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("DictionaryAddItem")));
  1235. }
  1236. } break;
  1237. }
  1238. }
  1239. void EditorPropertyDictionary::_edit_pressed() {
  1240. Variant prop_val = get_edited_property_value();
  1241. if (prop_val.get_type() == Variant::NIL && edit->is_pressed()) {
  1242. initialize_dictionary(prop_val);
  1243. emit_changed(get_edited_property(), prop_val);
  1244. }
  1245. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  1246. update_property();
  1247. }
  1248. void EditorPropertyDictionary::_page_changed(int p_page) {
  1249. page_index = p_page;
  1250. int i = p_page * page_length;
  1251. for (Slot &slot : slots) {
  1252. if (slot.index > -1) {
  1253. slot.set_index(i);
  1254. i++;
  1255. }
  1256. }
  1257. if (updating) {
  1258. return;
  1259. }
  1260. update_property();
  1261. }
  1262. bool EditorPropertyDictionary::is_colored(ColorationMode p_mode) {
  1263. return p_mode == COLORATION_CONTAINER_RESOURCE;
  1264. }
  1265. EditorPropertyDictionary::EditorPropertyDictionary() {
  1266. object.instantiate();
  1267. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  1268. edit = memnew(Button);
  1269. edit->set_accessibility_name(TTRC("Edit"));
  1270. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  1271. edit->set_clip_text(true);
  1272. edit->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyDictionary::_edit_pressed));
  1273. edit->set_toggle_mode(true);
  1274. add_child(edit);
  1275. add_focusable(edit);
  1276. container = nullptr;
  1277. button_add_item = nullptr;
  1278. paginator = nullptr;
  1279. change_type = memnew(EditorVariantTypePopupMenu(true));
  1280. add_child(change_type);
  1281. change_type->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyDictionary::_change_type_menu));
  1282. changing_type_index = EditorPropertyDictionaryObject::NOT_CHANGING_TYPE;
  1283. has_borders = true;
  1284. key_subtype = Variant::NIL;
  1285. key_subtype_hint = PROPERTY_HINT_NONE;
  1286. key_subtype_hint_string = "";
  1287. value_subtype = Variant::NIL;
  1288. value_subtype_hint = PROPERTY_HINT_NONE;
  1289. value_subtype_hint_string = "";
  1290. }
  1291. ///////////////////// LOCALIZABLE STRING ///////////////////////////
  1292. void EditorPropertyLocalizableString::_property_changed(const String &p_property, const Variant &p_value, const String &p_name, bool p_changing) {
  1293. if (p_property.begins_with("indices")) {
  1294. int index = p_property.get_slicec('/', 1).to_int();
  1295. Dictionary dict = object->get_dict().duplicate();
  1296. Variant key = dict.get_key_at_index(index);
  1297. dict[key] = p_value;
  1298. object->set_dict(dict);
  1299. emit_changed(get_edited_property(), dict, "", true);
  1300. }
  1301. }
  1302. void EditorPropertyLocalizableString::_add_locale_popup() {
  1303. locale_select->popup_locale_dialog();
  1304. }
  1305. void EditorPropertyLocalizableString::_add_locale(const String &p_locale) {
  1306. Dictionary dict = object->get_dict().duplicate();
  1307. object->set_new_item_key(p_locale);
  1308. object->set_new_item_value(String());
  1309. dict[object->get_new_item_key()] = object->get_new_item_value();
  1310. emit_changed(get_edited_property(), dict, "", false);
  1311. update_property();
  1312. }
  1313. void EditorPropertyLocalizableString::_remove_item(Object *p_button, int p_index) {
  1314. Dictionary dict = object->get_dict().duplicate();
  1315. Variant key = dict.get_key_at_index(p_index);
  1316. dict.erase(key);
  1317. emit_changed(get_edited_property(), dict, "", false);
  1318. update_property();
  1319. }
  1320. void EditorPropertyLocalizableString::update_property() {
  1321. Variant updated_val = get_edited_property_value();
  1322. if (updated_val.get_type() == Variant::NIL) {
  1323. edit->set_text(TTR("Localizable String (Nil)")); // This provides symmetry with the array property.
  1324. edit->set_pressed(false);
  1325. if (container) {
  1326. set_bottom_editor(nullptr);
  1327. memdelete(container);
  1328. button_add_item = nullptr;
  1329. container = nullptr;
  1330. }
  1331. return;
  1332. }
  1333. Dictionary dict = updated_val;
  1334. object->set_dict(dict);
  1335. edit->set_text(vformat(TTR("Localizable String (size %d)"), dict.size()));
  1336. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  1337. if (edit->is_pressed() != unfolded) {
  1338. edit->set_pressed(unfolded);
  1339. }
  1340. if (unfolded) {
  1341. updating = true;
  1342. if (!container) {
  1343. container = memnew(MarginContainer);
  1344. container->set_theme_type_variation("MarginContainer4px");
  1345. add_child(container);
  1346. set_bottom_editor(container);
  1347. VBoxContainer *vbox = memnew(VBoxContainer);
  1348. container->add_child(vbox);
  1349. property_vbox = memnew(VBoxContainer);
  1350. property_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1351. vbox->add_child(property_vbox);
  1352. paginator = memnew(EditorPaginator);
  1353. paginator->connect("page_changed", callable_mp(this, &EditorPropertyLocalizableString::_page_changed));
  1354. vbox->add_child(paginator);
  1355. } else {
  1356. // Queue children for deletion, deleting immediately might cause errors.
  1357. for (int i = property_vbox->get_child_count() - 1; i >= 0; i--) {
  1358. property_vbox->get_child(i)->queue_free();
  1359. }
  1360. }
  1361. int size = dict.size();
  1362. int max_page = MAX(0, size - 1) / page_length;
  1363. page_index = MIN(page_index, max_page);
  1364. paginator->update(page_index, max_page);
  1365. paginator->set_visible(max_page > 0);
  1366. int offset = page_index * page_length;
  1367. int amount = MIN(size - offset, page_length);
  1368. for (int i = 0; i < amount; i++) {
  1369. String prop_name;
  1370. Variant key;
  1371. Variant value;
  1372. prop_name = "indices/" + itos(i + offset);
  1373. key = dict.get_key_at_index(i + offset);
  1374. value = dict.get_value_at_index(i + offset);
  1375. EditorProperty *prop = memnew(EditorPropertyText);
  1376. prop->set_object_and_property(object.ptr(), prop_name);
  1377. int remove_index = 0;
  1378. String cs = key.get_construct_string();
  1379. prop->set_label(cs);
  1380. prop->set_tooltip_text(cs);
  1381. remove_index = i + offset;
  1382. prop->set_selectable(false);
  1383. prop->connect("property_changed", callable_mp(this, &EditorPropertyLocalizableString::_property_changed));
  1384. prop->connect("object_id_selected", callable_mp(this, &EditorPropertyLocalizableString::_object_id_selected));
  1385. HBoxContainer *hbox = memnew(HBoxContainer);
  1386. property_vbox->add_child(hbox);
  1387. hbox->add_child(prop);
  1388. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  1389. Button *edit_btn = memnew(Button);
  1390. edit_btn->set_accessibility_name(TTRC("Remove Translation"));
  1391. edit_btn->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  1392. hbox->add_child(edit_btn);
  1393. edit_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyLocalizableString::_remove_item).bind(edit_btn, remove_index));
  1394. prop->update_property();
  1395. }
  1396. if (page_index == max_page) {
  1397. button_add_item = EditorInspector::create_inspector_action_button(TTR("Add Translation"));
  1398. button_add_item->set_accessibility_name(TTRC("Add Translation"));
  1399. button_add_item->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  1400. button_add_item->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyLocalizableString::_add_locale_popup));
  1401. property_vbox->add_child(button_add_item);
  1402. }
  1403. updating = false;
  1404. } else {
  1405. if (container) {
  1406. set_bottom_editor(nullptr);
  1407. memdelete(container);
  1408. button_add_item = nullptr;
  1409. container = nullptr;
  1410. }
  1411. }
  1412. }
  1413. void EditorPropertyLocalizableString::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  1414. emit_signal(SNAME("object_id_selected"), p_property, p_id);
  1415. }
  1416. void EditorPropertyLocalizableString::_notification(int p_what) {
  1417. switch (p_what) {
  1418. case NOTIFICATION_THEME_CHANGED: {
  1419. if (button_add_item) {
  1420. button_add_item->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  1421. }
  1422. } break;
  1423. }
  1424. }
  1425. void EditorPropertyLocalizableString::_edit_pressed() {
  1426. Variant prop_val = get_edited_property_value();
  1427. if (prop_val.get_type() == Variant::NIL && edit->is_pressed()) {
  1428. VariantInternal::initialize(&prop_val, Variant::DICTIONARY);
  1429. get_edited_object()->set(get_edited_property(), prop_val);
  1430. }
  1431. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  1432. update_property();
  1433. }
  1434. void EditorPropertyLocalizableString::_page_changed(int p_page) {
  1435. if (updating) {
  1436. return;
  1437. }
  1438. page_index = p_page;
  1439. update_property();
  1440. }
  1441. EditorPropertyLocalizableString::EditorPropertyLocalizableString() {
  1442. object.instantiate();
  1443. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  1444. edit = memnew(Button);
  1445. edit->set_accessibility_name(TTRC("Edit"));
  1446. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  1447. edit->set_clip_text(true);
  1448. edit->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyLocalizableString::_edit_pressed));
  1449. edit->set_toggle_mode(true);
  1450. add_child(edit);
  1451. add_focusable(edit);
  1452. container = nullptr;
  1453. button_add_item = nullptr;
  1454. paginator = nullptr;
  1455. updating = false;
  1456. locale_select = memnew(EditorLocaleDialog);
  1457. locale_select->connect("locale_selected", callable_mp(this, &EditorPropertyLocalizableString::_add_locale));
  1458. add_child(locale_select);
  1459. }