This commit is contained in:
CortexCore
2024-11-13 17:47:45 +08:00
parent c4af12acd7
commit 416e3322db
208 changed files with 2591757 additions and 1497 deletions

View File

@@ -72,9 +72,9 @@
"name": "Aim",
"type": "Button",
"id": "f521b9d3-bfbb-4ce2-85c8-15cb3fcc2534",
"expectedControlType": "Button",
"expectedControlType": "",
"processors": "",
"interactions": "Tap,Hold,Press",
"interactions": "Press",
"initialStateCheck": false
},
{
@@ -135,7 +135,7 @@
"name": "Primary",
"type": "Button",
"id": "113a3c02-0fbb-4081-9fac-c39878ddfd09",
"expectedControlType": "Button",
"expectedControlType": "",
"processors": "",
"interactions": "Press,Hold",
"initialStateCheck": false
@@ -144,7 +144,7 @@
"name": "Secondary",
"type": "Button",
"id": "57a12f28-2fde-45e5-957f-d8d61afe44de",
"expectedControlType": "Button",
"expectedControlType": "",
"processors": "",
"interactions": "Press,Hold",
"initialStateCheck": false
@@ -153,7 +153,7 @@
"name": "Tertiary",
"type": "Button",
"id": "c2535f43-fec8-42bd-b680-6d1cee5246e5",
"expectedControlType": "Button",
"expectedControlType": "",
"processors": "",
"interactions": "Press,Hold",
"initialStateCheck": false
@@ -162,7 +162,7 @@
"name": "Quaternary",
"type": "Button",
"id": "6740f3a7-7571-4277-96e9-15aa08af7302",
"expectedControlType": "Button",
"expectedControlType": "",
"processors": "",
"interactions": "Press,Hold",
"initialStateCheck": false
@@ -171,7 +171,7 @@
"name": "Quinary",
"type": "Button",
"id": "97511588-e441-4e27-953e-e60cbea80489",
"expectedControlType": "Button",
"expectedControlType": "",
"processors": "",
"interactions": "",
"initialStateCheck": false
@@ -180,7 +180,7 @@
"name": "Senary",
"type": "Button",
"id": "46173186-2ef3-4ab9-8ff4-b94cc113df67",
"expectedControlType": "Button",
"expectedControlType": "",
"processors": "",
"interactions": "",
"initialStateCheck": false

View File

@@ -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;
}
}
}

View File

@@ -1,74 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit.StateMachine;
using UnityEngine;
namespace BITKit
{
public abstract class StateBasedMonoBehaviour<T> : MonoBehaviour, IStateMachine<T> where T : IState
{
[SerializeField] private MonoStateMachine<T> 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<T, T> OnStateChanged
{
add => stateMachine.OnStateChanged += value;
remove => stateMachine.OnStateChanged -= value;
}
public event Action<T> OnStateRegistered;
public event Action<T> OnStateUnRegistered;
public IDictionary<Type, T> 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<State>() where State : T
{
stateMachine.TransitionState<State>();
}
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<T>.InvokeOnStateRegistered(T state)
{
OnStateRegistered?.Invoke(state);
}
void IStateMachine<T>.InvokeOnStateUnRegistered(T state)
{
OnStateUnRegistered?.Invoke(state);
}
}
}

View 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);
}
}
}

View 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;
}
}
}

View File

@@ -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<T> LoadAssets<T>(int id) where T :class
{
var path = DictionaryReferenceScriptableObject.Dictionary[id];
return ModService.LoadAsset<T>(path);
}
}
public sealed class DictionaryReferenceConfigAttribute : System.Attribute
{
public readonly int index;

View File

@@ -2,9 +2,10 @@ using System.Collections;
using System.Collections.Generic;
using System.IO;
using AYellowpaper.SerializedCollections;
using YooAsset;
#if UNITY_EDITOR
using UnityEditor;
#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;
}

View File

@@ -18,7 +18,9 @@
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"defineConstraints": [
"deprecated"
],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -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>("SensorEditorWindow");
}
private void CreateGUI()
{
_toggle = rootVisualElement.Create<Toggle>("Enable Sensor");
_toggle.label = "Enable Sensor";
_toggle.value = SensorGlobalSettings.Enabled;
}
private void Update()
{
SensorGlobalSettings.Enabled = _toggle.value;
}
}
}

View File

@@ -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<TState> : IStateMachine<TState> 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<TState, TState> OnStateChanged;
public event Action<TState> OnStateRegistered;
public event Action<TState> OnStateUnRegistered;
[SerializeReference, SubclassSelector] public List<TState> states = new();
[SerializeField,ReadOnly] private string _currentStateName;
[SerializeField] private bool debug;
[SerializeField] private bool transitionOnNextFrame;
public IDictionary<Type, TState> StateDictionary { get; } = new Dictionary<Type, TState>();
private bool _enabled = true;
private readonly DoubleBuffer<TState> _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<State>() 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<MonoStateMachine<TState>>($"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);
}
}
}

View File

@@ -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,53 +98,89 @@ 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;
private bool _isInitialized = false;
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(nameof(UXContextMenu));
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();
});
_isInitialized = true;
Close();
}
public void Create(ContextMenuBuilder builder)
{
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);
if(_isInitialized is false)return;
_container.Clear();
_root.SetActive(false);
}
public void Dispose()
{
_uxService.OnPanelChanged -= OnPanelChanged;
}
private void OnPanelChanged(IUXPanel arg1, IUXPanel arg2)
{
Close();
}
}
}

View File

@@ -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<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;
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(false);
}
}
protected virtual Optional<float> EntryDuration { get; }= new();
@@ -62,42 +107,8 @@ namespace BITKit.UX
OnEntry?.Invoke();
}
async UniTask IEntryElement.EntryAsync()
{
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;
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);
}
{
await InitializeAsync();
RootVisualElement.SetActive(true);
RootVisualElement.AddToClassList(USSEntry);

View File

@@ -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)

