This commit is contained in:
CortexCore
2025-03-24 14:42:40 +08:00
parent 18239a5ae4
commit 9845d20f7f
99 changed files with 5418 additions and 5512 deletions

View File

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

View File

@@ -0,0 +1,73 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BITKit;
using BITKit.UX;
using Net.BITKit.Localization;
using UnityEngine;
using UnityEngine.UIElements;
namespace Net.BITKit.UX
{
public class UXLocalization
{
public string USS { get; set; } = "localized";
private readonly IUXService _uxService;
private readonly ILocalizationService _localizationService;
public UXLocalization(IUXService uxService, ILocalizationService localizationService)
{
_uxService = uxService;
_localizationService = localizationService;
_localizationService.OnLanguageChanged += OnLanguageChanged;
}
private void OnLanguageChanged(string arg1, string arg2)
{
if(_uxService.Root is not VisualElement visualElement)return;
foreach (var x in visualElement.Query<Label>(className:USS).ToList())
{
if (string.IsNullOrEmpty(x.viewDataKey))continue;
x.text = _localizationService.GetLocalizedString('#'+x.viewDataKey);
}
foreach (var x in visualElement.Query<Button>(className:USS).ToList())
{
if(string.IsNullOrEmpty(x.viewDataKey))continue;
x.text = _localizationService.GetLocalizedString('#'+x.viewDataKey);
}
foreach (var x in visualElement.Query(className:USS).ToList())
{
if(string.IsNullOrEmpty(x.viewDataKey))continue;
ContinueWithBaseType(x.GetType());
continue;
void ContinueWithBaseType(Type type)
{
while (true)
{
if (type == null || type == typeof(object)) return;
if (type.GetProperty("label", ReflectionHelper.Flags) is { } propertyInfo)
{
//Debug.Log($"{x.name}:#{x.viewDataKey}");
propertyInfo.SetValue(x, _localizationService.GetLocalizedString('#' + x.viewDataKey));
}
else
{
type = type.BaseType;
continue;
}
break;
}
}
}
}
}
}

View File

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

View File

