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 |
|
---|
38 | void ActionHistory::addElement(Action* action,Action::state_ptr state){
|
---|
39 | cout << "Adding action to end of history" << endl;
|
---|
40 | yrotsih.clear();
|
---|
41 | history.push_back(HistoryElement(action,state));
|
---|
42 | }
|
---|
43 |
|
---|
44 | void ActionHistory::clear(){
|
---|
45 | cout << "History cleared" << endl;
|
---|
46 | history.clear();
|
---|
47 | yrotsih.clear();
|
---|
48 | }
|
---|
49 |
|
---|
50 | void ActionHistory::init(){
|
---|
51 | ActionHistory *hist = new ActionHistory();
|
---|
52 | setInstance(hist);
|
---|
53 | new UndoAction(hist);
|
---|
54 | new RedoAction(hist);
|
---|
55 | }
|
---|
56 |
|
---|
57 | CONSTRUCT_SINGLETON(ActionHistory)
|
---|
58 |
|
---|
59 | /****************** Contained actions *******************/
|
---|
60 | ActionHistory::UndoAction::UndoAction(ActionHistory *_hist) :
|
---|
61 | Action("Undo"),
|
---|
62 | hist(_hist)
|
---|
63 | {}
|
---|
64 |
|
---|
65 | ActionHistory::UndoAction::~UndoAction(){}
|
---|
66 |
|
---|
67 | bool ActionHistory::UndoAction::canUndo(){
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 |
|
---|
71 | bool ActionHistory::UndoAction::shouldUndo(){
|
---|
72 | return false;
|
---|
73 | }
|
---|
74 |
|
---|
75 | Action::state_ptr ActionHistory::UndoAction::performCall(){
|
---|
76 | hist->undoLast();
|
---|
77 | return Action::success;
|
---|
78 | }
|
---|
79 |
|
---|
80 | Action::state_ptr ActionHistory::UndoAction::performUndo(Action::state_ptr){
|
---|
81 | ASSERT(0,"Cannot undo an undo (should use redo for this");
|
---|
82 | return Action::success;
|
---|
83 | }
|
---|
84 |
|
---|
85 | Action::state_ptr ActionHistory::UndoAction::performRedo(Action::state_ptr){
|
---|
86 | ASSERT(0,"Cannot redo an undo");
|
---|
87 | return Action::success;
|
---|
88 | }
|
---|
89 |
|
---|
90 | ActionHistory::RedoAction::RedoAction(ActionHistory *_hist) :
|
---|
91 | Action("Redo"),
|
---|
92 | hist(_hist)
|
---|
93 | {}
|
---|
94 |
|
---|
95 | ActionHistory::RedoAction::~RedoAction(){}
|
---|
96 |
|
---|
97 | bool ActionHistory::RedoAction::canUndo(){
|
---|
98 | return false;
|
---|
99 | }
|
---|
100 |
|
---|
101 | bool ActionHistory::RedoAction::shouldUndo(){
|
---|
102 | return false;
|
---|
103 | }
|
---|
104 |
|
---|
105 | Action::state_ptr ActionHistory::RedoAction::performCall(){
|
---|
106 | hist->redoLast();
|
---|
107 | return Action::success;
|
---|
108 | }
|
---|
109 |
|
---|
110 | Action::state_ptr ActionHistory::RedoAction::performUndo(Action::state_ptr){
|
---|
111 | ASSERT(0,"Cannot undo a redo (should use undo for this");
|
---|
112 | return Action::success;
|
---|
113 | }
|
---|
114 |
|
---|
115 | Action::state_ptr ActionHistory::RedoAction::performRedo(Action::state_ptr){
|
---|
116 | ASSERT(0,"Cannot redo a redo");
|
---|
117 | return Action::success;
|
---|
118 | }
|
---|