| [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 *******************/
 | 
|---|
| [446bc1] | 69 | 
 | 
|---|
 | 70 | const char ActionHistory::UndoAction::NAME[] = "undo";
 | 
|---|
 | 71 | 
 | 
|---|
| [d56640] | 72 | ActionHistory::UndoAction::UndoAction(ActionHistory *_hist) :
 | 
|---|
| [446bc1] | 73 |   Action(NAME),
 | 
|---|
| [d56640] | 74 |   hist(_hist)
 | 
|---|
 | 75 | {}
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | ActionHistory::UndoAction::~UndoAction(){}
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 | bool ActionHistory::UndoAction::canUndo(){
 | 
|---|
 | 80 |   return false;
 | 
|---|
 | 81 | }
 | 
|---|
 | 82 | 
 | 
|---|
 | 83 | bool ActionHistory::UndoAction::shouldUndo(){
 | 
|---|
 | 84 |   return false;
 | 
|---|
 | 85 | }
 | 
|---|
 | 86 | 
 | 
|---|
| [f9352d] | 87 | bool ActionHistory::UndoAction::isActive(){
 | 
|---|
 | 88 |   return hist->hasUndo();
 | 
|---|
 | 89 | }
 | 
|---|
 | 90 | 
 | 
|---|
| [047878] | 91 | Dialog* ActionHistory::UndoAction::fillDialog(Dialog *dialog){
 | 
|---|
 | 92 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
 | 93 |   return dialog;
 | 
|---|
| [80951de] | 94 | }
 | 
|---|
 | 95 | 
 | 
|---|
| [d56640] | 96 | Action::state_ptr ActionHistory::UndoAction::performCall(){
 | 
|---|
| [446bc1] | 97 |   std::cout << "Undo" << std::endl;
 | 
|---|
| [d56640] | 98 |   hist->undoLast();
 | 
|---|
 | 99 |   return Action::success;
 | 
|---|
 | 100 | }
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 | Action::state_ptr ActionHistory::UndoAction::performUndo(Action::state_ptr){
 | 
|---|
 | 103 |   ASSERT(0,"Cannot undo an undo (should use redo for this");
 | 
|---|
 | 104 |   return Action::success;
 | 
|---|
 | 105 | }
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 | Action::state_ptr ActionHistory::UndoAction::performRedo(Action::state_ptr){
 | 
|---|
 | 108 |   ASSERT(0,"Cannot redo an undo");
 | 
|---|
 | 109 |   return Action::success;
 | 
|---|
 | 110 | }
 | 
|---|
 | 111 | 
 | 
|---|
| [446bc1] | 112 | const char ActionHistory::RedoAction::NAME[] = "redo";
 | 
|---|
 | 113 | 
 | 
|---|
| [d56640] | 114 | ActionHistory::RedoAction::RedoAction(ActionHistory *_hist) :
 | 
|---|
| [446bc1] | 115 |     Action(NAME),
 | 
|---|
| [d56640] | 116 |     hist(_hist)
 | 
|---|
 | 117 | {}
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 | ActionHistory::RedoAction::~RedoAction(){}
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 | bool ActionHistory::RedoAction::canUndo(){
 | 
|---|
 | 122 |   return false;
 | 
|---|
 | 123 | }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 | bool ActionHistory::RedoAction::shouldUndo(){
 | 
|---|
 | 126 |   return false;
 | 
|---|
 | 127 | }
 | 
|---|
 | 128 | 
 | 
|---|
| [f9352d] | 129 | bool ActionHistory::RedoAction::isActive(){
 | 
|---|
| [446bc1] | 130 |   std::cout << "Redo" << std::endl;
 | 
|---|
| [f9352d] | 131 |   return hist->hasRedo();
 | 
|---|
 | 132 | }
 | 
|---|
 | 133 | 
 | 
|---|
| [047878] | 134 | Dialog* ActionHistory::RedoAction::fillDialog(Dialog *dialog){
 | 
|---|
 | 135 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
 | 136 |   return dialog;
 | 
|---|
| [80951de] | 137 | }
 | 
|---|
 | 138 | 
 | 
|---|
| [d56640] | 139 | Action::state_ptr ActionHistory::RedoAction::performCall(){
 | 
|---|
 | 140 |   hist->redoLast();
 | 
|---|
 | 141 |   return Action::success;
 | 
|---|
 | 142 | }
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 | Action::state_ptr ActionHistory::RedoAction::performUndo(Action::state_ptr){
 | 
|---|
 | 145 |   ASSERT(0,"Cannot undo a redo (should use undo for this");
 | 
|---|
 | 146 |   return Action::success;
 | 
|---|
 | 147 | }
 | 
|---|
 | 148 | 
 | 
|---|
 | 149 | Action::state_ptr ActionHistory::RedoAction::performRedo(Action::state_ptr){
 | 
|---|
 | 150 |   ASSERT(0,"Cannot redo a redo");
 | 
|---|
 | 151 |   return Action::success;
 | 
|---|
 | 152 | }
 | 
|---|