[nostalgia/core/studio] Add ability to insert tile

This commit is contained in:
Gary Talent 2020-03-22 01:24:53 -05:00
parent 4f3497326f
commit 5aeddd87e3
5 changed files with 93 additions and 14 deletions

View File

@ -3,6 +3,8 @@
namespace nostalgia::core {
constexpr auto PixelsPerTile = 64;
constexpr auto FileExt_ng = ".ng";
constexpr auto FileExt_npal = ".npal";

View File

@ -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;

View File

@ -18,6 +18,7 @@ constexpr auto PaletteDir = "/Palettes/";
enum class CommandId {
UpdatePixel = 1,
UpdateDimension = 2,
InsertTile = 3,
};
}

View File

@ -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<int>(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<QQuickItem*>(pixelItem.value<QObject*>());
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<NostalgiaGraphic> 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;

View File

@ -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();