undo.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "undo.h"
  23. #include "console/console.h"
  24. #include "console/consoleTypes.h"
  25. #include "undo_ScriptBinding.h"
  26. //-----------------------------------------------------------------------------
  27. // UndoAction
  28. //-----------------------------------------------------------------------------
  29. IMPLEMENT_CONOBJECT(UndoAction);
  30. IMPLEMENT_CONOBJECT(UndoScriptAction);
  31. UndoAction::UndoAction( const UTF8* actionName)
  32. {
  33. mActionName = StringTable->insert(actionName);
  34. mUndoManager = NULL;
  35. }
  36. S32 UndoManager::getRedoCount()
  37. {
  38. return mRedoStack.size();
  39. }
  40. // Modified to clean up quiet sub actions [KNM | 08/10/11 | ITGB-152]
  41. UndoAction::~UndoAction()
  42. {
  43. clearAllNotifications();
  44. for (U32 i = 0; i < (U32)mQuietSubActions.size(); i++)
  45. delete mQuietSubActions[i];
  46. mQuietSubActions.clear();
  47. }
  48. //-----------------------------------------------------------------------------
  49. void UndoAction::initPersistFields()
  50. {
  51. Parent::initPersistFields();
  52. addField("actionName", TypeString, Offset(mActionName, UndoAction),
  53. "A brief description of the action, for UI representation of this undo/redo action.");
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Implemented to trickle down into quiet sub actions [KNM | 08/10/11 | ITGB-152]
  57. void UndoAction::undo(void)
  58. {
  59. for (U32 i = 0; i < (U32)mQuietSubActions.size(); i++)
  60. mQuietSubActions[i]->undo();
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Implemented to trickle down into quiet sub actions [KNM | 08/10/11 | ITGB-152]
  64. void UndoAction::redo(void)
  65. {
  66. for (U32 i = 0; i < (U32)mQuietSubActions.size(); i++)
  67. mQuietSubActions[i]->redo();
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Adds a "quiet (hidden from user)" sub action [KNM | 08/10/11 | ITGB-152]
  71. void UndoAction::addQuietSubAction(UndoAction * quietSubAction)
  72. {
  73. if (!quietSubAction)
  74. return;
  75. mQuietSubActions.push_back(quietSubAction);
  76. }
  77. //-----------------------------------------------------------------------------
  78. void UndoAction::addToManager(UndoManager* theMan)
  79. {
  80. if(theMan)
  81. {
  82. mUndoManager = theMan;
  83. (*theMan).addAction(this);
  84. }
  85. else
  86. {
  87. mUndoManager = &UndoManager::getDefaultManager();
  88. mUndoManager->addAction(this);
  89. }
  90. }
  91. //-----------------------------------------------------------------------------
  92. // UndoManager
  93. //-----------------------------------------------------------------------------
  94. IMPLEMENT_CONOBJECT(UndoManager);
  95. UndoManager::UndoManager(U32 levels)
  96. {
  97. mNumLevels = levels;
  98. // levels can be arbitrarily high, so we don't really want to reserve(levels).
  99. mUndoStack.reserve(10);
  100. mRedoStack.reserve(10);
  101. }
  102. //-----------------------------------------------------------------------------
  103. UndoManager::~UndoManager()
  104. {
  105. clearStack(mUndoStack);
  106. clearStack(mRedoStack);
  107. }
  108. //-----------------------------------------------------------------------------
  109. void UndoManager::initPersistFields()
  110. {
  111. addField("numLevels", TypeS32, Offset(mNumLevels, UndoManager), "Number of undo & redo levels.");
  112. // arrange for the default undo manager to exist.
  113. // UndoManager &def = getDefaultManager();
  114. // Con::printf("def = %s undo manager created", def.getName());
  115. }
  116. //-----------------------------------------------------------------------------
  117. UndoManager& UndoManager::getDefaultManager()
  118. {
  119. // the default manager is created the first time it is asked for.
  120. static UndoManager *defaultMan = NULL;
  121. if(!defaultMan)
  122. {
  123. defaultMan = new UndoManager();
  124. defaultMan->assignName("DefaultUndoManager");
  125. defaultMan->registerObject();
  126. }
  127. return *defaultMan;
  128. }
  129. void UndoManager::clearAll()
  130. {
  131. clearStack(mUndoStack); clearStack(mRedoStack);
  132. Con::executef(this, 1, "onClear");
  133. }
  134. //-----------------------------------------------------------------------------
  135. void UndoManager::clearStack(Vector<UndoAction*> &stack)
  136. {
  137. Vector<UndoAction*>::iterator itr = stack.begin();
  138. while (itr != stack.end())
  139. {
  140. UndoAction* undo = stack.first();
  141. stack.pop_front();
  142. // Don't delete script created undos.
  143. if (dynamic_cast<UndoScriptAction*>(undo))
  144. undo->deleteObject();
  145. else
  146. delete undo;
  147. }
  148. stack.clear();
  149. }
  150. //-----------------------------------------------------------------------------
  151. void UndoManager::clampStack(Vector<UndoAction*> &stack)
  152. {
  153. while((U32)stack.size() > mNumLevels)
  154. {
  155. UndoAction *act = stack.front();
  156. stack.pop_front();
  157. UndoScriptAction* scriptAction = dynamic_cast<UndoScriptAction*>(act);
  158. if (scriptAction)
  159. scriptAction->deleteObject();
  160. else
  161. delete act;
  162. }
  163. }
  164. void UndoManager::removeAction(UndoAction *action)
  165. {
  166. Vector<UndoAction*>::iterator itr = mUndoStack.begin();
  167. while (itr != mUndoStack.end())
  168. {
  169. if ((*itr) == action)
  170. {
  171. UndoAction* deleteAction = *itr;
  172. mUndoStack.erase(itr);
  173. if (!dynamic_cast<UndoScriptAction*>(deleteAction))
  174. delete deleteAction;
  175. Con::executef(this, 1, "onRemoveUndo");
  176. return;
  177. }
  178. itr++;
  179. }
  180. itr = mRedoStack.begin();
  181. while (itr != mRedoStack.end())
  182. {
  183. if ((*itr) == action)
  184. {
  185. UndoAction* deleteAction = *itr;
  186. mRedoStack.erase(itr);
  187. if (!dynamic_cast<UndoScriptAction*>(deleteAction))
  188. delete deleteAction;
  189. Con::executef(this, 1, "onRemoveUndo");
  190. return;
  191. }
  192. itr++;
  193. }
  194. }
  195. //-----------------------------------------------------------------------------
  196. void UndoManager::undo()
  197. {
  198. // make sure we have an action available
  199. if(mUndoStack.size() < 1)
  200. return;
  201. // pop the action off the undo stack
  202. UndoAction *act = mUndoStack.last();
  203. mUndoStack.pop_back();
  204. // add it to the redo stack
  205. mRedoStack.push_back(act);
  206. if((U32)mRedoStack.size() > mNumLevels)
  207. mRedoStack.pop_front();
  208. Con::executef(this, 1, "onUndo");
  209. // perform the undo, whatever it may be.
  210. (*act).undo();
  211. }
  212. //-----------------------------------------------------------------------------
  213. void UndoManager::redo()
  214. {
  215. // make sure we have an action available
  216. if(mRedoStack.size() < 1)
  217. return;
  218. // pop the action off the redo stack
  219. UndoAction *react = mRedoStack.last();
  220. mRedoStack.pop_back();
  221. // add it to the undo stack
  222. mUndoStack.push_back(react);
  223. if((U32)mUndoStack.size() > mNumLevels)
  224. mUndoStack.pop_front();
  225. Con::executef(this, 1, "onRedo");
  226. // perform the redo, whatever it may be.
  227. (*react).redo();
  228. }
  229. S32 UndoManager::getUndoCount()
  230. {
  231. return mUndoStack.size();
  232. }
  233. StringTableEntry UndoManager::getUndoName(S32 index)
  234. {
  235. if ((index < getUndoCount()) && (index >= 0))
  236. return mUndoStack[index]->mActionName;
  237. return NULL;
  238. }
  239. StringTableEntry UndoManager::getRedoName(S32 index)
  240. {
  241. if ((index < getRedoCount()) && (index >= 0))
  242. return mRedoStack[getRedoCount() - index - 1]->mActionName;
  243. return NULL;
  244. }
  245. //-----------------------------------------------------------------------------
  246. StringTableEntry UndoManager::getNextUndoName()
  247. {
  248. if(mUndoStack.size() < 1)
  249. return NULL;
  250. UndoAction *act = mUndoStack.last();
  251. return (*act).mActionName;
  252. }
  253. //-----------------------------------------------------------------------------
  254. StringTableEntry UndoManager::getNextRedoName()
  255. {
  256. if(mRedoStack.size() < 1)
  257. return NULL;
  258. UndoAction *act = mRedoStack.last();
  259. return (*act).mActionName;
  260. }
  261. //-----------------------------------------------------------------------------
  262. void UndoManager::addAction(UndoAction* action)
  263. {
  264. // push the incoming action onto the stack, move old data off the end if necessary.
  265. mUndoStack.push_back(action);
  266. if((U32)mUndoStack.size() > mNumLevels)
  267. mUndoStack.pop_front();
  268. Con::executef(this, 1, "onAddUndo");
  269. // clear the redo stack
  270. clearStack(mRedoStack);
  271. }