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);
}
}
}

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
#if UNITY_EDITOR
using UnityEditor;
#endif
@@ -15,6 +16,7 @@ namespace BITKit.UX
/// </summary>
public class UXService : MonoBehaviour, IUXService
{
/// <summary>
/// 重新初始化,使用<see cref="RuntimeInitializeLoadType.SubsystemRegistration"/>确保在所有子系统注册后执行
/// </summary>
@@ -25,10 +27,13 @@ namespace BITKit.UX
UnRegistryQueue.Clear();
Panels.Clear();
EntryQueue.Clear();
EntryCompletedPanels.Clear();
CurrentPanel = null;
History.Clear();
EntryGroup = new EntryGroup<IUXPanel>();
}
private static EntryGroup<IUXPanel> EntryGroup = new();
/// <summary>
/// 内部注册面板队列
/// </summary>
@@ -52,12 +57,12 @@ namespace BITKit.UX
/// <summary>
/// 已启用面板
/// </summary>
internal static readonly Stack<IUXPanel> EntryCompletedPanels = new();
internal static IUXPanel CurrentPanel;
/// <summary>
/// 历史面板
/// </summary>
private static readonly Stack<IUXPanel> History = new();
internal static readonly Stack<IUXPanel> History = new();
public static void Register(IUXPanel panel) => RegistryQueue.Enqueue(panel);
@@ -67,7 +72,6 @@ namespace BITKit.UX
public static void Return()
{
if (!History.TryPop(out _)) return;
if (History.TryPop(out var returnPanel))
{
Entry(returnPanel);
@@ -80,57 +84,92 @@ namespace BITKit.UX
[SerializeReference, SubclassSelector] private IUXPanel initialPanel;
private bool initialized;
private void Awake()
{
DI.Register<IUXService>(this);
}
}
private void Start()
{
EntryGroup.OnEntry += OnEntry;
EntryGroup.OnExit += OnExit;
if (initialPanel is not null)
{
Entry(initialPanel);
}
}
private static void OnExit(IUXPanel obj)
{
History.Push(obj);
}
private static void OnEntry(IUXPanel obj)
{
CurrentPanel = obj;
}
private void Update()
{
while (UnRegistryQueue.TryDequeue(out var result))
{
if (result is null) continue;
EntryGroup.list.Remove(result);
Panels.Remove(result.Index);
}
while (RegistryQueue.TryDequeue(out var result))
{
if (result is null) continue;
EntryGroup.list.Add(result);
Panels.Set(result.Index, result);
result.Exit();
}
if (initialized is false && initialPanel is not null)
if (EntryQueue.TryPop(out var nextPanel))
{
initialized = true;
Entry(initialPanel);
EntryGroup.Entry(x=>x.Index==nextPanel.Index);
BITAppForUnity.AllowCursor.SetElements(this, nextPanel.AllowCursor);
BITInputSystem.AllowInput.SetElements(this, nextPanel.AllowInput);
}
if (!EntryQueue.TryPop(out var next) || next is null) return;
if (EntryGroup.TryGetEntried(out var currentPanel))
{
currentPanel.OnUpdate(Time.deltaTime);
};
if (Panels.ContainsKey(next.Index) is false) return;
while (EntryCompletedPanels.TryPop(out var entryCompletedPanel))
{
entryCompletedPanel?.Exit();
}
try
{
next.Entry();
}
catch (Exception e)
{
Debug.LogWarning(next.Index);
Debug.LogException(e);
}
BITAppForUnity.AllowCursor.SetElements(this, next.AllowCursor);
BITInputSystem.AllowInput.SetElements(this, next.AllowInput);
EntryCompletedPanels.Push(next);
History.Push(next);
// if (initialized is false && initialPanel is not null)
// {
// initialized = true;
// Entry(initialPanel);
// }
//
// if (!EntryQueue.TryPop(out var next) || next is null) return;
//
// if (Panels.ContainsKey(next.Index) is false) return;
//
// while (EntryCompletedPanels.TryPop(out var entryCompletedPanel))
// {
// entryCompletedPanel?.Exit();
// }
//
// try
// {
// next.Entry();
// }
// catch (Exception e)
// {
// Debug.LogWarning(next.Index);
// Debug.LogException(e);
// }
//
// BITAppForUnity.AllowCursor.SetElements(this, next.AllowCursor);
// BITInputSystem.AllowInput.SetElements(this, next.AllowInput);
//
// EntryCompletedPanels.Push(next);
// History.Push(next);
}
void IUXService.Register(IUXPanel panel) => Register(panel);
@@ -151,6 +190,7 @@ namespace BITKit.UX
{
private Label currentPanelLabel;
private Label panelsLabel;
private Label historyLabel;
public override VisualElement CreateInspectorGUI()
{
FillDefaultInspector();
@@ -158,16 +198,18 @@ namespace BITKit.UX
currentPanelLabel = root.Create<Label>();
CreateSubTitle("Panels");
panelsLabel = root.Create<Label>();
CreateSubTitle("History");
historyLabel = root.Create<Label>();
return root;
}
protected override void OnUpdate()
{
if (panelsLabel is null || currentPanelLabel is null) return;
if (panelsLabel is null || currentPanelLabel is null || historyLabel is null) return;
panelsLabel.text=string.Join("\n",UXService.Panels);
currentPanelLabel.text = string.Join("\n",UXService.EntryCompletedPanels.Select(x=>x.Index));
currentPanelLabel.text = string.Join("\n",UXService.CurrentPanel?.Index);
historyLabel.text = string.Join("\n",UXService.History.Select(x=>x.Index));
}
}
#endif