2025-08-08 16:25:09 +08:00
|
|
|
#if INPUTSYSTEM_SUPPORT
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections;
|
|
|
|
|
|
|
|
internal enum EHotkeyPressType
|
|
|
|
{
|
|
|
|
Started,
|
|
|
|
Performed
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static class UXHotkeyComponent
|
|
|
|
{
|
|
|
|
private readonly struct HotkeyRegistration
|
|
|
|
{
|
|
|
|
public readonly EHotkeyPressType pressType;
|
|
|
|
public readonly UXButton button;
|
|
|
|
|
2025-08-11 16:31:50 +08:00
|
|
|
public HotkeyRegistration(UXButton btn, EHotkeyPressType pressType)
|
2025-08-08 16:25:09 +08:00
|
|
|
{
|
|
|
|
button = btn;
|
|
|
|
this.pressType = pressType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static readonly Dictionary<string, List<HotkeyRegistration>> _hotkeyRegistry =
|
|
|
|
new Dictionary<string, List<HotkeyRegistration>>(32);
|
|
|
|
|
|
|
|
|
2025-08-11 16:31:50 +08:00
|
|
|
private static readonly Dictionary<string, (System.Action<InputAction.CallbackContext> handler, InputActionReference action)> _sharedHandlers =
|
|
|
|
new Dictionary<string, (System.Action<InputAction.CallbackContext>, InputActionReference)>(32);
|
2025-08-08 16:25:09 +08:00
|
|
|
|
|
|
|
|
2025-08-11 16:31:50 +08:00
|
|
|
private static readonly Dictionary<UXButton, string> _buttonRegistrations =
|
|
|
|
new Dictionary<UXButton, string>(64);
|
2025-08-08 16:25:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
#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(UXButton button, InputActionReference action, EHotkeyPressType pressType)
|
|
|
|
{
|
|
|
|
if (action == null || action.action == null || button == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
string actionId = action.action.id.ToString();
|
|
|
|
|
|
|
|
|
2025-08-11 16:31:50 +08:00
|
|
|
HotkeyRegistration registration = new HotkeyRegistration(button, pressType);
|
2025-08-08 16:25:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (!_hotkeyRegistry.TryGetValue(actionId, out var registrations))
|
|
|
|
{
|
|
|
|
registrations = new List<HotkeyRegistration>(4);
|
|
|
|
_hotkeyRegistry[actionId] = registrations;
|
|
|
|
}
|
|
|
|
|
|
|
|
registrations.Add(registration);
|
|
|
|
|
|
|
|
|
2025-08-11 16:31:50 +08:00
|
|
|
_buttonRegistrations[button] = actionId;
|
2025-08-08 16:25:09 +08:00
|
|
|
|
|
|
|
if (!_sharedHandlers.ContainsKey(actionId))
|
|
|
|
{
|
|
|
|
System.Action<InputAction.CallbackContext> handler = ctx => OnHotkeyTriggered(actionId);
|
2025-08-11 16:31:50 +08:00
|
|
|
_sharedHandlers[actionId] = (handler, action);
|
2025-08-08 16:25:09 +08:00
|
|
|
|
|
|
|
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(UXButton button)
|
|
|
|
{
|
2025-08-11 16:31:50 +08:00
|
|
|
if (button == null || !_buttonRegistrations.TryGetValue(button, out var actionId))
|
2025-08-08 16:25:09 +08:00
|
|
|
return;
|
|
|
|
|
2025-08-11 16:31:50 +08:00
|
|
|
|
|
|
|
if (_hotkeyRegistry.TryGetValue(actionId, out var registrations))
|
2025-08-08 16:25:09 +08:00
|
|
|
{
|
2025-08-11 16:31:50 +08:00
|
|
|
HotkeyRegistration hotkeyInfo;
|
|
|
|
for (int i = registrations.Count - 1; i >= 0; i--)
|
2025-08-08 16:25:09 +08:00
|
|
|
{
|
2025-08-11 16:31:50 +08:00
|
|
|
if (registrations[i].button == button)
|
2025-08-08 16:25:09 +08:00
|
|
|
{
|
2025-08-11 16:31:50 +08:00
|
|
|
hotkeyInfo = registrations[i];
|
|
|
|
registrations.RemoveAt(i);
|
2025-08-11 16:19:19 +08:00
|
|
|
|
2025-08-11 16:31:50 +08:00
|
|
|
if (registrations.Count == 0 && _sharedHandlers.TryGetValue(actionId, out var handlerInfo))
|
|
|
|
{
|
|
|
|
var (handler, actionRef) = handlerInfo;
|
|
|
|
if (actionRef != null && actionRef.action != null)
|
2025-08-08 16:25:09 +08:00
|
|
|
{
|
2025-08-11 16:31:50 +08:00
|
|
|
actionRef.action.Disable();
|
|
|
|
_sharedHandlers.Remove(actionId);
|
|
|
|
_hotkeyRegistry.Remove(actionId);
|
2025-08-08 16:25:09 +08:00
|
|
|
|
2025-08-11 16:31:50 +08:00
|
|
|
switch (hotkeyInfo.pressType)
|
2025-08-08 16:25:09 +08:00
|
|
|
{
|
2025-08-11 16:31:50 +08:00
|
|
|
case EHotkeyPressType.Started:
|
|
|
|
actionRef.action.started -= handler;
|
|
|
|
break;
|
|
|
|
case EHotkeyPressType.Performed:
|
|
|
|
actionRef.action.performed -= handler;
|
|
|
|
break;
|
2025-08-08 16:25:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-08-11 16:31:50 +08:00
|
|
|
|
|
|
|
break;
|
2025-08-08 16:25:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_buttonRegistrations.Remove(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void OnHotkeyTriggered(string actionId)
|
|
|
|
{
|
|
|
|
if (_hotkeyRegistry.TryGetValue(actionId, out var registrations) && registrations.Count > 0)
|
|
|
|
{
|
|
|
|
var registration = registrations[registrations.Count - 1];
|
|
|
|
registration.button.OnHotkeyTriggered();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class UXButtonHotkeyExtension
|
|
|
|
{
|
|
|
|
public static void BindHotKey(this UXButton button)
|
|
|
|
{
|
|
|
|
if (button == null) return;
|
|
|
|
|
|
|
|
#if INPUTSYSTEM_SUPPORT
|
|
|
|
if (button._hotKeyRefrence != null)
|
|
|
|
{
|
|
|
|
UXHotkeyComponent.RegisterHotkey(
|
|
|
|
button,
|
|
|
|
button._hotKeyRefrence,
|
|
|
|
button._hotkeyPressType
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void UnBindHotKey(this UXButton button)
|
|
|
|
{
|
|
|
|
if (button == null) return;
|
|
|
|
UXHotkeyComponent.UnregisterHotkey(button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|