Add edit-undo history for tileset palette changes

This commit is contained in:
Marcus Huderle 2018-10-05 18:38:11 -05:00
parent 7a47c64509
commit bbfecba1ba
3 changed files with 89 additions and 1 deletions

View file

@ -38,7 +38,11 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="spinBox_PaletteId"/>
<widget class="QSpinBox" name="spinBox_PaletteId">
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
</widget>
</item>
</layout>
</widget>
@ -1640,8 +1644,32 @@
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuEdit">
<property name="title">
<string>Edit</string>
</property>
<addaction name="actionUndo"/>
<addaction name="actionRedo"/>
</widget>
<addaction name="menuEdit"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionUndo">
<property name="text">
<string>Undo</string>
</property>
<property name="shortcut">
<string>Ctrl+Z</string>
</property>
</action>
<action name="actionRedo">
<property name="text">
<string>Redo</string>
</property>
<property name="shortcut">
<string>Ctrl+Y</string>
</property>
</action>
</widget>
<resources/>
<connections/>

View file

@ -6,11 +6,20 @@
#include <QFrame>
#include <QLabel>
#include "project.h"
#include "history.h"
namespace Ui {
class PaletteEditor;
}
class PaletteHistoryItem {
public:
QList<QRgb> colors;
PaletteHistoryItem(QList<QRgb> colors) {
this->colors = colors;
}
};
class PaletteEditor : public QMainWindow {
Q_OBJECT
public:
@ -26,6 +35,7 @@ private:
QList<QLabel*> rgbLabels;
Tileset *primaryTileset;
Tileset *secondaryTileset;
QList<History<PaletteHistoryItem*>> 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

View file

@ -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<PaletteHistoryItem*>());
}
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<QRgb> 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();
}