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

209 lines
5.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.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
2024-07-15 17:26:08 +08:00
using UnityEngine.Serialization;
2023-08-23 01:59:26 +08:00
using UnityEngine.UIElements;
// ReSharper disable MemberCanBeProtected.Global
2023-09-01 14:35:05 +08:00
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global
// ReSharper disable UnusedMember.Global
2023-08-23 01:59:26 +08:00
namespace BITKit.UX
{
public class UIToolKitPanel : MonoBehaviour,IUXPanel
{
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-03-31 23:31:00 +08:00
[RuntimeInitializeOnLoadMethod]
private static void Reload()
{
2024-08-13 18:42:51 +08:00
InputActionGroup = new InputActionGroup
2024-03-31 23:31:00 +08:00
{
allowGlobalActivation = false,
Source = nameof(UIToolKitPanel)
};
2024-08-13 18:42:51 +08:00
InputActionGroup.allowInput.AddElement(0);
2024-03-31 23:31:00 +08:00
}
2023-08-23 01:59:26 +08:00
public UIToolKitPanel()
{
Index = GetType().FullName;
}
[Header(Constant.Header.Components)]
[SerializeField] protected UIDocument document;
2024-07-07 14:27:34 +08:00
2023-08-23 01:59:26 +08:00
[Header(Constant.Header.Settings)]
2024-07-07 14:27:34 +08:00
[SerializeField] private bool isWindow;
2024-07-23 15:56:12 +08:00
[SerializeField] private bool closeWhenClickOutside;
2023-08-23 01:59:26 +08:00
[SerializeField] private bool isAnimate;
[SerializeField] private bool allowCursor;
[SerializeField] private bool allowInput;
2023-11-15 23:55:06 +08:00
[Header(Constant.Header.Settings)]
[SerializeField] private Optional<float> entryDuration;
[SerializeField] private Optional<float> exitDuration;
2024-08-13 18:42:51 +08:00
protected static 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-07-07 14:27:34 +08:00
public bool IsWindow => isWindow;
2024-03-31 23:31:00 +08:00
public string Index { get; private set; }
2023-08-23 01:59:26 +08:00
public bool AllowCursor => allowCursor;
public bool AllowInput => allowInput;
2023-11-15 23:55:06 +08:00
protected float TargetOpacity { get; private set; }
protected virtual VisualElement background => document.rootVisualElement;
2024-08-10 18:16:23 +08:00
// protected float CurrentOpacity
// {
// get => background?.GetOpacity() ?? currentOpacity;
// set
// {
// currentOpacity = value;
// background?.SetOpacity(value);
// }
// }
2024-07-15 17:26:08 +08:00
2023-08-23 01:59:26 +08:00
protected virtual void Awake()
{
Index= typeof(UIToolKitPanel) == GetType() ? gameObject.name : GetType().Name;
2023-11-15 23:55:06 +08:00
document.rootVisualElement.SetActive(false);
2024-08-10 18:16:23 +08:00
//background?.SetOpacity(0);
2024-07-07 14:27:34 +08:00
if (IsWindow) document.sortingOrder++;
2023-08-23 01:59:26 +08:00
}
2024-08-04 11:39:37 +08:00
2023-08-23 01:59:26 +08:00
protected virtual void Start()
{
2024-08-13 18:42:51 +08:00
UXUtils.Inject(this);
2024-08-10 18:16:23 +08:00
document.rootVisualElement.AddToClassList(USSEntry);
2024-08-04 11:39:37 +08:00
2024-08-13 18:42:51 +08:00
UxService.Register(this);
destroyCancellationToken.Register(() => { UxService.UnRegister(this); });
2024-08-04 11:39:37 +08:00
var returnButton = document.rootVisualElement.Q("return-button");
2024-04-06 16:33:32 +08:00
returnButton?.RegisterCallback<MouseDownEvent>(x =>
{
if (x.button is 0)
OnReturn();
});
2024-08-04 11:39:37 +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;
}
2024-07-23 15:56:12 +08:00
if (isWindow)
{
invisible.style.backgroundColor = new Color(0, 0, 0, 0.9f);
}
2023-08-23 01:59:26 +08:00
}
2024-08-04 11:39:37 +08:00
2023-11-15 23:55:06 +08:00
public bool IsEntered { get; set; }
2024-08-13 18:42:51 +08:00
[BIT]
2023-08-23 01:59:26 +08:00
public void Entry()
{
2024-08-13 18:42:51 +08:00
UxService.Entry(this);
2023-08-23 01:59:26 +08:00
}
2024-04-06 16:33:32 +08:00
protected virtual void OnReturn()
{
2024-08-13 18:42:51 +08:00
UxService.Return();
2024-04-06 16:33:32 +08:00
}
2024-06-07 13:31:34 +08:00
protected virtual void OnEnable(){}
protected virtual void OnDisable(){}
2024-03-31 23:31:00 +08:00
protected virtual void OnPanelEntry(){}
protected virtual void OnPanelExit(){}
2023-11-15 23:55:06 +08:00
void IEntryElement.Entry()
{
2023-11-30 00:25:43 +08:00
try
{
document.rootVisualElement.SetActive(true);
2024-08-10 18:16:23 +08:00
2023-11-30 00:25:43 +08:00
OnEntry?.Invoke();
}
catch (Exception e)
{
Debug.Log(gameObject.name);
throw;
}
2023-08-23 01:59:26 +08:00
}
2024-08-10 18:16:23 +08:00
async UniTask IEntryElement.EntryAsync()
2023-11-15 23:55:06 +08:00
{
2024-08-10 18:16:23 +08:00
document.rootVisualElement.AddToClassList(USSEntry);
2024-08-13 18:42:51 +08:00
2024-08-10 18:16:23 +08:00
document.rootVisualElement.AddToClassList(USSEntryAsync);
if (entryDuration.Allow)
2023-11-15 23:55:06 +08:00
{
2024-08-10 18:16:23 +08:00
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
{
await EntryAsync();
2023-11-15 23:55:06 +08:00
}
}
2024-08-10 18:16:23 +08:00
public virtual UniTask EntryAsync()
{
return UniTask.CompletedTask;
}
2023-11-15 23:55:06 +08:00
void IEntryElement.Entered()
{
2024-03-31 23:31:00 +08:00
OnPanelEntry();
2023-11-15 23:55:06 +08:00
}
void IEntryElement.Exit()
2023-08-23 01:59:26 +08:00
{
2024-08-10 18:16:23 +08:00
document.rootVisualElement.AddToClassList(USSExit);
2024-08-13 18:42:51 +08:00
//if (IsValid is false) return;
2024-03-31 23:31:00 +08:00
OnPanelExit();
2023-11-15 23:55:06 +08:00
}
async UniTask IEntryElement.ExitAsync()
{
2024-08-10 18:16:23 +08:00
document.rootVisualElement.RemoveFromClassList(USSEntered);
document.rootVisualElement.AddToClassList(USSExitAsync);
if (entryDuration.Allow is false) return;
await UniTask.Delay(TimeSpan.FromSeconds(entryDuration.Value), cancellationToken: destroyCancellationToken);
2023-08-23 01:59:26 +08:00
}
2023-11-15 23:55:06 +08:00
void IEntryElement.Exited()
{
2024-08-10 18:16:23 +08:00
document.rootVisualElement.RemoveFromClassList(USSExit);
document.rootVisualElement.RemoveFromClassList(USSExitAsync);
document.rootVisualElement.AddToClassList(USSEntry);
2023-11-15 23:55:06 +08:00
document.rootVisualElement.SetActive(false);
2024-08-08 23:07:50 +08:00
OnExit?.Invoke();
2023-11-15 23:55:06 +08:00
}
2023-09-01 14:35:05 +08:00
public event Action OnEntry;
public event Action OnExit;
2023-11-15 23:55:06 +08:00
public virtual void OnUpdate(float deltaTime)
{
}
2023-08-23 01:59:26 +08:00
}
}