diff --git a/src/nostalgia/core/studio/paletteeditor.cpp b/src/nostalgia/core/studio/paletteeditor.cpp index e6da31fd..fcbcb460 100644 --- a/src/nostalgia/core/studio/paletteeditor.cpp +++ b/src/nostalgia/core/studio/paletteeditor.cpp @@ -25,6 +25,7 @@ enum class PaletteEditorCommandId { AddColor, RemoveColor, UpdateColor, + MoveColor, }; struct PaletteEditorColorTableDelegate: public QStyledItemDelegate { @@ -138,6 +139,35 @@ class UpdateColorCommand: public QUndoCommand { }; +class MoveColorCommand: public QUndoCommand { + private: + PaletteEditor *m_editor = nullptr; + int m_idx = -1; + int m_offset = 0; + + public: + MoveColorCommand(PaletteEditor *editor, int idx, int offset) { + m_editor = editor; + m_idx = idx; + m_offset = offset; + } + + virtual ~MoveColorCommand() = default; + + int id() const override { + return static_cast(PaletteEditorCommandId::MoveColor); + } + + void redo() override { + m_editor->moveColor(m_idx, m_offset); + } + + void undo() override { + m_editor->moveColor(m_idx + m_offset, -m_offset); + } + +}; + static QTableWidgetItem *mkCell(QString v, bool editable = true) { auto c = new QTableWidgetItem; @@ -164,9 +194,15 @@ PaletteEditor::PaletteEditor(QString path, const studio::Context *ctx, QWidget * auto tb = new QToolBar(tr("Tile Sheet Options")); m_addBtn = new QPushButton(tr("Add"), tb); m_rmBtn = new QPushButton(tr("Remove"), tb); + m_moveUpBtn = new QPushButton(tr("Move Up"), tb); + m_moveDownBtn = new QPushButton(tr("Move Down"), tb); m_rmBtn->setEnabled(false); + m_moveUpBtn->setEnabled(false); + m_moveDownBtn->setEnabled(false); tb->addWidget(m_addBtn); tb->addWidget(m_rmBtn); + tb->addWidget(m_moveUpBtn); + tb->addWidget(m_moveDownBtn); canvasLyt->setMenuBar(tb); m_table = new QTableWidget(this); @@ -181,6 +217,8 @@ PaletteEditor::PaletteEditor(QString path, const studio::Context *ctx, QWidget * connect(m_table, &QTableWidget::itemSelectionChanged, this, &PaletteEditor::colorSelected); connect(m_addBtn, &QPushButton::clicked, this, &PaletteEditor::addColorClicked); connect(m_rmBtn, &QPushButton::clicked, this, &PaletteEditor::rmColorClicked); + connect(m_moveUpBtn, &QPushButton::clicked, this, &PaletteEditor::moveColorUpClicked); + connect(m_moveDownBtn, &QPushButton::clicked, this, &PaletteEditor::moveColorDownClicked); connect(m_table, &QTableWidget::cellChanged, this, &PaletteEditor::cellChanged); m_pal = m_ctx->project->loadObj(m_itemPath); load(); @@ -190,18 +228,6 @@ QString PaletteEditor::itemName() const { return m_itemPath.mid(m_itemPath.lastIndexOf('/')); } -void PaletteEditor::addColor(int idx, Color16 c) { - m_pal->colors.insert(static_cast(idx), c); - addTableRow(idx, c); - setUnsavedChanges(true); -} - -void PaletteEditor::rmColor(int idx) { - rmTableRow(idx); - m_pal->colors.erase(static_cast(idx)); - setUnsavedChanges(true); -} - void PaletteEditor::addTableRow(int i, Color16 c) { disconnect(m_table, &QTableWidget::cellChanged, this, &PaletteEditor::cellChanged); m_table->insertRow(i); @@ -229,12 +255,33 @@ void PaletteEditor::setTableRow(int idx, Color16 c) { connect(m_table, &QTableWidget::cellChanged, this, &PaletteEditor::cellChanged); } +void PaletteEditor::addColor(int idx, Color16 c) { + m_pal->colors.insert(static_cast(idx), c); + addTableRow(idx, c); + setUnsavedChanges(true); +} + +void PaletteEditor::rmColor(int idx) { + rmTableRow(idx); + m_pal->colors.erase(static_cast(idx)); + setUnsavedChanges(true); +} + void PaletteEditor::updateColor(int idx, Color16 c) { m_pal->colors[static_cast(idx)] = c; setTableRow(idx, c); setUnsavedChanges(true); } +void PaletteEditor::moveColor(int idx, int offset) { + auto c = m_pal->colors[static_cast(idx)]; + m_pal->colors.erase(static_cast(idx)); + m_pal->colors.insert(static_cast(idx + offset), c); + rmTableRow(idx); + addTableRow(idx + offset, c); + setUnsavedChanges(true); +} + void PaletteEditor::saveItem() { m_ctx->project->writeObj(m_itemPath, m_pal.get()); } @@ -260,8 +307,12 @@ void PaletteEditor::colorSelected() { auto row = m_table->currentRow(); if (row > -1) { m_rmBtn->setEnabled(true); + m_moveUpBtn->setEnabled(row > 0); + m_moveDownBtn->setEnabled(row < m_table->rowCount() - 1); } else { m_rmBtn->setEnabled(false); + m_moveUpBtn->setEnabled(false); + m_moveDownBtn->setEnabled(false); } } @@ -281,4 +332,14 @@ void PaletteEditor::rmColorClicked() { undoStack()->push(new RemoveColorCommand(this, m_pal->colors[static_cast(row)], row)); } +void PaletteEditor::moveColorUpClicked() { + auto row = m_table->currentRow(); + undoStack()->push(new MoveColorCommand(this, row, -1)); +} + +void PaletteEditor::moveColorDownClicked() { + auto row = m_table->currentRow(); + undoStack()->push(new MoveColorCommand(this, row, 1)); +} + } diff --git a/src/nostalgia/core/studio/paletteeditor.hpp b/src/nostalgia/core/studio/paletteeditor.hpp index d8de1e66..24bcfb2f 100644 --- a/src/nostalgia/core/studio/paletteeditor.hpp +++ b/src/nostalgia/core/studio/paletteeditor.hpp @@ -18,6 +18,7 @@ class PaletteEditor: public studio::Editor { friend class AddColorCommand; friend class RemoveColorCommand; friend class UpdateColorCommand; + friend class MoveColorCommand; private: const studio::Context *m_ctx = nullptr; @@ -26,6 +27,8 @@ class PaletteEditor: public studio::Editor { class QTableWidget *m_table = nullptr; class QPushButton *m_addBtn = nullptr; class QPushButton *m_rmBtn = nullptr; + class QPushButton *m_moveUpBtn = nullptr; + class QPushButton *m_moveDownBtn = nullptr; public: PaletteEditor(QString path, const studio::Context *ctx, QWidget *parent); @@ -42,6 +45,8 @@ class PaletteEditor: public studio::Editor { void updateColor(int idx, Color16); + void moveColor(int idx, int offset); + void saveItem() override; private: @@ -64,6 +69,10 @@ class PaletteEditor: public studio::Editor { void rmColorClicked(); + void moveColorUpClicked(); + + void moveColorDownClicked(); + }; }