View File

@@ -2,15 +2,15 @@
<Style src="project://database/Assets/BITKit/Unity/UX/Common/Common.uss?fileID=7433441132597879392&amp;guid=a3a69d3518fd02b489e721f3c5b0b539&amp;type=3#Common" />
<Style src="project://database/Assets/BITKit/Unity/UX/BITConsole.uss?fileID=7433441132597879392&amp;guid=9175bfb75cd47b9488b9f12bd34e50fd&amp;type=3#BITConsole" />
<ui:VisualElement name="Root" style="position: absolute; left: 0; top: 0; right: 0; bottom: 0;">
<ui:VisualElement name="Console" class="Console" style="width: 1024px; height: 768px; border-top-left-radius: 16px; border-bottom-left-radius: 16px; border-top-right-radius: 16px; border-bottom-right-radius: 16px; padding-left: 16px; padding-right: 16px; padding-top: 8px; padding-bottom: 8px; margin-left: auto; margin-right: auto; margin-top: auto; margin-bottom: auto;">
<ui:Label text="Console" display-tooltip-when-elided="true" class="Console-Text" />
<ui:ScrollView name="context-scrollview" class="Console-ScrollView" style="height: auto; flex-grow: 1; display: flex;">
<ui:Label text="This is Console&#10;This is &lt;size=24&gt;Rich Text&lt;/size&gt;&#10;This is &lt;color=yellow&gt;Warning&lt;/color&gt;&#10;This is &lt;color=red&gt;Error&lt;/color&gt;&#10;This is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text&#10;This is Console&#10;This is &lt;size=24&gt;Rich Text&lt;/size&gt;&#10;This is &lt;color=yellow&gt;Warning&lt;/color&gt;&#10;This is &lt;color=red&gt;Error&lt;/color&gt;&#10;This is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text&#10;&lt;color=#add8e6ff&gt;&lt;b&gt;TypeName&lt;/b&gt;&lt;/color&gt;:MyMessage" display-tooltip-when-elided="true" name="Text" enable-rich-text="true" parse-escape-sequences="false" class="Console-Text" style="padding-left: 8px; padding-right: 8px; padding-top: 4px; padding-bottom: 4px; white-space: normal;" />
<ui:VisualElement name="console" class="console" style="width: 1024px; height: 768px; border-top-left-radius: 16px; border-bottom-left-radius: 16px; border-top-right-radius: 16px; border-bottom-right-radius: 16px; padding-left: 16px; padding-right: 16px; padding-top: 8px; padding-bottom: 8px; margin-left: auto; margin-right: auto; margin-top: auto; margin-bottom: auto;">
<ui:Label text="console" display-tooltip-when-elided="true" class="console-Text" />
<ui:ScrollView name="context-scrollview" class="console-container" style="height: auto; flex-grow: 1; display: flex;">
<ui:Label text="This is console&#10;This is &lt;size=24&gt;Rich Text&lt;/size&gt;&#10;This is &lt;color=yellow&gt;Warning&lt;/color&gt;&#10;This is &lt;color=red&gt;Error&lt;/color&gt;&#10;This is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text&#10;This is console&#10;This is &lt;size=24&gt;Rich Text&lt;/size&gt;&#10;This is &lt;color=yellow&gt;Warning&lt;/color&gt;&#10;This is &lt;color=red&gt;Error&lt;/color&gt;&#10;This is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text&#10;&lt;color=#add8e6ff&gt;&lt;b&gt;TypeName&lt;/b&gt;&lt;/color&gt;:MyMessage" display-tooltip-when-elided="true" name="Text" enable-rich-text="true" parse-escape-sequences="false" class="console-Text" style="padding-left: 8px; padding-right: 8px; padding-top: 4px; padding-bottom: 4px; white-space: normal;" />
</ui:ScrollView>
<ui:TextField picking-mode="Ignore" value="filler text" text="filler text" name="TextField" class="Console-TextField" style="height: 48px;" />
<ui:TextField picking-mode="Ignore" value="filler text" text="filler text" name="TextField" class="console-TextField" style="height: 48px; margin-top: 8px;" />
</ui:VisualElement>
</ui:VisualElement>
<ui:VisualElement name="commands-container" picking-mode="Ignore" style="position: absolute; top: 830px; left: 120px; align-items: stretch; min-width: 512px;">
<ui:VisualElement name="commands-container" picking-mode="Ignore" class="console" style="position: absolute; top: 830px; left: 120px; align-items: stretch; min-width: 512px;">
<ui:Button text="Button" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="Button" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:Button text="Button" parse-escape-sequences="true" display-tooltip-when-elided="true" />

View File

@@ -31,3 +31,4 @@
scale: 1.2 1.2;
opacity: 0.5;
}

View File

@@ -1,19 +1,16 @@
Button {
background-color: rgba(0, 0, 0, 0);
color: rgba(255, 255, 255, 0.5);
-unity-font-definition: url('project://database/Assets/Artist/Art/Fonts/NotoSansSC-Bold%20SDF.asset?fileID=11400000&guid=033cc1a4c6c35c6488ad74f4cee476ac&type=2#NotoSansSC-Bold SDF');
border-left-width: 0;
border-right-width: 0;
border-top-width: 0;
border-bottom-width: 0;
-unity-text-align: middle-left;
}
Button:hover {
background-color: rgba(64, 64, 64, 255);
background-color: rgb(64, 64, 64);
}
Button:active {
background-color: rgba(125, 125, 125, 255);
color: rgba(29, 29, 29, 255);
background-color: rgb(125, 125, 125);
color: rgb(29, 29, 29);
}