Net.Like.Xue.Tokyo/Assets/BITKit/Unity/Scripts/InputSystem/InputActionGroup.cs

183 lines
5.8 KiB
C#
Raw Normal View History

2024-11-03 16:42:23 +08:00
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using System.Linq;
namespace BITKit
{
2025-04-17 19:36:08 +08:00
[Serializable]
2024-11-03 16:42:23 +08:00
public class InputActionGroup : IDisposable
{
2025-04-17 19:36:08 +08:00
private class InputActionProxy
2024-11-03 16:42:23 +08:00
{
2025-04-17 19:36:08 +08:00
public event Action<InputAction.CallbackContext> Callback;
public void Invoke(InputAction.CallbackContext context)
{
try
{
Callback?.Invoke(context);
}
catch (OperationCanceledException)
{
}
}
2024-11-03 16:42:23 +08:00
}
2025-04-17 19:36:08 +08:00
private int _lockFile = Guid.NewGuid().GetHashCode();
2024-11-03 16:42:23 +08:00
public bool allowGlobalActivation = true;
[SerializeField, ReadOnly] private bool isEnabled;
2025-04-17 19:36:08 +08:00
private InitializationState _state = InitializationState.None;
2024-11-03 16:42:23 +08:00
public readonly ValidHandle allowInput = new();
2025-04-17 19:36:08 +08:00
private readonly ConcurrentDictionary<string,InputAction> _actions = new();
private readonly ConcurrentDictionary<int, InputActionProxy> _callbacks = new();
2024-11-03 16:42:23 +08:00
/// <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);
2025-04-17 19:36:08 +08:00
_callbacks[action.GetHashCode()].Callback += callback;
2024-11-03 16:42:23 +08:00
return this;
}
2025-02-24 23:03:39 +08:00
public InputActionGroup RegisterCallback(InputAction inputAction,Action<InputAction.CallbackContext> callback)
{
2025-04-17 19:36:08 +08:00
var action = EnsureCreated(inputAction);
_callbacks[action.GetHashCode()].Callback += callback;
2025-02-24 23:03:39 +08:00
return this;
}
2024-11-03 16:42:23 +08:00
public InputAction EnsureCreated(InputActionReference reference)
{
if (reference is null)
{
Debug.LogWarning($"未知的引用");
return null;
}
EnsureConfiguration();
2025-04-17 19:36:08 +08:00
var action = _actions.GetOrAdd(reference.name, _ =>
2024-11-03 16:42:23 +08:00
{
var newAction = reference.action.Clone();
newAction.Rename(reference.name);
2025-04-17 19:36:08 +08:00
var callback = _callbacks[newAction.GetHashCode()] = new();
newAction.performed += callback.Invoke;
newAction.canceled+=callback.Invoke;
newAction.started+=callback.Invoke;
2024-11-03 16:42:23 +08:00
return newAction;
});
allowInput.Invoke();
return action;
}
2025-02-24 23:03:39 +08:00
public InputAction EnsureCreated(InputAction inputAction)
{
EnsureConfiguration();
2025-04-17 19:36:08 +08:00
var action = _actions.GetOrAdd(inputAction.name, _ =>
2025-02-24 23:03:39 +08:00
{
var newAction = inputAction.Clone();
newAction.Rename(inputAction.name);
2025-04-17 19:36:08 +08:00
var callback = _callbacks[newAction.GetHashCode()] = new();
newAction.performed += callback.Invoke;
newAction.canceled+=callback.Invoke;
newAction.started+=callback.Invoke;
2025-02-24 23:03:39 +08:00
return newAction;
});
allowInput.Invoke();
return action;
}
2024-11-03 16:42:23 +08:00
public void Inherit(InputActionGroup other)
{
throw new NotImplementedException();
}
public InputAction GetAction(string name)
{
2025-04-17 19:36:08 +08:00
if(_actions.TryGetValue(name,out var action))
2024-11-03 16:42:23 +08:00
return action;
throw new ArgumentException($"未知的引用{name}");
}
public InputAction GetAction(InputActionReference reference)
{
2025-04-17 19:36:08 +08:00
if(_actions.TryGetValue(reference.name,out var action))
2024-11-03 16:42:23 +08:00
return action;
throw new ArgumentException($"未知的引用{reference.name}");
}
public void UnRegisterCallback(InputActionReference reference, Action<InputAction.CallbackContext> callback)
{
2025-04-17 19:36:08 +08:00
if (_actions.TryGetValue(reference.name, out var action))
{
_callbacks[action.GetHashCode()].Callback -= callback;
}
2024-11-03 16:42:23 +08:00
}
2025-02-24 23:03:39 +08:00
public void UnRegisterCallback(InputAction inputAction, Action<InputAction.CallbackContext> callback)
{
2025-04-17 19:36:08 +08:00
if (_actions.TryGetValue(inputAction.name, out var action))
{
_callbacks[action.GetHashCode()].Callback -= callback;
}
2025-02-24 23:03:39 +08:00
}
2024-11-03 16:42:23 +08:00
private void EnsureConfiguration()
{
2025-04-17 19:36:08 +08:00
if (_state is not InitializationState.Initialized)
2024-11-03 16:42:23 +08:00
{
Init();
2025-04-17 19:36:08 +08:00
_state = InitializationState.Initialized;
2024-11-03 16:42:23 +08:00
}
}
private void Init()
{
if (allowGlobalActivation)
BITInputSystem.AllowInput.AddListener(ListenGlobalInput);
allowInput.AddListener(AllowInput);
}
private void ListenGlobalInput(bool allowInput)
{
2025-04-17 19:36:08 +08:00
this.allowInput.SetDisableElements(_lockFile, !allowInput);
2024-11-03 16:42:23 +08:00
}
private void AllowInput(bool allow)
{
2025-04-17 19:36:08 +08:00
foreach (var action in _actions.Values)
2024-11-03 16:42:23 +08:00
{
if (allow)
{
action.Enable();
}
else
{
action.Disable();
}
}
isEnabled = allow;
}
public void Dispose()
{
2025-04-17 19:36:08 +08:00
foreach (var action in _actions.Values)
2024-11-03 16:42:23 +08:00
{
action.Disable();
action.Dispose();
}
2025-04-17 19:36:08 +08:00
_actions.Clear();
2024-11-03 16:42:23 +08:00
}
}
}