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

148 lines
3.9 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-11-15 23:54:54 +08:00
[Header(Constant.Header.Settings)]
[SerializeField] private Optional<float> entryDuration;
[SerializeField] private Optional<float> exitDuration;
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; }
2023-11-15 23:54:54 +08:00
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);
}
}
private float _currentOpacity;
2023-08-23 01:59:40 +08:00
protected virtual void Awake()
{
cancellationToken = gameObject.GetCancellationTokenOnDestroy();
Index= typeof(UIToolKitPanel) == GetType() ? gameObject.name : GetType().Name;
2023-11-15 23:54:54 +08:00
document.rootVisualElement.SetActive(false);
background?.SetOpacity(0);
2023-08-23 01:59:40 +08:00
}
protected virtual void Start()
{
if(IsValid && autoEntry)
UXService.Entry(this);
}
2023-11-15 23:54:54 +08:00
public bool IsEntered { get; set; }
2023-08-23 01:59:40 +08:00
public void Entry()
{
UXService.Entry(this);
}
2023-11-15 23:54:54 +08:00
2023-08-23 01:59:40 +08:00
protected virtual void OnEnable()=>UXService.Register(this);
protected virtual void OnDisable()=>UXService.UnRegister(this);
2023-11-15 23:54:54 +08:00
void IEntryElement.Entry()
{
TargetOpacity = 1;
2023-08-23 01:59:40 +08:00
OnEntryOrExit(true);
document.rootVisualElement.SetActive(true);
2023-08-27 02:58:19 +08:00
OnEntry?.Invoke();
2023-08-23 01:59:40 +08:00
}
2023-11-15 23:54:54 +08:00
async UniTask IEntryElement.EntryAsync()
{
if (entryDuration.Allow is false) return;
while (CurrentOpacity < 1 && TargetOpacity is 1)
{
await UniTask.NextFrame(cancellationToken);
}
}
void IEntryElement.Entered()
{
inputActionGroup.allowInput.AddElement(this);
}
void IEntryElement.Exit()
2023-08-23 01:59:40 +08:00
{
if (IsValid is false) return;
2023-11-15 23:54:54 +08:00
TargetOpacity = 0;
2023-08-23 01:59:40 +08:00
OnEntryOrExit(false);
2023-10-24 23:37:59 +08:00
inputActionGroup.allowInput.RemoveElement(this);
2023-11-15 23:54:54 +08:00
}
async UniTask IEntryElement.ExitAsync()
{
if (exitDuration.Allow is false) return;
while (CurrentOpacity > 0 && TargetOpacity is 0)
2023-08-23 01:59:40 +08:00
{
2023-11-15 23:54:54 +08:00
await UniTask.NextFrame(cancellationToken);
2023-08-23 01:59:40 +08:00
}
2023-08-27 02:58:19 +08:00
OnExit?.Invoke();
2023-08-23 01:59:40 +08:00
}
2023-11-15 23:54:54 +08:00
void IEntryElement.Exited()
{
document.rootVisualElement.SetActive(false);
}
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)
{
}
2023-11-15 23:54:54 +08:00
public virtual void OnUpdate(float deltaTime)
{
var duration = 1f;
if (TargetOpacity is 1)
{
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-08-23 01:59:40 +08:00
}
}