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