224 lines
5.7 KiB
C#
224 lines
5.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Cysharp.Threading.Tasks;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace BITKit.UX
|
|
{
|
|
/// <summary>
|
|
/// 适用于Unity的UX Service
|
|
/// </summary>
|
|
public class UXService : MonoBehaviour, IUXService
|
|
{
|
|
|
|
/// <summary>
|
|
/// 重新初始化,使用<see cref="RuntimeInitializeLoadType.SubsystemRegistration"/>确保在所有子系统注册后执行
|
|
/// </summary>
|
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
|
private static void Initialized()
|
|
{
|
|
RegistryQueue.Clear();
|
|
UnRegistryQueue.Clear();
|
|
Panels.Clear();
|
|
EntryQueue.Clear();
|
|
CurrentPanel = null;
|
|
History.Clear();
|
|
EntryGroup = new EntryGroup<IUXPanel>();
|
|
WindowEntryGruop = new();
|
|
}
|
|
|
|
|
|
internal static EntryGroup<IUXPanel> EntryGroup = new();
|
|
internal static EntryGroup<IUXPanel> WindowEntryGruop = new();
|
|
/// <summary>
|
|
/// 内部注册面板队列
|
|
/// </summary>
|
|
private static readonly Queue<IUXPanel> RegistryQueue = new();
|
|
|
|
/// <summary>
|
|
/// 内部注销面板队列
|
|
/// </summary>
|
|
private static readonly Queue<IUXPanel> UnRegistryQueue = new();
|
|
|
|
/// <summary>
|
|
/// 已注册面板字典
|
|
/// </summary>
|
|
internal static readonly Dictionary<string, IUXPanel> Panels = new();
|
|
|
|
/// <summary>
|
|
/// 等待启用的面板队列
|
|
/// </summary>
|
|
private static readonly Stack<IUXPanel> EntryQueue = new();
|
|
|
|
/// <summary>
|
|
/// 返回面板缓冲区
|
|
/// </summary>
|
|
private static readonly DoubleBuffer<IUXPanel> ReturnBuffer = new();
|
|
|
|
/// <summary>
|
|
/// 已启用面板
|
|
/// </summary>
|
|
internal static IUXPanel CurrentPanel;
|
|
|
|
/// <summary>
|
|
/// 历史面板
|
|
/// </summary>
|
|
internal static readonly Stack<IUXPanel> History = new();
|
|
|
|
public static void Register(IUXPanel panel) => RegistryQueue.Enqueue(panel);
|
|
|
|
public static void UnRegister(IUXPanel panel) => UnRegistryQueue.Enqueue(panel);
|
|
|
|
public static void Entry<T>() where T : IUXPanel => EntryQueue.Push(Panels[typeof(T).Name]);
|
|
|
|
public static void Return()
|
|
{
|
|
if(WindowEntryGruop.TryGetEntried(out var window))
|
|
{
|
|
WindowEntryGruop.Entry(-1);
|
|
return;
|
|
}
|
|
if (History.TryPop(out var returnPanel))
|
|
{
|
|
ReturnBuffer.Release(returnPanel);
|
|
}
|
|
}
|
|
|
|
public static void Entry(IUXPanel panel) => EntryQueue.Push(panel);
|
|
public static void Entry(string panelName) => EntryQueue.Push(Panels[panelName]);
|
|
|
|
[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()
|
|
{
|
|
try
|
|
{
|
|
while (RegistryQueue.TryDequeue(out var result))
|
|
{
|
|
if (result is null) continue;
|
|
EntryGroup.list.Add(result);
|
|
Panels.Set(result.Index, result);
|
|
}
|
|
|
|
while (UnRegistryQueue.TryDequeue(out var result))
|
|
{
|
|
if (result is null) continue;
|
|
EntryGroup.list.Remove(result);
|
|
Panels.Remove(result.Index);
|
|
}
|
|
|
|
if (ReturnBuffer.TryGetRelease(out var returnPanel))
|
|
{
|
|
EntryGroup.Entry(x=>x.Index==returnPanel.Index);
|
|
BITAppForUnity.AllowCursor.SetElements(this, returnPanel.AllowCursor);
|
|
BITInputSystem.AllowInput.SetElements(this, returnPanel.AllowInput);
|
|
}
|
|
|
|
if (EntryQueue.TryPop(out var nextPanel))
|
|
{
|
|
if (nextPanel.IsWindow)
|
|
{
|
|
WindowEntryGruop.Entry(nextPanel);
|
|
History.Push(CurrentPanel);
|
|
return;
|
|
}
|
|
WindowEntryGruop.Entry(-1);
|
|
History.Push(CurrentPanel);
|
|
EntryGroup.Entry(x=>x.Index==nextPanel.Index);
|
|
BITAppForUnity.AllowCursor.SetElements(this, nextPanel.AllowCursor);
|
|
BITInputSystem.AllowInput.SetElements(this, nextPanel.AllowInput);
|
|
}
|
|
if (EntryGroup.TryGetEntried(out var currentPanel))
|
|
{
|
|
currentPanel.OnUpdate(Time.deltaTime);
|
|
};
|
|
if(WindowEntryGruop.TryGetEntried(out var windowPanel))
|
|
{
|
|
windowPanel.OnUpdate(Time.deltaTime);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
BIT4Log.LogException(e);
|
|
}
|
|
}
|
|
|
|
void IUXService.Register(IUXPanel panel) => Register(panel);
|
|
|
|
void IUXService.UnRegister(IUXPanel panel) => UnRegister(panel);
|
|
|
|
void IUXService.Entry<T>() => Entry<T>();
|
|
|
|
void IUXService.Return() => Return();
|
|
|
|
void IUXService.Entry(IUXPanel panel) => Entry(panel);
|
|
|
|
void IUXService.Entry(string panelName) => Entry(panelName);
|
|
}
|
|
#if UNITY_EDITOR
|
|
[CustomEditor(typeof(UXService))]
|
|
public class UXServiceInspector : BITInspector<UXService>
|
|
{
|
|
private Label currentPanelLabel;
|
|
private Label currentWindow;
|
|
private Label panelsLabel;
|
|
private Label historyLabel;
|
|
|
|
public override VisualElement CreateInspectorGUI()
|
|
{
|
|
FillDefaultInspector();
|
|
CreateSubTitle("States");
|
|
currentPanelLabel = root.Create<Label>();
|
|
currentWindow = 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 || historyLabel is null) return;
|
|
panelsLabel.text = string.Join("\n", UXService.Panels);
|
|
currentPanelLabel.text = string.Join("\n", UXService.CurrentPanel?.Index);
|
|
historyLabel.text = string.Join("\n", UXService.History.Select(x => x is null ? "Null" : x.Index));
|
|
currentWindow.text = UXService.WindowEntryGruop.TryGetEntried(out var window) ? window.Index : "None";
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|