From 2372c194f10d8f9ef7ea2dc415aa1613fbfc078a Mon Sep 17 00:00:00 2001 From: Ac_K Date: Tue, 6 Dec 2022 16:32:14 +0100 Subject: [PATCH] ava: Cleanup Input classes (#4042) * ava: Cleanup Input classes This PR just cleanup all Input classes for consistencies. * Addresses TSRBerry's feedback --- Ryujinx.Ava/Input/AvaloniaKeyboard.cs | 96 +++++++++---------- Ryujinx.Ava/Input/AvaloniaKeyboardDriver.cs | 34 +++---- ...er.cs => AvaloniaKeyboardMappingHelper.cs} | 14 ++- Ryujinx.Ava/Input/AvaloniaMouse.cs | 11 +-- Ryujinx.Ava/Input/AvaloniaMouseDriver.cs | 75 +++++++-------- .../Applet/AvaloniaDynamicTextInputHandler.cs | 4 +- .../Ui/ViewModels/MainWindowViewModel.cs | 6 +- 7 files changed, 112 insertions(+), 128 deletions(-) rename Ryujinx.Ava/Input/{AvaloniaMappingHelper.cs => AvaloniaKeyboardMappingHelper.cs} (92%) diff --git a/Ryujinx.Ava/Input/AvaloniaKeyboard.cs b/Ryujinx.Ava/Input/AvaloniaKeyboard.cs index 5b888bf5..d40ebbd2 100644 --- a/Ryujinx.Ava/Input/AvaloniaKeyboard.cs +++ b/Ryujinx.Ava/Input/AvaloniaKeyboard.cs @@ -4,7 +4,6 @@ using Ryujinx.Input; using System; using System.Collections.Generic; using System.Numerics; - using ConfigKey = Ryujinx.Common.Configuration.Hid.Key; using Key = Ryujinx.Input.Key; @@ -13,30 +12,37 @@ namespace Ryujinx.Ava.Input internal class AvaloniaKeyboard : IKeyboard { private readonly List _buttonsUserMapping; - private readonly AvaloniaKeyboardDriver _driver; + private readonly AvaloniaKeyboardDriver _driver; + private StandardKeyboardInputConfig _configuration; private readonly object _userMappingLock = new(); - private StandardKeyboardInputConfig _configuration; - - private bool HasConfiguration => _configuration != null; - - public string Id { get; } + public string Id { get; } public string Name { get; } - public bool IsConnected => true; + public bool IsConnected => true; + public GamepadFeaturesFlag Features => GamepadFeaturesFlag.None; - public GamepadFeaturesFlag Features => GamepadFeaturesFlag.None; + private class ButtonMappingEntry + { + public readonly Key From; + public readonly GamepadButtonInputId To; + + public ButtonMappingEntry(GamepadButtonInputId to, Key from) + { + To = to; + From = from; + } + } public AvaloniaKeyboard(AvaloniaKeyboardDriver driver, string id, string name) { - _driver = driver; - Id = id; - Name = name; _buttonsUserMapping = new List(); - } - public void Dispose() { } + _driver = driver; + Id = id; + Name = name; + } public KeyboardStateSnapshot GetKeyboardStateSnapshot() { @@ -46,11 +52,11 @@ namespace Ryujinx.Ava.Input public GamepadStateSnapshot GetMappedStateSnapshot() { KeyboardStateSnapshot rawState = GetKeyboardStateSnapshot(); - GamepadStateSnapshot result = default; + GamepadStateSnapshot result = default; lock (_userMappingLock) { - if (!HasConfiguration) + if (_configuration == null) { return result; } @@ -62,17 +68,17 @@ namespace Ryujinx.Ava.Input continue; } - // Do not touch state of the button already pressed + // NOTE: Do not touch state of the button already pressed. if (!result.IsPressed(entry.To)) { result.SetPressed(entry.To, rawState.IsPressed(entry.From)); } } - (short leftStickX, short leftStickY) = GetStickValues(ref rawState, _configuration.LeftJoyconStick); + (short leftStickX, short leftStickY) = GetStickValues(ref rawState, _configuration.LeftJoyconStick); (short rightStickX, short rightStickY) = GetStickValues(ref rawState, _configuration.RightJoyconStick); - result.SetStick(StickInputId.Left, ConvertRawStickValue(leftStickX), ConvertRawStickValue(leftStickY)); + result.SetStick(StickInputId.Left, ConvertRawStickValue(leftStickX), ConvertRawStickValue(leftStickY)); result.SetStick(StickInputId.Right, ConvertRawStickValue(rightStickX), ConvertRawStickValue(rightStickY)); } @@ -114,29 +120,29 @@ namespace Ryujinx.Ava.Input _buttonsUserMapping.Clear(); - // Left joycon - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.LeftStick, (Key)_configuration.LeftJoyconStick.StickButton)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.DpadUp, (Key)_configuration.LeftJoycon.DpadUp)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.DpadDown, (Key)_configuration.LeftJoycon.DpadDown)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.DpadLeft, (Key)_configuration.LeftJoycon.DpadLeft)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.DpadRight, (Key)_configuration.LeftJoycon.DpadRight)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.Minus, (Key)_configuration.LeftJoycon.ButtonMinus)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.LeftShoulder, (Key)_configuration.LeftJoycon.ButtonL)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.LeftTrigger, (Key)_configuration.LeftJoycon.ButtonZl)); + // Left JoyCon + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.LeftStick, (Key)_configuration.LeftJoyconStick.StickButton)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.DpadUp, (Key)_configuration.LeftJoycon.DpadUp)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.DpadDown, (Key)_configuration.LeftJoycon.DpadDown)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.DpadLeft, (Key)_configuration.LeftJoycon.DpadLeft)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.DpadRight, (Key)_configuration.LeftJoycon.DpadRight)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.Minus, (Key)_configuration.LeftJoycon.ButtonMinus)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.LeftShoulder, (Key)_configuration.LeftJoycon.ButtonL)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.LeftTrigger, (Key)_configuration.LeftJoycon.ButtonZl)); _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.SingleRightTrigger0, (Key)_configuration.LeftJoycon.ButtonSr)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.SingleLeftTrigger0, (Key)_configuration.LeftJoycon.ButtonSl)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.SingleLeftTrigger0, (Key)_configuration.LeftJoycon.ButtonSl)); - // Finally right joycon - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.RightStick, (Key)_configuration.RightJoyconStick.StickButton)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.A, (Key)_configuration.RightJoycon.ButtonA)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.B, (Key)_configuration.RightJoycon.ButtonB)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.X, (Key)_configuration.RightJoycon.ButtonX)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.Y, (Key)_configuration.RightJoycon.ButtonY)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.Plus, (Key)_configuration.RightJoycon.ButtonPlus)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.RightShoulder, (Key)_configuration.RightJoycon.ButtonR)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.RightTrigger, (Key)_configuration.RightJoycon.ButtonZr)); + // Right JoyCon + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.RightStick, (Key)_configuration.RightJoyconStick.StickButton)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.A, (Key)_configuration.RightJoycon.ButtonA)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.B, (Key)_configuration.RightJoycon.ButtonB)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.X, (Key)_configuration.RightJoycon.ButtonX)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.Y, (Key)_configuration.RightJoycon.ButtonY)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.Plus, (Key)_configuration.RightJoycon.ButtonPlus)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.RightShoulder, (Key)_configuration.RightJoycon.ButtonR)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.RightTrigger, (Key)_configuration.RightJoycon.ButtonZr)); _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.SingleRightTrigger1, (Key)_configuration.RightJoycon.ButtonSr)); - _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.SingleLeftTrigger1, (Key)_configuration.RightJoycon.ButtonSl)); + _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.SingleLeftTrigger1, (Key)_configuration.RightJoycon.ButtonSl)); } } @@ -190,16 +196,6 @@ namespace Ryujinx.Ava.Input _driver?.ResetKeys(); } - private class ButtonMappingEntry - { - public readonly Key From; - public readonly GamepadButtonInputId To; - - public ButtonMappingEntry(GamepadButtonInputId to, Key from) - { - To = to; - From = from; - } - } + public void Dispose() { } } } \ No newline at end of file diff --git a/Ryujinx.Ava/Input/AvaloniaKeyboardDriver.cs b/Ryujinx.Ava/Input/AvaloniaKeyboardDriver.cs index 910de186..31a53c32 100644 --- a/Ryujinx.Ava/Input/AvaloniaKeyboardDriver.cs +++ b/Ryujinx.Ava/Input/AvaloniaKeyboardDriver.cs @@ -4,7 +4,6 @@ using Ryujinx.Ava.Common.Locale; using Ryujinx.Input; using System; using System.Collections.Generic; - using AvaKey = Avalonia.Input.Key; using Key = Ryujinx.Input.Key; @@ -13,24 +12,23 @@ namespace Ryujinx.Ava.Input internal class AvaloniaKeyboardDriver : IGamepadDriver { private static readonly string[] _keyboardIdentifers = new string[1] { "0" }; - private readonly Control _control; + private readonly Control _control; private readonly HashSet _pressedKeys; public event EventHandler KeyPressed; public event EventHandler KeyRelease; - public event EventHandler TextInput; - - public string DriverName => "Avalonia"; + public event EventHandler TextInput; + public string DriverName => "AvaloniaKeyboardDriver"; public ReadOnlySpan GamepadsIds => _keyboardIdentifers; public AvaloniaKeyboardDriver(Control control) { - _control = control; + _control = control; _pressedKeys = new HashSet(); - _control.KeyDown += OnKeyPress; - _control.KeyUp += OnKeyRelease; + _control.KeyDown += OnKeyPress; + _control.KeyUp += OnKeyRelease; _control.TextInput += Control_TextInput; } @@ -41,21 +39,16 @@ namespace Ryujinx.Ava.Input public event Action OnGamepadConnected { - add { } + add { } remove { } } public event Action OnGamepadDisconnected { - add { } + add { } remove { } } - public void Dispose() - { - Dispose(true); - } - public IGamepad GetGamepad(string id) { if (!_keyboardIdentifers[0].Equals(id)) @@ -70,15 +63,13 @@ namespace Ryujinx.Ava.Input { if (disposing) { - _control.KeyUp -= OnKeyPress; + _control.KeyUp -= OnKeyPress; _control.KeyDown -= OnKeyRelease; } } protected void OnKeyPress(object sender, KeyEventArgs args) { - AvaKey key = args.Key; - _pressedKeys.Add(args.Key); KeyPressed?.Invoke(this, args); @@ -98,7 +89,7 @@ namespace Ryujinx.Ava.Input return false; } - AvaloniaMappingHelper.TryGetAvaKey(key, out var nativeKey); + AvaloniaKeyboardMappingHelper.TryGetAvaKey(key, out var nativeKey); return _pressedKeys.Contains(nativeKey); } @@ -107,5 +98,10 @@ namespace Ryujinx.Ava.Input { _pressedKeys.Clear(); } + + public void Dispose() + { + Dispose(true); + } } } \ No newline at end of file diff --git a/Ryujinx.Ava/Input/AvaloniaMappingHelper.cs b/Ryujinx.Ava/Input/AvaloniaKeyboardMappingHelper.cs similarity index 92% rename from Ryujinx.Ava/Input/AvaloniaMappingHelper.cs rename to Ryujinx.Ava/Input/AvaloniaKeyboardMappingHelper.cs index e1ef3ebc..8400ddad 100644 --- a/Ryujinx.Ava/Input/AvaloniaMappingHelper.cs +++ b/Ryujinx.Ava/Input/AvaloniaKeyboardMappingHelper.cs @@ -5,7 +5,7 @@ using AvaKey = Avalonia.Input.Key; namespace Ryujinx.Ava.Input { - internal static class AvaloniaMappingHelper + internal static class AvaloniaKeyboardMappingHelper { private static readonly AvaKey[] _keyMapping = new AvaKey[(int)Key.Count] { @@ -149,11 +149,11 @@ namespace Ryujinx.Ava.Input private static readonly Dictionary _avaKeyMapping; - static AvaloniaMappingHelper() + static AvaloniaKeyboardMappingHelper() { var inputKeys = Enum.GetValues(typeof(Key)); - // Avalonia.Input.Key is not contiguous and quite large, so use a dictionary instead of an array. + // NOTE: Avalonia.Input.Key is not contiguous and quite large, so use a dictionary instead of an array. _avaKeyMapping = new Dictionary(); foreach (var key in inputKeys) @@ -167,15 +167,13 @@ namespace Ryujinx.Ava.Input public static bool TryGetAvaKey(Key key, out AvaKey avaKey) { - var keyExist = (int)key < _keyMapping.Length; + avaKey = AvaKey.None; + + bool keyExist = (int)key < _keyMapping.Length; if (keyExist) { avaKey = _keyMapping[(int)key]; } - else - { - avaKey = AvaKey.None; - } return keyExist; } diff --git a/Ryujinx.Ava/Input/AvaloniaMouse.cs b/Ryujinx.Ava/Input/AvaloniaMouse.cs index a3ca2ff8..3a9c91c0 100644 --- a/Ryujinx.Ava/Input/AvaloniaMouse.cs +++ b/Ryujinx.Ava/Input/AvaloniaMouse.cs @@ -10,15 +10,12 @@ namespace Ryujinx.Ava.Input { private AvaloniaMouseDriver _driver; - public GamepadFeaturesFlag Features => throw new NotImplementedException(); - - public string Id => "0"; - + public string Id => "0"; public string Name => "AvaloniaMouse"; - public bool IsConnected => true; - - public bool[] Buttons => _driver.PressedButtons; + public bool IsConnected => true; + public GamepadFeaturesFlag Features => throw new NotImplementedException(); + public bool[] Buttons => _driver.PressedButtons; public AvaloniaMouse(AvaloniaMouseDriver driver) { diff --git a/Ryujinx.Ava/Input/AvaloniaMouseDriver.cs b/Ryujinx.Ava/Input/AvaloniaMouseDriver.cs index 9ad0310a..eb58752c 100644 --- a/Ryujinx.Ava/Input/AvaloniaMouseDriver.cs +++ b/Ryujinx.Ava/Input/AvaloniaMouseDriver.cs @@ -11,35 +11,50 @@ namespace Ryujinx.Ava.Input { internal class AvaloniaMouseDriver : IGamepadDriver { - private Control _widget; - private bool _isDisposed; - private Size _size; + private Control _widget; + private bool _isDisposed; + private Size _size; private readonly Window _window; - public bool[] PressedButtons { get; } - + public bool[] PressedButtons { get; } public Vector2 CurrentPosition { get; private set; } - public Vector2 Scroll { get; private set; } + public Vector2 Scroll { get; private set; } + + public string DriverName => "AvaloniaMouseDriver"; + public ReadOnlySpan GamepadsIds => new[] { "0" }; public AvaloniaMouseDriver(Window window, Control parent) { _widget = parent; _window = window; - _widget.PointerMoved += Parent_PointerMovedEvent; - _widget.PointerPressed += Parent_PointerPressEvent; - _widget.PointerReleased += Parent_PointerReleaseEvent; + _widget.PointerMoved += Parent_PointerMovedEvent; + _widget.PointerPressed += Parent_PointerPressEvent; + _widget.PointerReleased += Parent_PointerReleaseEvent; _widget.PointerWheelChanged += Parent_ScrollEvent; - _window.PointerMoved += Parent_PointerMovedEvent; - _window.PointerPressed += Parent_PointerPressEvent; - _window.PointerReleased += Parent_PointerReleaseEvent; + _window.PointerMoved += Parent_PointerMovedEvent; + _window.PointerPressed += Parent_PointerPressEvent; + _window.PointerReleased += Parent_PointerReleaseEvent; _window.PointerWheelChanged += Parent_ScrollEvent; PressedButtons = new bool[(int)MouseButton.Count]; _size = new Size((int)parent.Bounds.Width, (int)parent.Bounds.Height); - parent.GetObservable(Control.BoundsProperty).Subscribe(Resized); + + parent.GetObservable(Visual.BoundsProperty).Subscribe(Resized); + } + + public event Action OnGamepadConnected + { + add { } + remove { } + } + + public event Action OnGamepadDisconnected + { + add { } + remove { } } private void Resized(Rect rect) @@ -59,14 +74,12 @@ namespace Ryujinx.Ava.Input private void Parent_PointerPressEvent(object o, PointerPressedEventArgs args) { - var pointerProperties = args.GetCurrentPoint(_widget).Properties; - - PressedButtons[(int)pointerProperties.PointerUpdateKind] = true; + PressedButtons[(int)args.GetCurrentPoint(_widget).Properties.PointerUpdateKind] = true; } private void Parent_PointerMovedEvent(object o, PointerEventArgs args) { - var position = args.GetPosition(_widget); + Point position = args.GetPosition(_widget); CurrentPosition = new Vector2((float)position.X, (float)position.Y); } @@ -96,22 +109,6 @@ namespace Ryujinx.Ava.Input return _size; } - public string DriverName => "Avalonia"; - - public event Action OnGamepadConnected - { - add { } - remove { } - } - - public event Action OnGamepadDisconnected - { - add { } - remove { } - } - - public ReadOnlySpan GamepadsIds => new[] { "0" }; - public IGamepad GetGamepad(string id) { return new AvaloniaMouse(this); @@ -126,14 +123,14 @@ namespace Ryujinx.Ava.Input _isDisposed = true; - _widget.PointerMoved -= Parent_PointerMovedEvent; - _widget.PointerPressed -= Parent_PointerPressEvent; - _widget.PointerReleased -= Parent_PointerReleaseEvent; + _widget.PointerMoved -= Parent_PointerMovedEvent; + _widget.PointerPressed -= Parent_PointerPressEvent; + _widget.PointerReleased -= Parent_PointerReleaseEvent; _widget.PointerWheelChanged -= Parent_ScrollEvent; - _window.PointerMoved -= Parent_PointerMovedEvent; - _window.PointerPressed -= Parent_PointerPressEvent; - _window.PointerReleased -= Parent_PointerReleaseEvent; + _window.PointerMoved -= Parent_PointerMovedEvent; + _window.PointerPressed -= Parent_PointerPressEvent; + _window.PointerReleased -= Parent_PointerReleaseEvent; _window.PointerWheelChanged -= Parent_ScrollEvent; _widget = null; diff --git a/Ryujinx.Ava/Ui/Applet/AvaloniaDynamicTextInputHandler.cs b/Ryujinx.Ava/Ui/Applet/AvaloniaDynamicTextInputHandler.cs index 02a99c1d..ee0d435b 100644 --- a/Ryujinx.Ava/Ui/Applet/AvaloniaDynamicTextInputHandler.cs +++ b/Ryujinx.Ava/Ui/Applet/AvaloniaDynamicTextInputHandler.cs @@ -68,7 +68,7 @@ namespace Ryujinx.Ava.Ui.Applet private void AvaloniaDynamicTextInputHandler_KeyRelease(object sender, Avalonia.Input.KeyEventArgs e) { - var key = (HidKey)AvaloniaMappingHelper.ToInputKey(e.Key); + var key = (HidKey)AvaloniaKeyboardMappingHelper.ToInputKey(e.Key); if (!(KeyReleasedEvent?.Invoke(key)).GetValueOrDefault(true)) { @@ -88,7 +88,7 @@ namespace Ryujinx.Ava.Ui.Applet private void AvaloniaDynamicTextInputHandler_KeyPressed(object sender, KeyEventArgs e) { - var key = (HidKey)AvaloniaMappingHelper.ToInputKey(e.Key); + var key = (HidKey)AvaloniaKeyboardMappingHelper.ToInputKey(e.Key); if (!(KeyPressedEvent?.Invoke(key)).GetValueOrDefault(true)) { diff --git a/Ryujinx.Ava/Ui/ViewModels/MainWindowViewModel.cs b/Ryujinx.Ava/Ui/ViewModels/MainWindowViewModel.cs index f81afd2e..06513e37 100644 --- a/Ryujinx.Ava/Ui/ViewModels/MainWindowViewModel.cs +++ b/Ryujinx.Ava/Ui/ViewModels/MainWindowViewModel.cs @@ -883,17 +883,17 @@ namespace Ryujinx.Ava.Ui.ViewModels public void LoadConfigurableHotKeys() { - if (AvaloniaMappingHelper.TryGetAvaKey((Ryujinx.Input.Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ShowUi, out var showUiKey)) + if (AvaloniaKeyboardMappingHelper.TryGetAvaKey((Ryujinx.Input.Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ShowUi, out var showUiKey)) { ShowUiKey = new KeyGesture(showUiKey, KeyModifiers.None); } - if (AvaloniaMappingHelper.TryGetAvaKey((Ryujinx.Input.Key)ConfigurationState.Instance.Hid.Hotkeys.Value.Screenshot, out var screenshotKey)) + if (AvaloniaKeyboardMappingHelper.TryGetAvaKey((Ryujinx.Input.Key)ConfigurationState.Instance.Hid.Hotkeys.Value.Screenshot, out var screenshotKey)) { ScreenshotKey = new KeyGesture(screenshotKey, KeyModifiers.None); } - if (AvaloniaMappingHelper.TryGetAvaKey((Ryujinx.Input.Key)ConfigurationState.Instance.Hid.Hotkeys.Value.Pause, out var pauseKey)) + if (AvaloniaKeyboardMappingHelper.TryGetAvaKey((Ryujinx.Input.Key)ConfigurationState.Instance.Hid.Hotkeys.Value.Pause, out var pauseKey)) { PauseKey = new KeyGesture(pauseKey, KeyModifiers.None); }