[d56640] | 1 | /*
|
---|
| 2 | * ActionHistory.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Mar 25, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #include "ActionHistory.hpp"
|
---|
| 9 |
|
---|
| 10 | #include <iostream>
|
---|
| 11 |
|
---|
| 12 | #include "Patterns/Singleton_impl.hpp"
|
---|
| 13 | #include "Helpers/Assert.hpp"
|
---|
| 14 |
|
---|
| 15 | using namespace std;
|
---|
| 16 |
|
---|
| 17 | ActionHistory::ActionHistory()
|
---|
| 18 | {}
|
---|
| 19 |
|
---|
| 20 | ActionHistory::~ActionHistory()
|
---|
| 21 | {}
|
---|
| 22 |
|
---|
| 23 | void ActionHistory::undoLast(){
|
---|
| 24 | ASSERT(history.size(),"Undo performed when the undo-queue was empty");
|
---|
| 25 | HistoryElement elem = history.back();
|
---|
| 26 | history.pop_back();
|
---|
| 27 | Action::state_ptr newState = elem.action->undo(elem.state);
|
---|
| 28 | yrotsih.push_back(HistoryElement(elem.action,newState));
|
---|
| 29 | }
|
---|
| 30 | void ActionHistory::redoLast(){
|
---|
| 31 | ASSERT(yrotsih.size(),"Redo performed when the redo-queue was empty");
|
---|
| 32 | HistoryElement elem = yrotsih.back();
|
---|
| 33 | yrotsih.pop_back();
|
---|
| 34 | Action::state_ptr oldState = elem.action->redo(elem.state);
|
---|
| 35 | history.push_back(HistoryElement(elem.action,oldState));
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[f9352d] | 38 | bool ActionHistory::hasUndo(){
|
---|
| 39 | return history.size()>0;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | bool ActionHistory::hasRedo(){
|
---|
| 43 | return yrotsih.size()>0;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[d56640] | 46 | void ActionHistory::addElement(Action* action,Action::state_ptr state){
|
---|
| 47 | yrotsih.clear();
|
---|
| 48 | history.push_back(HistoryElement(action,state));
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | void ActionHistory::clear(){
|
---|
| 52 | history.clear();
|
---|
| 53 | yrotsih.clear();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | void ActionHistory::init(){
|
---|
| 57 | ActionHistory *hist = new ActionHistory();
|
---|
| 58 | setInstance(hist);
|
---|
| 59 | new UndoAction(hist);
|
---|
| 60 | new RedoAction(hist);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | CONSTRUCT_SINGLETON(ActionHistory)
|
---|
| 64 |
|
---|
| 65 | /****************** Contained actions *******************/
|
---|
| 66 | ActionHistory::UndoAction::UndoAction(ActionHistory *_hist) :
|
---|
| 67 | Action("Undo"),
|
---|
| 68 | hist(_hist)
|
---|
| 69 | {}
|
---|
| 70 |
|
---|
| 71 | ActionHistory::UndoAction::~UndoAction(){}
|
---|
| 72 |
|
---|
| 73 | bool ActionHistory::UndoAction::canUndo(){
|
---|
| 74 | return false;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | bool ActionHistory::UndoAction::shouldUndo(){
|
---|
| 78 | return false;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[f9352d] | 81 | bool ActionHistory::UndoAction::isActive(){
|
---|
| 82 | return hist->hasUndo();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[d56640] | 85 | Action::state_ptr ActionHistory::UndoAction::performCall(){
|
---|
| 86 | hist->undoLast();
|
---|
| 87 | return Action::success;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | Action::state_ptr ActionHistory::UndoAction::performUndo(Action::state_ptr){
|
---|
| 91 | ASSERT(0,"Cannot undo an undo (should use redo for this");
|
---|
| 92 | return Action::success;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | Action::state_ptr ActionHistory::UndoAction::performRedo(Action::state_ptr){
|
---|
| 96 | ASSERT(0,"Cannot redo an undo");
|
---|
| 97 | return Action::success;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | ActionHistory::RedoAction::RedoAction(ActionHistory *_hist) :
|
---|
| 101 | Action("Redo"),
|
---|
| 102 | hist(_hist)
|
---|
| 103 | {}
|
---|
| 104 |
|
---|
| 105 | ActionHistory::RedoAction::~RedoAction(){}
|
---|
| 106 |
|
---|
| 107 | bool ActionHistory::RedoAction::canUndo(){
|
---|
| 108 | return false;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | bool ActionHistory::RedoAction::shouldUndo(){
|
---|
| 112 | return false;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[f9352d] | 115 | bool ActionHistory::RedoAction::isActive(){
|
---|
| 116 | return hist->hasRedo();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[d56640] | 119 | Action::state_ptr ActionHistory::RedoAction::performCall(){
|
---|
| 120 | hist->redoLast();
|
---|
| 121 | return Action::success;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | Action::state_ptr ActionHistory::RedoAction::performUndo(Action::state_ptr){
|
---|
| 125 | ASSERT(0,"Cannot undo a redo (should use undo for this");
|
---|
| 126 | return Action::success;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | Action::state_ptr ActionHistory::RedoAction::performRedo(Action::state_ptr){
|
---|
| 130 | ASSERT(0,"Cannot redo a redo");
|
---|
| 131 | return Action::success;
|
---|
| 132 | }
|
---|