From 37b6095e137661315149ed6663e2552e9279606d Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 6 Dec 2019 00:50:32 -0600 Subject: [PATCH] [nostalgia/core/studio] Add command sequence grouping to tile sheet editor --- src/nostalgia/core/studio/TileSheetEditor.qml | 37 +++++++++++------- src/nostalgia/core/studio/tilesheeteditor.cpp | 38 +++++++++++-------- src/nostalgia/core/studio/tilesheeteditor.hpp | 8 +++- 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/src/nostalgia/core/studio/TileSheetEditor.qml b/src/nostalgia/core/studio/TileSheetEditor.qml index 4eb8d84d..053dc948 100644 --- a/src/nostalgia/core/studio/TileSheetEditor.qml +++ b/src/nostalgia/core/studio/TileSheetEditor.qml @@ -17,22 +17,19 @@ Rectangle { id: mouseArea anchors.fill: parent acceptedButtons: Qt.LeftButton - onPositionChanged: { - var gridX = mouseX - tileGrid.x; - var gridY = mouseY - tileGrid.y; - var tile = tileGrid.childAt(gridX, gridY); - if (tile === null) { - return; + + onPressed: { + var pixel = pixelAt(mouseX, mouseY); + if (pixel) { + sheetData.beginCmd() + sheetData.updatePixel(pixel) } - var tileX = gridX - tile.x; - var tileY = gridY - tile.y; - var pixel = tile.pixelAt(tileX, tileY); - if (pixel === null) { - return; - } - sheetData.updatePixels([pixel]); } + onPositionChanged: sheetData.updatePixel(pixelAt(mouseX, mouseY)) + onReleased: sheetData.endCmd(); + onCanceled: sheetData.endCmd(); + Grid { id: tileGrid property int baseTileSize: Math.min(parent.width / tileGrid.columns, parent.height / tileGrid.rows) @@ -51,6 +48,20 @@ Rectangle { } } } + + function pixelAt(x, y) { + var gridX = x - tileGrid.x; + var gridY = y - tileGrid.y; + var tile = tileGrid.childAt(gridX, gridY); + if (tile === null) { + return null; + } + var tileX = gridX - tile.x; + var tileY = gridY - tile.y; + var pixel = tile.pixelAt(tileX, tileY); + return pixel; + } + } } diff --git a/src/nostalgia/core/studio/tilesheeteditor.cpp b/src/nostalgia/core/studio/tilesheeteditor.cpp index 7631f32a..a54dc558 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -50,25 +49,20 @@ class UpdatePixelsCommand: public QUndoCommand { } }; - uint64_t cmdIdx = 0; + uint64_t m_cmdIdx = 0; int m_newColorId = 0; const QStringList &m_palette; QVector &m_pixels; QSet m_pixelUpdates; public: - UpdatePixelsCommand(QVector &pixels, const QStringList &palette, const QVariantList &pixelItems, int newColorId, uint64_t cmdIdx): m_palette(palette), m_pixels(pixels) { + UpdatePixelsCommand(QVector &pixels, const QStringList &palette, QQuickItem *pixelItem, int newColorId, uint64_t cmdIdx): m_palette(palette), m_pixels(pixels) { m_newColorId = newColorId; - cmdIdx = cmdIdx; - for (auto &pi : pixelItems) { - auto p = qobject_cast(pi.value()); - if (p) { - PixelUpdate pu; - pu.item = p; - pu.oldColorId = m_palette.indexOf(p->property("color").toString()); - m_pixelUpdates.insert(pu); - } - } + m_cmdIdx = cmdIdx; + PixelUpdate pu; + pu.item = pixelItem; + pu.oldColorId = m_palette.indexOf(pixelItem->property("color").toString()); + m_pixelUpdates.insert(pu); } virtual ~UpdatePixelsCommand() = default; @@ -79,7 +73,7 @@ class UpdatePixelsCommand: public QUndoCommand { bool mergeWith(const QUndoCommand *cmd) override { auto other = static_cast(cmd); - if (cmdIdx == other->cmdIdx) { + if (m_cmdIdx == other->m_cmdIdx) { m_pixelUpdates.unite(other->m_pixelUpdates); return true; } @@ -106,8 +100,12 @@ QString SheetData::pixel(int index) { return m_palette[m_pixels[index]]; } -void SheetData::updatePixels(QVariantList pixelItems) { - m_cmdStack.push(new UpdatePixelsCommand(m_pixels, m_palette, pixelItems, m_selectedColor, m_cmdIdx++)); +void SheetData::updatePixel(QVariant pixelItem) { + auto p = qobject_cast(pixelItem.value()); + if (p && p != m_prevPixelUpdated) { + m_cmdStack.push(new UpdatePixelsCommand(m_pixels, m_palette, p, m_selectedColor, m_cmdIdx)); + m_prevPixelUpdated = p; + } } int SheetData::columns() { @@ -180,6 +178,14 @@ void SheetData::updatePixels(const NostalgiaGraphic *ng, const NostalgiaPalette } } +void SheetData::beginCmd() { + ++m_cmdIdx; +} + +void SheetData::endCmd() { + m_prevPixelUpdated = nullptr; +} + TileSheetEditor::TileSheetEditor(QString path, const studio::Context *ctx, QWidget *parent): QWidget(parent) { m_ctx = ctx; diff --git a/src/nostalgia/core/studio/tilesheeteditor.hpp b/src/nostalgia/core/studio/tilesheeteditor.hpp index 043c72d8..0d549504 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.hpp +++ b/src/nostalgia/core/studio/tilesheeteditor.hpp @@ -8,6 +8,7 @@ #pragma once +#include #include #include #include @@ -27,6 +28,7 @@ class SheetData: public QObject { Q_PROPERTY(int rows READ rows WRITE setRows NOTIFY rowsChanged) private: + QQuickItem *m_prevPixelUpdated = nullptr; uint64_t m_cmdIdx = 0; QUndoStack m_cmdStack; QStringList m_palette; @@ -38,7 +40,11 @@ class SheetData: public QObject { public: Q_INVOKABLE QString pixel(int index); - Q_INVOKABLE void updatePixels(QVariantList pixels); + Q_INVOKABLE void updatePixel(QVariant pixel); + + Q_INVOKABLE void beginCmd(); + + Q_INVOKABLE void endCmd(); int columns();