This commit is contained in:
CortexCore
2023-11-15 23:55:06 +08:00
parent 5446067f91
commit 70247f0242
82 changed files with 3271 additions and 579 deletions

View File

@@ -24,6 +24,11 @@ namespace BITKit.UX
[SerializeField] private bool allowCursor;
[SerializeField] private bool allowInput;
[SerializeField] private bool autoEntry;
[Header(Constant.Header.Settings)]
[SerializeField] private Optional<float> entryDuration;
[SerializeField] private Optional<float> exitDuration;
protected readonly InputActionGroup inputActionGroup = new()
{
allowGlobalActivation = false
@@ -34,51 +39,109 @@ namespace BITKit.UX
public bool AllowCursor => allowCursor;
public bool AllowInput => allowInput;
protected CancellationToken cancellationToken { get; private set; }
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;
protected virtual void Awake()
{
cancellationToken = gameObject.GetCancellationTokenOnDestroy();
Index= typeof(UIToolKitPanel) == GetType() ? gameObject.name : GetType().Name;
document.rootVisualElement.SetActive(false);
background?.SetOpacity(0);
}
protected virtual void Start()
{
if(IsValid && autoEntry)
UXService.Entry(this);
}
public bool IsEntered { get; set; }
public void Entry()
{
UXService.Entry(this);
}
protected virtual void OnEnable()=>UXService.Register(this);
protected virtual void OnDisable()=>UXService.UnRegister(this);
void IUXPanel.Entry()
{
void IEntryElement.Entry()
{
TargetOpacity = 1;
OnEntryOrExit(true);
document.rootVisualElement.SetActive(true);
inputActionGroup.allowInput.AddElement(this);
OnEntry?.Invoke();
}
void IUXPanel.Exit()
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()
{
if (IsValid is false) return;
TargetOpacity = 0;
OnEntryOrExit(false);
inputActionGroup.allowInput.RemoveElement(this);
try
}
async UniTask IEntryElement.ExitAsync()
{
if (exitDuration.Allow is false) return;
while (CurrentOpacity > 0 && TargetOpacity is 0)
{
document.rootVisualElement.SetActive(false);
}
catch (Exception e)
{
BIT4Log.Warning<UIToolKitPanel>(name);
BIT4Log.LogException(e);
await UniTask.NextFrame(cancellationToken);
}
OnExit?.Invoke();
}
void IEntryElement.Exited()
{
document.rootVisualElement.SetActive(false);
}
public event Action OnEntry;
public event Action OnExit;
protected virtual void OnEntryOrExit(bool isEntry)
{
}
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);
}
}
}