editor_properties_array_dict.cpp 56 KB

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