From b517cf68585b52cad58983784fa38f90e5be26af Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 19 Feb 2022 03:06:18 -0600 Subject: [PATCH] [nostalgia/core/studio] Cleanup TileSheetEditorModel update tracking --- src/nostalgia/core/studio/tilesheeteditormodel.cpp | 11 +++++++++-- src/nostalgia/core/studio/tilesheeteditormodel.hpp | 12 +++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/nostalgia/core/studio/tilesheeteditormodel.cpp b/src/nostalgia/core/studio/tilesheeteditormodel.cpp index 782b126b..48f29d15 100644 --- a/src/nostalgia/core/studio/tilesheeteditormodel.cpp +++ b/src/nostalgia/core/studio/tilesheeteditormodel.cpp @@ -16,6 +16,8 @@ TileSheetEditorModel::TileSheetEditorModel(Context *ctx, const ox::String &path) oxRequireT(img, readObj(ctx, path.c_str())); m_img = *img; oxThrowError(readObj(ctx, m_img.defaultPalette).moveTo(&m_pal)); + m_undoStack.undoTriggered.connect(this, &TileSheetEditorModel::markUpdated); + m_undoStack.redoTriggered.connect(this, &TileSheetEditorModel::markUpdated); } void TileSheetEditorModel::cut() { @@ -29,11 +31,11 @@ void TileSheetEditorModel::paste() { void TileSheetEditorModel::drawCommand(const geo::Point &pt, std::size_t palIdx) noexcept { if (m_ongoingDrawCommand) { - m_updated = m_ongoingDrawCommand->append(ptToIdx(pt, m_img.columns())); + m_updated = m_updated || m_ongoingDrawCommand->append(ptToIdx(pt, m_img.columns())); } else { const auto idx = ptToIdx(pt, m_img.columns()); if (m_img.getPixel(idx) != palIdx) { - pushCommand(new DrawCommand(&m_updated, &m_img, idx, palIdx)); + pushCommand(new DrawCommand(&m_img, idx, palIdx)); } } } @@ -46,6 +48,11 @@ bool TileSheetEditorModel::updated() const noexcept { return m_updated; } +ox::Error TileSheetEditorModel::markUpdated() noexcept { + m_updated = true; + return OxError(0); +} + void TileSheetEditorModel::ackUpdate() noexcept { m_updated = false; } diff --git a/src/nostalgia/core/studio/tilesheeteditormodel.hpp b/src/nostalgia/core/studio/tilesheeteditormodel.hpp index 5145f1ee..8e7b2255 100644 --- a/src/nostalgia/core/studio/tilesheeteditormodel.hpp +++ b/src/nostalgia/core/studio/tilesheeteditormodel.hpp @@ -31,17 +31,15 @@ struct DrawCommand: public studio::UndoCommand { TileSheet *m_img = nullptr; ox::Vector m_changes; int m_palIdx = 0; - bool *m_modelUpdated = nullptr; public: - constexpr DrawCommand(bool *updated, TileSheet *img, std::size_t idx, int palIdx) noexcept { - m_modelUpdated = updated; + constexpr DrawCommand(TileSheet *img, std::size_t idx, int palIdx) noexcept { m_img = img; m_changes.emplace_back(idx, m_img->getPixel(idx)); m_palIdx = palIdx; } - constexpr bool append(std::size_t idx) noexcept { + constexpr auto append(std::size_t idx) noexcept { if (m_changes.back().value.idx != idx && m_img->getPixel(idx) != m_palIdx) { // duplicate entries are bad auto existing = std::find_if(m_changes.cbegin(), m_changes.cend(), [idx](const auto &c) { @@ -60,14 +58,12 @@ struct DrawCommand: public studio::UndoCommand { for (const auto &c : m_changes) { m_img->setPixel(c.idx, m_palIdx); } - *m_modelUpdated = true; } void undo() noexcept final { for (const auto &c : m_changes) { m_img->setPixel(c.idx, c.oldPalIdx); } - *m_modelUpdated = true; } }; @@ -111,7 +107,7 @@ oxModelBegin(TileSheetClipboard) oxModelFieldRename(p2, m_p2) oxModelEnd() -class TileSheetEditorModel { +class TileSheetEditorModel: public ox::SignalHandler { private: TileSheet m_img; @@ -149,6 +145,8 @@ class TileSheetEditorModel { [[nodiscard]] bool updated() const noexcept; + ox::Error markUpdated() noexcept; + void ackUpdate() noexcept; ox::Error saveFile() noexcept;