1
This commit is contained in:
@@ -40,7 +40,6 @@ namespace BITKit.UX
|
||||
uxService.Register(this);
|
||||
InitializeAsync().Forget();
|
||||
}
|
||||
|
||||
private async UniTask InitializeAsync()
|
||||
{
|
||||
await _isBusy;
|
||||
@@ -89,20 +88,20 @@ namespace BITKit.UX
|
||||
|
||||
UXUtils.Inject(this,RootVisualElement);
|
||||
|
||||
OnInitiated?.Invoke();
|
||||
|
||||
RootVisualElement.SetActive(false);
|
||||
|
||||
await OnInitiatedAsync.UniTaskFunc();
|
||||
|
||||
WaitUtilInitialized.TrySetResult();
|
||||
|
||||
RootVisualElement.RegisterCallback<TransitionRunEvent>(OnTransitionRun);
|
||||
RootVisualElement.RegisterCallback<TransitionStartEvent>(OnTransitionStart);
|
||||
RootVisualElement.RegisterCallback<TransitionStartEvent>(OnTransitionStart);
|
||||
RootVisualElement.RegisterCallback<TransitionEndEvent>(OnTransitionEnd);
|
||||
RootVisualElement.RegisterCallback<TransitionCancelEvent>(OnTransitionEnd);
|
||||
|
||||
WaitUtilTransitionCompleted.TrySetResult();
|
||||
|
||||
WaitUtilInitialized.TrySetResult();
|
||||
|
||||
OnInitiated?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,6 +126,7 @@ namespace BITKit.UX
|
||||
public virtual bool AllowReload { get; }
|
||||
public virtual bool AllowCursor { get; }
|
||||
public virtual bool AllowInput { get; }
|
||||
public object Root => RootVisualElement;
|
||||
public virtual string[] InitialUssClasses { get; } = Array.Empty<string>();
|
||||
public bool IsDisposed { get; private set; }
|
||||
|
||||
@@ -152,6 +152,11 @@ namespace BITKit.UX
|
||||
RootVisualElement.SetActive(true);
|
||||
|
||||
//await UniTask.NextFrame();
|
||||
|
||||
if (IsWindow)
|
||||
{
|
||||
RootVisualElement.BringToFront();
|
||||
}
|
||||
|
||||
RootVisualElement.AddToClassList(USSEntry);
|
||||
|
||||
@@ -174,9 +179,19 @@ namespace BITKit.UX
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
|
||||
await WaitUtilTransitionCompleted.Task;
|
||||
|
||||
try
|
||||
{
|
||||
var cts = new CancellationTokenSource();
|
||||
cts.CancelAfter(1000);
|
||||
await WaitUtilTransitionCompleted.Task.AttachExternalCancellation(cts.Token);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
|
||||
await UniTask.SwitchToMainThread();
|
||||
|
||||
RootVisualElement.AddToClassList(USSEntered);
|
||||
}
|
||||
private void OnTransitionEnd<TChangeEvent>(TChangeEvent evt)
|
||||
@@ -218,8 +233,17 @@ namespace BITKit.UX
|
||||
await UniTask.NextFrame();
|
||||
|
||||
await OnExitAsync.UniTaskFunc();
|
||||
|
||||
try
|
||||
{
|
||||
var cts = new CancellationTokenSource();
|
||||
cts.CancelAfter(1000);
|
||||
await WaitUtilTransitionCompleted.Task.AttachExternalCancellation(cts.Token);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
|
||||
await WaitUtilTransitionCompleted.Task;
|
||||
}
|
||||
void IEntryElement.Exited()
|
||||
{
|
||||
@@ -249,7 +273,7 @@ namespace BITKit.UX
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
public virtual void Dispose()
|
||||
{
|
||||
RootVisualElement?.RemoveFromHierarchy();
|
||||
IsDisposed = true;
|
||||
|
141
Src/Unity/Scripts/UX/Service/UI Toolkit/UIToolkitSubPanel.cs
Normal file
141
Src/Unity/Scripts/UX/Service/UI Toolkit/UIToolkitSubPanel.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.UX
|
||||
{
|
||||
public class UIToolkitSubPanel<T> :IUXPanel where T : IUXPanel
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
protected readonly T Panel;
|
||||
|
||||
protected readonly ValidHandle IsVisible=new();
|
||||
private readonly ValidHandle _busy = new();
|
||||
|
||||
public UIToolkitSubPanel(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
|
||||
Panel = serviceProvider.GetRequiredService<T>();
|
||||
|
||||
Panel.OnInitiated += OnInitiated;
|
||||
Panel.OnInitiatedAsync += OnInitiatedAsync;
|
||||
|
||||
if (Panel is UIToolKitPanel uiToolKitPanel)
|
||||
{
|
||||
if (uiToolKitPanel.WaitUtilInitialized.Task.Status is UniTaskStatus.Succeeded)
|
||||
{
|
||||
OnInitiatedAsync().Forget();
|
||||
OnInitiated();
|
||||
}
|
||||
}
|
||||
|
||||
Panel.OnEntry +=()=> IsVisible.RemoveDisableElements(this);
|
||||
Panel.OnExit +=()=> IsVisible.AddDisableElements(this);
|
||||
|
||||
IsVisible.AddListener(IsVisibleCallback);
|
||||
}
|
||||
|
||||
private async void IsVisibleCallback(bool x)
|
||||
{
|
||||
await _busy;
|
||||
using var busy = _busy.GetHandle();
|
||||
if (x)
|
||||
{
|
||||
OnEntry?.Invoke();
|
||||
OnEntryAsync?.Invoke();
|
||||
OnEntryCompleted?.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
OnExit?.Invoke();
|
||||
OnExitAsync?.Invoke();
|
||||
OnExitCompleted?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual UniTask OnInitiatedAsync()
|
||||
{
|
||||
UXUtils.Inject(this,Panel.Root as VisualElement);
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
protected virtual void OnInitiated()
|
||||
{ UXUtils.Inject(this,Panel.Root as VisualElement);
|
||||
|
||||
}
|
||||
|
||||
bool IEntryElement.IsEntered
|
||||
{
|
||||
get => Panel.IsEntered;
|
||||
set => Panel.IsEntered = value;
|
||||
}
|
||||
|
||||
void IEntryElement.Entry()
|
||||
{
|
||||
Panel.Entry();
|
||||
}
|
||||
|
||||
UniTask IEntryElement.EntryAsync()
|
||||
{
|
||||
return Panel.EntryAsync();
|
||||
}
|
||||
|
||||
void IEntryElement.Entered()
|
||||
{
|
||||
Panel.Entered();
|
||||
}
|
||||
|
||||
void IEntryElement.Exit()
|
||||
{
|
||||
Panel.Exit();
|
||||
}
|
||||
|
||||
UniTask IEntryElement.ExitAsync()
|
||||
{
|
||||
return Panel.ExitAsync();
|
||||
}
|
||||
|
||||
void IEntryElement.Exited()
|
||||
{
|
||||
Panel.Exited();
|
||||
}
|
||||
|
||||
bool IUXPanel.IsWindow => Panel.IsWindow;
|
||||
|
||||
string IUXPanel.Index => Panel.Index;
|
||||
|
||||
bool IUXPanel.AllowCursor => Panel.AllowCursor;
|
||||
|
||||
bool IUXPanel.AllowInput => Panel.AllowInput;
|
||||
|
||||
object IUXPanel.Root => Panel.Root;
|
||||
public event Action OnEntry;
|
||||
public event Func<UniTask> OnEntryAsync;
|
||||
public event Action OnEntryCompleted;
|
||||
public event Action OnExit;
|
||||
public event Func<UniTask> OnExitAsync;
|
||||
public event Action OnExitCompleted;
|
||||
|
||||
event Action IUXPanel.OnInitiated
|
||||
{
|
||||
add => Panel.OnInitiated += value;
|
||||
remove => Panel.OnInitiated -= value;
|
||||
}
|
||||
|
||||
event Func<UniTask> IUXPanel.OnInitiatedAsync
|
||||
{
|
||||
add => Panel.OnInitiatedAsync += value;
|
||||
remove => Panel.OnInitiatedAsync -= value;
|
||||
}
|
||||
|
||||
void IUXPanel.OnTick(float deltaTime)
|
||||
{
|
||||
Panel.OnTick(deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3b6d7b7e48316f04f9130c5301091ee7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -5,7 +5,10 @@ using System.Threading;
|
||||
using BITKit.Mod;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.UIElements;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
@@ -25,9 +28,26 @@ namespace BITKit.UX
|
||||
_serviceProvider = serviceProvider;
|
||||
_cancellationTokenSource = cancellationTokenSource;
|
||||
_entryGroup.OnEntry += OnEntry;
|
||||
|
||||
_windowEntryGroup.OnEntry += OnWindowEntry;
|
||||
_windowEntryGroup.OnExit += OnWindowExit;
|
||||
_ticker.Add(OnTick);
|
||||
}
|
||||
|
||||
|
||||
private void OnWindowExit(IUXPanel obj)
|
||||
{
|
||||
BITAppForUnity.AllowCursor.RemoveElement(_windowEntryGroup);
|
||||
if (obj.AllowInput is false)
|
||||
BITInputSystem.AllowInput.RemoveDisableElements(_windowEntryGroup);
|
||||
}
|
||||
|
||||
private void OnWindowEntry(IUXPanel obj)
|
||||
{
|
||||
BITAppForUnity.AllowCursor.SetElements(_windowEntryGroup, obj.AllowCursor);
|
||||
if (obj.AllowInput is false)
|
||||
BITInputSystem.AllowInput.AddDisableElements(_windowEntryGroup);
|
||||
}
|
||||
|
||||
private readonly EntryGroup<IUXPanel> _entryGroup = new();
|
||||
private readonly EntryGroup<IUXPanel> _windowEntryGroup = new();
|
||||
/// <summary>
|
||||
@@ -73,9 +93,9 @@ namespace BITKit.UX
|
||||
|
||||
public string SettingsPath { get; set; } = "ux_panel_settings";
|
||||
public object Root { get; private set; }
|
||||
public static VisualElement RootVisualElement { get; private set; }
|
||||
public async UniTask InitializeAsync()
|
||||
{
|
||||
|
||||
var gameObject = new GameObject("UXService");
|
||||
Object.DontDestroyOnLoad(gameObject);
|
||||
|
||||
@@ -87,6 +107,11 @@ Object.Destroy(gameObject);
|
||||
var document = gameObject.AddComponent<UIDocument>();
|
||||
try
|
||||
{
|
||||
if (Touchscreen.current is not null && SettingsPath == "ux_panel_settings")
|
||||
{
|
||||
SettingsPath = "ux_panel_settings_mobile";
|
||||
}
|
||||
|
||||
var panelSettings =await ModService.LoadAsset<PanelSettings>(SettingsPath);
|
||||
document.panelSettings = panelSettings;
|
||||
}
|
||||
@@ -96,8 +121,12 @@ Object.Destroy(gameObject);
|
||||
throw;
|
||||
}
|
||||
|
||||
|
||||
Root = document.rootVisualElement;
|
||||
Root = RootVisualElement= document.rootVisualElement;
|
||||
|
||||
if (Touchscreen.current is not null)
|
||||
{
|
||||
RootVisualElement.AddToClassList("mobile");
|
||||
}
|
||||
}
|
||||
|
||||
public void Register(IUXPanel panel) => _registryQueue.Enqueue(panel);
|
||||
@@ -115,6 +144,22 @@ Object.Destroy(gameObject);
|
||||
public void Entry(string panelName) => _entryQueueByName.TryAdd(panelName);
|
||||
public IUXPanel CurrentPanel => _currentPanel;
|
||||
public event Action<IUXPanel, IUXPanel> OnPanelChanged;
|
||||
public bool TryPick(float2 position, out object obj)
|
||||
{
|
||||
obj = null;
|
||||
if (!EventSystem.current.IsPointerOverGameObject())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
position.y = Screen.height - position.y;
|
||||
|
||||
var ve = RootVisualElement.panel.Pick(RuntimePanelUtils.ScreenToPanel(RootVisualElement.panel, position));
|
||||
|
||||
obj = ve;
|
||||
|
||||
return obj is not null;
|
||||
}
|
||||
|
||||
public void Return()
|
||||
{
|
||||
@@ -143,6 +188,7 @@ Object.Destroy(gameObject);
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
while (_registryQueue.TryDequeue(out var result))
|
||||
{
|
||||
if (result is null) continue;
|
||||
@@ -201,6 +247,13 @@ Object.Destroy(gameObject);
|
||||
|
||||
public async void Dispose()
|
||||
{
|
||||
foreach (var panelsValue in _panels.Values)
|
||||
{
|
||||
if (panelsValue is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
_ticker.Remove(OnTick);
|
||||
await UniTask.SwitchToMainThread();
|
||||
if (_currentPanel is not null)
|
||||
|
Reference in New Issue
Block a user