1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-19 14:31:59 +00:00

Add separate editor plugin for TileMap and TileSet

This commit is contained in:
kobewi
2023-06-22 15:42:45 +02:00
parent 202e4b2c1e
commit ec24d50469
8 changed files with 250 additions and 266 deletions

View File

@@ -38,6 +38,7 @@
#include "editor/editor_node.h"
#include "editor/editor_scale.h"
#include "editor/editor_settings.h"
#include "editor/inspector_dock.h"
#include "editor/plugins/canvas_item_editor_plugin.h"
#include "scene/2d/tile_map.h"
@@ -48,23 +49,24 @@
#include "scene/resources/image_texture.h"
#include "scene/resources/tile_set.h"
TilesEditorPlugin *TilesEditorPlugin::singleton = nullptr;
TilesEditorUtils *TilesEditorUtils::singleton = nullptr;
TileMapEditorPlugin *local_singleton = nullptr;
void TilesEditorPlugin::_preview_frame_started() {
RS::get_singleton()->request_frame_drawn_callback(callable_mp(const_cast<TilesEditorPlugin *>(this), &TilesEditorPlugin::_pattern_preview_done));
void TilesEditorUtils::_preview_frame_started() {
RS::get_singleton()->request_frame_drawn_callback(callable_mp(const_cast<TilesEditorUtils *>(this), &TilesEditorUtils::_pattern_preview_done));
}
void TilesEditorPlugin::_pattern_preview_done() {
void TilesEditorUtils::_pattern_preview_done() {
pattern_preview_done.post();
}
void TilesEditorPlugin::_thread_func(void *ud) {
TilesEditorPlugin *te = static_cast<TilesEditorPlugin *>(ud);
void TilesEditorUtils::_thread_func(void *ud) {
TilesEditorUtils *te = static_cast<TilesEditorUtils *>(ud);
set_current_thread_safe_for_nodes(true);
te->_thread();
}
void TilesEditorPlugin::_thread() {
void TilesEditorUtils::_thread() {
pattern_thread_exited.clear();
while (!pattern_thread_exit.is_set()) {
pattern_preview_sem.wait();
@@ -124,7 +126,7 @@ void TilesEditorPlugin::_thread() {
// Add the viewport at the last moment to avoid rendering too early.
EditorNode::get_singleton()->call_deferred("add_child", viewport);
RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(const_cast<TilesEditorPlugin *>(this), &TilesEditorPlugin::_preview_frame_started), Object::CONNECT_ONE_SHOT);
RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(const_cast<TilesEditorUtils *>(this), &TilesEditorUtils::_preview_frame_started), Object::CONNECT_ONE_SHOT);
pattern_preview_done.wait();
@@ -144,85 +146,7 @@ void TilesEditorPlugin::_thread() {
pattern_thread_exited.set();
}
void TilesEditorPlugin::_tile_map_changed() {
tile_map_changed_needs_update = true;
}
void TilesEditorPlugin::_update_editors() {
// If tile_map is not edited, we change the edited only if we are not editing a tile_set.
if (tile_set.is_valid()) {
tileset_editor->edit(tile_set);
}
TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
if (tile_map) {
tilemap_editor->edit(tile_map);
} else {
tilemap_editor->edit(nullptr);
}
// Update the viewport.
CanvasItemEditor::get_singleton()->update_viewport();
// Make sure the tile set editor is visible if we have one assigned.
tileset_editor_button->set_visible(is_visible && tile_set.is_valid());
tilemap_editor_button->set_visible(is_visible && tile_map);
// Update visibility of bottom panel buttons.
if (tileset_editor_button->is_pressed() && !tile_set.is_valid()) {
if (tile_map) {
EditorNode::get_singleton()->make_bottom_panel_item_visible(tilemap_editor);
} else {
EditorNode::get_singleton()->hide_bottom_panel();
}
}
}
void TilesEditorPlugin::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_INTERNAL_PROCESS: {
if (tile_map_changed_needs_update) {
TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
if (tile_map) {
tile_set = tile_map->get_tileset();
}
_update_editors();
tile_map_changed_needs_update = false;
}
} break;
}
}
void TilesEditorPlugin::make_visible(bool p_visible) {
if (p_visible || is_tile_map_selected()) {
// Disable and hide invalid editors.
TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
tileset_editor_button->set_visible(tile_set.is_valid());
tilemap_editor_button->set_visible(tile_map);
if (tile_map && (!is_editing_tile_set || !p_visible)) {
EditorNode::get_singleton()->make_bottom_panel_item_visible(tilemap_editor);
} else {
EditorNode::get_singleton()->make_bottom_panel_item_visible(tileset_editor);
}
is_visible = true;
} else {
tileset_editor_button->hide();
tilemap_editor_button->hide();
EditorNode::get_singleton()->hide_bottom_panel();
is_visible = false;
}
}
bool TilesEditorPlugin::is_tile_map_selected() {
TypedArray<Node> selection = EditorInterface::get_singleton()->get_selection()->get_selected_nodes();
if (selection.size() == 1 && Object::cast_to<TileMap>(selection[0])) {
return true;
}
return false;
}
void TilesEditorPlugin::queue_pattern_preview(Ref<TileSet> p_tile_set, Ref<TileMapPattern> p_pattern, Callable p_callback) {
void TilesEditorUtils::queue_pattern_preview(Ref<TileSet> p_tile_set, Ref<TileMapPattern> p_pattern, Callable p_callback) {
ERR_FAIL_COND(!p_tile_set.is_valid());
ERR_FAIL_COND(!p_pattern.is_valid());
{
@@ -232,11 +156,11 @@ void TilesEditorPlugin::queue_pattern_preview(Ref<TileSet> p_tile_set, Ref<TileM
pattern_preview_sem.post();
}
void TilesEditorPlugin::set_sources_lists_current(int p_current) {
void TilesEditorUtils::set_sources_lists_current(int p_current) {
atlas_sources_lists_current = p_current;
}
void TilesEditorPlugin::synchronize_sources_list(Object *p_current_list, Object *p_current_sort_button) {
void TilesEditorUtils::synchronize_sources_list(Object *p_current_list, Object *p_current_sort_button) {
ItemList *item_list = Object::cast_to<ItemList>(p_current_list);
MenuButton *sorting_button = Object::cast_to<MenuButton>(p_current_sort_button);
ERR_FAIL_COND(!item_list);
@@ -264,12 +188,12 @@ void TilesEditorPlugin::synchronize_sources_list(Object *p_current_list, Object
}
}
void TilesEditorPlugin::set_atlas_view_transform(float p_zoom, Vector2 p_scroll) {
void TilesEditorUtils::set_atlas_view_transform(float p_zoom, Vector2 p_scroll) {
atlas_view_zoom = p_zoom;
atlas_view_scroll = p_scroll;
}
void TilesEditorPlugin::synchronize_atlas_view(Object *p_current) {
void TilesEditorUtils::synchronize_atlas_view(Object *p_current) {
TileAtlasView *tile_atlas_view = Object::cast_to<TileAtlasView>(p_current);
ERR_FAIL_COND(!tile_atlas_view);
@@ -278,11 +202,11 @@ void TilesEditorPlugin::synchronize_atlas_view(Object *p_current) {
}
}
void TilesEditorPlugin::set_sorting_option(int p_option) {
void TilesEditorUtils::set_sorting_option(int p_option) {
source_sort = p_option;
}
List<int> TilesEditorPlugin::get_sorted_sources(const Ref<TileSet> p_tile_set) const {
List<int> TilesEditorUtils::get_sorted_sources(const Ref<TileSet> p_tile_set) const {
SourceNameComparator::tile_set = p_tile_set;
List<int> source_ids;
@@ -310,9 +234,9 @@ List<int> TilesEditorPlugin::get_sorted_sources(const Ref<TileSet> p_tile_set) c
return source_ids;
}
Ref<TileSet> TilesEditorPlugin::SourceNameComparator::tile_set;
Ref<TileSet> TilesEditorUtils::SourceNameComparator::tile_set;
bool TilesEditorPlugin::SourceNameComparator::operator()(const int &p_a, const int &p_b) const {
bool TilesEditorUtils::SourceNameComparator::operator()(const int &p_a, const int &p_b) const {
String name_a;
String name_b;
@@ -359,55 +283,7 @@ bool TilesEditorPlugin::SourceNameComparator::operator()(const int &p_a, const i
return NaturalNoCaseComparator()(name_a, name_b);
}
void TilesEditorPlugin::edit(Object *p_object) {
// Disconnect to changes.
TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
if (tile_map) {
tile_map->disconnect("changed", callable_mp(this, &TilesEditorPlugin::_tile_map_changed));
}
// Update edited objects.
tile_set = Ref<TileSet>();
is_editing_tile_set = false;
if (p_object) {
if (p_object->is_class("TileMap")) {
tile_map_id = p_object->get_instance_id();
tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
tile_set = tile_map->get_tileset();
EditorNode::get_singleton()->make_bottom_panel_item_visible(tilemap_editor);
} else if (p_object->is_class("TileSet")) {
tile_set = Ref<TileSet>(p_object);
if (tile_map) {
if (tile_map->get_tileset() != tile_set || !tile_map->is_inside_tree() || !is_tile_map_selected()) {
tile_map = nullptr;
tile_map_id = ObjectID();
}
}
is_editing_tile_set = true;
}
}
// Update the editors.
_update_editors();
// If the tileset is being edited, the visibility function must be called
// here after _update_editors has been called.
if (is_editing_tile_set) {
EditorNode::get_singleton()->make_bottom_panel_item_visible(tileset_editor);
}
// Add change listener.
if (tile_map) {
tile_map->connect("changed", callable_mp(this, &TilesEditorPlugin::_tile_map_changed));
}
}
bool TilesEditorPlugin::handles(Object *p_object) const {
return p_object->is_class("TileMap") || p_object->is_class("TileSet");
}
void TilesEditorPlugin::draw_selection_rect(CanvasItem *p_ci, const Rect2 &p_rect, const Color &p_color) {
void TilesEditorUtils::draw_selection_rect(CanvasItem *p_ci, const Rect2 &p_rect, const Color &p_color) {
real_t scale = p_ci->get_global_transform().get_scale().x * 0.5;
p_ci->draw_set_transform(p_rect.position, 0, Vector2(1, 1) / scale);
RS::get_singleton()->canvas_item_add_nine_patch(
@@ -416,40 +292,13 @@ void TilesEditorPlugin::draw_selection_rect(CanvasItem *p_ci, const Rect2 &p_rec
p_ci->draw_set_transform_matrix(Transform2D());
}
TilesEditorPlugin::TilesEditorPlugin() {
set_process_internal(true);
// Update the singleton.
TilesEditorUtils::TilesEditorUtils() {
singleton = this;
// Tileset editor.
tileset_editor = memnew(TileSetEditor);
tileset_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
tileset_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
tileset_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
tileset_editor->hide();
// Tilemap editor.
tilemap_editor = memnew(TileMapEditor);
tilemap_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
tilemap_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
tilemap_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
tilemap_editor->hide();
// Pattern preview generation thread.
pattern_preview_thread.start(_thread_func, this);
// Bottom buttons.
tileset_editor_button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("TileSet"), tileset_editor);
tileset_editor_button->hide();
tilemap_editor_button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("TileMap"), tilemap_editor);
tilemap_editor_button->hide();
// Initialization.
_update_editors();
}
TilesEditorPlugin::~TilesEditorPlugin() {
TilesEditorUtils::~TilesEditorUtils() {
if (pattern_preview_thread.is_started()) {
pattern_thread_exit.set();
pattern_preview_sem.post();
@@ -459,4 +308,124 @@ TilesEditorPlugin::~TilesEditorPlugin() {
}
pattern_preview_thread.wait_to_finish();
}
singleton = nullptr;
}
void TileMapEditorPlugin::_tile_map_changed() {
if (tile_map_changed_needs_update) {
return;
}
tile_map_changed_needs_update = true;
callable_mp(this, &TileMapEditorPlugin::_update_tile_map).call_deferred();
}
void TileMapEditorPlugin::_update_tile_map() {
if (tile_map && tile_map->get_tileset().is_valid()) {
EditorNode::get_singleton()->edit_item(tile_map->get_tileset().ptr(), InspectorDock::get_inspector_singleton());
}
tile_map_changed_needs_update = false;
}
void TileMapEditorPlugin::_notification(int p_notification) {
if (p_notification == NOTIFICATION_EXIT_TREE) {
get_tree()->queue_delete(TilesEditorUtils::get_singleton());
}
}
void TileMapEditorPlugin::edit(Object *p_object) {
if (tile_map) {
tile_map->disconnect("changed", callable_mp(this, &TileMapEditorPlugin::_tile_map_changed));
}
tile_map = Object::cast_to<TileMap>(p_object);
editor->edit(tile_map);
if (tile_map) {
tile_map->connect("changed", callable_mp(this, &TileMapEditorPlugin::_tile_map_changed));
if (tile_map->get_tileset().is_valid()) {
EditorNode::get_singleton()->edit_item(tile_map->get_tileset().ptr(), InspectorDock::get_inspector_singleton());
}
}
}
bool TileMapEditorPlugin::handles(Object *p_object) const {
return Object::cast_to<TileMap>(p_object) != nullptr;
}
void TileMapEditorPlugin::make_visible(bool p_visible) {
if (p_visible) {
button->show();
EditorNode::get_singleton()->make_bottom_panel_item_visible(editor);
} else {
button->hide();
if (editor->is_visible_in_tree()) {
EditorNode::get_singleton()->hide_bottom_panel();
}
}
}
bool TileMapEditorPlugin::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
return editor->forward_canvas_gui_input(p_event);
}
void TileMapEditorPlugin::forward_canvas_draw_over_viewport(Control *p_overlay) {
editor->forward_canvas_draw_over_viewport(p_overlay);
}
bool TileMapEditorPlugin::is_editor_visible() const {
return editor->is_visible_in_tree();
}
TileMapEditorPlugin::TileMapEditorPlugin() {
memnew(TilesEditorUtils);
local_singleton = this;
editor = memnew(TileMapEditor);
editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
editor->hide();
button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("TileMap"), editor);
button->hide();
}
TileMapEditorPlugin::~TileMapEditorPlugin() {
local_singleton = nullptr;
}
void TileSetEditorPlugin::edit(Object *p_object) {
editor->edit(Ref<TileSet>(p_object));
}
bool TileSetEditorPlugin::handles(Object *p_object) const {
return Object::cast_to<TileSet>(p_object) != nullptr;
}
void TileSetEditorPlugin::make_visible(bool p_visible) {
if (p_visible) {
button->show();
if (!local_singleton->is_editor_visible()) {
EditorNode::get_singleton()->make_bottom_panel_item_visible(editor);
}
} else {
button->hide();
if (editor->is_visible_in_tree()) {
EditorNode::get_singleton()->hide_bottom_panel();
}
}
}
TileSetEditorPlugin::TileSetEditorPlugin() {
DEV_ASSERT(local_singleton);
editor = memnew(TileSetEditor);
editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
editor->hide();
button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("TileSet"), editor);
button->hide();
}