diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/inserttilescommand.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/inserttilescommand.cpp index 3a986439..9651feed 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/inserttilescommand.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/commands/inserttilescommand.cpp @@ -6,7 +6,7 @@ namespace nostalgia::core { -core::InsertTilesCommand::InsertTilesCommand( +InsertTilesCommand::InsertTilesCommand( TileSheet &img, TileSheet::SubSheetIdx idx, std::size_t tileIdx, @@ -31,9 +31,11 @@ ox::Error InsertTilesCommand::redo() noexcept { auto &s = getSubSheet(m_img, m_idx); auto &p = s.pixels; auto dstPos = m_insertPos + m_insertCnt; - auto const dst = &p[dstPos]; auto const src = &p[m_insertPos]; - ox::memmove(dst, src, p.size() - dstPos); + if (dstPos < p.size()) { + auto const dst = &p[dstPos]; + ox::memmove(dst, src, p.size() - dstPos); + } ox::memset(src, 0, m_insertCnt * sizeof(decltype(p[0]))); return {}; } @@ -41,12 +43,14 @@ ox::Error InsertTilesCommand::redo() noexcept { ox::Error InsertTilesCommand::undo() noexcept { auto &s = getSubSheet(m_img, m_idx); auto &p = s.pixels; - auto const srcIdx = m_insertPos + m_insertCnt; - auto const src = &p[srcIdx]; auto const dst1 = &p[m_insertPos]; auto const dst2 = &p[p.size() - m_insertCnt]; - auto const sz = p.size() - srcIdx; - ox::memmove(dst1, src, sz); + auto const srcIdx = m_insertPos + m_insertCnt; + if (srcIdx < p.size()) { + auto const sz = p.size() - srcIdx; + auto const src = &p[srcIdx]; + ox::memmove(dst1, src, sz); + } ox::memcpy(dst2, m_deletedPixels.data(), m_deletedPixels.size()); return {}; } diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.cpp index 0cf1d87d..2181d864 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditorview.cpp @@ -56,8 +56,10 @@ void TileSheetEditorView::scrollH(ox::Vec2 const&paneSz, float wheelh) noexcept } void TileSheetEditorView::insertTile(ox::Vec2 const&paneSize, ox::Vec2 const&clickPos) noexcept { - auto const pt = clickPoint(paneSize, clickPos); + auto pt = clickPoint(paneSize, clickPos); auto const&s = m_model.activeSubSheet(); + pt.x = ox::min(pt.x, s.columns * TileWidth); + pt.y = ox::min(pt.y, s.rows * TileHeight); auto const tileIdx = ptToIdx(pt, s.columns) / PixelsPerTile; m_model.insertTiles(m_model.activeSubSheetIdx(), tileIdx, 1); }