84 lines
1.9 KiB
C#
84 lines
1.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
namespace BITKit
|
|
{
|
|
public interface IEntryGroup { }
|
|
public interface IEntryElement
|
|
{
|
|
void Entry();
|
|
void Exit();
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class EntryGroup<T> : IEntryGroup where T : IEntryElement
|
|
{
|
|
public int index = -1;
|
|
public List<T> list = new();
|
|
int m_index;
|
|
public void Entry(T t)
|
|
{
|
|
if (t is not null)
|
|
{
|
|
list.TryAdd(t);
|
|
|
|
}
|
|
index = list.IndexOf(t);
|
|
EnsureConfiguration();
|
|
}
|
|
public void Entry(int index)
|
|
{
|
|
if (index < list.Count)
|
|
{
|
|
this.index = index;
|
|
}
|
|
}
|
|
public void Entry(Func<IEntryElement,bool> entryFactory)
|
|
{
|
|
var index = list.FindIndex(x => entryFactory.Invoke(x));
|
|
Entry(index);
|
|
}
|
|
public void Entry()
|
|
{
|
|
index = 0;
|
|
EnsureConfiguration();
|
|
}
|
|
|
|
public void Exit()
|
|
{
|
|
index = -1;
|
|
EnsureConfiguration();
|
|
}
|
|
public bool TryGetEntried(out T value)
|
|
{
|
|
EnsureConfiguration();
|
|
if (index is not -1)
|
|
{
|
|
value = list[index];
|
|
return true;
|
|
}
|
|
value = default;
|
|
return false;
|
|
}
|
|
void EnsureConfiguration()
|
|
{
|
|
if (MathE.Equals(this.index, m_index))
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
if (m_index is not -1)
|
|
{
|
|
list[m_index].Exit();
|
|
}
|
|
if (index is not -1)
|
|
{
|
|
list[index].Entry();
|
|
}
|
|
}
|
|
m_index = index;
|
|
}
|
|
}
|
|
} |