RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
undoableactionsmanager.cpp
Go to the documentation of this file.
1 //--------------------------------------------------------------- @License begins
2 // RioEngine: The late night Coke -without whores- debugging sessions
3 // 2012-2015 Leopoldo Lomas Flores. Torreon, Coahuila. MEXICO
4 // leopoldolomas [at] gmail
5 // www.rioengine.com
6 // www.leopoldolomas.info
7 // "You have a problem, you face it like a man."
8 //
9 // This is free and unencumbered software released into the public domain.
10 //
11 // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
12 // software, either in source code form or as a compiled binary, for any purpose,
13 // commercial or non-commercial, and by any means.
14 //
15 // In jurisdictions that recognize copyright laws, the author or authors of this
16 // software dedicate any and all copyright interest in the software to the public
17 // domain. We make this dedication for the benefit of the public at large and to
18 // the detriment of our heirs and successors. We intend this dedication to be
19 // an overt act of relinquishment in perpetuity of all present and future
20 // rights to this software under copyright law.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS
24 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //--------------------------------------------------------------- @License ends
29 
30 #include "undoableaction.h"
31 #include "undoableactionsmanager.h"
32 #include "misc/genericshelper.hpp"
33 
34 //-----------------------------------------------------------------------------
35 
37  m_undoActions = new std::vector<IUndoableAction*>();
38  m_redoActions = new std::vector<IUndoableAction*>();
39 }
40 
41 //-----------------------------------------------------------------------------
42 
44  return m_undoActions->size();
45 }
46 
47 //-----------------------------------------------------------------------------
48 
50  return m_redoActions->size();
51 }
52 
53 //-----------------------------------------------------------------------------
54 
56  return getUndoActionsCount() > 0;
57 }
58 
59 //-----------------------------------------------------------------------------
60 
62  return getRedoActionsCount() > 0;
63 }
64 
65 //-----------------------------------------------------------------------------
66 
68  if (!hasUndoActions()) {
69  return false;
70  }
71 
72  // get the last undo action
73  IUndoableAction* undo_action = m_undoActions->at(m_undoActions->size() - 1);
74 
75  // perform the undo action
76  undo_action->performAction();
77 
78  // create the redo action
79  IUndoableAction* redo_action = undo_action->getOppositeAction();
80 
81  // push redo action
82  pushRedoAction(redo_action);
83 
84  // now we're done with the undo action, remove it from the vector and release it
85  GenericsHelper::deleteObjectFromVector(m_undoActions, undo_action);
86 
87  return true;
88 }
89 
90 //-----------------------------------------------------------------------------
91 
93  if (!hasRedoActions())
94  return false;
95 
96  // get the last undo action
97  IUndoableAction* redo_action = m_redoActions->at(m_redoActions->size() - 1);
98 
99  // perform the undo action
100  redo_action->performAction();
101 
102  // create the redo action
103  IUndoableAction* undo_action = redo_action->getOppositeAction();
104 
105  // push redo action
106  pushUndoAction(undo_action, false);
107 
108  // now we're done with the redo action, remove it from the vector and release it
109  GenericsHelper::deleteObjectFromVector(m_redoActions, redo_action);
110 
111  return true;
112 }
113 
114 //-----------------------------------------------------------------------------
115 
117  if (!hasUndoActions()) {
118  return "";
119  }
120 
121  IUndoableAction* undo_action = m_undoActions->at(m_undoActions->size() - 1);
122  return undo_action->getHint();
123 }
124 
125 //-----------------------------------------------------------------------------
126 
128  if (!hasRedoActions()) {
129  return "";
130  }
131 
132  IUndoableAction* redo_action = m_redoActions->at(m_redoActions->size() - 1);
133  return redo_action->getHint();
134 }
135 
136 //-----------------------------------------------------------------------------
137 
138 void UndoableActionsManager::pushUndoAction(IUndoableAction* undo_action, bool clear_redo_stack) {
139  if (clear_redo_stack && hasRedoActions()) {
140  GenericsHelper::deleteVector(m_redoActions);
141  m_redoActions = new std::vector<IUndoableAction*>();
142  }
143 
144  m_undoActions->push_back(undo_action);
145 }
146 
147 //-----------------------------------------------------------------------------
148 
150  pushUndoAction(undo_action, true);
151 }
152 
153 //-----------------------------------------------------------------------------
154 
156  // TODO don't duplicate a redo action
157  m_redoActions->push_back(redo_action);
158 }
159 
160 //-----------------------------------------------------------------------------
161 
163  GenericsHelper::deleteVector(m_undoActions);
164  GenericsHelper::deleteVector(m_redoActions);
165 
166  m_undoActions = new std::vector<IUndoableAction*>();
167  m_redoActions = new std::vector<IUndoableAction*>();
168 }
169 
170 //-----------------------------------------------------------------------------
171 
173  GenericsHelper::deleteVector(m_undoActions);
174  GenericsHelper::deleteVector(m_redoActions);
175 }
void pushRedoAction(IUndoableAction *action)
static void deleteVector(std::vector< T * > *vec)
virtual const char * getHint() const
const char * getHintForCurrentRedoAction()
const char * getHintForCurrentUndoAction()
virtual IUndoableAction * getOppositeAction()
virtual void performAction()
static bool deleteObjectFromVector(std::vector< T * > *vec, void *object_to_delete)
void pushUndoAction(IUndoableAction *undo_action, bool clear_redo_stack)