cool
This commit is contained in:
@@ -10,16 +10,27 @@ using UnityEngine.UIElements;
|
||||
|
||||
namespace Net.BITKit.UX.SnackBar
|
||||
{
|
||||
public class UXSnackBar<TPanel> : UIToolkitSubPanel<TPanel>,ISnackBar where TPanel :IUXPanel
|
||||
public static class UXSnackBarStatic
|
||||
{
|
||||
public static int Count;
|
||||
}
|
||||
public class UXSnackBar<TPanel> : UIToolkitSubPanel<TPanel>,IDisposable,ISnackBar where TPanel :IUXPanel
|
||||
{
|
||||
[UXBindPath("snack_bar-container")] private VisualElement _snackBarContainer;
|
||||
|
||||
private VisualTreeAsset _template;
|
||||
|
||||
private readonly ValidHandle _waitHandle = new ValidHandle();
|
||||
|
||||
private readonly Queue<(string message,Severity severity)> _queue=new();
|
||||
|
||||
public UXSnackBar(IServiceProvider serviceProvider) : base(serviceProvider)
|
||||
{
|
||||
if (UXSnackBarStatic.Count > 0)
|
||||
{
|
||||
throw new Exception("SnackBar can only be created once");
|
||||
}
|
||||
UXSnackBarStatic.Count++;
|
||||
}
|
||||
|
||||
protected override void OnInitiated()
|
||||
@@ -30,6 +41,11 @@ namespace Net.BITKit.UX.SnackBar
|
||||
_snackBarContainer.Clear();
|
||||
|
||||
BITAppForUnity.AllowCursor.AddListener(OnAllowCursor);
|
||||
|
||||
while (_queue.TryDequeue(out var temp ))
|
||||
{
|
||||
Add(temp.message,temp.severity);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAllowCursor(bool obj)
|
||||
@@ -44,26 +60,43 @@ namespace Net.BITKit.UX.SnackBar
|
||||
}
|
||||
}
|
||||
|
||||
public async void Add(string message, Severity severity = Severity.Normal)
|
||||
public void Add(string message, Severity severity = Severity.Normal)
|
||||
{
|
||||
if (_snackBarContainer is null)
|
||||
{
|
||||
_queue.Enqueue(new ValueTuple<string, Severity>(message,severity));
|
||||
return;
|
||||
}
|
||||
|
||||
var ve = _snackBarContainer.Create(_template);
|
||||
|
||||
ve.AddToClassList($"severity-{severity.ToString().ToLower()}");
|
||||
|
||||
ve.Get<Label>().text = message;
|
||||
|
||||
if (ve.Q("VisualElement--1") is { } bar)
|
||||
{
|
||||
await BITween.Lerp(x => bar.style.width = new StyleLength(Length.Percent(x)), 0f, 100, 5, Mathf.Lerp);
|
||||
}
|
||||
else
|
||||
{
|
||||
await UniTask.Delay(5000);
|
||||
}
|
||||
Continue();
|
||||
|
||||
await _waitHandle;
|
||||
return;
|
||||
async void Continue()
|
||||
{
|
||||
if (ve.Q("VisualElement--1") is { } bar)
|
||||
{
|
||||
await BITween.Lerp(x => bar.style.width = new StyleLength(Length.Percent(x)), 0f, 100, 5, Mathf.Lerp);
|
||||
}
|
||||
else
|
||||
{
|
||||
await UniTask.Delay(5000);
|
||||
}
|
||||
|
||||
await _waitHandle;
|
||||
|
||||
ve.RemoveFromHierarchy();
|
||||
ve.RemoveFromHierarchy();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
UXSnackBarStatic.Count--;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user