#include "StdAfx.h" #include "CommandMgr.h" #include "LogMgr.h" #include "GlobalFunction.h" #define MAX_UNDO_CNT 100//最大撤销次数 CCommandMgr gCommandMgr; CCommandMgr::CCommandMgr(void) { } CCommandMgr::~CCommandMgr(void) { m_UndoQueue.clear(); m_RedoQueue.clear(); } //p 是指令特定的操作对象 void CCommandMgr::AddUndoCommand(CCommandBase *p) { vector> Vec; AddUndoCommand(p,Vec); } void CCommandMgr::AddUndoCommand(CCommandBase *p,vector> &pMarkObjectVec) { if(p->HasOpObj()==false) { //加入指定的 if(pMarkObjectVec.empty()==false) { vector>::iterator iter = pMarkObjectVec.begin(); vector>::iterator iter_end = pMarkObjectVec.end(); for(;iter!=iter_end;iter++) { p->AddOpObj((*iter)); } } else//没有指定就加入当前选择的obj { gLayer.GetObjContainer().AddObjToCmd(p,true); } } if(p->HasOpObj()==false)//没有操作对象时不要添加指令 { delete p; return; } gCommandMgr.AddUndoCommandExt(p); } //添加指令 void CCommandMgr::AddUndoCommandExt(CCommandBase *p) { //------------------------------------日志start CLogInfo LogInfo; LogInfo.m_ClassName = "CCommandMgr"; LogInfo.m_FuncName = "AddUndoCommandExt"; LogInfo.m_LogMsg = p->GetStr(); gLogMgr->WriteLogInfo(LogInfo); //------------------------------------日志end //保存到智能指针 Sptr sPtr(p); m_UndoQueue.push_back(sPtr); if(m_UndoQueue.size()>MAX_UNDO_CNT) { m_UndoQueue.pop_front(); } //之前的redo 全部清除 m_RedoQueue.clear(); } bool CCommandMgr::HasUndoCmd() { return !m_UndoQueue.empty(); } bool CCommandMgr::HasRedoCmd() { return !m_RedoQueue.empty(); } void CCommandMgr::Undo() { if(!m_UndoQueue.empty()) { Sptr p = m_UndoQueue[m_UndoQueue.size()-1]; p->Undo(); m_RedoQueue.push_back(p); m_UndoQueue.pop_back(); //------------------------------------日志start CLogInfo LogInfo; LogInfo.m_ClassName = "CCommandMgr"; LogInfo.m_FuncName = "Undo"; LogInfo.m_LogMsg = p->GetStr(); gLogMgr->WriteLogInfo(LogInfo); //------------------------------------日志end } } void CCommandMgr::Redo() { if(!m_RedoQueue.empty()) { Sptr p = m_RedoQueue[m_RedoQueue.size()-1]; p->Excute(); m_UndoQueue.push_back(p); m_RedoQueue.pop_back(); //------------------------------------日志start CLogInfo LogInfo; LogInfo.m_ClassName = "CCommandMgr"; LogInfo.m_FuncName = "Redo"; LogInfo.m_LogMsg = p->GetStr(); gLogMgr->WriteLogInfo(LogInfo); //------------------------------------日志end } } //重置回撤指令状态 void CCommandMgr::Reset() { m_UndoQueue.clear(); m_RedoQueue.clear(); }