using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using BansheeEngine;
namespace BansheeEditor
{
///
/// Provides functionality to undo or redo recently performed operations in the editor.
///
public static class UndoRedo
{
///
/// Returns the unique identifier of the command currently at the top of the undo stack.
///
public static int TopCommandId
{
get { return Internal_GetTopCommandId(); }
}
///
/// Executes the last command on the undo stack, undoing its operations.
///
[MenuItem("Edit/Undo", 9500, true)]
[ToolbarItem("Undo", ToolbarIcon.Undo, "Undo (Ctrl + Z)", 1900, true)]
public static void Undo()
{
Internal_Undo();
}
///
/// Executes the last command on the redo stack (last command we called undo on), re-applying its operation.
///
[MenuItem("Edit/Redo", 9499)]
[ToolbarItem("Redo", ToolbarIcon.Redo, "Redo (Ctrl + Y)", 1899)]
public static void Redo()
{
Internal_Redo();
}
///
/// Records a state of the entire scene object at a specific point and allows you to restore it to its original
/// values as needed.
///
/// Scene object to record.
/// If true all children of this object will also be recorded.
/// Optional description of what exactly the command does.
public static void RecordSO(SceneObject so, bool recordHierarchy = false, string description = "")
{
if (so != null)
Internal_RecordSO(so.GetCachedPtr(), description);
}
///
/// Creates new scene object(s) by cloning existing objects.
///
/// Scene object(s) to clone.
/// Optional description of what exactly the command does.
/// Cloned scene objects.
public static SceneObject[] CloneSO(SceneObject[] so, string description = "")
{
if (so != null)
{
List soPtrs = new List();
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];
}
///
/// Creates new a scene object by cloning an existing object.
///
/// Scene object to clone.
/// Optional description of what exactly the command does.
/// Cloned scene object.
public static SceneObject CloneSO(SceneObject so, string description = "")
{
if (so != null)
return Internal_CloneSO(so.GetCachedPtr(), description);
return null;
}
///
/// Instantiates scene object(s) from a prefab.
///
/// Prefab to instantiate.
/// Optional description of what exactly the command does.
/// Instantiated scene object.
public static SceneObject Instantiate(Prefab prefab, string description = "")
{
if (prefab != null)
return Internal_Instantiate(prefab.GetCachedPtr(), description);
return null;
}
///
/// Creates a new scene object.
///
/// Name of the scene object.
/// Optional description of what exactly the command does.
/// Newly created scene object.
public static SceneObject CreateSO(string name, string description = "")
{
return Internal_CreateSO(name, description);
}
///
/// Deletes a scene object.
///
/// Scene object to delete.
/// Optional description of what exactly the command does.
public static void DeleteSO(SceneObject so, string description = "")
{
if (so != null)
Internal_DeleteSO(so.GetCachedPtr(), description);
}
///
/// Changes the parent of the scene object.
///
/// Scene object to change the parent of.
/// New parent.
/// Optional description of what exactly the command does.
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);
}
}
///
/// Changes the parent of a set of scene objects.
///
/// Scene objects to change the parent of.
/// New parent.
/// Optional description of what exactly the command does.
public static void ReparentSO(SceneObject[] so, SceneObject parent, string description = "")
{
if (so != null)
{
List soPtrs = new List();
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);
}
}
}
///
/// Breaks the prefab link on the provided scene object and makes the operation undo-able.
/// See .
///
/// Scene object whose prefab link to break.
/// Optional description of what exactly the command does.
public static void BreakPrefab(SceneObject so, string description = "")
{
if (so != null)
Internal_BreakPrefab(so.GetCachedPtr(), description);
}
///
/// 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 .
///
/// Unique name of the group.
public static void PushGroup(string name)
{
Internal_PushGroup(name);
}
///
/// Removes all the command registered to the current undo/redo group.
///
/// Unique name of the group.
public static void PopGroup(string name)
{
Internal_PopGroup(name);
}
///
/// Removes a command with the specified identifier from undo/redo stack without executing it.
///
/// Identifier of the command as returned by
public static void PopCommand(int id)
{
Internal_PopCommand(id);
}
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Internal_Undo();
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Internal_Redo();
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Internal_PushGroup(string name);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Internal_PopGroup(string name);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Internal_PopCommand(int id);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern int Internal_GetTopCommandId();
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Internal_RecordSO(IntPtr soPtr, string description);
[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);
}
}