BITKit/Src/Unity/Scripts/UX/Service/UI Toolkit/UIToolKitPanel.cs

290 lines
7.8 KiB
C#
Raw Normal View History

2023-08-23 01:59:26 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
2023-08-23 01:59:26 +08:00
using System.Threading;
2024-11-03 16:38:17 +08:00
using BITKit.Mod;
2025-03-24 14:42:40 +08:00
using BITKit.StateMachine;
2023-08-23 01:59:26 +08:00
using Cysharp.Threading.Tasks;
using UnityEngine;
2024-07-15 17:26:08 +08:00
using UnityEngine.Serialization;
2024-11-03 16:38:17 +08:00
using UnityEngine.InputSystem;
2023-08-23 01:59:26 +08:00
using UnityEngine.UIElements;
using Debug = UnityEngine.Debug;
2023-08-23 01:59:26 +08:00
// ReSharper disable MemberCanBeProtected.Global
2023-09-01 14:35:05 +08:00
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global
// ReSharper disable UnusedMember.Global
2024-11-03 16:38:17 +08:00
// ReSharper disable MemberCanBePrivate.Global
2023-08-23 01:59:26 +08:00
namespace BITKit.UX
{
2025-03-24 14:42:40 +08:00
public abstract class UIToolKitPanel :StateAsync, IUXPanel,IDisposable
2023-08-23 01:59:26 +08:00
{
2024-08-10 18:16: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";
2024-11-03 16:38:17 +08:00
protected readonly IUXService UXService;
protected abstract string DocumentPath { get; }
public VisualElement RootVisualElement { get; set; }
protected VisualTreeAsset VisualTreeAsset { get; private set; }
2024-11-08 17:28:07 +08:00
private readonly ValidHandle _isBusy = new();
2024-11-20 11:36:51 +08:00
public readonly UniTaskCompletionSource WaitUtilInitialized = new();
protected UniTaskCompletionSource WaitUtilTransitionCompleted=new();
2024-11-03 16:38:17 +08:00
protected UIToolKitPanel(IUXService uxService)
2023-08-23 01:59:26 +08:00
{
2024-11-03 16:38:17 +08:00
UXService = uxService;
uxService.Register(this);
2024-11-08 17:28:07 +08:00
}
2025-04-05 09:49:01 +08:00
2025-04-14 15:39:28 +08:00
public override async UniTask InitializeAsync()
2024-11-08 17:28:07 +08:00
{
await _isBusy;
using var b = _isBusy.GetHandle();
if (RootVisualElement is null)
{
VisualTreeAsset = await ModService.LoadAsset<VisualTreeAsset>(DocumentPath);
2025-04-05 09:49:01 +08:00
try
{
RootVisualElement = UXService.Root.As<VisualElement>().Create(VisualTreeAsset);
}
catch (Exception e)
{
Debug.LogWarning(GetType().Name+DocumentPath);
throw;
}
2024-12-26 18:00:05 +08:00
RootVisualElement.name = DocumentPath;
2024-11-08 17:28:07 +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-27 11:00:35 +08:00
foreach (var uss in InitialUssClasses)
{
RootVisualElement.AddToClassList(uss);
}
2024-11-08 17:28:07 +08:00
var invisible = RootVisualElement.Create<VisualElement>();
invisible.name = "invisible_return_generate";
2024-12-27 11:00:35 +08:00
invisible.AddToClassList("background_generate");
2024-11-08 17:28:07 +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-26 18:00:05 +08:00
UXUtils.Inject(this,RootVisualElement);
2024-11-08 17:28:07 +08:00
RootVisualElement.SetActive(false);
2024-11-20 11:36:51 +08:00
2024-12-17 17:51:58 +08:00
await OnInitiatedAsync.UniTaskFunc();
RootVisualElement.RegisterCallback<TransitionRunEvent>(OnTransitionRun);
2025-02-24 23:02:43 +08:00
RootVisualElement.RegisterCallback<TransitionStartEvent>(OnTransitionStart);
RootVisualElement.RegisterCallback<TransitionEndEvent>(OnTransitionEnd);
RootVisualElement.RegisterCallback<TransitionCancelEvent>(OnTransitionEnd);
WaitUtilTransitionCompleted.TrySetResult();
2025-02-24 23:02:43 +08:00
WaitUtilInitialized.TrySetResult();
OnInitiated?.Invoke();
2024-11-08 17:28:07 +08:00
}
2023-08-23 01:59:26 +08:00
}
private void OnTransitionStart(TransitionStartEvent evt)
{
WaitUtilTransitionCompleted = new();
}
private void OnTransitionRun(TransitionRunEvent evt)
{
//WaitUtilTransitionCompleted = new();
}
2025-03-24 14:42:40 +08:00
protected readonly InputActionGroup InputActionGroup = new()
2023-10-24 23:38:22 +08:00
{
2024-03-31 23:31:00 +08:00
allowGlobalActivation = false,
Source = nameof(UIToolKitPanel)
2023-10-24 23:38:22 +08:00
};
2024-11-03 16:38:17 +08:00
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:02:43 +08:00
public object Root => RootVisualElement;
2024-12-27 11:00:35 +08:00
public virtual string[] InitialUssClasses { get; } = Array.Empty<string>();
2025-01-12 11:13:19 +08:00
public bool IsDisposed { get; private set; }
2024-08-04 11:39:37 +08:00
2023-11-15 23:55:06 +08:00
public bool IsEntered { get; set; }
2024-04-06 16:33:32 +08:00
protected virtual void OnReturn()
{
2024-11-03 16:38:17 +08:00
UXService.Return();
2024-04-06 16:33:32 +08:00
}
2024-03-31 23:31:00 +08:00
protected virtual void OnPanelEntry(){}
protected virtual void OnPanelExit(){}
2025-03-24 14:42:40 +08:00
public override void OnStateEntry(IState old)
2024-11-03 16:38:17 +08:00
{
InputActionGroup.allowInput.AddElement(this);
OnEntry?.Invoke();
}
2025-03-24 14:42:40 +08:00
public override async UniTask OnStateEntryAsync(IState old)
2024-11-08 17:28:07 +08:00
{
await InitializeAsync();
2024-08-10 18:16:23 +08:00
//WaitUtilTransitionCompleted = new();
2024-11-03 16:38:17 +08:00
RootVisualElement.SetActive(true);
//await UniTask.NextFrame();
2025-02-24 23:02:43 +08:00
if (IsWindow)
{
RootVisualElement.BringToFront();
}
2024-11-03 16:38:17 +08:00
RootVisualElement.AddToClassList(USSEntry);
2024-08-10 18:16:23 +08:00
await UniTask.NextFrame();
RootVisualElement.AddToClassList(USSEntryAsync);
await UniTask.NextFrame();
2024-08-10 18:16:23 +08:00
await EntryAsync();
2024-11-03 16:38:17 +08:00
try
{
if (OnEntryAsync is not null)
{
await OnEntryAsync.UniTaskFunc();
}
}
catch (Exception e)
{
BIT4Log.LogException(e);
}
2025-02-24 23:02:43 +08:00
try
{
var cts = new CancellationTokenSource();
cts.CancelAfter(1000);
await WaitUtilTransitionCompleted.Task.AttachExternalCancellation(cts.Token);
}
catch (OperationCanceledException)
{
}
await UniTask.SwitchToMainThread();
RootVisualElement.AddToClassList(USSEntered);
2025-03-24 14:42:40 +08:00
OnPanelEntry();
OnEntryCompleted?.Invoke();
RootVisualElement.RemoveFromClassList(USSEntry);
RootVisualElement.RemoveFromClassList(USSEntryAsync);
}
private void OnTransitionEnd<TChangeEvent>(TChangeEvent evt)
{
WaitUtilTransitionCompleted.TrySetResult();
}
public virtual UniTask EntryAsync()
2024-08-10 18:16:23 +08:00
{
return UniTask.CompletedTask;
}
2023-11-15 23:55:06 +08:00
2025-03-24 14:42:40 +08:00
public override void OnStateExit(IState old, IState newState)
2023-08-23 01:59:26 +08:00
{
2024-03-31 23:31:00 +08:00
OnPanelExit();
2024-11-03 16:38:17 +08:00
InputActionGroup.allowInput.RemoveElement(this);
OnExit?.Invoke();
2023-11-15 23:55:06 +08:00
}
2025-03-24 14:42:40 +08:00
public override async UniTask OnStateExitAsync(IState old, IState newState)
2023-11-15 23:55:06 +08:00
{
2024-11-03 16:38:17 +08:00
RootVisualElement?.RemoveFromClassList(USSEntered);
await UniTask.NextFrame();
RootVisualElement?.AddToClassList(USSExit);
await UniTask.NextFrame();
2024-11-03 16:38:17 +08:00
RootVisualElement?.AddToClassList(USSExitAsync);
await UniTask.NextFrame();
2024-11-03 16:38:17 +08:00
await OnExitAsync.UniTaskFunc();
2025-02-24 23:02:43 +08:00
try
{
var cts = new CancellationTokenSource();
cts.CancelAfter(1000);
await WaitUtilTransitionCompleted.Task.AttachExternalCancellation(cts.Token);
}
catch (OperationCanceledException)
{
}
2024-11-03 16:38:17 +08:00
RootVisualElement?.RemoveFromClassList(USSExit);
RootVisualElement?.RemoveFromClassList(USSExitAsync);
RootVisualElement?.AddToClassList(USSExited);
2024-11-03 16:38:17 +08:00
RootVisualElement?.SetActive(false);
if (AllowReload)
{
RootVisualElement?.RemoveFromHierarchy();
RootVisualElement = null;
}
OnExitCompleted?.Invoke();
2023-11-15 23:55:06 +08:00
}
2023-09-01 14:35:05 +08:00
public event Action OnEntry;
2024-11-03 16:38:17 +08:00
public event Func<UniTask> OnEntryAsync;
public event Action OnEntryCompleted;
2023-09-01 14:35:05 +08:00
public event Action OnExit;
2024-11-03 16:38:17 +08:00
public event Func<UniTask> OnExitAsync;
public event Action OnExitCompleted;
2024-12-17 17:51:58 +08:00
public event Action OnInitiated;
public event Func<UniTask> OnInitiatedAsync;
2024-11-03 16:38:17 +08:00
public virtual void OnTick(float deltaTime)
2023-11-15 23:55:06 +08:00
{
}
2025-01-12 11:13:19 +08:00
2025-02-24 23:02:43 +08:00
public virtual void Dispose()
2025-01-12 11:13:19 +08:00
{
2025-03-24 14:42:40 +08:00
InputActionGroup.Dispose();
2025-01-12 11:13:19 +08:00
RootVisualElement?.RemoveFromHierarchy();
IsDisposed = true;
}
2023-08-23 01:59:26 +08:00
}
}