tile_set_editor.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*************************************************************************/
  2. /* tile_set_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_node.h"
  35. #include "editor/editor_scale.h"
  36. #include "editor/editor_undo_redo_manager.h"
  37. #include "scene/gui/box_container.h"
  38. #include "scene/gui/control.h"
  39. #include "scene/gui/tab_container.h"
  40. TileSetEditor *TileSetEditor::singleton = nullptr;
  41. void TileSetEditor::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  42. ERR_FAIL_COND(!tile_set.is_valid());
  43. if (!_can_drop_data_fw(p_point, p_data, p_from)) {
  44. return;
  45. }
  46. if (p_from == sources_list) {
  47. // Handle dropping a texture in the list of atlas resources.
  48. int source_id = TileSet::INVALID_SOURCE;
  49. int added = 0;
  50. Dictionary d = p_data;
  51. Vector<String> files = d["files"];
  52. for (int i = 0; i < files.size(); i++) {
  53. Ref<Texture2D> resource = ResourceLoader::load(files[i]);
  54. if (resource.is_valid()) {
  55. // Retrieve the id for the next created source.
  56. source_id = tile_set->get_next_source_id();
  57. // Actually create the new source.
  58. Ref<TileSetAtlasSource> atlas_source = memnew(TileSetAtlasSource);
  59. atlas_source->set_texture(resource);
  60. undo_redo->create_action(TTR("Add a new atlas source"));
  61. undo_redo->add_do_method(*tile_set, "add_source", atlas_source, source_id);
  62. undo_redo->add_do_method(*atlas_source, "set_texture_region_size", tile_set->get_tile_size());
  63. undo_redo->add_undo_method(*tile_set, "remove_source", source_id);
  64. undo_redo->commit_action();
  65. added += 1;
  66. }
  67. }
  68. if (added == 1) {
  69. tile_set_atlas_source_editor->init_source();
  70. }
  71. // Update the selected source (thus triggering an update).
  72. _update_sources_list(source_id);
  73. }
  74. }
  75. bool TileSetEditor::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  76. ERR_FAIL_COND_V(!tile_set.is_valid(), false);
  77. if (p_from == sources_list) {
  78. Dictionary d = p_data;
  79. if (!d.has("type")) {
  80. return false;
  81. }
  82. // Check if we have a Texture2D.
  83. if (String(d["type"]) == "files") {
  84. Vector<String> files = d["files"];
  85. if (files.size() == 0) {
  86. return false;
  87. }
  88. for (int i = 0; i < files.size(); i++) {
  89. String file = files[i];
  90. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  91. if (!ClassDB::is_parent_class(ftype, "Texture2D")) {
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. void TileSetEditor::_update_sources_list(int force_selected_id) {
  101. ERR_FAIL_COND(!tile_set.is_valid());
  102. // Get the previously selected id.
  103. int old_selected = TileSet::INVALID_SOURCE;
  104. if (sources_list->get_current() >= 0) {
  105. int source_id = sources_list->get_item_metadata(sources_list->get_current());
  106. if (tile_set->has_source(source_id)) {
  107. old_selected = source_id;
  108. }
  109. }
  110. int to_select = TileSet::INVALID_SOURCE;
  111. if (force_selected_id >= 0) {
  112. to_select = force_selected_id;
  113. } else if (old_selected >= 0) {
  114. to_select = old_selected;
  115. }
  116. // Clear the list.
  117. sources_list->clear();
  118. // Update the atlas sources.
  119. List<int> source_ids = TilesEditorPlugin::get_singleton()->get_sorted_sources(tile_set);
  120. for (const int &source_id : source_ids) {
  121. TileSetSource *source = *tile_set->get_source(source_id);
  122. Ref<Texture2D> texture;
  123. String item_text;
  124. // Common to all type of sources.
  125. if (!source->get_name().is_empty()) {
  126. item_text = vformat(TTR("%s (id:%d)"), source->get_name(), source_id);
  127. }
  128. // Atlas source.
  129. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  130. if (atlas_source) {
  131. texture = atlas_source->get_texture();
  132. if (item_text.is_empty()) {
  133. if (texture.is_valid()) {
  134. item_text = vformat("%s (ID: %d)", texture->get_path().get_file(), source_id);
  135. } else {
  136. item_text = vformat(TTR("No Texture Atlas Source (ID: %d)"), source_id);
  137. }
  138. }
  139. }
  140. // Scene collection source.
  141. TileSetScenesCollectionSource *scene_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  142. if (scene_collection_source) {
  143. texture = get_theme_icon(SNAME("PackedScene"), SNAME("EditorIcons"));
  144. if (item_text.is_empty()) {
  145. item_text = vformat(TTR("Scene Collection Source (ID: %d)"), source_id);
  146. }
  147. }
  148. // Use default if not valid.
  149. if (item_text.is_empty()) {
  150. item_text = vformat(TTR("Unknown Type Source (ID: %d)"), source_id);
  151. }
  152. if (!texture.is_valid()) {
  153. texture = missing_texture_texture;
  154. }
  155. sources_list->add_item(item_text, texture);
  156. sources_list->set_item_metadata(-1, source_id);
  157. }
  158. // Set again the current selected item if needed.
  159. if (to_select >= 0) {
  160. for (int i = 0; i < sources_list->get_item_count(); i++) {
  161. if ((int)sources_list->get_item_metadata(i) == to_select) {
  162. sources_list->set_current(i);
  163. sources_list->ensure_current_is_visible();
  164. if (old_selected != to_select) {
  165. sources_list->emit_signal(SNAME("item_selected"), sources_list->get_current());
  166. }
  167. break;
  168. }
  169. }
  170. }
  171. // If nothing is selected, select the first entry.
  172. if (sources_list->get_current() < 0 && sources_list->get_item_count() > 0) {
  173. sources_list->set_current(0);
  174. if (old_selected != int(sources_list->get_item_metadata(0))) {
  175. sources_list->emit_signal(SNAME("item_selected"), sources_list->get_current());
  176. }
  177. }
  178. // If there is no source left, hide all editors and show the label.
  179. _source_selected(sources_list->get_current());
  180. // Synchronize the lists.
  181. TilesEditorPlugin::get_singleton()->set_sources_lists_current(sources_list->get_current());
  182. }
  183. void TileSetEditor::_source_selected(int p_source_index) {
  184. ERR_FAIL_COND(!tile_set.is_valid());
  185. // Update the selected source.
  186. sources_delete_button->set_disabled(p_source_index < 0);
  187. if (p_source_index >= 0) {
  188. int source_id = sources_list->get_item_metadata(p_source_index);
  189. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id));
  190. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(*tile_set->get_source(source_id));
  191. if (atlas_source) {
  192. no_source_selected_label->hide();
  193. tile_set_atlas_source_editor->edit(*tile_set, atlas_source, source_id);
  194. tile_set_atlas_source_editor->show();
  195. tile_set_scenes_collection_source_editor->hide();
  196. } else if (scenes_collection_source) {
  197. no_source_selected_label->hide();
  198. tile_set_atlas_source_editor->hide();
  199. tile_set_scenes_collection_source_editor->edit(*tile_set, scenes_collection_source, source_id);
  200. tile_set_scenes_collection_source_editor->show();
  201. } else {
  202. no_source_selected_label->show();
  203. tile_set_atlas_source_editor->hide();
  204. tile_set_scenes_collection_source_editor->hide();
  205. }
  206. } else {
  207. no_source_selected_label->show();
  208. tile_set_atlas_source_editor->hide();
  209. tile_set_scenes_collection_source_editor->hide();
  210. }
  211. }
  212. void TileSetEditor::_source_delete_pressed() {
  213. ERR_FAIL_COND(!tile_set.is_valid());
  214. // Update the selected source.
  215. int to_delete = sources_list->get_item_metadata(sources_list->get_current());
  216. Ref<TileSetSource> source = tile_set->get_source(to_delete);
  217. // Remove the source.
  218. undo_redo->create_action(TTR("Remove source"));
  219. undo_redo->add_do_method(*tile_set, "remove_source", to_delete);
  220. undo_redo->add_undo_method(*tile_set, "add_source", source, to_delete);
  221. undo_redo->commit_action();
  222. _update_sources_list();
  223. }
  224. void TileSetEditor::_source_add_id_pressed(int p_id_pressed) {
  225. ERR_FAIL_COND(!tile_set.is_valid());
  226. switch (p_id_pressed) {
  227. case 0: {
  228. int source_id = tile_set->get_next_source_id();
  229. Ref<TileSetAtlasSource> atlas_source = memnew(TileSetAtlasSource);
  230. // Add a new source.
  231. undo_redo->create_action(TTR("Add atlas source"));
  232. undo_redo->add_do_method(*tile_set, "add_source", atlas_source, source_id);
  233. undo_redo->add_do_method(*atlas_source, "set_texture_region_size", tile_set->get_tile_size());
  234. undo_redo->add_undo_method(*tile_set, "remove_source", source_id);
  235. undo_redo->commit_action();
  236. _update_sources_list(source_id);
  237. } break;
  238. case 1: {
  239. int source_id = tile_set->get_next_source_id();
  240. Ref<TileSetScenesCollectionSource> scene_collection_source = memnew(TileSetScenesCollectionSource);
  241. // Add a new source.
  242. undo_redo->create_action(TTR("Add atlas source"));
  243. undo_redo->add_do_method(*tile_set, "add_source", scene_collection_source, source_id);
  244. undo_redo->add_undo_method(*tile_set, "remove_source", source_id);
  245. undo_redo->commit_action();
  246. _update_sources_list(source_id);
  247. } break;
  248. default:
  249. ERR_FAIL();
  250. }
  251. }
  252. void TileSetEditor::_sources_advanced_menu_id_pressed(int p_id_pressed) {
  253. ERR_FAIL_COND(!tile_set.is_valid());
  254. switch (p_id_pressed) {
  255. case 0: {
  256. atlas_merging_dialog->update_tile_set(tile_set);
  257. atlas_merging_dialog->popup_centered_ratio(0.5);
  258. } break;
  259. case 1: {
  260. tile_proxies_manager_dialog->update_tile_set(tile_set);
  261. tile_proxies_manager_dialog->popup_centered_ratio(0.5);
  262. } break;
  263. }
  264. }
  265. void TileSetEditor::_set_source_sort(int p_sort) {
  266. TilesEditorPlugin::get_singleton()->set_sorting_option(p_sort);
  267. for (int i = 0; i != TilesEditorPlugin::SOURCE_SORT_MAX; i++) {
  268. source_sort_button->get_popup()->set_item_checked(i, (i == (int)p_sort));
  269. }
  270. int old_selected = TileSet::INVALID_SOURCE;
  271. if (sources_list->get_current() >= 0) {
  272. int source_id = sources_list->get_item_metadata(sources_list->get_current());
  273. if (tile_set->has_source(source_id)) {
  274. old_selected = source_id;
  275. }
  276. }
  277. _update_sources_list(old_selected);
  278. }
  279. void TileSetEditor::_notification(int p_what) {
  280. switch (p_what) {
  281. case NOTIFICATION_ENTER_TREE:
  282. case NOTIFICATION_THEME_CHANGED: {
  283. sources_delete_button->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  284. sources_add_button->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  285. source_sort_button->set_icon(get_theme_icon(SNAME("Sort"), SNAME("EditorIcons")));
  286. sources_advanced_menu_button->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  287. missing_texture_texture = get_theme_icon(SNAME("TileSet"), SNAME("EditorIcons"));
  288. } break;
  289. case NOTIFICATION_INTERNAL_PROCESS: {
  290. if (tile_set_changed_needs_update) {
  291. if (tile_set.is_valid()) {
  292. tile_set->set_edited(true);
  293. }
  294. _update_sources_list();
  295. _update_patterns_list();
  296. tile_set_changed_needs_update = false;
  297. }
  298. } break;
  299. }
  300. }
  301. void TileSetEditor::_patterns_item_list_gui_input(const Ref<InputEvent> &p_event) {
  302. ERR_FAIL_COND(!tile_set.is_valid());
  303. if (ED_IS_SHORTCUT("tiles_editor/delete", p_event) && p_event->is_pressed() && !p_event->is_echo()) {
  304. Vector<int> selected = patterns_item_list->get_selected_items();
  305. undo_redo->create_action(TTR("Remove TileSet patterns"));
  306. for (int i = 0; i < selected.size(); i++) {
  307. int pattern_index = selected[i];
  308. undo_redo->add_do_method(*tile_set, "remove_pattern", pattern_index);
  309. undo_redo->add_undo_method(*tile_set, "add_pattern", tile_set->get_pattern(pattern_index), pattern_index);
  310. }
  311. undo_redo->commit_action();
  312. patterns_item_list->accept_event();
  313. }
  314. }
  315. void TileSetEditor::_pattern_preview_done(Ref<TileMapPattern> p_pattern, Ref<Texture2D> p_texture) {
  316. // TODO optimize ?
  317. for (int i = 0; i < patterns_item_list->get_item_count(); i++) {
  318. if (patterns_item_list->get_item_metadata(i) == p_pattern) {
  319. patterns_item_list->set_item_icon(i, p_texture);
  320. break;
  321. }
  322. }
  323. }
  324. void TileSetEditor::_update_patterns_list() {
  325. ERR_FAIL_COND(!tile_set.is_valid());
  326. // Recreate the items.
  327. patterns_item_list->clear();
  328. for (int i = 0; i < tile_set->get_patterns_count(); i++) {
  329. int id = patterns_item_list->add_item("");
  330. patterns_item_list->set_item_metadata(id, tile_set->get_pattern(i));
  331. TilesEditorPlugin::get_singleton()->queue_pattern_preview(tile_set, tile_set->get_pattern(i), callable_mp(this, &TileSetEditor::_pattern_preview_done));
  332. }
  333. // Update the label visibility.
  334. patterns_help_label->set_visible(patterns_item_list->get_item_count() == 0);
  335. }
  336. void TileSetEditor::_tile_set_changed() {
  337. tile_set_changed_needs_update = true;
  338. }
  339. void TileSetEditor::_tab_changed(int p_tab_changed) {
  340. split_container->set_visible(p_tab_changed == 0);
  341. patterns_item_list->set_visible(p_tab_changed == 1);
  342. }
  343. void TileSetEditor::_move_tile_set_array_element(Object *p_undo_redo, Object *p_edited, String p_array_prefix, int p_from_index, int p_to_pos) {
  344. Ref<EditorUndoRedoManager> undo_redo = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
  345. ERR_FAIL_COND(undo_redo.is_null());
  346. TileSet *tile_set = Object::cast_to<TileSet>(p_edited);
  347. if (!tile_set) {
  348. return;
  349. }
  350. Vector<String> components = String(p_array_prefix).split("/", true, 2);
  351. // Compute the array indices to save.
  352. int begin = 0;
  353. int end;
  354. if (p_array_prefix == "occlusion_layer_") {
  355. end = tile_set->get_occlusion_layers_count();
  356. } else if (p_array_prefix == "physics_layer_") {
  357. end = tile_set->get_physics_layers_count();
  358. } else if (p_array_prefix == "terrain_set_") {
  359. end = tile_set->get_terrain_sets_count();
  360. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {
  361. int terrain_set = components[0].trim_prefix("terrain_set_").to_int();
  362. end = tile_set->get_terrains_count(terrain_set);
  363. } else if (p_array_prefix == "navigation_layer_") {
  364. end = tile_set->get_navigation_layers_count();
  365. } else if (p_array_prefix == "custom_data_layer_") {
  366. end = tile_set->get_custom_data_layers_count();
  367. } else {
  368. ERR_FAIL_MSG("Invalid array prefix for TileSet.");
  369. }
  370. if (p_from_index < 0) {
  371. // Adding new.
  372. if (p_to_pos >= 0) {
  373. begin = p_to_pos;
  374. } else {
  375. end = 0; // Nothing to save when adding at the end.
  376. }
  377. } else if (p_to_pos < 0) {
  378. // Removing.
  379. begin = p_from_index;
  380. } else {
  381. // Moving.
  382. begin = MIN(p_from_index, p_to_pos);
  383. end = MIN(MAX(p_from_index, p_to_pos) + 1, end);
  384. }
  385. #define ADD_UNDO(obj, property) undo_redo->add_undo_property(obj, property, obj->get(property));
  386. // Save layers' properties.
  387. List<PropertyInfo> properties;
  388. tile_set->get_property_list(&properties);
  389. for (PropertyInfo pi : properties) {
  390. if (pi.name.begins_with(p_array_prefix)) {
  391. String str = pi.name.trim_prefix(p_array_prefix);
  392. int to_char_index = 0;
  393. while (to_char_index < str.length()) {
  394. if (!is_digit(str[to_char_index])) {
  395. break;
  396. }
  397. to_char_index++;
  398. }
  399. if (to_char_index > 0) {
  400. int array_index = str.left(to_char_index).to_int();
  401. if (array_index >= begin && array_index < end) {
  402. ADD_UNDO(tile_set, pi.name);
  403. }
  404. }
  405. }
  406. }
  407. // Save properties for TileSetAtlasSources tile data
  408. for (int i = 0; i < tile_set->get_source_count(); i++) {
  409. int source_id = tile_set->get_source_id(i);
  410. Ref<TileSetAtlasSource> tas = tile_set->get_source(source_id);
  411. if (tas.is_valid()) {
  412. for (int j = 0; j < tas->get_tiles_count(); j++) {
  413. Vector2i tile_id = tas->get_tile_id(j);
  414. for (int k = 0; k < tas->get_alternative_tiles_count(tile_id); k++) {
  415. int alternative_id = tas->get_alternative_tile_id(tile_id, k);
  416. TileData *tile_data = tas->get_tile_data(tile_id, alternative_id);
  417. ERR_FAIL_COND(!tile_data);
  418. // Actually saving stuff.
  419. if (p_array_prefix == "occlusion_layer_") {
  420. for (int layer_index = begin; layer_index < end; layer_index++) {
  421. ADD_UNDO(tile_data, vformat("occlusion_layer_%d/polygon", layer_index));
  422. }
  423. } else if (p_array_prefix == "physics_layer_") {
  424. for (int layer_index = begin; layer_index < end; layer_index++) {
  425. ADD_UNDO(tile_data, vformat("physics_layer_%d/polygons_count", layer_index));
  426. for (int polygon_index = 0; polygon_index < tile_data->get_collision_polygons_count(layer_index); polygon_index++) {
  427. ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/points", layer_index, polygon_index));
  428. ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/one_way", layer_index, polygon_index));
  429. ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/one_way_margin", layer_index, polygon_index));
  430. }
  431. }
  432. } else if (p_array_prefix == "terrain_set_") {
  433. ADD_UNDO(tile_data, "terrain_set");
  434. for (int terrain_set_index = begin; terrain_set_index < end; terrain_set_index++) {
  435. for (int l = 0; l < TileSet::CELL_NEIGHBOR_MAX; l++) {
  436. TileSet::CellNeighbor bit = TileSet::CellNeighbor(l);
  437. if (tile_data->is_valid_terrain_peering_bit(bit)) {
  438. ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[l]));
  439. }
  440. }
  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. for (int terrain_index = 0; terrain_index < TileSet::CELL_NEIGHBOR_MAX; terrain_index++) {
  444. TileSet::CellNeighbor bit = TileSet::CellNeighbor(terrain_index);
  445. if (tile_data->is_valid_terrain_peering_bit(bit)) {
  446. ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[terrain_index]));
  447. }
  448. }
  449. } else if (p_array_prefix == "navigation_layer_") {
  450. for (int layer_index = begin; layer_index < end; layer_index++) {
  451. ADD_UNDO(tile_data, vformat("navigation_layer_%d/polygon", layer_index));
  452. }
  453. } else if (p_array_prefix == "custom_data_layer_") {
  454. for (int layer_index = begin; layer_index < end; layer_index++) {
  455. ADD_UNDO(tile_data, vformat("custom_data_%d", layer_index));
  456. }
  457. }
  458. }
  459. }
  460. }
  461. }
  462. #undef ADD_UNDO
  463. // Add do method.
  464. if (p_array_prefix == "occlusion_layer_") {
  465. if (p_from_index < 0) {
  466. undo_redo->add_do_method(tile_set, "add_occlusion_layer", p_to_pos);
  467. } else if (p_to_pos < 0) {
  468. undo_redo->add_do_method(tile_set, "remove_occlusion_layer", p_from_index);
  469. } else {
  470. undo_redo->add_do_method(tile_set, "move_occlusion_layer", p_from_index, p_to_pos);
  471. }
  472. } else if (p_array_prefix == "physics_layer_") {
  473. if (p_from_index < 0) {
  474. undo_redo->add_do_method(tile_set, "add_physics_layer", p_to_pos);
  475. } else if (p_to_pos < 0) {
  476. undo_redo->add_do_method(tile_set, "remove_physics_layer", p_from_index);
  477. } else {
  478. undo_redo->add_do_method(tile_set, "move_physics_layer", p_from_index, p_to_pos);
  479. }
  480. } else if (p_array_prefix == "terrain_set_") {
  481. if (p_from_index < 0) {
  482. undo_redo->add_do_method(tile_set, "add_terrain_set", p_to_pos);
  483. } else if (p_to_pos < 0) {
  484. undo_redo->add_do_method(tile_set, "remove_terrain_set", p_from_index);
  485. } else {
  486. undo_redo->add_do_method(tile_set, "move_terrain_set", p_from_index, p_to_pos);
  487. }
  488. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {
  489. int terrain_set = components[0].trim_prefix("terrain_set_").to_int();
  490. if (p_from_index < 0) {
  491. undo_redo->add_do_method(tile_set, "add_terrain", terrain_set, p_to_pos);
  492. } else if (p_to_pos < 0) {
  493. undo_redo->add_do_method(tile_set, "remove_terrain", terrain_set, p_from_index);
  494. } else {
  495. undo_redo->add_do_method(tile_set, "move_terrain", terrain_set, p_from_index, p_to_pos);
  496. }
  497. } else if (p_array_prefix == "navigation_layer_") {
  498. if (p_from_index < 0) {
  499. undo_redo->add_do_method(tile_set, "add_navigation_layer", p_to_pos);
  500. } else if (p_to_pos < 0) {
  501. undo_redo->add_do_method(tile_set, "remove_navigation_layer", p_from_index);
  502. } else {
  503. undo_redo->add_do_method(tile_set, "move_navigation_layer", p_from_index, p_to_pos);
  504. }
  505. } else if (p_array_prefix == "custom_data_layer_") {
  506. if (p_from_index < 0) {
  507. undo_redo->add_do_method(tile_set, "add_custom_data_layer", p_to_pos);
  508. } else if (p_to_pos < 0) {
  509. undo_redo->add_do_method(tile_set, "remove_custom_data_layer", p_from_index);
  510. } else {
  511. undo_redo->add_do_method(tile_set, "move_custom_data_layer", p_from_index, p_to_pos);
  512. }
  513. }
  514. }
  515. void TileSetEditor::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, String p_property, Variant p_new_value) {
  516. Ref<EditorUndoRedoManager> undo_redo = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
  517. ERR_FAIL_COND(undo_redo.is_null());
  518. #define ADD_UNDO(obj, property) undo_redo->add_undo_property(obj, property, obj->get(property));
  519. TileSet *tile_set = Object::cast_to<TileSet>(p_edited);
  520. if (tile_set) {
  521. Vector<String> components = p_property.split("/", true, 3);
  522. for (int i = 0; i < tile_set->get_source_count(); i++) {
  523. int source_id = tile_set->get_source_id(i);
  524. Ref<TileSetAtlasSource> tas = tile_set->get_source(source_id);
  525. if (tas.is_valid()) {
  526. for (int j = 0; j < tas->get_tiles_count(); j++) {
  527. Vector2i tile_id = tas->get_tile_id(j);
  528. for (int k = 0; k < tas->get_alternative_tiles_count(tile_id); k++) {
  529. int alternative_id = tas->get_alternative_tile_id(tile_id, k);
  530. TileData *tile_data = tas->get_tile_data(tile_id, alternative_id);
  531. ERR_FAIL_COND(!tile_data);
  532. if (components.size() == 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "mode") {
  533. ADD_UNDO(tile_data, "terrain_set");
  534. ADD_UNDO(tile_data, "terrain");
  535. for (int l = 0; l < TileSet::CELL_NEIGHBOR_MAX; l++) {
  536. TileSet::CellNeighbor bit = TileSet::CellNeighbor(l);
  537. if (tile_data->is_valid_terrain_peering_bit(bit)) {
  538. ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[l]));
  539. }
  540. }
  541. } 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") {
  542. int custom_data_layer = components[0].trim_prefix("custom_data_layer_").is_valid_int();
  543. ADD_UNDO(tile_data, vformat("custom_data_%d", custom_data_layer));
  544. }
  545. }
  546. }
  547. }
  548. }
  549. }
  550. #undef ADD_UNDO
  551. }
  552. void TileSetEditor::_bind_methods() {
  553. ClassDB::bind_method(D_METHOD("_can_drop_data_fw"), &TileSetEditor::_can_drop_data_fw);
  554. ClassDB::bind_method(D_METHOD("_drop_data_fw"), &TileSetEditor::_drop_data_fw);
  555. }
  556. void TileSetEditor::edit(Ref<TileSet> p_tile_set) {
  557. if (p_tile_set == tile_set) {
  558. return;
  559. }
  560. // Remove listener.
  561. if (tile_set.is_valid()) {
  562. tile_set->disconnect("changed", callable_mp(this, &TileSetEditor::_tile_set_changed));
  563. }
  564. // Change the edited object.
  565. tile_set = p_tile_set;
  566. // Add the listener again.
  567. if (tile_set.is_valid()) {
  568. tile_set->connect("changed", callable_mp(this, &TileSetEditor::_tile_set_changed));
  569. _update_sources_list();
  570. _update_patterns_list();
  571. }
  572. tile_set_atlas_source_editor->hide();
  573. tile_set_scenes_collection_source_editor->hide();
  574. no_source_selected_label->show();
  575. }
  576. TileSetEditor::TileSetEditor() {
  577. singleton = this;
  578. undo_redo = EditorNode::get_undo_redo();
  579. set_process_internal(true);
  580. // TabBar.
  581. tabs_bar = memnew(TabBar);
  582. tabs_bar->set_tab_alignment(TabBar::ALIGNMENT_CENTER);
  583. tabs_bar->set_clip_tabs(false);
  584. tabs_bar->add_tab(TTR("Tiles"));
  585. tabs_bar->add_tab(TTR("Patterns"));
  586. tabs_bar->connect("tab_changed", callable_mp(this, &TileSetEditor::_tab_changed));
  587. tile_set_toolbar = memnew(HBoxContainer);
  588. tile_set_toolbar->set_h_size_flags(SIZE_EXPAND_FILL);
  589. tile_set_toolbar->add_child(tabs_bar);
  590. add_child(tile_set_toolbar);
  591. //// Tiles ////
  592. // Split container.
  593. split_container = memnew(HSplitContainer);
  594. split_container->set_name(TTR("Tiles"));
  595. split_container->set_h_size_flags(SIZE_EXPAND_FILL);
  596. split_container->set_v_size_flags(SIZE_EXPAND_FILL);
  597. add_child(split_container);
  598. // Sources list.
  599. VBoxContainer *split_container_left_side = memnew(VBoxContainer);
  600. split_container_left_side->set_h_size_flags(SIZE_EXPAND_FILL);
  601. split_container_left_side->set_v_size_flags(SIZE_EXPAND_FILL);
  602. split_container_left_side->set_stretch_ratio(0.25);
  603. split_container_left_side->set_custom_minimum_size(Size2i(70, 0) * EDSCALE);
  604. split_container->add_child(split_container_left_side);
  605. source_sort_button = memnew(MenuButton);
  606. source_sort_button->set_flat(true);
  607. source_sort_button->set_tooltip_text(TTR("Sort sources"));
  608. PopupMenu *p = source_sort_button->get_popup();
  609. p->connect("id_pressed", callable_mp(this, &TileSetEditor::_set_source_sort));
  610. p->add_radio_check_item(TTR("Sort by ID (Ascending)"), TilesEditorPlugin::SOURCE_SORT_ID);
  611. p->add_radio_check_item(TTR("Sort by ID (Descending)"), TilesEditorPlugin::SOURCE_SORT_ID_REVERSE);
  612. p->add_radio_check_item(TTR("Sort by Name (Ascending)"), TilesEditorPlugin::SOURCE_SORT_NAME);
  613. p->add_radio_check_item(TTR("Sort by Name (Descending)"), TilesEditorPlugin::SOURCE_SORT_NAME_REVERSE);
  614. p->set_item_checked(TilesEditorPlugin::SOURCE_SORT_ID, true);
  615. sources_list = memnew(ItemList);
  616. sources_list->set_fixed_icon_size(Size2i(60, 60) * EDSCALE);
  617. sources_list->set_h_size_flags(SIZE_EXPAND_FILL);
  618. sources_list->set_v_size_flags(SIZE_EXPAND_FILL);
  619. sources_list->connect("item_selected", callable_mp(this, &TileSetEditor::_source_selected));
  620. sources_list->connect("item_selected", callable_mp(TilesEditorPlugin::get_singleton(), &TilesEditorPlugin::set_sources_lists_current));
  621. sources_list->connect("visibility_changed", callable_mp(TilesEditorPlugin::get_singleton(), &TilesEditorPlugin::synchronize_sources_list).bind(sources_list, source_sort_button));
  622. sources_list->add_user_signal(MethodInfo("sort_request"));
  623. sources_list->connect("sort_request", callable_mp(this, &TileSetEditor::_update_sources_list).bind(-1));
  624. sources_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  625. sources_list->set_drag_forwarding(this);
  626. split_container_left_side->add_child(sources_list);
  627. HBoxContainer *sources_bottom_actions = memnew(HBoxContainer);
  628. sources_bottom_actions->set_alignment(BoxContainer::ALIGNMENT_END);
  629. split_container_left_side->add_child(sources_bottom_actions);
  630. sources_delete_button = memnew(Button);
  631. sources_delete_button->set_flat(true);
  632. sources_delete_button->set_disabled(true);
  633. sources_delete_button->connect("pressed", callable_mp(this, &TileSetEditor::_source_delete_pressed));
  634. sources_bottom_actions->add_child(sources_delete_button);
  635. sources_add_button = memnew(MenuButton);
  636. sources_add_button->set_flat(true);
  637. sources_add_button->get_popup()->add_item(TTR("Atlas"));
  638. sources_add_button->get_popup()->add_item(TTR("Scenes Collection"));
  639. sources_add_button->get_popup()->connect("id_pressed", callable_mp(this, &TileSetEditor::_source_add_id_pressed));
  640. sources_bottom_actions->add_child(sources_add_button);
  641. sources_advanced_menu_button = memnew(MenuButton);
  642. sources_advanced_menu_button->set_flat(true);
  643. sources_advanced_menu_button->get_popup()->add_item(TTR("Open Atlas Merging Tool"));
  644. sources_advanced_menu_button->get_popup()->add_item(TTR("Manage Tile Proxies"));
  645. sources_advanced_menu_button->get_popup()->connect("id_pressed", callable_mp(this, &TileSetEditor::_sources_advanced_menu_id_pressed));
  646. sources_bottom_actions->add_child(sources_advanced_menu_button);
  647. sources_bottom_actions->add_child(source_sort_button);
  648. atlas_merging_dialog = memnew(AtlasMergingDialog);
  649. add_child(atlas_merging_dialog);
  650. tile_proxies_manager_dialog = memnew(TileProxiesManagerDialog);
  651. add_child(tile_proxies_manager_dialog);
  652. // Right side container.
  653. VBoxContainer *split_container_right_side = memnew(VBoxContainer);
  654. split_container_right_side->set_h_size_flags(SIZE_EXPAND_FILL);
  655. split_container_right_side->set_v_size_flags(SIZE_EXPAND_FILL);
  656. split_container->add_child(split_container_right_side);
  657. // No source selected.
  658. no_source_selected_label = memnew(Label);
  659. no_source_selected_label->set_text(TTR("No TileSet source selected. Select or create a TileSet source."));
  660. no_source_selected_label->set_h_size_flags(SIZE_EXPAND_FILL);
  661. no_source_selected_label->set_v_size_flags(SIZE_EXPAND_FILL);
  662. no_source_selected_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  663. no_source_selected_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  664. split_container_right_side->add_child(no_source_selected_label);
  665. // Atlases editor.
  666. tile_set_atlas_source_editor = memnew(TileSetAtlasSourceEditor);
  667. tile_set_atlas_source_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  668. tile_set_atlas_source_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  669. tile_set_atlas_source_editor->connect("source_id_changed", callable_mp(this, &TileSetEditor::_update_sources_list));
  670. split_container_right_side->add_child(tile_set_atlas_source_editor);
  671. tile_set_atlas_source_editor->hide();
  672. // Scenes collection editor.
  673. tile_set_scenes_collection_source_editor = memnew(TileSetScenesCollectionSourceEditor);
  674. tile_set_scenes_collection_source_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  675. tile_set_scenes_collection_source_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  676. tile_set_scenes_collection_source_editor->connect("source_id_changed", callable_mp(this, &TileSetEditor::_update_sources_list));
  677. split_container_right_side->add_child(tile_set_scenes_collection_source_editor);
  678. tile_set_scenes_collection_source_editor->hide();
  679. //// Patterns ////
  680. int thumbnail_size = 64;
  681. patterns_item_list = memnew(ItemList);
  682. patterns_item_list->set_max_columns(0);
  683. patterns_item_list->set_icon_mode(ItemList::ICON_MODE_TOP);
  684. patterns_item_list->set_fixed_column_width(thumbnail_size * 3 / 2);
  685. patterns_item_list->set_max_text_lines(2);
  686. patterns_item_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  687. patterns_item_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  688. patterns_item_list->connect("gui_input", callable_mp(this, &TileSetEditor::_patterns_item_list_gui_input));
  689. add_child(patterns_item_list);
  690. patterns_item_list->hide();
  691. patterns_help_label = memnew(Label);
  692. patterns_help_label->set_text(TTR("Add new patterns in the TileMap editing mode."));
  693. patterns_help_label->set_anchors_and_offsets_preset(Control::PRESET_CENTER);
  694. patterns_item_list->add_child(patterns_help_label);
  695. // Registers UndoRedo inspector callback.
  696. EditorNode::get_singleton()->get_editor_data().add_move_array_element_function(SNAME("TileSet"), callable_mp(this, &TileSetEditor::_move_tile_set_array_element));
  697. EditorNode::get_singleton()->get_editor_data().add_undo_redo_inspector_hook_callback(callable_mp(this, &TileSetEditor::_undo_redo_inspector_callback));
  698. }
  699. TileSetEditor::~TileSetEditor() {
  700. if (tile_set.is_valid()) {
  701. tile_set->disconnect("changed", callable_mp(this, &TileSetEditor::_tile_set_changed));
  702. }
  703. }