2
0

UndoRedo.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. /// Creates a new scene object with a set of initial components. Undo operation recorded in global undo/redo stack.
  161. /// </summary>
  162. /// <param name="name">Name of the scene object.</param>
  163. /// <param name="description">Optional description of what exactly the command does.</param>
  164. /// <param name="componentTypes">Optional set of components that will be added to the scene object.</param>
  165. /// <returns>Newly created scene object.</returns>
  166. public static SceneObject CreateSO(string name, string description = "", params Type[] componentTypes)
  167. {
  168. return Internal_CreateSO2(name, componentTypes, description);
  169. }
  170. /// <summary>
  171. /// Deletes a scene object. Undo operation recorded in global undo/redo stack.
  172. /// </summary>
  173. /// <param name="so">Scene object to delete.</param>
  174. /// <param name="description">Optional description of what exactly the command does.</param>
  175. public static void DeleteSO(SceneObject so, string description = "")
  176. {
  177. if (so != null)
  178. Internal_DeleteSO(so.GetCachedPtr(), description);
  179. }
  180. /// <summary>
  181. /// Changes the parent of the scene object. Undo operation recorded in global undo/redo stack.
  182. /// </summary>
  183. /// <param name="so">Scene object to change the parent of.</param>
  184. /// <param name="parent">New parent.</param>
  185. /// <param name="description">Optional description of what exactly the command does.</param>
  186. public static void ReparentSO(SceneObject so, SceneObject parent, string description = "")
  187. {
  188. if (so != null)
  189. {
  190. IntPtr parentPtr = IntPtr.Zero;
  191. if (parent != null)
  192. parentPtr = parent.GetCachedPtr();
  193. Internal_ReparentSO(so.GetCachedPtr(), parentPtr, description);
  194. }
  195. }
  196. /// <summary>
  197. /// Changes the parent of a set of scene objects. Undo operation recorded in global undo/redo stack.
  198. /// </summary>
  199. /// <param name="so">Scene objects to change the parent of.</param>
  200. /// <param name="parent">New parent.</param>
  201. /// <param name="description">Optional description of what exactly the command does.</param>
  202. public static void ReparentSO(SceneObject[] so, SceneObject parent, string description = "")
  203. {
  204. if (so != null)
  205. {
  206. List<IntPtr> soPtrs = new List<IntPtr>();
  207. for (int i = 0; i < so.Length; i++)
  208. {
  209. if (so[i] != null)
  210. soPtrs.Add(so[i].GetCachedPtr());
  211. }
  212. if (soPtrs.Count > 0)
  213. {
  214. IntPtr parentPtr = IntPtr.Zero;
  215. if (parent != null)
  216. parentPtr = parent.GetCachedPtr();
  217. Internal_ReparentSOMulti(soPtrs.ToArray(), parentPtr, description);
  218. }
  219. }
  220. }
  221. /// <summary>
  222. /// Breaks the prefab link on the provided scene object and makes the operation undo-able. Undo operation recorded
  223. /// in global undo/redo stack.
  224. /// See <see cref="PrefabUtility.BreakPrefab"/>.
  225. /// </summary>
  226. /// <param name="so">Scene object whose prefab link to break.</param>
  227. /// <param name="description">Optional description of what exactly the command does.</param>
  228. public static void BreakPrefab(SceneObject so, string description = "")
  229. {
  230. if (so != null)
  231. Internal_BreakPrefab(so.GetCachedPtr(), description);
  232. }
  233. /// <summary>
  234. /// Used by the runtime to set the global undo/redo stack.
  235. /// </summary>
  236. /// <param name="global">Instance of the global undo/redo stack.</param>
  237. private static void Internal_SetGlobal(UndoRedo global)
  238. {
  239. // We can't set this directly through the field because there is an issue with Mono and static fields
  240. UndoRedo.global = global;
  241. }
  242. [MethodImpl(MethodImplOptions.InternalCall)]
  243. internal static extern void Internal_CreateInstance(UndoRedo instance);
  244. [MethodImpl(MethodImplOptions.InternalCall)]
  245. internal static extern void Internal_Undo(IntPtr thisPtr);
  246. [MethodImpl(MethodImplOptions.InternalCall)]
  247. internal static extern void Internal_Redo(IntPtr thisPtr);
  248. [MethodImpl(MethodImplOptions.InternalCall)]
  249. internal static extern void Internal_RegisterCommand(IntPtr thisPtr, IntPtr commandPtr);
  250. [MethodImpl(MethodImplOptions.InternalCall)]
  251. internal static extern void Internal_PushGroup(IntPtr thisPtr, string name);
  252. [MethodImpl(MethodImplOptions.InternalCall)]
  253. internal static extern void Internal_PopGroup(IntPtr thisPtr, string name);
  254. [MethodImpl(MethodImplOptions.InternalCall)]
  255. internal static extern void Internal_Clear(IntPtr thisPtr);
  256. [MethodImpl(MethodImplOptions.InternalCall)]
  257. internal static extern void Internal_PopCommand(IntPtr thisPtr, int id);
  258. [MethodImpl(MethodImplOptions.InternalCall)]
  259. internal static extern int Internal_GetTopCommandId(IntPtr thisPtr);
  260. [MethodImpl(MethodImplOptions.InternalCall)]
  261. internal static extern SceneObject Internal_CloneSO(IntPtr soPtr, string description);
  262. [MethodImpl(MethodImplOptions.InternalCall)]
  263. internal static extern SceneObject[] Internal_CloneSOMulti(IntPtr[] soPtr, string description);
  264. [MethodImpl(MethodImplOptions.InternalCall)]
  265. internal static extern SceneObject Internal_Instantiate(IntPtr prefabPtr, string description);
  266. [MethodImpl(MethodImplOptions.InternalCall)]
  267. internal static extern SceneObject Internal_CreateSO(string name, string description);
  268. [MethodImpl(MethodImplOptions.InternalCall)]
  269. internal static extern SceneObject Internal_CreateSO2(string name, Type[] componentTypes, string description);
  270. [MethodImpl(MethodImplOptions.InternalCall)]
  271. internal static extern void Internal_DeleteSO(IntPtr soPtr, string description);
  272. [MethodImpl(MethodImplOptions.InternalCall)]
  273. internal static extern void Internal_ReparentSO(IntPtr soPtr, IntPtr parentSOPtr, string description);
  274. [MethodImpl(MethodImplOptions.InternalCall)]
  275. internal static extern void Internal_ReparentSOMulti(IntPtr[] soPtr, IntPtr parentSOPtr, string description);
  276. [MethodImpl(MethodImplOptions.InternalCall)]
  277. internal static extern void Internal_BreakPrefab(IntPtr soPtr, string description);
  278. }
  279. /** @} */
  280. }