From 25a4e67aaa5b80518393935f060c1c2d0fcac80e Mon Sep 17 00:00:00 2001 From: David Capello Date: Tue, 27 Dec 2022 14:11:05 -0300 Subject: [PATCH] [lua] Add remaptileset event for sprites --- src/app/cmd/remap_tilemaps.cpp | 23 +++++++++++++++++------ src/app/cmd/remap_tilemaps.h | 3 ++- src/app/doc_observer.h | 7 +++++++ src/app/script/events_class.cpp | 16 +++++++++++++++- src/app/script/values.cpp | 24 ++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/app/cmd/remap_tilemaps.cpp b/src/app/cmd/remap_tilemaps.cpp index 76893a8f4..8ff91fa6b 100644 --- a/src/app/cmd/remap_tilemaps.cpp +++ b/src/app/cmd/remap_tilemaps.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2022 Igara Studio S.A. // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -10,9 +10,10 @@ #include "app/cmd/remap_tilemaps.h" +#include "app/doc.h" +#include "app/doc_event.h" #include "doc/cel.h" #include "doc/cels_range.h" -#include "doc/image.h" #include "doc/layer.h" #include "doc/layer_tilemap.h" #include "doc/remap.h" @@ -34,19 +35,29 @@ RemapTilemaps::RemapTilemaps(Tileset* tileset, void RemapTilemaps::onExecute() { Tileset* tileset = this->tileset(); - Sprite* spr = tileset->sprite(); - spr->remapTilemaps(tileset, m_remap); + remapTileset(tileset, m_remap); incrementVersions(tileset); } void RemapTilemaps::onUndo() { Tileset* tileset = this->tileset(); - Sprite* spr = tileset->sprite(); - spr->remapTilemaps(tileset, m_remap.invert()); + remapTileset(tileset, m_remap.invert()); incrementVersions(tileset); } +void RemapTilemaps::remapTileset(Tileset* tileset, const Remap& remap) +{ + Sprite* spr = tileset->sprite(); + spr->remapTilemaps(tileset, remap); + + Doc* doc = static_cast(spr->document()); + DocEvent ev(doc); + ev.sprite(spr); + ev.tileset(tileset); + doc->notify_observers(&DocObserver::onRemapTileset, ev, remap); +} + void RemapTilemaps::incrementVersions(Tileset* tileset) { Sprite* spr = tileset->sprite(); diff --git a/src/app/cmd/remap_tilemaps.h b/src/app/cmd/remap_tilemaps.h index c43743c21..dbc6cdcce 100644 --- a/src/app/cmd/remap_tilemaps.h +++ b/src/app/cmd/remap_tilemaps.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2022 Igara Studio S.A. // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -30,6 +30,7 @@ namespace cmd { } private: + void remapTileset(Tileset* tileset, const Remap& remap); void incrementVersions(Tileset* tileset); Remap m_remap; diff --git a/src/app/doc_observer.h b/src/app/doc_observer.h index 920f7c156..1c2453247 100644 --- a/src/app/doc_observer.h +++ b/src/app/doc_observer.h @@ -9,6 +9,10 @@ #define APP_DOC_OBSERVER_H_INCLUDED #pragma once +namespace doc { + class Remap; +} + namespace app { class Doc; class DocEvent; @@ -92,6 +96,9 @@ namespace app { // The collapsed/expanded flag of a specific layer changed. virtual void onLayerCollapsedChanged(DocEvent& ev) { } + // The tileset was remapped (e.g. when tiles are re-ordered). + virtual void onRemapTileset(DocEvent& ev, const doc::Remap& remap) { } + }; } // namespace app diff --git a/src/app/script/events_class.cpp b/src/app/script/events_class.cpp index 64dc5131e..e292c2a11 100644 --- a/src/app/script/events_class.cpp +++ b/src/app/script/events_class.cpp @@ -12,6 +12,7 @@ #include "app/context.h" #include "app/context_observer.h" #include "app/doc.h" +#include "app/doc_event.h" #include "app/doc_undo.h" #include "app/doc_undo_observer.h" #include "app/pref/preferences.h" @@ -209,7 +210,12 @@ class SpriteEvents : public Events , public DocUndoObserver , public DocObserver { public: - enum : EventType { Unknown = -1, Change, FilenameChange }; + enum : EventType { + Unknown = -1, + Change, + FilenameChange, + RemapTileset, + }; SpriteEvents(const Sprite* sprite) : m_spriteId(sprite->id()) { @@ -235,6 +241,8 @@ public: return Change; else if (std::strcmp(eventName, "filenamechange") == 0) return FilenameChange; + else if (std::strcmp(eventName, "remaptileset") == 0) + return RemapTileset; else return Unknown; } @@ -253,6 +261,12 @@ public: call(FilenameChange); } + void onRemapTileset(DocEvent& ev, const doc::Remap& remap) override { + const bool fromUndo = (ev.document()->transaction() == nullptr); + call(RemapTileset, { { "remap", std::any(&remap) }, + { "fromUndo", fromUndo } }); + } + // DocUndoObserver impl void onAddUndoState(DocUndo* history) override { call(Change); diff --git a/src/app/script/values.cpp b/src/app/script/values.cpp index 8c69b2979..582484d6d 100644 --- a/src/app/script/values.cpp +++ b/src/app/script/values.cpp @@ -13,6 +13,7 @@ #include "app/pref/preferences.h" #include "app/script/engine.h" #include "app/script/luacpp.h" +#include "doc/remap.h" #include @@ -76,6 +77,27 @@ std::string get_value_from_lua(lua_State* L, int index) { return std::string(); } +// ---------------------------------------------------------------------- +// doc::Remap + +template<> +void push_value_to_lua(lua_State* L, const doc::Remap& value) { + lua_newtable(L); + for (int i=0; i(&value)) push_value_to_lua(L, *v); + else if (const doc::Remap* v = std::any_cast(value)) + push_value_to_lua(L, *v); else { ASSERT(false); throw std::runtime_error("Cannot convert type inside std::any");