Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/iss 748 - undo redo keyframes #1885

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/src/timelinecells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ GNU General Public License for more details.
#include "object.h"
#include "playbackmanager.h"
#include "preferencemanager.h"
#include "undoredomanager.h"
#include "timeline.h"

#include "cameracontextmenu.h"
Expand Down Expand Up @@ -1066,8 +1067,13 @@ void TimeLineCells::mouseReleaseEvent(QMouseEvent* event)
int posUnderCursor = getFrameNumber(mMousePressX);
int offset = frameNumber - posUnderCursor;

currentLayer->moveSelectedFrames(offset);
if (currentLayer->canMoveSelectedFramesToOffset(offset)) {
UndoSaveState* state = mEditor->undoRedo()->createState(UndoRedoRecordType::KEYFRAME_MOVE);
state->moveFramesState = MoveFramesSaveState(offset, currentLayer->selectedKeyFramesPositions());

currentLayer->moveSelectedFrames(offset);
mEditor->undoRedo()->record(state, tr("Move Frames"));
}
mEditor->layers()->notifyAnimationLengthChanged();
emit mEditor->framesModified();
updateContent();
Expand Down
11 changes: 10 additions & 1 deletion core_lib/src/interface/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,9 +913,15 @@ KeyFrame* Editor::addKeyFrame(const int layerNumber, int frameIndex)
const bool ok = layer->addNewKeyFrameAt(frameIndex);
Q_ASSERT(ok); // We already ensured that there is no keyframe at frameIndex, so this should always succeed
scrubTo(frameIndex); // currentFrameChanged() emit inside.

UndoSaveState* state = undoRedo()->createState(UndoRedoRecordType::KEYFRAME_ADD);
emit frameModified(frameIndex);
layers()->notifyAnimationLengthChanged();
return layer->getKeyFrameAt(frameIndex);
KeyFrame* newFrame = layer->getKeyFrameAt(frameIndex);

undoRedo()->record(state, tr("Add frame"));

return newFrame;
}

void Editor::removeKey()
Expand All @@ -935,12 +941,15 @@ void Editor::removeKey()
return;
}

UndoSaveState* state = undoRedo()->createState(UndoRedoRecordType::KEYFRAME_REMOVE);
backup(tr("Remove frame"));

deselectAll();
layer->removeKeyFrame(currentFrame());
layers()->notifyAnimationLengthChanged();
emit layers()->currentLayerChanged(layers()->currentLayerIndex()); // trigger timeline repaint.

undoRedo()->record(state, tr("Remove frame"));
}

void Editor::scrubNextKeyFrame()
Expand Down
188 changes: 184 additions & 4 deletions core_lib/src/interface/undoredocommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ GNU General Public License for more details.
#include "layersound.h"
#include "layerbitmap.h"
#include "layervector.h"
#include "layer.h"

#include "editor.h"
#include "undoredocommand.h"
Expand All @@ -38,6 +39,168 @@ UndoRedoCommand::~UndoRedoCommand()
{
}

KeyFrameRemoveCommand::KeyFrameRemoveCommand(const KeyFrame* undoKeyFrame,
int undoLayerId,
const QString &description,
Editor *editor,
QUndoCommand *parent) : UndoRedoCommand(editor, parent)
{
this->undoKeyFrame = undoKeyFrame->clone();
this->undoLayerId = undoLayerId;

this->redoLayerId = editor->layers()->currentLayer()->id();
this->redoPosition = editor->currentFrame();

setText(description);
}

KeyFrameRemoveCommand::~KeyFrameRemoveCommand()
{
delete undoKeyFrame;
}

void KeyFrameRemoveCommand::undo()
{
UndoRedoCommand::undo();

Layer* layer = editor()->layers()->findLayerById(undoLayerId);
if (layer == nullptr) {
// Until we support layer deletion recovery, we mark the command as
// obsolete as soon as it's been
return setObsolete(true);
}

layer->addKeyFrame(undoKeyFrame->pos(), undoKeyFrame->clone());

emit editor()->frameModified(undoKeyFrame->pos());
editor()->layers()->notifyAnimationLengthChanged();
editor()->scrubTo(undoKeyFrame->pos());
}

void KeyFrameRemoveCommand::redo()
{
UndoRedoCommand::redo();

if (isFirstRedo()) { setFirstRedo(false); return; }

Layer* layer = editor()->layers()->findLayerById(redoLayerId);
layer->removeKeyFrame(redoPosition);

emit editor()->frameModified(redoPosition);
editor()->layers()->notifyAnimationLengthChanged();
editor()->scrubTo(redoPosition);
}

