Files
Studio/Assets/Scripts/XED/Managers/CommandManager.cs
2025-02-19 17:24:26 +09:00

61 lines
1.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XED.Core;
using XED.Interfaces;
namespace XED.Manage
{
public class CommandManager : UnitySingleton<CommandManager>, IInputHandler
{
private Stack<ICommand> _undoStack = new Stack<ICommand>();
private Stack<ICommand> _redoStack = new Stack<ICommand>();
public void AddCommand(ICommand command)
{
_undoStack.Push(command);
_redoStack.Clear(); // Clear redo stack when a new action is made
}
public void ExecuteCommand(ICommand command)
{
command.Execute();
AddCommand(command);
}
public void Undo()
{
if (_undoStack.Count > 0)
{
ICommand command = _undoStack.Pop();
command.Undo();
_redoStack.Push(command);
}
}
public void Redo()
{
if (_redoStack.Count > 0)
{
ICommand command = _redoStack.Pop();
command.Execute();
_undoStack.Push(command);
}
}
public InputHandler GetInputHandler()
{
var shortcutTable = new Dictionary<KeyCode, Dictionary<KeyCode, Action>>();
#if UNITY_EDITOR
shortcutTable.Add(KeyCode.LeftShift, new Dictionary<KeyCode, Action>());
shortcutTable[KeyCode.LeftShift].Add(KeyCode.Z, Undo);
shortcutTable[KeyCode.LeftShift].Add(KeyCode.Y, Redo);
#else
shortcutTable.Add(KeyCode.LeftControl, new Dictionary<KeyCode, Action>());
shortcutTable[KeyCode.LeftControl].Add(KeyCode.Z, Undo);
shortcutTable[KeyCode.LeftControl].Add(KeyCode.X, Redo);
#endif
var handler = new InputHandler(null, null, null, shortcutTable);
return handler;
}
}
}