1
This commit is contained in:
@@ -13,25 +13,82 @@ using System.Text;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using BITKit.Mod;
|
||||
using UnityEngine.InputSystem.Interactions;
|
||||
using Label = UnityEngine.UIElements.Label;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
// ReSharper disable PossibleMultipleEnumeration
|
||||
|
||||
namespace BITKit.Console
|
||||
{
|
||||
public class BITConsole : MonoBehaviour
|
||||
public class UXConsole:IDisposable
|
||||
{
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
private static void Reload()
|
||||
{
|
||||
Application.logMessageReceivedThreaded += EnqueueLog;
|
||||
}
|
||||
[BITCommand]
|
||||
// ReSharper disable once UnusedMember.Global
|
||||
public static void Console_Exception_Print_StackTrace(int allow)
|
||||
{
|
||||
exceptionPrintStackTrace = allow is 1;
|
||||
_exceptionPrintStackTrace = allow is 1;
|
||||
}
|
||||
private static bool exceptionPrintStackTrace = false;
|
||||
|
||||
private readonly IMainTicker _ticker;
|
||||
public UXConsole(IMainTicker ticker)
|
||||
{
|
||||
_ticker = ticker;
|
||||
_singleton = this;
|
||||
|
||||
_ticker.Add(OnTick);
|
||||
|
||||
InitializeAsync();
|
||||
|
||||
Application.logMessageReceivedThreaded += EnqueueLog;
|
||||
}
|
||||
|
||||
private async void InitializeAsync()
|
||||
{
|
||||
var go = new GameObject("UXConsole");
|
||||
Object.DontDestroyOnLoad(go);
|
||||
var document = go.AddComponent<UIDocument>();
|
||||
document.sortingOrder = 1;
|
||||
try
|
||||
{
|
||||
var panelSettings =await ModService.LoadAsset<PanelSettings>("ux_panel_settings");
|
||||
document.panelSettings = panelSettings;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.Warning<UXService>("未找到ux_panel_settings");
|
||||
throw;
|
||||
}
|
||||
document.visualTreeAsset = await ModService.LoadAsset<VisualTreeAsset>("ux_console");
|
||||
|
||||
_rootVisualElement = document.rootVisualElement;
|
||||
|
||||
UXUtils.Inject(this,_rootVisualElement);
|
||||
|
||||
_commandSelector = new()
|
||||
{
|
||||
Container = _commandContainer,
|
||||
};
|
||||
_commandSelector.OnSelected += x =>
|
||||
{
|
||||
_textField.SetValueWithoutNotify(x.Name);
|
||||
_textField.Blur();
|
||||
_textField.Focus();
|
||||
};
|
||||
_singleton = this;
|
||||
|
||||
_textField.RegisterValueChangedCallback(OnTextFieldValueChanged);
|
||||
_textField.RegisterCallback<KeyDownEvent>(OnKeyDown);
|
||||
|
||||
_text.text = string.Empty;
|
||||
|
||||
Toggle(false);
|
||||
}
|
||||
|
||||
private VisualElement _rootVisualElement;
|
||||
|
||||
private static bool _exceptionPrintStackTrace;
|
||||
private class CommandSelector
|
||||
{
|
||||
public VisualElement Container { get; set; }
|
||||
@@ -100,111 +157,58 @@ namespace BITKit.Console
|
||||
public static async void Clear()
|
||||
{
|
||||
await BITApp.SwitchToMainThread();
|
||||
singleton.outputString.Clear();
|
||||
singleton.text.text = string.Empty;
|
||||
_singleton._outputString.Clear();
|
||||
_singleton._text.text = string.Empty;
|
||||
}
|
||||
private static BITConsole singleton;
|
||||
[SerializeField] private UIDocument document;
|
||||
[SerializeReference] private InputActionReference toggleAction;
|
||||
private static UXConsole _singleton;
|
||||
|
||||
private static ConcurrentQueue<(string condition, string stackTrace, LogType type)> logQueue = new();
|
||||
private static readonly ConcurrentQueue<(string condition, string stackTrace, LogType type)> LOGQueue = new();
|
||||
private static void EnqueueLog(string condition, string stackTrace, LogType type)
|
||||
{
|
||||
logQueue.Enqueue((condition, stackTrace, type));
|
||||
LOGQueue.Enqueue((condition, stackTrace, type));
|
||||
}
|
||||
|
||||
private readonly InputActionGroup _inputActionGroup=new()
|
||||
{
|
||||
allowGlobalActivation = false
|
||||
};
|
||||
public int logLineLimit = 64;
|
||||
private const int LOGLineLimit = 64;
|
||||
|
||||
[UXBindPath("commands-container")]
|
||||
private VisualElement commandContainer;
|
||||
private VisualElement _commandContainer;
|
||||
[UXBindPath("TextField")]
|
||||
private TextField textField;
|
||||
private TextField _textField;
|
||||
[UXBindPath("Text")]
|
||||
private Label text;
|
||||
private Label _text;
|
||||
[UXBindPath( "context-scrollview")]
|
||||
private ScrollView scrollView;
|
||||
private ScrollView _scrollView;
|
||||
private bool _isRunning;
|
||||
private List<string> outputString = new();
|
||||
private List<string> _outputString = new();
|
||||
|
||||
private CommandSelector _commandSelector;
|
||||
private void Start()
|
||||
{
|
||||
UXUtils.Inject(this);
|
||||
|
||||
_commandSelector = new()
|
||||
{
|
||||
Container = commandContainer,
|
||||
};
|
||||
_commandSelector.OnSelected += x =>
|
||||
{
|
||||
textField.SetValueWithoutNotify(x.Name);
|
||||
textField.Blur();
|
||||
textField.Focus();
|
||||
};
|
||||
|
||||
|
||||
singleton = this;
|
||||
|
||||
|
||||
|
||||
textField.RegisterValueChangedCallback(OnTextFieldValueChanged);
|
||||
textField.RegisterCallback<KeyDownEvent>(OnKeyDown);
|
||||
|
||||
|
||||
text.text = string.Empty;
|
||||
|
||||
|
||||
_inputActionGroup.RegisterCallback(toggleAction,Toggle);
|
||||
//_inputActionGroup.RegisterCallback(nextOrPreviousAction, OnNextCommand);
|
||||
|
||||
_inputActionGroup.allowInput.AddElement(this);
|
||||
|
||||
Toggle(false);
|
||||
|
||||
BIT4Log.OnNextLine += OnNextLine;
|
||||
|
||||
destroyCancellationToken.Register(() =>
|
||||
{
|
||||
BIT4Log.OnNextLine -= OnNextLine;
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void OnNextLine()
|
||||
{
|
||||
if (outputString.Count is not 0 && outputString.Last() != string.Empty)
|
||||
outputString.Add(string.Empty);
|
||||
if (_outputString.Count is not 0 && _outputString.Last() != string.Empty)
|
||||
_outputString.Add(string.Empty);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_inputActionGroup.allowInput.RemoveElement(this);
|
||||
}
|
||||
public async void Toggle(bool active)
|
||||
private void Toggle(bool active)
|
||||
{
|
||||
_commandSelector.SetMethods(null);
|
||||
|
||||
document.rootVisualElement.SetActive(active);
|
||||
_rootVisualElement.SetActive(active);
|
||||
_isRunning = active;
|
||||
|
||||
BITAppForUnity.AllowCursor.SetElements(this,active);
|
||||
BITInputSystem.AllowInput.SetDisableElements(this,active);
|
||||
|
||||
await UniTask.WaitForEndOfFrame(this);
|
||||
if (active)
|
||||
{
|
||||
textField.SetValueWithoutNotify(string.Empty);
|
||||
_textField.SetValueWithoutNotify(string.Empty);
|
||||
|
||||
textField.Focus();
|
||||
_textField.Focus();
|
||||
|
||||
scrollView.ScrollToBottom();
|
||||
_scrollView.ScrollToBottom();
|
||||
}
|
||||
else
|
||||
{
|
||||
text.Blur();
|
||||
_text.Blur();
|
||||
}
|
||||
}
|
||||
private void OnTextFieldValueChanged(ChangeEvent<string> callback)
|
||||
@@ -215,11 +219,11 @@ namespace BITKit.Console
|
||||
|
||||
_commandSelector.SetMethods(commands);
|
||||
|
||||
commandContainer.SetActive(commands.Length is not 0);
|
||||
_commandContainer.SetActive(commands.Length is not 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
commandContainer.SetActive(false);
|
||||
_commandContainer.SetActive(false);
|
||||
|
||||
_commandSelector.SetMethods(null);
|
||||
}
|
||||
@@ -230,20 +234,17 @@ namespace BITKit.Console
|
||||
switch (keyDownEvent.keyCode)
|
||||
{
|
||||
case KeyCode.Return:
|
||||
var cmd = textField.text;
|
||||
var cmd = _textField.text;
|
||||
|
||||
LogCallback($">{cmd}", string.Empty, LogType.Log);
|
||||
|
||||
textField.SetValueWithoutNotify(string.Empty);
|
||||
_textField.SetValueWithoutNotify(string.Empty);
|
||||
|
||||
await UniTask.NextFrame();
|
||||
|
||||
if (destroyCancellationToken.IsCancellationRequested) return;
|
||||
|
||||
_textField.Blur();
|
||||
|
||||
textField.Blur();
|
||||
|
||||
textField.Focus();
|
||||
_textField.Focus();
|
||||
|
||||
BITCommands.Excute(cmd);
|
||||
|
||||
@@ -252,10 +253,10 @@ namespace BITKit.Console
|
||||
break;
|
||||
case KeyCode.Tab:
|
||||
break;
|
||||
case KeyCode.DownArrow when string.IsNullOrEmpty(textField.text) is false:
|
||||
case KeyCode.DownArrow when string.IsNullOrEmpty(_textField.text) is false:
|
||||
_commandSelector.Index-=1;
|
||||
break;
|
||||
case KeyCode.UpArrow when string.IsNullOrEmpty(textField.text) is false:
|
||||
case KeyCode.UpArrow when string.IsNullOrEmpty(_textField.text) is false:
|
||||
_commandSelector.Index+=1;
|
||||
break;
|
||||
default:
|
||||
@@ -269,16 +270,6 @@ namespace BITKit.Console
|
||||
keyDownEvent.PreventDefault();
|
||||
}
|
||||
}
|
||||
private void Toggle(InputAction.CallbackContext context)
|
||||
{
|
||||
switch (context)
|
||||
{
|
||||
case { interaction: PressInteraction, performed: true }:
|
||||
Toggle(!_isRunning);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async void LogCallback(string condition, string stackTrace, LogType type)
|
||||
{
|
||||
try
|
||||
@@ -286,33 +277,33 @@ namespace BITKit.Console
|
||||
switch (type)
|
||||
{
|
||||
case LogType.Error:
|
||||
outputString.Add($"<color=red>{condition}</color>");
|
||||
_outputString.Add($"<color=red>{condition}</color>");
|
||||
break;
|
||||
case LogType.Warning:
|
||||
outputString.Add($"<color=yellow>{condition}</color>");
|
||||
_outputString.Add($"<color=yellow>{condition}</color>");
|
||||
break;
|
||||
case LogType.Exception:
|
||||
outputString.Add($"<color=red>{condition}</color>");
|
||||
if (exceptionPrintStackTrace)
|
||||
outputString.Add($"<color=red>{stackTrace}</color>");
|
||||
_outputString.Add($"<color=red>{condition}</color>");
|
||||
if (_exceptionPrintStackTrace)
|
||||
_outputString.Add($"<color=red>{stackTrace}</color>");
|
||||
break;
|
||||
default:
|
||||
outputString.Add(condition);
|
||||
_outputString.Add(condition);
|
||||
break;
|
||||
}
|
||||
|
||||
var length = outputString.Count;
|
||||
if (length > logLineLimit)
|
||||
var length = _outputString.Count;
|
||||
if (length > LOGLineLimit)
|
||||
{
|
||||
outputString = outputString.GetRange(length - logLineLimit, logLineLimit);
|
||||
_outputString = _outputString.GetRange(length - LOGLineLimit, LOGLineLimit);
|
||||
}
|
||||
StringBuilder stringBuilder = new();
|
||||
outputString.ForEach(x => stringBuilder.AppendLine(x));
|
||||
_outputString.ForEach(x => stringBuilder.AppendLine(x));
|
||||
try
|
||||
{
|
||||
await BITApp.SwitchToMainThread();
|
||||
scrollView.ScrollToBottomAutomatic();
|
||||
text.text = stringBuilder.ToString();
|
||||
_scrollView.ScrollToBottomAutomatic();
|
||||
_text.text = stringBuilder.ToString();
|
||||
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
@@ -325,25 +316,39 @@ namespace BITKit.Console
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
private void OnTick(float delta)
|
||||
{
|
||||
while (logQueue.TryDequeue(out var log))
|
||||
if (_rootVisualElement is null) return;
|
||||
|
||||
if (Keyboard.current is { backquoteKey: { wasPressedThisFrame: true } })
|
||||
{
|
||||
Toggle(_isRunning=!_isRunning);
|
||||
return;
|
||||
}
|
||||
|
||||
if(LOGQueue.TryDequeue(out var log))
|
||||
{
|
||||
LogCallback(log.condition, log.stackTrace, log.type);
|
||||
}
|
||||
|
||||
if (_isRunning is false) return;
|
||||
|
||||
var pos = textField.worldTransform.GetPosition();
|
||||
var size = textField.layout.size;
|
||||
var pos = _textField.worldTransform.GetPosition();
|
||||
var size = _textField.layout.size;
|
||||
|
||||
commandContainer.style.left = 0;
|
||||
commandContainer.style.top = 0;
|
||||
_commandContainer.style.left = 0;
|
||||
_commandContainer.style.top = 0;
|
||||
|
||||
pos.y += size.y;
|
||||
commandContainer.transform.position = pos;
|
||||
_commandContainer.transform.position = pos;
|
||||
|
||||
commandContainer.style.width = size.x;
|
||||
_commandContainer.style.width = size.x;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_ticker.Remove(OnTick);
|
||||
Application.logMessageReceivedThreaded -= EnqueueLog;
|
||||
}
|
||||
}
|
||||
}
|
48
Src/Unity/Scripts/Physics/UnityCollisionController.cs
Normal file
48
Src/Unity/Scripts/Physics/UnityCollisionController.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.Physics
|
||||
{
|
||||
public class UnityCollisionController : MonoBehaviour
|
||||
{
|
||||
public event Action<Collision> OnUnityCollisionEnter;
|
||||
public event Action<Collision> OnUnityCollisionStay;
|
||||
public event Action<Collision> OnUnityCollisionExit;
|
||||
|
||||
public event Action<Collider> OnUnityTriggerEnter;
|
||||
public event Action<Collider> OnUnityTriggerExit;
|
||||
public event Action<Collider> OnUnityTriggerStay;
|
||||
|
||||
private void OnCollisionEnter(Collision other)
|
||||
{
|
||||
OnUnityCollisionEnter?.Invoke(other);
|
||||
}
|
||||
|
||||
private void OnCollisionStay(Collision other)
|
||||
{
|
||||
OnUnityCollisionStay?.Invoke(other);
|
||||
}
|
||||
|
||||
private void OnCollisionExit(Collision other)
|
||||
{
|
||||
OnUnityCollisionExit?.Invoke(other);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
OnUnityTriggerEnter?.Invoke(other);
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
OnUnityTriggerExit?.Invoke(other);
|
||||
}
|
||||
|
||||
private void OnTriggerStay(Collider other)
|
||||
{
|
||||
OnUnityTriggerStay?.Invoke(other);
|
||||
}
|
||||
}
|
||||
}
|
11
Src/Unity/Scripts/Physics/UnityCollisionController.cs.meta
Normal file
11
Src/Unity/Scripts/Physics/UnityCollisionController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4c7ee9ea9aa58d94b86a980a58c8140a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
8
Src/Unity/Scripts/Pool.meta
Normal file
8
Src/Unity/Scripts/Pool.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10e55a4b52fd0e443a17f5267823bd92
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
141
Src/Unity/Scripts/Pool/UnityPoolService.cs
Normal file
141
Src/Unity/Scripts/Pool/UnityPoolService.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BITKit.Mod;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace BITKit.Pool
|
||||
{
|
||||
public class UnityPoolService:IPoolService,IDisposable
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public UnityPoolService(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
private static readonly ConcurrentDictionary<string,List<object>> Pool = new();
|
||||
private static readonly ConcurrentDictionary<string,List<object>> UsingPool = new();
|
||||
private static readonly ConcurrentDictionary<string, Queue<object>> ReadyPool = new();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
var hashset = new HashSet<GameObject>();
|
||||
foreach (var (_,list) in Pool)
|
||||
{
|
||||
foreach (var obj in list)
|
||||
{
|
||||
if (obj is not Component component) continue;
|
||||
if (!hashset.Add(component.gameObject)) continue;
|
||||
if (component.gameObject)
|
||||
Object.Destroy(component.gameObject);
|
||||
}
|
||||
}
|
||||
Pool.Clear();
|
||||
UsingPool.Clear();
|
||||
ReadyPool.Clear();
|
||||
}
|
||||
|
||||
public async UniTask<T> Spawn<T>(string path) where T : class
|
||||
{
|
||||
if (Pool.ContainsKey(path))
|
||||
{
|
||||
var readyQueue = ReadyPool[path];
|
||||
var usingList = UsingPool[path];
|
||||
if (readyQueue.TryDequeue(out var obj))
|
||||
{
|
||||
var value = obj as T;
|
||||
usingList.Add(value);
|
||||
return obj as T;
|
||||
}
|
||||
|
||||
if (usingList.Count>0)
|
||||
{
|
||||
obj = usingList[0];
|
||||
usingList.RemoveAt(0);
|
||||
usingList.Add(obj);
|
||||
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
if (obj is GameObject gameObject)
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
#endif
|
||||
|
||||
return obj as T;
|
||||
}
|
||||
}
|
||||
|
||||
var list = Pool.GetOrCreate(path);
|
||||
|
||||
UsingPool.GetOrCreate(path);
|
||||
ReadyPool.GetOrCreate(path);
|
||||
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
if (typeof(Object).IsAssignableFrom(typeof(T)))
|
||||
{
|
||||
var asset =await ModService.LoadAsset<T>(path);
|
||||
if (asset is Object o)
|
||||
{
|
||||
var instance = Object.Instantiate(o);
|
||||
list.Add(instance);
|
||||
UsingPool.GetOrCreate(path).Add(instance);
|
||||
ReadyPool.GetOrCreate(path);
|
||||
return instance as T;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
//检查T的构造函数,如果有需要参数的构造函数,就从ServiceProvider中获取,如果没有,则直接通过System.Activator创建
|
||||
if (typeof(T).GetConstructors().Any(constructorInfo => constructorInfo.GetParameters().Length is 0))
|
||||
{
|
||||
var value = Activator.CreateInstance<T>();
|
||||
list.Add(value);
|
||||
UsingPool.GetOrCreate(path).Add(value);
|
||||
ReadyPool.GetOrCreate(path);
|
||||
return value;
|
||||
}
|
||||
{
|
||||
var value = _serviceProvider.GetRequiredService<T>();
|
||||
list.Add(value);
|
||||
UsingPool.GetOrCreate(path).Add(value);
|
||||
ReadyPool.GetOrCreate(path);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public void Despawn<T>(T obj, string path) where T : class
|
||||
{
|
||||
if (UsingPool.TryGetValue(path, out var value))
|
||||
{
|
||||
value.Remove(obj);
|
||||
ReadyPool[path].Enqueue(obj);
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
if (obj is GameObject gameObject)
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
if (Pool.ContainsKey(path)) return;
|
||||
Pool.GetOrCreate(path).Add(obj);
|
||||
ReadyPool.GetOrCreate(path).Enqueue(obj);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public UniTask InitializeAsync()
|
||||
{
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
11
Src/Unity/Scripts/Pool/UnityPoolService.cs.meta
Normal file
11
Src/Unity/Scripts/Pool/UnityPoolService.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58aae7a560a8881468df0f0f3a137f4a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -5,6 +5,7 @@ using AYellowpaper.SerializedCollections;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using YooAsset;
|
||||
#endif
|
||||
|
||||
using UnityEngine;
|
||||
@@ -21,7 +22,15 @@ namespace BITKit
|
||||
{
|
||||
if (_singleton == null)
|
||||
{
|
||||
//_singleton = ScriptableObjectHelper.Get<DictionaryReferenceScriptableObject>();
|
||||
#if UNITY_EDITOR
|
||||
_singleton =
|
||||
AssetDatabase.LoadAssetAtPath<DictionaryReferenceScriptableObject>(
|
||||
"Assets/Artists/Configs/reference_dictionary.asset");
|
||||
#else
|
||||
var task = YooAssets.LoadAssetAsync("reference_directory");
|
||||
task.WaitForAsyncComplete();
|
||||
_singleton=task.AssetObject as DictionaryReferenceScriptableObject;
|
||||
#endif
|
||||
}
|
||||
return _singleton;
|
||||
}
|
||||
|
@@ -1,12 +1,12 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Remoting.Contexts;
|
||||
using System.ComponentModel.Design;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using UnityEngine.InputSystem;
|
||||
using BITKit;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using BITKit.Mod;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace BITKit.UX.Internal
|
||||
{
|
||||
public class ContextMenu : IContextMenu
|
||||
@@ -20,16 +20,16 @@ namespace BITKit.UX.Internal
|
||||
{
|
||||
public Label(string text)
|
||||
{
|
||||
this.text = text;
|
||||
_text = text;
|
||||
}
|
||||
string text;
|
||||
private readonly string _text;
|
||||
public override VisualElement GetVisualElement()
|
||||
{
|
||||
UnityEngine.UIElements.Label label = new(text);
|
||||
UnityEngine.UIElements.Label label = new(_text);
|
||||
return label;
|
||||
}
|
||||
}
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class TestContextMenu
|
||||
{
|
||||
public void Execute()
|
||||
@@ -45,19 +45,20 @@ namespace BITKit.UX.Internal
|
||||
{
|
||||
public Button(string text, params Action[] actions)
|
||||
{
|
||||
this.text = text;
|
||||
_text = text;
|
||||
foreach (var x in actions)
|
||||
{
|
||||
action += x;
|
||||
_action += x;
|
||||
}
|
||||
}
|
||||
string text;
|
||||
Action action;
|
||||
|
||||
private readonly string _text;
|
||||
private readonly Action _action;
|
||||
public override VisualElement GetVisualElement()
|
||||
{
|
||||
UnityEngine.UIElements.Button button = new();
|
||||
button.clicked += action;
|
||||
button.text = text;
|
||||
button.clicked += _action;
|
||||
button.text = _text;
|
||||
return button;
|
||||
}
|
||||
}
|
||||
@@ -77,14 +78,14 @@ namespace BITKit.UX
|
||||
}
|
||||
public static ContextMenuBuilder BuildAction(this ContextMenuBuilder self, string text, Action action)
|
||||
{
|
||||
self.Add(new Internal.Button(text, action, self.Excute));
|
||||
self.Add(new Internal.Button(text, action, self.Execute));
|
||||
return self;
|
||||
}
|
||||
}
|
||||
public class ContextMenuBuilder
|
||||
{
|
||||
readonly List<IContextMenu> contexts = new();
|
||||
public event Action OnExcuted;
|
||||
private readonly List<IContextMenu> _contexts = new();
|
||||
public event Action OnExecuted;
|
||||
private ContextMenuBuilder()
|
||||
{
|
||||
|
||||
@@ -97,25 +98,48 @@ namespace BITKit.UX
|
||||
{
|
||||
UXContextMenu.Singleton.Create(this);
|
||||
}
|
||||
internal void Excute()
|
||||
internal void Execute()
|
||||
{
|
||||
OnExcuted?.Invoke();
|
||||
OnExecuted?.Invoke();
|
||||
}
|
||||
public void Add(IContextMenu x) => contexts.Add(x);
|
||||
public IEnumerable<IContextMenu> GetContextMenus() => contexts.ToArray();
|
||||
public void Add(IContextMenu x) => _contexts.Add(x);
|
||||
public IEnumerable<IContextMenu> GetContextMenus() => _contexts.ToArray();
|
||||
}
|
||||
public class UXContextMenu : MonoBehaviour
|
||||
public class UXContextMenu:IDisposable
|
||||
{
|
||||
internal static UXContextMenu Singleton;
|
||||
[SerializeField] private UIDocument document;
|
||||
private VisualElement root;
|
||||
private VisualElement container;
|
||||
private void Awake()
|
||||
private readonly IUXService _uxService;
|
||||
private VisualElement _root;
|
||||
private VisualElement _container;
|
||||
public UXContextMenu(IUXService uxService)
|
||||
{
|
||||
_uxService = uxService;
|
||||
Singleton = this;
|
||||
root = document.rootVisualElement;
|
||||
container = document.rootVisualElement[1];
|
||||
root.Q("background-image").RegisterCallback<MouseDownEvent>(x =>
|
||||
uxService.OnPanelChanged += OnPanelChanged;
|
||||
InitializeAsync();
|
||||
}
|
||||
|
||||
private async void InitializeAsync()
|
||||
{
|
||||
var go = new GameObject("UXConsole");
|
||||
Object.DontDestroyOnLoad(go);
|
||||
var document = go.AddComponent<UIDocument>();
|
||||
document.sortingOrder = 1;
|
||||
try
|
||||
{
|
||||
var panelSettings =await ModService.LoadAsset<PanelSettings>("ux_panel_settings");
|
||||
document.panelSettings = panelSettings;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
BIT4Log.Warning<UXService>("未找到ux_panel_settings");
|
||||
throw;
|
||||
}
|
||||
document.visualTreeAsset = await ModService.LoadAsset<VisualTreeAsset>("ux_context_menu");
|
||||
|
||||
_root = document.rootVisualElement;
|
||||
_container = _root.Q<VisualElement>("menu-container");
|
||||
_root.Q("background-image").RegisterCallback<MouseDownEvent>(_ =>
|
||||
{
|
||||
Close();
|
||||
});
|
||||
@@ -125,25 +149,35 @@ namespace BITKit.UX
|
||||
{
|
||||
var pos = Mouse.current.position.ReadValue();
|
||||
pos.y = Screen.height - pos.y;
|
||||
pos = RuntimePanelUtils.ScreenToPanel(root.panel, pos);
|
||||
pos = RuntimePanelUtils.ScreenToPanel(_root.panel, pos);
|
||||
|
||||
container.style.position = Position.Absolute;
|
||||
container.style.left = pos.x;
|
||||
container.style.top = pos.y;
|
||||
container.Clear();
|
||||
_container.style.position = Position.Absolute;
|
||||
_container.style.left = pos.x;
|
||||
_container.style.top = pos.y;
|
||||
_container.Clear();
|
||||
|
||||
root.SetActive(true);
|
||||
_root.SetActive(true);
|
||||
|
||||
foreach (var context in builder.GetContextMenus())
|
||||
{
|
||||
container.Add(context.GetVisualElement());
|
||||
_container.Add(context.GetVisualElement());
|
||||
}
|
||||
builder.OnExcuted += Close;
|
||||
builder.OnExecuted += Close;
|
||||
}
|
||||
void Close()
|
||||
private void Close()
|
||||
{
|
||||
container.Clear();
|
||||
root.SetActive(false);
|
||||
_container.Clear();
|
||||
_root.SetActive(false);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_uxService.OnPanelChanged -= OnPanelChanged;
|
||||
}
|
||||
|
||||
private void OnPanelChanged(IUXPanel arg1, IUXPanel arg2)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -103,7 +103,9 @@ namespace BITKit.UX
|
||||
|
||||
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 _))
|
||||
@@ -124,6 +126,7 @@ namespace BITKit.UX
|
||||
|
||||
private void OnEntry(IUXPanel obj)
|
||||
{
|
||||
OnPanelChanged?.Invoke(_currentPanel,obj);
|
||||
_currentPanel = obj;
|
||||
}
|
||||
private void OnTick(float delta)
|
||||
|
Reference in New Issue
Block a user