-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChangeHistory.cs
251 lines (229 loc) · 8.06 KB
/
ChangeHistory.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using Object = UnityEngine.Object;
public class ChangeHistory : MonoBehaviour
{
private static Stack<Change> past = new Stack<Change>();
private static Stack<Change> future = new Stack<Change>();
public class Change
{
public object obj;
public Type type;
public Action OnRevert;
public Action Undo;
public Action Redo;
public List<RefelectionVariable> variables = new List<RefelectionVariable>();
public enum ChangeType { Bad, All, Single, UserDefined, Custom }
public ChangeType changeType = ChangeType.Bad;
public struct RefelectionVariable
{
public string variable;
public Func<dynamic> get;
public Action<dynamic> set;
public dynamic value;
}
public Change(Action undo, Action redo)
{
obj = null;
type = null;
OnRevert = Undo = undo;
Redo = redo;
changeType = ChangeType.Custom;
}
public Change(object changed, Action OnSet = null)
{
obj = changed;
type = obj.GetType();
OnRevert = OnSet;
foreach (PropertyInfo p in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (!p.CanWrite || !p.CanRead) continue;
try
{
RefelectionVariable variable;
variable.variable = p.Name;
variable.value = p.GetValue(changed);
variable.get = null;
variable.set = null;
variables.Add(variable);
}
catch { }
}
changeType = ChangeType.All;
}
public Change(object changed, string property, Action OnSet = null)
{
obj = changed;
type = obj.GetType();
OnRevert = OnSet;
PropertyInfo p = type.GetProperty(property);
if (!p.CanWrite || !p.CanRead) return;
try
{
RefelectionVariable variable;
variable.variable = p.Name;
variable.value = p.GetValue(changed);
variable.get = null;
variable.set = null;
variables.Add(variable);
}
catch { }
changeType = ChangeType.Single;
}
public Change(object changed, Func<dynamic> getter, Action<dynamic> setter, Action OnSet = null)
{
if (getter == null || setter == null) return;
obj = changed;
type = obj.GetType();
OnRevert = OnSet;
RefelectionVariable variable;
variable.variable = "";
variable.get = getter;
variable.set = setter;
variable.value = getter.Invoke();
variables.Add(variable);
changeType = ChangeType.UserDefined;
}
public void Revert()
{
OnRevert?.Invoke();
foreach (RefelectionVariable v in variables)
{
if (v.set != null)
v.set.Invoke(v.value);
else type.GetProperty(v.variable).SetValue(obj, v.value);
}
}
public bool Valid()
{
bool valid = true;//false;
//foreach (RefelectionVariable v in variables)
//{
// Debug.Log($"{v.variable}: {type.GetProperty(v.variable).GetValue(obj)} == {v.value}");
// if (!type.GetProperty(v.variable).GetValue(obj).Equals(v.value))
// {
// valid = true;
// break;
// }
//}
return valid;
}
public override bool Equals(object obj)
{
Change c = obj as Change;
if(c == null)
return base.Equals(obj);
if (c.variables.Count != variables.Count)
return false;
bool allMatch = changeType == ChangeType.Custom ? false : true;
for (int i = 0; i < variables.Count; i++)
if (!c.variables[i].variable.Equals(variables[i].variable)
|| !c.variables[i].value.Equals(variables[i].value))
{
allMatch = false;
break;
}
return allMatch;
}
}
void Update()
{
if (/*(Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) &&*/ Input.GetKeyDown(KeyCode.Z)) Undo();
if (/*(Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && */Input.GetKeyDown(KeyCode.Y)) Redo();
}
public static void LogState(Action undo, Action redo)
{
Change c = new Change(undo, redo);
if (!c.Valid() || past.Count > 0 && past.Peek().Equals(c)) return;
future.Clear();
past.Push(c);
}
public static void LogState(object changed, Action OnSet = null)
{
Change c = new Change(changed, OnSet);
if (!c.Valid() || past.Count > 0 && past.Peek().Equals(c)) return;
future.Clear();
past.Push(c);
}
public static void LogState(object changed, string property, Action OnSet = null)
{
Change c = new Change(changed, property, OnSet);
if (!c.Valid() || past.Count > 0 && past.Peek().Equals(c)) return;
future.Clear();
past.Push(c);
}
public static void LogState(object changed, Func<dynamic> getter, Action<dynamic> setter, Action OnSet = null)
{
Change c = new Change(changed, getter, setter, OnSet);
if (!c.Valid() || past.Count > 0 && past.Peek().Equals(c)) return;
future.Clear();
past.Push(c);
}
private void Undo()
{
if (past.Count == 0) return;
Change c = past.Pop();
//Debug.Log(c.changeType);
if (c.changeType == Change.ChangeType.Bad) Undo();
switch (c.changeType)
{
case Change.ChangeType.All:
future.Push(new Change(c.obj, c.OnRevert));
break;
case Change.ChangeType.Single:
future.Push(new Change(c.obj, c.variables[0].variable, c.OnRevert));
break;
case Change.ChangeType.UserDefined:
future.Push(new Change(c.obj, c.variables[0].get, c.variables[0].set, c.OnRevert));
break;
case Change.ChangeType.Custom:
future.Push(new Change(c.Redo, c.Undo));
break;
}
ActionLog.Log($"Undo");
Debug.Log($"Undo");
c.Revert();
}
private void Redo()
{
if (future.Count == 0) return;
Change c = future.Pop();
if (c.changeType == Change.ChangeType.Bad) Redo();
switch (c.changeType)
{
case Change.ChangeType.All:
past.Push(new Change(c.obj, c.OnRevert));
break;
case Change.ChangeType.Single:
past.Push(new Change(c.obj, c.variables[0].variable, c.OnRevert));
break;
case Change.ChangeType.UserDefined:
past.Push(new Change(c.obj, c.variables[0].get, c.variables[0].set, c.OnRevert));
break;
case Change.ChangeType.Custom:
past.Push(new Change(c.Redo, c.Undo));
break;
}
ActionLog.Log($"Redo");
Debug.Log($"Redo");
c.Revert();
}
}
public static class ChangeHistoryExtensions
{
public static void LogState(this object c, Action OnSet = null)
{
ChangeHistory.LogState(c, OnSet);
}
public static void LogState(this object c, string property, Action OnSet = null)
{
ChangeHistory.LogState(c, property, OnSet);
}
public static void LogState(this object c, Func<dynamic> getter, Action<dynamic> setter, Action OnSet = null)
{
ChangeHistory.LogState(c, getter, setter, OnSet);
}
}