This commit is contained in:
CortexCore
2024-11-03 16:38:17 +08:00
parent 056e2cada5
commit 4ba741408d
4693 changed files with 2445 additions and 5443 deletions

View File

@@ -1,8 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.InputSystem.Composites;
using UnityEngine.UIElements;
namespace BITKit.UX
@@ -93,9 +94,15 @@ namespace BITKit.UX
});
}
}
public class UXAlert : MonoBehaviourSingleton<UXAlert>
public class UXAlert : UIToolkitOverlay,IUXDialogue
{
[SerializeField] private UIDocument document;
public static UXAlert Singleton;
public UXAlert(IUXService uxService, CancellationTokenSource cancellationToken) : base(uxService, cancellationToken)
{
Singleton = this;
}
protected override string DocumentPath => "ux_global_alert";
[UXBindPath("title-label")]
private Label _titleLabel;
@@ -105,24 +112,19 @@ namespace BITKit.UX
private Button _confirmButton;
[UXBindPath("cancel-button")]
private Button _cancelButton;
private void Start()
public override async UniTask InitializeAsync()
{
destroyCancellationToken.Register(Dispose);
await base.InitializeAsync();
UXUtils.Inject(this);
Close();
}
private void Dispose()
internal async void PrintAlertMessage(AlertMessage message)
{
}
internal void PrintAlertMessage(AlertMessage message)
{
if(destroyCancellationToken.IsCancellationRequested)return;
if(CancellationToken.IsCancellationRequested)return;
BITAppForUnity.AllowCursor.AddElement(this);
await InitializeAsync();
document.rootVisualElement.SetActive(true);
_titleLabel.text = message.title;
_contextLabel.text = message.message;
@@ -150,12 +152,22 @@ namespace BITKit.UX
_confirmButton.SetActive(true);
_cancelButton.SetActive(false);
}
BITAppForUnity.AllowCursor.AddElement(this);
}
private void Close()
{
document.rootVisualElement.SetActive(false);
Dispose();
BITAppForUnity.AllowCursor.RemoveElement(this);
}
public void Show(string content, string title = "Alert", Action confirmAction = null, Action<bool> onChoose = null)
{
PrintAlertMessage(new AlertMessage()
{
title = title,
message = content,
OnConfirm = confirmAction,
OnChoice = onChoose
});
}
}
}

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 2a0f1255bd3872c449a7ce3faef8ee74
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: f32aa4e4af25a9d409da57ae4bb72c8b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 7ba09e922d82abe4a817808a8f39e91b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 5dc34c310e0729b44a166514d03601b7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 9dc4856af36fd6440a4ace72cd372ef2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -6,13 +6,22 @@ using BITKit;
using UnityEngine.UIElements;
using Cysharp.Threading.Tasks;
using System.Linq;
using System.Net.Http;
using System.Text;
using UnityEngine.Serialization;
namespace BITKit.UX
{
public sealed class UXDebuger : MonoBehaviour
{
[SerializeField] private Color textColor = Color.black;
[SerializeField]private GUIStyle style;
[SerializeReference, SubclassSelector] private IReference postApi;
[SerializeField] private bool hidePostLog;
private readonly StringBuilder _logBuilder=new();
private readonly HttpClient _httpClient = new();
private void OnEnable()
{
Application.logMessageReceivedThreaded += OnLog;
@@ -21,16 +30,36 @@ namespace BITKit.UX
{
Application.logMessageReceivedThreaded -= OnLog;
}
private void OnLog(string condition, string stacktrace, LogType type)
{
_logBuilder.Append(condition);
_logBuilder.AppendLine(condition);
if (type is LogType.Error or LogType.Exception)
{
_logBuilder.AppendLine(stacktrace);
}
if (string.IsNullOrEmpty(postApi?.Value) is false)
{
try
{
_httpClient.PostAsync(postApi.Value, new StringContent(_logBuilder.ToString())).AsUniTask()
.Forget();
}
catch (Exception)
{
if (hidePostLog) return;
throw;
}
}
}
private void OnGUI()
{
GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
GUILayout.BeginArea(new Rect(120, 120, Screen.width, Screen.height));
GUILayout.BeginVertical();
GUILayout.Label(_logBuilder.ToString());
//颜色更改为黑色
GUI.color = textColor;
GUILayout.Label(_logBuilder.ToString(),style);
GUILayout.EndVertical();
GUILayout.EndArea();
}

View File

@@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.OnScreen;
using UnityEngine.UIElements;
namespace BITKit.UX
{
public class UXInputAction:OnScreenControl
{
public UXInputAction(VisualElement visualElement,string controlPathInternal)
{
this.controlPathInternal = controlPathInternal;
}
protected sealed override string controlPathInternal { get; set; }
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 66eb43c99c2ef43439773eee1acc7676
guid: 30cd0809f33228643861548780b80b61
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 1b2a8253eaf9b1b48bed89de7ff99242
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -15,10 +15,21 @@ using UnityEngine.UIElements;
namespace BITKit.UX
{
public class UXModService : MonoBehaviour
public class UXModService:UIToolKitPanel,IDisposable
{
[SerializeField] private UIDocument document;
[SerializeField] private VisualTreeAsset modTemplate;
protected override string DocumentPath => "ux_mod_Service";
private const string TemplatePath = "ux_mod_service_template";
public override bool AllowCursor => true;
public UXModService(IUXService uxService) : base(uxService)
{
ModService.OnModInstalled+=OnModInstalled;
ModService.OnModUnInstalled+=OnModUnInstalled;
ModService.OnModLoaded+=OnModLoaded;
ModService.OnModUnLoaded+=OnModUnLoaded;
ModService.OnLocked+=OnLocked;
}
private VisualTreeAsset _modTemplate;
[UXBindPath("open-mod-button")]
private Button _openModButton;
@@ -31,33 +42,18 @@ namespace BITKit.UX
[UXBindPath("mod-description-label")]
private Label _modDescriptionLabel;
[UXBindPath("reload-mod-button",true)]
private Button reloadModButton;
private Button _reloadModButton;
private readonly ConcurrentDictionary<string,VisualElement> _modContainers=new();
private void OnEnable()
{
ModService.OnModInstalled+=OnModInstalled;
ModService.OnModUnInstalled+=OnModUnInstalled;
ModService.OnModLoaded+=OnModLoaded;
ModService.OnModUnLoaded+=OnModUnLoaded;
ModService.OnLocked+=OnLocked;
}
private void OnDisable()
{
ModService.OnModInstalled-=OnModInstalled;
ModService.OnModUnInstalled-=OnModUnInstalled;
ModService.OnModLoaded-=OnModLoaded;
ModService.OnModUnLoaded-=OnModUnLoaded;
ModService.OnLocked-=OnLocked;
}
private void OnLocked(bool obj)
{
document.rootVisualElement.SetEnabled(!obj);
RootVisualElement?.SetEnabled(!obj);
}
private void Start()
public override async UniTask EntryAsync()
{
await base.EntryAsync();
_modTemplate =await ModService.LoadAsset<VisualTreeAsset>(TemplatePath);
UXUtils.Inject(this);
if (_openModButton is not null)
{
@@ -66,7 +62,7 @@ namespace BITKit.UX
if (_returnButton is not null)
{
_returnButton.clicked += UxService.Return;
_returnButton.clicked += UXService.Return;
}
_modsContainer.Clear();
@@ -75,24 +71,18 @@ namespace BITKit.UX
OnModInstalled(x);
}
if (reloadModButton is not null)
reloadModButton.clicked += async () =>
if (_reloadModButton is not null)
_reloadModButton.clicked += async () =>
{
reloadModButton.SetEnabled(false);
_reloadModButton.SetEnabled(false);
await ModService.Reload();
if (destroyCancellationToken.IsCancellationRequested)
return;
await UniTask.SwitchToMainThread();
if (destroyCancellationToken.IsCancellationRequested)
return;
reloadModButton.SetEnabled(true);
_reloadModButton.SetEnabled(true);
};
}
private async void OnModUnInstalled(IMod obj)
{
await UniTask.SwitchToMainThread();
if (destroyCancellationToken.IsCancellationRequested) return;
_modContainers.TryRemove(obj.Name, out var container);
container.RemoveFromHierarchy();
}
@@ -100,7 +90,6 @@ namespace BITKit.UX
private async void OnModInstalled(IMod obj)
{
await UniTask.SwitchToMainThread();
if (destroyCancellationToken.IsCancellationRequested) return;
var container = _modContainers.GetOrAdd(obj.Name,_=> Create(obj));
container.RegisterCallback<MouseDownEvent>(x =>
{
@@ -115,7 +104,6 @@ namespace BITKit.UX
private async void OnModUnLoaded(IMod obj)
{
await UniTask.SwitchToMainThread();
if (destroyCancellationToken.IsCancellationRequested) return;
//var container = _modContainers.GetOrAdd(obj.Name,_=> Create(obj));
if(_modContainers.TryGetValue(obj.Name,out var container))
{
@@ -126,7 +114,6 @@ namespace BITKit.UX
private async void OnModLoaded(IMod obj)
{
await UniTask.SwitchToMainThread();
if (destroyCancellationToken.IsCancellationRequested) return;
var container = _modContainers.GetOrAdd(obj.Name,_=> Create(obj));
container.Get<Toggle>().SetValueWithoutNotify(true);
}
@@ -204,7 +191,7 @@ namespace BITKit.UX
}
private VisualElement Create(IMod mod)
{
var container =_modsContainer.Create(modTemplate);
var container =_modsContainer.Create(_modTemplate);
container.Get<Toggle>().RegisterValueChangedCallback(evt =>
{
if (evt.newValue)
@@ -225,6 +212,15 @@ namespace BITKit.UX
};
return container;
}
public void Dispose()
{
ModService.OnModInstalled-=OnModInstalled;
ModService.OnModUnInstalled-=OnModUnInstalled;
ModService.OnModLoaded-=OnModLoaded;
ModService.OnModUnLoaded-=OnModUnLoaded;
ModService.OnLocked-=OnLocked;
}
}
}

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: d0757adba9accdb4caf57436d2f5ba31
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 8127b9701e668c549abd4e3c131168ce
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 43c9066041ede064c9620a23f1920392
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,15 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BITKit.UX
{
[Serializable]
public class UIToolkitPanelMonoProxy : UXPanelImplement
{
[SerializeField] private GameObject monoBehaviour;
protected override IUXPanel service => monoBehaviour.GetComponent<IUXPanel>();
}
}

View File

@@ -2,17 +2,20 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using BITKit.Mod;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
// ReSharper disable MemberCanBeProtected.Global
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable MemberCanBePrivate.Global
namespace BITKit.UX
{
public class UIToolKitPanel : MonoBehaviour,IUXPanel
public abstract class UIToolKitPanel : IUXPanel
{
public const string USSEntry = "transition_entry";
public const string USSEntryAsync = "transition_entry_async";
@@ -20,146 +23,96 @@ namespace BITKit.UX
public const string USSExit = "transition_exit";
public const string USSExitAsync = "transition_exit_async";
public const string USSExited = "transition_exited";
[RuntimeInitializeOnLoadMethod]
private static void Reload()
{
InputActionGroup = new InputActionGroup
{
allowGlobalActivation = false,
Source = nameof(UIToolKitPanel)
};
InputActionGroup.allowInput.AddElement(0);
}
public UIToolKitPanel()
{
Index = GetType().FullName;
}
[Header(Constant.Header.Components)]
[SerializeField] protected UIDocument document;
[Header(Constant.Header.Settings)]
[SerializeField] private bool isWindow;
[SerializeField] private bool closeWhenClickOutside;
[SerializeField] private bool isAnimate;
[SerializeField] private bool allowCursor;
[SerializeField] private bool allowInput;
protected readonly IUXService UXService;
protected abstract string DocumentPath { get; }
public VisualElement RootVisualElement { get; set; }
protected VisualTreeAsset VisualTreeAsset { get; private set; }
protected UIToolKitPanel(IUXService uxService)
{
UXService = uxService;
uxService.Register(this);
}
[Header(Constant.Header.Settings)]
[SerializeField] private Optional<float> entryDuration;
[SerializeField] private Optional<float> exitDuration;
protected static InputActionGroup InputActionGroup = new()
protected virtual Optional<float> EntryDuration { get; }= new();
protected virtual Optional<float> ExitDuration { get; }= new();
protected static readonly InputActionGroup InputActionGroup = new()
{
allowGlobalActivation = false,
Source = nameof(UIToolKitPanel)
};
public bool IsWindow => isWindow;
public string Index { get; private set; }
public bool AllowCursor => allowCursor;
public bool AllowInput => allowInput;
protected float TargetOpacity { get; private set; }
protected virtual VisualElement background => document.rootVisualElement;
// protected float CurrentOpacity
// {
// get => background?.GetOpacity() ?? currentOpacity;
// set
// {
// currentOpacity = value;
// background?.SetOpacity(value);
// }
// }
protected virtual void Awake()
{
Index= typeof(UIToolKitPanel) == GetType() ? gameObject.name : GetType().Name;
document.rootVisualElement.SetActive(false);
//background?.SetOpacity(0);
if (IsWindow) document.sortingOrder++;
}
protected virtual void Start()
{
UXUtils.Inject(this);
document.rootVisualElement.AddToClassList(USSEntry);
UxService.Register(this);
destroyCancellationToken.Register(() => { UxService.UnRegister(this); });
var returnButton = document.rootVisualElement.Q("return-button");
returnButton?.RegisterCallback<MouseDownEvent>(x =>
{
if (x.button is 0)
OnReturn();
});
var invisible = document.rootVisualElement.Create<VisualElement>();
invisible.name = "invisible_return_generate";
invisible.style.position = Position.Absolute;
invisible.pickingMode = PickingMode.Ignore;
invisible.style.left = 0;
invisible.style.right = 0;
invisible.style.top = 0;
invisible.style.bottom = 0;
invisible.SendToBack();
if (closeWhenClickOutside)
{
invisible.RegisterCallback<MouseDownEvent>(x => { OnReturn(); });
invisible.pickingMode = PickingMode.Position;
}
if (isWindow)
{
invisible.style.backgroundColor = new Color(0, 0, 0, 0.9f);
}
}
public virtual bool CloseWhenClickOutside { get;}
public virtual bool IsWindow { get; }
public virtual string Index => GetType().Name;
public virtual bool AllowReload { get; }
public virtual bool AllowCursor { get; }
public virtual bool AllowInput { get; }
public bool IsEntered { get; set; }
[BIT]
public void Entry()
{
UxService.Entry(this);
}
protected virtual void OnReturn()
{
UxService.Return();
UXService.Return();
}
protected virtual void OnEnable(){}
protected virtual void OnDisable(){}
protected virtual void OnPanelEntry(){}
protected virtual void OnPanelExit(){}
void IEntryElement.Entry()
{
try
{
document.rootVisualElement.SetActive(true);
OnEntry?.Invoke();
}
catch (Exception e)
{
Debug.Log(gameObject.name);
throw;
}
}
void IEntryElement.Entry()
{
InputActionGroup.allowInput.AddElement(this);
OnEntry?.Invoke();
}
async UniTask IEntryElement.EntryAsync()
{
document.rootVisualElement.AddToClassList(USSEntry);
document.rootVisualElement.AddToClassList(USSEntryAsync);
if (RootVisualElement is null)
{
VisualTreeAsset = await ModService.LoadAsset<VisualTreeAsset>(DocumentPath);
RootVisualElement = UXService.Root.As<VisualElement>().Create(VisualTreeAsset);
RootVisualElement.pickingMode = PickingMode.Ignore;
RootVisualElement.style.position = Position.Absolute;
RootVisualElement.style.left = 0;
RootVisualElement.style.right = 0;
RootVisualElement.style.top = 0;
RootVisualElement.style.bottom = 0;
if (entryDuration.Allow)
var invisible = RootVisualElement.Create<VisualElement>();
invisible.name = "invisible_return_generate";
invisible.style.position = Position.Absolute;
invisible.pickingMode = PickingMode.Ignore;
invisible.style.left = 0;
invisible.style.right = 0;
invisible.style.top = 0;
invisible.style.bottom = 0;
invisible.SendToBack();
if (CloseWhenClickOutside)
{
invisible.RegisterCallback<MouseDownEvent>(x => { OnReturn(); });
invisible.pickingMode = PickingMode.Position;
}
if (IsWindow)
{
invisible.style.backgroundColor = new Color(0, 0, 0, 0.9f);
}
UXUtils.Inject(this);
}
RootVisualElement.SetActive(true);
RootVisualElement.AddToClassList(USSEntry);
RootVisualElement.AddToClassList(USSEntryAsync);
if (EntryDuration.Allow)
{
var task = EntryAsync();
var durationTask = UniTask.Delay(TimeSpan.FromSeconds(entryDuration.Value), cancellationToken: destroyCancellationToken);
var durationTask = UniTask.Delay(TimeSpan.FromSeconds(EntryDuration.Value));
await durationTask;
document.rootVisualElement.RemoveFromClassList(USSEntry);
document.rootVisualElement.RemoveFromClassList(USSEntryAsync);
document.rootVisualElement.AddToClassList(USSEntered);
RootVisualElement.RemoveFromClassList(USSEntry);
RootVisualElement.RemoveFromClassList(USSEntryAsync);
RootVisualElement.AddToClassList(USSEntered);
await task;
}
@@ -167,6 +120,19 @@ namespace BITKit.UX
{
await EntryAsync();
}
try
{
if (OnEntryAsync is not null)
{
await OnEntryAsync.UniTaskFunc();
}
}
catch (Exception e)
{
BIT4Log.LogException(e);
}
}
public virtual UniTask EntryAsync()
{
@@ -175,32 +141,48 @@ namespace BITKit.UX
void IEntryElement.Entered()
{
OnPanelEntry();
OnEntryCompleted?.Invoke();
}
void IEntryElement.Exit()
{
document.rootVisualElement.AddToClassList(USSExit);
//if (IsValid is false) return;
RootVisualElement?.AddToClassList(USSExit);
OnPanelExit();
InputActionGroup.allowInput.RemoveElement(this);
OnExit?.Invoke();
}
async UniTask IEntryElement.ExitAsync()
{
document.rootVisualElement.RemoveFromClassList(USSEntered);
document.rootVisualElement.AddToClassList(USSExitAsync);
if (entryDuration.Allow is false) return;
await UniTask.Delay(TimeSpan.FromSeconds(entryDuration.Value), cancellationToken: destroyCancellationToken);
RootVisualElement?.RemoveFromClassList(USSEntered);
RootVisualElement?.AddToClassList(USSExitAsync);
await OnExitAsync.UniTaskFunc();
if (EntryDuration.Allow is false) return;
await UniTask.Delay(TimeSpan.FromSeconds(EntryDuration.Value));
}
void IEntryElement.Exited()
{
document.rootVisualElement.RemoveFromClassList(USSExit);
document.rootVisualElement.RemoveFromClassList(USSExitAsync);
document.rootVisualElement.AddToClassList(USSEntry);
document.rootVisualElement.SetActive(false);
OnExit?.Invoke();
RootVisualElement?.RemoveFromClassList(USSExit);
RootVisualElement?.RemoveFromClassList(USSExitAsync);
RootVisualElement?.AddToClassList(USSEntry);
RootVisualElement?.SetActive(false);
if (AllowReload)
{
RootVisualElement?.RemoveFromHierarchy();
RootVisualElement = null;
}
OnExitCompleted?.Invoke();
}
public event Action OnEntry;
public event Func<UniTask> OnEntryAsync;
public event Action OnEntryCompleted;
public event Action OnExit;
public virtual void OnUpdate(float deltaTime)
public event Func<UniTask> OnExitAsync;
public event Action OnExitCompleted;
public virtual void OnTick(float deltaTime)
{
}
}

View File

@@ -0,0 +1,54 @@
using System;
using System.Threading;
using BITKit.Mod;
using Cysharp.Threading.Tasks;
using UnityEngine.UIElements;
// ReSharper disable MemberCanBePrivate.Global
namespace BITKit.UX
{
public abstract class UIToolkitOverlay:IDisposable
{
protected readonly CancellationTokenSource CancellationToken;
protected readonly IUXService UXService;
protected VisualElement RootVisualElement { get; set; }
private bool _initialized;
protected UIToolkitOverlay(IUXService uxService, CancellationTokenSource cancellationToken)
{
UXService = uxService;
CancellationToken = cancellationToken;
CancellationToken.Token.Register(Dispose);
}
protected abstract string DocumentPath { get; }
// ReSharper disable once MemberCanBeProtected.Global
public virtual async UniTask InitializeAsync()
{
if(_initialized)return;
var asset =await ModService.LoadAsset<VisualTreeAsset>(DocumentPath);
var root= RootVisualElement = UXService.Root.As<VisualElement>().Create(asset);
RootVisualElement.BringToFront();
RootVisualElement.name = GetType().Name;
RootVisualElement.pickingMode = PickingMode.Ignore;
root.style.position = Position.Absolute;
root.style.top = 0;
root.style.bottom = 0;
root.style.left = 0;
root.style.right = 0;
UXUtils.Inject(this);
_initialized = true;
}
public virtual void Dispose()
{
if(RootVisualElement is null)return;
_initialized = false;
RootVisualElement.RemoveFromHierarchy();
RootVisualElement = null;
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 2dbf277baef324944aa89bbd72966261
guid: e78199af4607c0d438836fd782fdf8ff
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -1,18 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BITKit.UX
{
public class UIToolkitPanelDebuger : UIToolKitPanel
{
[SerializeField] private InputActionGroup inputActionGroup;
private void Update()
{
inputActionGroup = InputActionGroup;
}
}
}

View File

@@ -0,0 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using BITKit.UX;
using UnityEngine;
public class UIToolkitSubPanel
{
public UIToolkitSubPanel()
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 398253ee7f746d143bbbcd9effab9da1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,64 +1,62 @@
using System;
using System.Collections.Generic;
using System.Text;
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 : MonoBehaviour, IUXService
public class UXService : IUXService
{
/// <summary>
/// 重新初始化,使用<see cref="RuntimeInitializeLoadType.SubsystemRegistration"/>确保在所有子系统注册后执行
/// </summary>
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void Initialized()
private readonly IAfterTicker _ticker;
private readonly IServiceProvider _serviceProvider;
public UXService(IAfterTicker ticker, IServiceProvider serviceProvider)
{
RegistryQueue.Clear();
UnRegistryQueue.Clear();
Panels.Clear();
EntryQueue.Clear();
_currentPanel = null;
History.Clear();
_entryGroup = new EntryGroup<IUXPanel>();
_windowEntryGroup = new EntryGroup<IUXPanel>();
_ticker = ticker;
_serviceProvider = serviceProvider;
_entryGroup.OnEntry += OnEntry;
_ticker.Add(OnTick);
}
private static EntryGroup<IUXPanel> _entryGroup = new();
private static EntryGroup<IUXPanel> _windowEntryGroup = new();
private readonly EntryGroup<IUXPanel> _entryGroup = new();
private readonly EntryGroup<IUXPanel> _windowEntryGroup = new();
/// <summary>
/// 内部注册面板队列
/// </summary>
private static readonly Queue<IUXPanel> RegistryQueue = new();
private readonly Queue<IUXPanel> _registryQueue = new();
/// <summary>
/// 内部注销面板队列
/// </summary>
private static readonly Queue<IUXPanel> UnRegistryQueue = new();
private readonly Queue<IUXPanel> _unRegistryQueue = new();
/// <summary>
/// 已注册面板字典
/// </summary>
private static readonly Dictionary<string, IUXPanel> Panels = new();
private readonly Dictionary<string, IUXPanel> _panels = new();
/// <summary>
/// 等待启用的面板队列
/// </summary>
private static readonly Stack<IUXPanel> EntryQueue = new();
private readonly Stack<IUXPanel> _entryQueue = new();
private readonly List<string> _entryQueueByName = new();
/// <summary>
/// 返回面板缓冲区
/// </summary>
private static readonly DoubleBuffer<IUXPanel> ReturnBuffer = new();
private readonly DoubleBuffer<IUXPanel> _returnBuffer = new();
/// <summary>
/// 已启用面板
/// </summary>
private static IUXPanel _currentPanel;
private IUXPanel _currentPanel;
/// <summary>
/// 历史面板
@@ -70,13 +68,43 @@ namespace BITKit.UX
/// </summary>
public static void ClearHistory() => History.Clear();
public static void Register(IUXPanel panel) => RegistryQueue.Enqueue(panel);
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 static void UnRegister(IUXPanel panel) => UnRegistryQueue.Enqueue(panel);
public void Register(IUXPanel panel) => _registryQueue.Enqueue(panel);
public static void Entry<T>() where T : IUXPanel => EntryQueue.Push(Panels[typeof(T).Name]);
public void UnRegister(IUXPanel panel) => _unRegistryQueue.Enqueue(panel);
public static void Return()
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 void Return()
{
if(_windowEntryGroup.TryGetEntried(out _))
{
@@ -85,70 +113,58 @@ namespace BITKit.UX
}
if (History.TryPop(out var returnPanel))
{
ReturnBuffer.Release(returnPanel);
_returnBuffer.Release(returnPanel);
}
}
public static void Entry(IUXPanel panel) => EntryQueue.Push(panel);
private static void Entry(string panelName) => EntryQueue.Push(Panels[panelName]);
[SerializeReference, SubclassSelector] private IUXPanel initialPanel;
[SerializeField, ReadOnly(HideLabel = true)]
private string log;
private StringBuilder _reportBuilder = new();
private bool _initialized;
private void Start()
{
_entryGroup.OnEntry += OnEntry;
_entryGroup.OnExit += OnExit;
}
private static void OnExit(IUXPanel obj)
{
//History.Push(obj);
}
private static void OnEntry(IUXPanel obj)
private void OnEntry(IUXPanel obj)
{
_currentPanel = obj;
}
private void Update()
private void OnTick(float delta)
{
try
{
while (RegistryQueue.TryDequeue(out var result))
while (_registryQueue.TryDequeue(out var result))
{
if (result is null) continue;
_reportBuilder.AppendLine(("注册面板:" + result.Index));
_entryGroup.list.Add(result);
Panels.Set(result.Index, result);
_panels.Set(result.Index, result);
}
while (UnRegistryQueue.TryDequeue(out var result))
while (_unRegistryQueue.TryDequeue(out var result))
{
if (result is null) continue;
_reportBuilder.AppendLine(("注销面板:" + result.Index));
_entryGroup.list.Remove(result);
Panels.Remove(result.Index);
_panels.Remove(result.Index);
}
if (ReturnBuffer.TryGetRelease(out var returnPanel))
if (_returnBuffer.TryGetRelease(out var returnPanel))
{
_reportBuilder.AppendLine(("返回面板:" + returnPanel.Index));
_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))
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)
{
_reportBuilder.AppendLine(("窗口面板:" + nextPanel.Index));
_windowEntryGroup.Entry(nextPanel);
return;
}
_reportBuilder.AppendLine(("启用面板:" + nextPanel.Index));
_windowEntryGroup.Entry(-1);
History.Push(_currentPanel);
_entryGroup.Entry(x=>x.Index==nextPanel.Index);
@@ -157,36 +173,31 @@ namespace BITKit.UX
}
if (_entryGroup.TryGetEntried(out var currentPanel))
{
currentPanel.OnUpdate(Time.deltaTime);
currentPanel.OnTick(Time.deltaTime);
}
if(_windowEntryGroup.TryGetEntried(out var windowPanel))
{
windowPanel.OnUpdate(Time.deltaTime);
}
if (currentPanel is null && Panels.Count > 0)
{
Entry(initialPanel);
windowPanel.OnTick(Time.deltaTime);
}
}
catch (Exception e)
{
BIT4Log.LogException(e);
}
log = _reportBuilder.ToString();
}
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);
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();
}
}
}
}

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 1a4110ffce8eaaa4fbb4c6cc86d62f1e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 47ef6611ff66d144aba7e30f8cbecb09
guid: 951ffa58b33aefc46bb69a009983f61d
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@@ -0,0 +1,85 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
namespace BITKit.UX
{
public class UXToolTips:IDisposable
{
private readonly IUXService _uxService;
private readonly IMainTicker _ticker;
public UXToolTips(IUXService uxService, IMainTicker ticker)
{
_uxService = uxService;
_ticker = ticker;
_ticker.Add(OnTick);
}
private VisualElement _rootVisualElement;
private Label _label;
public void Dispose()
{
_ticker.Remove(OnTick);
}
private void OnTick(float obj)
{
if (_label is null && _uxService.Root is not null)
{
_rootVisualElement=_uxService.Root as VisualElement;
_label = _rootVisualElement.Create<Label>();
_label.AddToClassList("bitkit-tool-tips");
_label.style.position = Position.Absolute;
}
if(_label is null || _rootVisualElement is null)return;
var tooltip = CurrentToolTip(_rootVisualElement.panel);
if (tooltip != "")
{
var mouse = Mouse.current;
if (mouse is null) return;
var mousePos = mouse.position.ReadValue();
mousePos.y = Screen.height - mousePos.y;
var pos =RuntimePanelUtils.ScreenToPanel(_label.panel,mousePos);
pos.x += 24;
if (pos.x + _label.layout.width > _label.panel.visualTree.layout.width)
{
//pos.x = label.panel.visualTree.layout.width - label.layout.width - label.layout.width;
pos.x-=_label.layout.width+48;
}
_label.visible = true;
_label.text = tooltip;
_label.transform.position = pos;
_label.BringToFront();
}
else
{
_label.visible = false;
}
}
private string CurrentToolTip(IPanel panel)
{
// https://docs.unity3d.com/2022.2/Documentation/Manual/UIE-faq-event-and-input-system.html
if (!EventSystem.current.IsPointerOverGameObject()) return "";
var screenPosition = Mouse.current.position.ReadValue();
screenPosition.y = Screen.height - screenPosition.y;
VisualElement ve = panel.Pick(RuntimePanelUtils.ScreenToPanel(panel, screenPosition));
return ve == null ? "" : ve.tooltip;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ac89bede6edc4874380395982ecc8af8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: c2c992e0a089cf6469c3eca1f02e8617
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: db55007c589734842b1bd2e4d6367ed2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -8,43 +8,70 @@ using UnityEngine.UIElements;
namespace BITKit.UX
{
[AttributeUsage(AttributeTargets.Field|AttributeTargets.Property)]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class UXBindPathAttribute : Attribute
{
public string Path;
public bool CanBeNull;
public UXBindPathAttribute(){}
public UXBindPathAttribute()
{
}
public UXBindPathAttribute(string path)
{
Path = path;
}
public UXBindPathAttribute(string path,bool canBeNull)
public UXBindPathAttribute(string path, bool canBeNull)
{
Path = path;
CanBeNull = canBeNull;
}
}
public class UXUtils
public class UXUtils
{
public static void Inject(object self)
public static void Inject(object self,VisualElement root=null)
{
UIDocument document;
UIDocument document = null;
var field = self.GetType().GetField("document",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var rootVisualElementFieldInfo = self.GetType().GetField("RootVisualElement",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var rootVisualElementPropertyInfo = self.GetType().GetProperty("RootVisualElement",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
switch (field)
var rootVisualElement = (rootVisualElementFieldInfo, rootVisualElementPropertyInfo) switch
{
case null when self is MonoBehaviour monoBehaviour && monoBehaviour.TryGetComponent<UIDocument>(out document):
break;
case not null when field.GetValue(self) is UIDocument _document:
document = _document;
break;
default:
BIT4Log.Warning<UXUtils>($"document 未赋值或未找到");
return;
{ } when rootVisualElementPropertyInfo?.GetValue(self) is VisualElement ve => ve,
{ } when rootVisualElementFieldInfo?.GetValue(self) is VisualElement ve => ve,
_ => root,
};
if (rootVisualElement is not null)
{
}
else
{
switch (field)
{
case null when self is MonoBehaviour monoBehaviour &&
monoBehaviour.TryGetComponent<UIDocument>(out document):
break;
case not null when field.GetValue(self) is UIDocument _document:
document = _document;
break;
default:
BIT4Log.Warning<UXUtils>($"document 未赋值或未找到@{self.GetType().Name}");
return;
}
}
foreach (var fieldInfo in self.GetType()
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
@@ -52,7 +79,12 @@ namespace BITKit.UX
)
{
var bindPathAtt = fieldInfo.GetCustomAttribute<UXBindPathAttribute>();
VisualElement ve = document.rootVisualElement;
VisualElement ve = (document, rootVisualElement) switch
{
(_, not null) => rootVisualElement,
(not null, _) => document.rootVisualElement,
_ => throw new NotImplementedException(),
};
foreach (var path in bindPathAtt.Path.Split("."))
{
ve = ve.Q(path);
@@ -71,21 +103,6 @@ namespace BITKit.UX
BIT4Log.Warning<UXUtils>(field!.Name);
}
}
// if (field.GetValue(self) is not UIDocument _document)
// {
// if (self is MonoBehaviour monoBehaviour && (document = monoBehaviour.GetComponentInParent<UIDocument>()))
// {
// field.SetValue(self,document);
// }
// else
// {
// BIT4Log.Warning<UXUtils>($"document 未赋值");
// return;
// }
// }
}
}
}

View File

@@ -1,13 +1,10 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using BITKit.Mod;
using BITKit.UX;
using UnityEngine;
using Cysharp.Threading.Tasks;
using kcp2k;
using UnityEngine.Pool;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit
{
@@ -25,37 +22,45 @@ namespace BITKit
Implementation.Release(handle);
}
}
[CustomType(typeof(IUXWaiting))]
public class UXWaiting : MonoBehaviourSingleton<UXWaiting>,IUXWaiting
public sealed class WaitingHandle : IUXWaitingHandle
{
public sealed class WaitingHandle : IUXWaitingHandle
public string Message
{
public string Message
{
get => label.text;
set => SetMessage(value);
}
public object Container => container;
internal VisualElement container;
internal Label label;
internal Action OnDispose;
internal Action<string> OnSetMessage;
public void SetMessage(string message) => OnSetMessage?.Invoke(message);
public void Dispose() => OnDispose?.Invoke();
get => Label.text;
set => SetMessage(value);
}
public object Container => VisualElement;
internal VisualElement VisualElement;
internal Label Label;
internal Action OnDispose;
internal Action<string> OnSetMessage;
public void SetMessage(string message) => OnSetMessage?.Invoke(message);
public void Dispose() => OnDispose?.Invoke();
}
[CustomType(typeof(IUXWaiting))]
public sealed class UXWaiting : UIToolkitOverlay,IUXWaiting
{
private const string HandleTemplate = "ux_waiting_handle";
public static UXWaiting Singleton { get; private set; }
private readonly ITicker _ticker;
public UXWaiting(IUXService uxService, ITicker ticker, CancellationTokenSource cancellationToken) : base(uxService, cancellationToken)
{
_ticker = ticker;
Singleton = this;
_visibleHandle.AddListener(Dispose);
}
protected override string DocumentPath => "ux_global_waiting";
[SerializeField] private VisualTreeAsset handleTemplate;
[SerializeField] private bool asGlobal;
private VisualTreeAsset _handleTemplate;
[UXBindPath("waiting-container")]
private VisualElement _container;
[UXBindPath("waiting-root")]
private VisualElement _root;
private readonly ConcurrentQueue<(WaitingHandle handle,string text)> _messageQueue = new();
private readonly ConcurrentQueue<WaitingHandle> _initializedHandles = new();
private readonly ConcurrentQueue<WaitingHandle> _disposeQueue = new();
private readonly ValidHandle _visibleHandle = new();
private InitializationState _initializationState;
public IUXWaitingHandle Get()
{
var handle = new WaitingHandle();
@@ -67,42 +72,74 @@ namespace BITKit
handle.OnDispose = () =>
{
_disposeQueue.Enqueue(handle);
_ticker.Add(OnTick);
};
_ticker.Add(OnTick);
return handle;
}
public void Release(IUXWaitingHandle handle)
{
handle.Dispose();
}
private void Update()
private async void OnTick(float deltaTime)
{
switch (_initializationState)
{
case InitializationState.None:
_initializationState = InitializationState.Initializing;
await InitializeAsync();
break;
case InitializationState.Initializing:
return;
}
while (_initializedHandles.TryDequeue(out var handle))
{
var container = _container.Create(handleTemplate);
handle.container = container;
handle.label = container.Get<Label>();
var container = _container.Create(_handleTemplate);
_visibleHandle.AddElement(handle);
handle.VisualElement = container;
handle.Label = container.Get<Label>();
}
while (_messageQueue.TryDequeue(out var message))
{
message.handle.label.text = message.text;
message.handle.Label.text = message.text;
}
while (_disposeQueue.TryDequeue(out var handle))
{
handle.container.RemoveFromHierarchy();
handle.VisualElement.RemoveFromHierarchy();
_visibleHandle.RemoveElement(handle);
}
_visibleHandle.SetElements(0, _container.childCount>0);
_visibleHandle.Invoke();
}
protected override void Awake()
public override async UniTask InitializeAsync()
{
base.Awake();
await base.InitializeAsync();
_handleTemplate = await ModService.LoadAsset<VisualTreeAsset>(HandleTemplate);
if (_handleTemplate is null)
{
Debug.LogError($"{HandleTemplate} is null");
}
UXUtils.Inject(this);
_container.Clear();
_root.SetActive(false);
_initializationState = InitializationState.Initialized;
}
private void Start()
private void Dispose(bool dontDispose)
{
_visibleHandle.AddListener(_root.SetActive);
_visibleHandle.Invoke();
if (dontDispose is false)
{
Dispose();
}
}
public override void Dispose()
{
_initializationState = 0;
base.Dispose();
}
}
}