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