UndoRedo.cs 11 KB

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