BITFALL/Assets/BITKit/Unity/Scripts/UX/Service/UXService.cs

218 lines
5.3 KiB
C#
Raw Normal View History

2023-08-23 01:59:40 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2023-10-02 23:24:56 +08:00
using System.Linq;
2023-11-15 23:54:54 +08:00
using Cysharp.Threading.Tasks;
2023-08-23 01:59:40 +08:00
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.UX
{
/// <summary>
/// 适用于Unity的UX Service
/// </summary>
public class UXService : MonoBehaviour, IUXService
{
2023-11-15 23:54:54 +08:00
2023-10-02 23:24:56 +08:00
/// <summary>
/// 重新初始化,使用<see cref="RuntimeInitializeLoadType.SubsystemRegistration"/>确保在所有子系统注册后执行
/// </summary>
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
2023-08-23 01:59:40 +08:00
private static void Initialized()
{
RegistryQueue.Clear();
UnRegistryQueue.Clear();
Panels.Clear();
EntryQueue.Clear();
2023-11-15 23:54:54 +08:00
CurrentPanel = null;
2023-08-23 01:59:40 +08:00
History.Clear();
2023-11-15 23:54:54 +08:00
EntryGroup = new EntryGroup<IUXPanel>();
2023-08-23 01:59:40 +08:00
}
2023-11-15 23:54:54 +08:00
private static EntryGroup<IUXPanel> EntryGroup = new();
2023-08-23 01:59:40 +08:00
/// <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>
2023-11-15 23:54:54 +08:00
internal static IUXPanel CurrentPanel;
2023-08-23 01:59:40 +08:00
/// <summary>
/// 历史面板
/// </summary>
2023-11-15 23:54:54 +08:00
internal static readonly Stack<IUXPanel> History = new();
2023-08-23 01:59:40 +08:00
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 (History.TryPop(out var returnPanel))
{
Entry(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;
2023-11-15 23:54:54 +08:00
2023-08-23 01:59:40 +08:00
private bool initialized;
private void Awake()
{
DI.Register<IUXService>(this);
2023-11-15 23:54:54 +08:00
2023-08-23 01:59:40 +08:00
}
2023-11-15 23:54:54 +08:00
private void Start()
{
EntryGroup.OnEntry += OnEntry;
EntryGroup.OnExit += OnExit;
2023-08-23 01:59:40 +08:00
2023-11-15 23:54:54 +08:00
if (initialPanel is not null)
{
Entry(initialPanel);
}
}
private static void OnExit(IUXPanel obj)
{
History.Push(obj);
}
private static void OnEntry(IUXPanel obj)
{
CurrentPanel = obj;
}
2023-08-23 01:59:40 +08:00
private void Update()
{
while (UnRegistryQueue.TryDequeue(out var result))
{
if (result is null) continue;
2023-11-15 23:54:54 +08:00
EntryGroup.list.Remove(result);
2023-08-23 01:59:40 +08:00
Panels.Remove(result.Index);
}
while (RegistryQueue.TryDequeue(out var result))
{
if (result is null) continue;
2023-11-15 23:54:54 +08:00
EntryGroup.list.Add(result);
2023-08-23 01:59:40 +08:00
Panels.Set(result.Index, result);
}
2023-11-15 23:54:54 +08:00
if (EntryQueue.TryPop(out var nextPanel))
2023-08-23 01:59:40 +08:00
{
2023-11-15 23:54:54 +08:00
EntryGroup.Entry(x=>x.Index==nextPanel.Index);
BITAppForUnity.AllowCursor.SetElements(this, nextPanel.AllowCursor);
BITInputSystem.AllowInput.SetElements(this, nextPanel.AllowInput);
2023-08-23 01:59:40 +08:00
}
2023-11-15 23:54:54 +08:00
if (EntryGroup.TryGetEntried(out var currentPanel))
2023-08-23 01:59:40 +08:00
{
2023-11-15 23:54:54 +08:00
currentPanel.OnUpdate(Time.deltaTime);
};
2023-08-23 01:59:40 +08:00
2023-11-15 23:54:54 +08:00
// 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);
2023-08-23 01:59:40 +08:00
}
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 panelsLabel;
2023-11-15 23:54:54 +08:00
private Label historyLabel;
2023-08-23 01:59:40 +08:00
public override VisualElement CreateInspectorGUI()
{
FillDefaultInspector();
CreateSubTitle("States");
currentPanelLabel = root.Create<Label>();
CreateSubTitle("Panels");
panelsLabel = root.Create<Label>();
2023-11-15 23:54:54 +08:00
CreateSubTitle("History");
historyLabel = root.Create<Label>();
2023-08-23 01:59:40 +08:00
return root;
}
protected override void OnUpdate()
{
2023-11-15 23:54:54 +08:00
if (panelsLabel is null || currentPanelLabel is null || historyLabel is null) return;
2023-08-23 01:59:40 +08:00
panelsLabel.text=string.Join("\n",UXService.Panels);
2023-11-15 23:54:54 +08:00
currentPanelLabel.text = string.Join("\n",UXService.CurrentPanel?.Index);
historyLabel.text = string.Join("\n",UXService.History.Select(x=>x.Index));
2023-08-23 01:59:40 +08:00
}
}
#endif
}