From 72c3ca7769dcdde9b73d9892905d04335b93adc6 Mon Sep 17 00:00:00 2001 From: uncavo-hdmi Date: Sat, 22 Feb 2025 10:15:41 +0100 Subject: [PATCH] changed var name; fixed class name; removed hashset argument from GetConfiguredController function. --- src/Ryujinx/Assets/locales.json | 2 +- src/Ryujinx/Input/AutoAssignController.cs | 17 +- .../Input/ControllerAssignmentManager.cs | 414 +++++++++--------- 3 files changed, 215 insertions(+), 218 deletions(-) diff --git a/src/Ryujinx/Assets/locales.json b/src/Ryujinx/Assets/locales.json index 8688b95f8..aafa7200d 100644 --- a/src/Ryujinx/Assets/locales.json +++ b/src/Ryujinx/Assets/locales.json @@ -24173,4 +24173,4 @@ } } ] -} +} \ No newline at end of file diff --git a/src/Ryujinx/Input/AutoAssignController.cs b/src/Ryujinx/Input/AutoAssignController.cs index 7568d19ac..360d0af64 100644 --- a/src/Ryujinx/Input/AutoAssignController.cs +++ b/src/Ryujinx/Input/AutoAssignController.cs @@ -1,11 +1,6 @@ -using Ryujinx.Ava.UI.Models.Input; -using Ryujinx.Ava.UI.ViewModels; +using Ryujinx.Ava.UI.ViewModels; using Ryujinx.Ava.Utilities.Configuration; using Ryujinx.Common.Configuration.Hid; -using Ryujinx.Common.Configuration.Hid.Controller; -using Ryujinx.Common.Configuration.Hid.Controller.Motion; -using ConfigGamepadInputId = Ryujinx.Common.Configuration.Hid.Controller.GamepadInputId; -using ConfigStickInputId = Ryujinx.Common.Configuration.Hid.Controller.StickInputId; using Ryujinx.Common.Logging; using Ryujinx.Input; using Ryujinx.Input.HLE; @@ -20,7 +15,7 @@ namespace Ryujinx.Ava.Input private readonly InputManager _inputManager; private readonly MainWindowViewModel _viewModel; private readonly ConfigurationState _configurationState; - private readonly ControllerConfigurator _controllerConfigurator; + private readonly ControllerAssignmentManager _assignmentManager; public event Action ConfigurationUpdated; @@ -29,7 +24,7 @@ namespace Ryujinx.Ava.Input _inputManager = inputManager; _viewModel = mainWindowViewModel; _configurationState = ConfigurationState.Instance; - _controllerConfigurator = new ControllerConfigurator(); + _assignmentManager = new ControllerAssignmentManager(); _inputManager.GamepadDriver.OnGamepadConnected += HandleOnGamepadConnected; _inputManager.GamepadDriver.OnGamepadDisconnected += HandleOnGamepadDisconnected; @@ -56,8 +51,8 @@ namespace Ryujinx.Ava.Input List controllers = _inputManager.GamepadDriver.GetGamepads().ToList(); List oldConfig = _configurationState.Hid.InputConfig.Value.Where(x => x != null).ToList(); - List newConfig = _controllerConfigurator.GetConfiguredControllers( - controllers, oldConfig, new HashSet(), out bool hasNewControllersConnected); + List newConfig = _assignmentManager.GetConfiguredControllers( + controllers, oldConfig, out bool hasNewControllersConnected); _viewModel.AppHost?.NpadManager.ReloadConfiguration(newConfig, _configurationState.Hid.EnableKeyboard, _configurationState.Hid.EnableMouse); @@ -66,7 +61,7 @@ namespace Ryujinx.Ava.Input // there is no *new* controller, we must switch the order of the controllers in // oldConfig to match the new order since probably a controller was disconnected // or an old controller was reconnected - newConfig = _controllerConfigurator.ReorderControllers(newConfig, oldConfig); + newConfig = ControllerAssignmentManager.ReorderControllers(newConfig, oldConfig); } _configurationState.Hid.InputConfig.Value = newConfig; diff --git a/src/Ryujinx/Input/ControllerAssignmentManager.cs b/src/Ryujinx/Input/ControllerAssignmentManager.cs index 11f98a208..8b24cb073 100644 --- a/src/Ryujinx/Input/ControllerAssignmentManager.cs +++ b/src/Ryujinx/Input/ControllerAssignmentManager.cs @@ -10,83 +10,86 @@ using StickInputId = Ryujinx.Common.Configuration.Hid.Controller.StickInputId; namespace Ryujinx.Ava.Input { - public class ControllerConfigurator -{ - private readonly uint[] _playerColors = - [ - 0xFFFF0000, // Player 1 - Red - 0xFF0000FF, // Player 2 - Blue - 0xFF00FF00, // Player 3 - Green - 0xFFFFFF00, // Player 4 - Yellow - 0xFFFF00FF, // Player 5 - Magenta - 0xFFFFA500, // Player 6 - Orange - 0xFF00FFFF, // Player 7 - Cyan - 0xFF800080 // Player 8 - Purple - ]; - - private const int MaxControllers = 9; - - public List ReorderControllers(List newConfig, List oldConfig) + public class ControllerAssignmentManager { - List reorderedConfig = oldConfig.Select(config => new GamepadInputConfig(config).GetConfig()).ToList(); + private readonly uint[] _playerColors = + [ + 0xFFFF0000, // Player 1 - Red + 0xFF0000FF, // Player 2 - Blue + 0xFF00FF00, // Player 3 - Green + 0xFFFFFF00, // Player 4 - Yellow + 0xFFFF00FF, // Player 5 - Magenta + 0xFFFFA500, // Player 6 - Orange + 0xFF00FFFF, // Player 7 - Cyan + 0xFF800080 // Player 8 - Purple + ]; - foreach (var config in newConfig) + private const int MaxControllers = 9; + + public static List ReorderControllers(List newConfig, List oldConfig) { - InputConfig substitute = reorderedConfig.FirstOrDefault(x => x.Id == config.Id); - InputConfig toBeReplaced = reorderedConfig.FirstOrDefault(x => x.PlayerIndex == config.PlayerIndex); + List reorderedConfig = oldConfig.Select(config => new GamepadInputConfig(config).GetConfig()).ToList(); - if (substitute == null || toBeReplaced == null || substitute.PlayerIndex == toBeReplaced.PlayerIndex) continue; + foreach (var config in newConfig) + { + InputConfig substitute = reorderedConfig.FirstOrDefault(x => x.Id == config.Id); + InputConfig toBeReplaced = reorderedConfig.FirstOrDefault(x => x.PlayerIndex == config.PlayerIndex); - (substitute.PlayerIndex, toBeReplaced.PlayerIndex) = (toBeReplaced.PlayerIndex, substitute.PlayerIndex); + if (substitute == null || toBeReplaced == null || substitute.PlayerIndex == toBeReplaced.PlayerIndex) continue; + + (substitute.PlayerIndex, toBeReplaced.PlayerIndex) = (toBeReplaced.PlayerIndex, substitute.PlayerIndex); + } + + return reorderedConfig; + } + + public List GetConfiguredControllers( + List controllers, + List oldConfig, + out bool hasNewControllersConnected) + { + Dictionary oldConfigMap = oldConfig + .Where(c => c?.Id != null) + .ToDictionary(x => x.Id); + + Dictionary playerIndexMap = new(); + HashSet usedIndices = []; + int recognizedControllersCount = 0; + + List remainingControllers = controllers.Where(c => c?.Id != null).ToList(); + + // Add controllers with existing configurations + AddExistingControllers(remainingControllers, oldConfigMap, playerIndexMap, usedIndices, ref recognizedControllersCount); + + // Add new controllers + AddNewControllers(remainingControllers, playerIndexMap, usedIndices); + + List orderedConfigs = playerIndexMap + .OrderBy(x => x.Key) + .Select(x => x.Value) + .ToList(); + + // Update player indices and LED colors + UpdatePlayerIndicesAndLEDs(orderedConfigs); + + hasNewControllersConnected = controllers.Count > recognizedControllersCount; + return orderedConfigs; } - return reorderedConfig; - } - - public List GetConfiguredControllers( - List controllers, - List oldConfig, - HashSet usedIndices, - out bool hasNewControllersConnected) - { - Dictionary oldConfigMap = oldConfig - .Where(c => c?.Id != null) - .ToDictionary(x => x.Id); - - Dictionary playerIndexMap = new(); - int recognizedControllersCount = 0; - - List remainingControllers = controllers.Where(c => c?.Id != null).ToList(); - - // Add controllers with existing configurations - AddExistingControllers(remainingControllers, oldConfigMap, playerIndexMap, usedIndices, ref recognizedControllersCount); - - // Add new controllers - AddNewControllers(remainingControllers, playerIndexMap, usedIndices); - - List orderedConfigs = playerIndexMap - .OrderBy(x => x.Key) - .Select(x => x.Value) - .ToList(); - - // Update player indices and LED colors - UpdatePlayerIndicesAndLEDs(orderedConfigs); - - hasNewControllersConnected = controllers.Count > recognizedControllersCount; - return orderedConfigs; - } - - private void AddExistingControllers( - List controllers, - Dictionary oldConfigMap, - Dictionary playerIndexMap, - HashSet usedIndices, - ref int recognizedControllersCount) - { - foreach (var controller in controllers.ToList()) + private static void AddExistingControllers( + List controllers, + Dictionary oldConfigMap, + Dictionary playerIndexMap, + HashSet usedIndices, + ref int recognizedControllersCount) { - if (oldConfigMap.TryGetValue(controller.Id, out InputConfig existingConfig)) + foreach (var controller in controllers.ToList()) { + if (!oldConfigMap.TryGetValue(controller.Id, out InputConfig existingConfig)) + { + continue; + } + int desiredIndex = (int)existingConfig.PlayerIndex; // Ensure the index is valid and available @@ -94,7 +97,7 @@ namespace Ryujinx.Ava.Input { desiredIndex = GetFirstAvailableIndex(usedIndices); } - + if(desiredIndex == -1) continue; InputConfig config = new GamepadInputConfig(existingConfig).GetConfig(); @@ -106,159 +109,158 @@ namespace Ryujinx.Ava.Input controllers.Remove(controller); } } - } - private void AddNewControllers( - List controllers, - Dictionary playerIndexMap, - HashSet usedIndices) - { - foreach (var controller in controllers) + private static void AddNewControllers( + List controllers, + Dictionary playerIndexMap, + HashSet usedIndices) { - InputConfig config = CreateConfigFromController(controller); - int freeIndex = GetFirstAvailableIndex(usedIndices); - config.PlayerIndex = (PlayerIndex)freeIndex; - usedIndices.Add(freeIndex); - playerIndexMap[freeIndex] = config; - } - } - - private int GetFirstAvailableIndex(HashSet usedIndices) - { - for (int i = 0; i < MaxControllers; i++) - { - if (!usedIndices.Contains(i)) return i; - } - return -1; // Should not happen unless MaxControllers is exceeded - } - - private void UpdatePlayerIndicesAndLEDs(List orderedConfigs) - { - for (int index = 0; index < orderedConfigs.Count; index++) - { - orderedConfigs[index].PlayerIndex = (PlayerIndex)index; - - if (orderedConfigs[index] is not StandardControllerInputConfig standardConfig || - standardConfig.Led.UseRainbow) + foreach (var controller in controllers) { - continue; + InputConfig config = CreateConfigFromController(controller); + int freeIndex = GetFirstAvailableIndex(usedIndices); + config.PlayerIndex = (PlayerIndex)freeIndex; + usedIndices.Add(freeIndex); + playerIndexMap[freeIndex] = config; } - - Logger.Warning?.Print(LogClass.Application, $"Setting color for Player{index + 1}"); - standardConfig.Led = new LedConfigController - { - EnableLed = true, - LedColor = _playerColors[index] - }; } - } - private static InputConfig CreateConfigFromController(IGamepad controller) + private static int GetFirstAvailableIndex(HashSet usedIndices) { - if (controller == null) return null; - - Logger.Warning?.Print(LogClass.Application, $"Creating config for controller: {controller.Id}"); - - string id = controller.Id.Split(" ")[0]; - bool isNintendoStyle = controller.Name.Contains("Nintendo"); - ControllerType controllerType; - - if (isNintendoStyle && !controller.Name.Contains("(L/R)")) + for (int i = 0; i < MaxControllers; i++) { - if (controller.Name.Contains("(L)")) + if (!usedIndices.Contains(i)) return i; + } + return -1; // Should not happen unless MaxControllers is exceeded + } + + private void UpdatePlayerIndicesAndLEDs(List orderedConfigs) + { + for (int index = 0; index < orderedConfigs.Count; index++) + { + orderedConfigs[index].PlayerIndex = (PlayerIndex)index; + + if (orderedConfigs[index] is not StandardControllerInputConfig standardConfig || + standardConfig.Led.UseRainbow) { - controllerType = ControllerType.JoyconLeft; + continue; } - else if (controller.Name.Contains("(R)")) + + Logger.Warning?.Print(LogClass.Application, $"Setting color for Player{index + 1}"); + standardConfig.Led = new LedConfigController { - controllerType = ControllerType.JoyconRight; + EnableLed = true, + LedColor = _playerColors[index] + }; + } + } + + private static InputConfig CreateConfigFromController(IGamepad controller) + { + if (controller == null) return null; + + Logger.Warning?.Print(LogClass.Application, $"Creating config for controller: {controller.Id}"); + + string id = controller.Id.Split(" ")[0]; + bool isNintendoStyle = controller.Name.Contains("Nintendo"); + ControllerType controllerType; + + if (isNintendoStyle && !controller.Name.Contains("(L/R)")) + { + if (controller.Name.Contains("(L)")) + { + controllerType = ControllerType.JoyconLeft; + } + else if (controller.Name.Contains("(R)")) + { + controllerType = ControllerType.JoyconRight; + } + else + { + controllerType = ControllerType.ProController; + } } else { + // if it's not a nintendo controller, we assume it's a pro controller or a joy-con pair controllerType = ControllerType.ProController; } + + InputConfig config = new StandardControllerInputConfig + { + Version = InputConfig.CurrentVersion, + Backend = InputBackendType.GamepadSDL2, + Id = id, + ControllerType = controllerType, + DeadzoneLeft = 0.1f, + DeadzoneRight = 0.1f, + RangeLeft = 1.0f, + RangeRight = 1.0f, + TriggerThreshold = 0.5f, + LeftJoycon = new LeftJoyconCommonConfig + { + DpadUp = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.Y : GamepadInputId.DpadUp, + DpadDown = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.A : GamepadInputId.DpadDown, + DpadLeft = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.B : GamepadInputId.DpadLeft, + DpadRight = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.X : GamepadInputId.DpadRight, + ButtonMinus = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.Plus : GamepadInputId.Minus, + ButtonL = GamepadInputId.LeftShoulder, + ButtonZl = GamepadInputId.LeftTrigger, + ButtonSl = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.LeftShoulder : GamepadInputId.Unbound, + ButtonSr = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.RightShoulder : GamepadInputId.Unbound, + }, + LeftJoyconStick = new JoyconConfigControllerStick + { + Joystick = StickInputId.Left, + StickButton = GamepadInputId.LeftStick, + InvertStickX = false, + InvertStickY = false, + Rotate90CW = (controllerType == ControllerType.JoyconLeft), + }, + RightJoycon = new RightJoyconCommonConfig + { + ButtonA = GamepadInputId.B, + ButtonB = (controllerType == ControllerType.JoyconRight) ? GamepadInputId.Y : GamepadInputId.A, + ButtonX = (controllerType == ControllerType.JoyconRight) ? GamepadInputId.A : GamepadInputId.Y, + ButtonY = GamepadInputId.X, + ButtonPlus = GamepadInputId.Plus, + ButtonR = GamepadInputId.RightShoulder, + ButtonZr = GamepadInputId.RightTrigger, + ButtonSl = (controllerType == ControllerType.JoyconRight) ? GamepadInputId.LeftShoulder : GamepadInputId.Unbound, + ButtonSr = (controllerType == ControllerType.JoyconRight) ? GamepadInputId.RightShoulder : GamepadInputId.Unbound, + }, + RightJoyconStick = new JoyconConfigControllerStick + { + Joystick = (controllerType == ControllerType.JoyconRight) ? StickInputId.Left : StickInputId.Right, + StickButton = GamepadInputId.RightStick, + InvertStickX = (controllerType == ControllerType.JoyconRight), + InvertStickY = (controllerType == ControllerType.JoyconRight), + Rotate90CW = (controllerType == ControllerType.JoyconRight), + }, + Motion = new StandardMotionConfigController + { + MotionBackend = MotionInputBackendType.GamepadDriver, + EnableMotion = true, + Sensitivity = 100, + GyroDeadzone = 1, + }, + Rumble = new RumbleConfigController + { + StrongRumble = 1f, + WeakRumble = 1f, + EnableRumble = false, + }, + Led = new LedConfigController + { + EnableLed = true, + TurnOffLed = false, + UseRainbow = false, + LedColor = 0, + }, + }; + + return config; } - else - { - // if it's not a nintendo controller, we assume it's a pro controller or a joy-con pair - controllerType = ControllerType.ProController; - } - - InputConfig config = new StandardControllerInputConfig - { - Version = InputConfig.CurrentVersion, - Backend = InputBackendType.GamepadSDL2, - Id = id, - ControllerType = controllerType, - DeadzoneLeft = 0.1f, - DeadzoneRight = 0.1f, - RangeLeft = 1.0f, - RangeRight = 1.0f, - TriggerThreshold = 0.5f, - LeftJoycon = new LeftJoyconCommonConfig - { - DpadUp = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.Y : GamepadInputId.DpadUp, - DpadDown = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.A : GamepadInputId.DpadDown, - DpadLeft = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.B : GamepadInputId.DpadLeft, - DpadRight = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.X : GamepadInputId.DpadRight, - ButtonMinus = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.Plus : GamepadInputId.Minus, - ButtonL = GamepadInputId.LeftShoulder, - ButtonZl = GamepadInputId.LeftTrigger, - ButtonSl = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.LeftShoulder : GamepadInputId.Unbound, - ButtonSr = (controllerType == ControllerType.JoyconLeft) ? GamepadInputId.RightShoulder : GamepadInputId.Unbound, - }, - LeftJoyconStick = new JoyconConfigControllerStick - { - Joystick = StickInputId.Left, - StickButton = GamepadInputId.LeftStick, - InvertStickX = false, - InvertStickY = false, - Rotate90CW = (controllerType == ControllerType.JoyconLeft), - }, - RightJoycon = new RightJoyconCommonConfig - { - ButtonA = GamepadInputId.B, - ButtonB = (controllerType == ControllerType.JoyconRight) ? GamepadInputId.Y : GamepadInputId.A, - ButtonX = (controllerType == ControllerType.JoyconRight) ? GamepadInputId.A : GamepadInputId.Y, - ButtonY = GamepadInputId.X, - ButtonPlus = GamepadInputId.Plus, - ButtonR = GamepadInputId.RightShoulder, - ButtonZr = GamepadInputId.RightTrigger, - ButtonSl = (controllerType == ControllerType.JoyconRight) ? GamepadInputId.LeftShoulder : GamepadInputId.Unbound, - ButtonSr = (controllerType == ControllerType.JoyconRight) ? GamepadInputId.RightShoulder : GamepadInputId.Unbound, - }, - RightJoyconStick = new JoyconConfigControllerStick - { - Joystick = (controllerType == ControllerType.JoyconRight) ? StickInputId.Left : StickInputId.Right, - StickButton = GamepadInputId.RightStick, - InvertStickX = (controllerType == ControllerType.JoyconRight), - InvertStickY = (controllerType == ControllerType.JoyconRight), - Rotate90CW = (controllerType == ControllerType.JoyconRight), - }, - Motion = new StandardMotionConfigController - { - MotionBackend = MotionInputBackendType.GamepadDriver, - EnableMotion = true, - Sensitivity = 100, - GyroDeadzone = 1, - }, - Rumble = new RumbleConfigController - { - StrongRumble = 1f, - WeakRumble = 1f, - EnableRumble = false, - }, - Led = new LedConfigController - { - EnableLed = true, - TurnOffLed = false, - UseRainbow = false, - LedColor = 0, - }, - }; - - return config; - } -} + } }