Files
Cyd 2b8ca921cb Initial full mirror of c:\VWE (source + assets + toolchain + outputs) via Git LFS
Complete disaster-recovery snapshot: engine/game source, game data assets,
VC6 toolchain + DX SDKs, build outputs, deployed game, and _UNUSED archive.
Large binaries in Git LFS; text preserved byte-for-byte (core.autocrlf=false,
no eol attributes). See RECOVERY.md for the one-clone rebuild procedure.
2026-06-24 21:28:16 -05:00

171 lines
3.8 KiB
C++

// UndoManager.cpp: implementation of the UndoManager class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "mw4gameed2.h"
#include "UndoManager.h"
#include "ObjectManager.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Undo Command
void UndoCommand::Attach(UndoCommand *cmd)
{
if(Next)
Next->Attach(cmd);
else
Next=cmd;
}
void UndoCommand::ExecuteUndo(UndoCommand **revcom)
{
if(Next) Next->ExecuteUndo(revcom);
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Rename
void RenameCommand::ExecuteUndo(UndoCommand **revcom)
{
UndoCommand::ExecuteUndo(revcom);
UndoCommand *com=new RenameCommand(Object);
Object->SetName(OldName);
if(*revcom==NULL) *revcom=com; else (*revcom)->Attach(com);
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Move
void MoveCommand::ExecuteUndo(UndoCommand **revcom)
{
UndoCommand::ExecuteUndo(revcom);
UndoCommand *com=new MoveCommand(Object);
Object->SetPos(OldPos);
if(*revcom==NULL) *revcom=com; else (*revcom)->Attach(com);
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Rotate
void RotateCommand::ExecuteUndo(UndoCommand **revcom)
{
UndoCommand::ExecuteUndo(revcom);
UndoCommand *com=new RotateCommand(Object);
Object->SetRot(OldRot);
if(*revcom==NULL) *revcom=com; else (*revcom)->Attach(com);
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> AddCommand
void AddCommand::ExecuteUndo(UndoCommand **revcom)
{
UndoCommand::ExecuteUndo(revcom);
UndoCommand *com=new DeleteCommand(Object);
Parent->RemoveObject(Object);
if(*revcom==NULL) *revcom=com; else (*revcom)->Attach(com);
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DeleteCommand
void DeleteCommand::ExecuteUndo(UndoCommand **revcom)
{
UndoCommand::ExecuteUndo(revcom);
Parent->InsertObjectAt(Location,Object);
CString oldname=Object->GetName();
oldname=oldname.Left(oldname.GetLength()-strlen(OFFMAPTAG));
Object->SetName(oldname);
UndoCommand *com=new AddCommand(Object);
if(*revcom==NULL) *revcom=com; else (*revcom)->Attach(com);
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GroupCommand
void GroupCommand::ExecuteUndo(UndoCommand **revcom)
{
UndoCommand::ExecuteUndo(revcom);
UndoCommand *com=new GroupCommand(Object);
Object->GetParent()->RemoveObject(Object);
Parent->AddObject(Object);
if(*revcom==NULL) *revcom=com; else (*revcom)->Attach(com);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Command Stack
CommandStack::CommandStack()
{
CommandCount=0;
CommandMax=COMMAND_MAX;
}
void CommandStack::RemoveBottomCommand()
{
Verify(CommandCount>=1);
delete Commands[0];
for(int i=1;i<CommandCount;i++) Commands[i-1]=Commands[i];
CommandCount--;
}
void CommandStack::PushCommand(UndoCommand *cmd)
{
Verify(cmd!=NULL);
if(CommandCount>=(CommandMax-1))
RemoveBottomCommand();
Commands[CommandCount++]=cmd;
}
UndoCommand *CommandStack::Pop()
{
if(CommandCount>0)
{
CommandCount--;
return Commands[CommandCount];
}
return NULL;
}
UndoCommand *CommandStack::PopAndExecute()
{
if(CommandCount>0)
{
CommandCount--;
UndoCommand *revcom=NULL;
Commands[CommandCount]->ExecuteUndo(&revcom);
delete Commands[CommandCount];
return revcom;
}
return NULL;
}
void CommandStack::Clear()
{
while(CommandCount)
{
UndoCommand *com=Pop();
com->Discard();
delete com;
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Undo Manger
UndoManager::~UndoManager()
{
}
void UndoManager::Undo()
{
RedoStack.PushCommand(UndoStack.PopAndExecute());
}
void UndoManager::Redo()
{
UndoStack.PushCommand(RedoStack.PopAndExecute());
}
void UndoManager::AddCommand(UndoCommand *cmd)
{
RedoStack.Clear();
UndoStack.PushCommand(cmd);
}