editor_properties_array_dict.cpp 49 KB

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