1
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
@@ -15,7 +16,7 @@ namespace BITKit
|
||||
[SerializeField, ReadOnly] private bool isEnabled;
|
||||
private InitializationState state = InitializationState.None;
|
||||
public ValidHandle allowInput = new();
|
||||
private readonly List<InputAction> actions = new();
|
||||
private readonly ConcurrentDictionary<string,InputAction> actions = new();
|
||||
|
||||
public InputActionGroup RegisterCallback(InputActionReference reference,
|
||||
Action<InputAction.CallbackContext> callback)
|
||||
@@ -27,22 +28,38 @@ namespace BITKit
|
||||
}
|
||||
|
||||
EnsureConfiguration();
|
||||
var action = reference.action.Clone();
|
||||
var action = actions.GetOrAdd(reference.name, _ =>
|
||||
{
|
||||
var newAction = reference.action.Clone();
|
||||
newAction.Rename(reference.name);
|
||||
return newAction;
|
||||
});
|
||||
action.RegisterCallback(callback);
|
||||
allowInput.Invoke();
|
||||
|
||||
actions
|
||||
.Where(x => x.name == action.name)
|
||||
.CreateOrAddIfEmety(actions, action)
|
||||
.ForEach(x => { x.RegisterCallback(callback); });
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Inherit(InputActionGroup other)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public InputAction GetAction(string name)
|
||||
{
|
||||
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))
|
||||
return action;
|
||||
throw new ArgumentException($"未知的引用{reference.name}");
|
||||
}
|
||||
public void UnRegisterCallback(InputActionReference reference, Action<InputAction.CallbackContext> callback)
|
||||
{
|
||||
foreach (var action in actions.Where(x => x.name == reference.action.name))
|
||||
{
|
||||
if(actions.TryGetValue(reference.name,out var action))
|
||||
action.UnRegisterCallback(callback);
|
||||
}
|
||||
}
|
||||
|
||||
private void EnsureConfiguration()
|
||||
@@ -68,7 +85,7 @@ namespace BITKit
|
||||
|
||||
private void AllowInput(bool allow)
|
||||
{
|
||||
foreach (var action in actions)
|
||||
foreach (var action in actions.Values)
|
||||
{
|
||||
if (allow)
|
||||
{
|
||||
@@ -81,14 +98,13 @@ namespace BITKit
|
||||
}
|
||||
isEnabled = allow;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var action in actions)
|
||||
foreach (var action in actions.Values)
|
||||
{
|
||||
action.Disable();
|
||||
action.Dispose();
|
||||
}
|
||||
|
||||
actions.Clear();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user