From 2f86598a2a35aed60c21965c160b24bb8c344b62 Mon Sep 17 00:00:00 2001 From: Anton Mukhin Date: Fri, 30 May 2025 07:15:43 +0300 Subject: [PATCH] WIP: working on a new history class --- McBitFont/ChangeHistory.cs | 114 +++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 McBitFont/ChangeHistory.cs diff --git a/McBitFont/ChangeHistory.cs b/McBitFont/ChangeHistory.cs new file mode 100644 index 0000000..639b507 --- /dev/null +++ b/McBitFont/ChangeHistory.cs @@ -0,0 +1,114 @@ +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 timeline = []; + private List canvasChanges = []; + private List> 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 ff) { + //TruncateTail(); + + //fontChanges.Add(CopyFrameSimple(f)); + //timeline.Add(new ChangeEvent(ChangeType.Canvas, canvasChanges.Count - 1)); + //if (Count > Depth) RemoveOldest(); + //else Index++; + } + } +}