// 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=(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); }