Skip to content

Commit

Permalink
Rewrite how state is recorded and saved temporarily.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrStevns committed Apr 28, 2024
1 parent e629e9c commit bdd92fd
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 235 deletions.
109 changes: 14 additions & 95 deletions core_lib/src/interface/undoredocommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void BitmapReplaceCommand::redo()
}

VectorReplaceCommand::VectorReplaceCommand(const VectorImage* undoVector,
const int& undoLayerId,
const int undoLayerId,
const QString& description,
Editor* editor,
QUndoCommand* parent) : UndoRedoCommand(editor, parent)
Expand All @@ -98,8 +98,6 @@ VectorReplaceCommand::VectorReplaceCommand(const VectorImage* undoVector,

void VectorReplaceCommand::undo()
{
qDebug() << "BackupVectorElement: undo";

QUndoCommand::undo();

Layer* layer = editor()->layers()->findLayerById(undoLayerId);
Expand All @@ -111,7 +109,6 @@ void VectorReplaceCommand::undo()

void VectorReplaceCommand::redo()
{
qDebug() << "BackupVectorElement: redo";
QUndoCommand::redo();

// Ignore automatic redo when added to undo stack
Expand All @@ -124,69 +121,26 @@ void VectorReplaceCommand::redo()
editor()->scrubTo(redoVector.pos());
}

TransformCommand::TransformCommand(KeyFrame* undoKeyFrame,
const int undoLayerId,
const QRectF& undoSelectionRect,
const QPointF undoTranslation,
TransformCommand::TransformCommand(const QRectF& undoSelectionRect,
const QPointF& undoTranslation,
const qreal undoRotationAngle,
const qreal undoScaleX,
const qreal undoScaleY,
const QPointF undoTransformAnchor,
const QPointF& undoTransformAnchor,
const bool roundPixels,
const QString& description,
Editor* editor,
QUndoCommand *parent) : UndoRedoCommand(editor, parent)
{
this->roundPixels = roundPixels;


qDebug() << "add transform command";
this->undoLayerId = undoLayerId;
this->undoSelectionRect = undoSelectionRect;
this->undoAnchor = undoTransformAnchor;
this->undoTranslation = undoTranslation;
this->undoRotationAngle = undoRotationAngle;
this->undoScaleX = undoScaleX;
this->undoScaleY = undoScaleY;

Layer* undoLayer = editor->layers()->findLayerById(undoLayerId);

switch(undoLayer->type())
{
case Layer::BITMAP:
{
undoBitmap = *static_cast<BitmapImage*>(undoKeyFrame);
break;
}
case Layer::VECTOR:
{
undoVector = *static_cast<VectorImage*>(undoKeyFrame);
break;
}
default:
break;
}

Layer* redoLayer = editor->layers()->currentLayer();
redoLayerId = redoLayer->id();

const int currentFrame = editor->currentFrame();

switch(redoLayer->type())
{
case Layer::BITMAP:
{
redoBitmap = *static_cast<LayerBitmap*>(redoLayer)->getBitmapImageAtFrame(currentFrame);
break;
}
case Layer::VECTOR:
{
redoVector = *static_cast<LayerVector*>(redoLayer)->
getVectorImageAtFrame(currentFrame);
break;
}
default:
break;
}

auto selectMan = editor->select();
redoSelectionRect = selectMan->mySelectionRect();
redoAnchor = selectMan->currentTransformAnchor();
Expand All @@ -200,70 +154,37 @@ TransformCommand::TransformCommand(KeyFrame* undoKeyFrame,

void TransformCommand::undo()
{
apply(undoBitmap,
undoVector,
undoSelectionRect,
apply(undoSelectionRect,
undoTranslation,
undoRotationAngle,
undoScaleX,
undoScaleY,
undoAnchor,
undoLayerId);
roundPixels);
}

void TransformCommand::redo()
{
// Ignore automatic redo when added to undo stack
if (isFirstRedo()) { setFirstRedo(false); return; }

apply(redoBitmap,
redoVector,
redoSelectionRect,
apply(redoSelectionRect,
redoTranslation,
redoRotationAngle,
redoScaleX,
redoScaleY,
redoAnchor,
redoLayerId);
roundPixels);
}

void TransformCommand::apply(const BitmapImage& bitmapImage,
const VectorImage& vectorImage,
const QRectF& selectionRect,
const QPointF translation,
void TransformCommand::apply(const QRectF& selectionRect,
const QPointF& translation,
const qreal rotationAngle,
const qreal scaleX,
const qreal scaleY,
const QPointF selectionAnchor,
const int layerId)
const QPointF& selectionAnchor,
const bool roundPixels)
{

Layer* layer = editor()->layers()->findLayerById(layerId);

int frameNumber = 0;
bool roundPixels = true;
switch(layer->type())
{
case Layer::BITMAP:
{
static_cast<LayerBitmap*>(layer)->replaceKeyFrame(&bitmapImage);
frameNumber = bitmapImage.pos();
break;
}
case Layer::VECTOR:
{
LayerVector* vlayer = static_cast<LayerVector*>(layer);
vlayer->replaceKeyFrame(&vectorImage);
frameNumber = vectorImage.pos();
roundPixels = false;
break;
}
default:
// Only canvas related KeyFrame types are relevant for transforms.
Q_UNREACHABLE();
break;
}

auto selectMan = editor()->select();
selectMan->setSelection(selectionRect, roundPixels);
selectMan->setTransformAnchor(selectionAnchor);
Expand All @@ -272,6 +193,4 @@ void TransformCommand::apply(const BitmapImage& bitmapImage,
selectMan->setScale(scaleX, scaleY);

selectMan->calculateSelectionTransformation();

emit editor()->frameModified(frameNumber);
}
44 changes: 17 additions & 27 deletions core_lib/src/interface/undoredocommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class BitmapReplaceCommand : public UndoRedoCommand

public:
BitmapReplaceCommand(const BitmapImage* backupBitmap,
int backupLayerId,
const int undoLayerId,
const QString& description,
Editor* editor,
QUndoCommand* parent = nullptr);
Expand All @@ -76,7 +76,7 @@ class VectorReplaceCommand : public UndoRedoCommand
{
public:
VectorReplaceCommand(const VectorImage* undoVector,
const int& undoLayerId,
const int undoLayerId,
const QString& description,
Editor* editor,
QUndoCommand* parent = nullptr);
Expand All @@ -96,14 +96,13 @@ class TransformCommand : public UndoRedoCommand

{
public:
TransformCommand(KeyFrame* undoKeyFrame,
int undoLayerId,
const QRectF& undoSelectionRect,
QPointF undoTranslation,
qreal undoRotationAngle,
qreal undoScaleX,
qreal undoScaleY,
QPointF undoTransformAnchor,
TransformCommand(const QRectF& undoSelectionRect,
const QPointF& undoTranslation,
const qreal undoRotationAngle,
const qreal undoScaleX,
const qreal undoScaleY,
const QPointF& undoTransformAnchor,
const bool roundPixels,
const QString& description,
Editor* editor,
QUndoCommand* parent = nullptr);
Expand All @@ -112,15 +111,13 @@ class TransformCommand : public UndoRedoCommand
void redo() override;

private:
void apply(const BitmapImage& bitmapImage,
const VectorImage& vectorImage,
const QRectF& selectionRect,
QPointF translation,
qreal rotationAngle,
qreal scaleX,
qreal scaleY,
QPointF selectionAnchor,
int layerId);
void apply(const QRectF& selectionRect,
const QPointF& translation,
const qreal rotationAngle,
const qreal scaleX,
const qreal scaleY,
const QPointF& selectionAnchor,
const bool roundPixels);

QRectF undoSelectionRect;
QRectF redoSelectionRect;
Expand All @@ -140,14 +137,7 @@ class TransformCommand : public UndoRedoCommand
qreal undoRotationAngle;
qreal redoRotationAngle;

BitmapImage undoBitmap;
BitmapImage redoBitmap;

VectorImage undoVector;
VectorImage redoVector;

int undoLayerId = 0;
int redoLayerId = 0;
bool roundPixels;
};

#endif // UNDOREDOCOMMAND_H
Loading

0 comments on commit bdd92fd

Please sign in to comment.