diff --git a/forms/paletteeditor.ui b/forms/paletteeditor.ui index 12a0213e..c5cb9149 100644 --- a/forms/paletteeditor.ui +++ b/forms/paletteeditor.ui @@ -38,7 +38,11 @@ - + + + Qt::NoFocus + + @@ -1640,8 +1644,32 @@ 21 + + + Edit + + + + + + + + Undo + + + Ctrl+Z + + + + + Redo + + + Ctrl+Y + + diff --git a/include/ui/paletteeditor.h b/include/ui/paletteeditor.h index 878455db..084c772d 100644 --- a/include/ui/paletteeditor.h +++ b/include/ui/paletteeditor.h @@ -6,11 +6,20 @@ #include #include #include "project.h" +#include "history.h" namespace Ui { class PaletteEditor; } +class PaletteHistoryItem { +public: + QList colors; + PaletteHistoryItem(QList colors) { + this->colors = colors; + } +}; + class PaletteEditor : public QMainWindow { Q_OBJECT public: @@ -26,6 +35,7 @@ private: QList rgbLabels; Tileset *primaryTileset; Tileset *secondaryTileset; + QList> palettesHistory; void disableSliderSignals(); void enableSliderSignals(); void initColorSliders(); @@ -33,6 +43,8 @@ private: void refreshColors(); void refreshColor(int); void setColor(int); + void commitEditHistory(); + void setColorsFromHistory(PaletteHistoryItem*, int); signals: void closed(); @@ -40,6 +52,8 @@ signals: void changedPalette(int); private slots: void on_spinBox_PaletteId_valueChanged(int arg1); + void on_actionUndo_triggered(); + void on_actionRedo_triggered(); }; #endif // PALETTEEDITOR_H diff --git a/src/ui/paletteeditor.cpp b/src/ui/paletteeditor.cpp index b6f21600..e08ecd92 100644 --- a/src/ui/paletteeditor.cpp +++ b/src/ui/paletteeditor.cpp @@ -100,6 +100,11 @@ PaletteEditor::PaletteEditor(Project *project, Tileset *primaryTileset, Tileset this->rgbLabels.append(this->ui->label_rgb14); this->rgbLabels.append(this->ui->label_rgb15); + // Setup edit-undo history for each of the palettes. + for (int i = 0; i < Project::getNumPalettesTotal(); i++) { + this->palettesHistory.append(History()); + } + this->initColorSliders(); this->refreshColorSliders(); this->refreshColors(); @@ -188,6 +193,7 @@ void PaletteEditor::setColor(int colorIndex) { : this->secondaryTileset; (*tileset->palettes)[paletteNum][colorIndex] = qRgb(red, green, blue); this->refreshColor(colorIndex); + this->commitEditHistory(); emit this->changedPaletteColor(); } @@ -196,3 +202,43 @@ void PaletteEditor::on_spinBox_PaletteId_valueChanged(int paletteId) { this->refreshColors(); emit this->changedPalette(paletteId); } + +void PaletteEditor::commitEditHistory() { + int paletteId = this->ui->spinBox_PaletteId->value(); + QList colors; + for (int i = 0; i < 16; i++) { + colors.append(qRgb(this->sliders[i][0]->value() * 8, this->sliders[i][1]->value() * 8, this->sliders[i][2]->value() * 8)); + } + PaletteHistoryItem *commit = new PaletteHistoryItem(colors); + this->palettesHistory[paletteId].push(commit); +} + +void PaletteEditor::on_actionUndo_triggered() +{ + int paletteId = this->ui->spinBox_PaletteId->value(); + PaletteHistoryItem *prev = this->palettesHistory[paletteId].back(); + this->setColorsFromHistory(prev, paletteId); +} + +void PaletteEditor::on_actionRedo_triggered() +{ + int paletteId = this->ui->spinBox_PaletteId->value(); + PaletteHistoryItem *next = this->palettesHistory[paletteId].next(); + this->setColorsFromHistory(next, paletteId); +} + +void PaletteEditor::setColorsFromHistory(PaletteHistoryItem *history, int paletteId) { + if (!history) return; + + for (int i = 0; i < 16; i++) { + if (paletteId < Project::getNumPalettesPrimary()) { + (*this->primaryTileset->palettes)[paletteId][i] = history->colors.at(i); + } else { + (*this->secondaryTileset->palettes)[paletteId][i] = history->colors.at(i); + } + } + + this->refreshColorSliders(); + this->refreshColors(); + emit this->changedPaletteColor(); +}