BITKit/Src/Unity/Scripts/InputSystem/InputActionGroup.cs

162 lines
5.0 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System;
using System.Collections;
2023-11-15 23:55:06 +08:00
using System.Collections.Concurrent;
2023-06-05 19:57:17 +08:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using System.Linq;
2023-09-01 14:35:05 +08:00
2023-06-05 19:57:17 +08:00
namespace BITKit
{
[System.Serializable]
public class InputActionGroup : IDisposable
{
2024-03-31 23:31:00 +08:00
public override string ToString()
{
return $"Source:{Source}\nEnabled:{isEnabled}\nInitialized:{state}\nKeys:{string.Join("\n",actions.Keys)}";
}
public string Source;
2023-09-01 14:35:05 +08:00
private int lockFile = Guid.NewGuid().GetHashCode();
2023-06-05 19:57:17 +08:00
public bool allowGlobalActivation = true;
2023-09-01 14:35:05 +08:00
[SerializeField, ReadOnly] private bool isEnabled;
private InitializationState state = InitializationState.None;
2024-03-31 23:31:00 +08:00
public readonly ValidHandle allowInput = new();
2023-11-15 23:55:06 +08:00
private readonly ConcurrentDictionary<string,InputAction> actions = new();
2023-09-01 14:35:05 +08:00
2024-03-31 23:31:00 +08:00
/// <summary>
/// 注册所有(started,performed,canceled)回调
/// </summary>
/// <param name="reference"></param>
/// <param name="callback"></param>
/// <returns></returns>
2023-09-01 14:35:05 +08:00
public InputActionGroup RegisterCallback(InputActionReference reference,
Action<InputAction.CallbackContext> callback)
2024-03-31 23:31:00 +08:00
{
var action = EnsureCreated(reference);
action.RegisterCallback(callback);
return this;
}
2025-02-24 23:02:43 +08:00
public InputActionGroup RegisterCallback(InputAction inputAction,Action<InputAction.CallbackContext> callback)
{
EnsureConfiguration();
var action = actions.GetOrAdd(inputAction.name, _=>inputAction.Clone());
allowInput.Invoke();
action.RegisterCallback(callback);
return this;
}
2024-03-31 23:31:00 +08:00
public InputAction EnsureCreated(InputActionReference reference)
2023-06-05 19:57:17 +08:00
{
2023-06-07 02:02:14 +08:00
if (reference is null)
{
Debug.LogWarning($"未知的引用");
2024-03-31 23:31:00 +08:00
return null;
2023-06-07 02:02:14 +08:00
}
2023-09-01 14:35:05 +08:00
2023-06-05 19:57:17 +08:00
EnsureConfiguration();
2023-11-15 23:55:06 +08:00
var action = actions.GetOrAdd(reference.name, _ =>
{
var newAction = reference.action.Clone();
newAction.Rename(reference.name);
return newAction;
});
2023-06-05 19:57:17 +08:00
2024-03-31 23:31:00 +08:00
allowInput.Invoke();
2023-06-05 19:57:17 +08:00
2024-03-31 23:31:00 +08:00
return action;
2023-06-05 19:57:17 +08:00
}
2025-02-24 23:02:43 +08:00
public InputAction EnsureCreated(InputAction inputAction)
{
EnsureConfiguration();
var action = actions.GetOrAdd(inputAction.name, _ =>
{
var newAction = inputAction.Clone();
newAction.Rename(inputAction.name);
return newAction;
});
allowInput.Invoke();
return action;
}
2024-03-31 23:31:00 +08:00
2023-11-15 23:55:06 +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-05 19:57:17 +08:00
public void UnRegisterCallback(InputActionReference reference, Action<InputAction.CallbackContext> callback)
{
2023-11-15 23:55:06 +08:00
if(actions.TryGetValue(reference.name,out var action))
2023-06-05 19:57:17 +08:00
action.UnRegisterCallback(callback);
}
2023-09-01 14:35:05 +08:00
2025-02-24 23:02:43 +08:00
public void UnRegisterCallback(InputAction inputAction, Action<InputAction.CallbackContext> callback)
{
if(actions.TryGetValue(inputAction.name,out var action))
action.UnRegisterCallback(callback);
}
2023-09-01 14:35:05 +08:00
private void EnsureConfiguration()
2023-06-05 19:57:17 +08:00
{
if (state is not InitializationState.Initialized)
{
Init();
state = InitializationState.Initialized;
}
}
2023-09-01 14:35:05 +08:00
private void Init()
2023-06-05 19:57:17 +08:00
{
if (allowGlobalActivation)
2023-10-24 23:38:22 +08:00
BITInputSystem.AllowInput.AddListener(ListenGlobalInput);
allowInput.AddListener(AllowInput);
2023-06-05 19:57:17 +08:00
}
2023-09-01 14:35:05 +08:00
2023-10-24 23:38:22 +08:00
private void ListenGlobalInput(bool allowInput)
2023-06-05 19:57:17 +08:00
{
this.allowInput.SetDisableElements(lockFile, !allowInput);
}
2023-09-01 14:35:05 +08:00
2023-10-24 23:38:22 +08:00
private void AllowInput(bool allow)
2023-06-05 19:57:17 +08:00
{
2023-11-15 23:55:06 +08:00
foreach (var action in actions.Values)
2023-06-05 19:57:17 +08:00
{
if (allow)
{
action.Enable();
}
else
{
action.Disable();
}
}
isEnabled = allow;
}
public void Dispose()
{
2023-11-15 23:55:06 +08:00
foreach (var action in actions.Values)
2023-06-05 19:57:17 +08:00
{
action.Disable();
2023-11-15 23:55:06 +08:00
action.Dispose();
2023-06-05 19:57:17 +08:00
}
actions.Clear();
}
}
}