1
This commit is contained in:
@@ -42,13 +42,9 @@ namespace BITKit
|
||||
public static implicit operator T(References<T> self) => self.Get();
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public record Reference : References
|
||||
[Serializable]
|
||||
public struct Reference : IReference
|
||||
{
|
||||
public Reference()
|
||||
{
|
||||
}
|
||||
|
||||
public Reference(string value)
|
||||
{
|
||||
this.value = value;
|
||||
@@ -57,8 +53,7 @@ namespace BITKit
|
||||
[UnityEngine.TextArea]
|
||||
#endif
|
||||
public string value;
|
||||
public override string Get() => value;
|
||||
public override string ToString() => value;
|
||||
public string Get() => value;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
|
@@ -27,7 +27,7 @@ namespace BITKit
|
||||
public class MyHandle:IDisposable
|
||||
{
|
||||
private readonly ValidHandle _validHandle;
|
||||
private bool _isDisable = false;
|
||||
private readonly bool _isDisable;
|
||||
public MyHandle(ValidHandle validHandle,bool isDisable = false)
|
||||
{
|
||||
_validHandle = validHandle;
|
||||
@@ -61,106 +61,76 @@ namespace BITKit
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Allow:{enableHandle}\nElements:{string.Join("\n",objs)}\nDisableElements:{string.Join("\n",disableObjs)}";
|
||||
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);
|
||||
_eventOnEnableChanged?.Invoke(_enableHandle);
|
||||
}
|
||||
public static implicit operator bool(ValidHandle validHandle)
|
||||
{
|
||||
return !validHandle._isDisposed && validHandle.enableHandle;
|
||||
return !validHandle._isDisposed && validHandle._enableHandle;
|
||||
}
|
||||
|
||||
public bool Allow => this;
|
||||
|
||||
private bool enableHandle;
|
||||
private bool _enableHandle;
|
||||
/// <summary>
|
||||
/// ⚠️Dont operate this field directly
|
||||
/// </summary>
|
||||
public readonly List<object> objs = new List<object>();
|
||||
private readonly HashSet<object> _objs = new();
|
||||
|
||||
/// <summary>
|
||||
/// ⚠️Dont operate this field directly
|
||||
/// </summary>
|
||||
public readonly List<object> disableObjs = new List<object>();
|
||||
private bool tempEnable;
|
||||
private Action<bool> EventOnEnableChanged;
|
||||
private readonly HashSet<object> _disableObjs = new();
|
||||
private bool _tempEnable;
|
||||
private Action<bool> _eventOnEnableChanged;
|
||||
private readonly ConcurrentQueue<UniTaskCompletionSource> _completionSources = new();
|
||||
private bool _isDisposed;
|
||||
|
||||
public void AddElement(object obj)
|
||||
{
|
||||
if (objs.Contains(obj))
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
objs.Add(obj);
|
||||
}
|
||||
_objs.Add(obj);
|
||||
CheckEnable();
|
||||
}
|
||||
|
||||
private void CheckEnable()
|
||||
{
|
||||
tempEnable = objs.Count > 0 && disableObjs.Count == 0;
|
||||
if (tempEnable != enableHandle)
|
||||
_tempEnable = _objs.Count > 0 && _disableObjs.Count == 0;
|
||||
if (_tempEnable == _enableHandle) return;
|
||||
_enableHandle = _tempEnable;
|
||||
_eventOnEnableChanged?.Invoke(_enableHandle);
|
||||
if (_tempEnable) return;
|
||||
if (_completionSources.TryDequeue(out var cs))
|
||||
{
|
||||
enableHandle = tempEnable;
|
||||
if (EventOnEnableChanged is not null)
|
||||
{
|
||||
EventOnEnableChanged.Invoke(enableHandle);
|
||||
}
|
||||
if (tempEnable) return;
|
||||
if (_completionSources.TryDequeue(out var cs))
|
||||
{
|
||||
cs.TrySetResult();
|
||||
}
|
||||
cs.TrySetResult();
|
||||
}
|
||||
}
|
||||
public void RemoveElement(object obj)
|
||||
{
|
||||
if (objs.Contains(obj))
|
||||
if (_objs.Contains(obj))
|
||||
{
|
||||
objs.Remove(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 int Lenght => _objs.Count;
|
||||
public bool Contains(object obj) => _objs.Contains(obj);
|
||||
public void AddDisableElements(object obj)
|
||||
{
|
||||
if (disableObjs.Contains(obj))
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
disableObjs.Add(obj);
|
||||
}
|
||||
_disableObjs.Add(obj);
|
||||
CheckEnable();
|
||||
}
|
||||
public void RemoveDisableElements(object obj)
|
||||
{
|
||||
if (disableObjs.Contains(obj))
|
||||
if (_disableObjs.Contains(obj))
|
||||
{
|
||||
disableObjs.Remove(obj);
|
||||
_disableObjs.Remove(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -191,22 +161,22 @@ namespace BITKit
|
||||
}
|
||||
public void Invoke()
|
||||
{
|
||||
var enable = disableObjs.Count == 0 && objs.Count > 0;
|
||||
EventOnEnableChanged?.Invoke(enable);
|
||||
var enable = _disableObjs.Count == 0 && _objs.Count > 0;
|
||||
_eventOnEnableChanged?.Invoke(enable);
|
||||
}
|
||||
public void Invoke(bool value)
|
||||
{
|
||||
EventOnEnableChanged?.Invoke(value);
|
||||
_eventOnEnableChanged?.Invoke(value);
|
||||
}
|
||||
public void AddListener(Action<bool> action)
|
||||
{
|
||||
EventOnEnableChanged+= action;
|
||||
_eventOnEnableChanged+= action;
|
||||
}
|
||||
public void RemoveListener(Action<bool> action)
|
||||
{
|
||||
if(EventOnEnableChanged is not null && action is not null)
|
||||
if(_eventOnEnableChanged is not null && action is not null)
|
||||
{
|
||||
EventOnEnableChanged -= action;
|
||||
_eventOnEnableChanged -= action;
|
||||
}
|
||||
}
|
||||
public UniTask.Awaiter GetAwaiter()
|
||||
@@ -221,17 +191,17 @@ namespace BITKit
|
||||
}
|
||||
public void Clear()
|
||||
{
|
||||
objs.Clear();
|
||||
disableObjs.Clear();
|
||||
_objs.Clear();
|
||||
_disableObjs.Clear();
|
||||
Invoke();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_isDisposed = true;
|
||||
objs.Clear();
|
||||
disableObjs.Clear();
|
||||
EventOnEnableChanged = null;
|
||||
_objs.Clear();
|
||||
_disableObjs.Clear();
|
||||
_eventOnEnableChanged = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user