diff --git a/Ryujinx.Common/Input/GamePadButton.cs b/Ryujinx.Common/Input/GamePadButton.cs new file mode 100644 index 0000000000..5d78eb0de5 --- /dev/null +++ b/Ryujinx.Common/Input/GamePadButton.cs @@ -0,0 +1,28 @@ +namespace Ryujinx.Common.Input +{ + public enum GamePadButton + { + A, + B, + X, + Y, + LStick, + RStick, + LShoulder, + RShoulder, + DPadUp, + DPadDown, + DPadLeft, + DPadRight, + Start, + Back, + LTrigger, + RTrigger, + } + + public enum GamePadStick + { + LJoystick, + RJoystick + } +} diff --git a/Ryujinx.Common/Input/JoyConController.cs b/Ryujinx.Common/Input/JoyConController.cs index b7754e6a09..a22a52d352 100644 --- a/Ryujinx.Common/Input/JoyConController.cs +++ b/Ryujinx.Common/Input/JoyConController.cs @@ -2,28 +2,28 @@ { public struct JoyConControllerLeft { - public string Stick; - public string StickButton; - public string DPadUp; - public string DPadDown; - public string DPadLeft; - public string DPadRight; - public string ButtonMinus; - public string ButtonL; - public string ButtonZL; + public GamePadStick Stick; + public GamePadButton StickButton; + public GamePadButton DPadUp; + public GamePadButton DPadDown; + public GamePadButton DPadLeft; + public GamePadButton DPadRight; + public GamePadButton ButtonMinus; + public GamePadButton ButtonL; + public GamePadButton ButtonZL; } public struct JoyConControllerRight { - public string Stick; - public string StickButton; - public string ButtonA; - public string ButtonB; - public string ButtonX; - public string ButtonY; - public string ButtonPlus; - public string ButtonR; - public string ButtonZR; + public GamePadStick Stick; + public GamePadButton StickButton; + public GamePadButton ButtonA; + public GamePadButton ButtonB; + public GamePadButton ButtonX; + public GamePadButton ButtonY; + public GamePadButton ButtonPlus; + public GamePadButton ButtonR; + public GamePadButton ButtonZR; } public struct JoyConController diff --git a/Ryujinx.UI/Config.cs b/Ryujinx.UI/Config.cs index 949541f5f5..66e2ff301f 100644 --- a/Ryujinx.UI/Config.cs +++ b/Ryujinx.UI/Config.cs @@ -15,10 +15,10 @@ namespace Ryujinx public static JoyConKeyboard JoyConKeyboard { get; set; } public static JoyConController JoyConController { get; set; } - public static float GamePadDeadzone { get; private set; } - public static bool GamePadEnable { get; private set; } - public static int GamePadIndex { get; private set; } - public static float GamePadTriggerThreshold { get; private set; } + public static float GamePadDeadzone { get; set; } + public static bool GamePadEnable { get; set; } + public static int GamePadIndex { get; set; } + public static float GamePadTriggerThreshold { get; set; } public static string IniPath { get; set; } @@ -112,28 +112,28 @@ namespace Ryujinx { Left = new JoyConControllerLeft { - Stick = Parser.GetValue("Controls_Left_JoyConController_Stick"), - StickButton = Parser.GetValue("Controls_Left_JoyConController_Stick_Button"), - DPadUp = Parser.GetValue("Controls_Left_JoyConController_DPad_Up"), - DPadDown = Parser.GetValue("Controls_Left_JoyConController_DPad_Down"), - DPadLeft = Parser.GetValue("Controls_Left_JoyConController_DPad_Left"), - DPadRight = Parser.GetValue("Controls_Left_JoyConController_DPad_Right"), - ButtonMinus = Parser.GetValue("Controls_Left_JoyConController_Button_Minus"), - ButtonL = Parser.GetValue("Controls_Left_JoyConController_Button_L"), - ButtonZL = Parser.GetValue("Controls_Left_JoyConController_Button_ZL") + Stick = Enum.Parse(Parser.GetValue("Controls_Left_JoyConController_Stick")), + StickButton = Enum.Parse(Parser.GetValue("Controls_Left_JoyConController_Stick_Button")), + DPadUp = Enum.Parse(Parser.GetValue("Controls_Left_JoyConController_DPad_Up")), + DPadDown = Enum.Parse(Parser.GetValue("Controls_Left_JoyConController_DPad_Down")), + DPadLeft = Enum.Parse(Parser.GetValue("Controls_Left_JoyConController_DPad_Left")), + DPadRight = Enum.Parse(Parser.GetValue("Controls_Left_JoyConController_DPad_Right")), + ButtonMinus = Enum.Parse(Parser.GetValue("Controls_Left_JoyConController_Button_Minus")), + ButtonL = Enum.Parse(Parser.GetValue("Controls_Left_JoyConController_Button_L")), + ButtonZL = Enum.Parse(Parser.GetValue("Controls_Left_JoyConController_Button_ZL")) }, Right = new JoyConControllerRight { - Stick = Parser.GetValue("Controls_Right_JoyConController_Stick"), - StickButton = Parser.GetValue("Controls_Right_JoyConController_Stick_Button"), - ButtonA = Parser.GetValue("Controls_Right_JoyConController_Button_A"), - ButtonB = Parser.GetValue("Controls_Right_JoyConController_Button_B"), - ButtonX = Parser.GetValue("Controls_Right_JoyConController_Button_X"), - ButtonY = Parser.GetValue("Controls_Right_JoyConController_Button_Y"), - ButtonPlus = Parser.GetValue("Controls_Right_JoyConController_Button_Plus"), - ButtonR = Parser.GetValue("Controls_Right_JoyConController_Button_R"), - ButtonZR = Parser.GetValue("Controls_Right_JoyConController_Button_ZR") + Stick = Enum.Parse(Parser.GetValue("Controls_Right_JoyConController_Stick")), + StickButton = Enum.Parse(Parser.GetValue("Controls_Right_JoyConController_Stick_Button")), + ButtonA = Enum.Parse(Parser.GetValue("Controls_Right_JoyConController_Button_A")), + ButtonB = Enum.Parse(Parser.GetValue("Controls_Right_JoyConController_Button_B")), + ButtonX = Enum.Parse(Parser.GetValue("Controls_Right_JoyConController_Button_X")), + ButtonY = Enum.Parse(Parser.GetValue("Controls_Right_JoyConController_Button_Y")), + ButtonPlus = Enum.Parse(Parser.GetValue("Controls_Right_JoyConController_Button_Plus")), + ButtonR = Enum.Parse(Parser.GetValue("Controls_Right_JoyConController_Button_R")), + ButtonZR = Enum.Parse(Parser.GetValue("Controls_Right_JoyConController_Button_ZR")) } }; diff --git a/Ryujinx.UI/Extensions/ControlArchive.cs b/Ryujinx.UI/Extensions/ControlArchive.cs index 4e7cbdec4a..e478383716 100644 --- a/Ryujinx.UI/Extensions/ControlArchive.cs +++ b/Ryujinx.UI/Extensions/ControlArchive.cs @@ -1,17 +1,15 @@ -using System; -using System.Collections.Generic; +using System.IO; using System.Text; -using System.IO; namespace Ryujinx.UI { public class ControlArchive { public LanguageEntry[] LanguageEntries { get; set; } - public long ApplicationTitleID { get; set; } - public long BaseTitleID { get; set; } - public long ProductCode { get; set; } - public string ApplicationVersion { get; set; } + public long ApplicationTitleID { get; set; } + public long BaseTitleID { get; set; } + public long ProductCode { get; set; } + public string ApplicationVersion { get; set; } public ControlArchive(Stream Input) { @@ -22,7 +20,7 @@ namespace Ryujinx.UI Input.Seek(0x3060, SeekOrigin.Begin); ApplicationVersion = Encoding.ASCII.GetString(Reader.ReadBytes(0x10)); - BaseTitleID = Reader.ReadInt64(); + BaseTitleID = Reader.ReadInt64(); ApplicationTitleID = Reader.ReadInt64(); Input.Seek(0x30a8, SeekOrigin.Begin); @@ -38,7 +36,7 @@ namespace Ryujinx.UI LanguageEntries[index] = new LanguageEntry() { AplicationName = Encoding.ASCII.GetString(LanguageReader.ReadBytes(0x200)).Trim('\0'), - DeveloperName = Encoding.ASCII.GetString(LanguageReader.ReadBytes(0x100)).Trim('\0') + DeveloperName = Encoding.ASCII.GetString(LanguageReader.ReadBytes(0x100)).Trim('\0') }; } } diff --git a/Ryujinx.UI/Extensions/DialogResult.cs b/Ryujinx.UI/Extensions/DialogResult.cs index 515cbb1a5a..17827c9f9f 100644 --- a/Ryujinx.UI/Extensions/DialogResult.cs +++ b/Ryujinx.UI/Extensions/DialogResult.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; - -public enum DialogResult +public enum DialogResult { OK, Cancel, diff --git a/Ryujinx.UI/Extensions/Nro.cs b/Ryujinx.UI/Extensions/Nro.cs index 167cc7e8ff..e37ce87d3f 100644 --- a/Ryujinx.UI/Extensions/Nro.cs +++ b/Ryujinx.UI/Extensions/Nro.cs @@ -1,17 +1,15 @@ using System; -using System.Collections.Generic; -using System.Text; using System.IO; -using Ryujinx.HLE.Loaders; +using System.Text; namespace Ryujinx.UI { class Nro : HLE.Loaders.Executables.Nro { - public byte[] AssetRomfData { get; set; } - public byte[] IconData { get; set; } - private byte[] NACPData { get; set; } - public int AssetOffset { get; set; } + public byte[] AssetRomfData { get; set; } + public byte[] IconData { get; set; } + private byte[] NACPData { get; set; } + public int AssetOffset { get; set; } public ControlArchive ControlArchive { get; set; } @@ -36,18 +34,18 @@ namespace Ryujinx.UI { Input.Seek(AssetOffset, SeekOrigin.Begin); - int AssetMagic0 = Reader.ReadInt32(); - int AssetFormat = Reader.ReadInt32(); - byte[] IconSectionInfo = Reader.ReadBytes(0x10); - byte[] NACPSectionInfo = Reader.ReadBytes(0x10); + int AssetMagic0 = Reader.ReadInt32(); + int AssetFormat = Reader.ReadInt32(); + byte[] IconSectionInfo = Reader.ReadBytes(0x10); + byte[] NACPSectionInfo = Reader.ReadBytes(0x10); byte[] AssetRomfSectionInfo = Reader.ReadBytes(0x10); long IconOffset = BitConverter.ToInt64(IconSectionInfo, 0); - long IconSize = BitConverter.ToInt64(IconSectionInfo, 8); + long IconSize = BitConverter.ToInt64(IconSectionInfo, 8); long NACPOffset = BitConverter.ToInt64(NACPSectionInfo, 0); - long NACPSize = BitConverter.ToInt64(NACPSectionInfo, 8); + long NACPSize = BitConverter.ToInt64(NACPSectionInfo, 8); long RomfOffset = BitConverter.ToInt64(AssetRomfSectionInfo, 0); - long RomfSize = BitConverter.ToInt64(AssetRomfSectionInfo, 8); + long RomfSize = BitConverter.ToInt64(AssetRomfSectionInfo, 8); Input.Seek(AssetOffset + IconOffset, SeekOrigin.Begin); IconData = Reader.ReadBytes((int)IconSize); diff --git a/Ryujinx.UI/GUI/EmulationWindow.cs b/Ryujinx.UI/GUI/EmulationWindow.cs index 89bc47dcc0..5dc1f75b72 100644 --- a/Ryujinx.UI/GUI/EmulationWindow.cs +++ b/Ryujinx.UI/GUI/EmulationWindow.cs @@ -153,7 +153,7 @@ namespace Ryujinx.UI protected override void OnUpdateFrame(FrameEventArgs e) { - KeyboardState Keyboard = this.Keyboard.HasValue ? this.Keyboard.Value : new KeyboardState(); + KeyboardState Keyboard = this.Keyboard ?? new KeyboardState(); if (!UIActive) { @@ -181,42 +181,86 @@ namespace Ryujinx.UI HidJoystickPosition LeftJoystick; HidJoystickPosition RightJoystick; - int LeftJoystickDX = 0; - int LeftJoystickDY = 0; - int RightJoystickDX = 0; - int RightJoystickDY = 0; + int LeftJoystickDX = 0; + int LeftJoystickDY = 0; + int RightJoystickDX = 0; + int RightJoystickDY = 0; + float AnalogStickDeadzone = Config.GamePadDeadzone; - //RightJoystick - if (Keyboard[(Key)Config.FakeJoyCon.Left.StickUp]) LeftJoystickDY = short.MaxValue; - if (Keyboard[(Key)Config.FakeJoyCon.Left.StickDown]) LeftJoystickDY = -short.MaxValue; - if (Keyboard[(Key)Config.FakeJoyCon.Left.StickLeft]) LeftJoystickDX = -short.MaxValue; - if (Keyboard[(Key)Config.FakeJoyCon.Left.StickRight]) LeftJoystickDX = short.MaxValue; + //LeftJoystick + if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickUp]) LeftJoystickDY = short.MaxValue; + if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickDown]) LeftJoystickDY = -short.MaxValue; + if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickLeft]) LeftJoystickDX = -short.MaxValue; + if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickRight]) LeftJoystickDX = short.MaxValue; //LeftButtons - if (Keyboard[(Key)Config.FakeJoyCon.Left.StickButton]) CurrentButton |= HidControllerButtons.KEY_LSTICK; - if (Keyboard[(Key)Config.FakeJoyCon.Left.DPadUp]) CurrentButton |= HidControllerButtons.KEY_DUP; - if (Keyboard[(Key)Config.FakeJoyCon.Left.DPadDown]) CurrentButton |= HidControllerButtons.KEY_DDOWN; - if (Keyboard[(Key)Config.FakeJoyCon.Left.DPadLeft]) CurrentButton |= HidControllerButtons.KEY_DLEFT; - if (Keyboard[(Key)Config.FakeJoyCon.Left.DPadRight]) CurrentButton |= HidControllerButtons.KEY_DRIGHT; - if (Keyboard[(Key)Config.FakeJoyCon.Left.ButtonMinus]) CurrentButton |= HidControllerButtons.KEY_MINUS; - if (Keyboard[(Key)Config.FakeJoyCon.Left.ButtonL]) CurrentButton |= HidControllerButtons.KEY_L; - if (Keyboard[(Key)Config.FakeJoyCon.Left.ButtonZL]) CurrentButton |= HidControllerButtons.KEY_ZL; + if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickButton]) CurrentButton |= HidControllerButtons.KEY_LSTICK; + if (Keyboard[(Key)Config.JoyConKeyboard.Left.DPadUp]) CurrentButton |= HidControllerButtons.KEY_DUP; + if (Keyboard[(Key)Config.JoyConKeyboard.Left.DPadDown]) CurrentButton |= HidControllerButtons.KEY_DDOWN; + if (Keyboard[(Key)Config.JoyConKeyboard.Left.DPadLeft]) CurrentButton |= HidControllerButtons.KEY_DLEFT; + if (Keyboard[(Key)Config.JoyConKeyboard.Left.DPadRight]) CurrentButton |= HidControllerButtons.KEY_DRIGHT; + if (Keyboard[(Key)Config.JoyConKeyboard.Left.ButtonMinus]) CurrentButton |= HidControllerButtons.KEY_MINUS; + if (Keyboard[(Key)Config.JoyConKeyboard.Left.ButtonL]) CurrentButton |= HidControllerButtons.KEY_L; + if (Keyboard[(Key)Config.JoyConKeyboard.Left.ButtonZL]) CurrentButton |= HidControllerButtons.KEY_ZL; //RightJoystick - if (Keyboard[(Key)Config.FakeJoyCon.Right.StickUp]) RightJoystickDY = short.MaxValue; - if (Keyboard[(Key)Config.FakeJoyCon.Right.StickDown]) RightJoystickDY = -short.MaxValue; - if (Keyboard[(Key)Config.FakeJoyCon.Right.StickLeft]) RightJoystickDX = -short.MaxValue; - if (Keyboard[(Key)Config.FakeJoyCon.Right.StickRight]) RightJoystickDX = short.MaxValue; + if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickUp]) RightJoystickDY = short.MaxValue; + if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickDown]) RightJoystickDY = -short.MaxValue; + if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickLeft]) RightJoystickDX = -short.MaxValue; + if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickRight]) RightJoystickDX = short.MaxValue; //RightButtons - if (Keyboard[(Key)Config.FakeJoyCon.Right.StickButton]) CurrentButton |= HidControllerButtons.KEY_RSTICK; - if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonA]) CurrentButton |= HidControllerButtons.KEY_A; - if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonB]) CurrentButton |= HidControllerButtons.KEY_B; - if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonX]) CurrentButton |= HidControllerButtons.KEY_X; - if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonY]) CurrentButton |= HidControllerButtons.KEY_Y; - if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonPlus]) CurrentButton |= HidControllerButtons.KEY_PLUS; - if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonR]) CurrentButton |= HidControllerButtons.KEY_R; - if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonZR]) CurrentButton |= HidControllerButtons.KEY_ZR; + if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickButton]) CurrentButton |= HidControllerButtons.KEY_RSTICK; + if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonA]) CurrentButton |= HidControllerButtons.KEY_A; + if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonB]) CurrentButton |= HidControllerButtons.KEY_B; + if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonX]) CurrentButton |= HidControllerButtons.KEY_X; + if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonY]) CurrentButton |= HidControllerButtons.KEY_Y; + if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonPlus]) CurrentButton |= HidControllerButtons.KEY_PLUS; + if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonR]) CurrentButton |= HidControllerButtons.KEY_R; + if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonZR]) CurrentButton |= HidControllerButtons.KEY_ZR; + + //Controller Input + if (Config.GamePadEnable) + { + GamePadState GamePad = OpenTK.Input.GamePad.GetState(Config.GamePadIndex); + //LeftButtons + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.DPadUp)) CurrentButton |= HidControllerButtons.KEY_DUP; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.DPadDown)) CurrentButton |= HidControllerButtons.KEY_DDOWN; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.DPadLeft)) CurrentButton |= HidControllerButtons.KEY_DLEFT; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.DPadRight)) CurrentButton |= HidControllerButtons.KEY_DRIGHT; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.StickButton)) CurrentButton |= HidControllerButtons.KEY_LSTICK; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.ButtonMinus)) CurrentButton |= HidControllerButtons.KEY_MINUS; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.ButtonL)) CurrentButton |= HidControllerButtons.KEY_L; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.ButtonZL)) CurrentButton |= HidControllerButtons.KEY_ZL; + + //RightButtons + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonA)) CurrentButton |= HidControllerButtons.KEY_A; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonB)) CurrentButton |= HidControllerButtons.KEY_B; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonX)) CurrentButton |= HidControllerButtons.KEY_X; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonY)) CurrentButton |= HidControllerButtons.KEY_Y; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.StickButton)) CurrentButton |= HidControllerButtons.KEY_RSTICK; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonPlus)) CurrentButton |= HidControllerButtons.KEY_PLUS; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonR)) CurrentButton |= HidControllerButtons.KEY_R; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonZR)) CurrentButton |= HidControllerButtons.KEY_ZR; + + //LeftJoystick + if (GetJoystickAxis(GamePad, Config.JoyConController.Left.Stick).X >= AnalogStickDeadzone + || GetJoystickAxis(GamePad, Config.JoyConController.Left.Stick).X <= -AnalogStickDeadzone) + LeftJoystickDX = (int)(GetJoystickAxis(GamePad, Config.JoyConController.Left.Stick).X * short.MaxValue); + + if (GetJoystickAxis(GamePad, Config.JoyConController.Left.Stick).Y >= AnalogStickDeadzone + || GetJoystickAxis(GamePad, Config.JoyConController.Left.Stick).Y <= -AnalogStickDeadzone) + LeftJoystickDY = (int)(GetJoystickAxis(GamePad, Config.JoyConController.Left.Stick).Y * short.MaxValue); + + //RightJoystick + if (GetJoystickAxis(GamePad, Config.JoyConController.Right.Stick).X >= AnalogStickDeadzone + || GetJoystickAxis(GamePad, Config.JoyConController.Right.Stick).X <= -AnalogStickDeadzone) + RightJoystickDX = (int)(GetJoystickAxis(GamePad, Config.JoyConController.Right.Stick).X * short.MaxValue); + + if (GetJoystickAxis(GamePad, Config.JoyConController.Right.Stick).Y >= AnalogStickDeadzone + || GetJoystickAxis(GamePad, Config.JoyConController.Right.Stick).Y <= -AnalogStickDeadzone) + RightJoystickDY = (int)(GetJoystickAxis(GamePad, Config.JoyConController.Right.Stick).Y * short.MaxValue); + } LeftJoystick = new HidJoystickPosition { diff --git a/Ryujinx.UI/GUI/Widgets/ConfigurationWidget.cs b/Ryujinx.UI/GUI/Widgets/ConfigurationWidget.cs index 7d116d7b55..4141728e68 100644 --- a/Ryujinx.UI/GUI/Widgets/ConfigurationWidget.cs +++ b/Ryujinx.UI/GUI/Widgets/ConfigurationWidget.cs @@ -9,12 +9,16 @@ namespace Ryujinx.UI.Widgets static bool ConfigIntialized = false; static bool OpenFolderPicker; static string CurrentPath; + static float CurrentGamePadDeadzone; + static bool CurrentGamePadEnable; + static int CurrentGamePadIndex; + static float CurrentGamePadTriggerThreshold; static IniParser IniParser; static FilePicker FolderPicker; static JoyConKeyboard KeyboardInputLayout; static JoyConController ControllerInputLayout; - static Page CurrentPage = Page.General; + static Page CurrentPage = Page.General; static ConfigurationWidget() { @@ -23,11 +27,32 @@ namespace Ryujinx.UI.Widgets CurrentPath = Config.DefaultGameDirectory.ToString(); } + static void Reset() + { + KeyboardInputLayout = Config.JoyConKeyboard; + ControllerInputLayout = Config.JoyConController; + CurrentGamePadTriggerThreshold = Config.GamePadTriggerThreshold; + CurrentGamePadIndex = Config.GamePadIndex; + CurrentGamePadEnable = Config.GamePadEnable; + CurrentGamePadDeadzone = Config.GamePadDeadzone; + } + + static void Apply() + { + Config.JoyConKeyboard = KeyboardInputLayout; + Config.JoyConController = ControllerInputLayout; + Config.GamePadDeadzone = CurrentGamePadDeadzone; + Config.GamePadEnable = CurrentGamePadEnable; + Config.GamePadIndex = CurrentGamePadIndex; + Config.GamePadTriggerThreshold = CurrentGamePadTriggerThreshold; + } + public static void Draw() { if(!ConfigIntialized) { - KeyboardInputLayout = Config.JoyConKeyboard; + Reset(); + ConfigIntialized = true; } @@ -104,12 +129,14 @@ namespace Ryujinx.UI.Widgets { if (ImGui.Button("Apply", new Vector2(Values.ButtonWidth, Values.ButtonHeight))) { - Config.JoyConKeyboard = KeyboardInputLayout; + Apply(); } ImGui.SameLine(); } if (ImGui.Button("Save", new Vector2(Values.ButtonWidth, Values.ButtonHeight))) { + Apply(); + Config.Save(EmulationWindow.Ns.Log); } ImGui.SameLine(); diff --git a/Ryujinx.UI/GUI/Widgets/InputPage.cs b/Ryujinx.UI/GUI/Widgets/InputPage.cs index 24b33e9a1d..56eaf46871 100644 --- a/Ryujinx.UI/GUI/Widgets/InputPage.cs +++ b/Ryujinx.UI/GUI/Widgets/InputPage.cs @@ -1,8 +1,12 @@ using ImGuiNET; using OpenTK.Input; +using OpenTK.Platform; +using OpenTK.Platform.Windows; using System; +using System.Collections.Generic; using System.Linq; using System.Numerics; +using Ryujinx.Common.Input; namespace Ryujinx.UI.Widgets @@ -13,19 +17,978 @@ namespace Ryujinx.UI.Widgets static float ContentWidth; static Vector2 GroupSize; - static Key pressedKey; + static Key PressedKey; + static string PressedButton; + static string Axis; static bool RequestPopup; + static InputDevice CurrentSelectedDevice = default(InputDevice); + + static Dictionary ConnectedHIDs; + public static void DrawInputPage() { Vector2 AvailableSpace = ImGui.GetContentRegionAvailable(); + if (ConnectedHIDs == null) + RefreshDevices(); + + if (string.IsNullOrWhiteSpace(CurrentSelectedDevice.Name)) + CurrentSelectedDevice = ConnectedHIDs.First().Value; + + ImGui.Text("Connected Devices"); + + if (ImGui.BeginCombo(string.Empty, CurrentSelectedDevice.Name, + ComboFlags.HeightSmall)) + { + foreach(InputDevice Device in ConnectedHIDs.Values) + { + bool IsSelected = (CurrentSelectedDevice.Index == Device.Index); + if(ImGui.Selectable(Device.Name,IsSelected)) + { + CurrentSelectedDevice = Device; + } + } + + ImGui.EndCombo(); + } + + ImGui.SameLine(); + + if (ImGui.Button("Refresh")) + RefreshDevices(); + + if (CurrentSelectedDevice.DeviceType == DeviceType.GamePad) + { + ImGui.Checkbox("Enable GamePad", ref CurrentGamePadEnable); + + ImGuiNative.igBeginGroup(); + + ImGui.Text("Game Pad Deadzone"); + + ImGui.SliderFloat(string.Empty, ref CurrentGamePadDeadzone, 0, 1, CurrentGamePadDeadzone.ToString(), 1f); + + ImGuiNative.igEndGroup(); + + ImGui.SameLine(); + + ImGuiNative.igBeginGroup(); + + ImGui.Text("Game Pad Trigger Threshold"); + + ImGui.SliderFloat(string.Empty, ref CurrentGamePadTriggerThreshold, 0, 1, CurrentGamePadTriggerThreshold.ToString(), 1f); + + ImGuiNative.igEndGroup(); + } + GroupSize = new Vector2(AvailableSpace.X / 2, AvailableSpace.Y / 3); + if (CurrentSelectedDevice.DeviceType == DeviceType.Keyboard) + DrawKeyboardInputLayout(); + else + DrawControllerInputLayout(); + + if (Toggles.Contains(true)) + { + ImGui.OpenPopup("Enter Key"); + RequestPopup = true; + } + else + RequestPopup = false; + + if (ImGui.BeginPopupModal("Enter Key", WindowFlags.AlwaysAutoResize| + WindowFlags.NoMove + | WindowFlags.NoResize)) + { + ImGui.Text("Please enter a key"); + + if (!RequestPopup) + ImGui.CloseCurrentPopup(); + + ImGui.EndPopup(); + } + + if (ImGui.Button("Reset", new Vector2(ContentWidth, 50))) + { + Reset(); + } + + } + + static void DrawLeftAnalog() + { + // Show the Left Analog bindings + ImGui.Text("Left Analog"); + ImGuiNative.igBeginGroup(); + ImGuiNative.igBeginGroup(); + ImGui.Text("Up"); + + if (ImGui.Button(((Key)KeyboardInputLayout.Left.StickUp).ToString(), + new Vector2(ContentWidth, 50))) + { + Toggles[0] = true; + } + + if (Toggles[0]) + { + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Left.StickUp = (int)PressedKey; + Toggles[0] = false; + } + } + + ImGuiNative.igEndGroup(); + + ImGui.SameLine(); + ImGuiNative.igBeginGroup(); + ImGui.Text("Down"); + + if (ImGui.Button(((Key)KeyboardInputLayout.Left.StickDown).ToString(), + new Vector2(ContentWidth, 50))) + { + Toggles[1] = true; + } + + if (Toggles[1]) + { + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Left.StickDown = (int)PressedKey; + Toggles[1] = false; + } + } + + ImGuiNative.igEndGroup(); + + ImGuiNative.igBeginGroup(); + ImGui.Text("Left"); + + if (ImGui.Button(((Key)KeyboardInputLayout.Left.StickLeft).ToString(), + new Vector2(ContentWidth, 50))) + { + Toggles[2] = true; + } + + if (Toggles[2]) + { + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Left.StickLeft = (int)PressedKey; + Toggles[2] = false; + } + } + ImGuiNative.igEndGroup(); + + ImGui.SameLine(); + ImGuiNative.igBeginGroup(); + ImGui.Text("Right"); + + if (ImGui.Button(((Key)KeyboardInputLayout.Left.StickRight).ToString(), + new Vector2(ContentWidth, 50))) + { + Toggles[3] = true; + } + + if (Toggles[3]) + { + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Left.StickRight = (int)PressedKey; + Toggles[3] = false; + } + } + + ImGuiNative.igEndGroup(); + + ImGuiNative.igEndGroup(); + } + + static void DrawRightAnalog() + { + //Show Right Analog Bindings + ImGui.Text("Right Analog"); + ImGuiNative.igBeginGroup(); + + ImGuiNative.igBeginGroup(); + ImGui.Text("Up"); + + if (ImGui.Button(((Key)KeyboardInputLayout.Right.StickUp).ToString(), + new Vector2(ContentWidth, 50))) + { + Toggles[4] = true; + } + + if (Toggles[4]) + { + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Right.StickUp = (int)PressedKey; + + Toggles[4] = false; + } + } + ImGuiNative.igEndGroup(); + + ImGui.SameLine(); + ImGuiNative.igBeginGroup(); + ImGui.Text("Down"); + + if (ImGui.Button(((Key)KeyboardInputLayout.Right.StickDown).ToString(), + new Vector2(ContentWidth, 50))) + { + Toggles[5] = true; + } + + if (Toggles[5]) + { + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Right.StickDown = (int)PressedKey; + Toggles[5] = false; + } + } + ImGuiNative.igEndGroup(); + + ImGuiNative.igBeginGroup(); + ImGui.Text("Left"); + + if (ImGui.Button(((Key)KeyboardInputLayout.Right.StickLeft).ToString(), + new Vector2(ContentWidth, 50))) + { + Toggles[6] = true; + } + + if (Toggles[6]) + { + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Right.StickLeft = (int)PressedKey; + Toggles[6] = false; + } + } + ImGuiNative.igEndGroup(); + + ImGui.SameLine(); + ImGuiNative.igBeginGroup(); + ImGui.Text("Right"); + + if (ImGui.Button(((Key)KeyboardInputLayout.Right.StickRight).ToString(), + new Vector2(ContentWidth, 50))) + { + Toggles[7] = true; + } + + if (Toggles[7]) + { + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Right.StickRight = (int)PressedKey; + Toggles[7] = false; + } + } + ImGuiNative.igEndGroup(); + + ImGuiNative.igEndGroup(); + } + + static void DrawControllerLeftAnalog() + { + // Show the Left Analog bindings + ImGui.Text("Left Analog"); + + ImGuiNative.igBeginGroup(); + ImGuiNative.igBeginGroup(); + + ImGui.Text("Stick"); + + if (ImGui.Button(ControllerInputLayout.Left.Stick.ToString(), + new Vector2(ContentWidth, 50))) + { + Toggles[0] = true; + } + + if (Toggles[0]) + { + if (GetAxis(ref Axis)) + { + ControllerInputLayout.Left.Stick = + (GamePadStick)Enum.Parse(typeof(GamePadStick), Axis); + Toggles[0] = false; + } + } + + ImGuiNative.igEndGroup(); + + ImGui.SameLine(); + + ImGuiNative.igBeginGroup(); + + ImGui.Text("Button"); + + if (ImGui.Button(ControllerInputLayout.Left.StickButton.ToString(), + new Vector2(ContentWidth, 50))) + { + Toggles[3] = true; + } + + if (Toggles[3]) + { + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Left.StickButton = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[3] = false; + } + } + + ImGuiNative.igEndGroup(); + + ImGuiNative.igEndGroup(); + } + + static void DrawControllerRightAnalog() + { + //Show Right Analog Bindings + ImGui.Text("Right Analog"); + ImGuiNative.igBeginGroup(); + + ImGuiNative.igBeginGroup(); + ImGui.Text("Stick"); + + if (ImGui.Button(ControllerInputLayout.Right.Stick.ToString() + , new Vector2(ContentWidth, 50))) + { + Toggles[4] = true; + } + + if (Toggles[4]) + { + if (GetAxis(ref Axis)) + { + ControllerInputLayout.Right.Stick = + (GamePadStick)Enum.Parse(typeof(GamePadStick), Axis); + + Toggles[4] = false; + } + } + ImGuiNative.igEndGroup(); + + ImGui.SameLine(); + ImGuiNative.igBeginGroup(); + ImGui.Text("Button"); + + if (ImGui.Button(ControllerInputLayout.Right.StickButton.ToString(), + new Vector2(ContentWidth, 50))) + { + Toggles[7] = true; + } + + if (Toggles[7]) + { + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Right.StickButton = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[7] = false; + } + } + ImGuiNative.igEndGroup(); + + ImGuiNative.igEndGroup(); + } + + static void DrawDpad() + { + string ButtonHeader = string.Empty; + + //Show DPad Bindings + ImGui.Text("D-Pad"); + ImGuiNative.igBeginGroup(); + + ImGuiNative.igBeginGroup(); + ImGui.Text("Up"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Left.DPadUp).ToString() + : ControllerInputLayout.Left.DPadUp.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[8] = true; + } + + if (Toggles[8]) + { + switch(CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Left.DPadUp = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[8] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Left.DPadUp = (int)PressedKey; + Toggles[8] = false; + } + break; + } + } + ImGuiNative.igEndGroup(); + + ImGui.SameLine(); + ImGuiNative.igBeginGroup(); + ImGui.Text("Down"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Left.DPadDown).ToString() + : ControllerInputLayout.Left.DPadDown.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[9] = true; + } + + if (Toggles[9]) + { + switch (CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Left.DPadDown = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[9] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Left.DPadDown = (int)PressedKey; + Toggles[9] = false; + } + break; + } + } + ImGuiNative.igEndGroup(); + + ImGuiNative.igBeginGroup(); + ImGui.Text("Left"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Left.DPadLeft).ToString() + : ControllerInputLayout.Left.DPadLeft.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[10] = true; + } + + if (Toggles[10]) + { + switch (CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Left.DPadLeft = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[10] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Left.DPadLeft = (int)PressedKey; + Toggles[10] = false; + } + break; + } + } + ImGuiNative.igEndGroup(); + + ImGui.SameLine(); + ImGuiNative.igBeginGroup(); + ImGui.Text("Right"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Left.DPadRight).ToString() + : ControllerInputLayout.Left.DPadRight.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[11] = true; + } + + if (Toggles[11]) + { + switch (CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Left.DPadRight = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[11] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Left.DPadRight = (int)PressedKey; + Toggles[11] = false; + } + break; + } + } + ImGuiNative.igEndGroup(); + + ImGuiNative.igEndGroup(); + } + + static void DrawActionKeys() + { + string ButtonHeader = string.Empty; + //Show Action Key Bindings + ImGui.Text("Action Keys"); + ImGuiNative.igBeginGroup(); + + ImGuiNative.igBeginGroup(); + ImGui.Text("A"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Right.ButtonA).ToString() + : ControllerInputLayout.Right.ButtonA.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[12] = true; + } + + if (Toggles[12]) + { + switch (CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Right.ButtonA = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[12] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Right.ButtonA = (int)PressedKey; + Toggles[12] = false; + } + break; + } + } + ImGuiNative.igEndGroup(); + + ImGui.SameLine(); + ImGuiNative.igBeginGroup(); + ImGui.Text("B"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Right.ButtonB).ToString() + : ControllerInputLayout.Right.ButtonB.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[13] = true; + } + + if (Toggles[13]) + { + switch (CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Right.ButtonB = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[13] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Right.ButtonB = (int)PressedKey; + Toggles[13] = false; + } + break; + } + } + ImGuiNative.igEndGroup(); + + ImGuiNative.igBeginGroup(); + ImGui.Text("X"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Right.ButtonX).ToString() + : ControllerInputLayout.Right.ButtonX.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[14] = true; + } + + if (Toggles[14]) + { + switch (CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Right.ButtonX = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[14] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Right.ButtonX = (int)PressedKey; + Toggles[14] = false; + } + break; + } + } + ImGuiNative.igEndGroup(); + + ImGui.SameLine(); + ImGuiNative.igBeginGroup(); + ImGui.Text("Y"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Right.ButtonY).ToString() + : ControllerInputLayout.Right.ButtonY.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[15] = true; + } + + if (Toggles[15]) + { + switch (CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Right.ButtonY = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[15] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Right.ButtonY = (int)PressedKey; + Toggles[15] = false; + } + break; + } + } + + ImGuiNative.igEndGroup(); + + ImGuiNative.igEndGroup(); + } + + static void DrawTriggers() + { + string ButtonHeader = string.Empty; + //Draw Triggers + ImGuiNative.igBeginGroup(); + + ImGui.Text("Triggers"); + + ImGuiNative.igBeginGroup(); + ImGui.Text("L"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Left.ButtonL).ToString() + : ControllerInputLayout.Left.ButtonL.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[17] = true; + } + + if (Toggles[17]) + { + switch (CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Left.ButtonL = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[17] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Left.ButtonL = (int)PressedKey; + Toggles[17] = false; + } + break; + } + } + + ImGuiNative.igEndGroup(); + + ImGui.SameLine(); + ImGuiNative.igBeginGroup(); + ImGui.Text("R"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Right.ButtonR).ToString() + : ControllerInputLayout.Right.ButtonR.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[16] = true; + } + + if (Toggles[16]) + { + switch (CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Right.ButtonR = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[16] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Right.ButtonR = (int)PressedKey; + Toggles[16] = false; + } + break; + } + } + ImGuiNative.igEndGroup(); + + ImGuiNative.igBeginGroup(); + ImGui.Text("ZL"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Left.ButtonZL).ToString() + : ControllerInputLayout.Left.ButtonZL.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[19] = true; + } + + if (Toggles[19]) + { + switch (CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Left.ButtonZL = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[19] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Left.ButtonZL = (int)PressedKey; + Toggles[19] = false; + } + break; + } + } + ImGuiNative.igEndGroup(); + + ImGui.SameLine(); + ImGuiNative.igBeginGroup(); + ImGui.Text("ZR"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Right.ButtonZR).ToString() + : ControllerInputLayout.Right.ButtonZR.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[18] = true; + } + + if (Toggles[18]) + { + switch (CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Right.ButtonZR = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[18] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Right.ButtonZR = (int)PressedKey; + Toggles[18] = false; + } + break; + } + } + ImGuiNative.igEndGroup(); + + ImGuiNative.igEndGroup(); + } + + static void DrawExtras() + { + string ButtonHeader = string.Empty; + + //Draw Extra + ImGuiNative.igBeginGroup(); + ImGui.Text("Extra Keys"); + + ImGuiNative.igBeginGroup(); + ImGui.Text("-"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Left.ButtonMinus).ToString() + : ControllerInputLayout.Left.ButtonMinus.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[20] = true; + } + + if (Toggles[20]) + { + switch (CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Left.ButtonMinus = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[20] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Left.ButtonMinus = (int)PressedKey; + Toggles[20] = false; + } + break; + } + } + ImGuiNative.igEndGroup(); + + ImGuiNative.igBeginGroup(); + ImGui.Text("+"); + + ButtonHeader = CurrentSelectedDevice.DeviceType == DeviceType.Keyboard ? + ((Key)KeyboardInputLayout.Right.ButtonPlus).ToString() + : ControllerInputLayout.Right.ButtonPlus.ToString(); + + if (ImGui.Button(ButtonHeader, new Vector2(ContentWidth, 50))) + { + Toggles[21] = true; + } + + if (Toggles[21]) + { + switch (CurrentSelectedDevice.DeviceType) + { + case DeviceType.GamePad: + if (GetButton(ref PressedButton)) + { + ControllerInputLayout.Right.ButtonPlus = + (GamePadButton)Enum.Parse(typeof(GamePadButton),PressedButton); + Toggles[21] = false; + } + break; + case DeviceType.Keyboard: + if (GetKey(ref PressedKey)) + { + KeyboardInputLayout.Right.ButtonPlus = (int)PressedKey; + Toggles[21] = false; + } + break; + } + } + ImGuiNative.igEndGroup(); + + ImGuiNative.igEndGroup(); + } + + static bool GetKey(ref Key PressedKey) + { + IO IO = ImGui.GetIO(); + foreach (Key Key in Enum.GetValues(typeof(Key))) + { + if (IO.KeysDown[(int)Key]) + { + PressedKey = Key; + return true; + } + } + return false; + } + + static bool GetButton(ref string PressedButton) + { + IO IO = ImGui.GetIO(); + GamePadState GamePad = OpenTK.Input.GamePad.GetState(Config.GamePadIndex); + foreach (GamePadButton Button in Enum.GetValues(typeof(GamePadButton))) + { + if (WindowHelper.IsGamePadButtonPressed(GamePad, Button)) + { + PressedButton = Button.ToString(); + return true; + } + } + return false; + } + + static bool GetAxis(ref string Axis) + { + IO IO = ImGui.GetIO(); + GamePadState GamePad = OpenTK.Input.GamePad.GetState(Config.GamePadIndex); + foreach (GamePadStick Stick in Enum.GetValues(typeof(GamePadStick))) + { + if (WindowHelper.GetJoystickAxis(GamePad,Stick).Length > 0) + { + Axis = Stick.ToString(); + return true; + } + } + return false; + } + + static void DrawKeyboardInputLayout() + { if (ImGui.BeginChildFrame(11, GroupSize, WindowFlags.AlwaysAutoResize)) { ContentWidth = (ImGui.GetContentRegionAvailableWidth() - 10) / 2; - GroupSize = ImGui.GetContentRegionMax(); + GroupSize = ImGui.GetContentRegionMax(); DrawLeftAnalog(); @@ -40,6 +1003,34 @@ namespace Ryujinx.UI.Widgets ImGui.EndChildFrame(); } + DrawMainLayout(); + } + + static void DrawControllerInputLayout() + { + if (ImGui.BeginChildFrame(11, GroupSize, WindowFlags.AlwaysAutoResize)) + { + ContentWidth = (ImGui.GetContentRegionAvailableWidth() - 10) / 2; + GroupSize = ImGui.GetContentRegionMax(); + + DrawControllerLeftAnalog(); + + ImGui.EndChildFrame(); + } + + ImGui.SameLine(); + if (ImGui.BeginChildFrame(12, GroupSize, WindowFlags.AlwaysAutoResize)) + { + DrawControllerRightAnalog(); + + ImGui.EndChildFrame(); + } + + DrawMainLayout(); + } + + static void DrawMainLayout() + { if (ImGui.BeginChildFrame(13, GroupSize, WindowFlags.AlwaysAutoResize)) { DrawDpad(); @@ -69,528 +1060,40 @@ namespace Ryujinx.UI.Widgets ImGui.EndChildFrame(); } - - if (Toggles.Contains(true)) - { - ImGui.OpenPopup("Enter Key"); - RequestPopup = true; - } - else - RequestPopup = false; - - if (ImGui.BeginPopupModal("Enter Key", WindowFlags.AlwaysAutoResize| WindowFlags.NoMove - | WindowFlags.NoResize)) - { - ImGui.Text("Please enter a key"); - - if (!RequestPopup) - ImGui.CloseCurrentPopup(); - - ImGui.EndPopup(); - } } - - static void DrawLeftAnalog() + static void RefreshDevices() { - // Show the Left Analog bindings - ImGui.Text("Left Analog"); - ImGuiNative.igBeginGroup(); - ImGuiNative.igBeginGroup(); - ImGui.Text("Up"); + ConnectedHIDs = new Dictionary(); - if (ImGui.Button(((Key)KeyboardInputLayout.Left.StickUp).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[0] = true; - } + int GamePadIndex = 0; - if (Toggles[0]) - { - if (GetKey(ref pressedKey)) + InputDevice KeyboardInputDevice = new InputDevice() { - KeyboardInputLayout.Left.StickUp = (int)pressedKey; - Toggles[0] = false; - } - } - - ImGuiNative.igEndGroup(); - - ImGui.SameLine(); - ImGuiNative.igBeginGroup(); - ImGui.Text("Down"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Left.StickDown).ToString(), - new Vector2(ContentWidth, 50))) + Index = short.MaxValue, + DeviceType = DeviceType.Keyboard, + Name = "Keyboard" + }; + ConnectedHIDs.Add(short.MaxValue, KeyboardInputDevice); + + // Scans for connected joysticks + while (true) { - Toggles[1] = true; - } - - if (Toggles[1]) - { - if (GetKey(ref pressedKey)) + JoystickState GamePad = Joystick.GetState(GamePadIndex); + if (GamePad.IsConnected) { - KeyboardInputLayout.Left.StickDown = (int)pressedKey; - Toggles[1] = false; + InputDevice GamePadDevice = new InputDevice() + { + Index = GamePadIndex, + DeviceType = DeviceType.GamePad, + Name = "GamePad " + GamePadIndex + }; + ConnectedHIDs.Add(GamePadIndex, GamePadDevice); } + else + break; + GamePadIndex++; } - - ImGuiNative.igEndGroup(); - - ImGuiNative.igBeginGroup(); - ImGui.Text("Left"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Left.StickLeft).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[2] = true; - } - - if (Toggles[2]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Left.StickLeft = (int)pressedKey; - Toggles[2] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGui.SameLine(); - ImGuiNative.igBeginGroup(); - ImGui.Text("Right"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Left.StickRight).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[3] = true; - } - - if (Toggles[3]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Left.StickRight = (int)pressedKey; - Toggles[3] = false; - } - } - - ImGuiNative.igEndGroup(); - - ImGuiNative.igEndGroup(); - } - - static void DrawRightAnalog() - { - //Show Right Analog Bindings - ImGui.Text("Right Analog"); - ImGuiNative.igBeginGroup(); - - ImGuiNative.igBeginGroup(); - ImGui.Text("Up"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Right.StickUp).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[4] = true; - } - - if (Toggles[4]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Right.StickUp = (int)pressedKey; - - Toggles[4] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGui.SameLine(); - ImGuiNative.igBeginGroup(); - ImGui.Text("Down"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Right.StickDown).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[5] = true; - } - - if (Toggles[5]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Right.StickDown = (int)pressedKey; - Toggles[5] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGuiNative.igBeginGroup(); - ImGui.Text("Left"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Right.StickLeft).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[6] = true; - } - - if (Toggles[6]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Right.StickLeft = (int)pressedKey; - Toggles[6] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGui.SameLine(); - ImGuiNative.igBeginGroup(); - ImGui.Text("Right"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Right.StickRight).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[7] = true; - } - - if (Toggles[7]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Right.StickRight = (int)pressedKey; - Toggles[7] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGuiNative.igEndGroup(); - } - - static void DrawDpad() - { - //Show DPad Bindings - ImGui.Text("D-Pad"); - ImGuiNative.igBeginGroup(); - - ImGuiNative.igBeginGroup(); - ImGui.Text("Up"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Left.DPadUp).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[8] = true; - } - - if (Toggles[8]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Left.DPadUp = (int)pressedKey; - Toggles[8] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGui.SameLine(); - ImGuiNative.igBeginGroup(); - ImGui.Text("Down"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Left.DPadDown).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[9] = true; - } - - if (Toggles[9]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Left.DPadDown = (int)pressedKey; - Toggles[9] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGuiNative.igBeginGroup(); - ImGui.Text("Left"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Left.DPadLeft).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[10] = true; - } - - if (Toggles[10]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Left.DPadLeft = (int)pressedKey; - Toggles[10] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGui.SameLine(); - ImGuiNative.igBeginGroup(); - ImGui.Text("Right"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Left.DPadRight).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[11] = true; - } - - if (Toggles[11]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Left.DPadRight = (int)pressedKey; - Toggles[11] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGuiNative.igEndGroup(); - } - - static void DrawActionKeys() - { - //Show Action Key Bindings - ImGui.Text("Action Keys"); - ImGuiNative.igBeginGroup(); - - ImGuiNative.igBeginGroup(); - ImGui.Text("A"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Right.ButtonA).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[12] = true; - } - - if (Toggles[12]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Right.ButtonA = (int)pressedKey; - Toggles[12] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGui.SameLine(); - ImGuiNative.igBeginGroup(); - ImGui.Text("B"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Right.ButtonB).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[13] = true; - } - - if (Toggles[13]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Right.ButtonB = (int)pressedKey; - Toggles[13] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGuiNative.igBeginGroup(); - ImGui.Text("X"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Right.ButtonX).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[14] = true; - } - - if (Toggles[14]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Right.ButtonX = (int)pressedKey; - Toggles[14] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGui.SameLine(); - ImGuiNative.igBeginGroup(); - ImGui.Text("Y"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Right.ButtonY).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[15] = true; - } - - if (Toggles[15]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Right.ButtonY = (int)pressedKey; - Toggles[15] = false; - } - } - - ImGuiNative.igEndGroup(); - - ImGuiNative.igEndGroup(); - } - - static void DrawTriggers() - { - //Draw Triggers - ImGuiNative.igBeginGroup(); - - ImGui.Text("Triggers"); - - ImGuiNative.igBeginGroup(); - ImGui.Text("L"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Left.ButtonL).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[17] = true; - } - - if (Toggles[17]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Left.ButtonL = (int)pressedKey; - Toggles[17] = false; - } - } - - ImGuiNative.igEndGroup(); - - ImGui.SameLine(); - ImGuiNative.igBeginGroup(); - ImGui.Text("R"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Right.ButtonR).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[16] = true; - } - - if (Toggles[16]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Right.ButtonR = (int)pressedKey; - Toggles[16] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGuiNative.igBeginGroup(); - ImGui.Text("ZL"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Left.ButtonZL).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[19] = true; - } - - if (Toggles[19]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Left.ButtonZL = (int)pressedKey; - Toggles[19] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGui.SameLine(); - ImGuiNative.igBeginGroup(); - ImGui.Text("ZR"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Right.ButtonZR).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[18] = true; - } - - if (Toggles[18]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Right.ButtonZR = (int)pressedKey; - Toggles[18] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGuiNative.igEndGroup(); - } - - static void DrawExtras() - { - //Draw Extra - ImGuiNative.igBeginGroup(); - ImGui.Text("Extra Keys"); - - ImGuiNative.igBeginGroup(); - ImGui.Text("-"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Left.ButtonMinus).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[20] = true; - } - - if (Toggles[20]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Left.ButtonMinus = (int)pressedKey; - Toggles[20] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGuiNative.igBeginGroup(); - ImGui.Text("+"); - - if (ImGui.Button(((Key)KeyboardInputLayout.Right.ButtonPlus).ToString(), - new Vector2(ContentWidth, 50))) - { - Toggles[21] = true; - } - - if (Toggles[21]) - { - if (GetKey(ref pressedKey)) - { - KeyboardInputLayout.Right.ButtonPlus = (int)pressedKey; - Toggles[21] = false; - } - } - ImGuiNative.igEndGroup(); - - ImGuiNative.igEndGroup(); - } - - static bool GetKey(ref Key pressedKey) - { - IO IO = ImGui.GetIO(); - foreach (Key Key in Enum.GetValues(typeof(Key))) - { - if (IO.KeysDown[(int)Key]) - { - pressedKey = Key; - return true; - } - } - return false; } } } \ No newline at end of file diff --git a/Ryujinx.UI/GUI/WindowHelper.cs b/Ryujinx.UI/GUI/WindowHelper.cs index ca93d2782c..b803f47c0b 100644 --- a/Ryujinx.UI/GUI/WindowHelper.cs +++ b/Ryujinx.UI/GUI/WindowHelper.cs @@ -4,6 +4,7 @@ using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; using OpenTK.Input; using System; +using Ryujinx.Common.Input; namespace Ryujinx.UI { @@ -22,6 +23,8 @@ namespace Ryujinx.UI protected MouseState? Mouse = null; + protected GamePadState? GamePad = null; + public WindowHelper(string Title) : base(1280, 720, GraphicsMode.Default, Title, GameWindowFlags.Default , DisplayDevice.Default, 3, 3, GraphicsContextFlags.ForwardCompatible) { @@ -261,6 +264,60 @@ namespace Ryujinx.UI SwapBuffers(); } + internal static bool IsGamePadButtonPressed(GamePadState GamePad, GamePadButton Button) + { + if (Button == GamePadButton.LTrigger || Button == GamePadButton.RTrigger) + { + return GetGamePadTrigger(GamePad, Button) >= Config.GamePadTriggerThreshold; + } + else + { + return (GetGamePadButton(GamePad, Button) == ButtonState.Pressed); + } + } + + internal static ButtonState GetGamePadButton(GamePadState GamePad, GamePadButton Button) + { + switch (Button) + { + case GamePadButton.A: return GamePad.Buttons.A; + case GamePadButton.B: return GamePad.Buttons.B; + case GamePadButton.X: return GamePad.Buttons.X; + case GamePadButton.Y: return GamePad.Buttons.Y; + case GamePadButton.LStick: return GamePad.Buttons.LeftStick; + case GamePadButton.RStick: return GamePad.Buttons.RightStick; + case GamePadButton.LShoulder: return GamePad.Buttons.LeftShoulder; + case GamePadButton.RShoulder: return GamePad.Buttons.RightShoulder; + case GamePadButton.DPadUp: return GamePad.DPad.Up; + case GamePadButton.DPadDown: return GamePad.DPad.Down; + case GamePadButton.DPadLeft: return GamePad.DPad.Left; + case GamePadButton.DPadRight: return GamePad.DPad.Right; + case GamePadButton.Start: return GamePad.Buttons.Start; + case GamePadButton.Back: return GamePad.Buttons.Back; + default: throw new ArgumentException(); + } + } + + internal static float GetGamePadTrigger(GamePadState GamePad, GamePadButton Trigger) + { + switch (Trigger) + { + case GamePadButton.LTrigger: return GamePad.Triggers.Left; + case GamePadButton.RTrigger: return GamePad.Triggers.Right; + default: throw new ArgumentException(); + } + } + + internal static Vector2 GetJoystickAxis(GamePadState GamePad, GamePadStick Joystick) + { + switch (Joystick) + { + case GamePadStick.LJoystick: return GamePad.ThumbSticks.Left; + case GamePadStick.RJoystick: return new Vector2(-GamePad.ThumbSticks.Right.Y, -GamePad.ThumbSticks.Right.X); + default: throw new ArgumentException(); + } + } + protected override void OnKeyDown(KeyboardKeyEventArgs e) { Keyboard = e.Keyboard; diff --git a/Ryujinx.UI/InputDevice.cs b/Ryujinx.UI/InputDevice.cs new file mode 100644 index 0000000000..013f51b1ef --- /dev/null +++ b/Ryujinx.UI/InputDevice.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; +using OpenTK.Input; + +namespace Ryujinx.UI +{ + public struct InputDevice + { + public int Index; + public IInputDevice Device; + public DeviceType DeviceType; + public string Name; + } + + public enum DeviceType + { + GamePad, + Keyboard + } +} diff --git a/Ryujinx.UI/RyujinxUI.conf b/Ryujinx.UI/RyujinxUI.conf index 611f320717..59f7f859e7 100644 --- a/Ryujinx.UI/RyujinxUI.conf +++ b/Ryujinx.UI/RyujinxUI.conf @@ -19,29 +19,64 @@ Logging_Enable_Error = true #Filtered log classes, seperated by ", ", eg. `Logging_Filtered_Classes = Loader, ServiceFS` Logging_Filtered_Classes = -#https://github.com/opentk/opentk/blob/develop/src/OpenTK/Input/Key.cs -Controls_Left_FakeJoycon_Stick_Up = 105 -Controls_Left_FakeJoycon_Stick_Down = 101 -Controls_Left_FakeJoycon_Stick_Left = 83 -Controls_Left_FakeJoycon_Stick_Right = 86 -Controls_Left_FakeJoycon_Stick_Button = 88 -Controls_Left_FakeJoycon_DPad_Up = 45 -Controls_Left_FakeJoycon_DPad_Down = 46 -Controls_Left_FakeJoycon_DPad_Left = 47 -Controls_Left_FakeJoycon_DPad_Right = 48 -Controls_Left_FakeJoycon_Button_Minus = 120 -Controls_Left_FakeJoycon_Button_L = 87 -Controls_Left_FakeJoycon_Button_ZL = 99 +#Controller Device Index +GamePad_Index = 0 -Controls_Right_FakeJoycon_Stick_Up = 91 -Controls_Right_FakeJoycon_Stick_Down = 93 -Controls_Right_FakeJoycon_Stick_Left = 92 -Controls_Right_FakeJoycon_Stick_Right = 94 -Controls_Right_FakeJoycon_Stick_Button = 90 -Controls_Right_FakeJoycon_Button_A = 108 -Controls_Right_FakeJoycon_Button_B = 106 -Controls_Right_FakeJoycon_Button_X = 85 -Controls_Right_FakeJoycon_Button_Y = 104 -Controls_Right_FakeJoycon_Button_Plus = 121 -Controls_Right_FakeJoycon_Button_R = 103 -Controls_Right_FakeJoycon_Button_ZR = 97 \ No newline at end of file +#Controller Analog Stick Deadzone +GamePad_Deadzone = 0.05 + +#The value of how pressed down each trigger has to be in order to register a button press +GamePad_Trigger_Threshold = 0.5 + +#Whether or not to enable Controller support +GamePad_Enable = true + +#https://github.com/opentk/opentk/blob/develop/src/OpenTK/Input/Key.cs +Controls_Left_JoyConKeyboard_Stick_Up = 105 +Controls_Left_JoyConKeyboard_Stick_Down = 101 +Controls_Left_JoyConKeyboard_Stick_Left = 83 +Controls_Left_JoyConKeyboard_Stick_Right = 86 +Controls_Left_JoyConKeyboard_Stick_Button = 88 +Controls_Left_JoyConKeyboard_DPad_Up = 45 +Controls_Left_JoyConKeyboard_DPad_Down = 46 +Controls_Left_JoyConKeyboard_DPad_Left = 47 +Controls_Left_JoyConKeyboard_DPad_Right = 48 +Controls_Left_JoyConKeyboard_Button_Minus = 120 +Controls_Left_JoyConKeyboard_Button_L = 87 +Controls_Left_JoyConKeyboard_Button_ZL = 99 + +Controls_Right_JoyConKeyboard_Stick_Up = 91 +Controls_Right_JoyConKeyboard_Stick_Down = 93 +Controls_Right_JoyConKeyboard_Stick_Left = 92 +Controls_Right_JoyConKeyboard_Stick_Right = 94 +Controls_Right_JoyConKeyboard_Stick_Button = 90 +Controls_Right_JoyConKeyboard_Button_A = 108 +Controls_Right_JoyConKeyboard_Button_B = 106 +Controls_Right_JoyConKeyboard_Button_X = 85 +Controls_Right_JoyConKeyboard_Button_Y = 104 +Controls_Right_JoyConKeyboard_Button_Plus = 121 +Controls_Right_JoyConKeyboard_Button_R = 103 +Controls_Right_JoyConKeyboard_Button_ZR = 97 + +#Controller Controls + +Controls_Left_JoyConController_Stick_Button = LStick +Controls_Left_JoyConController_DPad_Up = DPadUp +Controls_Left_JoyConController_DPad_Down = DPadDown +Controls_Left_JoyConController_DPad_Left = DPadLeft +Controls_Left_JoyConController_DPad_Right = DPadRight +Controls_Left_JoyConController_Button_Minus = Back +Controls_Left_JoyConController_Button_L = LShoulder +Controls_Left_JoyConController_Button_ZL = LTrigger + +Controls_Right_JoyConController_Stick_Button = RStick +Controls_Right_JoyConController_Button_A = B +Controls_Right_JoyConController_Button_B = A +Controls_Right_JoyConController_Button_X = Y +Controls_Right_JoyConController_Button_Y = X +Controls_Right_JoyConController_Button_Plus = Start +Controls_Right_JoyConController_Button_R = RShoulder +Controls_Right_JoyConController_Button_ZR = RTrigger + +Controls_Left_JoyConController_Stick = LJoystick +Controls_Right_JoyConController_Stick = RJoystick \ No newline at end of file diff --git a/Ryujinx/Config.cs b/Ryujinx/Config.cs index 7a78d6c6f6..7c890bfd9f 100644 --- a/Ryujinx/Config.cs +++ b/Ryujinx/Config.cs @@ -109,28 +109,28 @@ namespace Ryujinx { Left = new JoyConControllerLeft { - Stick = Parser.Value("Controls_Left_JoyConController_Stick"), - StickButton = Parser.Value("Controls_Left_JoyConController_Stick_Button"), - DPadUp = Parser.Value("Controls_Left_JoyConController_DPad_Up"), - DPadDown = Parser.Value("Controls_Left_JoyConController_DPad_Down"), - DPadLeft = Parser.Value("Controls_Left_JoyConController_DPad_Left"), - DPadRight = Parser.Value("Controls_Left_JoyConController_DPad_Right"), - ButtonMinus = Parser.Value("Controls_Left_JoyConController_Button_Minus"), - ButtonL = Parser.Value("Controls_Left_JoyConController_Button_L"), - ButtonZL = Parser.Value("Controls_Left_JoyConController_Button_ZL") + Stick = Enum.Parse(Parser.Value("Controls_Left_JoyConController_Stick")), + StickButton = Enum.Parse(Parser.Value("Controls_Left_JoyConController_Stick_Button")), + DPadUp = Enum.Parse(Parser.Value("Controls_Left_JoyConController_DPad_Up")), + DPadDown = Enum.Parse(Parser.Value("Controls_Left_JoyConController_DPad_Down")), + DPadLeft = Enum.Parse(Parser.Value("Controls_Left_JoyConController_DPad_Left")), + DPadRight = Enum.Parse(Parser.Value("Controls_Left_JoyConController_DPad_Right")), + ButtonMinus = Enum.Parse(Parser.Value("Controls_Left_JoyConController_Button_Minus")), + ButtonL = Enum.Parse(Parser.Value("Controls_Left_JoyConController_Button_L")), + ButtonZL = Enum.Parse(Parser.Value("Controls_Left_JoyConController_Button_ZL")) }, Right = new JoyConControllerRight { - Stick = Parser.Value("Controls_Right_JoyConController_Stick"), - StickButton = Parser.Value("Controls_Right_JoyConController_Stick_Button"), - ButtonA = Parser.Value("Controls_Right_JoyConController_Button_A"), - ButtonB = Parser.Value("Controls_Right_JoyConController_Button_B"), - ButtonX = Parser.Value("Controls_Right_JoyConController_Button_X"), - ButtonY = Parser.Value("Controls_Right_JoyConController_Button_Y"), - ButtonPlus = Parser.Value("Controls_Right_JoyConController_Button_Plus"), - ButtonR = Parser.Value("Controls_Right_JoyConController_Button_R"), - ButtonZR = Parser.Value("Controls_Right_JoyConController_Button_ZR") + Stick = Enum.Parse(Parser.Value("Controls_Right_JoyConController_Stick")), + StickButton = Enum.Parse(Parser.Value("Controls_Right_JoyConController_Stick_Button")), + ButtonA = Enum.Parse(Parser.Value("Controls_Right_JoyConController_Button_A")), + ButtonB = Enum.Parse(Parser.Value("Controls_Right_JoyConController_Button_B")), + ButtonX = Enum.Parse(Parser.Value("Controls_Right_JoyConController_Button_X")), + ButtonY = Enum.Parse(Parser.Value("Controls_Right_JoyConController_Button_Y")), + ButtonPlus = Enum.Parse(Parser.Value("Controls_Right_JoyConController_Button_Plus")), + ButtonR = Enum.Parse(Parser.Value("Controls_Right_JoyConController_Button_R")), + ButtonZR = Enum.Parse(Parser.Value("Controls_Right_JoyConController_Button_ZR")) } }; } diff --git a/Ryujinx/Ui/GLScreen.cs b/Ryujinx/Ui/GLScreen.cs index 9b5dda4f0c..e7a74bbe8a 100644 --- a/Ryujinx/Ui/GLScreen.cs +++ b/Ryujinx/Ui/GLScreen.cs @@ -4,6 +4,7 @@ using OpenTK.Input; using Ryujinx.Graphics.Gal; using Ryujinx.HLE; using Ryujinx.HLE.Input; +using Ryujinx.Common.Input; using System; using System.Threading; @@ -130,57 +131,57 @@ namespace Ryujinx } } - private bool IsGamePadButtonPressedFromString(GamePadState GamePad, string Button) + private bool IsGamePadButtonPressed(GamePadState GamePad, GamePadButton Button) { - if (Button.ToUpper() == "LTRIGGER" || Button.ToUpper() == "RTRIGGER") + if (Button == GamePadButton.LTrigger || Button == GamePadButton.RTrigger) { - return GetGamePadTriggerFromString(GamePad, Button) >= Config.GamePadTriggerThreshold; + return GetGamePadTrigger(GamePad, Button) >= Config.GamePadTriggerThreshold; } else { - return (GetGamePadButtonFromString(GamePad, Button) == ButtonState.Pressed); + return (GetGamePadButton(GamePad, Button) == ButtonState.Pressed); } } - private ButtonState GetGamePadButtonFromString(GamePadState GamePad, string Button) + private ButtonState GetGamePadButton(GamePadState GamePad, GamePadButton Button) { - switch (Button.ToUpper()) + switch (Button) { - case "A": return GamePad.Buttons.A; - case "B": return GamePad.Buttons.B; - case "X": return GamePad.Buttons.X; - case "Y": return GamePad.Buttons.Y; - case "LSTICK": return GamePad.Buttons.LeftStick; - case "RSTICK": return GamePad.Buttons.RightStick; - case "LSHOULDER": return GamePad.Buttons.LeftShoulder; - case "RSHOULDER": return GamePad.Buttons.RightShoulder; - case "DPADUP": return GamePad.DPad.Up; - case "DPADDOWN": return GamePad.DPad.Down; - case "DPADLEFT": return GamePad.DPad.Left; - case "DPADRIGHT": return GamePad.DPad.Right; - case "START": return GamePad.Buttons.Start; - case "BACK": return GamePad.Buttons.Back; - default: throw new ArgumentException(); + case GamePadButton.A: return GamePad.Buttons.A; + case GamePadButton.B: return GamePad.Buttons.B; + case GamePadButton.X: return GamePad.Buttons.X; + case GamePadButton.Y: return GamePad.Buttons.Y; + case GamePadButton.LStick: return GamePad.Buttons.LeftStick; + case GamePadButton.RStick: return GamePad.Buttons.RightStick; + case GamePadButton.LShoulder: return GamePad.Buttons.LeftShoulder; + case GamePadButton.RShoulder: return GamePad.Buttons.RightShoulder; + case GamePadButton.DPadUp: return GamePad.DPad.Up; + case GamePadButton.DPadDown: return GamePad.DPad.Down; + case GamePadButton.DPadLeft: return GamePad.DPad.Left; + case GamePadButton.DPadRight: return GamePad.DPad.Right; + case GamePadButton.Start: return GamePad.Buttons.Start; + case GamePadButton.Back: return GamePad.Buttons.Back; + default: throw new ArgumentException(); } } - private float GetGamePadTriggerFromString(GamePadState GamePad, string Trigger) + private float GetGamePadTrigger(GamePadState GamePad, GamePadButton Trigger) { - switch (Trigger.ToUpper()) + switch (Trigger) { - case "LTRIGGER": return GamePad.Triggers.Left; - case "RTRIGGER": return GamePad.Triggers.Right; - default: throw new ArgumentException(); + case GamePadButton.LTrigger: return GamePad.Triggers.Left; + case GamePadButton.RTrigger: return GamePad.Triggers.Right; + default: throw new ArgumentException(); } } - private Vector2 GetJoystickAxisFromString(GamePadState GamePad, string Joystick) + private Vector2 GetJoystickAxisFromString(GamePadState GamePad, GamePadStick Joystick) { - switch (Joystick.ToUpper()) + switch (Joystick) { - case "LJOYSTICK": return GamePad.ThumbSticks.Left; - case "RJOYSTICK": return new Vector2(-GamePad.ThumbSticks.Right.Y, -GamePad.ThumbSticks.Right.X); - default: throw new ArgumentException(); + case GamePadStick.LJoystick: return GamePad.ThumbSticks.Left; + case GamePadStick.RJoystick: return new Vector2(-GamePad.ThumbSticks.Right.Y, -GamePad.ThumbSticks.Right.X); + default: throw new ArgumentException(); } } @@ -241,24 +242,24 @@ namespace Ryujinx { GamePadState GamePad = OpenTK.Input.GamePad.GetState(Config.GamePadIndex); //LeftButtons - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.DPadUp)) CurrentButton |= HidControllerButtons.KEY_DUP; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.DPadDown)) CurrentButton |= HidControllerButtons.KEY_DDOWN; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.DPadLeft)) CurrentButton |= HidControllerButtons.KEY_DLEFT; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.DPadRight)) CurrentButton |= HidControllerButtons.KEY_DRIGHT; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.StickButton)) CurrentButton |= HidControllerButtons.KEY_LSTICK; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.ButtonMinus)) CurrentButton |= HidControllerButtons.KEY_MINUS; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.ButtonL)) CurrentButton |= HidControllerButtons.KEY_L; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.ButtonZL)) CurrentButton |= HidControllerButtons.KEY_ZL; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.DPadUp)) CurrentButton |= HidControllerButtons.KEY_DUP; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.DPadDown)) CurrentButton |= HidControllerButtons.KEY_DDOWN; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.DPadLeft)) CurrentButton |= HidControllerButtons.KEY_DLEFT; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.DPadRight)) CurrentButton |= HidControllerButtons.KEY_DRIGHT; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.StickButton)) CurrentButton |= HidControllerButtons.KEY_LSTICK; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.ButtonMinus)) CurrentButton |= HidControllerButtons.KEY_MINUS; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.ButtonL)) CurrentButton |= HidControllerButtons.KEY_L; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Left.ButtonZL)) CurrentButton |= HidControllerButtons.KEY_ZL; //RightButtons - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonA)) CurrentButton |= HidControllerButtons.KEY_A; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonB)) CurrentButton |= HidControllerButtons.KEY_B; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonX)) CurrentButton |= HidControllerButtons.KEY_X; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonY)) CurrentButton |= HidControllerButtons.KEY_Y; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.StickButton)) CurrentButton |= HidControllerButtons.KEY_RSTICK; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonPlus)) CurrentButton |= HidControllerButtons.KEY_PLUS; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonR)) CurrentButton |= HidControllerButtons.KEY_R; - if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonZR)) CurrentButton |= HidControllerButtons.KEY_ZR; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonA)) CurrentButton |= HidControllerButtons.KEY_A; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonB)) CurrentButton |= HidControllerButtons.KEY_B; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonX)) CurrentButton |= HidControllerButtons.KEY_X; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonY)) CurrentButton |= HidControllerButtons.KEY_Y; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.StickButton)) CurrentButton |= HidControllerButtons.KEY_RSTICK; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonPlus)) CurrentButton |= HidControllerButtons.KEY_PLUS; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonR)) CurrentButton |= HidControllerButtons.KEY_R; + if (IsGamePadButtonPressed(GamePad, Config.JoyConController.Right.ButtonZR)) CurrentButton |= HidControllerButtons.KEY_ZR; //LeftJoystick if (GetJoystickAxisFromString(GamePad, Config.JoyConController.Left.Stick).X >= AnalogStickDeadzone