115 lines
3.7 KiB
C#
115 lines
3.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static McBitFont.ChangeHistory;
|
|
using static McBitFont.MainForm;
|
|
|
|
namespace McBitFont {
|
|
internal class ChangeHistory(int depth = 50) {
|
|
private List<ChangeEvent> timeline = [];
|
|
private List<FrameMiniature> canvasChanges = [];
|
|
private List<List<FrameMiniature>> fontChanges = [];
|
|
public int Depth { get; set; } = depth;
|
|
public int Index { get; set; } = -1;
|
|
public int Count {
|
|
get { return timeline.Count; }
|
|
}
|
|
|
|
public enum ChangeType {
|
|
None = 0,
|
|
Canvas = 1, // Changes made to canvas
|
|
Font = 2 // Symbol width has been changed
|
|
}
|
|
public class ChangeEvent(ChangeType type, int index) {
|
|
public ChangeType Type { get; set; } = type;
|
|
public int Index { get; set; } = index;
|
|
}
|
|
|
|
private static FrameMiniature CopyFrameSimple(FrameMiniature f) {
|
|
FrameMiniature newf = new(f.code, f.width, f.height);
|
|
Array.Copy(f.data, newf.data, f.data.Length);
|
|
return newf;
|
|
}
|
|
|
|
public void Clear() {
|
|
timeline.Clear();
|
|
canvasChanges.Clear();
|
|
fontChanges.Clear();
|
|
Index = -1;
|
|
}
|
|
|
|
// Remove oldest event and recalculate indices
|
|
private bool RemoveOldest() {
|
|
if (Count == 0) return false;
|
|
ChangeType ct = timeline.First().Type;
|
|
|
|
switch (ct) {
|
|
case ChangeType.Canvas:
|
|
if (canvasChanges.Count == 0) return false;
|
|
canvasChanges.RemoveAt(0);
|
|
break;
|
|
case ChangeType.Font:
|
|
if (fontChanges.Count == 0) return false;
|
|
fontChanges.RemoveAt(0);
|
|
break;
|
|
default:
|
|
return false;
|
|
}
|
|
timeline.RemoveAt(0);
|
|
for (int i = 0; i < Count; i++) {
|
|
if (timeline[i].Type == ct) timeline[i].Index--;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public bool Remove(bool useIndex = true) {
|
|
if (Index == -1) return false;
|
|
var ce = timeline.Last();
|
|
switch (ce.Type) {
|
|
case ChangeType.Canvas:
|
|
canvasChanges.RemoveAt(ce.Index);
|
|
break;
|
|
case ChangeType.Font:
|
|
fontChanges.RemoveAt(ce.Index);
|
|
break;
|
|
default:
|
|
return false;
|
|
}
|
|
timeline.Remove(ce);
|
|
if (useIndex) Index--;
|
|
return true;
|
|
}
|
|
|
|
// Remove history tail
|
|
private void TruncateTail() {
|
|
// Check if the Index does not point to the last event
|
|
if (Index >= 0 && Index < Count - 1) {
|
|
timeline.RemoveRange(Index + 1, Count - Index - 1);
|
|
}
|
|
}
|
|
|
|
// Add canvas change
|
|
public void Add(FrameMiniature f) {
|
|
TruncateTail();
|
|
|
|
canvasChanges.Add(CopyFrameSimple(f));
|
|
timeline.Add(new ChangeEvent(ChangeType.Canvas, canvasChanges.Count - 1));
|
|
if (Count > Depth) RemoveOldest();
|
|
else Index++;
|
|
}
|
|
|
|
// Add Font change
|
|
public void Add(List<FrameMiniature> ff) {
|
|
//TruncateTail();
|
|
|
|
//fontChanges.Add(CopyFrameSimple(f));
|
|
//timeline.Add(new ChangeEvent(ChangeType.Canvas, canvasChanges.Count - 1));
|
|
//if (Count > Depth) RemoveOldest();
|
|
//else Index++;
|
|
}
|
|
}
|
|
}
|