Files
ichni_Creator_Studio/Assets/Scripts/CommandSystem/CommandManager.cs
2026-07-10 14:05:45 +08:00

67 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using Ichni.RhythmGame;
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 (command == null) return;
if (undoStack.Count > 0)
{
var topCommand = undoStack.Peek();
// 尝试拦截短时间内的连续修改并归并为一步
if (topCommand.TryMerge(command))
{
topCommand.Execute();
return;
}
}
// 新操作彻底切断原本可能存在的重做未来
command.Execute();
undoStack.Push(command);
redoStack.Clear();
}
public static GameElement ExecuteCreate(Func<GameElement> createAction)
{
var command = new CreateElementCommand(createAction);
ExecuteCommand(command);
return command.CreatedElement;
}
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)");
}
}
}
}