UndoRedo.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. 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. /// Records a state of the entire scene object at a specific point and allows you to restore it to its original
  107. /// values as needed. Undo operation recorded in global undo/redo stack.
  108. /// </summary>
  109. /// <param name="so">Scene object to record.</param>
  110. /// <param name="recordHierarchy">If true all children of this object will also be recorded.</param>
  111. /// <param name="description">Optional description of what exactly the command does.</param>
  112. public static void RecordSO(SceneObject so, bool recordHierarchy = false, string description = "")
  113. {
  114. if (so != null)
  115. Internal_RecordSO(so.GetCachedPtr(), recordHierarchy, description);
  116. }
  117. /// <summary>
  118. /// Creates new scene object(s) by cloning existing objects. Undo operation recorded in global undo/redo stack.
  119. /// </summary>
  120. /// <param name="so">Scene object(s) to clone.</param>
  121. /// <param name="description">Optional description of what exactly the command does.</param>
  122. /// <returns>Cloned scene objects.</returns>
  123. public static SceneObject[] CloneSO(SceneObject[] so, string description = "")
  124. {
  125. if (so != null)
  126. {
  127. List<IntPtr> soPtrs = new List<IntPtr>();
  128. for (int i = 0; i < so.Length; i++)
  129. {
  130. if(so[i] != null)
  131. soPtrs.Add(so[i].GetCachedPtr());
  132. }
  133. return Internal_CloneSOMulti(soPtrs.ToArray(), description);
  134. }
  135. return new SceneObject[0];
  136. }
  137. /// <summary>
  138. /// Creates new a scene object by cloning an existing object. Undo operation recorded in global undo/redo stack.
  139. /// </summary>
  140. /// <param name="so">Scene object to clone.</param>
  141. /// <param name="description">Optional description of what exactly the command does.</param>
  142. /// <returns>Cloned scene object.</returns>
  143. public static SceneObject CloneSO(SceneObject so, string description = "")
  144. {
  145. if (so != null)
  146. return Internal_CloneSO(so.GetCachedPtr(), description);
  147. return null;
  148. }
  149. /// <summary>
  150. /// Instantiates scene object(s) from a prefab. Undo operation recorded in global undo/redo stack.
  151. /// </summary>
  152. /// <param name="prefab">Prefab to instantiate.</param>
  153. /// <param name="description">Optional description of what exactly the command does.</param>
  154. /// <returns>Instantiated scene object.</returns>
  155. public static SceneObject Instantiate(Prefab prefab, string description = "")
  156. {
  157. if (prefab != null)
  158. return Internal_Instantiate(prefab.GetCachedPtr(), description);
  159. return null;
  160. }
  161. /// <summary>
  162. /// Creates a new scene object. Undo operation recorded in global undo/redo stack.
  163. /// </summary>
  164. /// <param name="name">Name of the scene object.</param>
  165. /// <param name="description">Optional description of what exactly the command does.</param>
  166. /// <returns>Newly created scene object.</returns>
  167. public static SceneObject CreateSO(string name, string description = "")
  168. {
  169. return Internal_CreateSO(name, description);
  170. }
  171. /// <summary>
  172. /// Deletes a scene object. Undo operation recorded in global undo/redo stack.
  173. /// </summary>
  174. /// <param name="so">Scene object to delete.</param>
  175. /// <param name="description">Optional description of what exactly the command does.</param>
  176. public static void DeleteSO(SceneObject so, string description = "")
  177. {
  178. if (so != null)
  179. Internal_DeleteSO(so.GetCachedPtr(), description);
  180. }
  181. /// <summary>
  182. /// Changes the parent of the scene object. Undo operation recorded in global undo/redo stack.
  183. /// </summary>
  184. /// <param name="so">Scene object to change the parent of.</param>
  185. /// <param name="parent">New parent.</param>
  186. /// <param name="description">Optional description of what exactly the command does.</param>
  187. public static void ReparentSO(SceneObject so, SceneObject parent, string description = "")
  188. {
  189. if (so != null)
  190. {
  191. IntPtr parentPtr = IntPtr.Zero;
  192. if (parent != null)
  193. parentPtr = parent.GetCachedPtr();
  194. Internal_ReparentSO(so.GetCachedPtr(), parentPtr, description);
  195. }
  196. }
  197. /// <summary>
  198. /// Changes the parent of a set of scene objects. Undo operation recorded in global undo/redo stack.
  199. /// </summary>
  200. /// <param name="so">Scene objects to change the parent of.</param>
  201. /// <param name="parent">New parent.</param>
  202. /// <param name="description">Optional description of what exactly the command does.</param>
  203. public static void ReparentSO(SceneObject[] so, SceneObject parent, string description = "")
  204. {
  205. if (so != null)
  206. {
  207. List<IntPtr> soPtrs = new List<IntPtr>();
  208. for (int i = 0; i < so.Length; i++)
  209. {
  210. if (so[i] != null)
  211. soPtrs.Add(so[i].GetCachedPtr());
  212. }
  213. if (soPtrs.Count > 0)
  214. {
  215. IntPtr parentPtr = IntPtr.Zero;
  216. if (parent != null)
  217. parentPtr = parent.GetCachedPtr();
  218. Internal_ReparentSOMulti(soPtrs.ToArray(), parentPtr, description);
  219. }
  220. }
  221. }
  222. /// <summary>
  223. /// Breaks the prefab link on the provided scene object and makes the operation undo-able. Undo operation recorded
  224. /// in global undo/redo stack.
  225. /// See <see cref="PrefabUtility.BreakPrefab"/>.
  226. /// </summary>
  227. /// <param name="so">Scene object whose prefab link to break.</param>
  228. /// <param name="description">Optional description of what exactly the command does.</param>
  229. public static void BreakPrefab(SceneObject so, string description = "")
  230. {
  231. if (so != null)
  232. Internal_BreakPrefab(so.GetCachedPtr(), description);
  233. }
  234. /// <summary>
  235. /// Used by the runtime to set the global undo/redo stack.
  236. /// </summary>
  237. /// <param name="global">Instance of the global undo/redo stack.</param>
  238. private static void Internal_SetGlobal(UndoRedo global)
  239. {
  240. // We can't set this directly through the field because there is an issue with Mono and static fields
  241. UndoRedo.global = global;
  242. }
  243. [MethodImpl(MethodImplOptions.InternalCall)]
  244. internal static extern void Internal_CreateInstance(UndoRedo instance);
  245. [MethodImpl(MethodImplOptions.InternalCall)]
  246. internal static extern void Internal_Undo(IntPtr thisPtr);
  247. [MethodImpl(MethodImplOptions.InternalCall)]
  248. internal static extern void Internal_Redo(IntPtr thisPtr);
  249. [MethodImpl(MethodImplOptions.InternalCall)]
  250. internal static extern void Internal_RegisterCommand(IntPtr thisPtr, IntPtr commandPtr);
  251. [MethodImpl(MethodImplOptions.InternalCall)]
  252. internal static extern void Internal_PushGroup(IntPtr thisPtr, string name);
  253. [MethodImpl(MethodImplOptions.InternalCall)]
  254. internal static extern void Internal_PopGroup(IntPtr thisPtr, string name);
  255. [MethodImpl(MethodImplOptions.InternalCall)]
  256. internal static extern void Internal_Clear(IntPtr thisPtr);
  257. [MethodImpl(MethodImplOptions.InternalCall)]
  258. internal static extern void Internal_PopCommand(IntPtr thisPtr, int id);
  259. [MethodImpl(MethodImplOptions.InternalCall)]
  260. internal static extern int Internal_GetTopCommandId(IntPtr thisPtr);
  261. [MethodImpl(MethodImplOptions.InternalCall)]
  262. internal static extern void Internal_RecordSO(IntPtr soPtr, bool recordHierarchy, string description);
  263. [MethodImpl(MethodImplOptions.InternalCall)]
  264. internal static extern SceneObject Internal_CloneSO(IntPtr soPtr, string description);
  265. [MethodImpl(MethodImplOptions.InternalCall)]
  266. internal static extern SceneObject[] Internal_CloneSOMulti(IntPtr[] soPtr, string description);
  267. [MethodImpl(MethodImplOptions.InternalCall)]
  268. internal static extern SceneObject Internal_Instantiate(IntPtr prefabPtr, string description);
  269. [MethodImpl(MethodImplOptions.InternalCall)]
  270. internal static extern SceneObject Internal_CreateSO(string name, string description);
  271. [MethodImpl(MethodImplOptions.InternalCall)]
  272. internal static extern void Internal_DeleteSO(IntPtr soPtr, string description);
  273. [MethodImpl(MethodImplOptions.InternalCall)]
  274. internal static extern void Internal_ReparentSO(IntPtr soPtr, IntPtr parentSOPtr, string description);
  275. [MethodImpl(MethodImplOptions.InternalCall)]
  276. internal static extern void Internal_ReparentSOMulti(IntPtr[] soPtr, IntPtr parentSOPtr, string description);
  277. [MethodImpl(MethodImplOptions.InternalCall)]
  278. internal static extern void Internal_BreakPrefab(IntPtr soPtr, string description);
  279. }
  280. /** @} */
  281. }