212 lines
5.7 KiB
C#
212 lines
5.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using Cysharp.Threading.Tasks;
|
||
|
||
namespace BITKit
|
||
{
|
||
public interface IValidHandle
|
||
{
|
||
bool Allow { get; }
|
||
void AddElement(object obj);
|
||
void RemoveElement(object obj);
|
||
void AddDisableElements(object obj);
|
||
void RemoveDisableElements(object obj);
|
||
void SetElements(object obj, bool add = true);
|
||
void SetDisableElements(object obj, bool add = true);
|
||
void Invoke();
|
||
void Invoke(bool value);
|
||
void AddListener(Action<bool> action);
|
||
void RemoveListener(Action<bool> action);
|
||
void Clear();
|
||
}
|
||
|
||
[CustomType(typeof(IValidHandle))]
|
||
public sealed class ValidHandle: IValidHandle
|
||
{
|
||
public class MyHandle:IDisposable
|
||
{
|
||
private readonly ValidHandle _validHandle;
|
||
|
||
public MyHandle(ValidHandle validHandle)
|
||
{
|
||
_validHandle = validHandle;
|
||
_validHandle.AddElement(this);
|
||
}
|
||
public void Dispose()
|
||
{
|
||
_validHandle.RemoveElement(this);
|
||
}
|
||
}
|
||
|
||
public MyHandle GetHandle() => new MyHandle(this);
|
||
|
||
public override string ToString()
|
||
{
|
||
return $"Allow:{enableHandle}\nElements:{string.Join("\n",objs)}\nDisableElements:{string.Join("\n",disableObjs)}";
|
||
}
|
||
|
||
public ValidHandle() {}
|
||
public ValidHandle(Action<bool> boolDelegate)
|
||
{
|
||
AddListener(boolDelegate);
|
||
EventOnEnableChanged?.Invoke(enableHandle);
|
||
}
|
||
public static implicit operator bool(ValidHandle validHandle)
|
||
{
|
||
return validHandle.enableHandle;
|
||
}
|
||
|
||
public bool Allow => this;
|
||
|
||
private bool enableHandle;
|
||
/// <summary>
|
||
/// ⚠️Dont operate this field directly
|
||
/// </summary>
|
||
public readonly List<object> objs = new List<object>();
|
||
/// <summary>
|
||
/// ⚠️Dont operate this field directly
|
||
/// </summary>
|
||
public readonly List<object> disableObjs = new List<object>();
|
||
private bool tempEnable;
|
||
private Action<bool> EventOnEnableChanged;
|
||
private readonly List<UniTaskCompletionSource> _completionSources = new();
|
||
|
||
|
||
public void AddElement(object obj)
|
||
{
|
||
if (objs.Contains(obj))
|
||
{
|
||
|
||
}
|
||
else
|
||
{
|
||
objs.Add(obj);
|
||
}
|
||
CheckEnable();
|
||
}
|
||
|
||
private void CheckEnable()
|
||
{
|
||
tempEnable = objs.Count > 0 && disableObjs.Count == 0;
|
||
if (tempEnable != enableHandle)
|
||
{
|
||
enableHandle = tempEnable;
|
||
if (EventOnEnableChanged is not null)
|
||
{
|
||
EventOnEnableChanged.Invoke(enableHandle);
|
||
}
|
||
if (tempEnable) return;
|
||
foreach (var cs in _completionSources)
|
||
{
|
||
cs.TrySetResult();
|
||
}
|
||
}
|
||
}
|
||
public void RemoveElement(object obj)
|
||
{
|
||
if (objs.Contains(obj))
|
||
{
|
||
objs.Remove(obj);
|
||
}
|
||
else
|
||
{
|
||
|
||
}
|
||
CheckEnable();
|
||
}
|
||
public int lenght => objs.Count;
|
||
public string[] GetElements()
|
||
{
|
||
List<string> elementNames = new List<string>();
|
||
for (int i = 0; i < objs.Count; i++)
|
||
{
|
||
elementNames.Add(objs[i].ToString());
|
||
}
|
||
return elementNames.ToArray();
|
||
}
|
||
public bool Contains(object obj) => objs.Contains(obj);
|
||
public void AddDisableElements(object obj)
|
||
{
|
||
if (disableObjs.Contains(obj))
|
||
{
|
||
|
||
}
|
||
else
|
||
{
|
||
disableObjs.Add(obj);
|
||
}
|
||
CheckEnable();
|
||
}
|
||
public void RemoveDisableElements(object obj)
|
||
{
|
||
if (disableObjs.Contains(obj))
|
||
{
|
||
disableObjs.Remove(obj);
|
||
}
|
||
else
|
||
{
|
||
}
|
||
CheckEnable();
|
||
}
|
||
public void SetElements(object obj, bool add = true)
|
||
{
|
||
if (add)
|
||
{
|
||
AddElement(obj);
|
||
}
|
||
else
|
||
{
|
||
RemoveElement(obj);
|
||
}
|
||
}
|
||
public void SetDisableElements(object obj, bool add = true)
|
||
{
|
||
if (add)
|
||
{
|
||
AddDisableElements(obj);
|
||
}
|
||
else
|
||
{
|
||
RemoveDisableElements(obj);
|
||
}
|
||
}
|
||
public void Invoke()
|
||
{
|
||
var enable = disableObjs.Count == 0 && objs.Count > 0;
|
||
EventOnEnableChanged?.Invoke(enable);
|
||
}
|
||
public void Invoke(bool value)
|
||
{
|
||
EventOnEnableChanged?.Invoke(value);
|
||
}
|
||
public void AddListener(Action<bool> action)
|
||
{
|
||
EventOnEnableChanged+= action;
|
||
}
|
||
public void RemoveListener(Action<bool> action)
|
||
{
|
||
if(EventOnEnableChanged is not null && action is not null)
|
||
{
|
||
EventOnEnableChanged -= action;
|
||
}
|
||
}
|
||
public UniTask.Awaiter GetAwaiter()
|
||
{
|
||
if (Allow is false)
|
||
{
|
||
return UniTask.CompletedTask.GetAwaiter();
|
||
}
|
||
var cs = new UniTaskCompletionSource();
|
||
_completionSources.Add(cs);
|
||
return cs.Task.GetAwaiter();
|
||
}
|
||
public void Clear()
|
||
{
|
||
objs.Clear();
|
||
disableObjs.Clear();
|
||
Invoke();
|
||
}
|
||
}
|
||
|
||
}
|