From 95d58d04fb51f9cc8309e8ba2ea424688c5fa984 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 22 Mar 2020 02:06:21 -0500 Subject: [PATCH] [nostalgia/core/studio] Fix delete tile undo to restore pixel data --- src/nostalgia/core/studio/tilesheeteditor.cpp | 27 +++++++++++++------ src/nostalgia/core/studio/tilesheeteditor.hpp | 4 +-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/nostalgia/core/studio/tilesheeteditor.cpp b/src/nostalgia/core/studio/tilesheeteditor.cpp index e8526a12..c5ad7162 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor.cpp @@ -6,6 +6,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include + #include #include #include @@ -191,6 +193,7 @@ class InsertTileCommand: public QUndoCommand { SheetData *m_sheetData = nullptr; int m_idx = 0; bool m_delete = false; + QVector m_tileRestore; public: InsertTileCommand(SheetData *sheetData, int idx, bool del = false) { @@ -207,17 +210,17 @@ class InsertTileCommand: public QUndoCommand { void redo() override { if (m_delete) { - m_sheetData->deleteTile(m_idx); + m_tileRestore = m_sheetData->deleteTile(m_idx); } else { - m_sheetData->insertTile(m_idx); + m_sheetData->insertTile(m_idx, m_tileRestore); } } void undo() override { if (m_delete) { - m_sheetData->insertTile(m_idx); + m_sheetData->insertTile(m_idx, m_tileRestore); } else { - m_sheetData->deleteTile(m_idx); + m_tileRestore = m_sheetData->deleteTile(m_idx); } } @@ -316,16 +319,24 @@ void SheetData::setPalette(const studio::Context *ctx, QString palPath) { emit changeOccurred(); } -void SheetData::insertTile(int tileIdx) { - m_pixels.insert(tileIdx * PixelsPerTile, PixelsPerTile, 0); +void SheetData::insertTile(int tileIdx, QVector tileData) { + auto pxIdx = tileIdx * PixelsPerTile; + m_pixels.insert(pxIdx, PixelsPerTile, 0); + std::copy(tileData.begin(), tileData.end(), &m_pixels[pxIdx]); emit pixelsChanged(); emit changeOccurred(); } -void SheetData::deleteTile(int tileIdx) { - m_pixels.remove(tileIdx * PixelsPerTile, PixelsPerTile); +QVector SheetData::deleteTile(int tileIdx) { + QVector out; + auto pxIdx = tileIdx * PixelsPerTile; + std::copy(m_pixels.begin() + pxIdx, + m_pixels.begin() + (pxIdx + PixelsPerTile), + std::back_inserter(out)); + m_pixels.remove(pxIdx, PixelsPerTile); emit pixelsChanged(); emit changeOccurred(); + return out; } void SheetData::setSelectedColor(int index) { diff --git a/src/nostalgia/core/studio/tilesheeteditor.hpp b/src/nostalgia/core/studio/tilesheeteditor.hpp index 48f9d66b..d3a19853 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.hpp +++ b/src/nostalgia/core/studio/tilesheeteditor.hpp @@ -65,9 +65,9 @@ class SheetData: public QObject { void setPalette(const studio::Context *ctx, QString palPath); - void insertTile(int tileIdx); + void insertTile(int tileIdx, QVector tileData = {}); - void deleteTile(int tileIdx); + QVector deleteTile(int tileIdx); void setSelectedColor(int index);