UndoRedo.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. /// </summary>
  16. public static class UndoRedo
  17. {
  18. /// <summary>
  19. /// Returns the unique identifier of the command currently at the top of the undo stack.
  20. /// </summary>
  21. public static int TopCommandId
  22. {
  23. get { return Internal_GetTopCommandId(); }
  24. }
  25. /// <summary>
  26. /// Executes the last command on the undo stack, undoing its operations.
  27. /// </summary>
  28. [MenuItem("Edit/Undo", 9500, true)]
  29. [ToolbarItem("Undo", ToolbarIcon.Undo, "Undo (Ctrl + Z)", 1900, true)]
  30. public static void Undo()
  31. {
  32. Internal_Undo();
  33. }
  34. /// <summary>
  35. /// Executes the last command on the redo stack (last command we called undo on), re-applying its operation.
  36. /// </summary>
  37. [MenuItem("Edit/Redo", 9499)]
  38. [ToolbarItem("Redo", ToolbarIcon.Redo, "Redo (Ctrl + Y)", 1899)]
  39. public static void Redo()
  40. {
  41. Internal_Redo();
  42. }
  43. /// <summary>
  44. /// Records a state of the entire scene object at a specific point and allows you to restore it to its original
  45. /// values as needed.
  46. /// </summary>
  47. /// <param name="so">Scene object to record.</param>
  48. /// <param name="recordHierarchy">If true all children of this object will also be recorded.</param>
  49. /// <param name="description">Optional description of what exactly the command does.</param>
  50. public static void RecordSO(SceneObject so, bool recordHierarchy = false, string description = "")
  51. {
  52. if (so != null)
  53. Internal_RecordSO(so.GetCachedPtr(), description);
  54. }
  55. /// <summary>
  56. /// Creates new scene object(s) by cloning existing objects.
  57. /// </summary>
  58. /// <param name="so">Scene object(s) to clone.</param>
  59. /// <param name="description">Optional description of what exactly the command does.</param>
  60. /// <returns>Cloned scene objects.</returns>
  61. public static SceneObject[] CloneSO(SceneObject[] so, string description = "")
  62. {
  63. if (so != null)
  64. {
  65. List<IntPtr> soPtrs = new List<IntPtr>();
  66. for (int i = 0; i < so.Length; i++)
  67. {
  68. if(so[i] != null)
  69. soPtrs.Add(so[i].GetCachedPtr());
  70. }
  71. return Internal_CloneSOMulti(soPtrs.ToArray(), description);
  72. }
  73. return new SceneObject[0];
  74. }
  75. /// <summary>
  76. /// Creates new a scene object by cloning an existing object.
  77. /// </summary>
  78. /// <param name="so">Scene object to clone.</param>
  79. /// <param name="description">Optional description of what exactly the command does.</param>
  80. /// <returns>Cloned scene object.</returns>
  81. public static SceneObject CloneSO(SceneObject so, string description = "")
  82. {
  83. if (so != null)
  84. return Internal_CloneSO(so.GetCachedPtr(), description);
  85. return null;
  86. }
  87. /// <summary>
  88. /// Instantiates scene object(s) from a prefab.
  89. /// </summary>
  90. /// <param name="prefab">Prefab to instantiate.</param>
  91. /// <param name="description">Optional description of what exactly the command does.</param>
  92. /// <returns>Instantiated scene object.</returns>
  93. public static SceneObject Instantiate(Prefab prefab, string description = "")
  94. {
  95. if (prefab != null)
  96. return Internal_Instantiate(prefab.GetCachedPtr(), description);
  97. return null;
  98. }
  99. /// <summary>
  100. /// Creates a new scene object.
  101. /// </summary>
  102. /// <param name="name">Name of the scene object.</param>
  103. /// <param name="description">Optional description of what exactly the command does.</param>
  104. /// <returns>Newly created scene object.</returns>
  105. public static SceneObject CreateSO(string name, string description = "")
  106. {
  107. return Internal_CreateSO(name, description);
  108. }
  109. /// <summary>
  110. /// Deletes a scene object.
  111. /// </summary>
  112. /// <param name="so">Scene object to delete.</param>
  113. /// <param name="description">Optional description of what exactly the command does.</param>
  114. public static void DeleteSO(SceneObject so, string description = "")
  115. {
  116. if (so != null)
  117. Internal_DeleteSO(so.GetCachedPtr(), description);
  118. }
  119. /// <summary>
  120. /// Changes the parent of the scene object.
  121. /// </summary>
  122. /// <param name="so">Scene object to change the parent of.</param>
  123. /// <param name="parent">New parent.</param>
  124. /// <param name="description">Optional description of what exactly the command does.</param>
  125. public static void ReparentSO(SceneObject so, SceneObject parent, string description = "")
  126. {
  127. if (so != null)
  128. {
  129. IntPtr parentPtr = IntPtr.Zero;
  130. if (parent != null)
  131. parentPtr = parent.GetCachedPtr();
  132. Internal_ReparentSO(so.GetCachedPtr(), parentPtr, description);
  133. }
  134. }
  135. /// <summary>
  136. /// Changes the parent of a set of scene objects.
  137. /// </summary>
  138. /// <param name="so">Scene objects to change the parent of.</param>
  139. /// <param name="parent">New parent.</param>
  140. /// <param name="description">Optional description of what exactly the command does.</param>
  141. public static void ReparentSO(SceneObject[] so, SceneObject parent, string description = "")
  142. {
  143. if (so != null)
  144. {
  145. List<IntPtr> soPtrs = new List<IntPtr>();
  146. for (int i = 0; i < so.Length; i++)
  147. {
  148. if (so[i] != null)
  149. soPtrs.Add(so[i].GetCachedPtr());
  150. }
  151. if (soPtrs.Count > 0)
  152. {
  153. IntPtr parentPtr = IntPtr.Zero;
  154. if (parent != null)
  155. parentPtr = parent.GetCachedPtr();
  156. Internal_ReparentSOMulti(soPtrs.ToArray(), parentPtr, description);
  157. }
  158. }
  159. }
  160. /// <summary>
  161. /// Breaks the prefab link on the provided scene object and makes the operation undo-able.
  162. /// See <see cref="PrefabUtility.BreakPrefab"/>.
  163. /// </summary>
  164. /// <param name="so">Scene object whose prefab link to break.</param>
  165. /// <param name="description">Optional description of what exactly the command does.</param>
  166. public static void BreakPrefab(SceneObject so, string description = "")
  167. {
  168. if (so != null)
  169. Internal_BreakPrefab(so.GetCachedPtr(), description);
  170. }
  171. /// <summary>
  172. /// Creates a new undo/redo group. All new commands will be registered to this group. You may remove the group and
  173. /// all of its commands by calling <see cref="PopGroup"/>.
  174. /// </summary>
  175. /// <param name="name">Unique name of the group.</param>
  176. public static void PushGroup(string name)
  177. {
  178. Internal_PushGroup(name);
  179. }
  180. /// <summary>
  181. /// Removes all the command registered to the current undo/redo group.
  182. /// </summary>
  183. /// <param name="name">Unique name of the group.</param>
  184. public static void PopGroup(string name)
  185. {
  186. Internal_PopGroup(name);
  187. }
  188. /// <summary>
  189. /// Removes a command with the specified identifier from undo/redo stack without executing it.
  190. /// </summary>
  191. /// <param name="id">Identifier of the command as returned by <see cref="GetTopCommandId"/></param>
  192. public static void PopCommand(int id)
  193. {
  194. Internal_PopCommand(id);
  195. }
  196. [MethodImpl(MethodImplOptions.InternalCall)]
  197. internal static extern void Internal_Undo();
  198. [MethodImpl(MethodImplOptions.InternalCall)]
  199. internal static extern void Internal_Redo();
  200. [MethodImpl(MethodImplOptions.InternalCall)]
  201. internal static extern void Internal_PushGroup(string name);
  202. [MethodImpl(MethodImplOptions.InternalCall)]
  203. internal static extern void Internal_PopGroup(string name);
  204. [MethodImpl(MethodImplOptions.InternalCall)]
  205. internal static extern void Internal_PopCommand(int id);
  206. [MethodImpl(MethodImplOptions.InternalCall)]
  207. internal static extern int Internal_GetTopCommandId();
  208. [MethodImpl(MethodImplOptions.InternalCall)]
  209. internal static extern void Internal_RecordSO(IntPtr soPtr, string description);
  210. [MethodImpl(MethodImplOptions.InternalCall)]
  211. internal static extern SceneObject Internal_CloneSO(IntPtr soPtr, string description);
  212. [MethodImpl(MethodImplOptions.InternalCall)]
  213. internal static extern SceneObject[] Internal_CloneSOMulti(IntPtr[] soPtr, string description);
  214. [MethodImpl(MethodImplOptions.InternalCall)]
  215. internal static extern SceneObject Internal_Instantiate(IntPtr prefabPtr, string description);
  216. [MethodImpl(MethodImplOptions.InternalCall)]
  217. internal static extern SceneObject Internal_CreateSO(string name, string description);
  218. [MethodImpl(MethodImplOptions.InternalCall)]
  219. internal static extern void Internal_DeleteSO(IntPtr soPtr, string description);
  220. [MethodImpl(MethodImplOptions.InternalCall)]
  221. internal static extern void Internal_ReparentSO(IntPtr soPtr, IntPtr parentSOPtr, string description);
  222. [MethodImpl(MethodImplOptions.InternalCall)]
  223. internal static extern void Internal_ReparentSOMulti(IntPtr[] soPtr, IntPtr parentSOPtr, string description);
  224. [MethodImpl(MethodImplOptions.InternalCall)]
  225. internal static extern void Internal_BreakPrefab(IntPtr soPtr, string description);
  226. }
  227. /** @} */
  228. }