From 72f4db3d5ea1fc6da4f0b73a9df456932b868718 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 15 Nov 2024 01:39:57 -0600 Subject: [PATCH] [nostalgia/core/studio] Fix paste command to never paste beyond target dimensions --- .../commands/cutpastecommand.cpp | 20 ++++++++++--------- .../commands/cutpastecommand.hpp | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/cutpastecommand.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/cutpastecommand.cpp index 5f01df75..0ab608f4 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/cutpastecommand.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/cutpastecommand.cpp @@ -12,7 +12,7 @@ TileSheetClipboard::Pixel::Pixel(uint16_t pColorIdx, ox::Point pPt) noexcept { } -void TileSheetClipboard::addPixel(const ox::Point &pt, uint16_t colorIdx) noexcept { +void TileSheetClipboard::addPixel(ox::Point const&pt, uint16_t colorIdx) noexcept { m_pixels.emplace_back(colorIdx, pt); } @@ -25,18 +25,20 @@ CutPasteCommand::CutPasteCommand( CommandId commandId, TileSheet &img, TileSheet::SubSheetIdx subSheetIdx, - const ox::Point &dstStart, - const ox::Point &dstEnd, - const TileSheetClipboard &cb) noexcept: + ox::Point const&dstStart, + ox::Point dstEnd, + TileSheetClipboard const&cb) noexcept: m_commandId(commandId), m_img(img), m_subSheetIdx(std::move(subSheetIdx)) { - const auto &subsheet = getSubSheet(m_img, m_subSheetIdx); - for (const auto &p : cb.pixels()) { - const auto dstPt = p.pt + dstStart; + auto const&ss = getSubSheet(m_img, m_subSheetIdx); + dstEnd.x = std::min(ss.columns * TileWidth - 1, dstEnd.x); + dstEnd.y = std::min(ss.rows * TileHeight - 1, dstEnd.y); + for (auto const&p : cb.pixels()) { + auto const dstPt = p.pt + dstStart; if (dstPt.x <= dstEnd.x && dstPt.y <= dstEnd.y) { - const auto idx = core::idx(subsheet, dstPt); - m_changes.emplace_back(static_cast(idx), p.colorIdx, getPixel(subsheet, m_img.bpp, idx)); + auto const idx = core::idx(ss, dstPt); + m_changes.emplace_back(static_cast(idx), p.colorIdx, getPixel(ss, m_img.bpp, idx)); } } } diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/cutpastecommand.hpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/cutpastecommand.hpp index 5dece149..6df44640 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/cutpastecommand.hpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/cutpastecommand.hpp @@ -68,7 +68,7 @@ class CutPasteCommand: public TileSheetCommand { TileSheet &img, TileSheet::SubSheetIdx subSheetIdx, ox::Point const&dstStart, - ox::Point const&dstEnd, + ox::Point dstEnd, TileSheetClipboard const&cb) noexcept; ox::Error redo() noexcept final; @@ -83,4 +83,4 @@ class CutPasteCommand: public TileSheetCommand { }; -} \ No newline at end of file +}