2
0

editor_properties_array_dict.cpp 54 KB

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