UndoRedo.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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, "", 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, true)]
  35. [ToolbarItem("Redo", ToolbarIcon.Redo, "", 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="description">Optional description of what exactly the command does.</param>
  46. public static void RecordSO(SceneObject so, string description = "")
  47. {
  48. if (so != null)
  49. Internal_RecordSO(so.GetCachedPtr(), description);
  50. }
  51. /// <summary>
  52. /// Creates new scene object(s) by cloning existing objects.
  53. /// </summary>
  54. /// <param name="so">Scene object(s) to clone.</param>
  55. /// <param name="description">Optional description of what exactly the command does.</param>
  56. /// <returns>Cloned scene objects.</returns>
  57. public static SceneObject[] CloneSO(SceneObject[] so, string description = "")
  58. {
  59. if (so != null)
  60. {
  61. List<IntPtr> soPtrs = new List<IntPtr>();
  62. for (int i = 0; i < so.Length; i++)
  63. {
  64. if(so[i] != null)
  65. soPtrs.Add(so[i].GetCachedPtr());
  66. }
  67. return Internal_CloneSOMulti(soPtrs.ToArray(), description);
  68. }
  69. return new SceneObject[0];
  70. }
  71. /// <summary>
  72. /// Creates new a scene object by cloning an existing object.
  73. /// </summary>
  74. /// <param name="so">Scene object to clone.</param>
  75. /// <param name="description">Optional description of what exactly the command does.</param>
  76. /// <returns>Cloned scene object.</returns>
  77. public static SceneObject CloneSO(SceneObject so, string description = "")
  78. {
  79. if (so != null)
  80. return Internal_CloneSO(so.GetCachedPtr(), description);
  81. return null;
  82. }
  83. /// <summary>
  84. /// Instantiates scene object(s) from a prefab.
  85. /// </summary>
  86. /// <param name="prefab">Prefab to instantiate.</param>
  87. /// <param name="description">Optional description of what exactly the command does.</param>
  88. /// <returns>Instantiated scene object.</returns>
  89. public static SceneObject Instantiate(Prefab prefab, string description = "")
  90. {
  91. if (prefab != null)
  92. return Internal_Instantiate(prefab.GetCachedPtr(), description);
  93. return null;
  94. }
  95. /// <summary>
  96. /// Creates a new scene object.
  97. /// </summary>
  98. /// <param name="name">Name of the scene object.</param>
  99. /// <param name="description">Optional description of what exactly the command does.</param>
  100. /// <returns>Newly created scene object.</returns>
  101. public static SceneObject CreateSO(string name, string description = "")
  102. {
  103. return Internal_CreateSO(name, description);
  104. }
  105. /// <summary>
  106. /// Deletes a scene object.
  107. /// </summary>
  108. /// <param name="so">Scene object to delete.</param>
  109. /// <param name="description">Optional description of what exactly the command does.</param>
  110. public static void DeleteSO(SceneObject so, string description = "")
  111. {
  112. if (so != null)
  113. Internal_DeleteSO(so.GetCachedPtr(), description);
  114. }
  115. /// <summary>
  116. /// Changes the parent of the scene object.
  117. /// </summary>
  118. /// <param name="so">Scene object to change the parent of.</param>
  119. /// <param name="parent">New parent.</param>
  120. /// <param name="description">Optional description of what exactly the command does.</param>
  121. public static void ReparentSO(SceneObject so, SceneObject parent, string description = "")
  122. {
  123. if (so != null)
  124. {
  125. IntPtr parentPtr = IntPtr.Zero;
  126. if (parent != null)
  127. parentPtr = parent.GetCachedPtr();
  128. Internal_ReparentSO(so.GetCachedPtr(), parentPtr, description);
  129. }
  130. }
  131. /// <summary>
  132. /// Changes the parent of a set of scene objects.
  133. /// </summary>
  134. /// <param name="so">Scene objects to change the parent of.</param>
  135. /// <param name="parent">New parent.</param>
  136. /// <param name="description">Optional description of what exactly the command does.</param>
  137. public static void ReparentSO(SceneObject[] so, SceneObject parent, string description = "")
  138. {
  139. if (so != null)
  140. {
  141. List<IntPtr> soPtrs = new List<IntPtr>();
  142. for (int i = 0; i < so.Length; i++)
  143. {
  144. if (so[i] != null)
  145. soPtrs.Add(so[i].GetCachedPtr());
  146. }
  147. if (soPtrs.Count > 0)
  148. {
  149. IntPtr parentPtr = IntPtr.Zero;
  150. if (parent != null)
  151. parentPtr = parent.GetCachedPtr();
  152. Internal_ReparentSOMulti(soPtrs.ToArray(), parentPtr, description);
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// Creates a new undo/redo group. All new commands will be registered to this group. You may remove the group and
  158. /// all of its commands by calling <see cref="PopGroup"/>.
  159. /// </summary>
  160. /// <param name="name">Unique name of the group.</param>
  161. public static void PushGroup(string name)
  162. {
  163. Internal_PushGroup(name);
  164. }
  165. /// <summary>
  166. /// Removes all the command registered to the current undo/redo group.
  167. /// </summary>
  168. /// <param name="name">Unique name of the group.</param>
  169. public static void PopGroup(string name)
  170. {
  171. Internal_PopGroup(name);
  172. }
  173. /// <summary>
  174. /// Removes a command with the specified identifier from undo/redo stack without executing it.
  175. /// </summary>
  176. /// <param name="id">Identifier of the command as returned by <see cref="GetTopCommandId"/></param>
  177. public static void PopCommand(int id)
  178. {
  179. Internal_PopCommand(id);
  180. }
  181. [MethodImpl(MethodImplOptions.InternalCall)]
  182. internal static extern void Internal_Undo();
  183. [MethodImpl(MethodImplOptions.InternalCall)]
  184. internal static extern void Internal_Redo();
  185. [MethodImpl(MethodImplOptions.InternalCall)]
  186. internal static extern void Internal_PushGroup(string name);
  187. [MethodImpl(MethodImplOptions.InternalCall)]
  188. internal static extern void Internal_PopGroup(string name);
  189. [MethodImpl(MethodImplOptions.InternalCall)]
  190. internal static extern void Internal_PopCommand(int id);
  191. [MethodImpl(MethodImplOptions.InternalCall)]
  192. internal static extern int Internal_GetTopCommandId();
  193. [MethodImpl(MethodImplOptions.InternalCall)]
  194. internal static extern void Internal_RecordSO(IntPtr soPtr, string description);
  195. [MethodImpl(MethodImplOptions.InternalCall)]
  196. internal static extern SceneObject Internal_CloneSO(IntPtr soPtr, string description);
  197. [MethodImpl(MethodImplOptions.InternalCall)]
  198. internal static extern SceneObject[] Internal_CloneSOMulti(IntPtr[] soPtr, string description);
  199. [MethodImpl(MethodImplOptions.InternalCall)]
  200. internal static extern SceneObject Internal_Instantiate(IntPtr prefabPtr, string description);
  201. [MethodImpl(MethodImplOptions.InternalCall)]
  202. internal static extern SceneObject Internal_CreateSO(string name, string description);
  203. [MethodImpl(MethodImplOptions.InternalCall)]
  204. internal static extern void Internal_DeleteSO(IntPtr soPtr, string description);
  205. [MethodImpl(MethodImplOptions.InternalCall)]
  206. internal static extern void Internal_ReparentSO(IntPtr soPtr, IntPtr parentSOPtr, string description);
  207. [MethodImpl(MethodImplOptions.InternalCall)]
  208. internal static extern void Internal_ReparentSOMulti(IntPtr[] soPtr, IntPtr parentSOPtr, string description);
  209. }
  210. }