| 1 | /* | 
|---|
| 2 | * ActionSequenze.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Dec 17, 2009 | 
|---|
| 5 | *      Author: crueger | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "Helpers/MemDebug.hpp" | 
|---|
| 9 |  | 
|---|
| 10 | #include "Actions/ActionSequence.hpp" | 
|---|
| 11 | #include "Actions/Action.hpp" | 
|---|
| 12 |  | 
|---|
| 13 | #include "Helpers/Assert.hpp" | 
|---|
| 14 |  | 
|---|
| 15 | using namespace std; | 
|---|
| 16 |  | 
|---|
| 17 | ActionSequence::ActionSequence() | 
|---|
| 18 | {} | 
|---|
| 19 |  | 
|---|
| 20 | ActionSequence::~ActionSequence() | 
|---|
| 21 | {} | 
|---|
| 22 |  | 
|---|
| 23 |  | 
|---|
| 24 | void ActionSequence::addAction(Action* _action){ | 
|---|
| 25 | actions.push_back(_action); | 
|---|
| 26 | } | 
|---|
| 27 |  | 
|---|
| 28 | Action* ActionSequence::removeLastAction(){ | 
|---|
| 29 | if(actions.empty()) { | 
|---|
| 30 | return 0; | 
|---|
| 31 | } | 
|---|
| 32 | else { | 
|---|
| 33 | Action* theAction; | 
|---|
| 34 | theAction = actions.back(); | 
|---|
| 35 | actions.pop_back(); | 
|---|
| 36 | return theAction; | 
|---|
| 37 | } | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | // this method is used outside the ActionModule | 
|---|
| 41 | // Each action registers itself with the history | 
|---|
| 42 | void ActionSequence::callAll(){ | 
|---|
| 43 | for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){ | 
|---|
| 44 | // we want to have a global bookkeeping for all actions in the sequence, so | 
|---|
| 45 | // we bypass the normal call | 
|---|
| 46 | (*it)->call(); | 
|---|
| 47 | } | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | // This method is used internally when MakroActions are constructed. | 
|---|
| 51 | // In this case only the makro Action should be registered and | 
|---|
| 52 | // handle the states | 
|---|
| 53 | ActionSequence::stateSet ActionSequence::callAll(bool){ | 
|---|
| 54 | stateSet states; | 
|---|
| 55 | for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){ | 
|---|
| 56 | // we want to have a global bookkeeping for all actions in the sequence, so | 
|---|
| 57 | // we bypass the normal call | 
|---|
| 58 | Action::state_ptr state = (*it)->performCall(); | 
|---|
| 59 | states.push_back(state); | 
|---|
| 60 | } | 
|---|
| 61 | return states; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | ActionSequence::stateSet ActionSequence::undoAll(stateSet states){ | 
|---|
| 65 | ASSERT(canUndo(),"Trying to undo a sequence that contains methods that can't be undone"); | 
|---|
| 66 | stateSet res; | 
|---|
| 67 | actionSet::reverse_iterator actionRit = actions.rbegin(); | 
|---|
| 68 | stateSet::reverse_iterator stateRit = states.rbegin(); | 
|---|
| 69 | for(;actionRit!=actions.rend();++actionRit,++stateRit){ | 
|---|
| 70 | ASSERT(stateRit!=states.rend(),"End of states prematurely reached."); | 
|---|
| 71 | if((*actionRit)->shouldUndo()){ | 
|---|
| 72 | Action::state_ptr newState = (*actionRit)->performUndo(*stateRit); | 
|---|
| 73 | // The order of the states has to correspond to the order of the actions | 
|---|
| 74 | // this is why we have to add at the beginning | 
|---|
| 75 | res.push_front(newState); | 
|---|
| 76 | } | 
|---|
| 77 | else{ | 
|---|
| 78 | res.push_front(Action::success); | 
|---|
| 79 | } | 
|---|
| 80 | } | 
|---|
| 81 | return res; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | ActionSequence::stateSet ActionSequence::redoAll(stateSet states){ | 
|---|
| 85 | stateSet res; | 
|---|
| 86 | actionSet::iterator actionIt = actions.begin(); | 
|---|
| 87 | stateSet::iterator stateIt = states.begin(); | 
|---|
| 88 | for(;actionIt!=actions.end();++actionIt,++stateIt){ | 
|---|
| 89 | ASSERT(stateIt!=states.end(),"End of states prematurely reached."); | 
|---|
| 90 | if((*actionIt)->shouldUndo()){ | 
|---|
| 91 | Action::state_ptr newState =(*actionIt)->performRedo(*stateIt); | 
|---|
| 92 | res.push_back(newState); | 
|---|
| 93 | } | 
|---|
| 94 | else{ | 
|---|
| 95 | res.push_back(Action::success); | 
|---|
| 96 | } | 
|---|
| 97 | } | 
|---|
| 98 | return res; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | bool ActionSequence::canUndo(){ | 
|---|
| 102 | bool canUndo=true; | 
|---|
| 103 | for(deque<Action*>::iterator it=actions.begin(); it!=actions.end(); ++it){ | 
|---|
| 104 | if((*it)->shouldUndo()){ | 
|---|
| 105 | canUndo &= (*it)->canUndo(); | 
|---|
| 106 | } | 
|---|
| 107 | } | 
|---|
| 108 | return canUndo; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | bool ActionSequence::shouldUndo(){ | 
|---|
| 112 | bool shouldUndo = false; | 
|---|
| 113 | for(deque<Action*>::iterator it=actions.begin();it!=actions.end();++it){ | 
|---|
| 114 | shouldUndo |= (*it)->shouldUndo(); | 
|---|
| 115 | } | 
|---|
| 116 | return shouldUndo; | 
|---|
| 117 | } | 
|---|