editor_properties_array_dict.cpp 45 KB

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