Path: blob/master/editor/scene/2d/tiles/tiles_editor_plugin.cpp
9913 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/editor_interface.h"37#include "editor/editor_node.h"38#include "editor/editor_string_names.h"39#include "editor/gui/editor_bottom_panel.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_map_plugin_singleton->hide_editor();283tile_set_plugin_singleton->make_visible(true);284}285286void TilesEditorUtils::draw_selection_rect(CanvasItem *p_ci, const Rect2 &p_rect, const Color &p_color) {287Ref<Texture2D> selection_texture = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("TileSelection"), EditorStringName(EditorIcons));288289real_t scale = p_ci->get_global_transform().get_scale().x * 0.5;290p_ci->draw_set_transform(p_rect.position, 0, Vector2(1, 1) / scale);291RS::get_singleton()->canvas_item_add_nine_patch(292p_ci->get_canvas_item(), Rect2(Vector2(), p_rect.size * scale), Rect2(), selection_texture->get_rid(),293Vector2(2, 2), Vector2(2, 2), RS::NINE_PATCH_STRETCH, RS::NINE_PATCH_STRETCH, false, p_color);294p_ci->draw_set_transform_matrix(Transform2D());295}296297TilesEditorUtils::TilesEditorUtils() {298singleton = this;299// Pattern preview generation thread.300pattern_preview_thread.start(_thread_func, this);301302ED_SHORTCUT("tiles_editor/cut", TTRC("Cut"), KeyModifierMask::CMD_OR_CTRL | Key::X);303ED_SHORTCUT("tiles_editor/copy", TTRC("Copy"), KeyModifierMask::CMD_OR_CTRL | Key::C);304ED_SHORTCUT("tiles_editor/paste", TTRC("Paste"), KeyModifierMask::CMD_OR_CTRL | Key::V);305ED_SHORTCUT("tiles_editor/cancel", TTRC("Cancel"), Key::ESCAPE);306ED_SHORTCUT("tiles_editor/delete", TTRC("Delete"), Key::KEY_DELETE);307308ED_SHORTCUT("tiles_editor/paint_tool", TTRC("Paint Tool"), Key::D);309ED_SHORTCUT("tiles_editor/line_tool", TTRC("Line Tool"), Key::L);310ED_SHORTCUT("tiles_editor/rect_tool", TTRC("Rect Tool"), Key::R);311ED_SHORTCUT("tiles_editor/bucket_tool", TTRC("Bucket Tool"), Key::B);312ED_SHORTCUT("tiles_editor/eraser", TTRC("Eraser Tool"), Key::E);313ED_SHORTCUT("tiles_editor/picker", TTRC("Picker Tool"), Key::P);314}315316TilesEditorUtils::~TilesEditorUtils() {317if (pattern_preview_thread.is_started()) {318pattern_thread_exit.set();319pattern_preview_sem.post();320while (!pattern_thread_exited.is_set()) {321OS::get_singleton()->delay_usec(10000);322RenderingServer::get_singleton()->sync(); //sync pending stuff, as thread may be blocked on visual server323}324pattern_preview_thread.wait_to_finish();325}326singleton = nullptr;327}328329void TileMapEditorPlugin::_tile_map_layer_changed() {330if (tile_map_changed_needs_update) {331return;332}333tile_map_changed_needs_update = true;334callable_mp(this, &TileMapEditorPlugin::_update_tile_map).call_deferred();335}336337void TileMapEditorPlugin::_tile_map_layer_removed() {338// Workaround for TileMap, making sure the editor stays open when you delete the currently edited layer.339TileMap *tile_map = ObjectDB::get_instance<TileMap>(tile_map_group_id);340if (tile_map) {341edit(tile_map);342}343}344345void TileMapEditorPlugin::_update_tile_map() {346TileMapLayer *edited_layer = ObjectDB::get_instance<TileMapLayer>(tile_map_layer_id);347if (edited_layer) {348Ref<TileSet> tile_set = edited_layer->get_tile_set();349if (tile_set.is_valid() && tile_set_id != tile_set->get_instance_id()) {350tile_set_plugin_singleton->edit(tile_set.ptr());351tile_set_plugin_singleton->make_visible(true);352tile_set_id = tile_set->get_instance_id();353} else if (tile_set.is_null()) {354tile_set_plugin_singleton->edit(nullptr);355tile_set_plugin_singleton->make_visible(false);356tile_set_id = ObjectID();357}358}359tile_map_changed_needs_update = false;360}361362void TileMapEditorPlugin::_select_layer(const StringName &p_name) {363TileMapLayer *edited_layer = ObjectDB::get_instance<TileMapLayer>(tile_map_layer_id);364ERR_FAIL_NULL(edited_layer);365366Node *parent = edited_layer->get_parent();367if (parent) {368TileMapLayer *new_layer = Object::cast_to<TileMapLayer>(parent->get_node_or_null(String(p_name)));369edit(new_layer);370}371}372373void TileMapEditorPlugin::_edit_tile_map_layer(TileMapLayer *p_tile_map_layer, bool p_show_layer_selector) {374ERR_FAIL_NULL(p_tile_map_layer);375376editor->edit(p_tile_map_layer);377editor->set_show_layer_selector(p_show_layer_selector);378379// Update the object IDs.380tile_map_layer_id = p_tile_map_layer->get_instance_id();381p_tile_map_layer->connect(CoreStringName(changed), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_changed));382p_tile_map_layer->connect(SceneStringName(tree_exited), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_removed));383384// Update the edited tileset.385Ref<TileSet> tile_set = p_tile_map_layer->get_tile_set();386if (tile_set.is_valid()) {387tile_set_plugin_singleton->edit(tile_set.ptr());388tile_set_plugin_singleton->make_visible(true);389tile_set_id = tile_set->get_instance_id();390} else {391tile_set_plugin_singleton->edit(nullptr);392tile_set_plugin_singleton->make_visible(false);393}394}395396void TileMapEditorPlugin::_edit_tile_map(TileMap *p_tile_map) {397ERR_FAIL_NULL(p_tile_map);398399if (p_tile_map->get_layers_count() > 0) {400TileMapLayer *selected_layer = Object::cast_to<TileMapLayer>(p_tile_map->get_child(0));401_edit_tile_map_layer(selected_layer, true);402} else {403editor->edit(nullptr);404editor->set_show_layer_selector(false);405}406}407408void TileMapEditorPlugin::_notification(int p_notification) {409if (p_notification == NOTIFICATION_EXIT_TREE) {410get_tree()->queue_delete(TilesEditorUtils::get_singleton());411}412}413414void TileMapEditorPlugin::edit(Object *p_object) {415TileMapLayer *edited_layer = ObjectDB::get_instance<TileMapLayer>(tile_map_layer_id);416if (edited_layer) {417edited_layer->disconnect(CoreStringName(changed), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_changed));418edited_layer->disconnect(SceneStringName(tree_exited), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_removed));419}420421tile_map_group_id = ObjectID();422tile_map_layer_id = ObjectID();423tile_set_id = ObjectID();424425TileMap *tile_map = Object::cast_to<TileMap>(p_object);426TileMapLayer *tile_map_layer = Object::cast_to<TileMapLayer>(p_object);427MultiNodeEdit *multi_node_edit = Object::cast_to<MultiNodeEdit>(p_object);428if (tile_map) {429_edit_tile_map(tile_map);430} else if (tile_map_layer) {431_edit_tile_map_layer(tile_map_layer, false);432} else if (multi_node_edit) {433editor->edit(multi_node_edit);434} else {435editor->edit(nullptr);436}437}438439bool TileMapEditorPlugin::handles(Object *p_object) const {440MultiNodeEdit *multi_node_edit = Object::cast_to<MultiNodeEdit>(p_object);441Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();442if (multi_node_edit && edited_scene) {443bool only_tile_map_layers = true;444for (int i = 0; i < multi_node_edit->get_node_count(); i++) {445if (!Object::cast_to<TileMapLayer>(edited_scene->get_node(multi_node_edit->get_node(i)))) {446only_tile_map_layers = false;447break;448}449}450return only_tile_map_layers;451}452return Object::cast_to<TileMapLayer>(p_object) != nullptr || Object::cast_to<TileMap>(p_object) != nullptr;453}454455void TileMapEditorPlugin::make_visible(bool p_visible) {456if (p_visible) {457button->show();458EditorNode::get_bottom_panel()->make_item_visible(editor);459} else {460button->hide();461if (editor->is_visible_in_tree()) {462EditorNode::get_bottom_panel()->hide_bottom_panel();463}464}465}466467bool TileMapEditorPlugin::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {468return editor->forward_canvas_gui_input(p_event);469}470471void TileMapEditorPlugin::forward_canvas_draw_over_viewport(Control *p_overlay) {472editor->forward_canvas_draw_over_viewport(p_overlay);473}474475void TileMapEditorPlugin::hide_editor() {476if (editor->is_visible_in_tree()) {477EditorNode::get_bottom_panel()->hide_bottom_panel();478}479}480481bool TileMapEditorPlugin::is_editor_visible() const {482return editor->is_visible_in_tree();483}484485TileMapEditorPlugin::TileMapEditorPlugin() {486if (!TilesEditorUtils::get_singleton()) {487memnew(TilesEditorUtils);488}489tile_map_plugin_singleton = this;490491editor = memnew(TileMapLayerEditor);492editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);493editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);494editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);495editor->hide();496497button = EditorNode::get_bottom_panel()->add_item(TTRC("TileMap"), editor, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_tile_map_bottom_panel", TTRC("Toggle TileMap Bottom Panel")));498button->hide();499}500501TileMapEditorPlugin::~TileMapEditorPlugin() {502tile_map_plugin_singleton = nullptr;503}504505void TileSetEditorPlugin::edit(Object *p_object) {506editor->edit(Ref<TileSet>(p_object));507if (p_object) {508edited_tileset = p_object->get_instance_id();509} else {510edited_tileset = ObjectID();511}512}513514bool TileSetEditorPlugin::handles(Object *p_object) const {515return Object::cast_to<TileSet>(p_object) != nullptr;516}517518void TileSetEditorPlugin::make_visible(bool p_visible) {519if (p_visible) {520button->show();521if (!tile_map_plugin_singleton->is_editor_visible()) {522EditorNode::get_bottom_panel()->make_item_visible(editor);523}524} else {525button->hide();526if (editor->is_visible_in_tree()) {527EditorNode::get_bottom_panel()->hide_bottom_panel();528}529}530}531532ObjectID TileSetEditorPlugin::get_edited_tileset() const {533return edited_tileset;534}535536TileSetEditorPlugin::TileSetEditorPlugin() {537if (!TilesEditorUtils::get_singleton()) {538memnew(TilesEditorUtils);539}540tile_set_plugin_singleton = this;541542editor = memnew(TileSetEditor);543editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);544editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);545editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);546editor->hide();547548button = EditorNode::get_bottom_panel()->add_item(TTRC("TileSet"), editor, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_tile_set_bottom_panel", TTRC("Toggle TileSet Bottom Panel")));549button->hide();550}551552TileSetEditorPlugin::~TileSetEditorPlugin() {553tile_set_plugin_singleton = nullptr;554}555556557