This commit is contained in:
CortexCore
2024-11-03 16:38:17 +08:00
parent 056e2cada5
commit 4ba741408d
4693 changed files with 2445 additions and 5443 deletions

View File

@@ -2,17 +2,20 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using BITKit.Mod;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
// ReSharper disable MemberCanBeProtected.Global
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable MemberCanBePrivate.Global
namespace BITKit.UX
{
public class UIToolKitPanel : MonoBehaviour,IUXPanel
public abstract class UIToolKitPanel : IUXPanel
{
public const string USSEntry = "transition_entry";
public const string USSEntryAsync = "transition_entry_async";
@@ -20,146 +23,96 @@ namespace BITKit.UX
public const string USSExit = "transition_exit";
public const string USSExitAsync = "transition_exit_async";
public const string USSExited = "transition_exited";
[RuntimeInitializeOnLoadMethod]
private static void Reload()
{
InputActionGroup = new InputActionGroup
{
allowGlobalActivation = false,
Source = nameof(UIToolKitPanel)
};
InputActionGroup.allowInput.AddElement(0);
}
public UIToolKitPanel()
{
Index = GetType().FullName;
}
[Header(Constant.Header.Components)]
[SerializeField] protected UIDocument document;
[Header(Constant.Header.Settings)]
[SerializeField] private bool isWindow;
[SerializeField] private bool closeWhenClickOutside;
[SerializeField] private bool isAnimate;
[SerializeField] private bool allowCursor;
[SerializeField] private bool allowInput;
protected readonly IUXService UXService;
protected abstract string DocumentPath { get; }
public VisualElement RootVisualElement { get; set; }
protected VisualTreeAsset VisualTreeAsset { get; private set; }
protected UIToolKitPanel(IUXService uxService)
{
UXService = uxService;
uxService.Register(this);
}
[Header(Constant.Header.Settings)]
[SerializeField] private Optional<float> entryDuration;
[SerializeField] private Optional<float> exitDuration;
protected static InputActionGroup InputActionGroup = new()
protected virtual Optional<float> EntryDuration { get; }= new();
protected virtual Optional<float> ExitDuration { get; }= new();
protected static readonly InputActionGroup InputActionGroup = new()
{
allowGlobalActivation = false,
Source = nameof(UIToolKitPanel)
};
public bool IsWindow => isWindow;
public string Index { get; private set; }
public bool AllowCursor => allowCursor;
public bool AllowInput => allowInput;
protected float TargetOpacity { get; private set; }
protected virtual VisualElement background => document.rootVisualElement;
// protected float CurrentOpacity
// {
// get => background?.GetOpacity() ?? currentOpacity;
// set
// {
// currentOpacity = value;
// background?.SetOpacity(value);
// }
// }
protected virtual void Awake()
{
Index= typeof(UIToolKitPanel) == GetType() ? gameObject.name : GetType().Name;
document.rootVisualElement.SetActive(false);
//background?.SetOpacity(0);
if (IsWindow) document.sortingOrder++;
}
protected virtual void Start()
{
UXUtils.Inject(this);
document.rootVisualElement.AddToClassList(USSEntry);
UxService.Register(this);
destroyCancellationToken.Register(() => { UxService.UnRegister(this); });
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)
{
invisible.style.backgroundColor = new Color(0, 0, 0, 0.9f);
}
}
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; }
public bool IsEntered { get; set; }
[BIT]
public void Entry()
{
UxService.Entry(this);
}
protected virtual void OnReturn()
{
UxService.Return();
UXService.Return();
}
protected virtual void OnEnable(){}
protected virtual void OnDisable(){}
protected virtual void OnPanelEntry(){}
protected virtual void OnPanelExit(){}
void IEntryElement.Entry()
{
try
{
document.rootVisualElement.SetActive(true);
OnEntry?.Invoke();
}
catch (Exception e)
{
Debug.Log(gameObject.name);
throw;
}
}
void IEntryElement.Entry()
{
InputActionGroup.allowInput.AddElement(this);
OnEntry?.Invoke();
}
async UniTask IEntryElement.EntryAsync()
{
document.rootVisualElement.AddToClassList(USSEntry);
document.rootVisualElement.AddToClassList(USSEntryAsync);
if (RootVisualElement is null)
{
VisualTreeAsset = await ModService.LoadAsset<VisualTreeAsset>(DocumentPath);
RootVisualElement = UXService.Root.As<VisualElement>().Create(VisualTreeAsset);
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;
if (entryDuration.Allow)
var invisible = 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)
{
invisible.style.backgroundColor = new Color(0, 0, 0, 0.9f);
}
UXUtils.Inject(this);
}
RootVisualElement.SetActive(true);
RootVisualElement.AddToClassList(USSEntry);
RootVisualElement.AddToClassList(USSEntryAsync);
if (EntryDuration.Allow)
{
var task = EntryAsync();
var durationTask = UniTask.Delay(TimeSpan.FromSeconds(entryDuration.Value), cancellationToken: destroyCancellationToken);
var durationTask = UniTask.Delay(TimeSpan.FromSeconds(EntryDuration.Value));
await durationTask;
document.rootVisualElement.RemoveFromClassList(USSEntry);
document.rootVisualElement.RemoveFromClassList(USSEntryAsync);
document.rootVisualElement.AddToClassList(USSEntered);
RootVisualElement.RemoveFromClassList(USSEntry);
RootVisualElement.RemoveFromClassList(USSEntryAsync);
RootVisualElement.AddToClassList(USSEntered);
await task;
}
@@ -167,6 +120,19 @@ namespace BITKit.UX
{
await EntryAsync();
}
try
{
if (OnEntryAsync is not null)
{
await OnEntryAsync.UniTaskFunc();
}
}
catch (Exception e)
{
BIT4Log.LogException(e);
}
}
public virtual UniTask EntryAsync()
{
@@ -175,32 +141,48 @@ namespace BITKit.UX
void IEntryElement.Entered()
{
OnPanelEntry();
OnEntryCompleted?.Invoke();
}
void IEntryElement.Exit()
{
document.rootVisualElement.AddToClassList(USSExit);
//if (IsValid is false) return;
RootVisualElement?.AddToClassList(USSExit);
OnPanelExit();
InputActionGroup.allowInput.RemoveElement(this);
OnExit?.Invoke();
}
async UniTask IEntryElement.ExitAsync()
{
document.rootVisualElement.RemoveFromClassList(USSEntered);
document.rootVisualElement.AddToClassList(USSExitAsync);
if (entryDuration.Allow is false) return;
await UniTask.Delay(TimeSpan.FromSeconds(entryDuration.Value), cancellationToken: destroyCancellationToken);
RootVisualElement?.RemoveFromClassList(USSEntered);
RootVisualElement?.AddToClassList(USSExitAsync);
await OnExitAsync.UniTaskFunc();
if (EntryDuration.Allow is false) return;
await UniTask.Delay(TimeSpan.FromSeconds(EntryDuration.Value));
}
void IEntryElement.Exited()
{
document.rootVisualElement.RemoveFromClassList(USSExit);
document.rootVisualElement.RemoveFromClassList(USSExitAsync);
document.rootVisualElement.AddToClassList(USSEntry);
document.rootVisualElement.SetActive(false);
OnExit?.Invoke();
RootVisualElement?.RemoveFromClassList(USSExit);
RootVisualElement?.RemoveFromClassList(USSExitAsync);
RootVisualElement?.AddToClassList(USSEntry);
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 virtual void OnUpdate(float deltaTime)
public event Func<UniTask> OnExitAsync;
public event Action OnExitCompleted;
public virtual void OnTick(float deltaTime)
{
}
}