editor_properties_array_dict.cpp 42 KB

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