Path: blob/master/editor/scene/2d/tiles/tile_set_editor.cpp
9906 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_settings.h"41#include "editor/themes/editor_scale.h"4243#include "scene/gui/box_container.h"44#include "scene/gui/control.h"45#include "scene/gui/dialogs.h"4647TileSetEditor *TileSetEditor::singleton = nullptr;4849void TileSetEditor::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {50ERR_FAIL_COND(tile_set.is_null());5152if (!_can_drop_data_fw(p_point, p_data, p_from)) {53return;54}5556if (p_from == sources_list) {57// Handle dropping a texture in the list of atlas resources.58Dictionary d = p_data;59Vector<String> files = d["files"];60_load_texture_files(files);61}62}6364bool TileSetEditor::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {65ERR_FAIL_COND_V(tile_set.is_null(), false);6667if (read_only) {68return false;69}7071if (p_from == sources_list) {72Dictionary d = p_data;7374if (!d.has("type")) {75return false;76}7778// Check if we have a Texture2D.79if (String(d["type"]) == "files") {80Vector<String> files = d["files"];8182if (files.is_empty()) {83return false;84}8586for (int i = 0; i < files.size(); i++) {87String ftype = EditorFileSystem::get_singleton()->get_file_type(files[i]);8889if (!ClassDB::is_parent_class(ftype, "Texture2D")) {90return false;91}92}9394return true;95}96}97return false;98}99100void TileSetEditor::_load_texture_files(const Vector<String> &p_paths) {101int source_id = TileSet::INVALID_SOURCE;102Vector<Ref<TileSetAtlasSource>> atlases;103104for (const String &p_path : p_paths) {105Ref<Texture2D> texture = ResourceLoader::load(p_path);106107if (texture.is_null()) {108EditorNode::get_singleton()->show_warning(TTR("Invalid texture selected."));109continue;110}111112// Retrieve the id for the next created source.113source_id = tile_set->get_next_source_id();114115// Actually create the new source.116Ref<TileSetAtlasSource> atlas_source = memnew(TileSetAtlasSource);117atlas_source->set_texture(texture);118atlas_source->set_texture_region_size(tile_set->get_tile_size());119120EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();121undo_redo->create_action(TTR("Add a new atlas source"));122undo_redo->add_do_method(*tile_set, "add_source", atlas_source, source_id);123undo_redo->add_undo_method(*tile_set, "remove_source", source_id);124undo_redo->commit_action();125126atlases.append(atlas_source);127}128129if (!atlases.is_empty()) {130tile_set_atlas_source_editor->init_new_atlases(atlases);131}132133// Update the selected source (thus triggering an update).134_update_sources_list(source_id);135}136137void TileSetEditor::_update_sources_list(int force_selected_id) {138if (tile_set.is_null()) {139return;140}141142// Get the previously selected id.143int old_selected = TileSet::INVALID_SOURCE;144if (sources_list->get_current() >= 0) {145int source_id = sources_list->get_item_metadata(sources_list->get_current());146if (tile_set->has_source(source_id)) {147old_selected = source_id;148}149}150151int to_select = TileSet::INVALID_SOURCE;152if (force_selected_id >= 0) {153to_select = force_selected_id;154} else if (old_selected >= 0) {155to_select = old_selected;156}157158// Clear the list.159sources_list->clear();160161// Update the atlas sources.162List<int> source_ids = TilesEditorUtils::get_singleton()->get_sorted_sources(tile_set);163for (const int &source_id : source_ids) {164TileSetSource *source = *tile_set->get_source(source_id);165166Ref<Texture2D> texture;167String item_text;168169// Common to all type of sources.170if (!source->get_name().is_empty()) {171item_text = source->get_name();172}173174// Atlas source.175TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);176if (atlas_source) {177texture = atlas_source->get_texture();178if (item_text.is_empty()) {179if (texture.is_valid()) {180item_text = texture->get_path().get_file();181} else {182item_text = vformat(TTR("No Texture Atlas Source (ID: %d)"), source_id);183}184}185}186187// Scene collection source.188TileSetScenesCollectionSource *scene_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);189if (scene_collection_source) {190texture = get_editor_theme_icon(SNAME("PackedScene"));191if (item_text.is_empty()) {192if (scene_collection_source->get_scene_tiles_count() > 0) {193item_text = vformat(TTR("Scene Collection Source (ID: %d)"), source_id);194} else {195item_text = vformat(TTR("Empty Scene Collection Source (ID: %d)"), source_id);196}197}198}199200// Use default if not valid.201if (item_text.is_empty()) {202item_text = vformat(TTR("Unknown Type Source (ID: %d)"), source_id);203}204if (texture.is_null()) {205texture = missing_texture_texture;206}207208sources_list->add_item(item_text, texture);209sources_list->set_item_metadata(-1, source_id);210}211212// Set again the current selected item if needed.213if (to_select >= 0) {214for (int i = 0; i < sources_list->get_item_count(); i++) {215if ((int)sources_list->get_item_metadata(i) == to_select) {216sources_list->set_current(i);217sources_list->ensure_current_is_visible();218if (old_selected != to_select) {219sources_list->emit_signal(SceneStringName(item_selected), sources_list->get_current());220}221break;222}223}224}225226// If nothing is selected, select the first entry.227if (sources_list->get_current() < 0 && sources_list->get_item_count() > 0) {228sources_list->set_current(0);229if (old_selected != int(sources_list->get_item_metadata(0))) {230sources_list->emit_signal(SceneStringName(item_selected), sources_list->get_current());231}232}233234// If there is no source left, hide all editors and show the label.235_source_selected(sources_list->get_current());236237// Synchronize the lists.238TilesEditorUtils::get_singleton()->set_sources_lists_current(sources_list->get_current());239}240241void TileSetEditor::_source_selected(int p_source_index) {242ERR_FAIL_COND(tile_set.is_null());243244// Update the selected source.245sources_delete_button->set_disabled(p_source_index < 0 || read_only);246247if (p_source_index >= 0) {248int source_id = sources_list->get_item_metadata(p_source_index);249TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id));250TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(*tile_set->get_source(source_id));251if (atlas_source) {252no_source_selected_label->hide();253tile_set_atlas_source_editor->edit(*tile_set, atlas_source, source_id);254tile_set_atlas_source_editor->show();255tile_set_scenes_collection_source_editor->hide();256} else if (scenes_collection_source) {257no_source_selected_label->hide();258tile_set_atlas_source_editor->hide();259tile_set_scenes_collection_source_editor->edit(*tile_set, scenes_collection_source, source_id);260tile_set_scenes_collection_source_editor->show();261} else {262no_source_selected_label->show();263tile_set_atlas_source_editor->hide();264tile_set_scenes_collection_source_editor->hide();265}266} else {267no_source_selected_label->show();268tile_set_atlas_source_editor->hide();269tile_set_scenes_collection_source_editor->hide();270}271}272273void TileSetEditor::_source_delete_pressed() {274ERR_FAIL_COND(tile_set.is_null());275276// Update the selected source.277int to_delete = sources_list->get_item_metadata(sources_list->get_current());278279Ref<TileSetSource> source = tile_set->get_source(to_delete);280281// Remove the source.282EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();283undo_redo->create_action(TTR("Remove source"));284undo_redo->add_do_method(*tile_set, "remove_source", to_delete);285undo_redo->add_undo_method(*tile_set, "add_source", source, to_delete);286undo_redo->commit_action();287288_update_sources_list();289}290291void TileSetEditor::_source_add_id_pressed(int p_id_pressed) {292ERR_FAIL_COND(tile_set.is_null());293294switch (p_id_pressed) {295case 0: {296if (!texture_file_dialog) {297texture_file_dialog = memnew(EditorFileDialog);298add_child(texture_file_dialog);299texture_file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);300texture_file_dialog->connect("files_selected", callable_mp(this, &TileSetEditor::_load_texture_files));301302List<String> extensions;303ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);304for (const String &E : extensions) {305texture_file_dialog->add_filter("*." + E, E.to_upper());306}307}308texture_file_dialog->popup_file_dialog();309} break;310case 1: {311int source_id = tile_set->get_next_source_id();312313Ref<TileSetScenesCollectionSource> scene_collection_source = memnew(TileSetScenesCollectionSource);314315// Add a new source.316EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();317undo_redo->create_action(TTR("Add atlas source"));318undo_redo->add_do_method(*tile_set, "add_source", scene_collection_source, source_id);319undo_redo->add_undo_method(*tile_set, "remove_source", source_id);320undo_redo->commit_action();321322_update_sources_list(source_id);323} break;324default:325ERR_FAIL();326}327}328329void TileSetEditor::_sources_advanced_menu_id_pressed(int p_id_pressed) {330ERR_FAIL_COND(tile_set.is_null());331332switch (p_id_pressed) {333case 0: {334atlas_merging_dialog->update_tile_set(tile_set);335atlas_merging_dialog->popup_centered_ratio(0.5);336} break;337case 1: {338tile_proxies_manager_dialog->update_tile_set(tile_set);339tile_proxies_manager_dialog->popup_centered_ratio(0.5);340} break;341}342}343344void TileSetEditor::_set_source_sort(int p_sort) {345TilesEditorUtils::get_singleton()->set_sorting_option(p_sort);346for (int i = 0; i != TilesEditorUtils::SOURCE_SORT_MAX; i++) {347source_sort_button->get_popup()->set_item_checked(i, (i == (int)p_sort));348}349350int old_selected = TileSet::INVALID_SOURCE;351if (sources_list->get_current() >= 0) {352int source_id = sources_list->get_item_metadata(sources_list->get_current());353if (tile_set->has_source(source_id)) {354old_selected = source_id;355}356}357_update_sources_list(old_selected);358359if (!first_edit) {360EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "tile_source_sort", p_sort);361}362}363364void TileSetEditor::_notification(int p_what) {365switch (p_what) {366case NOTIFICATION_THEME_CHANGED: {367sources_delete_button->set_button_icon(get_editor_theme_icon(SNAME("Remove")));368sources_add_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));369source_sort_button->set_button_icon(get_editor_theme_icon(SNAME("Sort")));370sources_advanced_menu_button->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));371missing_texture_texture = get_editor_theme_icon(SNAME("TileSet"));372expanded_area->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), "Tree"));373_update_sources_list();374} break;375376case NOTIFICATION_INTERNAL_PROCESS: {377if (tile_set_changed_needs_update) {378if (tile_set.is_valid()) {379tile_set->set_edited(true);380}381382read_only = false;383if (tile_set.is_valid()) {384read_only = EditorNode::get_singleton()->is_resource_read_only(tile_set);385}386387_update_sources_list();388_update_patterns_list();389390sources_add_button->set_disabled(read_only);391sources_advanced_menu_button->set_disabled(read_only);392source_sort_button->set_disabled(read_only);393394tile_set_changed_needs_update = false;395}396} break;397398case NOTIFICATION_VISIBILITY_CHANGED: {399if (!is_visible_in_tree()) {400remove_expanded_editor();401}402} break;403}404}405406void TileSetEditor::_patterns_item_list_gui_input(const Ref<InputEvent> &p_event) {407ERR_FAIL_COND(tile_set.is_null());408409if (EditorNode::get_singleton()->is_resource_read_only(tile_set)) {410return;411}412413if (ED_IS_SHORTCUT("tiles_editor/delete", p_event) && p_event->is_pressed() && !p_event->is_echo()) {414Vector<int> selected = patterns_item_list->get_selected_items();415EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();416undo_redo->create_action(TTR("Remove TileSet patterns"));417for (int i = 0; i < selected.size(); i++) {418int pattern_index = selected[i];419undo_redo->add_do_method(*tile_set, "remove_pattern", pattern_index);420undo_redo->add_undo_method(*tile_set, "add_pattern", tile_set->get_pattern(pattern_index), pattern_index);421}422undo_redo->commit_action();423patterns_item_list->accept_event();424}425}426427void TileSetEditor::_pattern_preview_done(Ref<TileMapPattern> p_pattern, Ref<Texture2D> p_texture) {428// TODO optimize ?429for (int i = 0; i < patterns_item_list->get_item_count(); i++) {430if (patterns_item_list->get_item_metadata(i) == p_pattern) {431patterns_item_list->set_item_icon(i, p_texture);432break;433}434}435}436437void TileSetEditor::_update_patterns_list() {438ERR_FAIL_COND(tile_set.is_null());439440// Recreate the items.441patterns_item_list->clear();442for (int i = 0; i < tile_set->get_patterns_count(); i++) {443int id = patterns_item_list->add_item("");444patterns_item_list->set_item_metadata(id, tile_set->get_pattern(i));445patterns_item_list->set_item_tooltip(id, vformat(TTR("Index: %d"), i));446TilesEditorUtils::get_singleton()->queue_pattern_preview(tile_set, tile_set->get_pattern(i), callable_mp(this, &TileSetEditor::_pattern_preview_done));447}448449// Update the label visibility.450patterns_help_label->set_visible(patterns_item_list->get_item_count() == 0);451}452453void TileSetEditor::_tile_set_changed() {454tile_set_changed_needs_update = true;455}456457void TileSetEditor::_tab_changed(int p_tab_changed) {458split_container->set_visible(p_tab_changed == 0);459patterns_item_list->set_visible(p_tab_changed == 1);460}461462void 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) {463EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);464ERR_FAIL_NULL(undo_redo_man);465466TileSet *ed_tile_set = Object::cast_to<TileSet>(p_edited);467if (!ed_tile_set) {468return;469}470471Vector<String> components = String(p_array_prefix).split("/", true, 2);472473// Compute the array indices to save.474int begin = 0;475int end;476if (p_array_prefix == "occlusion_layer_") {477end = ed_tile_set->get_occlusion_layers_count();478} else if (p_array_prefix == "physics_layer_") {479end = ed_tile_set->get_physics_layers_count();480} else if (p_array_prefix == "terrain_set_") {481end = ed_tile_set->get_terrain_sets_count();482} else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {483int terrain_set = components[0].trim_prefix("terrain_set_").to_int();484end = ed_tile_set->get_terrains_count(terrain_set);485} else if (p_array_prefix == "navigation_layer_") {486end = ed_tile_set->get_navigation_layers_count();487} else if (p_array_prefix == "custom_data_layer_") {488end = ed_tile_set->get_custom_data_layers_count();489} else {490ERR_FAIL_MSG("Invalid array prefix for TileSet.");491}492if (p_from_index < 0) {493// Adding new.494if (p_to_pos >= 0) {495begin = p_to_pos;496} else {497end = 0; // Nothing to save when adding at the end.498}499} else if (p_to_pos < 0) {500// Removing.501begin = p_from_index;502} else {503// Moving.504begin = MIN(p_from_index, p_to_pos);505end = MIN(MAX(p_from_index, p_to_pos) + 1, end);506}507508#define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property));509510// Add undo method to adding array element.511if (p_array_prefix == "occlusion_layer_") {512if (p_from_index < 0) {513undo_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);514}515} else if (p_array_prefix == "physics_layer_") {516if (p_from_index < 0) {517undo_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);518}519} else if (p_array_prefix == "terrain_set_") {520if (p_from_index < 0) {521undo_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);522}523} else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {524int terrain_set = components[0].trim_prefix("terrain_set_").to_int();525if (p_from_index < 0) {526undo_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);527}528} else if (p_array_prefix == "navigation_layer_") {529if (p_from_index < 0) {530undo_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);531}532} else if (p_array_prefix == "custom_data_layer_") {533if (p_from_index < 0) {534undo_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);535}536}537538// Save layers' properties.539List<PropertyInfo> properties;540ed_tile_set->get_property_list(&properties);541for (PropertyInfo pi : properties) {542if (pi.name.begins_with(p_array_prefix)) {543String str = pi.name.trim_prefix(p_array_prefix);544int to_char_index = 0;545while (to_char_index < str.length()) {546if (!is_digit(str[to_char_index])) {547break;548}549to_char_index++;550}551if (to_char_index > 0) {552int array_index = str.left(to_char_index).to_int();553if (array_index >= begin && array_index < end) {554ADD_UNDO(ed_tile_set, pi.name);555}556}557}558}559560// Save properties for TileSetAtlasSources tile data561for (int i = 0; i < ed_tile_set->get_source_count(); i++) {562int source_id = ed_tile_set->get_source_id(i);563564Ref<TileSetAtlasSource> tas = ed_tile_set->get_source(source_id);565if (tas.is_valid()) {566for (int j = 0; j < tas->get_tiles_count(); j++) {567Vector2i tile_id = tas->get_tile_id(j);568for (int k = 0; k < tas->get_alternative_tiles_count(tile_id); k++) {569int alternative_id = tas->get_alternative_tile_id(tile_id, k);570TileData *tile_data = tas->get_tile_data(tile_id, alternative_id);571ERR_FAIL_NULL(tile_data);572573// Actually saving stuff.574if (p_array_prefix == "occlusion_layer_") {575for (int layer_index = begin; layer_index < end; layer_index++) {576ADD_UNDO(tile_data, vformat("occlusion_layer_%d/polygon", layer_index));577}578} else if (p_array_prefix == "physics_layer_") {579for (int layer_index = begin; layer_index < end; layer_index++) {580ADD_UNDO(tile_data, vformat("physics_layer_%d/polygons_count", layer_index));581for (int polygon_index = 0; polygon_index < tile_data->get_collision_polygons_count(layer_index); polygon_index++) {582ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/points", layer_index, polygon_index));583ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/one_way", layer_index, polygon_index));584ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/one_way_margin", layer_index, polygon_index));585}586}587} else if (p_array_prefix == "terrain_set_") {588ADD_UNDO(tile_data, "terrain_set");589for (int terrain_set_index = begin; terrain_set_index < end; terrain_set_index++) {590for (int l = 0; l < TileSet::CELL_NEIGHBOR_MAX; l++) {591TileSet::CellNeighbor bit = TileSet::CellNeighbor(l);592if (tile_data->is_valid_terrain_peering_bit(bit)) {593ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[l]));594}595}596}597} else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {598for (int terrain_index = 0; terrain_index < TileSet::CELL_NEIGHBOR_MAX; terrain_index++) {599TileSet::CellNeighbor bit = TileSet::CellNeighbor(terrain_index);600if (tile_data->is_valid_terrain_peering_bit(bit)) {601ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[terrain_index]));602}603}604} else if (p_array_prefix == "navigation_layer_") {605for (int layer_index = begin; layer_index < end; layer_index++) {606ADD_UNDO(tile_data, vformat("navigation_layer_%d/polygon", layer_index));607}608} else if (p_array_prefix == "custom_data_layer_") {609for (int layer_index = begin; layer_index < end; layer_index++) {610ADD_UNDO(tile_data, vformat("custom_data_%d", layer_index));611}612}613}614}615}616}617#undef ADD_UNDO618619// Add do method to add/remove array element.620if (p_array_prefix == "occlusion_layer_") {621if (p_from_index < 0) {622undo_redo_man->add_do_method(ed_tile_set, "add_occlusion_layer", p_to_pos);623} else if (p_to_pos < 0) {624undo_redo_man->add_do_method(ed_tile_set, "remove_occlusion_layer", p_from_index);625} else {626undo_redo_man->add_do_method(ed_tile_set, "move_occlusion_layer", p_from_index, p_to_pos);627}628} else if (p_array_prefix == "physics_layer_") {629if (p_from_index < 0) {630undo_redo_man->add_do_method(ed_tile_set, "add_physics_layer", p_to_pos);631} else if (p_to_pos < 0) {632undo_redo_man->add_do_method(ed_tile_set, "remove_physics_layer", p_from_index);633} else {634undo_redo_man->add_do_method(ed_tile_set, "move_physics_layer", p_from_index, p_to_pos);635}636} else if (p_array_prefix == "terrain_set_") {637if (p_from_index < 0) {638undo_redo_man->add_do_method(ed_tile_set, "add_terrain_set", p_to_pos);639} else if (p_to_pos < 0) {640undo_redo_man->add_do_method(ed_tile_set, "remove_terrain_set", p_from_index);641} else {642undo_redo_man->add_do_method(ed_tile_set, "move_terrain_set", p_from_index, p_to_pos);643}644} else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {645int terrain_set = components[0].trim_prefix("terrain_set_").to_int();646if (p_from_index < 0) {647undo_redo_man->add_do_method(ed_tile_set, "add_terrain", terrain_set, p_to_pos);648} else if (p_to_pos < 0) {649undo_redo_man->add_do_method(ed_tile_set, "remove_terrain", terrain_set, p_from_index);650} else {651undo_redo_man->add_do_method(ed_tile_set, "move_terrain", terrain_set, p_from_index, p_to_pos);652}653} else if (p_array_prefix == "navigation_layer_") {654if (p_from_index < 0) {655undo_redo_man->add_do_method(ed_tile_set, "add_navigation_layer", p_to_pos);656} else if (p_to_pos < 0) {657undo_redo_man->add_do_method(ed_tile_set, "remove_navigation_layer", p_from_index);658} else {659undo_redo_man->add_do_method(ed_tile_set, "move_navigation_layer", p_from_index, p_to_pos);660}661} else if (p_array_prefix == "custom_data_layer_") {662if (p_from_index < 0) {663undo_redo_man->add_do_method(ed_tile_set, "add_custom_data_layer", p_to_pos);664} else if (p_to_pos < 0) {665undo_redo_man->add_do_method(ed_tile_set, "remove_custom_data_layer", p_from_index);666} else {667undo_redo_man->add_do_method(ed_tile_set, "move_custom_data_layer", p_from_index, p_to_pos);668}669}670}671672void TileSetEditor::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, const String &p_property, const Variant &p_new_value) {673EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);674ERR_FAIL_NULL(undo_redo_man);675676#define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property));677TileSet *ed_tile_set = Object::cast_to<TileSet>(p_edited);678if (ed_tile_set) {679Vector<String> components = p_property.split("/", true, 3);680for (int i = 0; i < ed_tile_set->get_source_count(); i++) {681int source_id = ed_tile_set->get_source_id(i);682683Ref<TileSetAtlasSource> tas = ed_tile_set->get_source(source_id);684if (tas.is_valid()) {685for (int j = 0; j < tas->get_tiles_count(); j++) {686Vector2i tile_id = tas->get_tile_id(j);687for (int k = 0; k < tas->get_alternative_tiles_count(tile_id); k++) {688int alternative_id = tas->get_alternative_tile_id(tile_id, k);689TileData *tile_data = tas->get_tile_data(tile_id, alternative_id);690ERR_FAIL_NULL(tile_data);691692if (components.size() == 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "mode") {693ADD_UNDO(tile_data, "terrain_set");694ADD_UNDO(tile_data, "terrain");695for (int l = 0; l < TileSet::CELL_NEIGHBOR_MAX; l++) {696TileSet::CellNeighbor bit = TileSet::CellNeighbor(l);697if (tile_data->is_valid_terrain_peering_bit(bit)) {698ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[l]));699}700}701} 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") {702int custom_data_layer = components[0].trim_prefix("custom_data_layer_").is_valid_int();703ADD_UNDO(tile_data, vformat("custom_data_%d", custom_data_layer));704}705}706}707}708}709}710#undef ADD_UNDO711}712713void TileSetEditor::edit(Ref<TileSet> p_tile_set) {714bool new_read_only_state = false;715if (p_tile_set.is_valid()) {716new_read_only_state = EditorNode::get_singleton()->is_resource_read_only(p_tile_set);717}718719if (p_tile_set == tile_set && new_read_only_state == read_only) {720return;721}722723// Remove listener.724if (tile_set.is_valid()) {725tile_set->disconnect_changed(callable_mp(this, &TileSetEditor::_tile_set_changed));726}727728// Change the edited object.729tile_set = p_tile_set;730731// Read-only status is false by default732read_only = new_read_only_state;733734// Add the listener again and check for read-only status.735if (tile_set.is_valid()) {736sources_add_button->set_disabled(read_only);737sources_advanced_menu_button->set_disabled(read_only);738source_sort_button->set_disabled(read_only);739740tile_set->connect_changed(callable_mp(this, &TileSetEditor::_tile_set_changed));741if (first_edit) {742_set_source_sort(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "tile_source_sort", 0));743first_edit = false;744} else {745_update_sources_list();746}747_update_patterns_list();748}749}750751void TileSetEditor::add_expanded_editor(Control *p_editor) {752expanded_editor = p_editor;753expanded_editor_parent = p_editor->get_parent()->get_instance_id();754755// Find the scrollable control this node belongs to.756Node *check_parent = expanded_editor->get_parent();757Control *parent_container = nullptr;758while (check_parent) {759parent_container = Object::cast_to<EditorInspector>(check_parent);760if (parent_container) {761break;762}763parent_container = Object::cast_to<ScrollContainer>(check_parent);764if (parent_container) {765break;766}767check_parent = check_parent->get_parent();768}769ERR_FAIL_NULL(parent_container);770771expanded_editor->set_meta("reparented", true);772expanded_editor->reparent(expanded_area);773expanded_area->show();774expanded_area->set_size(Vector2(parent_container->get_global_rect().get_end().x - expanded_area->get_global_position().x, expanded_area->get_size().y));775776for (SplitContainer *split : disable_on_expand) {777split->set_dragger_visibility(SplitContainer::DRAGGER_HIDDEN);778}779}780781void TileSetEditor::remove_expanded_editor() {782if (!expanded_editor) {783return;784}785786Node *original_parent = ObjectDB::get_instance<Node>(expanded_editor_parent);787if (original_parent) {788expanded_editor->remove_meta("reparented");789expanded_editor->reparent(original_parent);790} else {791expanded_editor->queue_free();792}793expanded_editor = nullptr;794expanded_editor_parent = ObjectID();795expanded_area->hide();796797for (SplitContainer *split : disable_on_expand) {798split->set_dragger_visibility(SplitContainer::DRAGGER_VISIBLE);799}800}801802void TileSetEditor::register_split(SplitContainer *p_split) {803disable_on_expand.push_back(p_split);804}805806TileSetEditor::TileSetEditor() {807singleton = this;808809set_process_internal(true);810811VBoxContainer *main_vb = memnew(VBoxContainer);812add_child(main_vb);813main_vb->set_anchors_and_offsets_preset(PRESET_FULL_RECT);814815// TabBar.816tabs_bar = memnew(TabBar);817tabs_bar->set_tab_alignment(TabBar::ALIGNMENT_CENTER);818tabs_bar->set_clip_tabs(false);819tabs_bar->add_tab(TTR("Tile Sources"));820tabs_bar->add_tab(TTR("Patterns"));821tabs_bar->connect("tab_changed", callable_mp(this, &TileSetEditor::_tab_changed));822823tile_set_toolbar = memnew(HBoxContainer);824tile_set_toolbar->set_h_size_flags(SIZE_EXPAND_FILL);825tile_set_toolbar->add_child(tabs_bar);826main_vb->add_child(tile_set_toolbar);827828//// Tiles ////829// Split container.830split_container = memnew(HSplitContainer);831split_container->set_name(TTR("Tiles"));832split_container->set_h_size_flags(SIZE_EXPAND_FILL);833split_container->set_v_size_flags(SIZE_EXPAND_FILL);834main_vb->add_child(split_container);835836// Sources list.837VBoxContainer *split_container_left_side = memnew(VBoxContainer);838split_container_left_side->set_h_size_flags(SIZE_EXPAND_FILL);839split_container_left_side->set_v_size_flags(SIZE_EXPAND_FILL);840split_container_left_side->set_stretch_ratio(0.25);841split_container_left_side->set_custom_minimum_size(Size2(70, 0) * EDSCALE);842split_container->add_child(split_container_left_side);843844source_sort_button = memnew(MenuButton);845source_sort_button->set_flat(false);846source_sort_button->set_theme_type_variation(SceneStringName(FlatButton));847source_sort_button->set_tooltip_text(TTR("Sort Sources"));848849PopupMenu *p = source_sort_button->get_popup();850p->connect(SceneStringName(id_pressed), callable_mp(this, &TileSetEditor::_set_source_sort));851p->add_radio_check_item(TTR("Sort by ID (Ascending)"), TilesEditorUtils::SOURCE_SORT_ID);852p->add_radio_check_item(TTR("Sort by ID (Descending)"), TilesEditorUtils::SOURCE_SORT_ID_REVERSE);853p->add_radio_check_item(TTR("Sort by Name (Ascending)"), TilesEditorUtils::SOURCE_SORT_NAME);854p->add_radio_check_item(TTR("Sort by Name (Descending)"), TilesEditorUtils::SOURCE_SORT_NAME_REVERSE);855p->set_item_checked(TilesEditorUtils::SOURCE_SORT_ID, true);856857sources_list = memnew(ItemList);858sources_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);859sources_list->set_fixed_icon_size(Size2(60, 60) * EDSCALE);860sources_list->set_h_size_flags(SIZE_EXPAND_FILL);861sources_list->set_v_size_flags(SIZE_EXPAND_FILL);862sources_list->set_theme_type_variation("ItemListSecondary");863sources_list->connect(SceneStringName(item_selected), callable_mp(this, &TileSetEditor::_source_selected));864sources_list->connect(SceneStringName(item_selected), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::set_sources_lists_current));865sources_list->connect(SceneStringName(visibility_changed), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::synchronize_sources_list).bind(sources_list, source_sort_button));866sources_list->add_user_signal(MethodInfo("sort_request"));867sources_list->connect("sort_request", callable_mp(this, &TileSetEditor::_update_sources_list).bind(-1));868sources_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);869SET_DRAG_FORWARDING_CDU(sources_list, TileSetEditor);870split_container_left_side->add_child(sources_list);871872HBoxContainer *sources_bottom_actions = memnew(HBoxContainer);873sources_bottom_actions->set_alignment(BoxContainer::ALIGNMENT_END);874split_container_left_side->add_child(sources_bottom_actions);875876sources_delete_button = memnew(Button);877sources_delete_button->set_theme_type_variation(SceneStringName(FlatButton));878sources_delete_button->set_disabled(true);879sources_delete_button->connect(SceneStringName(pressed), callable_mp(this, &TileSetEditor::_source_delete_pressed));880sources_bottom_actions->add_child(sources_delete_button);881882sources_add_button = memnew(MenuButton);883sources_add_button->set_flat(false);884sources_add_button->set_theme_type_variation(SceneStringName(FlatButton));885sources_add_button->get_popup()->add_item(TTR("Atlas"));886sources_add_button->get_popup()->set_item_tooltip(-1, TTR("A palette of tiles made from a texture."));887sources_add_button->get_popup()->add_item(TTR("Scenes Collection"));888sources_add_button->get_popup()->set_item_tooltip(-1, TTR("A collection of scenes that can be instantiated and placed as tiles."));889sources_add_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TileSetEditor::_source_add_id_pressed));890sources_bottom_actions->add_child(sources_add_button);891892sources_advanced_menu_button = memnew(MenuButton);893sources_advanced_menu_button->set_flat(false);894sources_advanced_menu_button->set_theme_type_variation(SceneStringName(FlatButton));895sources_advanced_menu_button->get_popup()->add_item(TTR("Open Atlas Merging Tool"));896sources_advanced_menu_button->get_popup()->add_item(TTR("Manage Tile Proxies"));897sources_advanced_menu_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TileSetEditor::_sources_advanced_menu_id_pressed));898sources_advanced_menu_button->set_accessibility_name(TTRC("Advanced"));899sources_bottom_actions->add_child(sources_advanced_menu_button);900sources_bottom_actions->add_child(source_sort_button);901902atlas_merging_dialog = memnew(AtlasMergingDialog);903add_child(atlas_merging_dialog);904905tile_proxies_manager_dialog = memnew(TileProxiesManagerDialog);906add_child(tile_proxies_manager_dialog);907908// Right side container.909VBoxContainer *split_container_right_side = memnew(VBoxContainer);910split_container_right_side->set_h_size_flags(SIZE_EXPAND_FILL);911split_container_right_side->set_v_size_flags(SIZE_EXPAND_FILL);912split_container->add_child(split_container_right_side);913914// No source selected.915no_source_selected_label = memnew(Label);916no_source_selected_label->set_focus_mode(FOCUS_ACCESSIBILITY);917no_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."));918no_source_selected_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);919no_source_selected_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);920no_source_selected_label->set_h_size_flags(SIZE_EXPAND_FILL);921no_source_selected_label->set_v_size_flags(SIZE_EXPAND_FILL);922no_source_selected_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);923no_source_selected_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);924split_container_right_side->add_child(no_source_selected_label);925926// Atlases editor.927tile_set_atlas_source_editor = memnew(TileSetAtlasSourceEditor);928tile_set_atlas_source_editor->set_h_size_flags(SIZE_EXPAND_FILL);929tile_set_atlas_source_editor->set_v_size_flags(SIZE_EXPAND_FILL);930tile_set_atlas_source_editor->connect("source_id_changed", callable_mp(this, &TileSetEditor::_update_sources_list));931split_container_right_side->add_child(tile_set_atlas_source_editor);932tile_set_atlas_source_editor->hide();933934// Scenes collection editor.935tile_set_scenes_collection_source_editor = memnew(TileSetScenesCollectionSourceEditor);936tile_set_scenes_collection_source_editor->set_h_size_flags(SIZE_EXPAND_FILL);937tile_set_scenes_collection_source_editor->set_v_size_flags(SIZE_EXPAND_FILL);938tile_set_scenes_collection_source_editor->connect("source_id_changed", callable_mp(this, &TileSetEditor::_update_sources_list));939split_container_right_side->add_child(tile_set_scenes_collection_source_editor);940tile_set_scenes_collection_source_editor->hide();941942//// Patterns ////943int thumbnail_size = 64;944patterns_item_list = memnew(ItemList);945patterns_item_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);946patterns_item_list->set_max_columns(0);947patterns_item_list->set_icon_mode(ItemList::ICON_MODE_TOP);948patterns_item_list->set_fixed_column_width(thumbnail_size * 3 / 2);949patterns_item_list->set_max_text_lines(2);950patterns_item_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));951patterns_item_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);952patterns_item_list->set_theme_type_variation("ItemListSecondary");953patterns_item_list->connect(SceneStringName(gui_input), callable_mp(this, &TileSetEditor::_patterns_item_list_gui_input));954main_vb->add_child(patterns_item_list);955patterns_item_list->hide();956957patterns_help_label = memnew(Label);958patterns_help_label->set_focus_mode(FOCUS_ACCESSIBILITY);959patterns_help_label->set_text(TTR("Add new patterns in the TileMap editing mode."));960patterns_help_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);961patterns_help_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);962patterns_help_label->set_anchors_and_offsets_preset(Control::PRESET_HCENTER_WIDE);963patterns_item_list->add_child(patterns_help_label);964965// Expanded editor966expanded_area = memnew(PanelContainer);967add_child(expanded_area);968expanded_area->set_anchors_and_offsets_preset(PRESET_LEFT_WIDE);969expanded_area->hide();970971// Registers UndoRedo inspector callback.972EditorNode::get_editor_data().add_move_array_element_function(SNAME("TileSet"), callable_mp(this, &TileSetEditor::_move_tile_set_array_element));973EditorNode::get_editor_data().add_undo_redo_inspector_hook_callback(callable_mp(this, &TileSetEditor::_undo_redo_inspector_callback));974}975976void TileSourceInspectorPlugin::_show_id_edit_dialog(Object *p_for_source) {977if (!id_edit_dialog) {978id_edit_dialog = memnew(ConfirmationDialog);979TileSetEditor::get_singleton()->add_child(id_edit_dialog);980981VBoxContainer *vbox = memnew(VBoxContainer);982id_edit_dialog->add_child(vbox);983984Label *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.")));985label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);986// Workaround too tall popup window due to text autowrapping. See GH-83546.987label->set_custom_minimum_size(Vector2i(400, 0));988vbox->add_child(label);989990id_input = memnew(SpinBox);991vbox->add_child(id_input);992id_input->set_max(INT_MAX);993994id_edit_dialog->connect(SceneStringName(confirmed), callable_mp(this, &TileSourceInspectorPlugin::_confirm_change_id));995}996edited_source = p_for_source;997id_input->set_value(p_for_source->get("id"));998id_edit_dialog->popup_centered(Vector2i(400, 0) * EDSCALE);999callable_mp((Control *)id_input->get_line_edit(), &Control::grab_focus).call_deferred();1000}10011002void TileSourceInspectorPlugin::_confirm_change_id() {1003edited_source->set("id", id_input->get_value());1004id_label->set_text(itos(edited_source->get("id"))); // Use get(), because the provided ID might've been invalid.1005}10061007bool TileSourceInspectorPlugin::can_handle(Object *p_object) {1008return p_object && (p_object->is_class("TileSetAtlasSourceProxyObject") || p_object->is_class("TileSetScenesCollectionProxyObject"));1009}10101011bool 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) {1012if (p_path == "id") {1013const Variant value = p_object->get("id");1014if (value.get_type() == Variant::NIL) { // May happen if the object is not yet initialized.1015return true;1016}10171018EditorProperty *ep = memnew(EditorProperty);10191020HBoxContainer *hbox = memnew(HBoxContainer);1021hbox->set_alignment(BoxContainer::ALIGNMENT_CENTER);10221023id_label = memnew(Label(itos(value)));1024id_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);1025hbox->add_child(id_label);10261027Button *button = memnew(Button(TTR("Edit")));1028button->set_h_size_flags(Control::SIZE_EXPAND_FILL);1029hbox->add_child(button);1030button->connect(SceneStringName(pressed), callable_mp(this, &TileSourceInspectorPlugin::_show_id_edit_dialog).bind(p_object));10311032ep->add_child(hbox);1033add_property_editor(p_path, ep);1034return true;1035}1036return false;1037}103810391040