[nostalgia/studio] Fix TileSheetEditor to switch to appropriate subsheet on undo/redo

This commit is contained in:
2022-05-18 21:15:11 -05:00
parent 71f6364ea3
commit 6854e658a0
12 changed files with 158 additions and 52 deletions
@@ -73,7 +73,13 @@ constexpr bool operator==(int i, CommandId c) noexcept {
return static_cast<int>(c) == i;
}
class DrawCommand: public studio::UndoCommand {
class TileSheetCommand: public studio::UndoCommand {
public:
[[nodiscard]]
virtual const TileSheet::SubSheetIdx &subsheetIdx() const noexcept = 0;
};
class DrawCommand: public TileSheetCommand {
private:
struct Change {
uint32_t idx = 0;
@@ -150,10 +156,15 @@ class DrawCommand: public studio::UndoCommand {
return static_cast<int>(CommandId::Draw);
}
[[nodiscard]]
const TileSheet::SubSheetIdx &subsheetIdx() const noexcept override {
return m_subSheetIdx;
}
};
template<CommandId CommandId>
class CutPasteCommand: public studio::UndoCommand {
class CutPasteCommand: public TileSheetCommand {
private:
struct Change {
uint32_t idx = 0;
@@ -202,9 +213,14 @@ class CutPasteCommand: public studio::UndoCommand {
return static_cast<int>(CommandId);
}
[[nodiscard]]
const TileSheet::SubSheetIdx &subsheetIdx() const noexcept override {
return m_subSheetIdx;
}
};
class AddSubSheetCommand: public studio::UndoCommand {
class AddSubSheetCommand: public TileSheetCommand {
private:
TileSheet *m_img = nullptr;
TileSheet::SubSheetIdx m_parentIdx;
@@ -261,9 +277,14 @@ class AddSubSheetCommand: public studio::UndoCommand {
return static_cast<int>(CommandId::AddSubSheet);
}
[[nodiscard]]
const TileSheet::SubSheetIdx &subsheetIdx() const noexcept override {
return m_parentIdx;
}
};
class RmSubSheetCommand: public studio::UndoCommand {
class RmSubSheetCommand: public TileSheetCommand {
private:
TileSheet *m_img = nullptr;
TileSheet::SubSheetIdx m_idx;
@@ -296,9 +317,14 @@ class RmSubSheetCommand: public studio::UndoCommand {
return static_cast<int>(CommandId::RmSubSheet);
}
[[nodiscard]]
const TileSheet::SubSheetIdx &subsheetIdx() const noexcept override {
return m_idx;
}
};
class UpdateSubSheetCommand: public studio::UndoCommand {
class UpdateSubSheetCommand: public TileSheetCommand {
private:
TileSheet *m_img = nullptr;
TileSheet::SubSheetIdx m_idx;
@@ -336,16 +362,23 @@ class UpdateSubSheetCommand: public studio::UndoCommand {
return static_cast<int>(CommandId::UpdateSubSheet);
}
[[nodiscard]]
const TileSheet::SubSheetIdx &subsheetIdx() const noexcept override {
return m_idx;
}
};
class PaletteChangeCommand: public studio::UndoCommand {
class PaletteChangeCommand: public TileSheetCommand {
private:
TileSheet::SubSheetIdx m_idx;
TileSheet *m_img = nullptr;
ox::FileAddress m_oldPalette;
ox::FileAddress m_newPalette;
public:
constexpr PaletteChangeCommand(TileSheet *img, const ox::String &newPalette) noexcept {
constexpr PaletteChangeCommand(const TileSheet::SubSheetIdx &idx, TileSheet *img, const ox::String &newPalette) noexcept {
m_idx = idx;
m_img = img;
m_oldPalette = m_img->defaultPalette;
m_newPalette = newPalette;
@@ -364,6 +397,11 @@ class PaletteChangeCommand: public studio::UndoCommand {
return static_cast<int>(CommandId::PaletteChange);
}
[[nodiscard]]
const TileSheet::SubSheetIdx &subsheetIdx() const noexcept override {
return m_idx;
}
};
@@ -373,7 +411,8 @@ TileSheetEditorModel::TileSheetEditorModel(Context *ctx, const ox::String &path)
oxRequireT(img, readObj<TileSheet>(ctx, path.c_str()));
m_img = *img;
oxThrowError(readObj<Palette>(ctx, m_img.defaultPalette).moveTo(&m_pal));
m_undoStack.changeTriggered.connect(this, &TileSheetEditorModel::markUpdated);
m_pal.updated.connect(this, &TileSheetEditorModel::markUpdated);
m_undoStack.changeTriggered.connect(this, &TileSheetEditorModel::markUpdatedCmdId);
}
void TileSheetEditorModel::cut() {
@@ -431,7 +470,7 @@ const ox::FileAddress &TileSheetEditorModel::palPath() const noexcept {
}
ox::Error TileSheetEditorModel::setPalette(const ox::String &path) noexcept {
pushCommand(new PaletteChangeCommand(&m_img, path));
pushCommand(new PaletteChangeCommand(activeSubSheetIdx(), &m_img, path));
return OxError(0);
}
@@ -526,18 +565,12 @@ bool TileSheetEditorModel::updated() const noexcept {
return m_updated;
}
ox::Error TileSheetEditorModel::markUpdated(int cmdId) noexcept {
ox::Error TileSheetEditorModel::markUpdatedCmdId(const studio::UndoCommand *cmd) noexcept {
m_updated = true;
const auto cmdId = cmd->commandId();
switch (static_cast<CommandId>(cmdId)) {
case CommandId::AddSubSheet:
case CommandId::RmSubSheet: {
// make sure the current active SubSheet is still valid
auto idx = m_img.validateSubSheetIdx(m_activeSubsSheetIdx);
if (idx != m_activeSubsSheetIdx) {
setActiveSubsheet(idx);
}
break;
}
case CommandId::RmSubSheet:
case CommandId::Cut:
case CommandId::Draw:
case CommandId::Paste:
@@ -547,6 +580,16 @@ ox::Error TileSheetEditorModel::markUpdated(int cmdId) noexcept {
oxReturnError(readObj<Palette>(m_ctx, m_img.defaultPalette.getPath().value).moveTo(&m_pal));
break;
}
auto tsCmd = dynamic_cast<const TileSheetCommand*>(cmd);
auto idx = m_img.validateSubSheetIdx(tsCmd->subsheetIdx());
if (idx != m_activeSubsSheetIdx) {
setActiveSubsheet(idx);
}
return OxError(0);
}
ox::Error TileSheetEditorModel::markUpdated() noexcept {
m_updated = true;
return OxError(0);
}