修改
This commit is contained in:
parent
d840d92225
commit
f970e2df5f
@ -14,14 +14,12 @@ internal static class UXHotkeyComponent
|
||||
{
|
||||
private readonly struct HotkeyRegistration
|
||||
{
|
||||
public readonly InputActionReference reference;
|
||||
public readonly EHotkeyPressType pressType;
|
||||
public readonly UXButton button;
|
||||
|
||||
public HotkeyRegistration(UXButton btn, InputActionReference reference, EHotkeyPressType pressType)
|
||||
public HotkeyRegistration(UXButton btn, EHotkeyPressType pressType)
|
||||
{
|
||||
button = btn;
|
||||
this.reference = reference;
|
||||
this.pressType = pressType;
|
||||
}
|
||||
}
|
||||
@ -30,12 +28,12 @@ internal static class UXHotkeyComponent
|
||||
new Dictionary<string, List<HotkeyRegistration>>(32);
|
||||
|
||||
|
||||
private static readonly Dictionary<string, (System.Action<InputAction.CallbackContext> handler, EHotkeyPressType pressType)> _sharedHandlers =
|
||||
new Dictionary<string, (System.Action<InputAction.CallbackContext>, EHotkeyPressType)>(32);
|
||||
private static readonly Dictionary<string, (System.Action<InputAction.CallbackContext> handler, InputActionReference action)> _sharedHandlers =
|
||||
new Dictionary<string, (System.Action<InputAction.CallbackContext>, InputActionReference)>(32);
|
||||
|
||||
|
||||
private static readonly Dictionary<UXButton, HashSet<string>> _buttonRegistrations =
|
||||
new Dictionary<UXButton, HashSet<string>>(64);
|
||||
private static readonly Dictionary<UXButton, string> _buttonRegistrations =
|
||||
new Dictionary<UXButton, string>(64);
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
@ -61,7 +59,7 @@ internal static class UXHotkeyComponent
|
||||
string actionId = action.action.id.ToString();
|
||||
|
||||
|
||||
HotkeyRegistration registration = new HotkeyRegistration(button, action, pressType);
|
||||
HotkeyRegistration registration = new HotkeyRegistration(button, pressType);
|
||||
|
||||
|
||||
if (!_hotkeyRegistry.TryGetValue(actionId, out var registrations))
|
||||
@ -73,18 +71,12 @@ internal static class UXHotkeyComponent
|
||||
registrations.Add(registration);
|
||||
|
||||
|
||||
if (!_buttonRegistrations.TryGetValue(button, out var actionIds))
|
||||
{
|
||||
actionIds = new HashSet<string>();
|
||||
_buttonRegistrations[button] = actionIds;
|
||||
}
|
||||
|
||||
actionIds.Add(actionId);
|
||||
_buttonRegistrations[button] = actionId;
|
||||
|
||||
if (!_sharedHandlers.ContainsKey(actionId))
|
||||
{
|
||||
System.Action<InputAction.CallbackContext> handler = ctx => OnHotkeyTriggered(actionId);
|
||||
_sharedHandlers[actionId] = (handler, pressType);
|
||||
_sharedHandlers[actionId] = (handler, action);
|
||||
|
||||
switch (pressType)
|
||||
{
|
||||
@ -102,49 +94,42 @@ internal static class UXHotkeyComponent
|
||||
|
||||
public static void UnregisterHotkey(UXButton button)
|
||||
{
|
||||
if (button == null || !_buttonRegistrations.TryGetValue(button, out var actionIds))
|
||||
if (button == null || !_buttonRegistrations.TryGetValue(button, out var actionId))
|
||||
return;
|
||||
|
||||
foreach (var actionId in actionIds)
|
||||
|
||||
if (_hotkeyRegistry.TryGetValue(actionId, out var registrations))
|
||||
{
|
||||
if (_hotkeyRegistry.TryGetValue(actionId, out var registrations))
|
||||
HotkeyRegistration hotkeyInfo;
|
||||
for (int i = registrations.Count - 1; i >= 0; i--)
|
||||
{
|
||||
HotkeyRegistration hotkeyInfo;
|
||||
for (int i = registrations.Count - 1; i >= 0; i--)
|
||||
if (registrations[i].button == button)
|
||||
{
|
||||
if (registrations[i].button == button)
|
||||
hotkeyInfo = registrations[i];
|
||||
registrations.RemoveAt(i);
|
||||
|
||||
if (registrations.Count == 0 && _sharedHandlers.TryGetValue(actionId, out var handlerInfo))
|
||||
{
|
||||
hotkeyInfo = registrations[i];
|
||||
registrations.RemoveAt(i);
|
||||
|
||||
if (_sharedHandlers.TryGetValue(actionId, out var handlerInfo))
|
||||
var (handler, actionRef) = handlerInfo;
|
||||
if (actionRef != null && actionRef.action != null)
|
||||
{
|
||||
var (handler, pressType) = handlerInfo;
|
||||
var actionRef = hotkeyInfo.reference;
|
||||
actionRef.action.Disable();
|
||||
_sharedHandlers.Remove(actionId);
|
||||
_hotkeyRegistry.Remove(actionId);
|
||||
|
||||
if (actionRef != null && actionRef.action != null)
|
||||
switch (hotkeyInfo.pressType)
|
||||
{
|
||||
if (registrations.Count == 0)
|
||||
{
|
||||
actionRef.action.Disable();
|
||||
_sharedHandlers.Remove(actionId);
|
||||
_hotkeyRegistry.Remove(actionId);
|
||||
}
|
||||
|
||||
switch (pressType)
|
||||
{
|
||||
case EHotkeyPressType.Started:
|
||||
actionRef.action.started -= handler;
|
||||
break;
|
||||
case EHotkeyPressType.Performed:
|
||||
actionRef.action.performed -= handler;
|
||||
break;
|
||||
}
|
||||
case EHotkeyPressType.Started:
|
||||
actionRef.action.started -= handler;
|
||||
break;
|
||||
case EHotkeyPressType.Performed:
|
||||
actionRef.action.performed -= handler;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user