BizHawk/BizHawk.MultiClient/HistoryCollection.cs

89 lines
1.5 KiB
C#
Raw Normal View History

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