| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- using System;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using bs;
- namespace bs.Editor
- {
- /** @addtogroup Utility-Editor
- * @{
- */
- /// <summary>
- /// Provides functionality to undo or redo recently performed operations in the editor. All commands executed from this
- /// class are undoable/redoable.
- ///
- /// The class provides static methods that access the global undo/redo stack, but can also be instantiated to provide
- /// local undo/redo stacks.
- /// </summary>
- public class UndoRedo : ScriptObject
- {
- private static UndoRedo global;
- /// <summary>
- /// Constructor for internal runtime use.
- /// </summary>
- /// <param name="dummy">Dummy parameter to distinguish from public constructor.</param>
- private UndoRedo(bool dummy)
- { }
- /// <summary>
- /// Creates a new undo/redo stack.
- /// </summary>
- public UndoRedo()
- {
- Internal_CreateInstance(this);
- }
- /// <summary>
- /// Returns the global undo/redo stack.
- /// </summary>
- public static UndoRedo Global
- {
- get { return global; }
- }
- /// <summary>
- /// Returns the unique identifier of the command currently at the top of the undo stack.
- /// </summary>
- public int TopCommandId
- {
- get { return Internal_GetTopCommandId(mCachedPtr); }
- }
- /// <summary>
- /// Executes the last command on the undo stack, undoing its operations.
- /// </summary>
- public void Undo()
- {
- Internal_Undo(mCachedPtr);
- }
- /// <summary>
- /// Executes the last command on the redo stack (last command we called undo on), re-applying its operation.
- /// </summary>
- public void Redo()
- {
- Internal_Redo(mCachedPtr);
- }
- /// <summary>
- /// Registers a new undo command.
- /// </summary>
- /// <param name="command">Command to register</param>
- public void RegisterCommand(UndoableCommand command)
- {
- if (command == null)
- return;
- Internal_RegisterCommand(mCachedPtr, command.GetCachedPtr());
- }
- /// <summary>
- /// Creates a new undo/redo group. All new commands will be registered to this group. You may remove the group and
- /// all of its commands by calling <see cref="PopGroup"/>.
- /// </summary>
- /// <param name="name">Unique name of the group.</param>
- public void PushGroup(string name)
- {
- Internal_PushGroup(mCachedPtr, name);
- }
- /// <summary>
- /// Removes all the command registered to the current undo/redo group.
- /// </summary>
- /// <param name="name">Unique name of the group.</param>
- public void PopGroup(string name)
- {
- Internal_PopGroup(mCachedPtr, name);
- }
- /// <summary>
- /// Removes a command with the specified identifier from undo/redo stack without executing it.
- /// </summary>
- /// <param name="id">Identifier of the command as returned by <see cref="TopCommandId"/></param>
- public void PopCommand(int id)
- {
- Internal_PopCommand(mCachedPtr, id);
- }
- /// <summary>
- /// Clears all undo/redo commands from the stack.
- /// </summary>
- public void Clear()
- {
- Internal_Clear(mCachedPtr);
- }
- /// <summary>
- /// Creates new scene object(s) by cloning existing objects. Undo operation recorded in global undo/redo stack.
- /// </summary>
- /// <param name="so">Scene object(s) to clone.</param>
- /// <param name="description">Optional description of what exactly the command does.</param>
- /// <returns>Cloned scene objects.</returns>
- public static SceneObject[] CloneSO(SceneObject[] so, string description = "")
- {
- if (so != null)
- {
- List<IntPtr> soPtrs = new List<IntPtr>();
- for (int i = 0; i < so.Length; i++)
- {
- if(so[i] != null)
- soPtrs.Add(so[i].GetCachedPtr());
- }
- return Internal_CloneSOMulti(soPtrs.ToArray(), description);
- }
- return new SceneObject[0];
- }
- /// <summary>
- /// Creates new a scene object by cloning an existing object. Undo operation recorded in global undo/redo stack.
- /// </summary>
- /// <param name="so">Scene object to clone.</param>
- /// <param name="description">Optional description of what exactly the command does.</param>
- /// <returns>Cloned scene object.</returns>
- public static SceneObject CloneSO(SceneObject so, string description = "")
- {
- if (so != null)
- return Internal_CloneSO(so.GetCachedPtr(), description);
- return null;
- }
- /// <summary>
- /// Instantiates scene object(s) from a prefab. Undo operation recorded in global undo/redo stack.
- /// </summary>
- /// <param name="prefab">Prefab to instantiate.</param>
- /// <param name="description">Optional description of what exactly the command does.</param>
- /// <returns>Instantiated scene object.</returns>
- public static SceneObject Instantiate(Prefab prefab, string description = "")
- {
- if (prefab != null)
- return Internal_Instantiate(prefab.GetCachedPtr(), description);
- return null;
- }
- /// <summary>
- /// Creates a new scene object. Undo operation recorded in global undo/redo stack.
- /// </summary>
- /// <param name="name">Name of the scene object.</param>
- /// <param name="description">Optional description of what exactly the command does.</param>
- /// <returns>Newly created scene object.</returns>
- public static SceneObject CreateSO(string name, string description = "")
- {
- return Internal_CreateSO(name, description);
- }
- /// <summary>
- /// Deletes a scene object. Undo operation recorded in global undo/redo stack.
- /// </summary>
- /// <param name="so">Scene object to delete.</param>
- /// <param name="description">Optional description of what exactly the command does.</param>
- public static void DeleteSO(SceneObject so, string description = "")
- {
- if (so != null)
- Internal_DeleteSO(so.GetCachedPtr(), description);
- }
- /// <summary>
- /// Changes the parent of the scene object. Undo operation recorded in global undo/redo stack.
- /// </summary>
- /// <param name="so">Scene object to change the parent of.</param>
- /// <param name="parent">New parent.</param>
- /// <param name="description">Optional description of what exactly the command does.</param>
- public static void ReparentSO(SceneObject so, SceneObject parent, string description = "")
- {
- if (so != null)
- {
- IntPtr parentPtr = IntPtr.Zero;
- if (parent != null)
- parentPtr = parent.GetCachedPtr();
- Internal_ReparentSO(so.GetCachedPtr(), parentPtr, description);
- }
- }
- /// <summary>
- /// Changes the parent of a set of scene objects. Undo operation recorded in global undo/redo stack.
- /// </summary>
- /// <param name="so">Scene objects to change the parent of.</param>
- /// <param name="parent">New parent.</param>
- /// <param name="description">Optional description of what exactly the command does.</param>
- public static void ReparentSO(SceneObject[] so, SceneObject parent, string description = "")
- {
- if (so != null)
- {
- List<IntPtr> soPtrs = new List<IntPtr>();
- for (int i = 0; i < so.Length; i++)
- {
- if (so[i] != null)
- soPtrs.Add(so[i].GetCachedPtr());
- }
- if (soPtrs.Count > 0)
- {
- IntPtr parentPtr = IntPtr.Zero;
- if (parent != null)
- parentPtr = parent.GetCachedPtr();
- Internal_ReparentSOMulti(soPtrs.ToArray(), parentPtr, description);
- }
- }
- }
- /// <summary>
- /// Breaks the prefab link on the provided scene object and makes the operation undo-able. Undo operation recorded
- /// in global undo/redo stack.
- /// See <see cref="PrefabUtility.BreakPrefab"/>.
- /// </summary>
- /// <param name="so">Scene object whose prefab link to break.</param>
- /// <param name="description">Optional description of what exactly the command does.</param>
- public static void BreakPrefab(SceneObject so, string description = "")
- {
- if (so != null)
- Internal_BreakPrefab(so.GetCachedPtr(), description);
- }
- /// <summary>
- /// Used by the runtime to set the global undo/redo stack.
- /// </summary>
- /// <param name="global">Instance of the global undo/redo stack.</param>
- private static void Internal_SetGlobal(UndoRedo global)
- {
- // We can't set this directly through the field because there is an issue with Mono and static fields
- UndoRedo.global = global;
- }
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void Internal_CreateInstance(UndoRedo instance);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void Internal_Undo(IntPtr thisPtr);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void Internal_Redo(IntPtr thisPtr);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void Internal_RegisterCommand(IntPtr thisPtr, IntPtr commandPtr);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void Internal_PushGroup(IntPtr thisPtr, string name);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void Internal_PopGroup(IntPtr thisPtr, string name);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void Internal_Clear(IntPtr thisPtr);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void Internal_PopCommand(IntPtr thisPtr, int id);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern int Internal_GetTopCommandId(IntPtr thisPtr);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern SceneObject Internal_CloneSO(IntPtr soPtr, string description);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern SceneObject[] Internal_CloneSOMulti(IntPtr[] soPtr, string description);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern SceneObject Internal_Instantiate(IntPtr prefabPtr, string description);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern SceneObject Internal_CreateSO(string name, string description);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void Internal_DeleteSO(IntPtr soPtr, string description);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void Internal_ReparentSO(IntPtr soPtr, IntPtr parentSOPtr, string description);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void Internal_ReparentSOMulti(IntPtr[] soPtr, IntPtr parentSOPtr, string description);
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void Internal_BreakPrefab(IntPtr soPtr, string description);
- }
- /** @} */
- }
|