From 2b0fce3e9d2cd551e150275b4a1af3caf9deaae7 Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Thu, 18 May 2023 22:35:23 -0400 Subject: [PATCH] Crimes against code --- src/Ryujinx.Ava/Assets/Locales/en_US.json | 12 +-- .../UI/Helpers/ButtonKeyAssigner.cs | 25 ++---- .../UI/Helpers/KeyValueConverter.cs | 44 +--------- .../UI/Views/Input/ControllerInputView.axaml | 88 +++++++++---------- .../Views/Input/ControllerInputView.axaml.cs | 79 ++++++++++++++++- .../Assigner/GamepadButtonAssigner.cs | 8 +- src/Ryujinx.Input/Assigner/IButtonAssigner.cs | 2 +- .../Assigner/KeyboardKeyAssigner.cs | 10 +-- 8 files changed, 147 insertions(+), 121 deletions(-) diff --git a/src/Ryujinx.Ava/Assets/Locales/en_US.json b/src/Ryujinx.Ava/Assets/Locales/en_US.json index e4c5a5ae8..d5c2d8cfc 100644 --- a/src/Ryujinx.Ava/Assets/Locales/en_US.json +++ b/src/Ryujinx.Ava/Assets/Locales/en_US.json @@ -346,12 +346,12 @@ "GamepadRightShoulder": "Right Shoulder", "GamepadLeftTrigger": "Left Trigger", "GamepadRightTrigger": "Right Trigger", - "GamepadDpadUp": "D-Pad Up", - "GamepadDpadDown": "D-Pad Down", - "GamepadDpadLeft": "D-Pad Left", - "GamepadDpadRight": "D-Pad Right", - "GamepadMinus": "Gamepad Minus", - "GamepadPlus": "Gamepad Plus", + "GamepadDpadUp": "Up", + "GamepadDpadDown": "Down", + "GamepadDpadLeft": "Left", + "GamepadDpadRight": "Right", + "GamepadMinus": "-", + "GamepadPlus": "+", "GamepadGuide": "Guide", "GamepadMisc1": "Misc", "GamepadPaddle1": "Paddle 1", diff --git a/src/Ryujinx.Ava/UI/Helpers/ButtonKeyAssigner.cs b/src/Ryujinx.Ava/UI/Helpers/ButtonKeyAssigner.cs index c48b83cde..a39e785c3 100644 --- a/src/Ryujinx.Ava/UI/Helpers/ButtonKeyAssigner.cs +++ b/src/Ryujinx.Ava/UI/Helpers/ButtonKeyAssigner.cs @@ -1,3 +1,5 @@ +using Avalonia; +using Avalonia.Data; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.LogicalTree; @@ -17,11 +19,13 @@ namespace Ryujinx.Ava.UI.Helpers { public ToggleButton Button { get; } public bool IsAssigned { get; } + public object Key { get; } - public ButtonAssignedEventArgs(ToggleButton button, bool isAssigned) + public ButtonAssignedEventArgs(ToggleButton button, bool isAssigned, object key) { Button = button; IsAssigned = isAssigned; + Key = key; } } @@ -79,15 +83,11 @@ namespace Ryujinx.Ava.UI.Helpers await Dispatcher.UIThread.InvokeAsync(() => { - string pressedButton = assigner.GetPressedButton(); + object pressedButton = assigner.GetPressedButton(); if (_shouldUnbind) { - SetButtonText(ToggledButton, LocaleManager.Instance[LocaleKeys.KeyUnbound]); - } - else if (pressedButton != "") - { - SetButtonText(ToggledButton, pressedButton); + pressedButton = null; } _shouldUnbind = false; @@ -95,17 +95,8 @@ namespace Ryujinx.Ava.UI.Helpers ToggledButton.IsChecked = false; - ButtonAssigned?.Invoke(this, new ButtonAssignedEventArgs(ToggledButton, pressedButton != null)); + ButtonAssigned?.Invoke(this, new ButtonAssignedEventArgs(ToggledButton, pressedButton != null, pressedButton)); - static void SetButtonText(ToggleButton button, string text) - { - ILogical textBlock = button.GetLogicalDescendants().First(x => x is TextBlock); - - if (textBlock != null && textBlock is TextBlock block) - { - block.Text = text; - } - } }); } diff --git a/src/Ryujinx.Ava/UI/Helpers/KeyValueConverter.cs b/src/Ryujinx.Ava/UI/Helpers/KeyValueConverter.cs index 81e61728d..1d8def0b8 100644 --- a/src/Ryujinx.Ava/UI/Helpers/KeyValueConverter.cs +++ b/src/Ryujinx.Ava/UI/Helpers/KeyValueConverter.cs @@ -164,49 +164,7 @@ namespace Ryujinx.Ava.UI.Helpers public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { - object key = null; - - if (value != null) - { - if (targetType == typeof(Key)) - { - var optionalKey = KeysMap.FirstOrOptional(x => LocaleManager.Instance[x.Value] == value.ToString()); - if (optionalKey.HasValue) - { - key = optionalKey.Value; - } - else - { - key = Enum.Parse(value.ToString()); - } - } - else if (targetType == typeof(GamepadInputId)) - { - var optionalKey = GamepadInputIdMap.FirstOrOptional(x => LocaleManager.Instance[x.Value] == value.ToString()); - if (optionalKey.HasValue) - { - key = optionalKey.Value; - } - else - { - key = Enum.Parse(value.ToString()); - } - } - else if (targetType == typeof(StickInputId)) - { - var optionalKey = StickInputIdMap.FirstOrOptional(x => LocaleManager.Instance[x.Value] == value.ToString()); - if (optionalKey.HasValue) - { - key = optionalKey.Value; - } - else - { - key = Enum.Parse(value.ToString()); - } - } - } - - return key; + throw new NotSupportedException(); } } } diff --git a/src/Ryujinx.Ava/UI/Views/Input/ControllerInputView.axaml b/src/Ryujinx.Ava/UI/Views/Input/ControllerInputView.axaml index e96773bfa..e7c1d54ca 100644 --- a/src/Ryujinx.Ava/UI/Views/Input/ControllerInputView.axaml +++ b/src/Ryujinx.Ava/UI/Views/Input/ControllerInputView.axaml @@ -74,9 +74,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsTriggerZL}" TextAlignment="Center" /> - + @@ -90,9 +90,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsTriggerL}" TextAlignment="Center" /> - + @@ -106,9 +106,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsButtonMinus}" TextAlignment="Center" /> - + @@ -140,9 +140,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsStickButton}" TextAlignment="Center" /> - + @@ -157,9 +157,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsStickStick}" TextAlignment="Center" /> - + @@ -250,9 +250,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsDPadUp}" TextAlignment="Center" /> - + @@ -267,9 +267,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsDPadDown}" TextAlignment="Center" /> - + @@ -284,9 +284,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsDPadLeft}" TextAlignment="Center" /> - + @@ -301,9 +301,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsDPadRight}" TextAlignment="Center" /> - + @@ -354,9 +354,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsLeftSR}" TextAlignment="Center" /> - + @@ -372,9 +372,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsLeftSL}" TextAlignment="Center" /> - + @@ -390,9 +390,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsRightSR}" TextAlignment="Center" /> - + @@ -408,9 +408,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsRightSL}" TextAlignment="Center" /> - + @@ -516,9 +516,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsTriggerZR}" TextAlignment="Center" /> - + @@ -534,9 +534,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsTriggerR}" TextAlignment="Center" /> - + @@ -552,9 +552,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsButtonPlus}" TextAlignment="Center" /> - + @@ -587,9 +587,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsButtonA}" TextAlignment="Center" /> - + @@ -604,9 +604,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsButtonB}" TextAlignment="Center" /> - + @@ -621,9 +621,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsButtonX}" TextAlignment="Center" /> - + @@ -638,9 +638,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsButtonY}" TextAlignment="Center" /> - + @@ -672,9 +672,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsStickButton}" TextAlignment="Center" /> - + @@ -690,9 +690,9 @@ VerticalAlignment="Center" Text="{locale:Locale ControllerSettingsStickStick}" TextAlignment="Center" /> - + diff --git a/src/Ryujinx.Ava/UI/Views/Input/ControllerInputView.axaml.cs b/src/Ryujinx.Ava/UI/Views/Input/ControllerInputView.axaml.cs index 998f717ec..65f32504d 100644 --- a/src/Ryujinx.Ava/UI/Views/Input/ControllerInputView.axaml.cs +++ b/src/Ryujinx.Ava/UI/Views/Input/ControllerInputView.axaml.cs @@ -8,6 +8,8 @@ using Ryujinx.Ava.UI.ViewModels.Input; using Ryujinx.Common.Configuration.Hid.Controller; using Ryujinx.Input; using Ryujinx.Input.Assigner; +using System; +using StickInputId = Ryujinx.Common.Configuration.Hid.Controller.StickInputId; namespace Ryujinx.Ava.UI.Views.Input { @@ -65,7 +67,82 @@ namespace Ryujinx.Ava.UI.Views.Input { if (e.IsAssigned) { - (DataContext as ControllerInputViewModel).parentModel.IsModified = true; + var viewModel = (DataContext as ControllerInputViewModel); + viewModel.parentModel.IsModified = true; + + // This is a crime against code but I can't think of anything better rn + switch (button.Name) + { + case "ButtonZl": + viewModel.Config.ButtonZl = (GamepadInputId)e.Key; + break; + case "ButtonL": + viewModel.Config.ButtonL = (GamepadInputId)e.Key; + break; + case "ButtonMinus": + viewModel.Config.ButtonMinus = (GamepadInputId)e.Key; + break; + case "LeftStickButton": + viewModel.Config.LeftStickButton = (GamepadInputId)e.Key; + break; + case "LeftJoystick": + viewModel.Config.LeftJoystick = (StickInputId)e.Key; + break; + case "DpadUp": + viewModel.Config.DpadUp = (GamepadInputId)e.Key; + break; + case "DpadDown": + viewModel.Config.DpadDown = (GamepadInputId)e.Key; + break; + case "DpadLeft": + viewModel.Config.DpadLeft = (GamepadInputId)e.Key; + break; + case "DpadRight": + viewModel.Config.DpadRight = (GamepadInputId)e.Key; + break; + case "LeftButtonSr": + viewModel.Config.LeftButtonSr = (GamepadInputId)e.Key; + break; + case "LeftButtonSl": + viewModel.Config.LeftButtonSl = (GamepadInputId)e.Key; + break; + case "RightButtonSr": + viewModel.Config.RightButtonSr = (GamepadInputId)e.Key; + break; + case "RightButtonSl": + viewModel.Config.RightButtonSl = (GamepadInputId)e.Key; + break; + case "ButtonZr": + viewModel.Config.ButtonZr = (GamepadInputId)e.Key; + break; + case "ButtonR": + viewModel.Config.ButtonR = (GamepadInputId)e.Key; + break; + case "ButtonPlus": + viewModel.Config.ButtonPlus = (GamepadInputId)e.Key; + break; + case "ButtonA": + viewModel.Config.ButtonA = (GamepadInputId)e.Key; + break; + case "ButtonB": + viewModel.Config.ButtonB = (GamepadInputId)e.Key; + break; + case "ButtonX": + viewModel.Config.ButtonX = (GamepadInputId)e.Key; + break; + case "ButtonY": + viewModel.Config.ButtonY = (GamepadInputId)e.Key; + break; + case "RightStickButton": + viewModel.Config.RightStickButton = (GamepadInputId)e.Key; + break; + case "RightJoystick": + viewModel.Config.RightJoystick = (StickInputId)e.Key; + break; + default: + Console.WriteLine(button.Name); + break; + } } }; diff --git a/src/Ryujinx.Input/Assigner/GamepadButtonAssigner.cs b/src/Ryujinx.Input/Assigner/GamepadButtonAssigner.cs index 388ebcc07..be583508c 100644 --- a/src/Ryujinx.Input/Assigner/GamepadButtonAssigner.cs +++ b/src/Ryujinx.Input/Assigner/GamepadButtonAssigner.cs @@ -59,16 +59,16 @@ namespace Ryujinx.Input.Assigner return _gamepad == null || !_gamepad.IsConnected; } - public string GetPressedButton() + public object GetPressedButton() { IEnumerable pressedButtons = _detector.GetPressedButtons(); if (pressedButtons.Any()) { - return !_forStick ? pressedButtons.First().ToString() : ((StickInputId)pressedButtons.First()).ToString(); + return !_forStick ? pressedButtons.First() : ((StickInputId)pressedButtons.First()); } - return ""; + return null; } private void CollectButtonStats() @@ -194,4 +194,4 @@ namespace Ryujinx.Input.Assigner } } } -} +} \ No newline at end of file diff --git a/src/Ryujinx.Input/Assigner/IButtonAssigner.cs b/src/Ryujinx.Input/Assigner/IButtonAssigner.cs index 76a9fece4..27f1c6b8d 100644 --- a/src/Ryujinx.Input/Assigner/IButtonAssigner.cs +++ b/src/Ryujinx.Input/Assigner/IButtonAssigner.cs @@ -31,6 +31,6 @@ namespace Ryujinx.Input.Assigner /// Get the pressed button that was read in by the button assigner. /// /// The pressed button that was read - string GetPressedButton(); + object GetPressedButton(); } } diff --git a/src/Ryujinx.Input/Assigner/KeyboardKeyAssigner.cs b/src/Ryujinx.Input/Assigner/KeyboardKeyAssigner.cs index e52ef4a2c..a4dde1987 100644 --- a/src/Ryujinx.Input/Assigner/KeyboardKeyAssigner.cs +++ b/src/Ryujinx.Input/Assigner/KeyboardKeyAssigner.cs @@ -23,7 +23,7 @@ namespace Ryujinx.Input.Assigner public bool HasAnyButtonPressed() { - return GetPressedButton().Length != 0; + return GetPressedButton() is not null; } public bool ShouldCancel() @@ -31,20 +31,20 @@ namespace Ryujinx.Input.Assigner return _keyboardState.IsPressed(Key.Escape); } - public string GetPressedButton() + public object GetPressedButton() { - string keyPressed = ""; + object keyPressed = null; for (Key key = Key.Unknown; key < Key.Count; key++) { if (_keyboardState.IsPressed(key)) { - keyPressed = key.ToString(); + keyPressed = key; break; } } - return !ShouldCancel() ? keyPressed : ""; + return !ShouldCancel() ? keyPressed : null; } } }