com.alicizax.unity.ui.exten.../Runtime/UXComponent/Hotkey/UXHotkeyRegisterManager.cs
陈思海 551423f09d 大优化
大优化 懒得写了 重构了分离了 UXButton UXToggle UXSelectable UXGroup
2025-12-19 20:26:22 +08:00

177 lines
5.5 KiB
C#

#if INPUTSYSTEM_SUPPORT
using System;
using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections.Generic;
using UnityEngine.UI;
namespace UnityEngine.UI
{
internal enum EHotkeyPressType
{
Started,
Performed
}
internal static class UXHotkeyRegisterManager
{
private readonly struct HotkeyRegistration
{
public readonly EHotkeyPressType pressType;
public readonly IHotkeyTrigger button;
public HotkeyRegistration(IHotkeyTrigger btn, EHotkeyPressType pressType)
{
button = btn;
this.pressType = pressType;
}
}
private static readonly Dictionary<string, List<HotkeyRegistration>> _hotkeyRegistry = new(32);
private static readonly Dictionary<string, (Action<InputAction.CallbackContext> handler, InputActionReference action)> _sharedHandlers =
new(32);
private static readonly Dictionary<IHotkeyTrigger, string> _buttonRegistrations = new(64);
#if UNITY_EDITOR
[UnityEditor.Callbacks.DidReloadScripts]
internal static void ClearHotkeyRegistry()
{
foreach (var key in _buttonRegistrations.Keys)
{
UnregisterHotkey(key);
}
_sharedHandlers.Clear();
_hotkeyRegistry.Clear();
_buttonRegistrations.Clear();
}
#endif
internal static void RegisterHotkey(IHotkeyTrigger button, InputActionReference action, EHotkeyPressType pressType)
{
if (action == null || action.action == null || button == null)
return;
string actionId = action.action.id.ToString();
HotkeyRegistration registration = new HotkeyRegistration(button, pressType);
if (!_hotkeyRegistry.TryGetValue(actionId, out var registrations))
{
registrations = new List<HotkeyRegistration>(4);
_hotkeyRegistry[actionId] = registrations;
}
registrations.Add(registration);
_buttonRegistrations[button] = actionId;
if (!_sharedHandlers.ContainsKey(actionId))
{
Action<InputAction.CallbackContext> handler = ctx => OnHotkeyTriggered(actionId);
_sharedHandlers[actionId] = (handler, action);
switch (pressType)
{
case EHotkeyPressType.Started:
action.action.started += handler;
break;
case EHotkeyPressType.Performed:
action.action.performed += handler;
break;
}
action.action.Enable();
}
}
public static void UnregisterHotkey(IHotkeyTrigger button)
{
if (button == null || !_buttonRegistrations.TryGetValue(button, out var actionId))
return;
if (_hotkeyRegistry.TryGetValue(actionId, out var registrations))
{
HotkeyRegistration hotkeyInfo;
for (int i = registrations.Count - 1; i >= 0; i--)
{
if (registrations[i].button == button)
{
hotkeyInfo = registrations[i];
registrations.RemoveAt(i);
if (registrations.Count == 0 && _sharedHandlers.TryGetValue(actionId, out var handlerInfo))
{
var (handler, actionRef) = handlerInfo;
if (actionRef != null && actionRef.action != null)
{
actionRef.action.Disable();
_sharedHandlers.Remove(actionId);
_hotkeyRegistry.Remove(actionId);
switch (hotkeyInfo.pressType)
{
case EHotkeyPressType.Started:
actionRef.action.started -= handler;
break;
case EHotkeyPressType.Performed:
actionRef.action.performed -= handler;
break;
}
}
}
break;
}
}
}
_buttonRegistrations.Remove(button);
}
private static void OnHotkeyTriggered(string actionId)
{
if (_hotkeyRegistry.TryGetValue(actionId, out var registrations) && registrations.Count > 0)
{
var registration = registrations[^1];
registration.button.HotkeyActionTrigger();
}
}
}
}
public static class UXHotkeyHotkeyExtension
{
public static void BindHotKey(this IHotkeyTrigger button)
{
if (button == null) return;
if (button.HotkeyAction != null)
{
UXHotkeyRegisterManager.RegisterHotkey(
button,
button.HotkeyAction,
button.HotkeyPressType
);
}
}
public static void UnBindHotKey(this IHotkeyTrigger button)
{
if (button == null || button.HotkeyAction == null) return;
UXHotkeyRegisterManager.UnregisterHotkey(button);
}
}
#endif