217 lines
5.5 KiB
C#
217 lines
5.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using BITKit.Mod;
|
|
using Cysharp.Threading.Tasks;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace BITKit.UX
|
|
{
|
|
/// <summary>
|
|
/// 适用于Unity的UX Service
|
|
/// </summary>
|
|
public class UXService : IUXService
|
|
{
|
|
private readonly IAfterTicker _ticker;
|
|
private readonly IServiceProvider _serviceProvider;
|
|
private readonly CancellationTokenSource _cancellationTokenSource;
|
|
public UXService(IAfterTicker ticker, IServiceProvider serviceProvider, CancellationTokenSource cancellationTokenSource)
|
|
{
|
|
_ticker = ticker;
|
|
_serviceProvider = serviceProvider;
|
|
_cancellationTokenSource = cancellationTokenSource;
|
|
_entryGroup.OnEntry += OnEntry;
|
|
_ticker.Add(OnTick);
|
|
}
|
|
|
|
private readonly EntryGroup<IUXPanel> _entryGroup = new();
|
|
private readonly EntryGroup<IUXPanel> _windowEntryGroup = new();
|
|
/// <summary>
|
|
/// 内部注册面板队列
|
|
/// </summary>
|
|
private readonly Queue<IUXPanel> _registryQueue = new();
|
|
|
|
/// <summary>
|
|
/// 内部注销面板队列
|
|
/// </summary>
|
|
private readonly Queue<IUXPanel> _unRegistryQueue = new();
|
|
|
|
/// <summary>
|
|
/// 已注册面板字典
|
|
/// </summary>
|
|
private readonly Dictionary<string, IUXPanel> _panels = new();
|
|
|
|
/// <summary>
|
|
/// 等待启用的面板队列
|
|
/// </summary>
|
|
private readonly Stack<IUXPanel> _entryQueue = new();
|
|
private readonly List<string> _entryQueueByName = new();
|
|
|
|
/// <summary>
|
|
/// 返回面板缓冲区
|
|
/// </summary>
|
|
private readonly DoubleBuffer<IUXPanel> _returnBuffer = new();
|
|
|
|
/// <summary>
|
|
/// 已启用面板
|
|
/// </summary>
|
|
private IUXPanel _currentPanel;
|
|
|
|
/// <summary>
|
|
/// 历史面板
|
|
/// </summary>
|
|
private static readonly Stack<IUXPanel> History = new();
|
|
|
|
/// <summary>
|
|
/// 清空历史面板,通常用于关闭返回上一步
|
|
/// </summary>
|
|
public static void ClearHistory() => History.Clear();
|
|
|
|
public string SettingsPath { get; set; } = "ux_panel_settings";
|
|
public object Root { get; private set; }
|
|
public async UniTask InitializeAsync()
|
|
{
|
|
|
|
var gameObject = new GameObject("UXService");
|
|
Object.DontDestroyOnLoad(gameObject);
|
|
|
|
_cancellationTokenSource.Token.Register(() =>
|
|
{
|
|
Object.Destroy(gameObject);
|
|
});
|
|
|
|
var document = gameObject.AddComponent<UIDocument>();
|
|
try
|
|
{
|
|
var panelSettings =await ModService.LoadAsset<PanelSettings>(SettingsPath);
|
|
document.panelSettings = panelSettings;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
BIT4Log.Warning<UXService>("未找到ux_panel_settings");
|
|
throw;
|
|
}
|
|
|
|
|
|
Root = document.rootVisualElement;
|
|
}
|
|
|
|
public void Register(IUXPanel panel) => _registryQueue.Enqueue(panel);
|
|
|
|
public void UnRegister(IUXPanel panel) => _unRegistryQueue.Enqueue(panel);
|
|
|
|
public void Entry<T>() where T : IUXPanel
|
|
{
|
|
var panel = _serviceProvider.GetRequiredService<T>();
|
|
Entry(panel);
|
|
//Entry(typeof(T).Name);
|
|
}
|
|
|
|
public void Entry(IUXPanel panel) => _entryQueue.Push(panel);
|
|
public void Entry(string panelName) => _entryQueueByName.TryAdd(panelName);
|
|
public IUXPanel CurrentPanel => _currentPanel;
|
|
public event Action<IUXPanel, IUXPanel> OnPanelChanged;
|
|
|
|
public void Return()
|
|
{
|
|
if(_windowEntryGroup.TryGetEntried(out _))
|
|
{
|
|
_windowEntryGroup.Entry(-1);
|
|
return;
|
|
}
|
|
if (History.TryPop(out var returnPanel))
|
|
{
|
|
_returnBuffer.Release(returnPanel);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private bool _initialized;
|
|
|
|
|
|
private void OnEntry(IUXPanel obj)
|
|
{
|
|
OnPanelChanged?.Invoke(_currentPanel,obj);
|
|
_currentPanel = obj;
|
|
}
|
|
private void OnTick(float delta)
|
|
{
|
|
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);
|
|
}
|
|
|
|
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))
|
|
{
|
|
if (nextPanel.IsWindow)
|
|
{
|
|
_windowEntryGroup.Entry(nextPanel);
|
|
return;
|
|
}
|
|
_windowEntryGroup.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.OnTick(Time.deltaTime);
|
|
}
|
|
if(_windowEntryGroup.TryGetEntried(out var windowPanel))
|
|
{
|
|
windowPanel.OnTick(Time.deltaTime);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
BIT4Log.LogException(e);
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|