undo.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. //-----------------------------------------------------------------------------
  26. // UndoAction
  27. //-----------------------------------------------------------------------------
  28. IMPLEMENT_CONOBJECT(UndoAction);
  29. IMPLEMENT_CONOBJECT(UndoScriptAction);
  30. UndoAction::UndoAction( const UTF8* actionName)
  31. {
  32. mActionName = StringTable->insert(actionName);
  33. mUndoManager = NULL;
  34. }
  35. // Modified to clean up quiet sub actions [KNM | 08/10/11 | ITGB-152]
  36. UndoAction::~UndoAction()
  37. {
  38. clearAllNotifications();
  39. for (U32 i = 0; i < (U32)mQuietSubActions.size(); i++)
  40. delete mQuietSubActions[i];
  41. mQuietSubActions.clear();
  42. }
  43. //-----------------------------------------------------------------------------
  44. void UndoAction::initPersistFields()
  45. {
  46. Parent::initPersistFields();
  47. addField("actionName", TypeString, Offset(mActionName, UndoAction),
  48. "A brief description of the action, for UI representation of this undo/redo action.");
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Implemented to trickle down into quiet sub actions [KNM | 08/10/11 | ITGB-152]
  52. void UndoAction::undo(void)
  53. {
  54. for (U32 i = 0; i < (U32)mQuietSubActions.size(); i++)
  55. mQuietSubActions[i]->undo();
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Implemented to trickle down into quiet sub actions [KNM | 08/10/11 | ITGB-152]
  59. void UndoAction::redo(void)
  60. {
  61. for (U32 i = 0; i < (U32)mQuietSubActions.size(); i++)
  62. mQuietSubActions[i]->redo();
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Adds a "quiet (hidden from user)" sub action [KNM | 08/10/11 | ITGB-152]
  66. void UndoAction::addQuietSubAction(UndoAction * quietSubAction)
  67. {
  68. if (!quietSubAction)
  69. return;
  70. mQuietSubActions.push_back(quietSubAction);
  71. }
  72. //-----------------------------------------------------------------------------
  73. void UndoAction::addToManager(UndoManager* theMan)
  74. {
  75. if(theMan)
  76. {
  77. mUndoManager = theMan;
  78. (*theMan).addAction(this);
  79. }
  80. else
  81. {
  82. mUndoManager = &UndoManager::getDefaultManager();
  83. mUndoManager->addAction(this);
  84. }
  85. }
  86. //-----------------------------------------------------------------------------
  87. // UndoManager
  88. //-----------------------------------------------------------------------------
  89. IMPLEMENT_CONOBJECT(UndoManager);
  90. UndoManager::UndoManager(U32 levels)
  91. {
  92. mNumLevels = levels;
  93. // levels can be arbitrarily high, so we don't really want to reserve(levels).
  94. mUndoStack.reserve(10);
  95. mRedoStack.reserve(10);
  96. }
  97. //-----------------------------------------------------------------------------
  98. UndoManager::~UndoManager()
  99. {
  100. clearStack(mUndoStack);
  101. clearStack(mRedoStack);
  102. }
  103. //-----------------------------------------------------------------------------
  104. void UndoManager::initPersistFields()
  105. {
  106. addField("numLevels", TypeS32, Offset(mNumLevels, UndoManager), "Number of undo & redo levels.");
  107. // arrange for the default undo manager to exist.
  108. // UndoManager &def = getDefaultManager();
  109. // Con::printf("def = %s undo manager created", def.getName());
  110. }
  111. //-----------------------------------------------------------------------------
  112. UndoManager& UndoManager::getDefaultManager()
  113. {
  114. // the default manager is created the first time it is asked for.
  115. static UndoManager *defaultMan = NULL;
  116. if(!defaultMan)
  117. {
  118. defaultMan = new UndoManager();
  119. defaultMan->assignName("DefaultUndoManager");
  120. defaultMan->registerObject();
  121. }
  122. return *defaultMan;
  123. }
  124. ConsoleMethod(UndoManager, clearAll, void, 2, 2, "Clears the undo manager."
  125. "@return No Return Value")
  126. {
  127. object->clearAll();
  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. ConsoleMethod(UndoManager, getUndoCount, S32, 2, 2, "() \n @return Returns the number of UndoActions stored as an integer")
  230. {
  231. return object->getUndoCount();
  232. }
  233. S32 UndoManager::getUndoCount()
  234. {
  235. return mUndoStack.size();
  236. }
  237. ConsoleMethod(UndoManager, getUndoName, const char*, 3, 3, "( S32 index ) Gets the name of the UndoAction at given index.\n "
  238. "@param index An integer index value for the desired undo\n"
  239. "@return The name as a string")
  240. {
  241. return object->getUndoName(dAtoi(argv[2]));
  242. }
  243. StringTableEntry UndoManager::getUndoName(S32 index)
  244. {
  245. if ((index < getUndoCount()) && (index >= 0))
  246. return mUndoStack[index]->mActionName;
  247. return NULL;
  248. }
  249. ConsoleMethod(UndoManager, getRedoCount, S32, 2, 2, "() \n @return Returns the number of redo Actions stored as an integer")
  250. {
  251. return object->getRedoCount();
  252. }
  253. S32 UndoManager::getRedoCount()
  254. {
  255. return mRedoStack.size();
  256. }
  257. ConsoleMethod(UndoManager, getRedoName, const char*, 3, 3, "( S32 index ) Gets the name of the Action at given index.\n "
  258. "@param index An integer index value for the desired redo\n"
  259. "@return The name as a string")
  260. {
  261. return object->getRedoName(dAtoi(argv[2]));
  262. }
  263. StringTableEntry UndoManager::getRedoName(S32 index)
  264. {
  265. if ((index < getRedoCount()) && (index >= 0))
  266. return mRedoStack[getRedoCount() - index - 1]->mActionName;
  267. return NULL;
  268. }
  269. //-----------------------------------------------------------------------------
  270. StringTableEntry UndoManager::getNextUndoName()
  271. {
  272. if(mUndoStack.size() < 1)
  273. return NULL;
  274. UndoAction *act = mUndoStack.last();
  275. return (*act).mActionName;
  276. }
  277. //-----------------------------------------------------------------------------
  278. StringTableEntry UndoManager::getNextRedoName()
  279. {
  280. if(mRedoStack.size() < 1)
  281. return NULL;
  282. UndoAction *act = mRedoStack.last();
  283. return (*act).mActionName;
  284. }
  285. //-----------------------------------------------------------------------------
  286. void UndoManager::addAction(UndoAction* action)
  287. {
  288. // push the incoming action onto the stack, move old data off the end if necessary.
  289. mUndoStack.push_back(action);
  290. if((U32)mUndoStack.size() > mNumLevels)
  291. mUndoStack.pop_front();
  292. Con::executef(this, 1, "onAddUndo");
  293. // clear the redo stack
  294. clearStack(mRedoStack);
  295. }
  296. //-----------------------------------------------------------------------------
  297. ConsoleMethod(UndoAction, addToManager, void, 2, 3, "action.addToManager([undoManager]) Adds an UndoAction to the manager"
  298. "@param undoManager The manager to add the object to (default NULL)\n"
  299. "@return No Return Value")
  300. {
  301. UndoManager *theMan = NULL;
  302. if(argc == 3)
  303. {
  304. SimObject *obj = Sim::findObject(argv[2]);
  305. if(obj)
  306. theMan = dynamic_cast<UndoManager*> (obj);
  307. }
  308. object->addToManager(theMan);
  309. }
  310. //-----------------------------------------------------------------------------
  311. ConsoleMethod(UndoManager, undo, void, 2, 2, "UndoManager.undo(); Pops the top undo action off the stack, resolves it, "
  312. "and then pushes it onto the redo stack")
  313. {
  314. object->undo();
  315. }
  316. //-----------------------------------------------------------------------------
  317. ConsoleMethod(UndoManager, redo, void, 2, 2, "UndoManager.redo(); Pops the top redo action off the stack, resolves it, "
  318. "and then pushes it onto the undo stack")
  319. {
  320. object->redo();
  321. }
  322. //-----------------------------------------------------------------------------
  323. ConsoleMethod(UndoManager, getNextUndoName, const char *, 2, 2, "UndoManager.getNextUndoName(); Gets the name of the action at the top of the undo stack\n"
  324. "@return The name of the top action on the undo stack")
  325. {
  326. StringTableEntry name = object->getNextUndoName();
  327. if(!name)
  328. return NULL;
  329. char *ret = Con::getReturnBuffer(dStrlen(name) + 1);
  330. dStrcpy(ret, name);
  331. return ret;
  332. }
  333. //-----------------------------------------------------------------------------
  334. ConsoleMethod(UndoManager, getNextRedoName, const char *, 2, 2, "UndoManager.getNextRedoName(); Gets the name of the action at the top of the undo stack\n"
  335. "@return The name of the top action on the redo stack")
  336. {
  337. StringTableEntry name = object->getNextRedoName();
  338. if(!name)
  339. return NULL;
  340. char *ret = Con::getReturnBuffer(dStrlen(name) + 1);
  341. dStrcpy(ret, name);
  342. return ret;
  343. }