From faf72b05e9b19274a2b37b120252f393426c28e6 Mon Sep 17 00:00:00 2001 From: CortexCore <2630229280@qq.com> Date: Fri, 8 Nov 2024 17:28:07 +0800 Subject: [PATCH] 1 --- Src/Core/Item/ItemContainer.cs | 20 +-- Src/Core/Item/ItemEx.cs | 13 ++ .../Item/ItemEx.cs.meta} | 2 +- Src/Core/StateMachine/AsyncStateMachine.cs | 147 +++++++++--------- Src/Core/StateMachine/IStateMachine.cs | 4 +- Src/Core/Utility/ValidHandle.cs | 37 ++++- .../Scripts/Mono/StateBasedMonoBehaviour.cs | 74 --------- .../Mono/StateBasedMonoBehaviour.cs.meta | 11 -- .../Scripts/Reference/DictionaryReference.cs | 10 ++ Src/Unity/Scripts/Sensor/BITKit.Sensor.asmdef | 4 +- .../Sensor/Editor/SensorEditorWIndow.cs | 29 ---- .../Sensor/Editor/SensorEditorWIndow.cs.meta | 11 -- .../Scripts/StateMachine/MonoStateMachine.cs | 130 ---------------- .../UX/Service/UI Toolkit/UIToolKitPanel.cs | 83 +++++----- 14 files changed, 196 insertions(+), 379 deletions(-) create mode 100644 Src/Core/Item/ItemEx.cs rename Src/{Unity/Scripts/StateMachine/MonoStateMachine.cs.meta => Core/Item/ItemEx.cs.meta} (83%) delete mode 100644 Src/Unity/Scripts/Mono/StateBasedMonoBehaviour.cs delete mode 100644 Src/Unity/Scripts/Mono/StateBasedMonoBehaviour.cs.meta delete mode 100644 Src/Unity/Scripts/Sensor/Editor/SensorEditorWIndow.cs delete mode 100644 Src/Unity/Scripts/Sensor/Editor/SensorEditorWIndow.cs.meta delete mode 100644 Src/Unity/Scripts/StateMachine/MonoStateMachine.cs diff --git a/Src/Core/Item/ItemContainer.cs b/Src/Core/Item/ItemContainer.cs index c86fe31..d9c36e0 100644 --- a/Src/Core/Item/ItemContainer.cs +++ b/Src/Core/Item/ItemContainer.cs @@ -7,6 +7,7 @@ using Microsoft.SqlServer.Server; namespace BITKit { + /// /// 支持二进制化的的物品容器,适用于网络化物品容器 /// 支持属性 @@ -74,7 +75,7 @@ namespace BITKit public class RuntimeItemContainer : IRuntimeItemContainer { - private readonly ConcurrentDictionary _items = new(); + public readonly ConcurrentDictionary Items = new(); public void Read(BinaryReader r) { throw new NotImplementedException(); @@ -86,7 +87,7 @@ namespace BITKit } public int Id { get; set; } - public IRuntimeItem[] GetItems()=>_items.Values.ToArray(); + public IRuntimeItem[] GetItems()=>Items.Values.ToArray(); public bool Add(IRuntimeItem item) { @@ -98,17 +99,17 @@ namespace BITKit } } - _items.Set(item.Id,item); - + Items.Set(item.Id, item); + OnAdd?.Invoke(item); //BIT4Log.Log($"添加了了:{item.Id}"); - + return true; } public bool Remove(int id) { - if (_items.TryGetValue(id, out var item) is false) return false; + if (Items.TryGetValue(id, out var item) is false) return false; foreach (var func in RemoveFactory.CastAsFunc()) { @@ -117,7 +118,7 @@ namespace BITKit return false; } } - _items.TryRemove(item.Id); + Items.TryRemove(item.Id); OnRemove?.Invoke(item); //BIT4Log.Log($"移除了:{id}"); return true; @@ -125,8 +126,7 @@ namespace BITKit public bool Drop(int id) { - - if (_items.TryGetValue(id, out var item) is false) return false; + if (Items.TryGetValue(id, out var item) is false) return false; foreach (var func in DropFactory.CastAsFunc()) { if (func.Invoke(item) is false) @@ -134,7 +134,7 @@ namespace BITKit return false; } } - _items.TryRemove(item.Id); + Items.TryRemove(item.Id); OnDrop?.Invoke(item); //BIT4Log.Log($"丢下了:{id}"); return true; diff --git a/Src/Core/Item/ItemEx.cs b/Src/Core/Item/ItemEx.cs new file mode 100644 index 0000000..9f6c6db --- /dev/null +++ b/Src/Core/Item/ItemEx.cs @@ -0,0 +1,13 @@ +using System.Collections; +using System.Collections.Generic; + +namespace BITKit +{ + /// + /// 玩家向背包添加物品,但是被其他逻辑处理 + /// + public sealed class PlayerInventoryAddItemOverridden : InGameException + { + + } +} diff --git a/Src/Unity/Scripts/StateMachine/MonoStateMachine.cs.meta b/Src/Core/Item/ItemEx.cs.meta similarity index 83% rename from Src/Unity/Scripts/StateMachine/MonoStateMachine.cs.meta rename to Src/Core/Item/ItemEx.cs.meta index 307e053..203a9fa 100644 --- a/Src/Unity/Scripts/StateMachine/MonoStateMachine.cs.meta +++ b/Src/Core/Item/ItemEx.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 2abc8d2959200da4294a6177cc85c5ca +guid: ebeadaca27f99904a87e52f3f63042f9 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Src/Core/StateMachine/AsyncStateMachine.cs b/Src/Core/StateMachine/AsyncStateMachine.cs index 61d7f02..62e604b 100644 --- a/Src/Core/StateMachine/AsyncStateMachine.cs +++ b/Src/Core/StateMachine/AsyncStateMachine.cs @@ -1,8 +1,7 @@ using System; -using System.Collections; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; -using System.Threading.Tasks; using Cysharp.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; @@ -16,111 +15,113 @@ namespace BITKit.StateMachine { private enum AsyncState { - None, Initializing, InEntering, InExiting, - InUpdating, } - private readonly IServiceCollection _serviceCollection; - public AsyncStateMachine(IServiceCollection serviceCollection) + + private readonly IServiceProvider _serviceProvider; + public AsyncStateMachine(IServiceProvider serviceProvider) { - _serviceCollection = serviceCollection; + _serviceProvider = serviceProvider; + } + public AsyncStateMachine() + { + _serviceProvider = new ServiceCollection().BuildServiceProvider(); } public bool Enabled { get; set; } public T CurrentState { get; set; } private T _nextState; - public event Action OnStateChanging; + public event Action OnStateChanging; public event Action OnStateChanged; public event Action OnStateRegistered; public event Action OnStateUnRegistered; public IDictionary StateDictionary { get; } = new Dictionary(); - private AsyncState _currentState; - private Task _currentTask; private readonly CancellationTokenSource _cancellationTokenSource=new(); - public async void Initialize() + private readonly ValidHandle _isBusy=new(); + private readonly ConcurrentQueue<(UniTask task,AsyncState state,T value)> _taskQueue=new(); + public void Initialize() { - if (_currentState is not AsyncState.None) + foreach (var (_,value) in StateDictionary) { - throw new InvalidOperationException("状态机可能已初始化"); + _taskQueue.Enqueue(new (value.InitializeAsync(),AsyncState.Initializing,value)); } - - _currentState = AsyncState.Initializing; - foreach (var (type,value) in StateDictionary) - { - await value.InitializeAsync(); - } - foreach (var (type,value) in StateDictionary) - { - value.Initialize(); - } - _currentState = AsyncState.None; } - public void UpdateState(float deltaTime) + public async void UpdateState(float deltaTime) { - if(_cancellationTokenSource.IsCancellationRequested)return; - switch (_currentState) + if(Enabled is false)return; + CurrentState?.OnStateUpdate(deltaTime); + if(_isBusy)return; + using var _ = _isBusy.GetHandle(); + if (!_taskQueue.TryDequeue(out var task)) return; + await task.task; + if(_cancellationTokenSource.IsCancellationRequested|| Enabled is false)return; + switch (task.state) { - case AsyncState.None: - if (_nextState is not null) - { - _currentState = AsyncState.InEntering; - _currentTask = CurrentState.OnStateEntryAsync(CurrentState).AsTask(); - - OnStateChanging?.Invoke(CurrentState,_nextState); - return; - } - _currentState = AsyncState.InUpdating; - if (CurrentState is not null) - { - _currentTask = CurrentState.OnStateUpdateAsync(deltaTime).AsTask(); - } - break; - case AsyncState.InExiting: - if (_currentTask.IsCompleted) - { - _currentState =_nextState is not null ? AsyncState.InEntering : AsyncState.None; - CurrentState.OnStateExit(CurrentState,_nextState); - if (_nextState is not null) - { - _currentTask = _nextState.OnStateEntryAsync(_nextState).AsTask(); - } - } + case AsyncState.Initializing: + task.value.Initialize(); + OnStateRegistered?.Invoke(task.value); break; case AsyncState.InEntering: - if (_currentTask.IsCompleted) - { - _currentState = AsyncState.None; - _nextState.OnStateEntry(CurrentState); - - OnStateChanged?.Invoke(CurrentState,_nextState); - - CurrentState = _nextState; - _nextState = default; - } + _nextState.OnStateEntry(_nextState); + OnStateChanged?.Invoke(CurrentState,_nextState); + CurrentState = _nextState; break; - case AsyncState.InUpdating: - if (_currentTask.IsCompleted) - { - _currentTask = null; - _currentState = AsyncState.None; - } + case AsyncState.InExiting: + CurrentState?.OnStateExit(CurrentState,_nextState); break; } + + if(Enabled is false)return; + if (CurrentState is not null) + { + await CurrentState.OnStateUpdateAsync(deltaTime); + } + } public void DisposeState() { - + _taskQueue.Clear(); + if (CurrentState is not null) + { + _taskQueue.Enqueue(new(CurrentState.OnStateExitAsync(CurrentState,null),AsyncState.InExiting,CurrentState)); + } } - - public void TransitionState() where State : T + + public T TransitionState() where TState : T { - throw new InvalidOperationException("动态异步状态机不支持,请使用TransitionAsync"); + T nextState; + foreach (var (type, value) in StateDictionary) + { + if (!type.IsAssignableFrom(typeof(TState))) continue; + nextState = value; + + TransitionState(nextState); + return nextState; + } + nextState = _serviceProvider.GetRequiredService(); + _taskQueue.Enqueue(new(nextState.InitializeAsync(),AsyncState.Initializing,nextState)); + TransitionState(nextState); + + return nextState; } - public void TransitionState(T state) + public T TransitionState(T nextState) { + if(Equals(CurrentState,nextState))return nextState; + OnStateChanging?.Invoke(CurrentState,nextState); + if (CurrentState is not null) + { + _taskQueue.Enqueue(new(CurrentState.OnStateExitAsync(CurrentState,nextState),AsyncState.InExiting,CurrentState)); + } + + if (nextState is not null) + { + _taskQueue.Enqueue(new(nextState.OnStateEntryAsync(nextState),AsyncState.InEntering,nextState)); + } + + return nextState; } public void Dispose() diff --git a/Src/Core/StateMachine/IStateMachine.cs b/Src/Core/StateMachine/IStateMachine.cs index df4849e..aabe43e 100644 --- a/Src/Core/StateMachine/IStateMachine.cs +++ b/Src/Core/StateMachine/IStateMachine.cs @@ -56,8 +56,8 @@ namespace BITKit.StateMachine void Initialize(); void UpdateState(float deltaTime); void DisposeState(); - void TransitionState() where State : T; - void TransitionState(T state); + T TransitionState() where TState : T; + T TransitionState(T state); void Register(T newState) => throw new NotImplementedException("未实现的接口"); void UnRegister(T newState) => throw new NotImplementedException("未实现的接口"); void InvokeOnStateRegistered(T state){} diff --git a/Src/Core/Utility/ValidHandle.cs b/Src/Core/Utility/ValidHandle.cs index e7f942d..3588dce 100644 --- a/Src/Core/Utility/ValidHandle.cs +++ b/Src/Core/Utility/ValidHandle.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Cysharp.Threading.Tasks; namespace BITKit { @@ -22,6 +23,23 @@ namespace BITKit [CustomType(typeof(IValidHandle))] public sealed class ValidHandle: IValidHandle { + public class MyHandle:IDisposable + { + private readonly ValidHandle _validHandle; + + public MyHandle(ValidHandle validHandle) + { + _validHandle = validHandle; + _validHandle.AddElement(this); + } + public void Dispose() + { + _validHandle.RemoveElement(this); + } + } + + public MyHandle GetHandle() => new MyHandle(this); + public override string ToString() { return $"Allow:{enableHandle}\nElements:{string.Join("\n",objs)}\nDisableElements:{string.Join("\n",disableObjs)}"; @@ -51,6 +69,7 @@ namespace BITKit public readonly List disableObjs = new List(); private bool tempEnable; private Action EventOnEnableChanged; + private readonly List _completionSources = new(); public void AddElement(object obj) @@ -73,7 +92,14 @@ namespace BITKit { enableHandle = tempEnable; if (EventOnEnableChanged is not null) + { EventOnEnableChanged.Invoke(enableHandle); + } + if (tempEnable) return; + foreach (var cs in _completionSources) + { + cs.TrySetResult(); + } } } public void RemoveElement(object obj) @@ -164,7 +190,16 @@ namespace BITKit EventOnEnableChanged -= action; } } - + public UniTask.Awaiter GetAwaiter() + { + if (Allow is false) + { + return UniTask.CompletedTask.GetAwaiter(); + } + var cs = new UniTaskCompletionSource(); + _completionSources.Add(cs); + return cs.Task.GetAwaiter(); + } public void Clear() { objs.Clear(); diff --git a/Src/Unity/Scripts/Mono/StateBasedMonoBehaviour.cs b/Src/Unity/Scripts/Mono/StateBasedMonoBehaviour.cs deleted file mode 100644 index dbdc423..0000000 --- a/Src/Unity/Scripts/Mono/StateBasedMonoBehaviour.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using BITKit.StateMachine; -using UnityEngine; - -namespace BITKit -{ - public abstract class StateBasedMonoBehaviour : MonoBehaviour, IStateMachine where T : IState - { - [SerializeField] private MonoStateMachine stateMachine; - protected Transform Transform => _transform ? _transform : _transform = transform; - private Transform _transform; - public bool Enabled - { - get => stateMachine.Enabled; - set => stateMachine.Enabled = value; - } - - public T CurrentState - { - get => stateMachine.CurrentState; - set => stateMachine.CurrentState = value; - } - - public event Action OnStateChanged - { - add => stateMachine.OnStateChanged += value; - remove => stateMachine.OnStateChanged -= value; - } - - public event Action OnStateRegistered; - public event Action OnStateUnRegistered; - - public IDictionary StateDictionary => stateMachine.StateDictionary; - - public virtual void Initialize() - { - stateMachine.Initialize(); - } - - public virtual void UpdateState(float deltaTime) - { - stateMachine.UpdateState(deltaTime); - } - - public virtual void DisposeState() - { - stateMachine.DisposeState(); - } - - public virtual void TransitionState() where State : T - { - stateMachine.TransitionState(); - } - - public virtual void TransitionState(T state) - { - stateMachine.TransitionState(state); - } - - public virtual void Register(T newState) => StateMachineUtils.Register(this,newState); - public virtual void UnRegister(T newState)=>StateMachineUtils.UnRegister(this,newState); - void IStateMachine.InvokeOnStateRegistered(T state) - { - OnStateRegistered?.Invoke(state); - } - - void IStateMachine.InvokeOnStateUnRegistered(T state) - { - OnStateUnRegistered?.Invoke(state); - } - } -} diff --git a/Src/Unity/Scripts/Mono/StateBasedMonoBehaviour.cs.meta b/Src/Unity/Scripts/Mono/StateBasedMonoBehaviour.cs.meta deleted file mode 100644 index b21da8b..0000000 --- a/Src/Unity/Scripts/Mono/StateBasedMonoBehaviour.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9c3c4b253dc3f5144abdd6fa34699cdd -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Src/Unity/Scripts/Reference/DictionaryReference.cs b/Src/Unity/Scripts/Reference/DictionaryReference.cs index 77e6988..cb34c69 100644 --- a/Src/Unity/Scripts/Reference/DictionaryReference.cs +++ b/Src/Unity/Scripts/Reference/DictionaryReference.cs @@ -2,11 +2,21 @@ using System; using System.Collections; using System.Collections.Generic; using System.Linq; +using BITKit.Mod; +using Cysharp.Threading.Tasks; using UnityEngine; using UnityEngine.UIElements; namespace BITKit { + public static class ModServiceDictionaryReferenceExtensions + { + public static UniTask LoadAssets(int id) where T :class + { + var path = DictionaryReferenceScriptableObject.Dictionary[id]; + return ModService.LoadAsset(path); + } + } public sealed class DictionaryReferenceConfigAttribute : System.Attribute { public readonly int index; diff --git a/Src/Unity/Scripts/Sensor/BITKit.Sensor.asmdef b/Src/Unity/Scripts/Sensor/BITKit.Sensor.asmdef index 7ec7ae6..574411c 100644 --- a/Src/Unity/Scripts/Sensor/BITKit.Sensor.asmdef +++ b/Src/Unity/Scripts/Sensor/BITKit.Sensor.asmdef @@ -18,7 +18,9 @@ "overrideReferences": false, "precompiledReferences": [], "autoReferenced": true, - "defineConstraints": [], + "defineConstraints": [ + "deprecated" + ], "versionDefines": [], "noEngineReferences": false } \ No newline at end of file diff --git a/Src/Unity/Scripts/Sensor/Editor/SensorEditorWIndow.cs b/Src/Unity/Scripts/Sensor/Editor/SensorEditorWIndow.cs deleted file mode 100644 index b92ebf8..0000000 --- a/Src/Unity/Scripts/Sensor/Editor/SensorEditorWIndow.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using UnityEditor; -using UnityEngine; -using UnityEngine.UIElements; - -namespace BITKit.Sensors -{ - public class SensorEditorWindow : EditorWindow - { - private Toggle _toggle; - [MenuItem("Tools/Sensor/EditorWindow")] - public static void ShowWindow() - { - GetWindow("SensorEditorWindow"); - } - private void CreateGUI() - { - _toggle = rootVisualElement.Create("Enable Sensor"); - _toggle.label = "Enable Sensor"; - _toggle.value = SensorGlobalSettings.Enabled; - } - private void Update() - { - SensorGlobalSettings.Enabled = _toggle.value; - } - } -} diff --git a/Src/Unity/Scripts/Sensor/Editor/SensorEditorWIndow.cs.meta b/Src/Unity/Scripts/Sensor/Editor/SensorEditorWIndow.cs.meta deleted file mode 100644 index 456b95d..0000000 --- a/Src/Unity/Scripts/Sensor/Editor/SensorEditorWIndow.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: b0ad25384d3d5d74a95d99ceed99ec8d -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Src/Unity/Scripts/StateMachine/MonoStateMachine.cs b/Src/Unity/Scripts/StateMachine/MonoStateMachine.cs deleted file mode 100644 index 7897578..0000000 --- a/Src/Unity/Scripts/StateMachine/MonoStateMachine.cs +++ /dev/null @@ -1,130 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -#if UNITY_EDITOR -using UnityEditor; -using UnityEditor.UIElements; -#endif -using UnityEngine; -using UnityEngine.UIElements; - -namespace BITKit.StateMachine -{ - [Serializable] - public class MonoStateMachine : IStateMachine where TState : IState - { - public bool Enabled - { - get => _enabled; - set - { - _enabled = value; - if (CurrentState is null) return; - if (value) - { - CurrentState.OnStateExit(null,null); - } - else - { - CurrentState.OnStateEntry(null); - } - } - } - public TState CurrentState { get; set; } - public event Action OnStateChanged; - public event Action OnStateRegistered; - public event Action OnStateUnRegistered; - - [SerializeReference, SubclassSelector] public List states = new(); - [SerializeField,ReadOnly] private string _currentStateName; - [SerializeField] private bool debug; - [SerializeField] private bool transitionOnNextFrame; - public IDictionary StateDictionary { get; } = new Dictionary(); - private bool _enabled = true; - private readonly DoubleBuffer _nextState = new(); - protected bool cancelUpdateStateThisFrame; - public void Initialize() - { - foreach (var state in states) - { - //state.TransitionState = InternalTransitionState; - state.Initialize(); - StateDictionary.Add(state.GetType(), state); - } - // if (states.Count > 0) - // { - // TransitionState(states[0]); - // } - } - public void UpdateState(float deltaTime) - { - if(transitionOnNextFrame && _nextState.TryGetRelease(out var nextState)) - TransitionStateInternal(nextState); - if (Enabled) - { - if (cancelUpdateStateThisFrame is false) - { - CurrentState?.OnStateUpdate(deltaTime); - } - cancelUpdateStateThisFrame = false; - } - - } - - public void DisposeState() - { - } - public void TransitionState() where State : TState - { - var nextState = StateDictionary.GetOrCreate(typeof(State)); - TransitionState(nextState); - } - public void TransitionState(TState newState) - { - if(transitionOnNextFrame) - _nextState.Release(newState); - else - { - TransitionStateInternal(newState); - cancelUpdateStateThisFrame = true; - } - } - private void TransitionStateInternal(TState newState) - { - if (Equals(newState,CurrentState)) return; - - var oldState = CurrentState; - if (oldState is not null) - { - oldState.OnStateExit(oldState, newState); - oldState.Enabled = false; - } - - _currentStateName = newState is not null ? newState.GetType().Name : "null"; - if (debug) - { - BIT4Log.Log>($"TransitionState from {_currentStateName} to {_currentStateName}"); - } - CurrentState = newState; - - if (newState is not null) - { - newState.Enabled = true; - newState.OnStateEntry(oldState); - } - OnStateChanged?.Invoke(oldState, newState); - } - public virtual void Register(TState newState)=>StateMachineUtils.Register(this,newState); - public virtual void UnRegister(TState newState)=>StateMachineUtils.UnRegister(this,newState); - public void InvokeOnStateRegistered(TState state) - { - OnStateRegistered?.Invoke(state); - } - - public void InvokeOnStateUnRegistered(TState state) - { - OnStateUnRegistered?.Invoke(state); - } - } - -} \ No newline at end of file diff --git a/Src/Unity/Scripts/UX/Service/UI Toolkit/UIToolKitPanel.cs b/Src/Unity/Scripts/UX/Service/UI Toolkit/UIToolKitPanel.cs index b4bf8e3..6355950 100644 --- a/Src/Unity/Scripts/UX/Service/UI Toolkit/UIToolKitPanel.cs +++ b/Src/Unity/Scripts/UX/Service/UI Toolkit/UIToolKitPanel.cs @@ -28,10 +28,55 @@ namespace BITKit.UX protected abstract string DocumentPath { get; } public VisualElement RootVisualElement { get; set; } protected VisualTreeAsset VisualTreeAsset { get; private set; } + private readonly ValidHandle _isBusy = new(); protected UIToolKitPanel(IUXService uxService) { UXService = uxService; uxService.Register(this); + InitializeAsync().Forget(); + } + + private async UniTask InitializeAsync() + { + await _isBusy; + using var b = _isBusy.GetHandle(); + if (RootVisualElement is null) + { + VisualTreeAsset = await ModService.LoadAsset(DocumentPath); + + RootVisualElement = UXService.Root.As().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; + + var invisible = RootVisualElement.Create(); + 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(x => { OnReturn(); }); + invisible.pickingMode = PickingMode.Position; + } + + if (IsWindow) + { + invisible.style.backgroundColor = new Color(0, 0, 0, 0.9f); + } + + UXUtils.Inject(this); + + RootVisualElement.SetActive(false); + } } protected virtual Optional EntryDuration { get; }= new(); @@ -62,42 +107,8 @@ namespace BITKit.UX OnEntry?.Invoke(); } async UniTask IEntryElement.EntryAsync() - { - if (RootVisualElement is null) - { - VisualTreeAsset = await ModService.LoadAsset(DocumentPath); - - RootVisualElement = UXService.Root.As().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; - - var invisible = RootVisualElement.Create(); - 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(x => { OnReturn(); }); - invisible.pickingMode = PickingMode.Position; - } - - if (IsWindow) - { - invisible.style.backgroundColor = new Color(0, 0, 0, 0.9f); - } - - UXUtils.Inject(this); - } + { + await InitializeAsync(); RootVisualElement.SetActive(true); RootVisualElement.AddToClassList(USSEntry);