Skip to content

Commit

Permalink
Undo/Redo Part 2 (idk why i forgot to add those)
Browse files Browse the repository at this point in the history
  • Loading branch information
Panakotta00 committed Feb 1, 2022
1 parent 7912f0e commit 6a208e8
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Source/FicsItCam/UI/FICChangeList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "FICChangeList.h"

#include "FICEditorContext.h"

TArray<FChangeStackEntry> FFICChange::ChangeStack = TArray<FChangeStackEntry>();

FFICChange_ActiveFrame::FFICChange_ActiveFrame(UFICEditorContext* InEditorContext, int64 InFromFrame, int64 InToFrame) : EditorContext(InEditorContext), FromFrame(InFromFrame), ToFrame(InToFrame) {
if (ToFrame == TNumericLimits<int64>::Min()) {
ToFrame = EditorContext->GetCurrentFrame();
}
if (FromFrame == TNumericLimits<int64>::Min()) {
TFunction<bool(TSharedPtr<FFICChange>)> DoChange;
DoChange = [this, &DoChange](TSharedPtr<FFICChange> InChange) {
if (InChange) {
if (InChange->ChangeType() == "Group") {
for (TSharedPtr<FFICChange> Change : StaticCastSharedPtr<FFICChange_Group>(InChange)->Changes) {
if (DoChange(Change)) return true;
}
} else if (InChange->ChangeType() == "ActiveFrame") {
FromFrame = StaticCastSharedPtr<FFICChange_ActiveFrame>(InChange)->ToFrame;
return true;
}
}
return false;
};
if (!DoChange(EditorContext->ChangeList.PeakChange())) {
FromFrame = EditorContext->GetAnimation()->AnimationStart;
}
}
}

void FFICChange_ActiveFrame::RedoChange() {
EditorContext->SetCurrentFrame(ToFrame);
}

void FFICChange_ActiveFrame::UndoChange() {
EditorContext->SetCurrentFrame(FromFrame);
}

void FFICChangeList::PushChange(TSharedPtr<FFICChange> InChange) {
if (Changes.Num() > ChangeIndex+1) Changes.RemoveAt(ChangeIndex+1, Changes.Num() - ChangeIndex - 1);
Changes.Push(InChange);

if (Changes.Num() > MaxChanges) {
Changes.RemoveAt(0, Changes.Num() - MaxChanges);
}

ChangeIndex = Changes.Num() - 1;
}

TSharedPtr<FFICChange> FFICChangeList::PushChange() {
if (ChangeIndex >= Changes.Num()-1) return nullptr;
return Changes[++ChangeIndex];
}

TSharedPtr<FFICChange> FFICChangeList::PopChange() {
if (ChangeIndex < 0) return nullptr;
return Changes[ChangeIndex--];
}

TSharedPtr<FFICChange> FFICChangeList::PeakChange() {
if (ChangeIndex < 0) return nullptr;
return Changes[ChangeIndex];
}
102 changes: 102 additions & 0 deletions Source/FicsItCam/UI/FICChangeList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#pragma once

#include "FICEditorContext.h"
#include "../FICAnimation.h"
#include "../FICEditorAttributeBase.h"

class FFICChangeList;

typedef TPair<FFICAttribute*, TSharedPtr<FFICAttribute>> FChangeStackEntry;

#define BEGIN_ATTRIB_CHANGE(Attribute) \
FFICChange::ChangeStack.Push(FChangeStackEntry(Attribute, Attribute->Get()));

#define END_ATTRIB_CHANGE(ChangeList) { \
FChangeStackEntry StackEntry = FFICChange::ChangeStack.Pop(); \
ChangeList->PushChange(MakeShared<FFICChange_Attribute>(StackEntry.Key, StackEntry.Value)); \
}

#define BEGIN_QUICK_ATTRIB_CHANGE(Context, Attribute, FromFrame, ToFrame) \
auto _Change = MakeShared<FFICChange_Group>(); \
_Change->PushChange(MakeShared<FFICChange_ActiveFrame>(Context, FromFrame, ToFrame)); \
BEGIN_ATTRIB_CHANGE(Attribute)

#define END_QUICK_ATTRIB_CHANGE(ChangeList) \
END_ATTRIB_CHANGE(_Change) \
ChangeList.PushChange(_Change);

struct FFICChange {
static TArray<FChangeStackEntry> ChangeStack;

virtual ~FFICChange() = default;

virtual void RedoChange() = 0;
virtual void UndoChange() = 0;
virtual FName ChangeType() = 0;
};

struct FFICChange_ActiveFrame : public FFICChange {
class UFICEditorContext* EditorContext;
int64 FromFrame;
int64 ToFrame;

FFICChange_ActiveFrame(class UFICEditorContext* InEditorContext, int64 FromFrame = TNumericLimits<int64>::Min(), int64 ToFrame = TNumericLimits<int64>::Min());

virtual void RedoChange() override;
virtual void UndoChange() override;
virtual FName ChangeType() override { return FName(TEXT("ActiveFrame")); }
};

struct FFICChange_Attribute : public FFICChange {
FFICAttribute* Attribute;
TSharedPtr<FFICAttribute> FromAttribute;
TSharedPtr<FFICAttribute> ToAttribute;

FFICChange_Attribute(FFICAttribute* InAttribute, TSharedPtr<FFICAttribute> InFromAttribute) : Attribute(InAttribute), FromAttribute(InFromAttribute), ToAttribute(Attribute->Get()) {}

virtual void RedoChange() override {
Attribute->Set(ToAttribute);
}

virtual void UndoChange() override {
Attribute->Set(FromAttribute);
}

virtual FName ChangeType() override {
return FName(TEXT("Attribute"));
}
};

struct FFICChange_Group : public FFICChange {
TSet<TSharedPtr<FFICChange>> Changes;

virtual void RedoChange() override {
for (TSharedPtr<FFICChange> Change : Changes) Change->RedoChange();
}

virtual void UndoChange() override {
for (TSharedPtr<FFICChange> Change : Changes) Change->UndoChange();
}

virtual FName ChangeType() override {
return FName(TEXT("Group"));
}

void PushChange(TSharedPtr<FFICChange> InChange) {
Changes.Add(InChange);
}
};

class FFICChangeList {
private:
TArray<TSharedPtr<FFICChange>> Changes;
int ChangeIndex = -1;

int MaxChanges = 50;

public:
void PushChange(TSharedPtr<FFICChange> InChange);
TSharedPtr<FFICChange> PushChange();
TSharedPtr<FFICChange> PopChange();
TSharedPtr<FFICChange> PeakChange();
};

0 comments on commit 6a208e8

Please sign in to comment.