Skip to content

Commit

Permalink
MenuEdit: Add undo/redo options for frame ops
Browse files Browse the repository at this point in the history
This patch adds undo/redo options for "edit" menu in the app. Those
options were already here, but were unused. Only palette widget used
QUndoStack implemented in the application.
  • Loading branch information
tetektoza committed Nov 4, 2023
1 parent ffb5b1a commit a64d3b5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 15 deletions.
59 changes: 48 additions & 11 deletions source/celview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,42 @@ void CelScene::contextMenuEvent(QContextMenuEvent *event)
emit this->showContextMenu(event->globalPos());
}

CelView::CelView(QWidget *parent)
: QWidget(parent)
RemoveFrameCommand::RemoveFrameCommand(D1Gfx *g, CelView * cv, QUndoCommand *parent) : gfx(g), celview(cv), QUndoCommand(parent)
{
}

void RemoveFrameCommand::undo()
{
int prevFrameCount = this->gfx->getFrameCount();

this->gfx->insertFrame(this->currentFrameIndex, this->img);

this->celview->updateCurrentFrameIndex(prevFrameCount);
this->celview->updateGroupIndex();

this->celview->initialize(this->gfx);
this->celview->displayFrame();
}

void RemoveFrameCommand::redo()
{
this->currentFrameIndex = this->celview->getCurrentFrameIndex();
this->img = this->gfx->getFrameImage(this->currentFrameIndex);

// remove the frame
this->gfx->removeFrame(this->currentFrameIndex);

if (this->gfx->getFrameCount() == this->currentFrameIndex) {
this->celview->updateCurrentFrameIndex(std::max(0, this->currentFrameIndex - 1));
}
this->celview->updateGroupIndex();
// update the view
this->celview->initialize(this->gfx);
this->celview->displayFrame();
}

CelView::CelView(QUndoStack *us, QWidget *parent)
: QWidget(parent), undostack(us)
, ui(new Ui::CelView())
, celScene(new CelScene(this))
{
Expand Down Expand Up @@ -118,6 +152,11 @@ int CelView::getCurrentFrameIndex()
return this->currentFrameIndex;
}

void CelView::updateCurrentFrameIndex(int frameIdx)
{
this->currentFrameIndex = frameIdx;
}

void CelView::framePixelClicked(unsigned x, unsigned y)
{
int frameIndex = this->currentFrameIndex;
Expand Down Expand Up @@ -201,15 +240,13 @@ void CelView::replaceCurrentFrame(const QString &imagefilePath)

void CelView::removeCurrentFrame()
{
// remove the frame
this->gfx->removeFrame(this->currentFrameIndex);
if (this->gfx->getFrameCount() == this->currentFrameIndex) {
this->currentFrameIndex = std::max(0, this->currentFrameIndex - 1);
}
this->updateGroupIndex();
// update the view
this->initialize(this->gfx);
this->displayFrame();
std::cout << "RemoveCurrentFrame toggled" << std::endl;
// Build color editing command and connect it to the current palette widget
// to update the PAL/TRN and CEL views when undo/redo is performed
RemoveFrameCommand *command = new RemoveFrameCommand(this->gfx, this);
// QObject::connect(command, &EditTranslationsCommand::modified, this, &PaletteWidget::modify);

this->undostack->push(command);
}

void CelView::regroupFrames(int numGroups)
Expand Down
28 changes: 26 additions & 2 deletions source/celview.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <QStringList>
#include <QTimer>
#include <QWidget>
#include <QUndoStack>
#include <QUndoCommand>

#include "d1gfx.h"

Expand Down Expand Up @@ -46,16 +48,18 @@ class CelView : public QWidget {
Q_OBJECT

public:
explicit CelView(QWidget *parent = nullptr);
explicit CelView(QUndoStack *us, QWidget *parent = nullptr);
~CelView();

void initialize(D1Gfx *gfx);
void updateCurrentFrameIndex(int frameIdx);
int getCurrentFrameIndex();
void framePixelClicked(unsigned x, unsigned y);
void insertImageFiles(IMAGE_FILE_MODE mode, const QStringList &imagefilePaths, bool append);
void replaceCurrentFrame(const QString &imagefilePath);
void removeCurrentFrame();
void regroupFrames(int numGroups);
void updateGroupIndex();

void displayFrame();

Expand All @@ -66,7 +70,6 @@ class CelView : public QWidget {
private:
void update();
void insertFrame(IMAGE_FILE_MODE mode, int index, const QString &imagefilePath);
void updateGroupIndex();
void setGroupIndex();

private slots:
Expand Down Expand Up @@ -98,6 +101,7 @@ private slots:
void ShowContextMenu(const QPoint &pos);

private:
QPointer<QUndoStack> undostack;
Ui::CelView *ui;
CelScene *celScene;

Expand All @@ -109,3 +113,23 @@ private slots:

QTimer playTimer;
};

class RemoveFrameCommand : public QObject, public QUndoCommand {
Q_OBJECT

public:
explicit RemoveFrameCommand(D1Gfx *g, CelView *cv, QUndoCommand *parent = nullptr);
~RemoveFrameCommand() = default;

void undo() override;
void redo() override;

//signals:
// void modified();

private:
QPointer<D1Gfx> gfx;
QPointer<CelView> celview;
QImage img;
int currentFrameIndex = 0;
};
9 changes: 7 additions & 2 deletions source/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ MainWindow::MainWindow()
// Initialize 'Undo/Redo' of 'Edit
this->undoStack = new QUndoStack(this);
this->undoAction = undoStack->createUndoAction(this, "Undo");

this->undoAction->setShortcuts(QKeySequence::Undo);
this->ui->menuEdit->addAction(this->undoAction);
this->redoAction = undoStack->createRedoAction(this, "Redo");
this->redoAction->setEnabled(this->undoStack->canRedo());
this->redoAction->setShortcuts(QKeySequence::Redo);
this->ui->menuEdit->addAction(this->redoAction);
this->ui->menuEdit->addSeparator();
Expand Down Expand Up @@ -149,7 +151,7 @@ void MainWindow::updateWindow()
// rebuild palette hits
this->palHits->update();
this->palWidget->refresh();
this->undoStack->clear();
// this->undoStack->clear();
// update menu options
bool hasFrame = this->gfx->getFrameCount() != 0;
this->frameMenu.actions()[2]->setEnabled(hasFrame); // replace frame
Expand Down Expand Up @@ -640,7 +642,7 @@ void MainWindow::openFile(const OpenAsParam &params)
}
// Otherwise build a CelView
else {
this->celView = new CelView();
this->celView = new CelView(this->undoStack);
this->celView->initialize(this->gfx);

// Refresh CEL view if a PAL or TRN is modified
Expand Down Expand Up @@ -1099,6 +1101,7 @@ void MainWindow::on_actionDel_Frame_triggered()
{
if (this->celView != nullptr) {
this->celView->removeCurrentFrame();
this->undoAction->setEnabled(this->undoStack->canUndo());
}
if (this->levelCelView != nullptr) {
this->levelCelView->removeCurrentFrame();
Expand Down Expand Up @@ -1160,6 +1163,8 @@ void MainWindow::on_actionCreate_Tile_triggered()
this->updateWindow();
}

//void MainWindow::on_action

void MainWindow::on_actionClone_Tile_triggered()
{
this->levelCelView->cloneTile();
Expand Down

0 comments on commit a64d3b5

Please sign in to comment.