Initial commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CommandManager
|
||||
{
|
||||
private static CommandManager instance;
|
||||
|
||||
private LinkedList<ICommand> commandStack = new LinkedList<ICommand>();
|
||||
private LinkedList<ICommand> redoStack = new LinkedList<ICommand>();
|
||||
|
||||
private int maxCapacity = 100; // added to prevent memory leak in the long run
|
||||
|
||||
private CommandManager()
|
||||
{
|
||||
}
|
||||
|
||||
public static CommandManager getInstance()
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new CommandManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void setMaxCapacity(int capacity)
|
||||
{
|
||||
maxCapacity = capacity;
|
||||
}
|
||||
|
||||
public void executeCommand(ICommand command)
|
||||
{
|
||||
command.execute();
|
||||
|
||||
commandStack.AddFirst(command);
|
||||
redoStack.Clear();
|
||||
if (commandStack.Count > maxCapacity)
|
||||
{
|
||||
commandStack.RemoveLast();
|
||||
}
|
||||
}
|
||||
|
||||
public void undo()
|
||||
{
|
||||
if (commandStack.Count > 0)
|
||||
{
|
||||
ICommand command = commandStack.First.Value;
|
||||
commandStack.RemoveFirst();
|
||||
command.undo();
|
||||
redoStack.AddFirst(command);
|
||||
}
|
||||
}
|
||||
|
||||
public ICommand redo()
|
||||
{
|
||||
if (redoStack.Count > 0)
|
||||
{
|
||||
ICommand command = redoStack.First.Value;
|
||||
redoStack.RemoveFirst();
|
||||
command.execute();
|
||||
commandStack.AddFirst(command);
|
||||
return command;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void clearHistory()
|
||||
{
|
||||
commandStack.Clear();
|
||||
redoStack.Clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user