From ae80d22769efe0149c79fac304236cf8b69cb866 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 11 Mar 2022 02:11:58 -0600 Subject: [PATCH] [nostalgia/core/studio] Fix TileSheet Editor paste bounds checking --- .../core/studio/tilesheeteditormodel.cpp | 6 ++++-- .../core/studio/tilesheeteditormodel.hpp | 17 ++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/nostalgia/core/studio/tilesheeteditormodel.cpp b/src/nostalgia/core/studio/tilesheeteditormodel.cpp index 6404673eb..2fcf2f247 100644 --- a/src/nostalgia/core/studio/tilesheeteditormodel.cpp +++ b/src/nostalgia/core/studio/tilesheeteditormodel.cpp @@ -71,8 +71,10 @@ void TileSheetEditorModel::paste() { oxErrf("Could not read clipboard: {}", toStr(err)); return; } - const auto pt = m_selectionOrigin == geo::Point(-1, -1) ? geo::Point(0, 0) : m_selectionOrigin; - pushCommand(new PasteCommand(&m_img, m_activeSubsSheetIdx, pt, cb)); + 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)); } void TileSheetEditorModel::drawCommand(const geo::Point &pt, std::size_t palIdx) noexcept { diff --git a/src/nostalgia/core/studio/tilesheeteditormodel.hpp b/src/nostalgia/core/studio/tilesheeteditormodel.hpp index b35836fad..493f47d3f 100644 --- a/src/nostalgia/core/studio/tilesheeteditormodel.hpp +++ b/src/nostalgia/core/studio/tilesheeteditormodel.hpp @@ -142,31 +142,30 @@ struct PasteCommand: public studio::UndoCommand { ox::Vector m_changes; public: - constexpr PasteCommand(TileSheet *img, const TileSheet::SubSheetIdx &subSheetIdx, const geo::Point &dst, const TileSheetClipboard &cb) noexcept { + constexpr PasteCommand(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); for (const auto &p : cb.pixels()) { - const auto idx = subsheet.idx(p.pt + dst); - m_changes.emplace_back(idx, p.colorIdx, subsheet.getPixel(m_img->bpp, idx)); + const auto dstPt = p.pt + dstStart; + if (dstPt.x <= dstEnd.x && dstPt.y <= dstEnd.y) { + const auto idx = subsheet.idx(dstPt); + m_changes.emplace_back(idx, p.colorIdx, subsheet.getPixel(m_img->bpp, idx)); + } } } void redo() noexcept final { auto &subsheet = m_img->getSubSheet(m_subSheetIdx); for (const auto &c : m_changes) { - if (c.idx < subsheet.pixelCnt(m_img->bpp)) { - subsheet.setPixel(m_img->bpp, c.idx, c.newPalIdx); - } + subsheet.setPixel(m_img->bpp, c.idx, c.newPalIdx); } } void undo() noexcept final { auto &subsheet = m_img->getSubSheet(m_subSheetIdx); for (const auto &c : m_changes) { - if (c.idx < subsheet.pixelCnt(m_img->bpp)) { - subsheet.setPixel(m_img->bpp, c.idx, c.oldPalIdx); - } + subsheet.setPixel(m_img->bpp, c.idx, c.oldPalIdx); } }