MyCommand.cpp
上传用户:netltd
上传日期:2013-02-12
资源大小:7234k
文件大小:2k
- #include "stdafx.h"
- #include "MyDefine.h"
- #include "MyCommand.h"
- //////////////////////////////////////////////////////////////////////////////////////////////
- CCommandStruct::CCommandStruct(int command, int num, int* object, int* index)
- {
- ASSERT(num > 0);
- int i;
- m_command = command;
- m_num = num;
- m_object = new int[m_num];
- m_index = new int[m_num];
-
- for(i=0; i<m_num; i++)
- {
- m_object[i] = object[i];
- m_index[i] = index[i];
- }
- }
- CCommandStruct::~CCommandStruct()
- {
- delete m_object;
- delete m_index;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- IMPLEMENT_DYNCREATE(CCommandStack, CObject)
- CCommandStack::CCommandStack()
- {
- CommandPointer = -1; //命令指针
-
- }
- CCommandStack::~CCommandStack()
- {
- int index = m_CommandArray.GetSize();
- while(index--)
- delete (CCommandStruct*)m_CommandArray.GetAt(index);
- m_CommandArray.RemoveAll();
- }
- void CCommandStack::PushCommand(int command, int num, int* object, int* index)
- {
- ASSERT(num >= 0);
- CCommandStruct* pCommand;
-
- DelCommandFrom(CommandPointer + 1);
- pCommand = new CCommandStruct(command, num, object, index);
- m_CommandArray.Add(pCommand);
- CommandPointer++;
- }
- void CCommandStack::RemoveAll()
- {
- for(int index = 0; index < m_CommandArray.GetSize(); index++)
- delete (CCommandStack*)m_CommandArray.GetAt(index);
- m_CommandArray.RemoveAll();
- }
- CCommandStruct* CCommandStack::Undo()
- {
- ASSERT(CommandPointer > -1 && CommandPointer < m_CommandArray.GetSize());
- if(CommandPointer == -1)
- return NULL;
- CCommandStruct* pCommand;
- pCommand = (CCommandStruct*)m_CommandArray.GetAt(CommandPointer);
- CommandPointer--;
- return pCommand;
- }
- CCommandStruct* CCommandStack::Redo()
- {
- if(CommandPointer >= m_CommandArray.GetSize())
- return NULL;
- CCommandStruct* pCommand;
- CommandPointer++;
- pCommand = (CCommandStruct*)m_CommandArray.GetAt(CommandPointer);
- return pCommand;
- }
- void CCommandStack::DelCommandFrom(int index)
- {
- ASSERT(index >= 0);
- while(index < m_CommandArray.GetSize())
- {
- delete (CCommandStruct*)m_CommandArray.GetAt(index);
- m_CommandArray.RemoveAt(index);
- }
- }
- BOOL CCommandStack::CanUndo()
- {
- if(m_CommandArray.GetSize() > 0 && CommandPointer > -1)
- return TRUE;
- else
- return FALSE;
- }
- BOOL CCommandStack::CanRedo()
- {
- if(m_CommandArray.GetSize() > 0 && CommandPointer < m_CommandArray.GetUpperBound())
- return TRUE;
- else
- return FALSE;
- }