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

85 lines
2.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;
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
{
public UIToolKitPanel()
{
Index = GetType().FullName;
}
[Header(Constant.Header.Components)]
[SerializeField] protected UIDocument document;
[Header(Constant.Header.Settings)]
[SerializeField] private bool isAnimate;
[SerializeField] private bool allowCursor;
[SerializeField] private bool allowInput;
[SerializeField] private bool autoEntry;
2023-10-24 23:37:59 +08:00
protected readonly InputActionGroup inputActionGroup = new()
{
allowGlobalActivation = false
};
2023-08-23 01:59:40 +08:00
public bool IsAnimate => isAnimate;
public bool IsValid => cancellationToken.IsCancellationRequested is false;
public string Index { get; private set; }
public bool AllowCursor => allowCursor;
public bool AllowInput => allowInput;
protected CancellationToken cancellationToken { get; private set; }
protected virtual void Awake()
{
cancellationToken = gameObject.GetCancellationTokenOnDestroy();
Index= typeof(UIToolKitPanel) == GetType() ? gameObject.name : GetType().Name;
}
protected virtual void Start()
{
if(IsValid && autoEntry)
UXService.Entry(this);
}
public void Entry()
{
UXService.Entry(this);
}
protected virtual void OnEnable()=>UXService.Register(this);
protected virtual void OnDisable()=>UXService.UnRegister(this);
void IUXPanel.Entry()
{
OnEntryOrExit(true);
document.rootVisualElement.SetActive(true);
2023-10-24 23:37:59 +08:00
inputActionGroup.allowInput.AddElement(this);
2023-08-27 02:58:19 +08:00
OnEntry?.Invoke();
2023-08-23 01:59:40 +08:00
}
void IUXPanel.Exit()
{
if (IsValid is false) return;
OnEntryOrExit(false);
2023-10-24 23:37:59 +08:00
inputActionGroup.allowInput.RemoveElement(this);
2023-08-23 01:59:40 +08:00
try
{
document.rootVisualElement.SetActive(false);
}
catch (Exception e)
{
BIT4Log.Warning<UIToolKitPanel>(name);
BIT4Log.LogException(e);
}
2023-08-27 02:58:19 +08:00
OnExit?.Invoke();
2023-08-23 01:59:40 +08:00
}
2023-08-27 02:58:19 +08:00
public event Action OnEntry;
public event Action OnExit;
2023-08-23 01:59:40 +08:00
protected virtual void OnEntryOrExit(bool isEntry)
{
}
}
}