using System.Collections.Generic; using System.Linq; namespace BizHawk.MultiClient { public class HistoryCollection { public List> History { get; private set; } private int curPos; //1-based public bool Enabled { get; private set; } public HistoryCollection(bool enabled) { History = new List>(); Enabled = enabled; } public HistoryCollection(List newState, bool enabled) { History = new List>(); AddState(newState); Enabled = enabled; } public void Clear() { History = new List>(); } public bool CanUndo { get { return Enabled && curPos > 1; } } public bool CanRedo { get { return Enabled && curPos < History.Count; } } public bool HasHistory { get { return Enabled && History.Any(); } } public void AddState(List newState) { if (Enabled) { if (curPos < History.Count) { for (int i = curPos + 1; i <= History.Count; i++) { History.Remove(History[i - 1]); } } History.Add(newState); curPos = History.Count; } } public List Undo() { if (CanUndo && Enabled) { curPos--; return History[curPos - 1]; } else { return null; } } public List Redo() { if (CanRedo && Enabled) { curPos++; return History[curPos - 1]; } else { return null; } } } }