From b2a35ecf6ca1faed852b186243c27b6b34659f5a Mon Sep 17 00:00:00 2001 From: Evan Husted Date: Mon, 14 Oct 2024 15:03:09 -0500 Subject: [PATCH] misc: Small code improvements. --- README.md | 2 +- .../Collections/IntervalTree.cs | 2 +- .../HeadlessDynamicTextInputHandler.cs | 7 +- src/Ryujinx.Input.SDL2/SDL2Gamepad.cs | 140 ++++++++---------- src/Ryujinx.UI.Common/Helper/TitleHelper.cs | 15 +- .../UI/Windows/TitleUpdateWindow.axaml.cs | 17 +-- 6 files changed, 71 insertions(+), 112 deletions(-) diff --git a/README.md b/README.md index 6171c3232..b9b3af721 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ alt=""> - Discord
diff --git a/src/Ryujinx.Common/Collections/IntervalTree.cs b/src/Ryujinx.Common/Collections/IntervalTree.cs index d3a5e7fcf..c9bfe0e46 100644 --- a/src/Ryujinx.Common/Collections/IntervalTree.cs +++ b/src/Ryujinx.Common/Collections/IntervalTree.cs @@ -492,7 +492,7 @@ namespace Ryujinx.Common.Collections Start = start; End = end; Max = end; - Values = new List> { new RangeNode(start, end, value) }; + Values = [ new RangeNode(start, end, value) ]; Parent = parent; } } diff --git a/src/Ryujinx.Headless.SDL2/HeadlessDynamicTextInputHandler.cs b/src/Ryujinx.Headless.SDL2/HeadlessDynamicTextInputHandler.cs index 503874ff1..f44f5a9d0 100644 --- a/src/Ryujinx.Headless.SDL2/HeadlessDynamicTextInputHandler.cs +++ b/src/Ryujinx.Headless.SDL2/HeadlessDynamicTextInputHandler.cs @@ -17,11 +17,8 @@ namespace Ryujinx.Headless.SDL2 public bool TextProcessingEnabled { - get - { - return Volatile.Read(ref _canProcessInput); - } - + get => Volatile.Read(ref _canProcessInput); + set { Volatile.Write(ref _canProcessInput, value); diff --git a/src/Ryujinx.Input.SDL2/SDL2Gamepad.cs b/src/Ryujinx.Input.SDL2/SDL2Gamepad.cs index c8834fdea..fa341fe5e 100644 --- a/src/Ryujinx.Input.SDL2/SDL2Gamepad.cs +++ b/src/Ryujinx.Input.SDL2/SDL2Gamepad.cs @@ -12,7 +12,10 @@ namespace Ryujinx.Input.SDL2 { private bool HasConfiguration => _configuration != null; - private record struct ButtonMappingEntry(GamepadButtonInputId To, GamepadButtonInputId From); + private readonly record struct ButtonMappingEntry(GamepadButtonInputId To, GamepadButtonInputId From) + { + public bool IsValid => To is not GamepadButtonInputId.Unbound && From is not GamepadButtonInputId.Unbound; + } private StandardControllerInputConfig _configuration; @@ -144,86 +147,64 @@ namespace Ryujinx.Input.SDL2 public void Rumble(float lowFrequency, float highFrequency, uint durationMs) { - if (Features.HasFlag(GamepadFeaturesFlag.Rumble)) - { - ushort lowFrequencyRaw = (ushort)(lowFrequency * ushort.MaxValue); - ushort highFrequencyRaw = (ushort)(highFrequency * ushort.MaxValue); + if (!Features.HasFlag(GamepadFeaturesFlag.Rumble)) return; - if (durationMs == uint.MaxValue) - { - if (SDL_GameControllerRumble(_gamepadHandle, lowFrequencyRaw, highFrequencyRaw, SDL_HAPTIC_INFINITY) != 0) - { - Logger.Error?.Print(LogClass.Hid, "Rumble is not supported on this game controller."); - } - } - else if (durationMs > SDL_HAPTIC_INFINITY) - { - Logger.Error?.Print(LogClass.Hid, $"Unsupported rumble duration {durationMs}"); - } - else - { - if (SDL_GameControllerRumble(_gamepadHandle, lowFrequencyRaw, highFrequencyRaw, durationMs) != 0) - { - Logger.Error?.Print(LogClass.Hid, "Rumble is not supported on this game controller."); - } - } + ushort lowFrequencyRaw = (ushort)(lowFrequency * ushort.MaxValue); + ushort highFrequencyRaw = (ushort)(highFrequency * ushort.MaxValue); + + if (durationMs == uint.MaxValue) + { + if (SDL_GameControllerRumble(_gamepadHandle, lowFrequencyRaw, highFrequencyRaw, SDL_HAPTIC_INFINITY) != 0) + Logger.Error?.Print(LogClass.Hid, "Rumble is not supported on this game controller."); + } + else if (durationMs > SDL_HAPTIC_INFINITY) + { + Logger.Error?.Print(LogClass.Hid, $"Unsupported rumble duration {durationMs}"); + } + else + { + if (SDL_GameControllerRumble(_gamepadHandle, lowFrequencyRaw, highFrequencyRaw, durationMs) != 0) + Logger.Error?.Print(LogClass.Hid, "Rumble is not supported on this game controller."); } } public Vector3 GetMotionData(MotionInputId inputId) { - SDL_SensorType sensorType = SDL_SensorType.SDL_SENSOR_INVALID; - - if (inputId == MotionInputId.Accelerometer) + SDL_SensorType sensorType = inputId switch { - sensorType = SDL_SensorType.SDL_SENSOR_ACCEL; - } - else if (inputId == MotionInputId.Gyroscope) - { - sensorType = SDL_SensorType.SDL_SENSOR_GYRO; - } + MotionInputId.Accelerometer => SDL_SensorType.SDL_SENSOR_ACCEL, + MotionInputId.Gyroscope => SDL_SensorType.SDL_SENSOR_GYRO, + _ => SDL_SensorType.SDL_SENSOR_INVALID + }; - if (Features.HasFlag(GamepadFeaturesFlag.Motion) && sensorType != SDL_SensorType.SDL_SENSOR_INVALID) - { - const int ElementCount = 3; + if (!Features.HasFlag(GamepadFeaturesFlag.Motion) || sensorType is SDL_SensorType.SDL_SENSOR_INVALID) + return Vector3.Zero; - unsafe + const int ElementCount = 3; + + unsafe + { + float* values = stackalloc float[ElementCount]; + + int result = SDL_GameControllerGetSensorData(_gamepadHandle, sensorType, (IntPtr)values, ElementCount); + + if (result != 0) + return Vector3.Zero; + + Vector3 value = new(values[0], values[1], values[2]); + + return inputId switch { - float* values = stackalloc float[ElementCount]; - - int result = SDL_GameControllerGetSensorData(_gamepadHandle, sensorType, (IntPtr)values, ElementCount); - - if (result == 0) - { - Vector3 value = new(values[0], values[1], values[2]); - - if (inputId == MotionInputId.Gyroscope) - { - return RadToDegree(value); - } - - if (inputId == MotionInputId.Accelerometer) - { - return GsToMs2(value); - } - - return value; - } - } + MotionInputId.Gyroscope => RadToDegree(value), + MotionInputId.Accelerometer => GsToMs2(value), + _ => value + }; } - - return Vector3.Zero; } - private static Vector3 RadToDegree(Vector3 rad) - { - return rad * (180 / MathF.PI); - } + private static Vector3 RadToDegree(Vector3 rad) => rad * (180 / MathF.PI); - private static Vector3 GsToMs2(Vector3 gs) - { - return gs / SDL_STANDARD_GRAVITY; - } + private static Vector3 GsToMs2(Vector3 gs) => gs / SDL_STANDARD_GRAVITY; public void SetConfiguration(InputConfig configuration) { @@ -278,16 +259,13 @@ namespace Ryujinx.Input.SDL2 lock (_userMappingLock) { if (_buttonsUserMapping.Count == 0) - { return rawState; - } + + // ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator foreach (ButtonMappingEntry entry in _buttonsUserMapping) { - if (entry.From == GamepadButtonInputId.Unbound || entry.To == GamepadButtonInputId.Unbound) - { - continue; - } + if (!entry.IsValid) continue; // Do not touch state of button already pressed if (!result.IsPressed(entry.To)) @@ -316,9 +294,8 @@ namespace Ryujinx.Input.SDL2 public (float, float) GetStick(StickInputId inputId) { if (inputId == StickInputId.Unbound) - { return (0.0f, 0.0f); - } + (short stickX, short stickY) = GetStickXY(inputId); @@ -351,6 +328,7 @@ namespace Ryujinx.Input.SDL2 return (resultX, resultY); } + // ReSharper disable once InconsistentNaming private (short, short) GetStickXY(StickInputId inputId) => inputId switch { @@ -365,14 +343,12 @@ namespace Ryujinx.Input.SDL2 public bool IsPressed(GamepadButtonInputId inputId) { - if (inputId == GamepadButtonInputId.LeftTrigger) + switch (inputId) { - return ConvertRawStickValue(SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERLEFT)) > _triggerThreshold; - } - - if (inputId == GamepadButtonInputId.RightTrigger) - { - return ConvertRawStickValue(SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) > _triggerThreshold; + case GamepadButtonInputId.LeftTrigger: + return ConvertRawStickValue(SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERLEFT)) > _triggerThreshold; + case GamepadButtonInputId.RightTrigger: + return ConvertRawStickValue(SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) > _triggerThreshold; } if (_buttonsDriverMapping[(int)inputId] == SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_INVALID) diff --git a/src/Ryujinx.UI.Common/Helper/TitleHelper.cs b/src/Ryujinx.UI.Common/Helper/TitleHelper.cs index 8b47ac38b..f0d2fd78a 100644 --- a/src/Ryujinx.UI.Common/Helper/TitleHelper.cs +++ b/src/Ryujinx.UI.Common/Helper/TitleHelper.cs @@ -8,10 +8,8 @@ namespace Ryujinx.UI.Common.Helper public static string ActiveApplicationTitle(ProcessResult activeProcess, string applicationVersion, string pauseString = "") { if (activeProcess == null) - { - return String.Empty; - } - + return string.Empty; + string titleNameSection = string.IsNullOrWhiteSpace(activeProcess.Name) ? string.Empty : $" {activeProcess.Name}"; string titleVersionSection = string.IsNullOrWhiteSpace(activeProcess.DisplayVersion) ? string.Empty : $" v{activeProcess.DisplayVersion}"; string titleIdSection = $" ({activeProcess.ProgramIdText.ToUpper()})"; @@ -19,12 +17,9 @@ namespace Ryujinx.UI.Common.Helper string appTitle = $"Ryujinx {applicationVersion} -{titleNameSection}{titleVersionSection}{titleIdSection}{titleArchSection}"; - if (!string.IsNullOrEmpty(pauseString)) - { - appTitle += $" ({pauseString})"; - } - - return appTitle; + return !string.IsNullOrEmpty(pauseString) + ? appTitle + $" ({pauseString})" + : appTitle; } } } diff --git a/src/Ryujinx/UI/Windows/TitleUpdateWindow.axaml.cs b/src/Ryujinx/UI/Windows/TitleUpdateWindow.axaml.cs index d4d397c4a..c95aafbf4 100644 --- a/src/Ryujinx/UI/Windows/TitleUpdateWindow.axaml.cs +++ b/src/Ryujinx/UI/Windows/TitleUpdateWindow.axaml.cs @@ -1,6 +1,4 @@ -using Avalonia; using Avalonia.Controls; -using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Interactivity; using Avalonia.Styling; using FluentAvalonia.UI.Controls; @@ -64,21 +62,14 @@ namespace Ryujinx.Ava.UI.Windows private void OpenLocation(object sender, RoutedEventArgs e) { - if (sender is Button button) - { - if (button.DataContext is TitleUpdateModel model) - { - OpenHelper.LocateFile(model.Path); - } - } + if (sender is Button { DataContext: TitleUpdateModel model }) + OpenHelper.LocateFile(model.Path); } private void RemoveUpdate(object sender, RoutedEventArgs e) { - if (sender is Button button) - { - ViewModel.RemoveUpdate((TitleUpdateModel)button.DataContext); - } + if (sender is Button { DataContext: TitleUpdateModel model }) + ViewModel.RemoveUpdate(model); } private void RemoveAll(object sender, RoutedEventArgs e)