Files
BITKit/Src/Unity/Scripts/UX/Service/UXService.cs

193 lines
5.1 KiB
C#
Raw Normal View History

2023-08-23 01:59:26 +08:00
using System;
using System.Collections.Generic;
2024-08-13 18:42:51 +08:00
using System.Text;
2023-08-23 01:59:26 +08:00
using UnityEngine;
namespace BITKit.UX
{
/// <summary>
/// 适用于Unity的UX Service
/// </summary>
2024-08-13 18:42:51 +08:00
public class UxService : MonoBehaviour, IUXService
2023-08-23 01:59:26 +08:00
{
2023-11-15 23:55:06 +08:00
2023-10-06 23:43:19 +08:00
/// <summary>
/// 重新初始化,使用<see cref="RuntimeInitializeLoadType.SubsystemRegistration"/>确保在所有子系统注册后执行
/// </summary>
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
2023-08-23 01:59:26 +08:00
private static void Initialized()
{
RegistryQueue.Clear();
UnRegistryQueue.Clear();
Panels.Clear();
EntryQueue.Clear();
2024-08-13 18:42:51 +08:00
_currentPanel = null;
2023-08-23 01:59:26 +08:00
History.Clear();
2024-08-13 18:42:51 +08:00
_entryGroup = new EntryGroup<IUXPanel>();
_windowEntryGroup = new EntryGroup<IUXPanel>();
2023-08-23 01:59:26 +08:00
}
2023-11-15 23:55:06 +08:00
2024-08-13 18:42:51 +08:00
private static EntryGroup<IUXPanel> _entryGroup = new();
private static EntryGroup<IUXPanel> _windowEntryGroup = new();
2023-08-23 01:59:26 +08:00
/// <summary>
/// 内部注册面板队列
/// </summary>
private static readonly Queue<IUXPanel> RegistryQueue = new();
/// <summary>
/// 内部注销面板队列
/// </summary>
private static readonly Queue<IUXPanel> UnRegistryQueue = new();
/// <summary>
/// 已注册面板字典
/// </summary>
2024-08-13 18:42:51 +08:00
private static readonly Dictionary<string, IUXPanel> Panels = new();
2023-08-23 01:59:26 +08:00
/// <summary>
/// 等待启用的面板队列
/// </summary>
private static readonly Stack<IUXPanel> EntryQueue = new();
2024-06-14 16:16:13 +08:00
/// <summary>
/// 返回面板缓冲区
/// </summary>
private static readonly DoubleBuffer<IUXPanel> ReturnBuffer = new();
2023-08-23 01:59:26 +08:00
/// <summary>
/// 已启用面板
/// </summary>
2024-08-13 18:42:51 +08:00
private static IUXPanel _currentPanel;
2023-08-23 01:59:26 +08:00
/// <summary>
/// 历史面板
/// </summary>
2024-08-13 18:42:51 +08:00
private static readonly Stack<IUXPanel> History = new();
2024-08-05 09:53:22 +08:00
/// <summary>
/// 清空历史面板,通常用于关闭返回上一步
/// </summary>
public static void ClearHistory() => History.Clear();
2023-08-23 01:59:26 +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()
{
2024-08-13 18:42:51 +08:00
if(_windowEntryGroup.TryGetEntried(out _))
2024-07-07 14:27:34 +08:00
{
2024-08-13 18:42:51 +08:00
_windowEntryGroup.Entry(-1);
2024-07-07 14:27:34 +08:00
return;
}
2023-08-23 01:59:26 +08:00
if (History.TryPop(out var returnPanel))
{
2024-06-14 16:16:13 +08:00
ReturnBuffer.Release(returnPanel);
2023-08-23 01:59:26 +08:00
}
}
public static void Entry(IUXPanel panel) => EntryQueue.Push(panel);
2024-08-13 18:42:51 +08:00
private static void Entry(string panelName) => EntryQueue.Push(Panels[panelName]);
2023-08-23 01:59:26 +08:00
[SerializeReference, SubclassSelector] private IUXPanel initialPanel;
2024-08-13 18:42:51 +08:00
[SerializeField, ReadOnly(HideLabel = true)]
private string log;
private StringBuilder _reportBuilder = new();
2023-11-15 23:55:06 +08:00
2024-08-13 18:42:51 +08:00
private bool _initialized;
2023-11-15 23:55:06 +08:00
private void Start()
{
2024-08-13 18:42:51 +08:00
_entryGroup.OnEntry += OnEntry;
_entryGroup.OnExit += OnExit;
2023-11-15 23:55:06 +08:00
}
private static void OnExit(IUXPanel obj)
{
2024-06-14 16:16:13 +08:00
//History.Push(obj);
2023-11-15 23:55:06 +08:00
}
private static void OnEntry(IUXPanel obj)
{
2024-08-13 18:42:51 +08:00
_currentPanel = obj;
2023-11-15 23:55:06 +08:00
}
2023-08-23 01:59:26 +08:00
private void Update()
{
2024-07-15 17:26:08 +08:00
try
2023-08-23 01:59:26 +08:00
{
2024-07-15 17:26:08 +08:00
while (RegistryQueue.TryDequeue(out var result))
{
if (result is null) continue;
2024-08-13 18:42:51 +08:00
_reportBuilder.AppendLine(("注册面板:" + result.Index));
_entryGroup.list.Add(result);
2024-07-15 17:26:08 +08:00
Panels.Set(result.Index, result);
}
2023-11-30 00:25:43 +08:00
2024-07-15 17:26:08 +08:00
while (UnRegistryQueue.TryDequeue(out var result))
{
if (result is null) continue;
2024-08-13 18:42:51 +08:00
_reportBuilder.AppendLine(("注销面板:" + result.Index));
_entryGroup.list.Remove(result);
2024-07-15 17:26:08 +08:00
Panels.Remove(result.Index);
}
2023-08-23 01:59:26 +08:00
2024-07-15 17:26:08 +08:00
if (ReturnBuffer.TryGetRelease(out var returnPanel))
{
2024-08-13 18:42:51 +08:00
_reportBuilder.AppendLine(("返回面板:" + returnPanel.Index));
_entryGroup.Entry(x=>x.Index==returnPanel.Index);
2024-07-15 17:26:08 +08:00
BITAppForUnity.AllowCursor.SetElements(this, returnPanel.AllowCursor);
BITInputSystem.AllowInput.SetElements(this, returnPanel.AllowInput);
}
2024-06-14 16:16:13 +08:00
2024-07-15 17:26:08 +08:00
if (EntryQueue.TryPop(out var nextPanel))
2024-07-07 14:27:34 +08:00
{
2024-07-15 17:26:08 +08:00
if (nextPanel.IsWindow)
{
2024-08-13 18:42:51 +08:00
_reportBuilder.AppendLine(("窗口面板:" + nextPanel.Index));
_windowEntryGroup.Entry(nextPanel);
2024-07-15 17:26:08 +08:00
return;
}
2024-08-13 18:42:51 +08:00
_reportBuilder.AppendLine(("启用面板:" + nextPanel.Index));
_windowEntryGroup.Entry(-1);
History.Push(_currentPanel);
_entryGroup.Entry(x=>x.Index==nextPanel.Index);
2024-07-15 17:26:08 +08:00
BITAppForUnity.AllowCursor.SetElements(this, nextPanel.AllowCursor);
BITInputSystem.AllowInput.SetElements(this, nextPanel.AllowInput);
}
2024-08-13 18:42:51 +08:00
if (_entryGroup.TryGetEntried(out var currentPanel))
2024-07-15 17:26:08 +08:00
{
currentPanel.OnUpdate(Time.deltaTime);
2024-08-13 18:42:51 +08:00
}
if(_windowEntryGroup.TryGetEntried(out var windowPanel))
2024-07-15 17:26:08 +08:00
{
windowPanel.OnUpdate(Time.deltaTime);
2024-07-07 14:27:34 +08:00
}
2024-08-13 18:42:51 +08:00
if (currentPanel is null && Panels.Count > 0)
{
Entry(initialPanel);
}
2023-08-23 01:59:26 +08:00
}
2024-07-15 17:26:08 +08:00
catch (Exception e)
2024-07-07 14:27:34 +08:00
{
2024-07-15 17:26:08 +08:00
BIT4Log.LogException(e);
2024-07-07 14:27:34 +08:00
}
2024-08-13 18:42:51 +08:00
log = _reportBuilder.ToString();
2023-08-23 01:59:26 +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);
}
}