1
This commit is contained in:
@@ -8,20 +8,31 @@ using System.Linq;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class InputActionGroup : IDisposable
|
||||
{
|
||||
public override string ToString()
|
||||
private class InputActionProxy
|
||||
{
|
||||
return $"Source:{Source}\nEnabled:{isEnabled}\nInitialized:{state}\nKeys:{string.Join("\n",actions.Keys)}";
|
||||
public event Action<InputAction.CallbackContext> Callback;
|
||||
public void Invoke(InputAction.CallbackContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
Callback?.Invoke(context);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
public string Source;
|
||||
private int lockFile = Guid.NewGuid().GetHashCode();
|
||||
|
||||
private int _lockFile = Guid.NewGuid().GetHashCode();
|
||||
public bool allowGlobalActivation = true;
|
||||
[SerializeField, ReadOnly] private bool isEnabled;
|
||||
private InitializationState state = InitializationState.None;
|
||||
private InitializationState _state = InitializationState.None;
|
||||
public readonly ValidHandle allowInput = new();
|
||||
private readonly ConcurrentDictionary<string,InputAction> actions = new();
|
||||
private readonly ConcurrentDictionary<string,InputAction> _actions = new();
|
||||
private readonly ConcurrentDictionary<int, InputActionProxy> _callbacks = new();
|
||||
|
||||
/// <summary>
|
||||
/// 注册所有(started,performed,canceled)回调
|
||||
@@ -33,20 +44,14 @@ namespace BITKit
|
||||
Action<InputAction.CallbackContext> callback)
|
||||
{
|
||||
var action = EnsureCreated(reference);
|
||||
action.RegisterCallback(callback);
|
||||
_callbacks[action.GetHashCode()].Callback += callback;
|
||||
return this;
|
||||
}
|
||||
|
||||
public InputActionGroup RegisterCallback(InputAction inputAction,Action<InputAction.CallbackContext> callback)
|
||||
{
|
||||
EnsureConfiguration();
|
||||
|
||||
var action = actions.GetOrAdd(inputAction.name, _=>inputAction.Clone());
|
||||
|
||||
allowInput.Invoke();
|
||||
|
||||
action.RegisterCallback(callback);
|
||||
|
||||
var action = EnsureCreated(inputAction);
|
||||
_callbacks[action.GetHashCode()].Callback += callback;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -59,10 +64,16 @@ namespace BITKit
|
||||
}
|
||||
|
||||
EnsureConfiguration();
|
||||
var action = actions.GetOrAdd(reference.name, _ =>
|
||||
var action = _actions.GetOrAdd(reference.name, _ =>
|
||||
{
|
||||
var newAction = reference.action.Clone();
|
||||
newAction.Rename(reference.name);
|
||||
|
||||
var callback = _callbacks[newAction.GetHashCode()] = new();
|
||||
newAction.performed += callback.Invoke;
|
||||
newAction.canceled+=callback.Invoke;
|
||||
newAction.started+=callback.Invoke;
|
||||
|
||||
return newAction;
|
||||
});
|
||||
|
||||
@@ -73,10 +84,16 @@ namespace BITKit
|
||||
public InputAction EnsureCreated(InputAction inputAction)
|
||||
{
|
||||
EnsureConfiguration();
|
||||
var action = actions.GetOrAdd(inputAction.name, _ =>
|
||||
var action = _actions.GetOrAdd(inputAction.name, _ =>
|
||||
{
|
||||
var newAction = inputAction.Clone();
|
||||
newAction.Rename(inputAction.name);
|
||||
|
||||
var callback = _callbacks[newAction.GetHashCode()] = new();
|
||||
newAction.performed += callback.Invoke;
|
||||
newAction.canceled+=callback.Invoke;
|
||||
newAction.started+=callback.Invoke;
|
||||
|
||||
return newAction;
|
||||
});
|
||||
|
||||
@@ -91,34 +108,38 @@ namespace BITKit
|
||||
}
|
||||
public InputAction GetAction(string name)
|
||||
{
|
||||
if(actions.TryGetValue(name,out var action))
|
||||
if(_actions.TryGetValue(name,out var action))
|
||||
return action;
|
||||
throw new ArgumentException($"未知的引用{name}");
|
||||
}
|
||||
public InputAction GetAction(InputActionReference reference)
|
||||
{
|
||||
if(actions.TryGetValue(reference.name,out var action))
|
||||
if(_actions.TryGetValue(reference.name,out var action))
|
||||
return action;
|
||||
throw new ArgumentException($"未知的引用{reference.name}");
|
||||
}
|
||||
public void UnRegisterCallback(InputActionReference reference, Action<InputAction.CallbackContext> callback)
|
||||
{
|
||||
if(actions.TryGetValue(reference.name,out var action))
|
||||
action.UnRegisterCallback(callback);
|
||||
if (_actions.TryGetValue(reference.name, out var action))
|
||||
{
|
||||
_callbacks[action.GetHashCode()].Callback -= callback;
|
||||
}
|
||||
}
|
||||
|
||||
public void UnRegisterCallback(InputAction inputAction, Action<InputAction.CallbackContext> callback)
|
||||
{
|
||||
if(actions.TryGetValue(inputAction.name,out var action))
|
||||
action.UnRegisterCallback(callback);
|
||||
if (_actions.TryGetValue(inputAction.name, out var action))
|
||||
{
|
||||
_callbacks[action.GetHashCode()].Callback -= callback;
|
||||
}
|
||||
}
|
||||
|
||||
private void EnsureConfiguration()
|
||||
{
|
||||
if (state is not InitializationState.Initialized)
|
||||
if (_state is not InitializationState.Initialized)
|
||||
{
|
||||
Init();
|
||||
state = InitializationState.Initialized;
|
||||
_state = InitializationState.Initialized;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,12 +152,12 @@ namespace BITKit
|
||||
|
||||
private void ListenGlobalInput(bool allowInput)
|
||||
{
|
||||
this.allowInput.SetDisableElements(lockFile, !allowInput);
|
||||
this.allowInput.SetDisableElements(_lockFile, !allowInput);
|
||||
}
|
||||
|
||||
private void AllowInput(bool allow)
|
||||
{
|
||||
foreach (var action in actions.Values)
|
||||
foreach (var action in _actions.Values)
|
||||
{
|
||||
if (allow)
|
||||
{
|
||||
@@ -151,12 +172,12 @@ namespace BITKit
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var action in actions.Values)
|
||||
foreach (var action in _actions.Values)
|
||||
{
|
||||
action.Disable();
|
||||
action.Dispose();
|
||||
}
|
||||
actions.Clear();
|
||||
_actions.Clear();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user