Path: blob/master/editor/scene/2d/tiles/tile_set_editor.cpp
21320 views
/**************************************************************************/1/* tile_set_editor.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tile_set_editor.h"3132#include "tile_data_editors.h"33#include "tiles_editor_plugin.h"3435#include "editor/editor_node.h"36#include "editor/editor_undo_redo_manager.h"37#include "editor/file_system/editor_file_system.h"38#include "editor/gui/editor_file_dialog.h"39#include "editor/inspector/editor_inspector.h"40#include "editor/settings/editor_command_palette.h"41#include "editor/settings/editor_settings.h"42#include "editor/themes/editor_scale.h"4344#include "scene/gui/box_container.h"45#include "scene/gui/control.h"46#include "scene/gui/dialogs.h"4748TileSetEditor *TileSetEditor::singleton = nullptr;4950void TileSetEditor::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {51ERR_FAIL_COND(tile_set.is_null());5253if (!_can_drop_data_fw(p_point, p_data, p_from)) {54return;55}5657if (p_from == sources_list) {58// Handle dropping a texture in the list of atlas resources.59Dictionary d = p_data;60Vector<String> files = d["files"];61_load_texture_files(files);62}63}6465bool TileSetEditor::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {66ERR_FAIL_COND_V(tile_set.is_null(), false);6768if (read_only) {69return false;70}7172if (p_from == sources_list) {73Dictionary d = p_data;7475if (!d.has("type")) {76return false;77}7879// Check if we have a Texture2D.80if (String(d["type"]) == "files") {81Vector<String> files = d["files"];8283if (files.is_empty()) {84return false;85}8687for (int i = 0; i < files.size(); i++) {88String ftype = EditorFileSystem::get_singleton()->get_file_type(files[i]);8990if (!ClassDB::is_parent_class(ftype, "Texture2D")) {91return false;92}93}9495return true;96}97}98return false;99}100101void TileSetEditor::_load_texture_files(const Vector<String> &p_paths) {102int source_id = TileSet::INVALID_SOURCE;103Vector<Ref<TileSetAtlasSource>> atlases;104105for (const String &p_path : p_paths) {106Ref<Texture2D> texture = ResourceLoader::load(p_path);107108if (texture.is_null()) {109EditorNode::get_singleton()->show_warning(TTR("Invalid texture selected."));110continue;111}112113// Retrieve the id for the next created source.114source_id = tile_set->get_next_source_id();115116// Actually create the new source.117Ref<TileSetAtlasSource> atlas_source = memnew(TileSetAtlasSource);118atlas_source->set_texture(texture);119atlas_source->set_texture_region_size(tile_set->get_tile_size());120121EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();122undo_redo->create_action(TTR("Add a new atlas source"));123undo_redo->add_do_method(*tile_set, "add_source", atlas_source, source_id);124undo_redo->add_undo_method(*tile_set, "remove_source", source_id);125undo_redo->commit_action();126127atlases.append(atlas_source);128}129130if (!atlases.is_empty()) {131tile_set_atlas_source_editor->init_new_atlases(atlases);132}133134// Update the selected source (thus triggering an update).135_update_sources_list(source_id);136}137138void TileSetEditor::_update_sources_list(int force_selected_id) {139if (tile_set.is_null()) {140return;141}142143// Get the previously selected id.144int old_selected = TileSet::INVALID_SOURCE;145if (sources_list->get_current() >= 0) {146int source_id = sources_list->get_item_metadata(sources_list->get_current());147if (tile_set->has_source(source_id)) {148old_selected = source_id;149}150}151152int to_select = TileSet::INVALID_SOURCE;153if (force_selected_id >= 0) {154to_select = force_selected_id;155} else if (old_selected >= 0) {156to_select = old_selected;157}158159// Clear the list.160sources_list->clear();161162// Update the atlas sources.163List<int> source_ids = TilesEditorUtils::get_singleton()->get_sorted_sources(tile_set);164for (const int &source_id : source_ids) {165TileSetSource *source = *tile_set->get_source(source_id);166167Ref<Texture2D> texture;168String item_text;169170// Common to all type of sources.171if (!source->get_name().is_empty()) {172item_text = source->get_name();173}174175// Atlas source.176TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);177if (atlas_source) {178texture = atlas_source->get_texture();179if (item_text.is_empty()) {180if (texture.is_valid()) {181item_text = texture->get_path().get_file();182} else {183item_text = vformat(TTR("No Texture Atlas Source (ID: %d)"), source_id);184}185}186}187188// Scene collection source.189TileSetScenesCollectionSource *scene_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);190if (scene_collection_source) {191texture = get_editor_theme_icon(SNAME("PackedScene"));192if (item_text.is_empty()) {193if (scene_collection_source->get_scene_tiles_count() > 0) {194item_text = vformat(TTR("Scene Collection Source (ID: %d)"), source_id);195} else {196item_text = vformat(TTR("Empty Scene Collection Source (ID: %d)"), source_id);197}198}199}200201// Use default if not valid.202if (item_text.is_empty()) {203item_text = vformat(TTR("Unknown Type Source (ID: %d)"), source_id);204}205if (texture.is_null()) {206texture = missing_texture_texture;207}208209sources_list->add_item(item_text, texture);210sources_list->set_item_metadata(-1, source_id);211}212213// Set again the current selected item if needed.214if (to_select >= 0) {215for (int i = 0; i < sources_list->get_item_count(); i++) {216if ((int)sources_list->get_item_metadata(i) == to_select) {217sources_list->set_current(i);218sources_list->ensure_current_is_visible();219if (old_selected != to_select) {220sources_list->emit_signal(SceneStringName(item_selected), sources_list->get_current());221}222break;223}224}225}226227// If nothing is selected, select the first entry.228if (sources_list->get_current() < 0 && sources_list->get_item_count() > 0) {229sources_list->set_current(0);230if (old_selected != int(sources_list->get_item_metadata(0))) {231sources_list->emit_signal(SceneStringName(item_selected), sources_list->get_current());232}233}234235// If there is no source left, hide all editors and show the label.236_source_selected(sources_list->get_current());237238// Synchronize the lists.239TilesEditorUtils::get_singleton()->set_sources_lists_current(sources_list->get_current());240}241242void TileSetEditor::_source_selected(int p_source_index) {243ERR_FAIL_COND(tile_set.is_null());244245// Update the selected source.246sources_delete_button->set_disabled(p_source_index < 0 || read_only);247248if (p_source_index >= 0) {249int source_id = sources_list->get_item_metadata(p_source_index);250TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id));251TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(*tile_set->get_source(source_id));252if (atlas_source) {253no_source_selected_label->hide();254tile_set_atlas_source_editor->edit(*tile_set, atlas_source, source_id);255tile_set_atlas_source_editor->show();256tile_set_scenes_collection_source_editor->hide();257} else if (scenes_collection_source) {258no_source_selected_label->hide();259tile_set_atlas_source_editor->hide();260tile_set_scenes_collection_source_editor->edit(*tile_set, scenes_collection_source, source_id);261tile_set_scenes_collection_source_editor->show();262} else {263no_source_selected_label->show();264tile_set_atlas_source_editor->hide();265tile_set_scenes_collection_source_editor->hide();266}267} else {268no_source_selected_label->show();269tile_set_atlas_source_editor->hide();270tile_set_scenes_collection_source_editor->hide();271}272}273274void TileSetEditor::_source_delete_pressed() {275ERR_FAIL_COND(tile_set.is_null());276277// Update the selected source.278int to_delete = sources_list->get_item_metadata(sources_list->get_current());279280Ref<TileSetSource> source = tile_set->get_source(to_delete);281282// Remove the source.283EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();284undo_redo->create_action(TTR("Remove source"));285undo_redo->add_do_method(*tile_set, "remove_source", to_delete);286undo_redo->add_undo_method(*tile_set, "add_source", source, to_delete);287undo_redo->commit_action();288289_update_sources_list();290}291292void TileSetEditor::_source_add_id_pressed(int p_id_pressed) {293ERR_FAIL_COND(tile_set.is_null());294295switch (p_id_pressed) {296case 0: {297if (!texture_file_dialog) {298texture_file_dialog = memnew(EditorFileDialog);299add_child(texture_file_dialog);300texture_file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);301texture_file_dialog->connect("files_selected", callable_mp(this, &TileSetEditor::_load_texture_files));302303List<String> extensions;304ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);305for (const String &E : extensions) {306texture_file_dialog->add_filter("*." + E, E.to_upper());307}308}309texture_file_dialog->popup_file_dialog();310} break;311case 1: {312int source_id = tile_set->get_next_source_id();313314Ref<TileSetScenesCollectionSource> scene_collection_source = memnew(TileSetScenesCollectionSource);315316// Add a new source.317EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();318undo_redo->create_action(TTR("Add atlas source"));319undo_redo->add_do_method(*tile_set, "add_source", scene_collection_source, source_id);320undo_redo->add_undo_method(*tile_set, "remove_source", source_id);321undo_redo->commit_action();322323_update_sources_list(source_id);324} break;325default:326ERR_FAIL();327}328}329330void TileSetEditor::_sources_advanced_menu_id_pressed(int p_id_pressed) {331ERR_FAIL_COND(tile_set.is_null());332333switch (p_id_pressed) {334case 0: {335atlas_merging_dialog->update_tile_set(tile_set);336atlas_merging_dialog->popup_centered_ratio(0.5);337} break;338case 1: {339tile_proxies_manager_dialog->update_tile_set(tile_set);340tile_proxies_manager_dialog->popup_centered_ratio(0.5);341} break;342}343}344345void TileSetEditor::_set_source_sort(int p_sort) {346TilesEditorUtils::get_singleton()->set_sorting_option(p_sort);347for (int i = 0; i != TilesEditorUtils::SOURCE_SORT_MAX; i++) {348source_sort_button->get_popup()->set_item_checked(i, (i == (int)p_sort));349}350351int old_selected = TileSet::INVALID_SOURCE;352if (sources_list->get_current() >= 0) {353int source_id = sources_list->get_item_metadata(sources_list->get_current());354if (tile_set->has_source(source_id)) {355old_selected = source_id;356}357}358_update_sources_list(old_selected);359360if (!first_edit) {361EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "tile_source_sort", p_sort);362}363}364365void TileSetEditor::_notification(int p_what) {366switch (p_what) {367case NOTIFICATION_THEME_CHANGED: {368sources_delete_button->set_button_icon(get_editor_theme_icon(SNAME("Remove")));369sources_add_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));370source_sort_button->set_button_icon(get_editor_theme_icon(SNAME("Sort")));371sources_advanced_menu_button->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));372missing_texture_texture = get_editor_theme_icon(SNAME("TileSet"));373expanded_area->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("expand_panel"), SNAME("TileSetEditor")));374_update_sources_list();375} break;376377case NOTIFICATION_INTERNAL_PROCESS: {378if (tile_set_changed_needs_update) {379if (tile_set.is_valid()) {380tile_set->set_edited(true);381}382383read_only = false;384if (tile_set.is_valid()) {385read_only = EditorNode::get_singleton()->is_resource_read_only(tile_set);386}387388_update_sources_list();389_update_patterns_list();390391sources_add_button->set_disabled(read_only);392sources_advanced_menu_button->set_disabled(read_only);393source_sort_button->set_disabled(read_only);394395tile_set_changed_needs_update = false;396}397} break;398399case NOTIFICATION_VISIBILITY_CHANGED: {400if (!is_visible_in_tree()) {401remove_expanded_editor();402}403} break;404}405}406407void TileSetEditor::_patterns_item_list_gui_input(const Ref<InputEvent> &p_event) {408ERR_FAIL_COND(tile_set.is_null());409410if (EditorNode::get_singleton()->is_resource_read_only(tile_set)) {411return;412}413414if (ED_IS_SHORTCUT("tiles_editor/delete", p_event) && p_event->is_pressed() && !p_event->is_echo()) {415Vector<int> selected = patterns_item_list->get_selected_items();416EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();417undo_redo->create_action(TTR("Remove TileSet patterns"));418for (int i = 0; i < selected.size(); i++) {419int pattern_index = selected[i];420undo_redo->add_do_method(*tile_set, "remove_pattern", pattern_index);421undo_redo->add_undo_method(*tile_set, "add_pattern", tile_set->get_pattern(pattern_index), pattern_index);422}423undo_redo->commit_action();424patterns_item_list->accept_event();425}426}427428void TileSetEditor::_pattern_preview_done(Ref<TileMapPattern> p_pattern, Ref<Texture2D> p_texture) {429// TODO optimize ?430for (int i = 0; i < patterns_item_list->get_item_count(); i++) {431if (patterns_item_list->get_item_metadata(i) == p_pattern) {432patterns_item_list->set_item_icon(i, p_texture);433break;434}435}436}437438void TileSetEditor::_update_patterns_list() {439ERR_FAIL_COND(tile_set.is_null());440441// Recreate the items.442patterns_item_list->clear();443for (int i = 0; i < tile_set->get_patterns_count(); i++) {444int id = patterns_item_list->add_item("");445patterns_item_list->set_item_metadata(id, tile_set->get_pattern(i));446patterns_item_list->set_item_tooltip(id, vformat(TTR("Index: %d"), i));447TilesEditorUtils::get_singleton()->queue_pattern_preview(tile_set, tile_set->get_pattern(i), callable_mp(this, &TileSetEditor::_pattern_preview_done));448}449450// Update the label visibility.451patterns_help_label->set_visible(patterns_item_list->get_item_count() == 0);452}453454void TileSetEditor::_tile_set_changed() {455tile_set_changed_needs_update = true;456}457458void TileSetEditor::_tab_changed(int p_tab_changed) {459split_container->set_visible(p_tab_changed == 0);460patterns_mc->set_visible(p_tab_changed == 1);461}462463void 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) {464EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);465ERR_FAIL_NULL(undo_redo_man);466467TileSet *ed_tile_set = Object::cast_to<TileSet>(p_edited);468if (!ed_tile_set) {469return;470}471472Vector<String> components = String(p_array_prefix).split("/", true, 2);473474// Compute the array indices to save.475int begin = 0;476int end;477if (p_array_prefix == "occlusion_layer_") {478end = ed_tile_set->get_occlusion_layers_count();479} else if (p_array_prefix == "physics_layer_") {480end = ed_tile_set->get_physics_layers_count();481} else if (p_array_prefix == "terrain_set_") {482end = ed_tile_set->get_terrain_sets_count();483} else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {484int terrain_set = components[0].trim_prefix("terrain_set_").to_int();485end = ed_tile_set->get_terrains_count(terrain_set);486} else if (p_array_prefix == "navigation_layer_") {487end = ed_tile_set->get_navigation_layers_count();488} else if (p_array_prefix == "custom_data_layer_") {489end = ed_tile_set->get_custom_data_layers_count();490} else {491ERR_FAIL_MSG("Invalid array prefix for TileSet.");492}493if (p_from_index < 0) {494// Adding new.495if (p_to_pos >= 0) {496begin = p_to_pos;497} else {498end = 0; // Nothing to save when adding at the end.499}500} else if (p_to_pos < 0) {501// Removing.502begin = p_from_index;503} else {504// Moving.505begin = MIN(p_from_index, p_to_pos);506end = MIN(MAX(p_from_index, p_to_pos) + 1, end);507}508509#define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property));510511// Add undo method to adding array element.512if (p_array_prefix == "occlusion_layer_") {513if (p_from_index < 0) {514undo_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);515}516} else if (p_array_prefix == "physics_layer_") {517if (p_from_index < 0) {518undo_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);519}520} else if (p_array_prefix == "terrain_set_") {521if (p_from_index < 0) {522undo_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);523}524} else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {525int terrain_set = components[0].trim_prefix("terrain_set_").to_int();526if (p_from_index < 0) {527undo_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);528}529} else if (p_array_prefix == "navigation_layer_") {530if (p_from_index < 0) {531undo_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);532}533} else if (p_array_prefix == "custom_data_layer_") {534if (p_from_index < 0) {535undo_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);536}537}538539// Save layers' properties.540List<PropertyInfo> properties;541ed_tile_set->get_property_list(&properties);542for (PropertyInfo pi : properties) {543if (pi.name.begins_with(p_array_prefix)) {544String str = pi.name.trim_prefix(p_array_prefix);545int to_char_index = 0;546while (to_char_index < str.length()) {547if (!is_digit(str[to_char_index])) {548break;549}550to_char_index++;551}552if (to_char_index > 0) {553int array_index = str.left(to_char_index).to_int();554if (array_index >= begin && array_index < end) {555ADD_UNDO(ed_tile_set, pi.name);556}557}558}559}560561// Save properties for TileSetAtlasSources tile data562for (int i = 0; i < ed_tile_set->get_source_count(); i++) {563int source_id = ed_tile_set->get_source_id(i);564565Ref<TileSetAtlasSource> tas = ed_tile_set->get_source(source_id);566if (tas.is_valid()) {567for (int j = 0; j < tas->get_tiles_count(); j++) {568Vector2i tile_id = tas->get_tile_id(j);569for (int k = 0; k < tas->get_alternative_tiles_count(tile_id); k++) {570int alternative_id = tas->get_alternative_tile_id(tile_id, k);571TileData *tile_data = tas->get_tile_data(tile_id, alternative_id);572ERR_FAIL_NULL(tile_data);573574// Actually saving stuff.575if (p_array_prefix == "occlusion_layer_") {576for (int layer_index = begin; layer_index < end; layer_index++) {577ADD_UNDO(tile_data, vformat("occlusion_layer_%d/polygon", layer_index));578}579} else if (p_array_prefix == "physics_layer_") {580for (int layer_index = begin; layer_index < end; layer_index++) {581ADD_UNDO(tile_data, vformat("physics_layer_%d/polygons_count", layer_index));582for (int polygon_index = 0; polygon_index < tile_data->get_collision_polygons_count(layer_index); polygon_index++) {583ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/points", layer_index, polygon_index));584ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/one_way", layer_index, polygon_index));585ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/one_way_margin", layer_index, polygon_index));586}587}588} else if (p_array_prefix == "terrain_set_") {589ADD_UNDO(tile_data, "terrain_set");590for (int terrain_set_index = begin; terrain_set_index < end; terrain_set_index++) {591for (int l = 0; l < TileSet::CELL_NEIGHBOR_MAX; l++) {592TileSet::CellNeighbor bit = TileSet::CellNeighbor(l);593if (tile_data->is_valid_terrain_peering_bit(bit)) {594ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[l]));595}596}597}598} else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {599for (int terrain_index = 0; terrain_index < TileSet::CELL_NEIGHBOR_MAX; terrain_index++) {600TileSet::CellNeighbor bit = TileSet::CellNeighbor(terrain_index);601if (tile_data->is_valid_terrain_peering_bit(bit)) {602ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[terrain_index]));603}604}605} else if (p_array_prefix == "navigation_layer_") {606for (int layer_index = begin; layer_index < end; layer_index++) {607ADD_UNDO(tile_data, vformat("navigation_layer_%d/polygon", layer_index));608}609} else if (p_array_prefix == "custom_data_layer_") {610for (int layer_index = begin; layer_index < end; layer_index++) {611ADD_UNDO(tile_data, vformat("custom_data_%d", layer_index));612}613}614}615}616}617}618#undef ADD_UNDO619620// Add do method to add/remove array element.621if (p_array_prefix == "occlusion_layer_") {622if (p_from_index < 0) {623undo_redo_man->add_do_method(ed_tile_set, "add_occlusion_layer", p_to_pos);624} else if (p_to_pos < 0) {625undo_redo_man->add_do_method(ed_tile_set, "remove_occlusion_layer", p_from_index);626} else {627undo_redo_man->add_do_method(ed_tile_set, "move_occlusion_layer", p_from_index, p_to_pos);628}629} else if (p_array_prefix == "physics_layer_") {630if (p_from_index < 0) {631undo_redo_man->add_do_method(ed_tile_set, "add_physics_layer", p_to_pos);632} else if (p_to_pos < 0) {633undo_redo_man->add_do_method(ed_tile_set, "remove_physics_layer", p_from_index);634} else {635undo_redo_man->add_do_method(ed_tile_set, "move_physics_layer", p_from_index, p_to_pos);636}637} else if (p_array_prefix == "terrain_set_") {638if (p_from_index < 0) {639undo_redo_man->add_do_method(ed_tile_set, "add_terrain_set", p_to_pos);640} else if (p_to_pos < 0) {641undo_redo_man->add_do_method(ed_tile_set, "remove_terrain_set", p_from_index);642} else {643undo_redo_man->add_do_method(ed_tile_set, "move_terrain_set", p_from_index, p_to_pos);644}645} else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {646int terrain_set = components[0].trim_prefix("terrain_set_").to_int();647if (p_from_index < 0) {648undo_redo_man->add_do_method(ed_tile_set, "add_terrain", terrain_set, p_to_pos);649} else if (p_to_pos < 0) {650undo_redo_man->add_do_method(ed_tile_set, "remove_terrain", terrain_set, p_from_index);651} else {652undo_redo_man->add_do_method(ed_tile_set, "move_terrain", terrain_set, p_from_index, p_to_pos);653}654} else if (p_array_prefix == "navigation_layer_") {655if (p_from_index < 0) {656undo_redo_man->add_do_method(ed_tile_set, "add_navigation_layer", p_to_pos);657} else if (p_to_pos < 0) {658undo_redo_man->add_do_method(ed_tile_set, "remove_navigation_layer", p_from_index);659} else {660undo_redo_man->add_do_method(ed_tile_set, "move_navigation_layer", p_from_index, p_to_pos);661}662} else if (p_array_prefix == "custom_data_layer_") {663if (p_from_index < 0) {664undo_redo_man->add_do_method(ed_tile_set, "add_custom_data_layer", p_to_pos);665} else if (p_to_pos < 0) {666undo_redo_man->add_do_method(ed_tile_set, "remove_custom_data_layer", p_from_index);667} else {668undo_redo_man->add_do_method(ed_tile_set, "move_custom_data_layer", p_from_index, p_to_pos);669}670}671}672673void TileSetEditor::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, const String &p_property, const Variant &p_new_value) {674EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);675ERR_FAIL_NULL(undo_redo_man);676677#define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property));678TileSet *ed_tile_set = Object::cast_to<TileSet>(p_edited);679if (ed_tile_set) {680Vector<String> components = p_property.split("/", true, 3);681for (int i = 0; i < ed_tile_set->get_source_count(); i++) {682int source_id = ed_tile_set->get_source_id(i);683684Ref<TileSetAtlasSource> tas = ed_tile_set->get_source(source_id);685if (tas.is_valid()) {686for (int j = 0; j < tas->get_tiles_count(); j++) {687Vector2i tile_id = tas->get_tile_id(j);688for (int k = 0; k < tas->get_alternative_tiles_count(tile_id); k++) {689int alternative_id = tas->get_alternative_tile_id(tile_id, k);690TileData *tile_data = tas->get_tile_data(tile_id, alternative_id);691ERR_FAIL_NULL(tile_data);692693if (components.size() == 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "mode") {694ADD_UNDO(tile_data, "terrain_set");695ADD_UNDO(tile_data, "terrain");696for (int l = 0; l < TileSet::CELL_NEIGHBOR_MAX; l++) {697TileSet::CellNeighbor bit = TileSet::CellNeighbor(l);698if (tile_data->is_valid_terrain_peering_bit(bit)) {699ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[l]));700}701}702} 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") {703int custom_data_layer = components[0].trim_prefix("custom_data_layer_").is_valid_int();704ADD_UNDO(tile_data, vformat("custom_data_%d", custom_data_layer));705}706}707}708}709}710}711#undef ADD_UNDO712}713714void TileSetEditor::edit(Ref<TileSet> p_tile_set) {715bool new_read_only_state = false;716if (p_tile_set.is_valid()) {717new_read_only_state = EditorNode::get_singleton()->is_resource_read_only(p_tile_set);718}719720if (p_tile_set == tile_set && new_read_only_state == read_only) {721return;722}723724// Remove listener.725if (tile_set.is_valid()) {726tile_set->disconnect_changed(callable_mp(this, &TileSetEditor::_tile_set_changed));727}728729// Change the edited object.730tile_set = p_tile_set;731sources_list->tile_set = p_tile_set;732733// Read-only status is false by default734read_only = new_read_only_state;735736// Add the listener again and check for read-only status.737if (tile_set.is_valid()) {738sources_add_button->set_disabled(read_only);739sources_advanced_menu_button->set_disabled(read_only);740source_sort_button->set_disabled(read_only);741742tile_set->connect_changed(callable_mp(this, &TileSetEditor::_tile_set_changed));743if (first_edit) {744_set_source_sort(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "tile_source_sort", 0));745first_edit = false;746} else {747_update_sources_list();748}749_update_patterns_list();750}751}752753void TileSetEditor::add_expanded_editor(Control *p_editor) {754expanded_editor = p_editor;755expanded_editor_parent = p_editor->get_parent()->get_instance_id();756757// Find the scrollable control this node belongs to.758Node *check_parent = expanded_editor->get_parent();759Control *parent_container = nullptr;760while (check_parent) {761parent_container = Object::cast_to<EditorInspector>(check_parent);762if (parent_container) {763break;764}765parent_container = Object::cast_to<ScrollContainer>(check_parent);766if (parent_container) {767break;768}769check_parent = check_parent->get_parent();770}771ERR_FAIL_NULL(parent_container);772773expanded_editor->set_meta("reparented", true);774expanded_editor->reparent(expanded_area);775expanded_area->show();776expanded_area->set_size(Vector2(parent_container->get_global_rect().get_end().x - expanded_area->get_global_position().x, expanded_area->get_size().y));777778for (SplitContainer *split : disable_on_expand) {779split->set_dragger_visibility(SplitContainer::DRAGGER_HIDDEN);780}781}782783void TileSetEditor::remove_expanded_editor() {784if (!expanded_editor) {785return;786}787788Node *original_parent = ObjectDB::get_instance<Node>(expanded_editor_parent);789if (original_parent) {790expanded_editor->remove_meta("reparented");791expanded_editor->reparent(original_parent);792} else {793expanded_editor->queue_free();794}795expanded_editor = nullptr;796expanded_editor_parent = ObjectID();797expanded_area->hide();798799for (SplitContainer *split : disable_on_expand) {800split->set_dragger_visibility(SplitContainer::DRAGGER_VISIBLE);801}802}803804void TileSetEditor::register_split(SplitContainer *p_split) {805disable_on_expand.push_back(p_split);806}807808TileSetEditor::TileSetEditor() {809singleton = this;810set_name(TTRC("TileSet"));811set_icon_name("TileSet");812set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_tile_set_bottom_panel", TTRC("Open TileSet Dock")));813set_default_slot(EditorDock::DOCK_SLOT_BOTTOM);814set_available_layouts(EditorDock::DOCK_LAYOUT_HORIZONTAL | EditorDock::DOCK_LAYOUT_FLOATING);815set_global(false);816set_transient(true);817818set_process_internal(true);819820VBoxContainer *main_vb = memnew(VBoxContainer);821add_child(main_vb);822main_vb->set_anchors_and_offsets_preset(PRESET_FULL_RECT);823824// TabBar.825tabs_bar = memnew(TabBar);826tabs_bar->set_theme_type_variation("TabBarInner");827tabs_bar->set_tab_alignment(TabBar::ALIGNMENT_CENTER);828tabs_bar->set_clip_tabs(false);829tabs_bar->add_tab(TTR("Tile Sources"));830tabs_bar->add_tab(TTR("Patterns"));831tabs_bar->connect("tab_changed", callable_mp(this, &TileSetEditor::_tab_changed));832833tile_set_toolbar = memnew(HBoxContainer);834tile_set_toolbar->set_h_size_flags(SIZE_EXPAND_FILL);835836PanelContainer *tabs_panel = memnew(PanelContainer);837tabs_panel->set_theme_type_variation("PanelContainerTabbarInner");838tabs_panel->add_child(tabs_bar);839tile_set_toolbar->add_child(tabs_panel);840main_vb->add_child(tile_set_toolbar);841842//// Tiles ////843// Split container.844split_container = memnew(HSplitContainer);845split_container->set_name(TTR("Tiles"));846split_container->set_h_size_flags(SIZE_EXPAND_FILL);847split_container->set_v_size_flags(SIZE_EXPAND_FILL);848main_vb->add_child(split_container);849850// Sources list.851VBoxContainer *split_container_left_side = memnew(VBoxContainer);852split_container_left_side->set_h_size_flags(SIZE_EXPAND_FILL);853split_container_left_side->set_v_size_flags(SIZE_EXPAND_FILL);854split_container_left_side->set_stretch_ratio(0.25);855split_container_left_side->set_custom_minimum_size(Size2(70, 0) * EDSCALE);856split_container->add_child(split_container_left_side);857858source_sort_button = memnew(MenuButton);859source_sort_button->set_flat(false);860source_sort_button->set_theme_type_variation(SceneStringName(FlatButton));861source_sort_button->set_tooltip_text(TTR("Sort Sources"));862863PopupMenu *p = source_sort_button->get_popup();864p->connect(SceneStringName(id_pressed), callable_mp(this, &TileSetEditor::_set_source_sort));865p->add_radio_check_item(TTR("Sort by ID (Ascending)"), TilesEditorUtils::SOURCE_SORT_ID);866p->add_radio_check_item(TTR("Sort by ID (Descending)"), TilesEditorUtils::SOURCE_SORT_ID_REVERSE);867p->add_radio_check_item(TTR("Sort by Name (Ascending)"), TilesEditorUtils::SOURCE_SORT_NAME);868p->add_radio_check_item(TTR("Sort by Name (Descending)"), TilesEditorUtils::SOURCE_SORT_NAME_REVERSE);869p->set_item_checked(TilesEditorUtils::SOURCE_SORT_ID, true);870871sources_list = memnew(TileSetSourceItemList);872sources_list->connect(SceneStringName(item_selected), callable_mp(this, &TileSetEditor::_source_selected));873sources_list->connect(SceneStringName(item_selected), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::set_sources_lists_current));874sources_list->connect(SceneStringName(visibility_changed), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::synchronize_sources_list).bind(sources_list, source_sort_button));875sources_list->connect("sort_request", callable_mp(this, &TileSetEditor::_update_sources_list).bind(-1));876SET_DRAG_FORWARDING_CDU(sources_list, TileSetEditor);877split_container_left_side->add_child(sources_list);878879HBoxContainer *sources_bottom_actions = memnew(HBoxContainer);880sources_bottom_actions->set_alignment(BoxContainer::ALIGNMENT_END);881split_container_left_side->add_child(sources_bottom_actions);882883sources_delete_button = memnew(Button);884sources_delete_button->set_theme_type_variation(SceneStringName(FlatButton));885sources_delete_button->set_disabled(true);886sources_delete_button->connect(SceneStringName(pressed), callable_mp(this, &TileSetEditor::_source_delete_pressed));887sources_bottom_actions->add_child(sources_delete_button);888889sources_add_button = memnew(MenuButton);890sources_add_button->set_flat(false);891sources_add_button->set_theme_type_variation(SceneStringName(FlatButton));892sources_add_button->get_popup()->add_item(TTR("Atlas"));893sources_add_button->get_popup()->set_item_tooltip(-1, TTR("A palette of tiles made from a texture."));894sources_add_button->get_popup()->add_item(TTR("Scenes Collection"));895sources_add_button->get_popup()->set_item_tooltip(-1, TTR("A collection of scenes that can be instantiated and placed as tiles."));896sources_add_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TileSetEditor::_source_add_id_pressed));897sources_bottom_actions->add_child(sources_add_button);898899sources_advanced_menu_button = memnew(MenuButton);900sources_advanced_menu_button->set_flat(false);901sources_advanced_menu_button->set_theme_type_variation(SceneStringName(FlatButton));902sources_advanced_menu_button->get_popup()->add_item(TTR("Open Atlas Merging Tool"));903sources_advanced_menu_button->get_popup()->add_item(TTR("Manage Tile Proxies"));904sources_advanced_menu_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TileSetEditor::_sources_advanced_menu_id_pressed));905sources_advanced_menu_button->set_accessibility_name(TTRC("Advanced"));906sources_bottom_actions->add_child(sources_advanced_menu_button);907sources_bottom_actions->add_child(source_sort_button);908909atlas_merging_dialog = memnew(AtlasMergingDialog);910add_child(atlas_merging_dialog);911912tile_proxies_manager_dialog = memnew(TileProxiesManagerDialog);913add_child(tile_proxies_manager_dialog);914915// Right side container.916VBoxContainer *split_container_right_side = memnew(VBoxContainer);917split_container_right_side->set_h_size_flags(SIZE_EXPAND_FILL);918split_container_right_side->set_v_size_flags(SIZE_EXPAND_FILL);919split_container->add_child(split_container_right_side);920921// No source selected.922no_source_selected_label = memnew(Label);923no_source_selected_label->set_focus_mode(FOCUS_ACCESSIBILITY);924no_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."));925no_source_selected_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);926no_source_selected_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);927no_source_selected_label->set_h_size_flags(SIZE_EXPAND_FILL);928no_source_selected_label->set_v_size_flags(SIZE_EXPAND_FILL);929no_source_selected_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);930no_source_selected_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);931split_container_right_side->add_child(no_source_selected_label);932933// Atlases editor.934tile_set_atlas_source_editor = memnew(TileSetAtlasSourceEditor);935tile_set_atlas_source_editor->set_h_size_flags(SIZE_EXPAND_FILL);936tile_set_atlas_source_editor->set_v_size_flags(SIZE_EXPAND_FILL);937tile_set_atlas_source_editor->connect("source_id_changed", callable_mp(this, &TileSetEditor::_update_sources_list));938split_container_right_side->add_child(tile_set_atlas_source_editor);939tile_set_atlas_source_editor->hide();940941// Scenes collection editor.942tile_set_scenes_collection_source_editor = memnew(TileSetScenesCollectionSourceEditor);943tile_set_scenes_collection_source_editor->set_h_size_flags(SIZE_EXPAND_FILL);944tile_set_scenes_collection_source_editor->set_v_size_flags(SIZE_EXPAND_FILL);945tile_set_scenes_collection_source_editor->connect("source_id_changed", callable_mp(this, &TileSetEditor::_update_sources_list));946split_container_right_side->add_child(tile_set_scenes_collection_source_editor);947tile_set_scenes_collection_source_editor->hide();948949patterns_mc = memnew(MarginContainer);950patterns_mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);951patterns_mc->set_theme_type_variation("NoBorderBottomPanel");952main_vb->add_child(patterns_mc);953patterns_mc->hide();954955//// Patterns ////956int thumbnail_size = 64;957patterns_item_list = memnew(ItemList);958patterns_item_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);959patterns_item_list->set_max_columns(0);960patterns_item_list->set_icon_mode(ItemList::ICON_MODE_TOP);961patterns_item_list->set_fixed_column_width(thumbnail_size * 3 / 2);962patterns_item_list->set_max_text_lines(2);963patterns_item_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));964patterns_item_list->set_scroll_hint_mode(ItemList::SCROLL_HINT_MODE_BOTH);965patterns_item_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);966patterns_item_list->connect(SceneStringName(gui_input), callable_mp(this, &TileSetEditor::_patterns_item_list_gui_input));967patterns_mc->add_child(patterns_item_list);968969patterns_help_label = memnew(Label);970patterns_help_label->set_focus_mode(FOCUS_ACCESSIBILITY);971patterns_help_label->set_text(TTR("Add new patterns in the TileMap editing mode."));972patterns_help_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);973patterns_help_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);974patterns_help_label->set_anchors_and_offsets_preset(Control::PRESET_HCENTER_WIDE);975patterns_item_list->add_child(patterns_help_label);976977// Expanded editor978expanded_area = memnew(PanelContainer);979add_child(expanded_area);980expanded_area->set_anchors_and_offsets_preset(PRESET_LEFT_WIDE);981expanded_area->hide();982983// Registers UndoRedo inspector callback.984EditorNode::get_editor_data().add_move_array_element_function(SNAME("TileSet"), callable_mp(this, &TileSetEditor::_move_tile_set_array_element));985EditorNode::get_editor_data().add_undo_redo_inspector_hook_callback(callable_mp(this, &TileSetEditor::_undo_redo_inspector_callback));986}987988void TileSourceInspectorPlugin::_show_id_edit_dialog(Object *p_for_source) {989if (!id_edit_dialog) {990id_edit_dialog = memnew(ConfirmationDialog);991TileSetEditor::get_singleton()->add_child(id_edit_dialog);992993VBoxContainer *vbox = memnew(VBoxContainer);994id_edit_dialog->add_child(vbox);995996Label *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.")));997label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);998// Workaround too tall popup window due to text autowrapping. See GH-83546.999label->set_custom_minimum_size(Vector2i(400, 0));1000vbox->add_child(label);10011002id_input = memnew(SpinBox);1003vbox->add_child(id_input);1004id_input->set_max(INT_MAX);10051006id_edit_dialog->connect(SceneStringName(confirmed), callable_mp(this, &TileSourceInspectorPlugin::_confirm_change_id));1007}1008edited_source = p_for_source;1009id_input->set_value(p_for_source->get("id"));1010id_edit_dialog->popup_centered(Vector2i(400, 0) * EDSCALE);1011callable_mp((Control *)id_input->get_line_edit(), &Control::grab_focus).call_deferred(false);1012}10131014void TileSourceInspectorPlugin::_confirm_change_id() {1015edited_source->set("id", id_input->get_value());1016id_label->set_text(itos(edited_source->get("id"))); // Use get(), because the provided ID might've been invalid.1017}10181019bool TileSourceInspectorPlugin::can_handle(Object *p_object) {1020return p_object && (p_object->is_class("TileSetAtlasSourceProxyObject") || p_object->is_class("TileSetScenesCollectionProxyObject"));1021}10221023bool 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) {1024if (p_path == "id") {1025const Variant value = p_object->get("id");1026if (value.get_type() == Variant::NIL) { // May happen if the object is not yet initialized.1027return true;1028}10291030EditorProperty *ep = memnew(EditorProperty);10311032HBoxContainer *hbox = memnew(HBoxContainer);1033hbox->set_alignment(BoxContainer::ALIGNMENT_CENTER);10341035id_label = memnew(Label(itos(value)));1036id_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);1037hbox->add_child(id_label);10381039Button *button = memnew(Button(TTR("Edit")));1040button->set_h_size_flags(Control::SIZE_EXPAND_FILL);1041hbox->add_child(button);1042button->connect(SceneStringName(pressed), callable_mp(this, &TileSourceInspectorPlugin::_show_id_edit_dialog).bind(p_object));10431044ep->add_child(hbox);1045add_property_editor(p_path, ep);1046return true;1047}1048return false;1049}105010511052