This commit is contained in:
parent
66ba9c8a77
commit
d33e1c1713
|
@ -1,4 +1,4 @@
|
|||
#if UNITY_5_3_OR_NEWER
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
|
@ -9,7 +9,9 @@ using System.Threading;
|
|||
#if UNITY_WINDOW
|
||||
using AnotherFileBrowser.Windows;
|
||||
#endif
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
using UnityEngine;
|
||||
#endif
|
||||
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
|
@ -225,5 +227,3 @@ namespace BITKit.IO
|
|||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
|
@ -78,47 +78,45 @@ namespace BITKit.UX
|
|||
background?.SetOpacity(0);
|
||||
if (IsWindow) document.sortingOrder++;
|
||||
}
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
BITKit.UX.UXUtils.Inject(this);
|
||||
|
||||
|
||||
UXService.Register(this);
|
||||
destroyCancellationToken.Register(() =>
|
||||
{
|
||||
UXService.UnRegister(this);
|
||||
});
|
||||
|
||||
if(IsValid && autoEntry)
|
||||
destroyCancellationToken.Register(() => { UXService.UnRegister(this); });
|
||||
|
||||
if (IsValid && autoEntry)
|
||||
UXService.Entry(this);
|
||||
|
||||
var returnButton= document.rootVisualElement.Q("return-button");
|
||||
|
||||
var returnButton = document.rootVisualElement.Q("return-button");
|
||||
returnButton?.RegisterCallback<MouseDownEvent>(x =>
|
||||
{
|
||||
if (x.button is 0)
|
||||
OnReturn();
|
||||
});
|
||||
|
||||
|
||||
var invisible = document.rootVisualElement.Create<VisualElement>();
|
||||
invisible.name = "invisible_return_generate";
|
||||
invisible.style.position = Position.Absolute;
|
||||
invisible.pickingMode = PickingMode.Ignore;
|
||||
invisible.style.left = 0;
|
||||
invisible.style.right = 0;
|
||||
invisible.style.top = 0;
|
||||
invisible.style.bottom = 0;
|
||||
invisible.SendToBack();
|
||||
|
||||
if (closeWhenClickOutside)
|
||||
{
|
||||
invisible.RegisterCallback<MouseDownEvent>(x => { OnReturn(); });
|
||||
invisible.pickingMode = PickingMode.Position;
|
||||
}
|
||||
if (isWindow)
|
||||
{
|
||||
var invisible = document.rootVisualElement.Create<VisualElement>();
|
||||
invisible.name = "invisible_return_generate";
|
||||
invisible.style.position = Position.Absolute;
|
||||
invisible.style.left = 0;
|
||||
invisible.style.right = 0;
|
||||
invisible.style.top = 0;
|
||||
invisible.style.bottom = 0;
|
||||
invisible.SendToBack();
|
||||
invisible.style.backgroundColor = new Color(0, 0, 0, 0.9f);
|
||||
{
|
||||
if(closeWhenClickOutside)
|
||||
invisible.RegisterCallback<MouseDownEvent>(x =>
|
||||
{
|
||||
OnReturn();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public bool IsEntered { get; set; }
|
||||
public void Entry()
|
||||
{
|
||||
|
|
|
@ -39,22 +39,8 @@ namespace BITKit
|
|||
internal VisualElement container;
|
||||
internal Label label;
|
||||
internal Action OnDispose;
|
||||
internal Action<Label,string> OnSetMessage;
|
||||
public async void SetMessage(string message)
|
||||
{
|
||||
OnSetMessage?.Invoke(label,message);
|
||||
// await UniTask.SwitchToMainThread();
|
||||
// BIT4Log.Log<UXWaiting>($"SetMessage {message}");
|
||||
// if(container.panel!=null)
|
||||
// {
|
||||
// label.text = message;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// BIT4Log.Log<UXWaiting>("WaitingHandle is destroyed");
|
||||
// }
|
||||
}
|
||||
|
||||
internal Action<string> OnSetMessage;
|
||||
public void SetMessage(string message) => OnSetMessage?.Invoke(message);
|
||||
public void Dispose() => OnDispose?.Invoke();
|
||||
}
|
||||
|
||||
|
@ -66,90 +52,61 @@ namespace BITKit
|
|||
[UXBindPath("waiting-root")]
|
||||
private VisualElement _root;
|
||||
|
||||
private Pool<WaitingHandle> _pool;
|
||||
private readonly HashSet<WaitingHandle> _handles = new();
|
||||
private readonly ConcurrentQueue<(Label label,string text)> _messageQueue = new();
|
||||
|
||||
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();
|
||||
public IUXWaitingHandle Get()
|
||||
{
|
||||
var handle = _pool.Take();
|
||||
Active();
|
||||
_handles.Add(handle);
|
||||
handle.OnDispose = () => Release(handle);
|
||||
return handle;
|
||||
async void Active()
|
||||
var handle = new WaitingHandle();
|
||||
_initializedHandles.Enqueue(handle);
|
||||
handle.OnSetMessage = (text) =>
|
||||
{
|
||||
await UniTask.SwitchToMainThread();
|
||||
handle.As<WaitingHandle>().container.SetActive(true);
|
||||
_root.SetActive(true);
|
||||
}
|
||||
_messageQueue.Enqueue((handle,text));
|
||||
};
|
||||
handle.OnDispose = () =>
|
||||
{
|
||||
_disposeQueue.Enqueue(handle);
|
||||
};
|
||||
return handle;
|
||||
}
|
||||
public async void Release(IUXWaitingHandle handle)
|
||||
public void Release(IUXWaitingHandle handle)
|
||||
{
|
||||
await UniTask.SwitchToMainThread();
|
||||
if (destroyCancellationToken.IsCancellationRequested) return;
|
||||
_pool.Return(handle.As<WaitingHandle>());
|
||||
_handles.Remove(handle.As<WaitingHandle>());
|
||||
Check();
|
||||
handle.Dispose();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
while (_initializedHandles.TryDequeue(out var handle))
|
||||
{
|
||||
var container = _container.Create(handleTemplate);
|
||||
handle.container = container;
|
||||
handle.label = container.Get<Label>();
|
||||
}
|
||||
while (_messageQueue.TryDequeue(out var message))
|
||||
{
|
||||
message.label.text = message.text;
|
||||
message.handle.label.text = message.text;
|
||||
}
|
||||
while (_disposeQueue.TryDequeue(out var handle))
|
||||
{
|
||||
handle.container.RemoveFromHierarchy();
|
||||
}
|
||||
_visibleHandle.SetElements(0, _container.childCount>0);
|
||||
}
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
UXUtils.Inject(this);
|
||||
_container.Clear();
|
||||
_pool = new Pool<WaitingHandle>(Create,OnReset,10);
|
||||
if (asGlobal)
|
||||
{
|
||||
DI.Register<IUXWaiting>(this);
|
||||
}
|
||||
_root.SetActive(false);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Check();
|
||||
}
|
||||
|
||||
private async void OnReset(IUXWaitingHandle obj)
|
||||
{
|
||||
await UniTask.SwitchToMainThread();
|
||||
obj.As<WaitingHandle>().container.SetActive(false);
|
||||
}
|
||||
[BIT]
|
||||
private async void Check()
|
||||
{
|
||||
await UniTask.Yield();
|
||||
await UniTask.SwitchToMainThread();
|
||||
if(destroyCancellationToken.IsCancellationRequested) return;
|
||||
_root.SetActive(_handles.Count>0);
|
||||
}
|
||||
private WaitingHandle Create()
|
||||
{
|
||||
var handle = new WaitingHandle
|
||||
{
|
||||
container = _container.Create(handleTemplate),
|
||||
label = _container.Q<Label>(UXConstant.ContextLabel)
|
||||
};
|
||||
|
||||
handle.OnSetMessage = (label, text) =>
|
||||
{
|
||||
_messageQueue.Enqueue((label,text));
|
||||
};
|
||||
|
||||
handle.SetMessage($"未初始化");
|
||||
|
||||
handle.container.SetActive(false);
|
||||
|
||||
return handle;
|
||||
_visibleHandle.AddListener(_root.SetActive);
|
||||
_visibleHandle.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements">
|
||||
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False">
|
||||
<ui:VisualElement style="flex-grow: 1;">
|
||||
<ui:Label tabindex="-1" text="等待加载中..." parse-escape-sequences="true" display-tooltip-when-elided="true" name="context-label" />
|
||||
<ui:Label tabindex="-1" text="等待加载中..." parse-escape-sequences="true" display-tooltip-when-elided="true" name="Label--0" />
|
||||
</ui:VisualElement>
|
||||
</ui:UXML>
|
||||
|
|
Loading…
Reference in New Issue