UndoRedo.cs 10 KB

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