1 /*! 2 @file 3 @author Albert Semenov 4 @date 07/2012 5 */ 6 7 #include "ActionManager.h" 8 #include "pugixml.hpp" 9 #include "MyGUI_DataManager.h" 10 #include "DataInfoManager.h" 11 12 namespace tools 13 { 14 ActionManager* ActionManager::mInstance = nullptr; 15 ActionManager()16 ActionManager::ActionManager() : 17 mMaxActions(5) 18 { 19 mInstance = this; 20 } 21 ~ActionManager()22 ActionManager::~ActionManager() 23 { 24 mInstance = nullptr; 25 } 26 getInstance()27 ActionManager& ActionManager::getInstance() 28 { 29 return *mInstance; 30 } 31 getInstancePtr()32 ActionManager* ActionManager::getInstancePtr() 33 { 34 return mInstance; 35 } 36 initialise()37 void ActionManager::initialise() 38 { 39 reset(); 40 } 41 shutdown()42 void ActionManager::shutdown() 43 { 44 clear(); 45 } 46 clear()47 void ActionManager::clear() 48 { 49 for (ListAction::iterator command = mActions.begin(); command != mActions.end(); command ++) 50 delete *command; 51 mActions.clear(); 52 53 mCurrentAction = mActions.end(); 54 mActionAsSave = mActions.end(); 55 } 56 doAction(Action * _command)57 void ActionManager::doAction(Action* _command) 58 { 59 removeRedo(); 60 61 mActions.push_back(_command); 62 mCurrentAction ++; 63 64 updateMaxActions(); 65 66 _command->doAction(); 67 68 onChangeActions(); 69 } 70 undoAction()71 void ActionManager::undoAction() 72 { 73 if (mCurrentAction == mActions.begin()) 74 return; 75 76 Action* command = (*mCurrentAction); 77 mCurrentAction --; 78 79 command->undoAction(); 80 onChangeActions(); 81 } 82 redoAction()83 void ActionManager::redoAction() 84 { 85 ListAction::iterator next = mCurrentAction; 86 next ++; 87 88 if (next == mActions.end()) 89 return; 90 91 mCurrentAction = next; 92 Action* command = *mCurrentAction; 93 94 command->doAction(); 95 onChangeActions(); 96 } 97 resetChanges()98 void ActionManager::resetChanges() 99 { 100 mActionAsSave = mCurrentAction; 101 102 onChangeActions(); 103 } 104 getChanges()105 bool ActionManager::getChanges() 106 { 107 return mCurrentAction != mActionAsSave; 108 } 109 setMaxActions(size_t _value)110 void ActionManager::setMaxActions(size_t _value) 111 { 112 MYGUI_ASSERT(_value > 0, "Max commands wrong"); 113 mMaxActions = _value; 114 115 bool change = updateMaxActions(); 116 if (change) 117 onChangeActions(); 118 } 119 getMaxActions() const120 size_t ActionManager::getMaxActions() const 121 { 122 return mMaxActions; 123 } 124 updateMaxActions()125 bool ActionManager::updateMaxActions() 126 { 127 bool change = false; 128 129 if (mActions.size() < 2) 130 return change; 131 132 while (mActions.size() > (mMaxActions + 1)) 133 { 134 ListAction::iterator second = mActions.begin(); 135 second ++; 136 137 if (second == mActionAsSave || mActionAsSave == mActions.begin()) 138 { 139 mActionAsSave = mActions.end(); 140 change = true; 141 } 142 143 Action* command = *second; 144 mActions.erase(second); 145 delete command; 146 } 147 148 return change; 149 } 150 removeRedo()151 void ActionManager::removeRedo() 152 { 153 ListAction::iterator last = mActions.end(); 154 last --; 155 156 while (mCurrentAction != last) 157 { 158 Action* command = *last; 159 160 if (last == mActionAsSave) 161 mActionAsSave = mActions.end(); 162 163 last--; 164 mActions.pop_back(); 165 166 delete command; 167 } 168 } 169 onChangeActions()170 void ActionManager::onChangeActions() 171 { 172 eventChanges(); 173 } 174 reset()175 void ActionManager::reset() 176 { 177 clear(); 178 179 Action* command = new Action(); 180 mActions.push_back(command); 181 182 mCurrentAction = mActions.begin(); 183 mActionAsSave = mActions.begin(); 184 } 185 } 186