diff --git a/src/nostalgia/core/consts.hpp b/src/nostalgia/core/consts.hpp index d8de8e78..8f973413 100644 --- a/src/nostalgia/core/consts.hpp +++ b/src/nostalgia/core/consts.hpp @@ -3,6 +3,8 @@ namespace nostalgia::core { +constexpr auto PixelsPerTile = 64; + constexpr auto FileExt_ng = ".ng"; constexpr auto FileExt_npal = ".npal"; diff --git a/src/nostalgia/core/studio/TileSheetEditor.qml b/src/nostalgia/core/studio/TileSheetEditor.qml index 7ec09331..d783c6e1 100644 --- a/src/nostalgia/core/studio/TileSheetEditor.qml +++ b/src/nostalgia/core/studio/TileSheetEditor.qml @@ -19,13 +19,23 @@ Rectangle { id: mouseArea width: tileSheetEditor.width height: tileSheetEditor.height - acceptedButtons: Qt.LeftButton + acceptedButtons: Qt.LeftButton | Qt.RightButton + + onClicked: { + if (mouse.button === Qt.RightButton) { + contextMenu.popup(); + } else { + contextMenu.dismiss(); + } + } onPressed: { - var pixel = pixelAt(mouseX, mouseY); - if (pixel) { - sheetData.beginCmd() - sheetData.updatePixel(pixel) + if (mouse.button === Qt.LeftButton && !contextMenu.visible) { + var pixel = pixelAt(mouseX, mouseY); + if (pixel) { + sheetData.beginCmd(); + sheetData.updatePixel(pixel); + } } } @@ -75,10 +85,28 @@ Rectangle { wheel.accepted = true; } + Menu { + id: contextMenu + + MenuItem { + text: "Insert Tile" + onTriggered: { + var tile = mouseArea.tileAt(contextMenu.x, contextMenu.y); + sheetData.insertTileCmd(tile.tileNumber); + } + } + } + onPositionChanged: sheetData.updatePixel(pixelAt(mouseX, mouseY)) onReleased: sheetData.endCmd() onCanceled: sheetData.endCmd() + function tileAt(x, y) { + var gridX = x - tileGrid.x; + var gridY = y - tileGrid.y; + return tileGrid.childAt(gridX, gridY); + } + function pixelAt(x, y) { var gridX = x - tileGrid.x; var gridY = y - tileGrid.y; diff --git a/src/nostalgia/core/studio/consts.hpp b/src/nostalgia/core/studio/consts.hpp index a3b6b450..185cf465 100644 --- a/src/nostalgia/core/studio/consts.hpp +++ b/src/nostalgia/core/studio/consts.hpp @@ -18,6 +18,7 @@ constexpr auto PaletteDir = "/Palettes/"; enum class CommandId { UpdatePixel = 1, UpdateDimension = 2, + InsertTile = 3, }; } diff --git a/src/nostalgia/core/studio/tilesheeteditor.cpp b/src/nostalgia/core/studio/tilesheeteditor.cpp index 5fa15c2f..7f1942d2 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor.cpp @@ -186,6 +186,34 @@ class UpdatePixelsCommand: public QUndoCommand { }; +class InsertTileCommand: public QUndoCommand { + private: + SheetData *m_sheetData = nullptr; + int m_idx = 0; + + public: + InsertTileCommand(SheetData *sheetData, int idx) { + m_sheetData = sheetData; + m_idx = idx; + } + + virtual ~InsertTileCommand() = default; + + int id() const override { + return static_cast(CommandId::InsertTile); + } + + void redo() override { + m_sheetData->insertTile(m_idx); + } + + void undo() override { + m_sheetData->deleteTile(m_idx); + } + +}; + + void SheetData::updatePixel(QVariant pixelItem) { auto p = qobject_cast(pixelItem.value()); if (p && p != m_prevPixelUpdated) { @@ -195,6 +223,18 @@ void SheetData::updatePixel(QVariant pixelItem) { } } +void SheetData::beginCmd() { + ++m_cmdIdx; +} + +void SheetData::endCmd() { + m_prevPixelUpdated = nullptr; +} + +void SheetData::insertTileCmd(int tileIdx) { + m_cmdStack.push(new InsertTileCommand(this, tileIdx)); +} + int SheetData::columns() const { return m_columns; } @@ -262,6 +302,17 @@ void SheetData::setPalette(const studio::Context *ctx, QString palPath) { emit changeOccurred(); } +void SheetData::insertTile(int tileIdx) { + m_pixels.insert(tileIdx * PixelsPerTile, PixelsPerTile, 0); + emit pixelsChanged(); + emit changeOccurred(); +} + +void SheetData::deleteTile(int tileIdx) { + m_pixels.remove(tileIdx * PixelsPerTile, PixelsPerTile); + emit pixelsChanged(); +} + void SheetData::setSelectedColor(int index) { m_selectedColor = index; } @@ -334,15 +385,6 @@ std::unique_ptr SheetData::toNostalgiaGraphic() const { return ng; } -void SheetData::beginCmd() { - ++m_cmdIdx; -} - -void SheetData::endCmd() { - m_prevPixelUpdated = nullptr; -} - - TileSheetEditor::TileSheetEditor(QString path, const studio::Context *ctx, QWidget *parent): studio::Editor(parent) { m_ctx = ctx; m_itemPath = path; diff --git a/src/nostalgia/core/studio/tilesheeteditor.hpp b/src/nostalgia/core/studio/tilesheeteditor.hpp index 3609ddcc..7b463306 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.hpp +++ b/src/nostalgia/core/studio/tilesheeteditor.hpp @@ -43,6 +43,8 @@ class SheetData: public QObject { Q_INVOKABLE void endCmd(); + Q_INVOKABLE void insertTileCmd(int tileIdx); + [[nodiscard]] int columns() const; [[nodiscard]] int rows() const; @@ -61,6 +63,10 @@ class SheetData: public QObject { void setPalette(const studio::Context *ctx, QString palPath); + void insertTile(int tileIdx); + + void deleteTile(int tileIdx); + void setSelectedColor(int index); QUndoStack *undoStack();