BITKit/Src/Unity/Scripts/UX/Waiting/UXWaiting.cs

112 lines
3.5 KiB
C#
Raw Normal View History

2024-03-31 23:31:00 +08:00
using System;
using System.Collections;
2024-07-29 09:39:22 +08:00
using System.Collections.Concurrent;
2024-03-31 23:31:00 +08:00
using System.Collections.Generic;
using System.Linq;
using BITKit.UX;
using UnityEngine;
using Cysharp.Threading.Tasks;
using kcp2k;
using UnityEngine.Pool;
using UnityEngine.UIElements;
namespace BITKit
{
2024-06-11 17:16:34 +08:00
[Serializable]
public sealed class UXWaitingSingleton : IUXWaiting
{
private IUXWaiting Implementation => UXWaiting.Singleton;
public IUXWaitingHandle Get()
{
return Implementation.Get();
}
public void Release(IUXWaitingHandle handle)
{
Implementation.Release(handle);
}
}
2024-05-31 01:23:15 +08:00
[CustomType(typeof(IUXWaiting))]
2024-06-11 17:16:34 +08:00
public class UXWaiting : MonoBehaviourSingleton<UXWaiting>,IUXWaiting
2024-03-31 23:31:00 +08:00
{
public sealed class WaitingHandle : IUXWaitingHandle
{
public string Message
{
get => label.text;
set => SetMessage(value);
}
public object Container => container;
internal VisualElement container;
internal Label label;
2024-06-27 18:53:49 +08:00
internal Action OnDispose;
2024-08-04 11:39:37 +08:00
internal Action<string> OnSetMessage;
public void SetMessage(string message) => OnSetMessage?.Invoke(message);
2024-06-27 18:53:49 +08:00
public void Dispose() => OnDispose?.Invoke();
2024-03-31 23:31:00 +08:00
}
[SerializeField] private VisualTreeAsset handleTemplate;
[SerializeField] private bool asGlobal;
[UXBindPath("waiting-container")]
private VisualElement _container;
[UXBindPath("waiting-root")]
private VisualElement _root;
2024-08-04 11:39:37 +08:00
private readonly ConcurrentQueue<(WaitingHandle handle,string text)> _messageQueue = new();
private readonly ConcurrentQueue<WaitingHandle> _initializedHandles = new();
private readonly ConcurrentQueue<WaitingHandle> _disposeQueue = new();
private readonly ValidHandle _visibleHandle = new();
2024-03-31 23:31:00 +08:00
public IUXWaitingHandle Get()
{
2024-08-04 11:39:37 +08:00
var handle = new WaitingHandle();
_initializedHandles.Enqueue(handle);
handle.OnSetMessage = (text) =>
2024-03-31 23:31:00 +08:00
{
2024-08-04 11:39:37 +08:00
_messageQueue.Enqueue((handle,text));
};
handle.OnDispose = () =>
{
_disposeQueue.Enqueue(handle);
};
return handle;
2024-03-31 23:31:00 +08:00
}
2024-08-04 11:39:37 +08:00
public void Release(IUXWaitingHandle handle)
2024-03-31 23:31:00 +08:00
{
2024-08-04 11:39:37 +08:00
handle.Dispose();
2024-03-31 23:31:00 +08:00
}
2024-07-29 09:39:22 +08:00
private void Update()
{
2024-08-04 11:39:37 +08:00
while (_initializedHandles.TryDequeue(out var handle))
{
var container = _container.Create(handleTemplate);
handle.container = container;
handle.label = container.Get<Label>();
}
2024-07-29 09:39:22 +08:00
while (_messageQueue.TryDequeue(out var message))
{
2024-08-04 11:39:37 +08:00
message.handle.label.text = message.text;
}
while (_disposeQueue.TryDequeue(out var handle))
{
handle.container.RemoveFromHierarchy();
2024-07-29 09:39:22 +08:00
}
2024-08-04 11:39:37 +08:00
_visibleHandle.SetElements(0, _container.childCount>0);
2024-07-29 09:39:22 +08:00
}
2024-06-11 17:16:34 +08:00
protected override void Awake()
2024-03-31 23:31:00 +08:00
{
2024-06-11 17:16:34 +08:00
base.Awake();
2024-03-31 23:31:00 +08:00
UXUtils.Inject(this);
2024-05-31 01:23:15 +08:00
_container.Clear();
2024-03-31 23:31:00 +08:00
if (asGlobal)
{
DI.Register<IUXWaiting>(this);
}
_root.SetActive(false);
}
2024-05-31 01:23:15 +08:00
private void Start()
{
2024-08-04 11:39:37 +08:00
_visibleHandle.AddListener(_root.SetActive);
_visibleHandle.Invoke();
2024-03-31 23:31:00 +08:00
}
}
}