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