UndoRedo.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. using BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. /** @addtogroup Utility-Editor
  10. * @{
  11. */
  12. /// <summary>
  13. /// Provides functionality to undo or redo recently performed operations in the editor. All commands executed from this
  14. /// class are undoable/redoable.
  15. ///
  16. /// The class provides static methods that access the global undo/redo stack, but can also be instantiated to provide
  17. /// local undo/redo stacks.
  18. /// </summary>
  19. public class UndoRedo : ScriptObject
  20. {
  21. /// <summary>
  22. /// Constructor for internal runtime use.
  23. /// </summary>
  24. /// <param name="dummy">Dummy parameter to distinguish from public constructor.</param>
  25. private UndoRedo(bool dummy)
  26. { }
  27. /// <summary>
  28. /// Creates a new undo/redo stack.
  29. /// </summary>
  30. public UndoRedo()
  31. {
  32. Internal_CreateInstance(this);
  33. }
  34. /// <summary>
  35. /// Returns the global undo/redo stack.
  36. /// </summary>
  37. public static UndoRedo Global
  38. {
  39. get { return Internal_GetGlobal(); }
  40. }
  41. /// <summary>
  42. /// Returns the unique identifier of the command currently at the top of the undo stack.
  43. /// </summary>
  44. public int TopCommandId
  45. {
  46. get { return Internal_GetTopCommandId(mCachedPtr); }
  47. }
  48. /// <summary>
  49. /// Executes the last command on the undo stack, undoing its operations.
  50. /// </summary>
  51. public void Undo()
  52. {
  53. Internal_Undo(mCachedPtr);
  54. }
  55. /// <summary>
  56. /// Executes the last command on the redo stack (last command we called undo on), re-applying its operation.
  57. /// </summary>
  58. public void Redo()
  59. {
  60. Internal_Redo(mCachedPtr);
  61. }
  62. /// <summary>
  63. /// Creates a new undo/redo group. All new commands will be registered to this group. You may remove the group and
  64. /// all of its commands by calling <see cref="PopGroup"/>.
  65. /// </summary>
  66. /// <param name="name">Unique name of the group.</param>
  67. public void PushGroup(string name)
  68. {
  69. Internal_PushGroup(mCachedPtr, name);
  70. }
  71. /// <summary>
  72. /// Removes all the command registered to the current undo/redo group.
  73. /// </summary>
  74. /// <param name="name">Unique name of the group.</param>
  75. public void PopGroup(string name)
  76. {
  77. Internal_PopGroup(mCachedPtr, name);
  78. }
  79. /// <summary>
  80. /// Removes a command with the specified identifier from undo/redo stack without executing it.
  81. /// </summary>
  82. /// <param name="id">Identifier of the command as returned by <see cref="GetTopCommandId"/></param>
  83. public void PopCommand(int id)
  84. {
  85. Internal_PopCommand(mCachedPtr, id);
  86. }
  87. /// <summary>
  88. /// Records a state of the entire scene object at a specific point and allows you to restore it to its original
  89. /// values as needed. Undo operation recorded in global undo/redo stack.
  90. /// </summary>
  91. /// <param name="so">Scene object to record.</param>
  92. /// <param name="recordHierarchy">If true all children of this object will also be recorded.</param>
  93. /// <param name="description">Optional description of what exactly the command does.</param>
  94. public static void RecordSO(SceneObject so, bool recordHierarchy = false, string description = "")
  95. {
  96. if (so != null)
  97. Internal_RecordSO(so.GetCachedPtr(), recordHierarchy, description);
  98. }
  99. /// <summary>
  100. /// Creates new scene object(s) by cloning existing objects. Undo operation recorded in global undo/redo stack.
  101. /// </summary>
  102. /// <param name="so">Scene object(s) to clone.</param>
  103. /// <param name="description">Optional description of what exactly the command does.</param>
  104. /// <returns>Cloned scene objects.</returns>
  105. public static SceneObject[] CloneSO(SceneObject[] so, string description = "")
  106. {
  107. if (so != null)
  108. {
  109. List<IntPtr> soPtrs = new List<IntPtr>();
  110. for (int i = 0; i < so.Length; i++)
  111. {
  112. if(so[i] != null)
  113. soPtrs.Add(so[i].GetCachedPtr());
  114. }
  115. return Internal_CloneSOMulti(soPtrs.ToArray(), description);
  116. }
  117. return new SceneObject[0];
  118. }
  119. /// <summary>
  120. /// Creates new a scene object by cloning an existing object. Undo operation recorded in global undo/redo stack.
  121. /// </summary>
  122. /// <param name="so">Scene object to clone.</param>
  123. /// <param name="description">Optional description of what exactly the command does.</param>
  124. /// <returns>Cloned scene object.</returns>
  125. public static SceneObject CloneSO(SceneObject so, string description = "")
  126. {
  127. if (so != null)
  128. return Internal_CloneSO(so.GetCachedPtr(), description);
  129. return null;
  130. }
  131. /// <summary>
  132. /// Instantiates scene object(s) from a prefab. Undo operation recorded in global undo/redo stack.
  133. /// </summary>
  134. /// <param name="prefab">Prefab to instantiate.</param>
  135. /// <param name="description">Optional description of what exactly the command does.</param>
  136. /// <returns>Instantiated scene object.</returns>
  137. public static SceneObject Instantiate(Prefab prefab, string description = "")
  138. {
  139. if (prefab != null)
  140. return Internal_Instantiate(prefab.GetCachedPtr(), description);
  141. return null;
  142. }
  143. /// <summary>
  144. /// Creates a new scene object. Undo operation recorded in global undo/redo stack.
  145. /// </summary>
  146. /// <param name="name">Name of the scene object.</param>
  147. /// <param name="description">Optional description of what exactly the command does.</param>
  148. /// <returns>Newly created scene object.</returns>
  149. public static SceneObject CreateSO(string name, string description = "")
  150. {
  151. return Internal_CreateSO(name, description);
  152. }
  153. /// <summary>
  154. /// Deletes a scene object. Undo operation recorded in global undo/redo stack.
  155. /// </summary>
  156. /// <param name="so">Scene object to delete.</param>
  157. /// <param name="description">Optional description of what exactly the command does.</param>
  158. public static void DeleteSO(SceneObject so, string description = "")
  159. {
  160. if (so != null)
  161. Internal_DeleteSO(so.GetCachedPtr(), description);
  162. }
  163. /// <summary>
  164. /// Changes the parent of the scene object. Undo operation recorded in global undo/redo stack.
  165. /// </summary>
  166. /// <param name="so">Scene object to change the parent of.</param>
  167. /// <param name="parent">New parent.</param>
  168. /// <param name="description">Optional description of what exactly the command does.</param>
  169. public static void ReparentSO(SceneObject so, SceneObject parent, string description = "")
  170. {
  171. if (so != null)
  172. {
  173. IntPtr parentPtr = IntPtr.Zero;
  174. if (parent != null)
  175. parentPtr = parent.GetCachedPtr();
  176. Internal_ReparentSO(so.GetCachedPtr(), parentPtr, description);
  177. }
  178. }
  179. /// <summary>
  180. /// Changes the parent of a set of scene objects. Undo operation recorded in global undo/redo stack.
  181. /// </summary>
  182. /// <param name="so">Scene objects to change the parent of.</param>
  183. /// <param name="parent">New parent.</param>
  184. /// <param name="description">Optional description of what exactly the command does.</param>
  185. public static void ReparentSO(SceneObject[] so, SceneObject parent, string description = "")
  186. {
  187. if (so != null)
  188. {
  189. List<IntPtr> soPtrs = new List<IntPtr>();
  190. for (int i = 0; i < so.Length; i++)
  191. {
  192. if (so[i] != null)
  193. soPtrs.Add(so[i].GetCachedPtr());
  194. }
  195. if (soPtrs.Count > 0)
  196. {
  197. IntPtr parentPtr = IntPtr.Zero;
  198. if (parent != null)
  199. parentPtr = parent.GetCachedPtr();
  200. Internal_ReparentSOMulti(soPtrs.ToArray(), parentPtr, description);
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// Breaks the prefab link on the provided scene object and makes the operation undo-able. Undo operation recorded
  206. /// in global undo/redo stack.
  207. /// See <see cref="PrefabUtility.BreakPrefab"/>.
  208. /// </summary>
  209. /// <param name="so">Scene object whose prefab link to break.</param>
  210. /// <param name="description">Optional description of what exactly the command does.</param>
  211. public static void BreakPrefab(SceneObject so, string description = "")
  212. {
  213. if (so != null)
  214. Internal_BreakPrefab(so.GetCachedPtr(), description);
  215. }
  216. [MethodImpl(MethodImplOptions.InternalCall)]
  217. internal static extern void Internal_CreateInstance(UndoRedo instance);
  218. [MethodImpl(MethodImplOptions.InternalCall)]
  219. internal static extern UndoRedo Internal_GetGlobal();
  220. [MethodImpl(MethodImplOptions.InternalCall)]
  221. internal static extern void Internal_Undo(IntPtr thisPtr);
  222. [MethodImpl(MethodImplOptions.InternalCall)]
  223. internal static extern void Internal_Redo(IntPtr thisPtr);
  224. [MethodImpl(MethodImplOptions.InternalCall)]
  225. internal static extern void Internal_PushGroup(IntPtr thisPtr, string name);
  226. [MethodImpl(MethodImplOptions.InternalCall)]
  227. internal static extern void Internal_PopGroup(IntPtr thisPtr, string name);
  228. [MethodImpl(MethodImplOptions.InternalCall)]
  229. internal static extern void Internal_PopCommand(IntPtr thisPtr, int id);
  230. [MethodImpl(MethodImplOptions.InternalCall)]
  231. internal static extern int Internal_GetTopCommandId(IntPtr thisPtr);
  232. [MethodImpl(MethodImplOptions.InternalCall)]
  233. internal static extern void Internal_RecordSO(IntPtr soPtr, bool recordHierarchy, string description);
  234. [MethodImpl(MethodImplOptions.InternalCall)]
  235. internal static extern SceneObject Internal_CloneSO(IntPtr soPtr, string description);
  236. [MethodImpl(MethodImplOptions.InternalCall)]
  237. internal static extern SceneObject[] Internal_CloneSOMulti(IntPtr[] soPtr, string description);
  238. [MethodImpl(MethodImplOptions.InternalCall)]
  239. internal static extern SceneObject Internal_Instantiate(IntPtr prefabPtr, string description);
  240. [MethodImpl(MethodImplOptions.InternalCall)]
  241. internal static extern SceneObject Internal_CreateSO(string name, string description);
  242. [MethodImpl(MethodImplOptions.InternalCall)]
  243. internal static extern void Internal_DeleteSO(IntPtr soPtr, string description);
  244. [MethodImpl(MethodImplOptions.InternalCall)]
  245. internal static extern void Internal_ReparentSO(IntPtr soPtr, IntPtr parentSOPtr, string description);
  246. [MethodImpl(MethodImplOptions.InternalCall)]
  247. internal static extern void Internal_ReparentSOMulti(IntPtr[] soPtr, IntPtr parentSOPtr, string description);
  248. [MethodImpl(MethodImplOptions.InternalCall)]
  249. internal static extern void Internal_BreakPrefab(IntPtr soPtr, string description);
  250. }
  251. /** @} */
  252. }