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