1
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using BITKit.Mod;
|
||||
using BITKit.UX;
|
||||
using UnityEngine;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using kcp2k;
|
||||
using UnityEngine.Pool;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
namespace BITKit
|
||||
{
|
||||
@@ -25,37 +22,45 @@ namespace BITKit
|
||||
Implementation.Release(handle);
|
||||
}
|
||||
}
|
||||
[CustomType(typeof(IUXWaiting))]
|
||||
public class UXWaiting : MonoBehaviourSingleton<UXWaiting>,IUXWaiting
|
||||
public sealed class WaitingHandle : IUXWaitingHandle
|
||||
{
|
||||
public sealed class WaitingHandle : IUXWaitingHandle
|
||||
public string Message
|
||||
{
|
||||
public string Message
|
||||
{
|
||||
get => label.text;
|
||||
set => SetMessage(value);
|
||||
}
|
||||
public object Container => container;
|
||||
internal VisualElement container;
|
||||
internal Label label;
|
||||
internal Action OnDispose;
|
||||
internal Action<string> OnSetMessage;
|
||||
public void SetMessage(string message) => OnSetMessage?.Invoke(message);
|
||||
public void Dispose() => OnDispose?.Invoke();
|
||||
get => Label.text;
|
||||
set => SetMessage(value);
|
||||
}
|
||||
public object Container => VisualElement;
|
||||
internal VisualElement VisualElement;
|
||||
internal Label Label;
|
||||
internal Action OnDispose;
|
||||
internal Action<string> OnSetMessage;
|
||||
public void SetMessage(string message) => OnSetMessage?.Invoke(message);
|
||||
public void Dispose() => OnDispose?.Invoke();
|
||||
}
|
||||
[CustomType(typeof(IUXWaiting))]
|
||||
public sealed class UXWaiting : UIToolkitOverlay,IUXWaiting
|
||||
{
|
||||
private const string HandleTemplate = "ux_waiting_handle";
|
||||
public static UXWaiting Singleton { get; private set; }
|
||||
private readonly ITicker _ticker;
|
||||
public UXWaiting(IUXService uxService, ITicker ticker, CancellationTokenSource cancellationToken) : base(uxService, cancellationToken)
|
||||
{
|
||||
_ticker = ticker;
|
||||
Singleton = this;
|
||||
_visibleHandle.AddListener(Dispose);
|
||||
}
|
||||
protected override string DocumentPath => "ux_global_waiting";
|
||||
|
||||
[SerializeField] private VisualTreeAsset handleTemplate;
|
||||
[SerializeField] private bool asGlobal;
|
||||
private VisualTreeAsset _handleTemplate;
|
||||
|
||||
[UXBindPath("waiting-container")]
|
||||
private VisualElement _container;
|
||||
[UXBindPath("waiting-root")]
|
||||
private VisualElement _root;
|
||||
|
||||
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();
|
||||
private InitializationState _initializationState;
|
||||
public IUXWaitingHandle Get()
|
||||
{
|
||||
var handle = new WaitingHandle();
|
||||
@@ -67,42 +72,74 @@ namespace BITKit
|
||||
handle.OnDispose = () =>
|
||||
{
|
||||
_disposeQueue.Enqueue(handle);
|
||||
_ticker.Add(OnTick);
|
||||
};
|
||||
|
||||
_ticker.Add(OnTick);
|
||||
|
||||
return handle;
|
||||
}
|
||||
public void Release(IUXWaitingHandle handle)
|
||||
{
|
||||
handle.Dispose();
|
||||
}
|
||||
private void Update()
|
||||
private async void OnTick(float deltaTime)
|
||||
{
|
||||
switch (_initializationState)
|
||||
{
|
||||
case InitializationState.None:
|
||||
_initializationState = InitializationState.Initializing;
|
||||
await InitializeAsync();
|
||||
break;
|
||||
case InitializationState.Initializing:
|
||||
return;
|
||||
}
|
||||
while (_initializedHandles.TryDequeue(out var handle))
|
||||
{
|
||||
var container = _container.Create(handleTemplate);
|
||||
handle.container = container;
|
||||
handle.label = container.Get<Label>();
|
||||
var container = _container.Create(_handleTemplate);
|
||||
_visibleHandle.AddElement(handle);
|
||||
handle.VisualElement = container;
|
||||
handle.Label = container.Get<Label>();
|
||||
}
|
||||
while (_messageQueue.TryDequeue(out var message))
|
||||
{
|
||||
message.handle.label.text = message.text;
|
||||
message.handle.Label.text = message.text;
|
||||
}
|
||||
while (_disposeQueue.TryDequeue(out var handle))
|
||||
{
|
||||
handle.container.RemoveFromHierarchy();
|
||||
handle.VisualElement.RemoveFromHierarchy();
|
||||
_visibleHandle.RemoveElement(handle);
|
||||
}
|
||||
_visibleHandle.SetElements(0, _container.childCount>0);
|
||||
_visibleHandle.Invoke();
|
||||
}
|
||||
protected override void Awake()
|
||||
|
||||
public override async UniTask InitializeAsync()
|
||||
{
|
||||
base.Awake();
|
||||
await base.InitializeAsync();
|
||||
_handleTemplate = await ModService.LoadAsset<VisualTreeAsset>(HandleTemplate);
|
||||
if (_handleTemplate is null)
|
||||
{
|
||||
Debug.LogError($"{HandleTemplate} is null");
|
||||
}
|
||||
|
||||
UXUtils.Inject(this);
|
||||
_container.Clear();
|
||||
_root.SetActive(false);
|
||||
|
||||
_initializationState = InitializationState.Initialized;
|
||||
}
|
||||
private void Start()
|
||||
|
||||
private void Dispose(bool dontDispose)
|
||||
{
|
||||
_visibleHandle.AddListener(_root.SetActive);
|
||||
_visibleHandle.Invoke();
|
||||
if (dontDispose is false)
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
_initializationState = 0;
|
||||
base.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user