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

207 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;
2024-11-03 16:38:17 +08:00
using BITKit.Mod;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
2023-08-23 01:59:26 +08:00
using UnityEngine;
2024-11-03 16:38:17 +08:00
using UnityEngine.UIElements;
using Object = UnityEngine.Object;
2023-08-23 01:59:26 +08:00
namespace BITKit.UX
{
/// <summary>
/// 适用于Unity的UX Service
/// </summary>
2024-11-03 16:38:17 +08:00
public class UXService : IUXService
2023-08-23 01:59:26 +08:00
{
2024-11-03 16:38:17 +08:00
private readonly IAfterTicker _ticker;
private readonly IServiceProvider _serviceProvider;
public UXService(IAfterTicker ticker, IServiceProvider serviceProvider)
2023-08-23 01:59:26 +08:00
{
2024-11-03 16:38:17 +08:00
_ticker = ticker;
_serviceProvider = serviceProvider;
_entryGroup.OnEntry += OnEntry;
_ticker.Add(OnTick);
2023-08-23 01:59:26 +08:00
}
2024-11-03 16:38:17 +08:00
private readonly EntryGroup<IUXPanel> _entryGroup = new();
private readonly EntryGroup<IUXPanel> _windowEntryGroup = new();
2023-08-23 01:59:26 +08:00
/// <summary>
/// 内部注册面板队列
/// </summary>
2024-11-03 16:38:17 +08:00
private readonly Queue<IUXPanel> _registryQueue = new();
2023-08-23 01:59:26 +08:00
/// <summary>
/// 内部注销面板队列
/// </summary>
2024-11-03 16:38:17 +08:00
private readonly Queue<IUXPanel> _unRegistryQueue = new();
2023-08-23 01:59:26 +08:00
/// <summary>
/// 已注册面板字典
/// </summary>
2024-11-03 16:38:17 +08:00
private readonly Dictionary<string, IUXPanel> _panels = new();
2023-08-23 01:59:26 +08:00
/// <summary>
/// 等待启用的面板队列
/// </summary>
2024-11-03 16:38:17 +08:00
private readonly Stack<IUXPanel> _entryQueue = new();
private readonly List<string> _entryQueueByName = new();
2023-08-23 01:59:26 +08:00
2024-06-14 16:16:13 +08:00
/// <summary>
/// 返回面板缓冲区
/// </summary>
2024-11-03 16:38:17 +08:00
private readonly DoubleBuffer<IUXPanel> _returnBuffer = new();
2024-06-14 16:16:13 +08:00
2023-08-23 01:59:26 +08:00
/// <summary>
/// 已启用面板
/// </summary>
2024-11-03 16:38:17 +08:00
private 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
2024-11-03 16:38:17 +08:00
public object Root { get; private set; }
public async UniTask InitializeAsync()
{
var gameObject = new GameObject("UXService");
Object.DontDestroyOnLoad(gameObject);
var document = gameObject.AddComponent<UIDocument>();
try
{
var panelSettings =await ModService.LoadAsset<PanelSettings>("ux_panel_settings");
document.panelSettings = panelSettings;
}
catch (Exception e)
{
BIT4Log.Warning<UXService>("未找到ux_panel_settings");
throw;
}
Root = document.rootVisualElement;
}
public void Register(IUXPanel panel) => _registryQueue.Enqueue(panel);
2023-08-23 01:59:26 +08:00
2024-11-03 16:38:17 +08:00
public void UnRegister(IUXPanel panel) => _unRegistryQueue.Enqueue(panel);
2023-08-23 01:59:26 +08:00
2024-11-03 16:38:17 +08:00
public void Entry<T>() where T : IUXPanel
{
var panel = _serviceProvider.GetRequiredService<T>();
Entry(panel);
//Entry(typeof(T).Name);
}
2023-08-23 01:59:26 +08:00
2024-11-03 16:38:17 +08:00
public void Entry(IUXPanel panel) => _entryQueue.Push(panel);
public void Entry(string panelName) => _entryQueueByName.TryAdd(panelName);
2024-11-08 12:52:09 +08:00
public IUXPanel CurrentPanel => _currentPanel;
public event Action<IUXPanel, IUXPanel> OnPanelChanged;
2024-11-03 16:38:17 +08:00
public void Return()
2023-08-23 01:59:26 +08:00
{
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-11-03 16:38:17 +08:00
_returnBuffer.Release(returnPanel);
2023-08-23 01:59:26 +08:00
}
}
2024-11-03 16:38:17 +08:00
2023-11-15 23:55:06 +08:00
2024-08-13 18:42:51 +08:00
private bool _initialized;
2024-11-03 16:38:17 +08:00
private void OnEntry(IUXPanel obj)
2023-11-15 23:55:06 +08:00
{
2024-11-08 12:52:09 +08:00
OnPanelChanged?.Invoke(_currentPanel,obj);
2024-08-13 18:42:51 +08:00
_currentPanel = obj;
2023-11-15 23:55:06 +08:00
}
2024-11-03 16:38:17 +08:00
private void OnTick(float delta)
2023-08-23 01:59:26 +08:00
{
2024-07-15 17:26:08 +08:00
try
2023-08-23 01:59:26 +08:00
{
2024-11-03 16:38:17 +08:00
while (_registryQueue.TryDequeue(out var result))
2024-07-15 17:26:08 +08:00
{
if (result is null) continue;
2024-08-13 18:42:51 +08:00
_entryGroup.list.Add(result);
2024-11-03 16:38:17 +08:00
_panels.Set(result.Index, result);
2024-07-15 17:26:08 +08:00
}
2023-11-30 00:25:43 +08:00
2024-11-03 16:38:17 +08:00
while (_unRegistryQueue.TryDequeue(out var result))
2024-07-15 17:26:08 +08:00
{
if (result is null) continue;
2024-08-13 18:42:51 +08:00
_entryGroup.list.Remove(result);
2024-11-03 16:38:17 +08:00
_panels.Remove(result.Index);
2024-07-15 17:26:08 +08:00
}
2023-08-23 01:59:26 +08:00
2024-11-03 16:38:17 +08:00
if (_returnBuffer.TryGetRelease(out var returnPanel))
2024-07-15 17:26:08 +08:00
{
2024-08-13 18:42:51 +08:00
_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-11-03 16:38:17 +08:00
foreach (var panelName in _entryQueueByName)
{
if (!_panels.TryGetValue(panelName, out var panel))continue;
_entryQueue.Push(panel);
_entryQueueByName.TryRemove(panelName);
break;
}
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
_windowEntryGroup.Entry(nextPanel);
2024-07-15 17:26:08 +08:00
return;
}
2024-08-13 18:42:51 +08:00
_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
{
2024-11-03 16:38:17 +08:00
currentPanel.OnTick(Time.deltaTime);
2024-08-13 18:42:51 +08:00
}
if(_windowEntryGroup.TryGetEntried(out var windowPanel))
2024-07-15 17:26:08 +08:00
{
2024-11-03 16:38:17 +08:00
windowPanel.OnTick(Time.deltaTime);
2024-08-13 18:42:51 +08:00
}
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
}
2023-08-23 01:59:26 +08:00
}
2024-11-03 16:38:17 +08:00
public async void Dispose()
{
_ticker.Remove(OnTick);
await UniTask.SwitchToMainThread();
if (_currentPanel is not null)
{
// ReSharper disable once MethodHasAsyncOverload
_currentPanel.Exit();
await _currentPanel.ExitAsync();
_currentPanel.Exited();
}
}
2023-08-23 01:59:26 +08:00
}
}