Net.Like.Xue.Tokyo/Assets/BITKit/Unity/Scripts/UX/Service/UI Toolkit/UIToolKitPanel.cs

284 lines
7.6 KiB
C#
Raw Normal View History

2024-11-03 16:42:23 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2024-12-28 23:19:55 +08:00
using System.Diagnostics;
2024-11-03 16:42:23 +08:00
using System.Threading;
using BITKit.Mod;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
2024-12-28 23:19:55 +08:00
using Debug = UnityEngine.Debug;
2024-11-03 16:42:23 +08:00
// ReSharper disable MemberCanBeProtected.Global
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable MemberCanBePrivate.Global
namespace BITKit.UX
{
2025-02-24 23:03:39 +08:00
public abstract class UIToolKitPanel : IUXPanel,IDisposable
2024-11-03 16:42:23 +08:00
{
public const string USSEntry = "transition_entry";
public const string USSEntryAsync = "transition_entry_async";
public const string USSEntered = "transition_entried";
public const string USSExit = "transition_exit";
public const string USSExitAsync = "transition_exit_async";
public const string USSExited = "transition_exited";
protected readonly IUXService UXService;
protected abstract string DocumentPath { get; }
public VisualElement RootVisualElement { get; set; }
protected VisualTreeAsset VisualTreeAsset { get; private set; }
2024-11-13 17:47:45 +08:00
private readonly ValidHandle _isBusy = new();
2024-11-20 11:36:36 +08:00
public readonly UniTaskCompletionSource WaitUtilInitialized = new();
2024-12-28 23:19:55 +08:00
protected UniTaskCompletionSource WaitUtilTransitionCompleted=new();
2024-11-03 16:42:23 +08:00
protected UIToolKitPanel(IUXService uxService)
{
UXService = uxService;
uxService.Register(this);
2024-11-13 17:47:45 +08:00
InitializeAsync().Forget();
}
private async UniTask InitializeAsync()
{
await _isBusy;
using var b = _isBusy.GetHandle();
if (RootVisualElement is null)
{
VisualTreeAsset = await ModService.LoadAsset<VisualTreeAsset>(DocumentPath);
RootVisualElement = UXService.Root.As<VisualElement>().Create(VisualTreeAsset);
2024-12-28 23:19:55 +08:00
RootVisualElement.name = DocumentPath;
2024-11-13 17:47:45 +08:00
RootVisualElement.pickingMode = PickingMode.Ignore;
RootVisualElement.style.position = Position.Absolute;
RootVisualElement.style.left = 0;
RootVisualElement.style.right = 0;
RootVisualElement.style.top = 0;
RootVisualElement.style.bottom = 0;
2024-12-28 23:19:55 +08:00
foreach (var uss in InitialUssClasses)
{
RootVisualElement.AddToClassList(uss);
}
2024-11-13 17:47:45 +08:00
var invisible = RootVisualElement.Create<VisualElement>();
invisible.name = "invisible_return_generate";
2024-12-28 23:19:55 +08:00
invisible.AddToClassList("background_generate");
2024-11-13 17:47:45 +08:00
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)
{
invisible.style.backgroundColor = new Color(0, 0, 0, 0.9f);
}
2024-12-28 23:19:55 +08:00
UXUtils.Inject(this,RootVisualElement);
2024-11-13 17:47:45 +08:00
RootVisualElement.SetActive(false);
2024-11-20 11:36:36 +08:00
2024-12-28 23:19:55 +08:00
await OnInitiatedAsync.UniTaskFunc();
RootVisualElement.RegisterCallback<TransitionRunEvent>(OnTransitionRun);
2025-02-24 23:03:39 +08:00
RootVisualElement.RegisterCallback<TransitionStartEvent>(OnTransitionStart);
2024-12-28 23:19:55 +08:00
RootVisualElement.RegisterCallback<TransitionEndEvent>(OnTransitionEnd);
RootVisualElement.RegisterCallback<TransitionCancelEvent>(OnTransitionEnd);
WaitUtilTransitionCompleted.TrySetResult();
2025-02-24 23:03:39 +08:00
WaitUtilInitialized.TrySetResult();
OnInitiated?.Invoke();
2024-11-13 17:47:45 +08:00
}
2024-11-03 16:42:23 +08:00
}
2024-12-28 23:19:55 +08:00
private void OnTransitionStart(TransitionStartEvent evt)
{
WaitUtilTransitionCompleted = new();
}
private void OnTransitionRun(TransitionRunEvent evt)
{
//WaitUtilTransitionCompleted = new();
}
2024-11-03 16:42:23 +08:00
protected static readonly InputActionGroup InputActionGroup = new()
{
allowGlobalActivation = false,
Source = nameof(UIToolKitPanel)
};
public virtual bool CloseWhenClickOutside { get;}
public virtual bool IsWindow { get; }
public virtual string Index => GetType().Name;
public virtual bool AllowReload { get; }
public virtual bool AllowCursor { get; }
public virtual bool AllowInput { get; }
2025-02-24 23:03:39 +08:00
public object Root => RootVisualElement;
2024-12-28 23:19:55 +08:00
public virtual string[] InitialUssClasses { get; } = Array.Empty<string>();
2025-02-24 23:03:39 +08:00
public bool IsDisposed { get; private set; }
2024-11-03 16:42:23 +08:00
public bool IsEntered { get; set; }
protected virtual void OnReturn()
{
UXService.Return();
}
protected virtual void OnPanelEntry(){}
protected virtual void OnPanelExit(){}
void IEntryElement.Entry()
{
InputActionGroup.allowInput.AddElement(this);
OnEntry?.Invoke();
}
async UniTask IEntryElement.EntryAsync()
2024-11-13 17:47:45 +08:00
{
await InitializeAsync();
2024-11-03 16:42:23 +08:00
2024-12-28 23:19:55 +08:00
//WaitUtilTransitionCompleted = new();
2024-11-03 16:42:23 +08:00
RootVisualElement.SetActive(true);
2024-12-28 23:19:55 +08:00
//await UniTask.NextFrame();
2025-02-24 23:03:39 +08:00
if (IsWindow)
{
RootVisualElement.BringToFront();
}
2024-12-28 23:19:55 +08:00
2024-11-03 16:42:23 +08:00
RootVisualElement.AddToClassList(USSEntry);
2024-12-28 23:19:55 +08:00
await UniTask.NextFrame();
RootVisualElement.AddToClassList(USSEntryAsync);
await UniTask.NextFrame();
2024-11-03 16:42:23 +08:00
2024-12-28 23:19:55 +08:00
await EntryAsync();
2024-11-03 16:42:23 +08:00
try
{
if (OnEntryAsync is not null)
{
await OnEntryAsync.UniTaskFunc();
}
}
catch (Exception e)
{
BIT4Log.LogException(e);
}
2025-02-24 23:03:39 +08:00
try
{
var cts = new CancellationTokenSource();
cts.CancelAfter(1000);
await WaitUtilTransitionCompleted.Task.AttachExternalCancellation(cts.Token);
}
catch (OperationCanceledException)
{
}
await UniTask.SwitchToMainThread();
2024-12-28 23:19:55 +08:00
RootVisualElement.AddToClassList(USSEntered);
}
private void OnTransitionEnd<TChangeEvent>(TChangeEvent evt)
{
WaitUtilTransitionCompleted.TrySetResult();
}
public virtual UniTask EntryAsync()
2024-11-03 16:42:23 +08:00
{
return UniTask.CompletedTask;
}
void IEntryElement.Entered()
{
OnPanelEntry();
OnEntryCompleted?.Invoke();
2024-12-28 23:19:55 +08:00
RootVisualElement.RemoveFromClassList(USSEntry);
RootVisualElement.RemoveFromClassList(USSEntryAsync);
2024-11-03 16:42:23 +08:00
}
void IEntryElement.Exit()
{
OnPanelExit();
InputActionGroup.allowInput.RemoveElement(this);
OnExit?.Invoke();
}
async UniTask IEntryElement.ExitAsync()
{
RootVisualElement?.RemoveFromClassList(USSEntered);
2024-12-28 23:19:55 +08:00
await UniTask.NextFrame();
RootVisualElement?.AddToClassList(USSExit);
await UniTask.NextFrame();
2024-11-03 16:42:23 +08:00
RootVisualElement?.AddToClassList(USSExitAsync);
2024-12-28 23:19:55 +08:00
await UniTask.NextFrame();
2024-11-03 16:42:23 +08:00
await OnExitAsync.UniTaskFunc();
2025-02-24 23:03:39 +08:00
try
{
var cts = new CancellationTokenSource();
cts.CancelAfter(1000);
await WaitUtilTransitionCompleted.Task.AttachExternalCancellation(cts.Token);
}
catch (OperationCanceledException)
{
}
2024-12-28 23:19:55 +08:00
2024-11-03 16:42:23 +08:00
}
void IEntryElement.Exited()
{
RootVisualElement?.RemoveFromClassList(USSExit);
RootVisualElement?.RemoveFromClassList(USSExitAsync);
2024-12-28 23:19:55 +08:00
RootVisualElement?.AddToClassList(USSExited);
2024-11-03 16:42:23 +08:00
RootVisualElement?.SetActive(false);
if (AllowReload)
{
RootVisualElement?.RemoveFromHierarchy();
RootVisualElement = null;
}
OnExitCompleted?.Invoke();
}
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;
2024-12-28 23:19:55 +08:00
public event Action OnInitiated;
public event Func<UniTask> OnInitiatedAsync;
2024-11-03 16:42:23 +08:00
public virtual void OnTick(float deltaTime)
{
}
2025-02-24 23:03:39 +08:00
public virtual void Dispose()
{
RootVisualElement?.RemoveFromHierarchy();
IsDisposed = true;
}
2024-11-03 16:42:23 +08:00
}
}