This commit is contained in:
CortexCore
2024-03-31 23:31:00 +08:00
parent e179d2eb53
commit b7b89ee71a
641 changed files with 31286 additions and 22134 deletions

View File

@@ -2,10 +2,12 @@ using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using UnityEngine.UIElements;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.InputSystem;
#endif
namespace BITKit
{
@@ -21,9 +23,15 @@ namespace BITKit
#if UNITY_EDITOR
public class BITInputSystemEditor : EditorWindow
{
Toggle allowInput;
private Toggle allowInput;
private Toggle allowInputLog;
private InputAction _inputAction;
private ObjectField inputActionField;
private Label nameLabel;
private Label valueLabel;
[MenuItem("Tools/InputSystemEditor")]
static void Entry()
private static void Open()
{
GetWindow<BITInputSystemEditor>().Show();
}
@@ -36,10 +44,87 @@ namespace BITKit
rootVisualElement.style.paddingBottom = 16;
rootVisualElement.Add(allowInput =new Toggle());
allowInput.text = "Enable Input";
allowInput.SetEnabled(false);
allowInput.style.opacity = 1;
allowInputLog = rootVisualElement.Create<Toggle>();
allowInputLog.text = "Enable Input Log";
inputActionField = rootVisualElement.Create<ObjectField>();
inputActionField.label = "Debug InputAction";
inputActionField.RegisterValueChangedCallback(NewValue);
inputActionField.objectType = typeof(InputActionReference);
var hContainer = rootVisualElement.Create<VisualElement>();
hContainer.style.flexDirection = FlexDirection.Row;
nameLabel = hContainer.Create<Label>();
valueLabel = hContainer.Create<Label>();
valueLabel.style.paddingLeft = 8;
nameLabel.text = "Wait";
valueLabel.text ="Input";
}
private void OnDisable()
{
_inputAction?.Dispose();
}
private void NewValue(ChangeEvent<Object> evt)
{
_inputAction?.Dispose();
_inputAction = null;
if (evt.newValue is not InputActionReference inputActionReference) return;
_inputAction = inputActionReference.action.Clone();
_inputAction.Enable();
_inputAction.RegisterCallback(x =>
{
if (allowInputLog.value)
{
Debug.Log(x);
}
});
}
private void Update()
{
allowInput.value = BITInputSystem.AllowInput;
if (_inputAction is null)
{
nameLabel.text = "Wait";
valueLabel.text = "Input";
return;
}
var nameBuilder = new System.Text.StringBuilder();
var valueBuilder = new System.Text.StringBuilder();
nameBuilder.AppendLine(nameof(InputAction.enabled));
valueBuilder.AppendLine(_inputAction.enabled.ToString());
nameBuilder.AppendLine(nameof(InputAction.IsPressed));
valueBuilder.AppendLine(_inputAction.IsPressed().ToString());
nameBuilder.AppendLine(nameof(InputAction.ReadValue));
valueBuilder.AppendLine(_inputAction.ReadValue<float>().ToString());
nameBuilder.AppendLine(nameof(InputAction.ReadValueAsObject));
valueBuilder.AppendLine(_inputAction.ReadValueAsObject()?.ToString());
nameBuilder.AppendLine(nameof(InputAction.triggered));
valueBuilder.AppendLine(_inputAction.triggered.ToString());
nameBuilder.AppendLine(nameof(InputAction.phase));
valueBuilder.AppendLine(_inputAction.phase.ToString());
nameLabel.text = nameBuilder.ToString();
valueLabel.text = valueBuilder.ToString();
}
}
#endif

View File

@@ -11,20 +11,38 @@ namespace BITKit
[System.Serializable]
public class InputActionGroup : IDisposable
{
public override string ToString()
{
return $"Source:{Source}\nEnabled:{isEnabled}\nInitialized:{state}\nKeys:{string.Join("\n",actions.Keys)}";
}
public string Source;
private int lockFile = Guid.NewGuid().GetHashCode();
public bool allowGlobalActivation = true;
[SerializeField, ReadOnly] private bool isEnabled;
private InitializationState state = InitializationState.None;
public ValidHandle allowInput = new();
public readonly ValidHandle allowInput = new();
private readonly ConcurrentDictionary<string,InputAction> actions = new();
/// <summary>
/// 注册所有(started,performed,canceled)回调
/// </summary>
/// <param name="reference"></param>
/// <param name="callback"></param>
/// <returns></returns>
public InputActionGroup RegisterCallback(InputActionReference reference,
Action<InputAction.CallbackContext> callback)
{
var action = EnsureCreated(reference);
action.RegisterCallback(callback);
return this;
}
public InputAction EnsureCreated(InputActionReference reference)
{
if (reference is null)
{
Debug.LogWarning($"未知的引用");
return this;
return null;
}
EnsureConfiguration();
@@ -34,12 +52,12 @@ namespace BITKit
newAction.Rename(reference.name);
return newAction;
});
action.RegisterCallback(callback);
allowInput.Invoke();
return this;
return action;
}
public void Inherit(InputActionGroup other)
{
throw new NotImplementedException();

View File

@@ -3,6 +3,8 @@ using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using System;
using System.Linq;
namespace BITKit
{
public static partial class InputSystemExtensions
@@ -18,6 +20,15 @@ namespace BITKit
self.canceled += action;
return self;
}
public static string GetKeyMap(this InputAction self)
{
return string.Join("|", self.bindings.Select(x=>x.path.Split("/").Last().ToUpper()));
}
public static string GetKeyMap(this InputActionReference self)
{
return string.Join("|", self.action.bindings.Select(x=>x.path.Split("/").Last().ToUpper()));
}
public static InputAction UnRegisterCallback(this InputAction self, Action<InputAction.CallbackContext> action)
{
self.performed -= action;