forked from AerysBat/XNALara
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUndoHistory.cs
92 lines (75 loc) · 2.42 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
using System.Collections.Generic;
using System;
namespace XNALara
{
public class UndoHistory
{
private const int MaxHistorySize = 10000;
private ControlGUI gui;
private bool isEnabled;
private LinkedList<UndoHistoryState> history;
private UndoHistoryState lastState;
private UndoHistoryStateInterrupt stateInterrupt;
public UndoHistory(ControlGUI gui) {
this.gui = gui;
isEnabled = true;
history = new LinkedList<UndoHistoryState>();
lastState = null;
stateInterrupt = new UndoHistoryStateInterrupt();
}
public void Clear() {
history.Clear();
lastState = null;
}
public void SaveState(UndoHistoryState state) {
if (!isEnabled) {
return;
}
bool isSignificant = true;
if (lastState != null) {
isSignificant = state.DetermineSignificance(lastState);
}
if (isSignificant) {
history.AddLast(state);
if (history.Count > MaxHistorySize) {
history.RemoveFirst();
}
//PrintUndoHistory();
}
lastState = state;
}
public void SaveInterrupt() {
SaveState(stateInterrupt);
}
public void RestoreState() {
while (true) {
UndoHistoryState newState = RestoreStateInternal();
if (newState == null) {
break;
}
if (newState is UndoHistoryStateBoneTransform) {
break;
}
}
//PrintUndoHistory();
}
private UndoHistoryState RestoreStateInternal() {
if (history.Count == 0) {
return null;
}
UndoHistoryState state = history.Last.Value;
history.RemoveLast();
isEnabled = false;
state.Apply(gui);
isEnabled = true;
lastState = (history.Count > 0 ? history.Last.Value : null);
return state;
}
private void PrintUndoHistory() {
foreach (UndoHistoryState state in history) {
Console.WriteLine(state);
}
Console.WriteLine("----------------------------------------");
}
}
}