UndoRedo.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 bs;
  7. namespace bs.Editor
  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. private static UndoRedo global;
  22. /// <summary>
  23. /// Constructor for internal runtime use.
  24. /// </summary>
  25. /// <param name="dummy">Dummy parameter to distinguish from public constructor.</param>
  26. private UndoRedo(bool dummy)
  27. { }
  28. /// <summary>
  29. /// Creates a new undo/redo stack.
  30. /// </summary>
  31. public UndoRedo()
  32. {
  33. Internal_CreateInstance(this);
  34. }
  35. /// <summary>
  36. /// Returns the global undo/redo stack.
  37. /// </summary>
  38. public static UndoRedo Global
  39. {
  40. get { return global; }
  41. }
  42. /// <summary>
  43. /// Returns the unique identifier of the command currently at the top of the undo stack.
  44. /// </summary>
  45. public int TopCommandId
  46. {
  47. get { return Internal_GetTopCommandId(mCachedPtr); }
  48. }
  49. /// <summary>
  50. /// Executes the last command on the undo stack, undoing its operations.
  51. /// </summary>
  52. public void Undo()
  53. {
  54. Internal_Undo(mCachedPtr);
  55. }
  56. /// <summary>
  57. /// Executes the last command on the redo stack (last command we called undo on), re-applying its operation.
  58. /// </summary>
  59. public void Redo()
  60. {
  61. Internal_Redo(mCachedPtr);
  62. }
  63. /// <summary>
  64. /// Registers a new undo command.
  65. /// </summary>
  66. /// <param name="command">Command to register</param>
  67. public void RegisterCommand(UndoableCommand command)
  68. {
  69. if (command == null)
  70. return;
  71. Internal_RegisterCommand(mCachedPtr, command.GetCachedPtr());
  72. }
  73. /// <summary>
  74. /// Creates a new undo/redo group. All new commands will be registered to this group. You may remove the group and
  75. /// all of its commands by calling <see cref="PopGroup"/>.
  76. /// </summary>
  77. /// <param name="name">Unique name of the group.</param>
  78. public void PushGroup(string name)
  79. {
  80. Internal_PushGroup(mCachedPtr, name);
  81. }
  82. /// <summary>
  83. /// Removes all the command registered to the current undo/redo group.
  84. /// </summary>
  85. /// <param name="name">Unique name of the group.</param>
  86. public void PopGroup(string name)
  87. {
  88. Internal_PopGroup(mCachedPtr, name);
  89. }
  90. /// <summary>
  91. /// Removes a command with the specified identifier from undo/redo stack without executing it.
  92. /// </summary>
  93. /// <param name="id">Identifier of the command as returned by <see cref="TopCommandId"/></param>
  94. public void PopCommand(int id)
  95. {
  96. Internal_PopCommand(mCachedPtr, id);
  97. }
  98. /// <summary>
  99. /// Clears all undo/redo commands from the stack.
  100. /// </summary>
  101. public void Clear()
  102. {
  103. Internal_Clear(mCachedPtr);
  104. }
  105. /// <summary>
  106. /// Creates new scene object(s) by cloning existing objects. Undo operation recorded in global undo/redo stack.
  107. /// </summary>
  108. /// <param name="so">Scene object(s) to clone.</param>
  109. /// <param name="description">Optional description of what exactly the command does.</param>
  110. /// <returns>Cloned scene objects.</returns>
  111. public static SceneObject[] CloneSO(SceneObject[] so, string description = "")
  112. {
  113. if (so != null)
  114. {
  115. List<IntPtr> soPtrs = new List<IntPtr>();
  116. for (int i = 0; i < so.Length; i++)
  117. {
  118. if(so[i] != null)
  119. soPtrs.Add(so[i].GetCachedPtr());
  120. }
  121. return Internal_CloneSOMulti(soPtrs.ToArray(), description);
  122. }
  123. return new SceneObject[0];
  124. }
  125. /// <summary>
  126. /// Creates new a scene object by cloning an existing object. Undo operation recorded in global undo/redo stack.
  127. /// </summary>
  128. /// <param name="so">Scene object to clone.</param>
  129. /// <param name="description">Optional description of what exactly the command does.</param>
  130. /// <returns>Cloned scene object.</returns>
  131. public static SceneObject CloneSO(SceneObject so, string description = "")
  132. {
  133. if (so != null)
  134. return Internal_CloneSO(so.GetCachedPtr(), description);
  135. return null;
  136. }
  137. /// <summary>
  138. /// Instantiates scene object(s) from a prefab. Undo operation recorded in global undo/redo stack.
  139. /// </summary>
  140. /// <param name="prefab">Prefab to instantiate.</param>
  141. /// <param name="description">Optional description of what exactly the command does.</param>
  142. /// <returns>Instantiated scene object.</returns>
  143. public static SceneObject Instantiate(Prefab prefab, string description = "")
  144. {
  145. if (prefab != null)
  146. return Internal_Instantiate(prefab.GetCachedPtr(), description);
  147. return null;
  148. }
  149. /// <summary>
  150. /// Creates a new scene object. Undo operation recorded in global undo/redo stack.
  151. /// </summary>
  152. /// <param name="name">Name of the scene object.</param>
  153. /// <param name="description">Optional description of what exactly the command does.</param>
  154. /// <returns>Newly created scene object.</returns>
  155. public static SceneObject CreateSO(string name, string description = "")
  156. {
  157. return Internal_CreateSO(name, description);
  158. }
  159. /// <summary>
  160. /// Deletes a scene object. Undo operation recorded in global undo/redo stack.
  161. /// </summary>
  162. /// <param name="so">Scene object to delete.</param>
  163. /// <param name="description">Optional description of what exactly the command does.</param>
  164. public static void DeleteSO(SceneObject so, string description = "")
  165. {
  166. if (so != null)
  167. Internal_DeleteSO(so.GetCachedPtr(), description);
  168. }
  169. /// <summary>
  170. /// Changes the parent of the scene object. Undo operation recorded in global undo/redo stack.
  171. /// </summary>
  172. /// <param name="so">Scene object to change the parent of.</param>
  173. /// <param name="parent">New parent.</param>
  174. /// <param name="description">Optional description of what exactly the command does.</param>
  175. public static void ReparentSO(SceneObject so, SceneObject parent, string description = "")
  176. {
  177. if (so != null)
  178. {
  179. IntPtr parentPtr = IntPtr.Zero;
  180. if (parent != null)
  181. parentPtr = parent.GetCachedPtr();
  182. Internal_ReparentSO(so.GetCachedPtr(), parentPtr, description);
  183. }
  184. }
  185. /// <summary>
  186. /// Changes the parent of a set of scene objects. Undo operation recorded in global undo/redo stack.
  187. /// </summary>
  188. /// <param name="so">Scene objects to change the parent of.</param>
  189. /// <param name="parent">New parent.</param>
  190. /// <param name="description">Optional description of what exactly the command does.</param>
  191. public static void ReparentSO(SceneObject[] so, SceneObject parent, string description = "")
  192. {
  193. if (so != null)
  194. {
  195. List<IntPtr> soPtrs = new List<IntPtr>();
  196. for (int i = 0; i < so.Length; i++)
  197. {
  198. if (so[i] != null)
  199. soPtrs.Add(so[i].GetCachedPtr());
  200. }
  201. if (soPtrs.Count > 0)
  202. {
  203. IntPtr parentPtr = IntPtr.Zero;
  204. if (parent != null)
  205. parentPtr = parent.GetCachedPtr();
  206. Internal_ReparentSOMulti(soPtrs.ToArray(), parentPtr, description);
  207. }
  208. }
  209. }
  210. /// <summary>
  211. /// Breaks the prefab link on the provided scene object and makes the operation undo-able. Undo operation recorded
  212. /// in global undo/redo stack.
  213. /// See <see cref="PrefabUtility.BreakPrefab"/>.
  214. /// </summary>
  215. /// <param name="so">Scene object whose prefab link to break.</param>
  216. /// <param name="description">Optional description of what exactly the command does.</param>
  217. public static void BreakPrefab(SceneObject so, string description = "")
  218. {
  219. if (so != null)
  220. Internal_BreakPrefab(so.GetCachedPtr(), description);
  221. }
  222. /// <summary>
  223. /// Used by the runtime to set the global undo/redo stack.
  224. /// </summary>
  225. /// <param name="global">Instance of the global undo/redo stack.</param>
  226. private static void Internal_SetGlobal(UndoRedo global)
  227. {
  228. // We can't set this directly through the field because there is an issue with Mono and static fields
  229. UndoRedo.global = global;
  230. }
  231. [MethodImpl(MethodImplOptions.InternalCall)]
  232. internal static extern void Internal_CreateInstance(UndoRedo instance);
  233. [MethodImpl(MethodImplOptions.InternalCall)]
  234. internal static extern void Internal_Undo(IntPtr thisPtr);
  235. [MethodImpl(MethodImplOptions.InternalCall)]
  236. internal static extern void Internal_Redo(IntPtr thisPtr);
  237. [MethodImpl(MethodImplOptions.InternalCall)]
  238. internal static extern void Internal_RegisterCommand(IntPtr thisPtr, IntPtr commandPtr);
  239. [MethodImpl(MethodImplOptions.InternalCall)]
  240. internal static extern void Internal_PushGroup(IntPtr thisPtr, string name);
  241. [MethodImpl(MethodImplOptions.InternalCall)]
  242. internal static extern void Internal_PopGroup(IntPtr thisPtr, string name);
  243. [MethodImpl(MethodImplOptions.InternalCall)]
  244. internal static extern void Internal_Clear(IntPtr thisPtr);
  245. [MethodImpl(MethodImplOptions.InternalCall)]
  246. internal static extern void Internal_PopCommand(IntPtr thisPtr, int id);
  247. [MethodImpl(MethodImplOptions.InternalCall)]
  248. internal static extern int Internal_GetTopCommandId(IntPtr thisPtr);
  249. [MethodImpl(MethodImplOptions.InternalCall)]
  250. internal static extern SceneObject Internal_CloneSO(IntPtr soPtr, string description);
  251. [MethodImpl(MethodImplOptions.InternalCall)]
  252. internal static extern SceneObject[] Internal_CloneSOMulti(IntPtr[] soPtr, string description);
  253. [MethodImpl(MethodImplOptions.InternalCall)]
  254. internal static extern SceneObject Internal_Instantiate(IntPtr prefabPtr, string description);
  255. [MethodImpl(MethodImplOptions.InternalCall)]
  256. internal static extern SceneObject Internal_CreateSO(string name, string description);
  257. [MethodImpl(MethodImplOptions.InternalCall)]
  258. internal static extern void Internal_DeleteSO(IntPtr soPtr, string description);
  259. [MethodImpl(MethodImplOptions.InternalCall)]
  260. internal static extern void Internal_ReparentSO(IntPtr soPtr, IntPtr parentSOPtr, string description);
  261. [MethodImpl(MethodImplOptions.InternalCall)]
  262. internal static extern void Internal_ReparentSOMulti(IntPtr[] soPtr, IntPtr parentSOPtr, string description);
  263. [MethodImpl(MethodImplOptions.InternalCall)]
  264. internal static extern void Internal_BreakPrefab(IntPtr soPtr, string description);
  265. }
  266. /** @} */
  267. }