Files
BITFALL/Assets/BITKit/Unity/Scripts/UX/Service/UI Toolkit/UIToolKitPanel.cs

258 lines
7.3 KiB
C#
Raw Normal View History

2023-08-23 01:59:40 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
2024-08-11 12:46:15 +08:00
using UnityEngine.Serialization;
2023-08-23 01:59:40 +08:00
using UnityEngine.UIElements;
// ReSharper disable MemberCanBeProtected.Global
2023-08-27 02:58:19 +08:00
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global
// ReSharper disable UnusedMember.Global
2023-08-23 01:59:40 +08:00
namespace BITKit.UX
{
public class UIToolKitPanel : MonoBehaviour,IUXPanel
{
2024-08-11 12:46:15 +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-01-03 00:27:12 +08:00
[RuntimeInitializeOnLoadMethod]
private static void Reload()
{
inputActionGroup = new InputActionGroup
{
allowGlobalActivation = false,
Source = nameof(UIToolKitPanel)
};
inputActionGroup.allowInput.AddElement(0);
}
2023-08-23 01:59:40 +08:00
public UIToolKitPanel()
{
Index = GetType().FullName;
}
[Header(Constant.Header.Components)]
[SerializeField] protected UIDocument document;
2024-08-11 12:46:15 +08:00
[Header(Constant.Header.Settings)]
[SerializeField, ReadOnly] private bool isActive;
//[SerializeField,ReadOnly] private float currentOpacity;
2023-08-23 01:59:40 +08:00
[Header(Constant.Header.Settings)]
2024-08-11 12:46:15 +08:00
[SerializeField] private bool isWindow;
[SerializeField] private bool closeWhenClickOutside;
2023-08-23 01:59:40 +08:00
[SerializeField] private bool isAnimate;
[SerializeField] private bool allowCursor;
[SerializeField] private bool allowInput;
[SerializeField] private bool autoEntry;
2023-11-15 23:54:54 +08:00
[Header(Constant.Header.Settings)]
[SerializeField] private Optional<float> entryDuration;
[SerializeField] private Optional<float> exitDuration;
2024-01-03 00:27:12 +08:00
protected static InputActionGroup inputActionGroup = new()
2023-10-24 23:37:59 +08:00
{
2024-01-03 00:27:12 +08:00
allowGlobalActivation = false,
Source = nameof(UIToolKitPanel)
2023-10-24 23:37:59 +08:00
};
2024-08-11 12:46:15 +08:00
public bool IsWindow => isWindow;
2023-08-23 01:59:40 +08:00
public bool IsAnimate => isAnimate;
2024-08-11 12:46:15 +08:00
public bool IsValid => destroyCancellationToken.IsCancellationRequested;
2024-01-03 00:27:12 +08:00
public string Index { get; private set; }
2023-08-23 01:59:40 +08:00
public bool AllowCursor => allowCursor;
public bool AllowInput => allowInput;
2023-11-15 23:54:54 +08:00
protected float TargetOpacity { get; private set; }
protected virtual VisualElement background => document.rootVisualElement;
2024-08-11 12:46:15 +08:00
// protected float CurrentOpacity
// {
// get => background?.GetOpacity() ?? currentOpacity;
// set
// {
// currentOpacity = value;
// background?.SetOpacity(value);
// }
// }
2023-08-23 01:59:40 +08:00
protected virtual void Awake()
{
Index= typeof(UIToolKitPanel) == GetType() ? gameObject.name : GetType().Name;
2023-11-15 23:54:54 +08:00
document.rootVisualElement.SetActive(false);
2024-08-11 12:46:15 +08:00
//background?.SetOpacity(0);
if (IsWindow) document.sortingOrder++;
2023-08-23 01:59:40 +08:00
}
2024-08-11 12:46:15 +08:00
2023-08-23 01:59:40 +08:00
protected virtual void Start()
{
2024-01-03 00:27:12 +08:00
BITKit.UX.UXUtils.Inject(this);
2024-04-06 16:33:57 +08:00
2024-08-11 12:46:15 +08:00
document.rootVisualElement.AddToClassList(USSEntry);
UXService.Register(this);
destroyCancellationToken.Register(() => { UXService.UnRegister(this); });
if (IsValid && autoEntry)
UXService.Entry(this);
var returnButton = document.rootVisualElement.Q("return-button");
2024-04-06 16:33:57 +08:00
returnButton?.RegisterCallback<MouseDownEvent>(x =>
{
if (x.button is 0)
OnReturn();
});
2024-08-11 12:46:15 +08:00
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);
}
2023-08-23 01:59:40 +08:00
}
2024-08-11 12:46:15 +08:00
2023-11-15 23:54:54 +08:00
public bool IsEntered { get; set; }
2024-08-11 12:46:15 +08:00
[BIT]
2023-08-23 01:59:40 +08:00
public void Entry()
{
UXService.Entry(this);
}
2024-04-06 16:33:57 +08:00
protected virtual void OnReturn()
{
UXService.Return();
}
2024-08-11 12:46:15 +08:00
protected virtual void OnEnable(){}
protected virtual void OnDisable(){}
2024-01-03 00:27:12 +08:00
protected virtual void OnPanelEntry(){}
protected virtual void OnPanelExit(){}
2023-11-15 23:54:54 +08:00
void IEntryElement.Entry()
{
2023-11-30 00:23:23 +08:00
try
{
2024-08-11 12:46:15 +08:00
//CurrentOpacity = 0;
//TargetOpacity = 1f;
2023-11-30 00:23:23 +08:00
document.rootVisualElement.SetActive(true);
2024-08-11 12:46:15 +08:00
2023-11-30 00:23:23 +08:00
OnEntry?.Invoke();
}
catch (Exception e)
{
Debug.Log(gameObject.name);
throw;
}
2024-08-11 12:46:15 +08:00
isActive = true;
2023-08-23 01:59:40 +08:00
}
2024-08-11 12:46:15 +08:00
async UniTask IEntryElement.EntryAsync()
2023-11-15 23:54:54 +08:00
{
2024-08-11 12:46:15 +08:00
document.rootVisualElement.AddToClassList(USSEntry);
// for (var i = 0; i < 32; i++)
// {
// await UniTask.NextFrame();
// }
document.rootVisualElement.AddToClassList(USSEntryAsync);
if (entryDuration.Allow)
{
var task = EntryAsync();
var durationTask = UniTask.Delay(TimeSpan.FromSeconds(entryDuration.Value), cancellationToken: destroyCancellationToken);
await durationTask;
document.rootVisualElement.RemoveFromClassList(USSEntry);
document.rootVisualElement.RemoveFromClassList(USSEntryAsync);
document.rootVisualElement.AddToClassList(USSEntered);
await task;
}
else
2023-11-15 23:54:54 +08:00
{
2024-08-11 12:46:15 +08:00
await EntryAsync();
2023-11-15 23:54:54 +08:00
}
}
2024-08-11 12:46:15 +08:00
public virtual UniTask EntryAsync()
{
return UniTask.CompletedTask;
}
2023-11-15 23:54:54 +08:00
void IEntryElement.Entered()
{
2024-08-11 12:46:15 +08:00
2024-01-03 00:27:12 +08:00
//inputActionGroup.allowInput.AddElement(this);
OnPanelEntry();
2023-11-15 23:54:54 +08:00
}
void IEntryElement.Exit()
2023-08-23 01:59:40 +08:00
{
2024-08-11 12:46:15 +08:00
document.rootVisualElement.AddToClassList(USSExit);
2023-08-23 01:59:40 +08:00
if (IsValid is false) return;
2024-08-11 12:46:15 +08:00
//TargetOpacity = 0;
2024-01-03 00:27:12 +08:00
//inputActionGroup.allowInput.RemoveElement(this);
OnPanelExit();
2023-11-15 23:54:54 +08:00
}
async UniTask IEntryElement.ExitAsync()
{
2024-08-11 12:46:15 +08:00
document.rootVisualElement.RemoveFromClassList(USSEntered);
document.rootVisualElement.AddToClassList(USSExitAsync);
// if (exitDuration.Allow is false) return;
// while (CurrentOpacity > 0 && TargetOpacity is 0)
// {
// await UniTask.NextFrame(cancellationToken);
// }
if (entryDuration.Allow is false) return;
await UniTask.Delay(TimeSpan.FromSeconds(entryDuration.Value), cancellationToken: destroyCancellationToken);
//return UniTask.CompletedTask;
2023-08-23 01:59:40 +08:00
}
2023-11-15 23:54:54 +08:00
void IEntryElement.Exited()
{
2024-08-11 12:46:15 +08:00
document.rootVisualElement.RemoveFromClassList(USSExit);
document.rootVisualElement.RemoveFromClassList(USSExitAsync);
document.rootVisualElement.AddToClassList(USSEntry);
2023-11-15 23:54:54 +08:00
document.rootVisualElement.SetActive(false);
2024-08-11 12:46:15 +08:00
isActive = false;
OnExit?.Invoke();
2023-11-15 23:54:54 +08:00
}
2023-08-27 02:58:19 +08:00
public event Action OnEntry;
public event Action OnExit;
2023-11-15 23:54:54 +08:00
public virtual void OnUpdate(float deltaTime)
{
2024-08-11 12:46:15 +08:00
// var duration = 1f;
// if (TargetOpacity is not 0)
// {
// if (entryDuration.Allow is false)
// {
// CurrentOpacity = TargetOpacity;
// return;
// }
// duration = entryDuration.Value;
// }
// else
// {
// if (exitDuration.Allow is false)
// {
// CurrentOpacity = TargetOpacity;
// return;
// }
// duration = exitDuration.Value;
// }
// CurrentOpacity = Mathf.MoveTowards(CurrentOpacity,TargetOpacity,1f/duration * Time.deltaTime);
2023-11-15 23:54:54 +08:00
}
2023-08-23 01:59:40 +08:00
}
}