Skip to content

Instantly share code, notes, and snippets.

@lebek
Last active February 2, 2017 10:40
Show Gist options
  • Save lebek/cf1b95948e5cbd25de3194b51020b9e9 to your computer and use it in GitHub Desktop.
Save lebek/cf1b95948e5cbd25de3194b51020b9e9 to your computer and use it in GitHub Desktop.
class UMoveObjectCommand: public USketchCommand {
private:
FString SketchObjectGuid;
/* New transform in sketch-space */
FTransform NewTransform;
public:
UMoveObjectCommand() {};
void PostInitialize(FString SketchObjectGuid_, FTransform NewTransform_) {
SketchObjectGuid = SketchObjectGuid_;
NewTransform = NewTransform_;
};
USketchCommand * Execute(ASketchActor * SketchActor) {
/* Effects */
ASketchObjectActor * SketchObject = SketchActor->SketchObjects[SketchObjectGuid];
FTransform OldTransform = SketchObject->GetTransform();
SketchObject->SetActorRelativeTransform(NewTransform);
/* Create undo command */
UMoveObjectCommand * Command = NewObject <UMoveObjectCommand> ();
Command->PostInitialize(SketchObjectGuid, OldTransform);
return Command;
};
};
class UCommandRunner: public UObject {
private:
TArray<USketchCommand *> UndoCommands;
TArray<USketchCommand *> RedoCommands;
int32 MaxUndo = 100;
public:
UCommandRunner() {};
void Undo(ASketchActor * SketchActor) {
if (UndoCommands.Num() == 0) return;
USketchCommand * UndoCommand = UndoCommands.Pop();
USketchCommand * RedoCommand = UndoCommand->Execute(SketchActor);
if (RedoCommand != nullptr) RedoCommands.Add(RedoCommand);
};
void Redo(ASketchActor * SketchActor) {
if (RedoCommands.Num() == 0) return;
USketchCommand * RedoCommand = RedoCommands.Pop();
USketchCommand * UndoCommand = RedoCommand->Execute(SketchActor);
if (UndoCommand != nullptr) UndoCommands.Add(UndoCommand);
};
void Execute(ASketchActor * SketchActor, USketchCommand * Command, bool bUndoable = true) {
USketchCommand * UndoCommand = Command->Execute(SketchActor);
if (UndoCommand != nullptr && bUndoable) {
UndoCommands.Add(UndoCommand);
if (UndoCommands.Num() > MaxUndo) UndoCommands.RemoveAt(0);
}
RedoCommands.Empty();
};
void ClearUndoRedo() {
UndoCommands.Empty();
RedoCommands.Empty();
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment