Simplify gameplay keyboard physical-key paths

This commit is contained in:
Babib3l
2026-03-19 20:45:48 +01:00
parent 327f90b420
commit 3cbe372b18
9 changed files with 120 additions and 136 deletions

View File

@@ -12,7 +12,7 @@ namespace Ryujinx.Ava.Input
{
internal class AvaloniaKeyboard : IKeyboard
{
private readonly List<ButtonMappingEntry> _buttonsUserMapping;
private readonly List<KeyboardInputMappingHelper.KeyboardButtonMapping> _buttonsUserMapping;
private readonly AvaloniaKeyboardDriver _driver;
private readonly KeyboardInputMode _mode;
private StandardKeyboardInputConfig _configuration;
@@ -25,12 +25,6 @@ namespace Ryujinx.Ava.Input
public bool IsConnected => true;
public GamepadFeaturesFlag Features => GamepadFeaturesFlag.None;
private readonly record struct ButtonMappingEntry(GamepadButtonInputId To, Key From)
{
public bool IsValid => To is not GamepadButtonInputId.Unbound && From is not Key.Unknown and not Key.Unbound;
}
public AvaloniaKeyboard(AvaloniaKeyboardDriver driver, string id, string name, KeyboardInputMode mode)
{
_buttonsUserMapping = [];
@@ -58,7 +52,7 @@ namespace Ryujinx.Ava.Input
return result;
}
foreach (ButtonMappingEntry entry in _buttonsUserMapping)
foreach (KeyboardInputMappingHelper.KeyboardButtonMapping entry in _buttonsUserMapping)
{
if (!entry.IsValid)
{
@@ -117,10 +111,7 @@ namespace Ryujinx.Ava.Input
_buttonsUserMapping.Clear();
foreach (KeyboardInputMappingHelper.KeyboardButtonMapping mapping in KeyboardInputMappingHelper.BuildButtonMappings(_configuration))
{
_buttonsUserMapping.Add(new ButtonMappingEntry(mapping.To, mapping.From));
}
_buttonsUserMapping.AddRange(KeyboardInputMappingHelper.BuildButtonMappings(_configuration));
}
}

View File

@@ -4,6 +4,7 @@ using Ryujinx.Ava.Common.Locale;
using Ryujinx.Input;
using System;
using System.Collections.Generic;
using ConfigPhysicalKey = Ryujinx.Common.Configuration.Hid.PhysicalKey;
using Key = Ryujinx.Input.Key;
namespace Ryujinx.Ava.Input
@@ -13,7 +14,7 @@ namespace Ryujinx.Ava.Input
private static readonly string[] _keyboardIdentifers = ["0"];
private readonly Control _control;
private readonly Dictionary<Key, int> _semanticPressedKeys;
private readonly Dictionary<Key, int> _physicalPressedKeys;
private readonly Dictionary<ConfigPhysicalKey, int> _physicalPressedKeys;
private readonly KeyboardInputMode _defaultMode;
public event EventHandler<KeyEventArgs> KeyPressed;
@@ -98,12 +99,21 @@ namespace Ryujinx.Ava.Input
return false;
}
return GetPressedKeys(mode).ContainsKey(key);
return mode == KeyboardInputMode.Physical
? _physicalPressedKeys.ContainsKey((ConfigPhysicalKey)(int)key)
: _semanticPressedKeys.ContainsKey(key);
}
internal void Clear(KeyboardInputMode mode)
{
GetPressedKeys(mode).Clear();
if (mode == KeyboardInputMode.Physical)
{
_physicalPressedKeys.Clear();
}
else
{
_semanticPressedKeys.Clear();
}
}
public void Clear()
@@ -112,11 +122,6 @@ namespace Ryujinx.Ava.Input
_physicalPressedKeys.Clear();
}
private Dictionary<Key, int> GetPressedKeys(KeyboardInputMode mode)
{
return mode == KeyboardInputMode.Physical ? _physicalPressedKeys : _semanticPressedKeys;
}
private static void UpdateKeyState(Dictionary<Key, int> pressedKeys, Key key, bool isPressed)
{
if (key is Key.Unknown or Key.Unbound)
@@ -151,24 +156,53 @@ namespace Ryujinx.Ava.Input
}
}
private void UpdateKeyStates(KeyEventArgs args, bool isPressed)
private static void UpdateKeyState(Dictionary<ConfigPhysicalKey, int> pressedKeys, ConfigPhysicalKey key, bool isPressed)
{
UpdateKeyState(_semanticPressedKeys, GetInputKey(args, KeyboardInputMode.Semantic), isPressed);
UpdateKeyState(_physicalPressedKeys, GetInputKey(args, KeyboardInputMode.Physical), isPressed);
}
private static Key GetInputKey(KeyEventArgs args, KeyboardInputMode mode)
{
if (mode == KeyboardInputMode.Physical)
if (key is ConfigPhysicalKey.Unknown or ConfigPhysicalKey.Unbound)
{
Key physicalKey = AvaloniaKeyboardMappingHelper.ToInputKey(args.PhysicalKey);
return physicalKey != Key.Unknown
? physicalKey
: AvaloniaKeyboardMappingHelper.ToInputKey(args.Key);
return;
}
return AvaloniaKeyboardMappingHelper.ToInputKey(args.PhysicalKey, args.Key);
if (isPressed)
{
if (pressedKeys.TryGetValue(key, out int count))
{
pressedKeys[key] = count + 1;
}
else
{
pressedKeys[key] = 1;
}
return;
}
if (pressedKeys.TryGetValue(key, out int currentCount))
{
if (currentCount <= 1)
{
pressedKeys.Remove(key);
}
else
{
pressedKeys[key] = currentCount - 1;
}
}
}
private void UpdateKeyStates(KeyEventArgs args, bool isPressed)
{
UpdateKeyState(_semanticPressedKeys, AvaloniaKeyboardMappingHelper.ToInputKey(args.PhysicalKey, args.Key), isPressed);
UpdateKeyState(_physicalPressedKeys, GetPhysicalInputKey(args), isPressed);
}
private static ConfigPhysicalKey GetPhysicalInputKey(KeyEventArgs args)
{
Key key = AvaloniaKeyboardMappingHelper.ToInputKey(args.PhysicalKey);
return key is >= Key.Unknown and < Key.Count
? (ConfigPhysicalKey)(int)key
: ConfigPhysicalKey.Unknown;
}
public void Dispose()

View File

@@ -154,42 +154,42 @@ namespace Ryujinx.Ava.UI.Models.Input
{
KeyboardStateSnapshot snapshot = keyboard.GetKeyboardStateSnapshot();
if (snapshot.IsPressed(KeyboardConfig.LeftStickRight.ToInputKey()))
if (snapshot.IsPressed(KeyboardConfig.LeftStickRight))
{
leftBuffer.Item1 += 1;
}
if (snapshot.IsPressed(KeyboardConfig.LeftStickLeft.ToInputKey()))
if (snapshot.IsPressed(KeyboardConfig.LeftStickLeft))
{
leftBuffer.Item1 -= 1;
}
if (snapshot.IsPressed(KeyboardConfig.LeftStickUp.ToInputKey()))
if (snapshot.IsPressed(KeyboardConfig.LeftStickUp))
{
leftBuffer.Item2 += 1;
}
if (snapshot.IsPressed(KeyboardConfig.LeftStickDown.ToInputKey()))
if (snapshot.IsPressed(KeyboardConfig.LeftStickDown))
{
leftBuffer.Item2 -= 1;
}
if (snapshot.IsPressed(KeyboardConfig.RightStickRight.ToInputKey()))
if (snapshot.IsPressed(KeyboardConfig.RightStickRight))
{
rightBuffer.Item1 += 1;
}
if (snapshot.IsPressed(KeyboardConfig.RightStickLeft.ToInputKey()))
if (snapshot.IsPressed(KeyboardConfig.RightStickLeft))
{
rightBuffer.Item1 -= 1;
}
if (snapshot.IsPressed(KeyboardConfig.RightStickUp.ToInputKey()))
if (snapshot.IsPressed(KeyboardConfig.RightStickUp))
{
rightBuffer.Item2 += 1;
}
if (snapshot.IsPressed(KeyboardConfig.RightStickDown.ToInputKey()))
if (snapshot.IsPressed(KeyboardConfig.RightStickDown))
{
rightBuffer.Item2 -= 1;
}