| [a5041ec] | 1 | /* | 
|---|
|  | 2 | * ActionSequenze.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Dec 17, 2009 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [112b09] | 8 | #include "Helpers/MemDebug.hpp" | 
|---|
|  | 9 |  | 
|---|
| [a5041ec] | 10 | #include "Actions/ActionSequence.hpp" | 
|---|
|  | 11 | #include "Actions/Action.hpp" | 
|---|
|  | 12 |  | 
|---|
| [67e2b3] | 13 | #include "Helpers/Assert.hpp" | 
|---|
|  | 14 |  | 
|---|
| [a5041ec] | 15 | using namespace std; | 
|---|
|  | 16 |  | 
|---|
|  | 17 | ActionSequence::ActionSequence() | 
|---|
| [1fa107] | 18 | {} | 
|---|
| [a5041ec] | 19 |  | 
|---|
|  | 20 | ActionSequence::~ActionSequence() | 
|---|
| [1fa107] | 21 | {} | 
|---|
| [a5041ec] | 22 |  | 
|---|
|  | 23 |  | 
|---|
|  | 24 | void ActionSequence::addAction(Action* _action){ | 
|---|
|  | 25 | actions.push_back(_action); | 
|---|
|  | 26 | } | 
|---|
|  | 27 |  | 
|---|
|  | 28 | Action* ActionSequence::removeLastAction(){ | 
|---|
| [1fa107] | 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 | } | 
|---|
| [a5041ec] | 38 | } | 
|---|
|  | 39 |  | 
|---|
| [2efa90] | 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){ | 
|---|
| [67e2b3] | 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 | 
|---|
| [5b0b98] | 58 | Action::state_ptr state = (*it)->performCall(); | 
|---|
| [67e2b3] | 59 | states.push_back(state); | 
|---|
|  | 60 | } | 
|---|
|  | 61 | return states; | 
|---|
|  | 62 | } | 
|---|
|  | 63 |  | 
|---|
| [5b0b98] | 64 | ActionSequence::stateSet ActionSequence::undoAll(stateSet states){ | 
|---|
| [67e2b3] | 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()){ | 
|---|
| [5b0b98] | 72 | Action::state_ptr newState = (*actionRit)->performUndo(*stateRit); | 
|---|
| [67e2b3] | 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; | 
|---|
| [a5041ec] | 82 | } | 
|---|
|  | 83 |  | 
|---|
| [5b0b98] | 84 | ActionSequence::stateSet ActionSequence::redoAll(stateSet states){ | 
|---|
| [67e2b3] | 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()){ | 
|---|
| [5b0b98] | 91 | Action::state_ptr newState =(*actionIt)->performRedo(*stateIt); | 
|---|
| [67e2b3] | 92 | res.push_back(newState); | 
|---|
|  | 93 | } | 
|---|
|  | 94 | else{ | 
|---|
|  | 95 | res.push_back(Action::success); | 
|---|
|  | 96 | } | 
|---|
|  | 97 | } | 
|---|
|  | 98 | return res; | 
|---|
| [a5041ec] | 99 | } | 
|---|
|  | 100 |  | 
|---|
|  | 101 | bool ActionSequence::canUndo(){ | 
|---|
|  | 102 | bool canUndo=true; | 
|---|
| [67e2b3] | 103 | for(deque<Action*>::iterator it=actions.begin(); it!=actions.end(); ++it){ | 
|---|
|  | 104 | if((*it)->shouldUndo()){ | 
|---|
|  | 105 | canUndo &= (*it)->canUndo(); | 
|---|
|  | 106 | } | 
|---|
|  | 107 | } | 
|---|
| [a5041ec] | 108 | return canUndo; | 
|---|
|  | 109 | } | 
|---|
| [67e2b3] | 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 | } | 
|---|