2using System.Collections;
3using System.Collections.Generic;
85 [SerializeField]
private Stack<List<UndoState>> undoStack =
new Stack<List<UndoState>>();
86 [SerializeField]
private Stack<List<UndoState>> redoStack =
new Stack<List<UndoState>>();
107 private string PrintStack(Stack<List<UndoState>> stack)
109 System.Text.StringBuilder sb =
new System.Text.StringBuilder();
111 foreach(List<UndoState> collection
in stack)
113 foreach(UndoState state
in collection)
115 sb.AppendLine(state.ToString());
119 sb.AppendLine(
"-----");
122 return sb.ToString();
125 [SerializeField] UndoState currentUndo, currentRedo;
127 private void PushUndo(List<UndoState> state)
129 currentUndo = state[0];
130 undoStack.Push(state);
136 private void PushRedo(List<UndoState> state)
138 currentRedo = state[0];
139 redoStack.Push(state);
145 private List<UndoState> PopUndo()
147 List<UndoState> states = Pop(undoStack);
149 if(states ==
null || undoStack.Count < 1)
152 currentUndo = ((List<UndoState>)undoStack.Peek())[0];
160 private List<UndoState> PopRedo()
162 List<UndoState> states = Pop(redoStack);
164 if(states ==
null || redoStack.Count < 1)
167 currentRedo = ((List<UndoState>)redoStack.Peek())[0];
175 private List<UndoState> Pop(Stack<List<UndoState>> stack)
178 return (List<UndoState>) stack.Pop();
183 private static void ClearStack(Stack<List<UndoState>> stack)
185 foreach(List<UndoState> commands
in stack)
186 foreach(UndoState state
in commands)
187 state.target.OnExitScope();
230 List<UndoState> states = targets.Select(x =>
new UndoState(x, message)).ToList();
239 List<UndoState> states =
instance.PopUndo();
244 instance.PushRedo(states.Select(x =>
new UndoState(x.target, x.message)).ToList());
251 if(
instance.undoPerformed !=
null )
261 List<UndoState> states =
instance.PopRedo();
266 instance.PushUndo(states.Select(x =>
new UndoState(x.target, x.message)).ToList());
271 if(
instance.redoPerformed !=
null )
UndoState(IUndo target, string msg)
override string ToString()
static void AddRedoPerformedListener(Callback callback)
static string PrintRedoStack()
static string PrintUndoStack()
static void AddUndoPerformedListener(Callback callback)
Callback undoStackModified
static string GetCurrentRedo()
static void PerformUndo()
static void PerformRedo()
static string GetCurrentUndo()
Callback redoStackModified
static void RegisterStates(IEnumerable< IUndo > targets, string message)
static void RegisterState(IUndo target, string message)
void ApplyState(Hashtable values)