@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using BITKit.Mod;
using BITKit.StateMachine;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Serialization;
@@ -18,7 +19,7 @@ using Debug = UnityEngine.Debug;
namespace BITKit.UX
{
public abstract class UIToolKitPanel : IUXPanel,IDisposable
public abstract class UIToolKitPanel :StateAsync, IUXPanel,IDisposable
{
public const string USSEntry = "transition_entry";
public const string USSEntryAsync = "transition_entry_async";
@@ -115,7 +116,7 @@ namespace BITKit.UX
//WaitUtilTransitionCompleted = new();
}
protected static readonly InputActionGroup InputActionGroup = new()
protected readonly InputActionGroup InputActionGroup = new()
{
allowGlobalActivation = false,
Source = nameof(UIToolKitPanel)
@@ -137,13 +138,12 @@ namespace BITKit.UX
}
protected virtual void OnPanelEntry(){}
protected virtual void OnPanelExit(){}
void IEntryElement.Entry()
public override void OnStateEntry(IState old)
{
InputActionGroup.allowInput.AddElement(this);
OnEntry?.Invoke();
}
async UniTask IEntryElement.EntryAsync()
public override async UniTask OnStateEntryAsync(IState old)
{
await InitializeAsync();
@@ -193,6 +193,12 @@ namespace BITKit.UX
await UniTask.SwitchToMainThread();
RootVisualElement.AddToClassList(USSEntered);
OnPanelEntry();
OnEntryCompleted?.Invoke();
RootVisualElement.RemoveFromClassList(USSEntry);
RootVisualElement.RemoveFromClassList(USSEntryAsync);
}
private void OnTransitionEnd<TChangeEvent>(TChangeEvent evt)
{
@@ -203,22 +209,15 @@ namespace BITKit.UX
{
return UniTask.CompletedTask;
}
void IEntryElement.Entered()
{
OnPanelEntry();
OnEntryCompleted?.Invoke();
RootVisualElement.RemoveFromClassList(USSEntry);
RootVisualElement.RemoveFromClassList(USSEntryAsync);
}
void IEntryElement.Exit()
public override void OnStateExit(IState old, IState newState)
{
OnPanelExit();
InputActionGroup.allowInput.RemoveElement(this);
OnExit?.Invoke();
}
async UniTask IEntryElement.ExitAsync()
public override async UniTask OnStateExitAsync(IState old, IState newState)
{
RootVisualElement?.RemoveFromClassList(USSEntered);
await UniTask.NextFrame();
@@ -244,9 +243,6 @@ namespace BITKit.UX
{
}
}
void IEntryElement.Exited()
{
RootVisualElement?.RemoveFromClassList(USSExit);
RootVisualElement?.RemoveFromClassList(USSExitAsync);
RootVisualElement?.AddToClassList(USSExited);
@@ -275,6 +271,7 @@ namespace BITKit.UX
public virtual void Dispose()
{
InputActionGroup.Dispose();
RootVisualElement?.RemoveFromHierarchy();
IsDisposed = true;
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit.StateMachine;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using UnityEngine;
@@ -67,43 +68,6 @@ namespace BITKit.UX
{ UXUtils.Inject(this,Panel.Root as VisualElement);
}
bool IEntryElement.IsEntered
{
get => Panel.IsEntered;
set => Panel.IsEntered = value;
}
void IEntryElement.Entry()
{
Panel.Entry();
}
UniTask IEntryElement.EntryAsync()
{
return Panel.EntryAsync();
}
void IEntryElement.Entered()
{
Panel.Entered();
}
void IEntryElement.Exit()
{
Panel.Exit();
}
UniTask IEntryElement.ExitAsync()
{
return Panel.ExitAsync();
}
void IEntryElement.Exited()
{
Panel.Exited();
}
bool IUXPanel.IsWindow => Panel.IsWindow;
string IUXPanel.Index => Panel.Index;
@@ -136,6 +100,58 @@ namespace BITKit.UX
{
Panel.OnTick(deltaTime);
}
public bool Enabled
{
get => Panel.Enabled;
set => Panel.Enabled = value;
}
public void Initialize()
{
Panel.Initialize();
}
public void OnStateEntry(IState old)
{
Panel.OnStateEntry(old);
}
public void OnStateUpdate(float deltaTime)
{
Panel.OnStateUpdate(deltaTime);
}
public void OnStateExit(IState old, IState newState)
{
Panel.OnStateExit(old, newState);
}
public int Identifier
{
get => Panel.Identifier;
set => Panel.Identifier = value;
}
public UniTask InitializeAsync()
{
return Panel.InitializeAsync();
}
public UniTask OnStateEntryAsync(IState old)
{
return Panel.OnStateEntryAsync(old);
}
public UniTask OnStateUpdateAsync(float deltaTime)
{
return Panel.OnStateUpdateAsync(deltaTime);
}
public UniTask OnStateExitAsync(IState old, IState newState)
{
return Panel.OnStateExitAsync(old, newState);
}
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Text;
using System.Threading;
using BITKit.Mod;
using BITKit.StateMachine;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Unity.Mathematics;
@@ -27,10 +28,9 @@ namespace BITKit.UX
_ticker = ticker;
_serviceProvider = serviceProvider;
_cancellationTokenSource = cancellationTokenSource;
_entryGroup.OnEntry += OnEntry;
_entryGroup.OnStateChanged += OnEntry;
_windowEntryGroup.OnEntry += OnWindowEntry;
_windowEntryGroup.OnExit += OnWindowExit;
_windowEntryGroup.OnStateChanged += OnWindowEntry;
_ticker.Add(OnTick);
}
@@ -41,15 +41,15 @@ namespace BITKit.UX
BITInputSystem.AllowInput.RemoveDisableElements(_windowEntryGroup);
}
private void OnWindowEntry(IUXPanel obj)
private void OnWindowEntry(IUXPanel prev, IUXPanel next)
{
BITAppForUnity.AllowCursor.SetElements(_windowEntryGroup, obj.AllowCursor);
if (obj.AllowInput is false)
BITInputSystem.AllowInput.AddDisableElements(_windowEntryGroup);
BITAppForUnity.AllowCursor.SetElements(_windowEntryGroup, next is { AllowCursor: true });
BITInputSystem.AllowInput.SetDisableElements(_windowEntryGroup, next is { AllowInput: false });
}
private readonly EntryGroup<IUXPanel> _entryGroup = new();
private readonly EntryGroup<IUXPanel> _windowEntryGroup = new();
private readonly AsyncStateMachine<IUXPanel> _entryGroup = new();
private readonly AsyncStateMachine<IUXPanel> _windowEntryGroup = new();
/// <summary>
/// 内部注册面板队列
/// </summary>
@@ -163,9 +163,9 @@ Object.Destroy(gameObject);
public void Return()
{
if(_windowEntryGroup.TryGetEntried(out _))
if (_windowEntryGroup.CurrentState is not null)
{
_windowEntryGroup.Entry(-1);
_windowEntryGroup.DisposeState();
return;
}
if (History.TryPop(out var returnPanel))
@@ -173,16 +173,15 @@ Object.Destroy(gameObject);
_returnBuffer.Release(returnPanel);
}
}
private bool _initialized;
private void OnEntry(IUXPanel obj)
private void OnEntry(IUXPanel prev,IUXPanel next)
{
OnPanelChanged?.Invoke(_currentPanel,obj);
_currentPanel = obj;
OnPanelChanged?.Invoke(_currentPanel,next);
_currentPanel = next;
}
private void OnTick(float delta)
{
@@ -192,20 +191,20 @@ Object.Destroy(gameObject);
while (_registryQueue.TryDequeue(out var result))
{
if (result is null) continue;
_entryGroup.list.Add(result);
_entryGroup.Register(result);
_panels.Set(result.Index, result);
}
while (_unRegistryQueue.TryDequeue(out var result))
{
if (result is null) continue;
_entryGroup.list.Remove(result);
_entryGroup.UnRegister(result);
_panels.Remove(result.Index);
}
if (_returnBuffer.TryGetRelease(out var returnPanel))
{
_entryGroup.Entry(x=>x.Index==returnPanel.Index);
_entryGroup.TransitionState(returnPanel);
BITAppForUnity.AllowCursor.SetElements(this, returnPanel.AllowCursor);
BITInputSystem.AllowInput.SetElements(this, returnPanel.AllowInput);
}
@@ -221,22 +220,23 @@ Object.Destroy(gameObject);
{
if (nextPanel.IsWindow)
{
_windowEntryGroup.Entry(nextPanel);
_windowEntryGroup.TransitionState(nextPanel);
return;
}
_windowEntryGroup.Entry(-1);
_windowEntryGroup.DisposeState();
History.Push(_currentPanel);
_entryGroup.Entry(x=>x.Index==nextPanel.Index);
_entryGroup.TransitionState(nextPanel);
BITAppForUnity.AllowCursor.SetElements(this, nextPanel.AllowCursor);
BITInputSystem.AllowInput.SetElements(this, nextPanel.AllowInput);
}
if (_entryGroup.TryGetEntried(out var currentPanel))
if (_entryGroup.CurrentState is {Enabled:true})
{
currentPanel.OnTick(Time.deltaTime);
_entryGroup.CurrentState.OnTick(delta);
}
if(_windowEntryGroup.TryGetEntried(out var windowPanel))
if (_windowEntryGroup.CurrentState is {Enabled:true})
{
windowPanel.OnTick(Time.deltaTime);
_windowEntryGroup.CurrentState.OnTick(delta);
}
}
catch (Exception e)
@@ -256,13 +256,8 @@ Object.Destroy(gameObject);
}
_ticker.Remove(OnTick);
await UniTask.SwitchToMainThread();
if (_currentPanel is not null)
{
// ReSharper disable once MethodHasAsyncOverload
_currentPanel.Exit();
await _currentPanel.ExitAsync();
_currentPanel.Exited();
}
_entryGroup.Dispose();
_windowEntryGroup.Dispose();
}
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
using Net.BITKit.Localization;
using Newtonsoft.Json;
using Unity.Mathematics;
using UnityEngine;
@@ -15,11 +16,13 @@ namespace BITKit.UX.Settings
public interface IUXSettings:IUXPanel{}
public class UXSettings<T,TValue> : UIToolkitSubPanel<T>,IUXSettings where T: IUXPanel
{
private readonly ILocalizationService _localizationService;
private readonly TValue _value;
private readonly IWrapper<TValue> _valueWrapper;
public UXSettings(IServiceProvider serviceProvider, IWrapper<TValue> valueWrapper) : base(serviceProvider)
public UXSettings(IServiceProvider serviceProvider, IWrapper<TValue> valueWrapper, ILocalizationService localizationService) : base(serviceProvider)
{
_valueWrapper = valueWrapper;
_localizationService = localizationService;
_value = _valueWrapper.Value;
}
@@ -36,80 +39,98 @@ namespace BITKit.UX.Settings
_settingsContainer.Clear();
foreach (var propertyInfo in typeof(TValue).GetProperties())
{
if(propertyInfo.SetMethod is null)continue;
var name = propertyInfo.GetDisplayName();
var value = propertyInfo.GetValue(_valueWrapper.Value);
switch (propertyInfo.PropertyType)
if (CreateVisualElement() is { } visualElement)
{
case var type when type == typeof(bool):
visualElement.viewDataKey = name;
visualElement.AddToClassList("localized");
}
VisualElement CreateVisualElement()
{
switch (propertyInfo.PropertyType)
{
var checkBox = _settingsContainer.Create<Toggle>();
checkBox.label = name;
checkBox.SetValueWithoutNotify(value.As<bool>());
checkBox.RegisterValueChangedCallback(x =>
case var type when type == typeof(bool):
{
propertyInfo.SetValue(_value,x.newValue);
Save();
});
}
break;
case var type when type == typeof(float):
{
var slider = _settingsContainer.Create<Slider>();
slider.label = name;
slider.showInputField = true;
slider.showMixedValue = true;
slider.lowValue = 1;
slider.highValue = 100;
slider.SetValueWithoutNotify(value.As<float>());
slider.RegisterValueChangedCallback(x =>
var checkBox = _settingsContainer.Create<Toggle>();
checkBox.label = _localizationService.GetLocalizedString(name);
checkBox.SetValueWithoutNotify(value.As<bool>());
checkBox.RegisterValueChangedCallback(x =>
{
propertyInfo.SetValue(_value, x.newValue);
Save();
});
return checkBox;
}
break;
case var type when type == typeof(float):
{
propertyInfo.SetValue(_value,x.newValue);
Save();
});
}
break;
case var type when type == typeof(int):
{
var slider = _settingsContainer.Create<SliderInt>();
slider.label = name;
slider.showInputField = true;
slider.showMixedValue = true;
slider.lowValue = 1;
slider.highValue = 100;
slider.SetValueWithoutNotify(value.As<int>());
slider.RegisterValueChangedCallback(x =>
var slider = _settingsContainer.Create<Slider>();
slider.label = _localizationService.GetLocalizedString(name);
slider.showInputField = true;
slider.showMixedValue = true;
slider.lowValue = 1;
slider.highValue = 100;
slider.SetValueWithoutNotify(value.As<float>());
slider.RegisterValueChangedCallback(x =>
{
propertyInfo.SetValue(_value, x.newValue);
Save();
});
return slider;
}
break;
case var type when type == typeof(int):
{
propertyInfo.SetValue(_value,x.newValue);
Save();
});
}
break;
case var type when type == typeof(string):
{
var textField = _settingsContainer.Create<TextField>();
textField.label = name;
textField.SetValueWithoutNotify(value.As<string>());
textField.RegisterValueChangedCallback(x =>
var slider = _settingsContainer.Create<SliderInt>();
slider.label = _localizationService.GetLocalizedString(name);
slider.showInputField = true;
slider.showMixedValue = true;
slider.lowValue = 1;
slider.highValue = 100;
slider.SetValueWithoutNotify(value.As<int>());
slider.RegisterValueChangedCallback(x =>
{
propertyInfo.SetValue(_value, x.newValue);
Save();
});
return slider;
}
break;
case var type when type == typeof(string):
{
propertyInfo.SetValue(_value,x.newValue);
Save();
});
}
break;
case var type when type.IsEnum:
{
var enumField = _settingsContainer.Create<EnumField>();
enumField.label = name;
enumField.Init(value as Enum);
enumField.SetValueWithoutNotify(value as Enum);
enumField.RegisterValueChangedCallback(x =>
var textField = _settingsContainer.Create<TextField>();
textField.label = _localizationService.GetLocalizedString(name);
textField.SetValueWithoutNotify(value.As<string>());
textField.RegisterValueChangedCallback(x =>
{
propertyInfo.SetValue(_value, x.newValue);
Save();
});
return textField;
}
break;
case var type when type.IsEnum:
{
propertyInfo.SetValue(_value,x.newValue);
Save();
});
var enumField = _settingsContainer.Create<EnumField>();
enumField.label = _localizationService.GetLocalizedString(name);
enumField.Init(value as Enum);
enumField.SetValueWithoutNotify(value as Enum);
enumField.RegisterValueChangedCallback(x =>
{
propertyInfo.SetValue(_value, x.newValue);
Save();
});
return enumField;
}
break;
}
break;
return null;
}
}
}