UndoRedo.cs 8.3 KB

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