source: molecuilder/src/Actions/ActionHistory.cpp@ cbc639

Last change on this file since cbc639 was 8d9984, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Added full undo functioniality

  • Property mode set to 100644
File size: 2.7 KB
Line 
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
15using namespace std;
16
17ActionHistory::ActionHistory()
18{}
19
20ActionHistory::~ActionHistory()
21{}
22
23void 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}
30void 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
38void 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
44void ActionHistory::clear(){
45 cout << "History cleared" << endl;
46 history.clear();
47 yrotsih.clear();
48}
49
50void ActionHistory::init(){
51 ActionHistory *hist = new ActionHistory();
52 setInstance(hist);
53 new UndoAction(hist);
54 new RedoAction(hist);
55}
56
57CONSTRUCT_SINGLETON(ActionHistory)
58
59/****************** Contained actions *******************/
60ActionHistory::UndoAction::UndoAction(ActionHistory *_hist) :
61 Action("Undo"),
62 hist(_hist)
63{}
64
65ActionHistory::UndoAction::~UndoAction(){}
66
67bool ActionHistory::UndoAction::canUndo(){
68 return false;
69}
70
71bool ActionHistory::UndoAction::shouldUndo(){
72 return false;
73}
74
75Action::state_ptr ActionHistory::UndoAction::performCall(){
76 hist->undoLast();
77 return Action::success;
78}
79
80Action::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
85Action::state_ptr ActionHistory::UndoAction::performRedo(Action::state_ptr){
86 ASSERT(0,"Cannot redo an undo");
87 return Action::success;
88}
89
90ActionHistory::RedoAction::RedoAction(ActionHistory *_hist) :
91 Action("Redo"),
92 hist(_hist)
93{}
94
95ActionHistory::RedoAction::~RedoAction(){}
96
97bool ActionHistory::RedoAction::canUndo(){
98 return false;
99}
100
101bool ActionHistory::RedoAction::shouldUndo(){
102 return false;
103}
104
105Action::state_ptr ActionHistory::RedoAction::performCall(){
106 hist->redoLast();
107 return Action::success;
108}
109
110Action::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
115Action::state_ptr ActionHistory::RedoAction::performRedo(Action::state_ptr){
116 ASSERT(0,"Cannot redo a redo");
117 return Action::success;
118}
Note: See TracBrowser for help on using the repository browser.