From 10d2f2c0647bb0bf6a5a0f0d0d80bcda9206eda4 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 27 Mar 2022 03:03:20 -0500 Subject: [PATCH] [nostalgia/core/studio] Fix TileSheetEditor cut to use undo stack --- .../core/studio/tilesheeteditormodel.cpp | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/nostalgia/core/studio/tilesheeteditormodel.cpp b/src/nostalgia/core/studio/tilesheeteditormodel.cpp index 11afdc84..44545206 100644 --- a/src/nostalgia/core/studio/tilesheeteditormodel.cpp +++ b/src/nostalgia/core/studio/tilesheeteditormodel.cpp @@ -56,8 +56,9 @@ enum class CommandId { AddSubSheet = 2, RmSubSheet = 3, UpdateSubSheet = 4, - Paste = 5, - PaletteChange = 6, + Cut = 5, + Paste = 6, + PaletteChange = 7, }; constexpr bool operator==(CommandId c, int i) noexcept { @@ -143,7 +144,8 @@ class DrawCommand: public studio::UndoCommand { }; -class PasteCommand: public studio::UndoCommand { +template +class CutPasteCommand: public studio::UndoCommand { private: struct Change { uint32_t idx = 0; @@ -155,7 +157,7 @@ class PasteCommand: public studio::UndoCommand { ox::Vector m_changes; public: - constexpr PasteCommand(TileSheet *img, const TileSheet::SubSheetIdx &subSheetIdx, const geo::Point &dstStart, const geo::Point &dstEnd, const TileSheetClipboard &cb) noexcept { + constexpr CutPasteCommand(TileSheet *img, const TileSheet::SubSheetIdx &subSheetIdx, const geo::Point &dstStart, const geo::Point &dstEnd, const TileSheetClipboard &cb) noexcept { m_img = img; m_subSheetIdx = subSheetIdx; const auto &subsheet = m_img->getSubSheet(subSheetIdx); @@ -184,7 +186,7 @@ class PasteCommand: public studio::UndoCommand { [[nodiscard]] int commandId() const noexcept final { - return static_cast(CommandId::Paste); + return static_cast(CommandId); } }; @@ -362,20 +364,24 @@ TileSheetEditorModel::TileSheetEditorModel(Context *ctx, const ox::String &path) } void TileSheetEditorModel::cut() { - auto cb = ox::make_unique();; + TileSheetClipboard blankCb; + auto cb = ox::make_unique(); + const auto s = activeSubSheet(); for (int y = m_selectionBounds.y; y <= m_selectionBounds.y2(); ++y) { for (int x = m_selectionBounds.x; x <= m_selectionBounds.x2(); ++x) { auto pt = geo::Point(x, y); - const auto s = activeSubSheet(); const auto idx = s->idx(pt); const auto c = s->getPixel(m_img.bpp, idx); - s->setPixel(m_img.bpp, idx, 0); pt.x -= m_selectionBounds.x; pt.y -= m_selectionBounds.y; cb->addPixel(pt, c); + blankCb.addPixel(pt, 0); } } + const auto pt1 = m_selectionOrigin == geo::Point(-1, -1) ? geo::Point(0, 0) : m_selectionOrigin; + const auto pt2 = geo::Point(s->columns * TileWidth, s->rows * TileHeight); setClipboardObject(m_ctx, std::move(cb)); + pushCommand(new CutPasteCommand(&m_img, m_activeSubsSheetIdx, pt1, pt2, blankCb)); } void TileSheetEditorModel::copy() { @@ -404,7 +410,7 @@ void TileSheetEditorModel::paste() { const auto s = activeSubSheet(); const auto pt1 = m_selectionOrigin == geo::Point(-1, -1) ? geo::Point(0, 0) : m_selectionOrigin; const auto pt2 = geo::Point(s->columns * TileWidth, s->rows * TileHeight); - pushCommand(new PasteCommand(&m_img, m_activeSubsSheetIdx, pt1, pt2, *cb)); + pushCommand(new CutPasteCommand(&m_img, m_activeSubsSheetIdx, pt1, pt2, *cb)); } const ox::FileAddress &TileSheetEditorModel::palPath() const noexcept { @@ -519,6 +525,7 @@ ox::Error TileSheetEditorModel::markUpdated(int cmdId) noexcept { } break; } + case CommandId::Cut: case CommandId::Draw: case CommandId::Paste: case CommandId::UpdateSubSheet: