Path: blob/master/editor/scene/2d/tiles/tiles_editor_plugin.cpp
21273 views
/**************************************************************************/1/* tiles_editor_plugin.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 "tiles_editor_plugin.h"3132#include "tile_set_editor.h"3334#include "core/os/mutex.h"3536#include "editor/docks/editor_dock_manager.h"37#include "editor/editor_interface.h"38#include "editor/editor_node.h"39#include "editor/editor_string_names.h"40#include "editor/inspector/multi_node_edit.h"41#include "editor/scene/canvas_item_editor_plugin.h"42#include "editor/settings/editor_command_palette.h"43#include "editor/settings/editor_settings.h"44#include "editor/themes/editor_scale.h"45#include "scene/2d/tile_map.h"46#include "scene/2d/tile_map_layer.h"47#include "scene/gui/button.h"48#include "scene/gui/control.h"49#include "scene/resources/2d/tile_set.h"50#include "scene/resources/image_texture.h"5152TilesEditorUtils *TilesEditorUtils::singleton = nullptr;53TileMapEditorPlugin *tile_map_plugin_singleton = nullptr;54TileSetEditorPlugin *tile_set_plugin_singleton = nullptr;5556void TilesEditorUtils::_preview_frame_started() {57RS::get_singleton()->request_frame_drawn_callback(callable_mp(this, &TilesEditorUtils::_pattern_preview_done));58}5960void TilesEditorUtils::_pattern_preview_done() {61pattern_preview_done.post();62}6364void TilesEditorUtils::_thread_func(void *ud) {65TilesEditorUtils *te = static_cast<TilesEditorUtils *>(ud);66set_current_thread_safe_for_nodes(true);67te->_thread();68}6970void TilesEditorUtils::_thread() {71pattern_thread_exited.clear();72while (!pattern_thread_exit.is_set()) {73pattern_preview_sem.wait();7475pattern_preview_mutex.lock();76if (pattern_preview_queue.is_empty()) {77pattern_preview_mutex.unlock();78} else {79QueueItem item = pattern_preview_queue.front()->get();80pattern_preview_queue.pop_front();81pattern_preview_mutex.unlock();8283int thumbnail_size = EDITOR_GET("filesystem/file_dialog/thumbnail_size");84thumbnail_size *= EDSCALE;85Vector2 thumbnail_size2 = Vector2(thumbnail_size, thumbnail_size);8687if (item.pattern.is_valid() && !item.pattern->is_empty()) {88// Generate the pattern preview89SubViewport *viewport = memnew(SubViewport);90viewport->set_size(thumbnail_size2);91viewport->set_disable_input(true);92viewport->set_transparent_background(true);93viewport->set_update_mode(SubViewport::UPDATE_ONCE);9495TileMapLayer *tile_map_layer = memnew(TileMapLayer);96tile_map_layer->set_tile_set(item.tile_set);97tile_map_layer->set_pattern(Vector2(), item.pattern);98viewport->add_child(tile_map_layer);99100Rect2 encompassing_rect;101encompassing_rect.set_position(tile_map_layer->map_to_local(tile_map_layer->get_tile_map_layer_data().begin()->key));102for (KeyValue<Vector2i, CellData> kv : tile_map_layer->get_tile_map_layer_data()) {103Vector2i cell = kv.key;104Vector2 world_pos = tile_map_layer->map_to_local(cell);105encompassing_rect.expand_to(world_pos);106107// Texture.108Ref<TileSetAtlasSource> atlas_source = item.tile_set->get_source(tile_map_layer->get_cell_source_id(cell));109if (atlas_source.is_valid()) {110Vector2i coords = tile_map_layer->get_cell_atlas_coords(cell);111int alternative = tile_map_layer->get_cell_alternative_tile(cell);112113if (atlas_source->has_tile(coords) && atlas_source->has_alternative_tile(coords, alternative)) {114Vector2 center = world_pos - atlas_source->get_tile_data(coords, alternative)->get_texture_origin();115encompassing_rect.expand_to(center - atlas_source->get_tile_texture_region(coords).size / 2);116encompassing_rect.expand_to(center + atlas_source->get_tile_texture_region(coords).size / 2);117}118}119}120121Vector2 scale = thumbnail_size2 / MAX(encompassing_rect.size.x, encompassing_rect.size.y);122tile_map_layer->set_scale(scale);123tile_map_layer->set_position(-(scale * encompassing_rect.get_center()) + thumbnail_size2 / 2);124125// Add the viewport at the last moment to avoid rendering too early.126callable_mp((Node *)EditorNode::get_singleton(), &Node::add_child).call_deferred(viewport, false, Node::INTERNAL_MODE_DISABLED);127128RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(this, &TilesEditorUtils::_preview_frame_started), Object::CONNECT_ONE_SHOT);129130pattern_preview_done.wait();131132Ref<Image> image = viewport->get_texture()->get_image();133134// Find the index for the given pattern. TODO: optimize.135item.callback.call(item.pattern, ImageTexture::create_from_image(image));136137viewport->queue_free();138}139}140}141pattern_thread_exited.set();142}143144void TilesEditorUtils::queue_pattern_preview(Ref<TileSet> p_tile_set, Ref<TileMapPattern> p_pattern, Callable p_callback) {145ERR_FAIL_COND(p_tile_set.is_null());146ERR_FAIL_COND(p_pattern.is_null());147{148MutexLock lock(pattern_preview_mutex);149pattern_preview_queue.push_back({ p_tile_set, p_pattern, p_callback });150}151pattern_preview_sem.post();152}153154void TilesEditorUtils::set_sources_lists_current(int p_current) {155atlas_sources_lists_current = p_current;156}157158void TilesEditorUtils::synchronize_sources_list(Object *p_current_list, Object *p_current_sort_button) {159ItemList *item_list = Object::cast_to<ItemList>(p_current_list);160MenuButton *sorting_button = Object::cast_to<MenuButton>(p_current_sort_button);161ERR_FAIL_NULL(item_list);162ERR_FAIL_NULL(sorting_button);163164if (sorting_button->is_visible_in_tree()) {165for (int i = 0; i != SOURCE_SORT_MAX; i++) {166sorting_button->get_popup()->set_item_checked(i, (i == (int)source_sort));167}168}169170if (item_list->is_visible_in_tree()) {171// Make sure the selection is not overwritten after sorting.172int atlas_sources_lists_current_mem = atlas_sources_lists_current;173item_list->emit_signal(SNAME("sort_request"));174atlas_sources_lists_current = atlas_sources_lists_current_mem;175176if (atlas_sources_lists_current < 0 || atlas_sources_lists_current >= item_list->get_item_count()) {177item_list->deselect_all();178} else {179item_list->set_current(atlas_sources_lists_current);180item_list->ensure_current_is_visible();181item_list->emit_signal(SceneStringName(item_selected), atlas_sources_lists_current);182}183}184}185186void TilesEditorUtils::set_atlas_view_transform(float p_zoom, Vector2 p_scroll) {187atlas_view_zoom = p_zoom;188atlas_view_scroll = p_scroll;189}190191void TilesEditorUtils::synchronize_atlas_view(Object *p_current) {192TileAtlasView *tile_atlas_view = Object::cast_to<TileAtlasView>(p_current);193ERR_FAIL_NULL(tile_atlas_view);194195if (tile_atlas_view->is_visible_in_tree()) {196tile_atlas_view->set_transform(atlas_view_zoom, atlas_view_scroll);197}198}199200void TilesEditorUtils::set_sorting_option(int p_option) {201source_sort = p_option;202}203204List<int> TilesEditorUtils::get_sorted_sources(const Ref<TileSet> p_tile_set) const {205SourceNameComparator::tile_set = p_tile_set;206List<int> source_ids;207208for (int i = 0; i < p_tile_set->get_source_count(); i++) {209source_ids.push_back(p_tile_set->get_source_id(i));210}211212switch (source_sort) {213case SOURCE_SORT_ID_REVERSE:214// Already sorted.215source_ids.reverse();216break;217case SOURCE_SORT_NAME:218source_ids.sort_custom<SourceNameComparator>();219break;220case SOURCE_SORT_NAME_REVERSE:221source_ids.sort_custom<SourceNameComparator>();222source_ids.reverse();223break;224default: // SOURCE_SORT_ID225break;226}227228SourceNameComparator::tile_set.unref();229return source_ids;230}231232Ref<TileSet> TilesEditorUtils::SourceNameComparator::tile_set;233234bool TilesEditorUtils::SourceNameComparator::operator()(const int &p_a, const int &p_b) const {235String name_a;236String name_b;237238{239TileSetSource *source = *tile_set->get_source(p_a);240241if (!source->get_name().is_empty()) {242name_a = source->get_name();243}244245TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);246if (atlas_source) {247Ref<Texture2D> texture = atlas_source->get_texture();248if (name_a.is_empty() && texture.is_valid()) {249name_a = texture->get_path().get_file();250}251}252253if (name_a.is_empty()) {254name_a = itos(p_a);255}256}257258{259TileSetSource *source = *tile_set->get_source(p_b);260261if (!source->get_name().is_empty()) {262name_b = source->get_name();263}264265TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);266if (atlas_source) {267Ref<Texture2D> texture = atlas_source->get_texture();268if (name_b.is_empty() && texture.is_valid()) {269name_b = texture->get_path().get_file();270}271}272273if (name_b.is_empty()) {274name_b = itos(p_b);275}276}277278return NaturalNoCaseComparator()(name_a, name_b);279}280281void TilesEditorUtils::display_tile_set_editor_panel() {282tile_set_plugin_singleton->make_visible(true);283}284285void TilesEditorUtils::draw_selection_rect(CanvasItem *p_ci, const Rect2 &p_rect, const Color &p_color) {286Ref<Texture2D> selection_texture = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("TileSelection"), EditorStringName(EditorIcons));287288real_t scale = p_ci->get_global_transform().get_scale().x * 0.5;289p_ci->draw_set_transform(p_rect.position, 0, Vector2(1, 1) / scale);290RS::get_singleton()->canvas_item_add_nine_patch(291p_ci->get_canvas_item(), Rect2(Vector2(), p_rect.size * scale), Rect2(), selection_texture->get_rid(),292Vector2(2, 2), Vector2(2, 2), RS::NINE_PATCH_STRETCH, RS::NINE_PATCH_STRETCH, false, p_color);293p_ci->draw_set_transform_matrix(Transform2D());294}295296TilesEditorUtils::TilesEditorUtils() {297singleton = this;298// Pattern preview generation thread.299pattern_preview_thread.start(_thread_func, this);300301ED_SHORTCUT("tiles_editor/cut", TTRC("Cut"), KeyModifierMask::CMD_OR_CTRL | Key::X);302ED_SHORTCUT("tiles_editor/copy", TTRC("Copy"), KeyModifierMask::CMD_OR_CTRL | Key::C);303ED_SHORTCUT("tiles_editor/paste", TTRC("Paste"), KeyModifierMask::CMD_OR_CTRL | Key::V);304ED_SHORTCUT("tiles_editor/cancel", TTRC("Cancel"), Key::ESCAPE);305ED_SHORTCUT("tiles_editor/delete", TTRC("Delete"), Key::KEY_DELETE);306307ED_SHORTCUT("tiles_editor/paint_tool", TTRC("Paint Tool"), Key::D);308ED_SHORTCUT("tiles_editor/line_tool", TTRC("Line Tool"), Key::L);309ED_SHORTCUT("tiles_editor/rect_tool", TTRC("Rect Tool"), Key::R);310ED_SHORTCUT("tiles_editor/bucket_tool", TTRC("Bucket Tool"), Key::B);311ED_SHORTCUT("tiles_editor/eraser", TTRC("Eraser Tool"), Key::E);312ED_SHORTCUT("tiles_editor/picker", TTRC("Picker Tool"), Key::P);313}314315TilesEditorUtils::~TilesEditorUtils() {316if (pattern_preview_thread.is_started()) {317pattern_thread_exit.set();318pattern_preview_sem.post();319while (!pattern_thread_exited.is_set()) {320OS::get_singleton()->delay_usec(10000);321RenderingServer::get_singleton()->sync(); //sync pending stuff, as thread may be blocked on visual server322}323pattern_preview_thread.wait_to_finish();324}325singleton = nullptr;326}327328String TileSetSourceItemList::get_tooltip(const Point2 &p_pos) const {329int idx = get_item_at_position(p_pos);330if (tile_set.is_null() || idx == -1) {331return ItemList::get_tooltip(p_pos);332}333idx = get_item_metadata(idx);334335Ref<TileSetAtlasSource> atlas = tile_set->get_source(idx);336if (atlas.is_valid() && atlas->get_texture().is_valid()) {337return vformat(TTR("Source ID: %d\nTexture path: %s"), idx, atlas->get_texture()->get_path());338}339return vformat(TTR("Source ID: %d"), idx);340}341342TileSetSourceItemList::TileSetSourceItemList() {343set_fixed_icon_size(Size2(60, 60) * EDSCALE);344set_h_size_flags(SIZE_EXPAND_FILL);345set_v_size_flags(SIZE_EXPAND_FILL);346set_stretch_ratio(0.25);347set_custom_minimum_size(Size2(70, 0) * EDSCALE);348set_theme_type_variation("ItemListSecondary");349set_texture_filter(TEXTURE_FILTER_NEAREST);350set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);351add_user_signal(MethodInfo("sort_request"));352}353354void TileMapEditorPlugin::_tile_map_layer_changed() {355if (tile_map_changed_needs_update) {356return;357}358tile_map_changed_needs_update = true;359callable_mp(this, &TileMapEditorPlugin::_update_tile_map).call_deferred();360}361362void TileMapEditorPlugin::_tile_map_layer_removed() {363// Workaround for TileMap, making sure the editor stays open when you delete the currently edited layer.364TileMap *tile_map = ObjectDB::get_instance<TileMap>(tile_map_group_id);365if (tile_map) {366edit(tile_map);367}368}369370void TileMapEditorPlugin::_update_tile_map() {371TileMapLayer *edited_layer = ObjectDB::get_instance<TileMapLayer>(tile_map_layer_id);372if (edited_layer) {373Ref<TileSet> tile_set = edited_layer->get_tile_set();374if (tile_set.is_valid() && tile_set_id != tile_set->get_instance_id()) {375tile_set_plugin_singleton->edit(tile_set.ptr());376tile_set_plugin_singleton->make_visible(true);377tile_set_id = tile_set->get_instance_id();378} else if (tile_set.is_null()) {379tile_set_plugin_singleton->edit(nullptr);380tile_set_plugin_singleton->make_visible(false);381tile_set_id = ObjectID();382}383}384tile_map_changed_needs_update = false;385}386387void TileMapEditorPlugin::_select_layer(const StringName &p_name) {388TileMapLayer *edited_layer = ObjectDB::get_instance<TileMapLayer>(tile_map_layer_id);389ERR_FAIL_NULL(edited_layer);390391Node *parent = edited_layer->get_parent();392if (parent) {393TileMapLayer *new_layer = Object::cast_to<TileMapLayer>(parent->get_node_or_null(String(p_name)));394edit(new_layer);395}396}397398void TileMapEditorPlugin::_edit_tile_map_layer(TileMapLayer *p_tile_map_layer, bool p_show_layer_selector) {399ERR_FAIL_NULL(p_tile_map_layer);400401editor->edit(p_tile_map_layer);402editor->set_show_layer_selector(p_show_layer_selector);403404// Update the object IDs.405tile_map_layer_id = p_tile_map_layer->get_instance_id();406p_tile_map_layer->connect(CoreStringName(changed), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_changed));407p_tile_map_layer->connect(SceneStringName(tree_exited), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_removed));408409// Update the edited tileset.410Ref<TileSet> tile_set = p_tile_map_layer->get_tile_set();411if (tile_set.is_valid()) {412tile_set_plugin_singleton->edit(tile_set.ptr());413tile_set_plugin_singleton->open_editor();414tile_set_id = tile_set->get_instance_id();415} else {416tile_set_plugin_singleton->edit(nullptr);417tile_set_plugin_singleton->make_visible(false);418}419}420421void TileMapEditorPlugin::_edit_tile_map(TileMap *p_tile_map) {422ERR_FAIL_NULL(p_tile_map);423424if (p_tile_map->get_layers_count() > 0) {425TileMapLayer *selected_layer = Object::cast_to<TileMapLayer>(p_tile_map->get_child(0));426_edit_tile_map_layer(selected_layer, true);427} else {428editor->edit(nullptr);429editor->set_show_layer_selector(false);430}431}432433void TileMapEditorPlugin::_notification(int p_notification) {434if (p_notification == NOTIFICATION_EXIT_TREE) {435get_tree()->queue_delete(TilesEditorUtils::get_singleton());436}437}438439void TileMapEditorPlugin::edit(Object *p_object) {440TileMapLayer *edited_layer = ObjectDB::get_instance<TileMapLayer>(tile_map_layer_id);441if (edited_layer) {442edited_layer->disconnect(CoreStringName(changed), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_changed));443edited_layer->disconnect(SceneStringName(tree_exited), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_removed));444}445446tile_map_group_id = ObjectID();447tile_map_layer_id = ObjectID();448tile_set_id = ObjectID();449450TileMap *tile_map = Object::cast_to<TileMap>(p_object);451TileMapLayer *tile_map_layer = Object::cast_to<TileMapLayer>(p_object);452MultiNodeEdit *multi_node_edit = Object::cast_to<MultiNodeEdit>(p_object);453if (tile_map) {454_edit_tile_map(tile_map);455} else if (tile_map_layer) {456_edit_tile_map_layer(tile_map_layer, false);457} else if (multi_node_edit) {458editor->edit(multi_node_edit);459} else {460editor->edit(nullptr);461}462}463464bool TileMapEditorPlugin::handles(Object *p_object) const {465MultiNodeEdit *multi_node_edit = Object::cast_to<MultiNodeEdit>(p_object);466Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();467if (multi_node_edit && edited_scene) {468bool only_tile_map_layers = true;469for (int i = 0; i < multi_node_edit->get_node_count(); i++) {470if (!Object::cast_to<TileMapLayer>(edited_scene->get_node(multi_node_edit->get_node(i)))) {471only_tile_map_layers = false;472break;473}474}475return only_tile_map_layers;476}477return Object::cast_to<TileMapLayer>(p_object) != nullptr || Object::cast_to<TileMap>(p_object) != nullptr;478}479480void TileMapEditorPlugin::make_visible(bool p_visible) {481if (p_visible) {482editor->make_visible();483} else {484editor->close();485TileSetEditor::get_singleton()->close();486}487}488489bool TileMapEditorPlugin::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {490return editor->forward_canvas_gui_input(p_event);491}492493void TileMapEditorPlugin::forward_canvas_draw_over_viewport(Control *p_overlay) {494editor->forward_canvas_draw_over_viewport(p_overlay);495}496497bool TileMapEditorPlugin::is_editor_visible() const {498return editor->is_visible_in_tree();499}500501TileMapEditorPlugin::TileMapEditorPlugin() {502if (!TilesEditorUtils::get_singleton()) {503memnew(TilesEditorUtils);504}505tile_map_plugin_singleton = this;506507editor = memnew(TileMapLayerEditor);508editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);509editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);510editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);511editor->hide();512513EditorDockManager::get_singleton()->add_dock(editor);514editor->close();515}516517TileMapEditorPlugin::~TileMapEditorPlugin() {518tile_map_plugin_singleton = nullptr;519}520521void TileSetEditorPlugin::edit(Object *p_object) {522editor->edit(Ref<TileSet>(p_object));523if (p_object) {524edited_tileset = p_object->get_instance_id();525} else {526edited_tileset = ObjectID();527}528}529530bool TileSetEditorPlugin::handles(Object *p_object) const {531return Object::cast_to<TileSet>(p_object) != nullptr;532}533534void TileSetEditorPlugin::make_visible(bool p_visible) {535if (p_visible) {536editor->make_visible();537} else {538editor->close();539}540}541542void TileSetEditorPlugin::open_editor() {543editor->open();544}545546ObjectID TileSetEditorPlugin::get_edited_tileset() const {547return edited_tileset;548}549550TileSetEditorPlugin::TileSetEditorPlugin() {551if (!TilesEditorUtils::get_singleton()) {552memnew(TilesEditorUtils);553}554tile_set_plugin_singleton = this;555556editor = memnew(TileSetEditor);557editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);558editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);559editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);560editor->hide();561562EditorDockManager::get_singleton()->add_dock(editor);563editor->close();564}565566TileSetEditorPlugin::~TileSetEditorPlugin() {567tile_set_plugin_singleton = nullptr;568}569570571