[nostalgia/core/studio] Cleanup TileSheetEditorModel update tracking

This commit is contained in:
Gary Talent 2022-02-19 03:06:18 -06:00
parent db2a225855
commit b517cf6858
2 changed files with 14 additions and 9 deletions

View File

@ -16,6 +16,8 @@ TileSheetEditorModel::TileSheetEditorModel(Context *ctx, const ox::String &path)
oxRequireT(img, readObj<TileSheet>(ctx, path.c_str())); oxRequireT(img, readObj<TileSheet>(ctx, path.c_str()));
m_img = *img; m_img = *img;
oxThrowError(readObj<Palette>(ctx, m_img.defaultPalette).moveTo(&m_pal)); oxThrowError(readObj<Palette>(ctx, m_img.defaultPalette).moveTo(&m_pal));
m_undoStack.undoTriggered.connect(this, &TileSheetEditorModel::markUpdated);
m_undoStack.redoTriggered.connect(this, &TileSheetEditorModel::markUpdated);
} }
void TileSheetEditorModel::cut() { void TileSheetEditorModel::cut() {
@ -29,11 +31,11 @@ void TileSheetEditorModel::paste() {
void TileSheetEditorModel::drawCommand(const geo::Point &pt, std::size_t palIdx) noexcept { void TileSheetEditorModel::drawCommand(const geo::Point &pt, std::size_t palIdx) noexcept {
if (m_ongoingDrawCommand) { 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 { } else {
const auto idx = ptToIdx(pt, m_img.columns()); const auto idx = ptToIdx(pt, m_img.columns());
if (m_img.getPixel(idx) != palIdx) { 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; return m_updated;
} }
ox::Error TileSheetEditorModel::markUpdated() noexcept {
m_updated = true;
return OxError(0);
}
void TileSheetEditorModel::ackUpdate() noexcept { void TileSheetEditorModel::ackUpdate() noexcept {
m_updated = false; m_updated = false;
} }

View File

@ -31,17 +31,15 @@ struct DrawCommand: public studio::UndoCommand {
TileSheet *m_img = nullptr; TileSheet *m_img = nullptr;
ox::Vector<Change, 8> m_changes; ox::Vector<Change, 8> m_changes;
int m_palIdx = 0; int m_palIdx = 0;
bool *m_modelUpdated = nullptr;
public: public:
constexpr DrawCommand(bool *updated, TileSheet *img, std::size_t idx, int palIdx) noexcept { constexpr DrawCommand(TileSheet *img, std::size_t idx, int palIdx) noexcept {
m_modelUpdated = updated;
m_img = img; m_img = img;
m_changes.emplace_back(idx, m_img->getPixel(idx)); m_changes.emplace_back(idx, m_img->getPixel(idx));
m_palIdx = palIdx; 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) { if (m_changes.back().value.idx != idx && m_img->getPixel(idx) != m_palIdx) {
// duplicate entries are bad // duplicate entries are bad
auto existing = std::find_if(m_changes.cbegin(), m_changes.cend(), [idx](const auto &c) { 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) { for (const auto &c : m_changes) {
m_img->setPixel(c.idx, m_palIdx); m_img->setPixel(c.idx, m_palIdx);
} }
*m_modelUpdated = true;
} }
void undo() noexcept final { void undo() noexcept final {
for (const auto &c : m_changes) { for (const auto &c : m_changes) {
m_img->setPixel(c.idx, c.oldPalIdx); m_img->setPixel(c.idx, c.oldPalIdx);
} }
*m_modelUpdated = true;
} }
}; };
@ -111,7 +107,7 @@ oxModelBegin(TileSheetClipboard)
oxModelFieldRename(p2, m_p2) oxModelFieldRename(p2, m_p2)
oxModelEnd() oxModelEnd()
class TileSheetEditorModel { class TileSheetEditorModel: public ox::SignalHandler {
private: private:
TileSheet m_img; TileSheet m_img;
@ -149,6 +145,8 @@ class TileSheetEditorModel {
[[nodiscard]] [[nodiscard]]
bool updated() const noexcept; bool updated() const noexcept;
ox::Error markUpdated() noexcept;
void ackUpdate() noexcept; void ackUpdate() noexcept;
ox::Error saveFile() noexcept; ox::Error saveFile() noexcept;