editor_properties_array_dict.cpp 58 KB

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