tile_set_editor.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /**************************************************************************/
  2. /* tile_set_editor.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 "tile_set_editor.h"
  31. #include "tile_data_editors.h"
  32. #include "tiles_editor_plugin.h"
  33. #include "editor/editor_file_system.h"
  34. #include "editor/editor_inspector.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/editor_undo_redo_manager.h"
  38. #include "editor/gui/editor_file_dialog.h"
  39. #include "editor/themes/editor_scale.h"
  40. #include "scene/gui/box_container.h"
  41. #include "scene/gui/control.h"
  42. #include "scene/gui/dialogs.h"
  43. #include "scene/gui/tab_container.h"
  44. TileSetEditor *TileSetEditor::singleton = nullptr;
  45. void TileSetEditor::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  46. ERR_FAIL_COND(!tile_set.is_valid());
  47. if (!_can_drop_data_fw(p_point, p_data, p_from)) {
  48. return;
  49. }
  50. if (p_from == sources_list) {
  51. // Handle dropping a texture in the list of atlas resources.
  52. Dictionary d = p_data;
  53. Vector<String> files = d["files"];
  54. _load_texture_files(files);
  55. }
  56. }
  57. bool TileSetEditor::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  58. ERR_FAIL_COND_V(!tile_set.is_valid(), false);
  59. if (read_only) {
  60. return false;
  61. }
  62. if (p_from == sources_list) {
  63. Dictionary d = p_data;
  64. if (!d.has("type")) {
  65. return false;
  66. }
  67. // Check if we have a Texture2D.
  68. if (String(d["type"]) == "files") {
  69. Vector<String> files = d["files"];
  70. if (files.size() == 0) {
  71. return false;
  72. }
  73. for (int i = 0; i < files.size(); i++) {
  74. String ftype = EditorFileSystem::get_singleton()->get_file_type(files[i]);
  75. if (!ClassDB::is_parent_class(ftype, "Texture2D")) {
  76. return false;
  77. }
  78. }
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. void TileSetEditor::_load_texture_files(const Vector<String> &p_paths) {
  85. int source_id = TileSet::INVALID_SOURCE;
  86. Vector<Ref<TileSetAtlasSource>> atlases;
  87. for (const String &p_path : p_paths) {
  88. Ref<Texture2D> texture = ResourceLoader::load(p_path);
  89. if (texture.is_null()) {
  90. EditorNode::get_singleton()->show_warning(TTR("Invalid texture selected."));
  91. continue;
  92. }
  93. // Retrieve the id for the next created source.
  94. source_id = tile_set->get_next_source_id();
  95. // Actually create the new source.
  96. Ref<TileSetAtlasSource> atlas_source = memnew(TileSetAtlasSource);
  97. atlas_source->set_texture(texture);
  98. atlas_source->set_texture_region_size(tile_set->get_tile_size());
  99. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  100. undo_redo->create_action(TTR("Add a new atlas source"));
  101. undo_redo->add_do_method(*tile_set, "add_source", atlas_source, source_id);
  102. undo_redo->add_undo_method(*tile_set, "remove_source", source_id);
  103. undo_redo->commit_action();
  104. atlases.append(atlas_source);
  105. }
  106. if (!atlases.is_empty()) {
  107. tile_set_atlas_source_editor->init_new_atlases(atlases);
  108. }
  109. // Update the selected source (thus triggering an update).
  110. _update_sources_list(source_id);
  111. }
  112. void TileSetEditor::_update_sources_list(int force_selected_id) {
  113. if (tile_set.is_null()) {
  114. return;
  115. }
  116. // Get the previously selected id.
  117. int old_selected = TileSet::INVALID_SOURCE;
  118. if (sources_list->get_current() >= 0) {
  119. int source_id = sources_list->get_item_metadata(sources_list->get_current());
  120. if (tile_set->has_source(source_id)) {
  121. old_selected = source_id;
  122. }
  123. }
  124. int to_select = TileSet::INVALID_SOURCE;
  125. if (force_selected_id >= 0) {
  126. to_select = force_selected_id;
  127. } else if (old_selected >= 0) {
  128. to_select = old_selected;
  129. }
  130. // Clear the list.
  131. sources_list->clear();
  132. // Update the atlas sources.
  133. List<int> source_ids = TilesEditorUtils::get_singleton()->get_sorted_sources(tile_set);
  134. for (const int &source_id : source_ids) {
  135. TileSetSource *source = *tile_set->get_source(source_id);
  136. Ref<Texture2D> texture;
  137. String item_text;
  138. // Common to all type of sources.
  139. if (!source->get_name().is_empty()) {
  140. item_text = source->get_name();
  141. }
  142. // Atlas source.
  143. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  144. if (atlas_source) {
  145. texture = atlas_source->get_texture();
  146. if (item_text.is_empty()) {
  147. if (texture.is_valid()) {
  148. item_text = texture->get_path().get_file();
  149. } else {
  150. item_text = vformat(TTR("No Texture Atlas Source (ID: %d)"), source_id);
  151. }
  152. }
  153. }
  154. // Scene collection source.
  155. TileSetScenesCollectionSource *scene_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  156. if (scene_collection_source) {
  157. texture = get_editor_theme_icon(SNAME("PackedScene"));
  158. if (item_text.is_empty()) {
  159. if (scene_collection_source->get_scene_tiles_count() > 0) {
  160. item_text = vformat(TTR("Scene Collection Source (ID: %d)"), source_id);
  161. } else {
  162. item_text = vformat(TTR("Empty Scene Collection Source (ID: %d)"), source_id);
  163. }
  164. }
  165. }
  166. // Use default if not valid.
  167. if (item_text.is_empty()) {
  168. item_text = vformat(TTR("Unknown Type Source (ID: %d)"), source_id);
  169. }
  170. if (!texture.is_valid()) {
  171. texture = missing_texture_texture;
  172. }
  173. sources_list->add_item(item_text, texture);
  174. sources_list->set_item_metadata(-1, source_id);
  175. }
  176. // Set again the current selected item if needed.
  177. if (to_select >= 0) {
  178. for (int i = 0; i < sources_list->get_item_count(); i++) {
  179. if ((int)sources_list->get_item_metadata(i) == to_select) {
  180. sources_list->set_current(i);
  181. sources_list->ensure_current_is_visible();
  182. if (old_selected != to_select) {
  183. sources_list->emit_signal(SceneStringName(item_selected), sources_list->get_current());
  184. }
  185. break;
  186. }
  187. }
  188. }
  189. // If nothing is selected, select the first entry.
  190. if (sources_list->get_current() < 0 && sources_list->get_item_count() > 0) {
  191. sources_list->set_current(0);
  192. if (old_selected != int(sources_list->get_item_metadata(0))) {
  193. sources_list->emit_signal(SceneStringName(item_selected), sources_list->get_current());
  194. }
  195. }
  196. // If there is no source left, hide all editors and show the label.
  197. _source_selected(sources_list->get_current());
  198. // Synchronize the lists.
  199. TilesEditorUtils::get_singleton()->set_sources_lists_current(sources_list->get_current());
  200. }
  201. void TileSetEditor::_source_selected(int p_source_index) {
  202. ERR_FAIL_COND(!tile_set.is_valid());
  203. // Update the selected source.
  204. sources_delete_button->set_disabled(p_source_index < 0 || read_only);
  205. if (p_source_index >= 0) {
  206. int source_id = sources_list->get_item_metadata(p_source_index);
  207. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id));
  208. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(*tile_set->get_source(source_id));
  209. if (atlas_source) {
  210. no_source_selected_label->hide();
  211. tile_set_atlas_source_editor->edit(*tile_set, atlas_source, source_id);
  212. tile_set_atlas_source_editor->show();
  213. tile_set_scenes_collection_source_editor->hide();
  214. } else if (scenes_collection_source) {
  215. no_source_selected_label->hide();
  216. tile_set_atlas_source_editor->hide();
  217. tile_set_scenes_collection_source_editor->edit(*tile_set, scenes_collection_source, source_id);
  218. tile_set_scenes_collection_source_editor->show();
  219. } else {
  220. no_source_selected_label->show();
  221. tile_set_atlas_source_editor->hide();
  222. tile_set_scenes_collection_source_editor->hide();
  223. }
  224. } else {
  225. no_source_selected_label->show();
  226. tile_set_atlas_source_editor->hide();
  227. tile_set_scenes_collection_source_editor->hide();
  228. }
  229. }
  230. void TileSetEditor::_source_delete_pressed() {
  231. ERR_FAIL_COND(!tile_set.is_valid());
  232. // Update the selected source.
  233. int to_delete = sources_list->get_item_metadata(sources_list->get_current());
  234. Ref<TileSetSource> source = tile_set->get_source(to_delete);
  235. // Remove the source.
  236. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  237. undo_redo->create_action(TTR("Remove source"));
  238. undo_redo->add_do_method(*tile_set, "remove_source", to_delete);
  239. undo_redo->add_undo_method(*tile_set, "add_source", source, to_delete);
  240. undo_redo->commit_action();
  241. _update_sources_list();
  242. }
  243. void TileSetEditor::_source_add_id_pressed(int p_id_pressed) {
  244. ERR_FAIL_COND(!tile_set.is_valid());
  245. switch (p_id_pressed) {
  246. case 0: {
  247. if (!texture_file_dialog) {
  248. texture_file_dialog = memnew(EditorFileDialog);
  249. add_child(texture_file_dialog);
  250. texture_file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  251. texture_file_dialog->connect("files_selected", callable_mp(this, &TileSetEditor::_load_texture_files));
  252. List<String> extensions;
  253. ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);
  254. for (const String &E : extensions) {
  255. texture_file_dialog->add_filter("*." + E, E.to_upper());
  256. }
  257. }
  258. texture_file_dialog->popup_file_dialog();
  259. } break;
  260. case 1: {
  261. int source_id = tile_set->get_next_source_id();
  262. Ref<TileSetScenesCollectionSource> scene_collection_source = memnew(TileSetScenesCollectionSource);
  263. // Add a new source.
  264. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  265. undo_redo->create_action(TTR("Add atlas source"));
  266. undo_redo->add_do_method(*tile_set, "add_source", scene_collection_source, source_id);
  267. undo_redo->add_undo_method(*tile_set, "remove_source", source_id);
  268. undo_redo->commit_action();
  269. _update_sources_list(source_id);
  270. } break;
  271. default:
  272. ERR_FAIL();
  273. }
  274. }
  275. void TileSetEditor::_sources_advanced_menu_id_pressed(int p_id_pressed) {
  276. ERR_FAIL_COND(!tile_set.is_valid());
  277. switch (p_id_pressed) {
  278. case 0: {
  279. atlas_merging_dialog->update_tile_set(tile_set);
  280. atlas_merging_dialog->popup_centered_ratio(0.5);
  281. } break;
  282. case 1: {
  283. tile_proxies_manager_dialog->update_tile_set(tile_set);
  284. tile_proxies_manager_dialog->popup_centered_ratio(0.5);
  285. } break;
  286. }
  287. }
  288. void TileSetEditor::_set_source_sort(int p_sort) {
  289. TilesEditorUtils::get_singleton()->set_sorting_option(p_sort);
  290. for (int i = 0; i != TilesEditorUtils::SOURCE_SORT_MAX; i++) {
  291. source_sort_button->get_popup()->set_item_checked(i, (i == (int)p_sort));
  292. }
  293. int old_selected = TileSet::INVALID_SOURCE;
  294. if (sources_list->get_current() >= 0) {
  295. int source_id = sources_list->get_item_metadata(sources_list->get_current());
  296. if (tile_set->has_source(source_id)) {
  297. old_selected = source_id;
  298. }
  299. }
  300. _update_sources_list(old_selected);
  301. if (!first_edit) {
  302. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "tile_source_sort", p_sort);
  303. }
  304. }
  305. void TileSetEditor::_notification(int p_what) {
  306. switch (p_what) {
  307. case NOTIFICATION_THEME_CHANGED: {
  308. sources_delete_button->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  309. sources_add_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  310. source_sort_button->set_button_icon(get_editor_theme_icon(SNAME("Sort")));
  311. sources_advanced_menu_button->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
  312. missing_texture_texture = get_editor_theme_icon(SNAME("TileSet"));
  313. expanded_area->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), "Tree"));
  314. _update_sources_list();
  315. } break;
  316. case NOTIFICATION_INTERNAL_PROCESS: {
  317. if (tile_set_changed_needs_update) {
  318. if (tile_set.is_valid()) {
  319. tile_set->set_edited(true);
  320. }
  321. read_only = false;
  322. if (tile_set.is_valid()) {
  323. read_only = EditorNode::get_singleton()->is_resource_read_only(tile_set);
  324. }
  325. _update_sources_list();
  326. _update_patterns_list();
  327. sources_add_button->set_disabled(read_only);
  328. sources_advanced_menu_button->set_disabled(read_only);
  329. source_sort_button->set_disabled(read_only);
  330. tile_set_changed_needs_update = false;
  331. }
  332. } break;
  333. case NOTIFICATION_VISIBILITY_CHANGED: {
  334. if (!is_visible_in_tree()) {
  335. remove_expanded_editor();
  336. }
  337. } break;
  338. }
  339. }
  340. void TileSetEditor::_patterns_item_list_gui_input(const Ref<InputEvent> &p_event) {
  341. ERR_FAIL_COND(!tile_set.is_valid());
  342. if (EditorNode::get_singleton()->is_resource_read_only(tile_set)) {
  343. return;
  344. }
  345. if (ED_IS_SHORTCUT("tiles_editor/delete", p_event) && p_event->is_pressed() && !p_event->is_echo()) {
  346. Vector<int> selected = patterns_item_list->get_selected_items();
  347. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  348. undo_redo->create_action(TTR("Remove TileSet patterns"));
  349. for (int i = 0; i < selected.size(); i++) {
  350. int pattern_index = selected[i];
  351. undo_redo->add_do_method(*tile_set, "remove_pattern", pattern_index);
  352. undo_redo->add_undo_method(*tile_set, "add_pattern", tile_set->get_pattern(pattern_index), pattern_index);
  353. }
  354. undo_redo->commit_action();
  355. patterns_item_list->accept_event();
  356. }
  357. }
  358. void TileSetEditor::_pattern_preview_done(Ref<TileMapPattern> p_pattern, Ref<Texture2D> p_texture) {
  359. // TODO optimize ?
  360. for (int i = 0; i < patterns_item_list->get_item_count(); i++) {
  361. if (patterns_item_list->get_item_metadata(i) == p_pattern) {
  362. patterns_item_list->set_item_icon(i, p_texture);
  363. break;
  364. }
  365. }
  366. }
  367. void TileSetEditor::_update_patterns_list() {
  368. ERR_FAIL_COND(!tile_set.is_valid());
  369. // Recreate the items.
  370. patterns_item_list->clear();
  371. for (int i = 0; i < tile_set->get_patterns_count(); i++) {
  372. int id = patterns_item_list->add_item("");
  373. patterns_item_list->set_item_metadata(id, tile_set->get_pattern(i));
  374. patterns_item_list->set_item_tooltip(id, vformat(TTR("Index: %d"), i));
  375. TilesEditorUtils::get_singleton()->queue_pattern_preview(tile_set, tile_set->get_pattern(i), callable_mp(this, &TileSetEditor::_pattern_preview_done));
  376. }
  377. // Update the label visibility.
  378. patterns_help_label->set_visible(patterns_item_list->get_item_count() == 0);
  379. }
  380. void TileSetEditor::_tile_set_changed() {
  381. tile_set_changed_needs_update = true;
  382. }
  383. void TileSetEditor::_tab_changed(int p_tab_changed) {
  384. split_container->set_visible(p_tab_changed == 0);
  385. patterns_item_list->set_visible(p_tab_changed == 1);
  386. }
  387. void TileSetEditor::_move_tile_set_array_element(Object *p_undo_redo, Object *p_edited, const String &p_array_prefix, int p_from_index, int p_to_pos) {
  388. EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
  389. ERR_FAIL_NULL(undo_redo_man);
  390. TileSet *ed_tile_set = Object::cast_to<TileSet>(p_edited);
  391. if (!ed_tile_set) {
  392. return;
  393. }
  394. Vector<String> components = String(p_array_prefix).split("/", true, 2);
  395. // Compute the array indices to save.
  396. int begin = 0;
  397. int end;
  398. if (p_array_prefix == "occlusion_layer_") {
  399. end = ed_tile_set->get_occlusion_layers_count();
  400. } else if (p_array_prefix == "physics_layer_") {
  401. end = ed_tile_set->get_physics_layers_count();
  402. } else if (p_array_prefix == "terrain_set_") {
  403. end = ed_tile_set->get_terrain_sets_count();
  404. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {
  405. int terrain_set = components[0].trim_prefix("terrain_set_").to_int();
  406. end = ed_tile_set->get_terrains_count(terrain_set);
  407. } else if (p_array_prefix == "navigation_layer_") {
  408. end = ed_tile_set->get_navigation_layers_count();
  409. } else if (p_array_prefix == "custom_data_layer_") {
  410. end = ed_tile_set->get_custom_data_layers_count();
  411. } else {
  412. ERR_FAIL_MSG("Invalid array prefix for TileSet.");
  413. }
  414. if (p_from_index < 0) {
  415. // Adding new.
  416. if (p_to_pos >= 0) {
  417. begin = p_to_pos;
  418. } else {
  419. end = 0; // Nothing to save when adding at the end.
  420. }
  421. } else if (p_to_pos < 0) {
  422. // Removing.
  423. begin = p_from_index;
  424. } else {
  425. // Moving.
  426. begin = MIN(p_from_index, p_to_pos);
  427. end = MIN(MAX(p_from_index, p_to_pos) + 1, end);
  428. }
  429. #define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property));
  430. // Add undo method to adding array element.
  431. if (p_array_prefix == "occlusion_layer_") {
  432. if (p_from_index < 0) {
  433. undo_redo_man->add_undo_method(ed_tile_set, "remove_occlusion_layer", p_to_pos < 0 ? ed_tile_set->get_occlusion_layers_count() : p_to_pos);
  434. }
  435. } else if (p_array_prefix == "physics_layer_") {
  436. if (p_from_index < 0) {
  437. undo_redo_man->add_undo_method(ed_tile_set, "remove_physics_layer", p_to_pos < 0 ? ed_tile_set->get_physics_layers_count() : p_to_pos);
  438. }
  439. } else if (p_array_prefix == "terrain_set_") {
  440. if (p_from_index < 0) {
  441. undo_redo_man->add_undo_method(ed_tile_set, "remove_terrain_set", p_to_pos < 0 ? ed_tile_set->get_terrain_sets_count() : p_to_pos);
  442. }
  443. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {
  444. int terrain_set = components[0].trim_prefix("terrain_set_").to_int();
  445. if (p_from_index < 0) {
  446. undo_redo_man->add_undo_method(ed_tile_set, "remove_terrain", terrain_set, p_to_pos < 0 ? ed_tile_set->get_terrains_count(terrain_set) : p_to_pos);
  447. }
  448. } else if (p_array_prefix == "navigation_layer_") {
  449. if (p_from_index < 0) {
  450. undo_redo_man->add_undo_method(ed_tile_set, "remove_navigation_layer", p_to_pos < 0 ? ed_tile_set->get_navigation_layers_count() : p_to_pos);
  451. }
  452. } else if (p_array_prefix == "custom_data_layer_") {
  453. if (p_from_index < 0) {
  454. undo_redo_man->add_undo_method(ed_tile_set, "remove_custom_data_layer", p_to_pos < 0 ? ed_tile_set->get_custom_data_layers_count() : p_to_pos);
  455. }
  456. }
  457. // Save layers' properties.
  458. List<PropertyInfo> properties;
  459. ed_tile_set->get_property_list(&properties);
  460. for (PropertyInfo pi : properties) {
  461. if (pi.name.begins_with(p_array_prefix)) {
  462. String str = pi.name.trim_prefix(p_array_prefix);
  463. int to_char_index = 0;
  464. while (to_char_index < str.length()) {
  465. if (!is_digit(str[to_char_index])) {
  466. break;
  467. }
  468. to_char_index++;
  469. }
  470. if (to_char_index > 0) {
  471. int array_index = str.left(to_char_index).to_int();
  472. if (array_index >= begin && array_index < end) {
  473. ADD_UNDO(ed_tile_set, pi.name);
  474. }
  475. }
  476. }
  477. }
  478. // Save properties for TileSetAtlasSources tile data
  479. for (int i = 0; i < ed_tile_set->get_source_count(); i++) {
  480. int source_id = ed_tile_set->get_source_id(i);
  481. Ref<TileSetAtlasSource> tas = ed_tile_set->get_source(source_id);
  482. if (tas.is_valid()) {
  483. for (int j = 0; j < tas->get_tiles_count(); j++) {
  484. Vector2i tile_id = tas->get_tile_id(j);
  485. for (int k = 0; k < tas->get_alternative_tiles_count(tile_id); k++) {
  486. int alternative_id = tas->get_alternative_tile_id(tile_id, k);
  487. TileData *tile_data = tas->get_tile_data(tile_id, alternative_id);
  488. ERR_FAIL_NULL(tile_data);
  489. // Actually saving stuff.
  490. if (p_array_prefix == "occlusion_layer_") {
  491. for (int layer_index = begin; layer_index < end; layer_index++) {
  492. ADD_UNDO(tile_data, vformat("occlusion_layer_%d/polygon", layer_index));
  493. }
  494. } else if (p_array_prefix == "physics_layer_") {
  495. for (int layer_index = begin; layer_index < end; layer_index++) {
  496. ADD_UNDO(tile_data, vformat("physics_layer_%d/polygons_count", layer_index));
  497. for (int polygon_index = 0; polygon_index < tile_data->get_collision_polygons_count(layer_index); polygon_index++) {
  498. ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/points", layer_index, polygon_index));
  499. ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/one_way", layer_index, polygon_index));
  500. ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/one_way_margin", layer_index, polygon_index));
  501. }
  502. }
  503. } else if (p_array_prefix == "terrain_set_") {
  504. ADD_UNDO(tile_data, "terrain_set");
  505. for (int terrain_set_index = begin; terrain_set_index < end; terrain_set_index++) {
  506. for (int l = 0; l < TileSet::CELL_NEIGHBOR_MAX; l++) {
  507. TileSet::CellNeighbor bit = TileSet::CellNeighbor(l);
  508. if (tile_data->is_valid_terrain_peering_bit(bit)) {
  509. ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[l]));
  510. }
  511. }
  512. }
  513. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {
  514. for (int terrain_index = 0; terrain_index < TileSet::CELL_NEIGHBOR_MAX; terrain_index++) {
  515. TileSet::CellNeighbor bit = TileSet::CellNeighbor(terrain_index);
  516. if (tile_data->is_valid_terrain_peering_bit(bit)) {
  517. ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[terrain_index]));
  518. }
  519. }
  520. } else if (p_array_prefix == "navigation_layer_") {
  521. for (int layer_index = begin; layer_index < end; layer_index++) {
  522. ADD_UNDO(tile_data, vformat("navigation_layer_%d/polygon", layer_index));
  523. }
  524. } else if (p_array_prefix == "custom_data_layer_") {
  525. for (int layer_index = begin; layer_index < end; layer_index++) {
  526. ADD_UNDO(tile_data, vformat("custom_data_%d", layer_index));
  527. }
  528. }
  529. }
  530. }
  531. }
  532. }
  533. #undef ADD_UNDO
  534. // Add do method to add/remove array element.
  535. if (p_array_prefix == "occlusion_layer_") {
  536. if (p_from_index < 0) {
  537. undo_redo_man->add_do_method(ed_tile_set, "add_occlusion_layer", p_to_pos);
  538. } else if (p_to_pos < 0) {
  539. undo_redo_man->add_do_method(ed_tile_set, "remove_occlusion_layer", p_from_index);
  540. } else {
  541. undo_redo_man->add_do_method(ed_tile_set, "move_occlusion_layer", p_from_index, p_to_pos);
  542. }
  543. } else if (p_array_prefix == "physics_layer_") {
  544. if (p_from_index < 0) {
  545. undo_redo_man->add_do_method(ed_tile_set, "add_physics_layer", p_to_pos);
  546. } else if (p_to_pos < 0) {
  547. undo_redo_man->add_do_method(ed_tile_set, "remove_physics_layer", p_from_index);
  548. } else {
  549. undo_redo_man->add_do_method(ed_tile_set, "move_physics_layer", p_from_index, p_to_pos);
  550. }
  551. } else if (p_array_prefix == "terrain_set_") {
  552. if (p_from_index < 0) {
  553. undo_redo_man->add_do_method(ed_tile_set, "add_terrain_set", p_to_pos);
  554. } else if (p_to_pos < 0) {
  555. undo_redo_man->add_do_method(ed_tile_set, "remove_terrain_set", p_from_index);
  556. } else {
  557. undo_redo_man->add_do_method(ed_tile_set, "move_terrain_set", p_from_index, p_to_pos);
  558. }
  559. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {
  560. int terrain_set = components[0].trim_prefix("terrain_set_").to_int();
  561. if (p_from_index < 0) {
  562. undo_redo_man->add_do_method(ed_tile_set, "add_terrain", terrain_set, p_to_pos);
  563. } else if (p_to_pos < 0) {
  564. undo_redo_man->add_do_method(ed_tile_set, "remove_terrain", terrain_set, p_from_index);
  565. } else {
  566. undo_redo_man->add_do_method(ed_tile_set, "move_terrain", terrain_set, p_from_index, p_to_pos);
  567. }
  568. } else if (p_array_prefix == "navigation_layer_") {
  569. if (p_from_index < 0) {
  570. undo_redo_man->add_do_method(ed_tile_set, "add_navigation_layer", p_to_pos);
  571. } else if (p_to_pos < 0) {
  572. undo_redo_man->add_do_method(ed_tile_set, "remove_navigation_layer", p_from_index);
  573. } else {
  574. undo_redo_man->add_do_method(ed_tile_set, "move_navigation_layer", p_from_index, p_to_pos);
  575. }
  576. } else if (p_array_prefix == "custom_data_layer_") {
  577. if (p_from_index < 0) {
  578. undo_redo_man->add_do_method(ed_tile_set, "add_custom_data_layer", p_to_pos);
  579. } else if (p_to_pos < 0) {
  580. undo_redo_man->add_do_method(ed_tile_set, "remove_custom_data_layer", p_from_index);
  581. } else {
  582. undo_redo_man->add_do_method(ed_tile_set, "move_custom_data_layer", p_from_index, p_to_pos);
  583. }
  584. }
  585. }
  586. void TileSetEditor::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, const String &p_property, const Variant &p_new_value) {
  587. EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
  588. ERR_FAIL_NULL(undo_redo_man);
  589. #define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property));
  590. TileSet *ed_tile_set = Object::cast_to<TileSet>(p_edited);
  591. if (ed_tile_set) {
  592. Vector<String> components = p_property.split("/", true, 3);
  593. for (int i = 0; i < ed_tile_set->get_source_count(); i++) {
  594. int source_id = ed_tile_set->get_source_id(i);
  595. Ref<TileSetAtlasSource> tas = ed_tile_set->get_source(source_id);
  596. if (tas.is_valid()) {
  597. for (int j = 0; j < tas->get_tiles_count(); j++) {
  598. Vector2i tile_id = tas->get_tile_id(j);
  599. for (int k = 0; k < tas->get_alternative_tiles_count(tile_id); k++) {
  600. int alternative_id = tas->get_alternative_tile_id(tile_id, k);
  601. TileData *tile_data = tas->get_tile_data(tile_id, alternative_id);
  602. ERR_FAIL_NULL(tile_data);
  603. if (components.size() == 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "mode") {
  604. ADD_UNDO(tile_data, "terrain_set");
  605. ADD_UNDO(tile_data, "terrain");
  606. for (int l = 0; l < TileSet::CELL_NEIGHBOR_MAX; l++) {
  607. TileSet::CellNeighbor bit = TileSet::CellNeighbor(l);
  608. if (tile_data->is_valid_terrain_peering_bit(bit)) {
  609. ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[l]));
  610. }
  611. }
  612. } else if (components.size() == 2 && components[0].begins_with("custom_data_layer_") && components[0].trim_prefix("custom_data_layer_").is_valid_int() && components[1] == "type") {
  613. int custom_data_layer = components[0].trim_prefix("custom_data_layer_").is_valid_int();
  614. ADD_UNDO(tile_data, vformat("custom_data_%d", custom_data_layer));
  615. }
  616. }
  617. }
  618. }
  619. }
  620. }
  621. #undef ADD_UNDO
  622. }
  623. void TileSetEditor::edit(Ref<TileSet> p_tile_set) {
  624. bool new_read_only_state = false;
  625. if (p_tile_set.is_valid()) {
  626. new_read_only_state = EditorNode::get_singleton()->is_resource_read_only(p_tile_set);
  627. }
  628. if (p_tile_set == tile_set && new_read_only_state == read_only) {
  629. return;
  630. }
  631. // Remove listener.
  632. if (tile_set.is_valid()) {
  633. tile_set->disconnect_changed(callable_mp(this, &TileSetEditor::_tile_set_changed));
  634. }
  635. // Change the edited object.
  636. tile_set = p_tile_set;
  637. // Read-only status is false by default
  638. read_only = new_read_only_state;
  639. // Add the listener again and check for read-only status.
  640. if (tile_set.is_valid()) {
  641. sources_add_button->set_disabled(read_only);
  642. sources_advanced_menu_button->set_disabled(read_only);
  643. source_sort_button->set_disabled(read_only);
  644. tile_set->connect_changed(callable_mp(this, &TileSetEditor::_tile_set_changed));
  645. if (first_edit) {
  646. _set_source_sort(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "tile_source_sort", 0));
  647. first_edit = false;
  648. } else {
  649. _update_sources_list();
  650. }
  651. _update_patterns_list();
  652. }
  653. }
  654. void TileSetEditor::add_expanded_editor(Control *p_editor) {
  655. expanded_editor = p_editor;
  656. expanded_editor_parent = p_editor->get_parent()->get_instance_id();
  657. // Find the scrollable control this node belongs to.
  658. Node *check_parent = expanded_editor->get_parent();
  659. Control *parent_container = nullptr;
  660. while (check_parent) {
  661. parent_container = Object::cast_to<EditorInspector>(check_parent);
  662. if (parent_container) {
  663. break;
  664. }
  665. parent_container = Object::cast_to<ScrollContainer>(check_parent);
  666. if (parent_container) {
  667. break;
  668. }
  669. check_parent = check_parent->get_parent();
  670. }
  671. ERR_FAIL_NULL(parent_container);
  672. expanded_editor->set_meta("reparented", true);
  673. expanded_editor->reparent(expanded_area);
  674. expanded_area->show();
  675. expanded_area->set_size(Vector2(parent_container->get_global_rect().get_end().x - expanded_area->get_global_position().x, expanded_area->get_size().y));
  676. for (SplitContainer *split : disable_on_expand) {
  677. split->set_dragger_visibility(SplitContainer::DRAGGER_HIDDEN);
  678. }
  679. }
  680. void TileSetEditor::remove_expanded_editor() {
  681. if (!expanded_editor) {
  682. return;
  683. }
  684. Node *original_parent = Object::cast_to<Node>(ObjectDB::get_instance(expanded_editor_parent));
  685. if (original_parent) {
  686. expanded_editor->remove_meta("reparented");
  687. expanded_editor->reparent(original_parent);
  688. } else {
  689. expanded_editor->queue_free();
  690. }
  691. expanded_editor = nullptr;
  692. expanded_editor_parent = ObjectID();
  693. expanded_area->hide();
  694. for (SplitContainer *split : disable_on_expand) {
  695. split->set_dragger_visibility(SplitContainer::DRAGGER_VISIBLE);
  696. }
  697. }
  698. void TileSetEditor::register_split(SplitContainer *p_split) {
  699. disable_on_expand.push_back(p_split);
  700. }
  701. TileSetEditor::TileSetEditor() {
  702. singleton = this;
  703. set_process_internal(true);
  704. VBoxContainer *main_vb = memnew(VBoxContainer);
  705. add_child(main_vb);
  706. main_vb->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  707. // TabBar.
  708. tabs_bar = memnew(TabBar);
  709. tabs_bar->set_tab_alignment(TabBar::ALIGNMENT_CENTER);
  710. tabs_bar->set_clip_tabs(false);
  711. tabs_bar->add_tab(TTR("Tile Sources"));
  712. tabs_bar->add_tab(TTR("Patterns"));
  713. tabs_bar->connect("tab_changed", callable_mp(this, &TileSetEditor::_tab_changed));
  714. tile_set_toolbar = memnew(HBoxContainer);
  715. tile_set_toolbar->set_h_size_flags(SIZE_EXPAND_FILL);
  716. tile_set_toolbar->add_child(tabs_bar);
  717. main_vb->add_child(tile_set_toolbar);
  718. //// Tiles ////
  719. // Split container.
  720. split_container = memnew(HSplitContainer);
  721. split_container->set_name(TTR("Tiles"));
  722. split_container->set_h_size_flags(SIZE_EXPAND_FILL);
  723. split_container->set_v_size_flags(SIZE_EXPAND_FILL);
  724. main_vb->add_child(split_container);
  725. // Sources list.
  726. VBoxContainer *split_container_left_side = memnew(VBoxContainer);
  727. split_container_left_side->set_h_size_flags(SIZE_EXPAND_FILL);
  728. split_container_left_side->set_v_size_flags(SIZE_EXPAND_FILL);
  729. split_container_left_side->set_stretch_ratio(0.25);
  730. split_container_left_side->set_custom_minimum_size(Size2(70, 0) * EDSCALE);
  731. split_container->add_child(split_container_left_side);
  732. source_sort_button = memnew(MenuButton);
  733. source_sort_button->set_flat(false);
  734. source_sort_button->set_theme_type_variation("FlatButton");
  735. source_sort_button->set_tooltip_text(TTR("Sort Sources"));
  736. PopupMenu *p = source_sort_button->get_popup();
  737. p->connect(SceneStringName(id_pressed), callable_mp(this, &TileSetEditor::_set_source_sort));
  738. p->add_radio_check_item(TTR("Sort by ID (Ascending)"), TilesEditorUtils::SOURCE_SORT_ID);
  739. p->add_radio_check_item(TTR("Sort by ID (Descending)"), TilesEditorUtils::SOURCE_SORT_ID_REVERSE);
  740. p->add_radio_check_item(TTR("Sort by Name (Ascending)"), TilesEditorUtils::SOURCE_SORT_NAME);
  741. p->add_radio_check_item(TTR("Sort by Name (Descending)"), TilesEditorUtils::SOURCE_SORT_NAME_REVERSE);
  742. p->set_item_checked(TilesEditorUtils::SOURCE_SORT_ID, true);
  743. sources_list = memnew(ItemList);
  744. sources_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  745. sources_list->set_fixed_icon_size(Size2(60, 60) * EDSCALE);
  746. sources_list->set_h_size_flags(SIZE_EXPAND_FILL);
  747. sources_list->set_v_size_flags(SIZE_EXPAND_FILL);
  748. sources_list->connect(SceneStringName(item_selected), callable_mp(this, &TileSetEditor::_source_selected));
  749. sources_list->connect(SceneStringName(item_selected), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::set_sources_lists_current));
  750. sources_list->connect(SceneStringName(visibility_changed), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::synchronize_sources_list).bind(sources_list, source_sort_button));
  751. sources_list->add_user_signal(MethodInfo("sort_request"));
  752. sources_list->connect("sort_request", callable_mp(this, &TileSetEditor::_update_sources_list).bind(-1));
  753. sources_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  754. SET_DRAG_FORWARDING_CDU(sources_list, TileSetEditor);
  755. split_container_left_side->add_child(sources_list);
  756. HBoxContainer *sources_bottom_actions = memnew(HBoxContainer);
  757. sources_bottom_actions->set_alignment(BoxContainer::ALIGNMENT_END);
  758. split_container_left_side->add_child(sources_bottom_actions);
  759. sources_delete_button = memnew(Button);
  760. sources_delete_button->set_theme_type_variation("FlatButton");
  761. sources_delete_button->set_disabled(true);
  762. sources_delete_button->connect(SceneStringName(pressed), callable_mp(this, &TileSetEditor::_source_delete_pressed));
  763. sources_bottom_actions->add_child(sources_delete_button);
  764. sources_add_button = memnew(MenuButton);
  765. sources_add_button->set_flat(false);
  766. sources_add_button->set_theme_type_variation("FlatButton");
  767. sources_add_button->get_popup()->add_item(TTR("Atlas"));
  768. sources_add_button->get_popup()->set_item_tooltip(-1, TTR("A palette of tiles made from a texture."));
  769. sources_add_button->get_popup()->add_item(TTR("Scenes Collection"));
  770. sources_add_button->get_popup()->set_item_tooltip(-1, TTR("A collection of scenes that can be instantiated and placed as tiles."));
  771. sources_add_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TileSetEditor::_source_add_id_pressed));
  772. sources_bottom_actions->add_child(sources_add_button);
  773. sources_advanced_menu_button = memnew(MenuButton);
  774. sources_advanced_menu_button->set_flat(false);
  775. sources_advanced_menu_button->set_theme_type_variation("FlatButton");
  776. sources_advanced_menu_button->get_popup()->add_item(TTR("Open Atlas Merging Tool"));
  777. sources_advanced_menu_button->get_popup()->add_item(TTR("Manage Tile Proxies"));
  778. sources_advanced_menu_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TileSetEditor::_sources_advanced_menu_id_pressed));
  779. sources_bottom_actions->add_child(sources_advanced_menu_button);
  780. sources_bottom_actions->add_child(source_sort_button);
  781. atlas_merging_dialog = memnew(AtlasMergingDialog);
  782. add_child(atlas_merging_dialog);
  783. tile_proxies_manager_dialog = memnew(TileProxiesManagerDialog);
  784. add_child(tile_proxies_manager_dialog);
  785. // Right side container.
  786. VBoxContainer *split_container_right_side = memnew(VBoxContainer);
  787. split_container_right_side->set_h_size_flags(SIZE_EXPAND_FILL);
  788. split_container_right_side->set_v_size_flags(SIZE_EXPAND_FILL);
  789. split_container->add_child(split_container_right_side);
  790. // No source selected.
  791. no_source_selected_label = memnew(Label);
  792. no_source_selected_label->set_text(TTR("No TileSet source selected. Select or create a TileSet source.\nYou can create a new source by using the Add button on the left or by dropping a tileset texture onto the source list."));
  793. no_source_selected_label->set_h_size_flags(SIZE_EXPAND_FILL);
  794. no_source_selected_label->set_v_size_flags(SIZE_EXPAND_FILL);
  795. no_source_selected_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  796. no_source_selected_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  797. split_container_right_side->add_child(no_source_selected_label);
  798. // Atlases editor.
  799. tile_set_atlas_source_editor = memnew(TileSetAtlasSourceEditor);
  800. tile_set_atlas_source_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  801. tile_set_atlas_source_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  802. tile_set_atlas_source_editor->connect("source_id_changed", callable_mp(this, &TileSetEditor::_update_sources_list));
  803. split_container_right_side->add_child(tile_set_atlas_source_editor);
  804. tile_set_atlas_source_editor->hide();
  805. // Scenes collection editor.
  806. tile_set_scenes_collection_source_editor = memnew(TileSetScenesCollectionSourceEditor);
  807. tile_set_scenes_collection_source_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  808. tile_set_scenes_collection_source_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  809. tile_set_scenes_collection_source_editor->connect("source_id_changed", callable_mp(this, &TileSetEditor::_update_sources_list));
  810. split_container_right_side->add_child(tile_set_scenes_collection_source_editor);
  811. tile_set_scenes_collection_source_editor->hide();
  812. //// Patterns ////
  813. int thumbnail_size = 64;
  814. patterns_item_list = memnew(ItemList);
  815. patterns_item_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  816. patterns_item_list->set_max_columns(0);
  817. patterns_item_list->set_icon_mode(ItemList::ICON_MODE_TOP);
  818. patterns_item_list->set_fixed_column_width(thumbnail_size * 3 / 2);
  819. patterns_item_list->set_max_text_lines(2);
  820. patterns_item_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  821. patterns_item_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  822. patterns_item_list->connect(SceneStringName(gui_input), callable_mp(this, &TileSetEditor::_patterns_item_list_gui_input));
  823. main_vb->add_child(patterns_item_list);
  824. patterns_item_list->hide();
  825. patterns_help_label = memnew(Label);
  826. patterns_help_label->set_text(TTR("Add new patterns in the TileMap editing mode."));
  827. patterns_help_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  828. patterns_help_label->set_anchors_and_offsets_preset(Control::PRESET_CENTER);
  829. patterns_item_list->add_child(patterns_help_label);
  830. // Expanded editor
  831. expanded_area = memnew(PanelContainer);
  832. add_child(expanded_area);
  833. expanded_area->set_anchors_and_offsets_preset(PRESET_LEFT_WIDE);
  834. expanded_area->hide();
  835. // Registers UndoRedo inspector callback.
  836. EditorNode::get_editor_data().add_move_array_element_function(SNAME("TileSet"), callable_mp(this, &TileSetEditor::_move_tile_set_array_element));
  837. EditorNode::get_editor_data().add_undo_redo_inspector_hook_callback(callable_mp(this, &TileSetEditor::_undo_redo_inspector_callback));
  838. }
  839. void TileSourceInspectorPlugin::_show_id_edit_dialog(Object *p_for_source) {
  840. if (!id_edit_dialog) {
  841. id_edit_dialog = memnew(ConfirmationDialog);
  842. TileSetEditor::get_singleton()->add_child(id_edit_dialog);
  843. VBoxContainer *vbox = memnew(VBoxContainer);
  844. id_edit_dialog->add_child(vbox);
  845. Label *label = memnew(Label(TTR("Warning: Modifying a source ID will result in all TileMaps using that source to reference an invalid source instead. This may result in unexpected data loss. Change this ID carefully.")));
  846. label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  847. vbox->add_child(label);
  848. id_input = memnew(SpinBox);
  849. vbox->add_child(id_input);
  850. id_input->set_max(INT_MAX);
  851. id_edit_dialog->connect(SceneStringName(confirmed), callable_mp(this, &TileSourceInspectorPlugin::_confirm_change_id));
  852. }
  853. edited_source = p_for_source;
  854. id_input->set_value(p_for_source->get("id"));
  855. id_edit_dialog->popup_centered(Vector2i(400, 0) * EDSCALE);
  856. callable_mp((Control *)id_input->get_line_edit(), &Control::grab_focus).call_deferred();
  857. }
  858. void TileSourceInspectorPlugin::_confirm_change_id() {
  859. edited_source->set("id", id_input->get_value());
  860. id_label->set_text(itos(edited_source->get("id"))); // Use get(), because the provided ID might've been invalid.
  861. }
  862. bool TileSourceInspectorPlugin::can_handle(Object *p_object) {
  863. return p_object->is_class("TileSetAtlasSourceProxyObject") || p_object->is_class("TileSetScenesCollectionProxyObject");
  864. }
  865. bool TileSourceInspectorPlugin::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
  866. if (p_path == "id") {
  867. const Variant value = p_object->get("id");
  868. if (value.get_type() == Variant::NIL) { // May happen if the object is not yet initialized.
  869. return true;
  870. }
  871. EditorProperty *ep = memnew(EditorProperty);
  872. HBoxContainer *hbox = memnew(HBoxContainer);
  873. hbox->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  874. id_label = memnew(Label(itos(value)));
  875. id_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  876. hbox->add_child(id_label);
  877. Button *button = memnew(Button(TTR("Edit")));
  878. button->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  879. hbox->add_child(button);
  880. button->connect(SceneStringName(pressed), callable_mp(this, &TileSourceInspectorPlugin::_show_id_edit_dialog).bind(p_object));
  881. ep->add_child(hbox);
  882. add_property_editor(p_path, ep);
  883. return true;
  884. }
  885. return false;
  886. }