[nostalgia/core/studio] Cleanup TileSheetEditorModel update tracking
This commit is contained in:
parent
db2a225855
commit
b517cf6858
@ -16,6 +16,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.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;
|
||||
}
|
||||
|
@ -31,17 +31,15 @@ struct DrawCommand: public studio::UndoCommand {
|
||||
TileSheet *m_img = nullptr;
|
||||
ox::Vector<Change, 8> 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;
|
||||
|
Loading…
Reference in New Issue
Block a user