1
This commit is contained in:
@@ -4,37 +4,39 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using System.Linq;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
[System.Serializable]
|
||||
public class InputActionGroup : IDisposable
|
||||
{
|
||||
int lockFile = Guid.NewGuid().GetHashCode();
|
||||
private int lockFile = Guid.NewGuid().GetHashCode();
|
||||
public bool allowGlobalActivation = true;
|
||||
public bool isEnabled;
|
||||
InitializationState state = InitializationState.None;
|
||||
[SerializeField, ReadOnly] private bool isEnabled;
|
||||
private InitializationState state = InitializationState.None;
|
||||
public ValidHandle allowInput = new();
|
||||
List<InputAction> actions = new();
|
||||
public InputActionGroup RegisterCallback(InputActionReference reference, Action<InputAction.CallbackContext> callback)
|
||||
private readonly List<InputAction> actions = new();
|
||||
|
||||
public InputActionGroup RegisterCallback(InputActionReference reference,
|
||||
Action<InputAction.CallbackContext> callback)
|
||||
{
|
||||
if (reference is null)
|
||||
{
|
||||
Debug.LogWarning($"未知的引用");
|
||||
return this;
|
||||
}
|
||||
|
||||
EnsureConfiguration();
|
||||
var action = reference.action.Clone();
|
||||
|
||||
actions
|
||||
.Where(x => x.name == action.name)
|
||||
.CreateOrAddIfEmety(actions, action)
|
||||
.ForEach(x =>
|
||||
{
|
||||
x.RegisterCallback(callback);
|
||||
});
|
||||
.Where(x => x.name == action.name)
|
||||
.CreateOrAddIfEmety(actions, action)
|
||||
.ForEach(x => { x.RegisterCallback(callback); });
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public void UnRegisterCallback(InputActionReference reference, Action<InputAction.CallbackContext> callback)
|
||||
{
|
||||
foreach (var action in actions.Where(x => x.name == reference.action.name))
|
||||
@@ -42,7 +44,8 @@ namespace BITKit
|
||||
action.UnRegisterCallback(callback);
|
||||
}
|
||||
}
|
||||
void EnsureConfiguration()
|
||||
|
||||
private void EnsureConfiguration()
|
||||
{
|
||||
if (state is not InitializationState.Initialized)
|
||||
{
|
||||
@@ -50,18 +53,21 @@ namespace BITKit
|
||||
state = InitializationState.Initialized;
|
||||
}
|
||||
}
|
||||
void Init()
|
||||
|
||||
private void Init()
|
||||
{
|
||||
if (allowGlobalActivation)
|
||||
BITInputSystem.AllowInput.AddListener(Listen);
|
||||
allowInput.AddListener(Allow);
|
||||
}
|
||||
void Listen(bool allowInput)
|
||||
|
||||
private void Listen(bool allowInput)
|
||||
{
|
||||
this.allowInput.SetElements(lockFile, allowInput);
|
||||
this.allowInput.SetDisableElements(lockFile, !allowInput);
|
||||
}
|
||||
void Allow(bool allow)
|
||||
|
||||
private void Allow(bool allow)
|
||||
{
|
||||
foreach (var action in actions)
|
||||
{
|
||||
@@ -74,6 +80,7 @@ namespace BITKit
|
||||
action.Disable();
|
||||
}
|
||||
}
|
||||
|
||||
isEnabled = allow;
|
||||
}
|
||||
|
||||
@@ -83,6 +90,7 @@ namespace BITKit
|
||||
{
|
||||
action.Disable();
|
||||
}
|
||||
|
||||
actions.Clear();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user