This commit is contained in:
CortexCore 2024-08-10 18:16:23 +08:00
parent 68a998ff96
commit c563c539a5
1 changed files with 89 additions and 48 deletions

View File

@ -14,6 +14,13 @@ namespace BITKit.UX
{ {
public class UIToolKitPanel : MonoBehaviour,IUXPanel public class UIToolKitPanel : MonoBehaviour,IUXPanel
{ {
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";
[RuntimeInitializeOnLoadMethod] [RuntimeInitializeOnLoadMethod]
private static void Reload() private static void Reload()
{ {
@ -34,7 +41,7 @@ namespace BITKit.UX
[Header(Constant.Header.Settings)] [Header(Constant.Header.Settings)]
[SerializeField, ReadOnly] private bool isActive; [SerializeField, ReadOnly] private bool isActive;
[SerializeField,ReadOnly] private float currentOpacity; //[SerializeField,ReadOnly] private float currentOpacity;
[Header(Constant.Header.Settings)] [Header(Constant.Header.Settings)]
[SerializeField] private bool isWindow; [SerializeField] private bool isWindow;
[SerializeField] private bool closeWhenClickOutside; [SerializeField] private bool closeWhenClickOutside;
@ -53,35 +60,35 @@ namespace BITKit.UX
}; };
public bool IsWindow => isWindow; public bool IsWindow => isWindow;
public bool IsAnimate => isAnimate; public bool IsAnimate => isAnimate;
public bool IsValid => cancellationToken.IsCancellationRequested is false; public bool IsValid => destroyCancellationToken.IsCancellationRequested;
public string Index { get; private set; } public string Index { get; private set; }
public bool AllowCursor => allowCursor; public bool AllowCursor => allowCursor;
public bool AllowInput => allowInput; public bool AllowInput => allowInput;
protected CancellationToken cancellationToken { get; private set; }
protected float TargetOpacity { get; private set; } protected float TargetOpacity { get; private set; }
protected virtual VisualElement background => document.rootVisualElement; protected virtual VisualElement background => document.rootVisualElement;
protected float CurrentOpacity // protected float CurrentOpacity
{ // {
get => background?.GetOpacity() ?? currentOpacity; // get => background?.GetOpacity() ?? currentOpacity;
set // set
{ // {
currentOpacity = value; // currentOpacity = value;
background?.SetOpacity(value); // background?.SetOpacity(value);
} // }
} // }
protected virtual void Awake() protected virtual void Awake()
{ {
cancellationToken = gameObject.GetCancellationTokenOnDestroy();
Index= typeof(UIToolKitPanel) == GetType() ? gameObject.name : GetType().Name; Index= typeof(UIToolKitPanel) == GetType() ? gameObject.name : GetType().Name;
document.rootVisualElement.SetActive(false); document.rootVisualElement.SetActive(false);
background?.SetOpacity(0); //background?.SetOpacity(0);
if (IsWindow) document.sortingOrder++; if (IsWindow) document.sortingOrder++;
} }
protected virtual void Start() protected virtual void Start()
{ {
BITKit.UX.UXUtils.Inject(this); BITKit.UX.UXUtils.Inject(this);
document.rootVisualElement.AddToClassList(USSEntry);
UXService.Register(this); UXService.Register(this);
destroyCancellationToken.Register(() => { UXService.UnRegister(this); }); destroyCancellationToken.Register(() => { UXService.UnRegister(this); });
@ -134,11 +141,11 @@ namespace BITKit.UX
{ {
try try
{ {
CurrentOpacity = 0; //CurrentOpacity = 0;
TargetOpacity = 1f; //TargetOpacity = 1f;
document.rootVisualElement.SetActive(true); document.rootVisualElement.SetActive(true);
OnEntry?.Invoke(); OnEntry?.Invoke();
} }
catch (Exception e) catch (Exception e)
@ -148,38 +155,72 @@ namespace BITKit.UX
} }
isActive = true; isActive = true;
} }
public virtual async UniTask EntryAsync()
async UniTask IEntryElement.EntryAsync()
{ {
if (entryDuration.Allow is false) return; document.rootVisualElement.AddToClassList(USSEntry);
while (CurrentOpacity < 1 && TargetOpacity is 1)
// for (var i = 0; i < 32; i++)
// {
// await UniTask.NextFrame();
// }
document.rootVisualElement.AddToClassList(USSEntryAsync);
if (entryDuration.Allow)
{ {
await UniTask.NextFrame(cancellationToken); var task = EntryAsync();
if(destroyCancellationToken.IsCancellationRequested)return; 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();
}
}
public virtual UniTask EntryAsync()
{
return UniTask.CompletedTask;
} }
void IEntryElement.Entered() void IEntryElement.Entered()
{ {
//inputActionGroup.allowInput.AddElement(this); //inputActionGroup.allowInput.AddElement(this);
OnPanelEntry(); OnPanelEntry();
} }
void IEntryElement.Exit() void IEntryElement.Exit()
{ {
document.rootVisualElement.AddToClassList(USSExit);
if (IsValid is false) return; if (IsValid is false) return;
TargetOpacity = 0; //TargetOpacity = 0;
//inputActionGroup.allowInput.RemoveElement(this); //inputActionGroup.allowInput.RemoveElement(this);
OnPanelExit(); OnPanelExit();
} }
async UniTask IEntryElement.ExitAsync() async UniTask IEntryElement.ExitAsync()
{ {
if (exitDuration.Allow is false) return; document.rootVisualElement.RemoveFromClassList(USSEntered);
while (CurrentOpacity > 0 && TargetOpacity is 0) document.rootVisualElement.AddToClassList(USSExitAsync);
{ // if (exitDuration.Allow is false) return;
await UniTask.NextFrame(cancellationToken); // 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;
} }
void IEntryElement.Exited() void IEntryElement.Exited()
{ {
document.rootVisualElement.RemoveFromClassList(USSExit);
document.rootVisualElement.RemoveFromClassList(USSExitAsync);
document.rootVisualElement.AddToClassList(USSEntry);
document.rootVisualElement.SetActive(false); document.rootVisualElement.SetActive(false);
isActive = false; isActive = false;
OnExit?.Invoke(); OnExit?.Invoke();
@ -189,26 +230,26 @@ namespace BITKit.UX
public event Action OnExit; public event Action OnExit;
public virtual void OnUpdate(float deltaTime) public virtual void OnUpdate(float deltaTime)
{ {
var duration = 1f; // var duration = 1f;
if (TargetOpacity is not 0) // if (TargetOpacity is not 0)
{ // {
if (entryDuration.Allow is false) // if (entryDuration.Allow is false)
{ // {
CurrentOpacity = TargetOpacity; // CurrentOpacity = TargetOpacity;
return; // return;
} // }
duration = entryDuration.Value; // duration = entryDuration.Value;
} // }
else // else
{ // {
if (exitDuration.Allow is false) // if (exitDuration.Allow is false)
{ // {
CurrentOpacity = TargetOpacity; // CurrentOpacity = TargetOpacity;
return; // return;
} // }
duration = exitDuration.Value; // duration = exitDuration.Value;
} // }
CurrentOpacity = Mathf.MoveTowards(CurrentOpacity,TargetOpacity,1f/duration * Time.deltaTime); // CurrentOpacity = Mathf.MoveTowards(CurrentOpacity,TargetOpacity,1f/duration * Time.deltaTime);
} }
} }