Client LuaCsForBarotrauma
Memento.cs
1 using System.Collections.Generic;
2 using System.Linq;
3 
4 namespace Barotrauma
5 {
6  interface IMemorizable<T>
7  {
8  Memento<T> Memento { get; }
9  void StoreSnapshot();
10  void Undo();
11  void Redo();
12  void ClearHistory();
13  }
14 
15  public class Memento<T>
16  {
17  public T Current { get; private set; }
18 
19  public int UndoCount => undoStack.Count;
20  public int RedoCount => redoStack.Count;
21 
22  private Stack<T> undoStack = new Stack<T>();
23  private Stack<T> redoStack = new Stack<T>();
24 
25  public void Store(T newState)
26  {
27  redoStack.Clear();
28  if (Current != null && !Current.Equals(default(T)))
29  {
30  undoStack.Push(Current);
31  }
32  Current = newState;
33  }
34 
35  public T Undo()
36  {
37  if (undoStack.Any())
38  {
39  redoStack.Push(Current);
40  Current = undoStack.Pop();
41  }
42  return Current;
43  }
44 
45  public T Redo()
46  {
47  if (redoStack.Any())
48  {
49  undoStack.Push(Current);
50  Current = redoStack.Pop();
51  }
52  return Current;
53  }
54 
55  public void Clear()
56  {
57  undoStack.Clear();
58  redoStack.Clear();
59  Current = default(T);
60  }
61  }
62 }
void Store(T newState)
Definition: Memento.cs:25