Undo.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include "EditorCoreAPI.h"
  10. #include "IUndoManagerListener.h"
  11. #include "IUndoObject.h"
  12. #include <AzCore/Asset/AssetManager.h>
  13. #include <CryCommon/StlUtils.h>
  14. struct IUndoObject;
  15. class CSuperUndoStep;
  16. class AssetManagerUndoInterruptor;
  17. //! CUndo is a collection of IUndoObjects instances that forms single undo step.
  18. class CUndoStep
  19. {
  20. public:
  21. CUndoStep() {};
  22. virtual ~CUndoStep() { ClearObjects(); }
  23. //! Set undo object name.
  24. void SetName(const QString& name) { m_name = name; };
  25. //! Get undo object name.
  26. const QString& GetName() { return m_name; };
  27. //! Add new undo object to undo step.
  28. void AddUndoObject(IUndoObject* o)
  29. {
  30. m_undoObjects.push_back(o);
  31. }
  32. void ClearObjects()
  33. {
  34. int i;
  35. // Release all undo objects.
  36. std::vector<IUndoObject*> undoObjects = m_undoObjects;
  37. m_undoObjects.clear();
  38. for (i = 0; i < undoObjects.size(); i++)
  39. {
  40. undoObjects[i]->Release();
  41. }
  42. };
  43. virtual int GetSize() const
  44. {
  45. int size = 0;
  46. for (int i = 0; i < m_undoObjects.size(); i++)
  47. {
  48. size += m_undoObjects[i]->GetSize();
  49. }
  50. return size;
  51. }
  52. // Confetti: Get the size of m_undoObjects
  53. virtual int GetCount() const
  54. {
  55. return static_cast<int>(m_undoObjects.size());
  56. }
  57. virtual bool IsEmpty() const { return m_undoObjects.empty(); };
  58. virtual void Undo(bool bUndo)
  59. {
  60. for (int i = static_cast<int>(m_undoObjects.size() - 1); i >= 0; i--)
  61. {
  62. m_undoObjects[i]->Undo(bUndo);
  63. }
  64. }
  65. virtual void Redo()
  66. {
  67. for (int i = 0; i < m_undoObjects.size(); i++)
  68. {
  69. m_undoObjects[i]->Redo();
  70. }
  71. }
  72. // get undo object at index i
  73. IUndoObject* GetUndoObject(int i = 0)
  74. {
  75. if (i < m_undoObjects.size())
  76. {
  77. return m_undoObjects[i];
  78. }
  79. return nullptr;
  80. }
  81. const QString GetObjectNames()
  82. {
  83. QString objNamesStr("");
  84. std::vector<QString> objNames;
  85. bool bFirst = true;
  86. for (int i = 0; i < m_undoObjects.size(); ++i)
  87. {
  88. if (!m_undoObjects[i])
  89. {
  90. continue;
  91. }
  92. if (m_undoObjects[i]->GetObjectName() == nullptr)
  93. {
  94. continue;
  95. }
  96. if (stl::find(objNames, m_undoObjects[i]->GetObjectName()))
  97. {
  98. continue;
  99. }
  100. if (bFirst == false)
  101. {
  102. objNamesStr += ",";
  103. }
  104. else
  105. {
  106. bFirst = false;
  107. }
  108. objNamesStr += m_undoObjects[i]->GetObjectName();
  109. objNames.push_back(m_undoObjects[i]->GetObjectName());
  110. }
  111. return objNamesStr;
  112. };
  113. private: // ------------------------------------------------------
  114. friend class CUndoManager;
  115. QString m_name;
  116. // Undo objects registered for this step.
  117. std::vector<IUndoObject*> m_undoObjects;
  118. };
  119. AZ_PUSH_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  120. /*!
  121. * CUndoManager is keeping and operating on CUndo class instances.
  122. * TODO: this class is superseded by AzToolsFramework::UndoSystem
  123. */
  124. class EDITOR_CORE_API CUndoManager
  125. {
  126. AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  127. public:
  128. CUndoManager();
  129. ~CUndoManager();
  130. //! Begin operation requiring undo.
  131. //! Undo manager enters holding state.
  132. void Begin();
  133. //! Restore all undo objects registered since last Begin call.
  134. //! @param bUndo if true all Undo object registered up to this point will be undone.
  135. void Restore(bool bUndo);
  136. //! Accept changes and registers an undo object with the undo manager.
  137. //! This will allow the user to undo the operation.
  138. void Accept(const QString& name);
  139. //! Cancel changes and restore undo objects.
  140. void Cancel();
  141. //! Normally this is NOT needed but in special cases this can be useful.
  142. //! This allows to group a set of Begin()/Accept() sequences to be undone in one operation.
  143. void SuperBegin();
  144. //! When a SuperBegin() used, this method is used to Accept.
  145. //! This leaves the undo database in its modified state and registers the IUndoObjects with the undo system.
  146. //! This will allow the user to undo the operation.
  147. void SuperAccept(const QString& name);
  148. //! Cancel changes and restore undo objects.
  149. void SuperCancel();
  150. //! Temporarily suspends recording of undo.
  151. void Suspend();
  152. //! Resume recording if was suspended.
  153. void Resume();
  154. // Undo last operation.
  155. void Undo(int numSteps = 1);
  156. // Undo a specific operation
  157. void Reset(const QString& name);
  158. //! Redo last undo.
  159. void Redo(int numSteps = 1);
  160. //! Check if undo information is recording now.
  161. bool IsUndoRecording() const;
  162. //////////////////////////////////////////////////////////////////////////
  163. bool IsUndoSuspended() const;
  164. //! Put new undo object, must be called between Begin and Accept/Cancel methods.
  165. void RecordUndo(IUndoObject* obj);
  166. bool IsHaveUndo() const;
  167. bool IsHaveRedo() const;
  168. void SetMaxUndoStep(int steps);
  169. int GetMaxUndoStep() const;
  170. //! Returns length of undo stack.
  171. int GetUndoStackLen() const;
  172. //! Returns length of redo stack.
  173. int GetRedoStackLen() const;
  174. //! Retreves array of undo objects names.
  175. std::vector<QString> GetUndoStackNames() const;
  176. //! Retreves array of redo objects names.
  177. std::vector<QString> GetRedoStackNames() const;
  178. //! Get size of current Undo and redo database size.
  179. int GetDatabaseSize();
  180. //! Completly flush all Undo and redo buffers.
  181. //! Must be done on level reloads or global Fetch operation.
  182. void Flush();
  183. //! Get Next Undo Item and Next Redo Item -- Vera, Confetti
  184. CUndoStep* GetNextUndo();
  185. CUndoStep* GetNextRedo();
  186. void ClearRedoStack();
  187. void ClearUndoStack();
  188. void ClearUndoStack(int num);
  189. void ClearRedoStack(int num);
  190. void AddListener(IUndoManagerListener* pListener);
  191. void RemoveListener(IUndoManagerListener* pListener);
  192. int GetSuspendCount() { return m_suspendCount; }
  193. private: // ---------------------------------------------------------------
  194. void BeginUndoTransaction();
  195. void EndUndoTransaction();
  196. void BeginRestoreTransaction();
  197. void EndRestoreTransaction();
  198. void SignalNumUndoRedoToListeners();
  199. void SignalUndoFlushedToListeners();
  200. bool m_bRecording;
  201. bool m_bSuperRecording;
  202. int m_suspendCount;
  203. bool m_bUndoing;
  204. bool m_bRedoing;
  205. bool m_bClearRedoStackQueued;
  206. CUndoStep* m_currentUndo;
  207. //! Undo step object created by SuperBegin.
  208. CSuperUndoStep* m_superUndo;
  209. AssetManagerUndoInterruptor* m_assetManagerUndoInterruptor;
  210. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  211. std::list<CUndoStep*> m_undoStack;
  212. std::list<CUndoStep*> m_redoStack;
  213. std::vector<IUndoManagerListener*> m_listeners;
  214. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  215. };
  216. class CScopedSuspendUndo
  217. {
  218. public:
  219. EDITOR_CORE_API CScopedSuspendUndo();
  220. EDITOR_CORE_API ~CScopedSuspendUndo();
  221. };