using System.Collections.Generic;
namespace Lean.Touch
{
/// This base class can be used to associate extra data with the specified LeanFinger instance.
public abstract class LeanFingerData
{
// The finger associated with this link
public LeanFinger Finger;
public static int Count(List fingerDatas)
where T : LeanFingerData
{
var count = 0;
if (fingerDatas != null)
{
for (var i = fingerDatas.Count - 1; i >= 0; i--)
{
if (fingerDatas[i].Finger != null)
{
count++;
}
}
}
return count;
}
public static bool Exists(List fingerDatas, LeanFinger finger)
where T : LeanFingerData
{
if (fingerDatas != null)
{
for (var i = fingerDatas.Count - 1; i >= 0; i--)
{
if (fingerDatas[i].Finger == finger)
{
return true;
}
}
}
return false;
}
public static void Remove(List fingerDatas, LeanFinger finger, Stack pool = null)
where T : LeanFingerData
{
if (fingerDatas != null)
{
for (var i = fingerDatas.Count - 1; i >= 0; i--)
{
var fingerData = fingerDatas[i];
if (fingerData.Finger == finger)
{
fingerDatas.RemoveAt(i);
if (pool != null)
{
pool.Push(fingerData);
}
}
}
}
}
public static void RemoveAll(List fingerDatas, Stack pool = null)
where T : LeanFingerData
{
if (fingerDatas != null)
{
if (pool != null)
{
for (var i = fingerDatas.Count - 1; i >= 0; i--)
{
pool.Push(fingerDatas[i]);
}
}
fingerDatas.Clear();
}
}
public static T Find(List fingerDatas, LeanFinger finger)
where T : LeanFingerData
{
if (fingerDatas != null)
{
// Find existing link?
for (var i = fingerDatas.Count - 1; i >= 0; i--)
{
var fingerData = fingerDatas[i];
if (fingerData.Finger == finger)
{
return fingerData;
}
}
}
return null;
}
public static T FindOrCreate(ref List fingerDatas, LeanFinger finger)
where T : LeanFingerData, new()
{
if (fingerDatas == null)
{
fingerDatas = new List();
}
// Find existing link?
for (var i = fingerDatas.Count - 1; i >= 0; i--)
{
var fingerData = fingerDatas[i];
if (fingerData.Finger == finger)
{
return fingerData;
}
}
// Make new link?
var newFingerData = new T();
newFingerData.Finger = finger;
fingerDatas.Add(newFingerData);
return newFingerData;
}
}
}