BITFALL/Assets/BITKit/Unity/Scripts/InputSystem/InputActionGroup.cs

111 lines
3.3 KiB
C#
Raw Normal View History

2023-06-08 14:09:50 +08:00
using System;
using System.Collections;
2023-11-15 23:54:54 +08:00
using System.Collections.Concurrent;
2023-06-08 14:09:50 +08:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using System.Linq;
2023-09-01 14:33:54 +08:00
2023-06-08 14:09:50 +08:00
namespace BITKit
{
[System.Serializable]
public class InputActionGroup : IDisposable
{
2023-09-01 14:33:54 +08:00
private int lockFile = Guid.NewGuid().GetHashCode();
2023-06-08 14:09:50 +08:00
public bool allowGlobalActivation = true;
2023-09-01 14:33:54 +08:00
[SerializeField, ReadOnly] private bool isEnabled;
private InitializationState state = InitializationState.None;
2023-06-08 14:09:50 +08:00
public ValidHandle allowInput = new();
2023-11-15 23:54:54 +08:00
private readonly ConcurrentDictionary<string,InputAction> actions = new();
2023-09-01 14:33:54 +08:00
public InputActionGroup RegisterCallback(InputActionReference reference,
Action<InputAction.CallbackContext> callback)
2023-06-08 14:09:50 +08:00
{
if (reference is null)
{
Debug.LogWarning($"未知的引用");
return this;
}
2023-09-01 14:33:54 +08:00
2023-06-08 14:09:50 +08:00
EnsureConfiguration();
2023-11-15 23:54:54 +08:00
var action = actions.GetOrAdd(reference.name, _ =>
{
var newAction = reference.action.Clone();
newAction.Rename(reference.name);
return newAction;
});
action.RegisterCallback(callback);
allowInput.Invoke();
2023-06-08 14:09:50 +08:00
return this;
}
2023-11-15 23:54:54 +08:00
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}");
}
2023-06-08 14:09:50 +08:00
public void UnRegisterCallback(InputActionReference reference, Action<InputAction.CallbackContext> callback)
{
2023-11-15 23:54:54 +08:00
if(actions.TryGetValue(reference.name,out var action))
2023-06-08 14:09:50 +08:00
action.UnRegisterCallback(callback);
}
2023-09-01 14:33:54 +08:00
private void EnsureConfiguration()
2023-06-08 14:09:50 +08:00
{
if (state is not InitializationState.Initialized)
{
Init();
state = InitializationState.Initialized;
}
}
2023-09-01 14:33:54 +08:00
private void Init()
2023-06-08 14:09:50 +08:00
{
if (allowGlobalActivation)
2023-10-24 23:37:59 +08:00
BITInputSystem.AllowInput.AddListener(ListenGlobalInput);
allowInput.AddListener(AllowInput);
2023-06-08 14:09:50 +08:00
}
2023-09-01 14:33:54 +08:00
2023-10-24 23:37:59 +08:00
private void ListenGlobalInput(bool allowInput)
2023-06-08 14:09:50 +08:00
{
this.allowInput.SetDisableElements(lockFile, !allowInput);
}
2023-09-01 14:33:54 +08:00
2023-10-24 23:37:59 +08:00
private void AllowInput(bool allow)
2023-06-08 14:09:50 +08:00
{
2023-11-15 23:54:54 +08:00
foreach (var action in actions.Values)
2023-06-08 14:09:50 +08:00
{
if (allow)
{
action.Enable();
}
else
{
action.Disable();
}
}
isEnabled = allow;
}
public void Dispose()
{
2023-11-15 23:54:54 +08:00
foreach (var action in actions.Values)
2023-06-08 14:09:50 +08:00
{
action.Disable();
2023-11-15 23:54:54 +08:00
action.Dispose();
2023-06-08 14:09:50 +08:00
}
actions.Clear();
}
}
}