editor_properties_array_dict.cpp 54 KB

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