HeavenStudioPlus/Assets/Scripts/LevelEditor/Commands/CommandManager.cs

75 lines
1.7 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2022-03-14 14:21:05 +00:00
using HeavenStudio.Editor.Commands;
2022-03-14 14:21:05 +00:00
namespace HeavenStudio.Editor
{
public class CommandManager : MonoBehaviour
{
private Stack<IAction> historyStack = new Stack<IAction>();
private Stack<IAction> redoHistoryStack = new Stack<IAction>();
int maxItems = 128;
public bool canUndo()
{
return historyStack.Count > 0;
}
public bool canRedo()
{
return redoHistoryStack.Count > 0;
}
public static CommandManager instance { get; private set; }
private void Awake()
{
instance = this;
}
public void Execute(IAction action)
{
action.Execute();
historyStack.Push(action);
redoHistoryStack.Clear();
}
public void Undo()
{
2022-01-29 02:26:34 +00:00
if (!canUndo() || Conductor.instance.NotStopped()) return;
if (historyStack.Count > 0)
{
redoHistoryStack.Push(historyStack.Peek());
historyStack.Pop().Undo();
}
}
public void Redo()
{
2022-01-29 02:26:34 +00:00
if (!canRedo() || Conductor.instance.NotStopped()) return;
if (redoHistoryStack.Count > 0)
{
historyStack.Push(redoHistoryStack.Peek());
redoHistoryStack.Pop().Redo();
}
}
// this is here as to not hog up memory, "max undos" basically
private void EnsureCapacity()
{
if (maxItems > 0)
{
}
}
2022-01-30 12:03:37 +00:00
public void Clear()
{
historyStack.Clear();
redoHistoryStack.Clear();
}
}
}