KeyFrameAddCommand::KeyFrameAddCommand(int undoPosition,
int undoLayerId,
const QString &description,
Editor *editor,
QUndoCommand *parent)
: UndoRedoCommand(editor, parent)
{
this->undoPosition = undoPosition;
this->undoLayerId = undoLayerId;

this->redoLayerId = editor->layers()->currentLayer()->id();
this->redoPosition = editor->currentFrame();

setText(description);
}

KeyFrameAddCommand::~KeyFrameAddCommand()
{
}

void KeyFrameAddCommand::undo()
{
UndoRedoCommand::undo();

Layer* layer = editor()->layers()->findLayerById(undoLayerId);
if (!layer) {
return setObsolete(true);
}

layer->removeKeyFrame(undoPosition);

emit editor()->frameModified(undoPosition);
editor()->layers()->notifyAnimationLengthChanged();
editor()->layers()->setCurrentLayer(layer);
editor()->scrubTo(undoPosition);
}

void KeyFrameAddCommand::redo()
{
UndoRedoCommand::redo();

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

Layer* layer = editor()->layers()->findLayerById(redoLayerId);
if (!layer) {
return setObsolete(true);
}

layer->addNewKeyFrameAt(redoPosition);

emit editor()->frameModified(redoPosition);
editor()->layers()->notifyAnimationLengthChanged();
editor()->layers()->setCurrentLayer(layer);
editor()->scrubTo(redoPosition);
}

MoveKeyFramesCommand::MoveKeyFramesCommand(int offset,
QList<int> listOfPositions,
int undoLayerId,
const QString& description,
Editor* editor,
QUndoCommand *parent)
: UndoRedoCommand(editor, parent)
{
this->frameOffset = offset;
this->positions = listOfPositions;

this->undoLayerId = undoLayerId;
this->redoLayerId = editor->layers()->currentLayer()->id();

setText(description);
}

void MoveKeyFramesCommand::undo()
{
UndoRedoCommand::undo();

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

if (!undoLayer) {
return setObsolete(true);
}

for (int position : qAsConst(positions)) {
undoLayer->moveKeyFrame(position + frameOffset, -frameOffset);
}

emit editor()->framesModified();
}

void MoveKeyFramesCommand::redo()
{
UndoRedoCommand::redo();

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

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

if (!redoLayer) {
return setObsolete(true);
}

for (int position : qAsConst(positions)) {
redoLayer->moveKeyFrame(position, frameOffset);
}

emit editor()->framesModified();
}
BitmapReplaceCommand::BitmapReplaceCommand(const BitmapImage* undoBitmap,
const int undoLayerId,
const QString& description,
Expand All @@ -58,22 +221,30 @@ BitmapReplaceCommand::BitmapReplaceCommand(const BitmapImage* undoBitmap,

void BitmapReplaceCommand::undo()
{
QUndoCommand::undo();
UndoRedoCommand::undo();

Layer* layer = editor()->layers()->findLayerById(undoLayerId);
if (!layer) {
return setObsolete(true);
}

static_cast<LayerBitmap*>(layer)->replaceKeyFrame(&undoBitmap);

editor()->scrubTo(undoBitmap.pos());
}

void BitmapReplaceCommand::redo()
{
QUndoCommand::redo();
UndoRedoCommand::redo();

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

Layer* layer = editor()->layers()->findLayerById(redoLayerId);
if (!layer) {
return setObsolete(true);
}

static_cast<LayerBitmap*>(layer)->replaceKeyFrame(&redoBitmap);

editor()->scrubTo(redoBitmap.pos());
Expand All @@ -98,9 +269,12 @@ VectorReplaceCommand::VectorReplaceCommand(const VectorImage* undoVector,

void VectorReplaceCommand::undo()
{
QUndoCommand::undo();
UndoRedoCommand::undo();

Layer* layer = editor()->layers()->findLayerById(undoLayerId);
if (!layer) {
return setObsolete(true);
}

static_cast<LayerVector*>(layer)->replaceKeyFrame(&undoVector);

Expand All @@ -109,12 +283,15 @@ void VectorReplaceCommand::undo()

void VectorReplaceCommand::redo()
{
QUndoCommand::redo();
UndoRedoCommand::redo();

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

Layer* layer = editor()->layers()->findLayerById(redoLayerId);
if (!layer) {
return setObsolete(true);
}

static_cast<LayerVector*>(layer)->replaceKeyFrame(&redoVector);

Expand Down Expand Up @@ -154,6 +331,7 @@ TransformCommand::TransformCommand(const QRectF& undoSelectionRect,

void TransformCommand::undo()
{
UndoRedoCommand::undo();
apply(undoSelectionRect,
undoTranslation,
undoRotationAngle,
Expand All @@ -165,6 +343,8 @@ void TransformCommand::undo()

void TransformCommand::redo()
{
UndoRedoCommand::redo();

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

Expand Down
Loading