NpadController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using OpenTK;
  2. using OpenTK.Input;
  3. using Ryujinx.Common.Configuration.Hid;
  4. using Ryujinx.HLE.Input;
  5. using System;
  6. using InnerNpadController = Ryujinx.Common.Configuration.Hid.NpadController;
  7. namespace Ryujinx.Ui.Input
  8. {
  9. public class NpadController
  10. {
  11. private InnerNpadController _inner;
  12. // NOTE: This should be initialized AFTER GTK for compat reasons with OpenTK SDL2 backend and GTK on Linux.
  13. // BODY: Usage of Joystick.GetState must be defer to after GTK full initialization. Otherwise, GTK will segfault because SDL2 was already init *sighs*
  14. public NpadController(InnerNpadController inner)
  15. {
  16. _inner = inner;
  17. }
  18. private bool IsEnabled()
  19. {
  20. return _inner.Enabled && Joystick.GetState(_inner.Index).IsConnected;
  21. }
  22. public ControllerButtons GetButtons()
  23. {
  24. if (!IsEnabled())
  25. {
  26. return 0;
  27. }
  28. JoystickState joystickState = Joystick.GetState(_inner.Index);
  29. ControllerButtons buttons = 0;
  30. if (IsActivated(joystickState, _inner.LeftJoycon.DPadUp)) buttons |= ControllerButtons.DpadUp;
  31. if (IsActivated(joystickState, _inner.LeftJoycon.DPadDown)) buttons |= ControllerButtons.DpadDown;
  32. if (IsActivated(joystickState, _inner.LeftJoycon.DPadLeft)) buttons |= ControllerButtons.DpadLeft;
  33. if (IsActivated(joystickState, _inner.LeftJoycon.DPadRight)) buttons |= ControllerButtons.DPadRight;
  34. if (IsActivated(joystickState, _inner.LeftJoycon.StickButton)) buttons |= ControllerButtons.StickLeft;
  35. if (IsActivated(joystickState, _inner.LeftJoycon.ButtonMinus)) buttons |= ControllerButtons.Minus;
  36. if (IsActivated(joystickState, _inner.LeftJoycon.ButtonL)) buttons |= ControllerButtons.L;
  37. if (IsActivated(joystickState, _inner.LeftJoycon.ButtonZl)) buttons |= ControllerButtons.Zl;
  38. if (IsActivated(joystickState, _inner.RightJoycon.ButtonA)) buttons |= ControllerButtons.A;
  39. if (IsActivated(joystickState, _inner.RightJoycon.ButtonB)) buttons |= ControllerButtons.B;
  40. if (IsActivated(joystickState, _inner.RightJoycon.ButtonX)) buttons |= ControllerButtons.X;
  41. if (IsActivated(joystickState, _inner.RightJoycon.ButtonY)) buttons |= ControllerButtons.Y;
  42. if (IsActivated(joystickState, _inner.RightJoycon.StickButton)) buttons |= ControllerButtons.StickRight;
  43. if (IsActivated(joystickState, _inner.RightJoycon.ButtonPlus)) buttons |= ControllerButtons.Plus;
  44. if (IsActivated(joystickState, _inner.RightJoycon.ButtonR)) buttons |= ControllerButtons.R;
  45. if (IsActivated(joystickState, _inner.RightJoycon.ButtonZr)) buttons |= ControllerButtons.Zr;
  46. return buttons;
  47. }
  48. private bool IsActivated(JoystickState joystickState,ControllerInputId controllerInputId)
  49. {
  50. if (controllerInputId <= ControllerInputId.Button20)
  51. {
  52. return joystickState.IsButtonDown((int)controllerInputId);
  53. }
  54. else if (controllerInputId <= ControllerInputId.Axis5)
  55. {
  56. int axis = controllerInputId - ControllerInputId.Axis0;
  57. return joystickState.GetAxis(axis) > _inner.TriggerThreshold;
  58. }
  59. else if (controllerInputId <= ControllerInputId.Hat2Right)
  60. {
  61. int hat = (controllerInputId - ControllerInputId.Hat0Up) / 4;
  62. int baseHatId = (int)ControllerInputId.Hat0Up + (hat * 4);
  63. JoystickHatState hatState = joystickState.GetHat((JoystickHat)hat);
  64. if (hatState.IsUp && ((int)controllerInputId % baseHatId == 0)) return true;
  65. if (hatState.IsDown && ((int)controllerInputId % baseHatId == 1)) return true;
  66. if (hatState.IsLeft && ((int)controllerInputId % baseHatId == 2)) return true;
  67. if (hatState.IsRight && ((int)controllerInputId % baseHatId == 3)) return true;
  68. }
  69. return false;
  70. }
  71. public (short, short) GetLeftStick()
  72. {
  73. if (!IsEnabled())
  74. {
  75. return (0, 0);
  76. }
  77. return GetStick(_inner.LeftJoycon.Stick);
  78. }
  79. public (short, short) GetRightStick()
  80. {
  81. if (!IsEnabled())
  82. {
  83. return (0, 0);
  84. }
  85. return GetStick(_inner.RightJoycon.Stick);
  86. }
  87. private (short, short) GetStick(ControllerInputId stickInputId)
  88. {
  89. if (stickInputId < ControllerInputId.Axis0 || stickInputId > ControllerInputId.Axis5)
  90. {
  91. return (0, 0);
  92. }
  93. JoystickState jsState = Joystick.GetState(_inner.Index);
  94. int xAxis = stickInputId - ControllerInputId.Axis0;
  95. float xValue = jsState.GetAxis(xAxis);
  96. float yValue = 0 - jsState.GetAxis(xAxis + 1); // Invert Y-axis
  97. return ApplyDeadzone(new Vector2(xValue, yValue));
  98. }
  99. private (short, short) ApplyDeadzone(Vector2 axis)
  100. {
  101. return (ClampAxis(MathF.Abs(axis.X) > _inner.Deadzone ? axis.X : 0f),
  102. ClampAxis(MathF.Abs(axis.Y) > _inner.Deadzone ? axis.Y : 0f));
  103. }
  104. private static short ClampAxis(float value)
  105. {
  106. if (value <= -short.MaxValue)
  107. {
  108. return -short.MaxValue;
  109. }
  110. else
  111. {
  112. return (short)(value * short.MaxValue);
  113. }
  114. }
  115. }
  116. }