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