UndoRedo.cs 8.5 KB

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