forked from TASEmulators/BizHawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUndoHistory.cs
96 lines (80 loc) · 1.81 KB
/
UndoHistory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Collections.Generic;
using System.Linq;
namespace BizHawk.Common
{
public class UndoHistory<T>
{
private List<List<T>> _history = new List<List<T>>();
private int _curPos; // 1-based
public int MaxUndoLevels { get; set; }
public UndoHistory(bool enabled)
{
MaxUndoLevels = 5;
Enabled = enabled;
}
public UndoHistory(IEnumerable<T> newState, bool enabled)
{
AddState(newState);
Enabled = enabled;
}
public bool Enabled { get; private set; }
public bool CanUndo
{
get { return Enabled && _curPos > 1; }
}
public bool CanRedo
{
get { return Enabled && _curPos < _history.Count; }
}
public bool HasHistory
{
get { return Enabled && _history.Any(); }
}
public void Clear()
{
_history = new List<List<T>>();
_curPos = 0;
}
public void AddState(IEnumerable<T> newState)
{
if (Enabled)
{
if (_curPos < _history.Count)
{
for (var i = _curPos + 1; i <= _history.Count; i++)
{
_history.Remove(_history[i - 1]);
}
}
// TODO: don't assume we are one over, since MaxUndoLevels is public, it could be set to a small number after a large list has occured
if (_history.Count > MaxUndoLevels)
{
foreach (var item in _history.Take(_history.Count - MaxUndoLevels))
{
_history.Remove(item);
}
}
_history.Add(newState.ToList());
_curPos = _history.Count;
}
}
public IEnumerable<T> Undo()
{
if (CanUndo && Enabled)
{
_curPos--;
return _history[_curPos - 1];
}
return Enumerable.Empty<T>();
}
public IEnumerable<T> Redo()
{
if (CanRedo && Enabled)
{
_curPos++;
return _history[_curPos - 1];
}
return Enumerable.Empty<T>();
}
}
}