57 lines
1.6 KiB
C#
57 lines
1.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Ichni.Editor.Commands
|
|
{
|
|
public static class CommandManager
|
|
{
|
|
private static readonly Stack<ICommand> undoStack = new Stack<ICommand>();
|
|
private static readonly Stack<ICommand> redoStack = new Stack<ICommand>();
|
|
|
|
/// <summary>
|
|
/// 全局指令拦截与推送入口。任何受撤回保护的修改都应通过此处执行。
|
|
/// </summary>
|
|
public static void ExecuteCommand(ICommand command)
|
|
{
|
|
if (undoStack.Count > 0)
|
|
{
|
|
var topCommand = undoStack.Peek();
|
|
// 尝试拦截短时间内的连续修改并归并为一步
|
|
if (topCommand.TryMerge(command))
|
|
{
|
|
topCommand.Execute();
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 新操作彻底切断原本可能存在的重做未来
|
|
command.Execute();
|
|
undoStack.Push(command);
|
|
redoStack.Clear();
|
|
}
|
|
|
|
public static void Undo()
|
|
{
|
|
if (undoStack.Count > 0)
|
|
{
|
|
var command = undoStack.Pop();
|
|
command.Undo();
|
|
redoStack.Push(command);
|
|
Debug.Log("[Command] 执行撤销 (Undo)");
|
|
}
|
|
}
|
|
|
|
public static void Redo()
|
|
{
|
|
if (redoStack.Count > 0)
|
|
{
|
|
var command = redoStack.Pop();
|
|
command.Redo();
|
|
undoStack.Push(command);
|
|
Debug.Log("[Command] 执行重做 (Redo)");
|
|
}
|
|
}
|
|
}
|
|
}
|