WIP: Undo/Redo
This commit is contained in:
74
McBitFont/CanvasHistory.cs
Normal file
74
McBitFont/CanvasHistory.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace McBitFont {
|
||||
internal class CanvasHistory {
|
||||
private List<bool[,]> stack;
|
||||
public int Depth { get; set; }
|
||||
public int Index { get; set; }
|
||||
public int Count {
|
||||
get { return stack.Count; }
|
||||
}
|
||||
public int Redos {
|
||||
get {
|
||||
return Count - Index - 1;
|
||||
}
|
||||
}
|
||||
public int Undos {
|
||||
get {
|
||||
return Index + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public CanvasHistory(int depth = 5, int index = -1) {
|
||||
Depth = depth;
|
||||
Index = index;
|
||||
stack = [];
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
stack.Clear();
|
||||
Index = -1;
|
||||
}
|
||||
|
||||
public void Add(bool[,] data, bool useIndex = true) {
|
||||
if (Index < Count - 1) {
|
||||
stack.RemoveRange(Index + 1, Count - Index - 1);
|
||||
}
|
||||
stack.Add(data);
|
||||
if (useIndex) {
|
||||
if (Count > Depth) stack.RemoveAt(0);
|
||||
else Index++;
|
||||
}
|
||||
}
|
||||
|
||||
public void ApplyAdded() {
|
||||
if (Count > Depth)
|
||||
while (Count > Depth) stack.RemoveAt(0);
|
||||
else Index = Count - 1;
|
||||
}
|
||||
|
||||
public void Remove(bool useIndex = true) {
|
||||
stack.RemoveAt(stack.Count - 1);
|
||||
if (useIndex) Index--;
|
||||
}
|
||||
|
||||
public void Undo(ref bool[,] data) {
|
||||
if (Index < 0) return;
|
||||
data = stack.ElementAt(Index);
|
||||
Index--;
|
||||
}
|
||||
|
||||
public void Redo(ref bool[,] data) {
|
||||
if (Index >= Count - 1) return;
|
||||
Index++;
|
||||
data = stack.ElementAt(Index);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user