NpadController.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using OpenTK;
  2. using OpenTK.Input;
  3. using Ryujinx.HLE.Input;
  4. using System;
  5. namespace Ryujinx.UI.Input
  6. {
  7. public enum ControllerInputId
  8. {
  9. Invalid,
  10. LStick,
  11. DPadUp,
  12. DPadDown,
  13. DPadLeft,
  14. DPadRight,
  15. Back,
  16. LShoulder,
  17. RStick,
  18. A,
  19. B,
  20. X,
  21. Y,
  22. Start,
  23. RShoulder,
  24. LTrigger,
  25. RTrigger,
  26. LJoystick,
  27. RJoystick
  28. }
  29. public struct NpadControllerLeft
  30. {
  31. public ControllerInputId Stick;
  32. public ControllerInputId StickButton;
  33. public ControllerInputId DPadUp;
  34. public ControllerInputId DPadDown;
  35. public ControllerInputId DPadLeft;
  36. public ControllerInputId DPadRight;
  37. public ControllerInputId ButtonMinus;
  38. public ControllerInputId ButtonL;
  39. public ControllerInputId ButtonZl;
  40. }
  41. public struct NpadControllerRight
  42. {
  43. public ControllerInputId Stick;
  44. public ControllerInputId StickButton;
  45. public ControllerInputId ButtonA;
  46. public ControllerInputId ButtonB;
  47. public ControllerInputId ButtonX;
  48. public ControllerInputId ButtonY;
  49. public ControllerInputId ButtonPlus;
  50. public ControllerInputId ButtonR;
  51. public ControllerInputId ButtonZr;
  52. }
  53. public class NpadController
  54. {
  55. public bool Enabled { private set; get; }
  56. public int Index { private set; get; }
  57. public float Deadzone { private set; get; }
  58. public float TriggerThreshold { private set; get; }
  59. public NpadControllerLeft Left { private set; get; }
  60. public NpadControllerRight Right { private set; get; }
  61. public NpadController(
  62. bool enabled,
  63. int index,
  64. float deadzone,
  65. float triggerThreshold,
  66. NpadControllerLeft left,
  67. NpadControllerRight right)
  68. {
  69. Enabled = enabled;
  70. Index = index;
  71. Deadzone = deadzone;
  72. TriggerThreshold = triggerThreshold;
  73. Left = left;
  74. Right = right;
  75. //Unmapped controllers are problematic, skip them
  76. if (GamePad.GetName(index) == "Unmapped Controller")
  77. {
  78. Enabled = false;
  79. }
  80. }
  81. public HidControllerButtons GetButtons()
  82. {
  83. if (!Enabled)
  84. {
  85. return 0;
  86. }
  87. GamePadState gpState = GamePad.GetState(Index);
  88. HidControllerButtons buttons = 0;
  89. if (IsPressed(gpState, Left.DPadUp)) buttons |= HidControllerButtons.DpadUp;
  90. if (IsPressed(gpState, Left.DPadDown)) buttons |= HidControllerButtons.DpadDown;
  91. if (IsPressed(gpState, Left.DPadLeft)) buttons |= HidControllerButtons.DpadLeft;
  92. if (IsPressed(gpState, Left.DPadRight)) buttons |= HidControllerButtons.DPadRight;
  93. if (IsPressed(gpState, Left.StickButton)) buttons |= HidControllerButtons.StickLeft;
  94. if (IsPressed(gpState, Left.ButtonMinus)) buttons |= HidControllerButtons.Minus;
  95. if (IsPressed(gpState, Left.ButtonL)) buttons |= HidControllerButtons.L;
  96. if (IsPressed(gpState, Left.ButtonZl)) buttons |= HidControllerButtons.Zl;
  97. if (IsPressed(gpState, Right.ButtonA)) buttons |= HidControllerButtons.A;
  98. if (IsPressed(gpState, Right.ButtonB)) buttons |= HidControllerButtons.B;
  99. if (IsPressed(gpState, Right.ButtonX)) buttons |= HidControllerButtons.X;
  100. if (IsPressed(gpState, Right.ButtonY)) buttons |= HidControllerButtons.Y;
  101. if (IsPressed(gpState, Right.StickButton)) buttons |= HidControllerButtons.StickRight;
  102. if (IsPressed(gpState, Right.ButtonPlus)) buttons |= HidControllerButtons.Plus;
  103. if (IsPressed(gpState, Right.ButtonR)) buttons |= HidControllerButtons.R;
  104. if (IsPressed(gpState, Right.ButtonZr)) buttons |= HidControllerButtons.Zr;
  105. return buttons;
  106. }
  107. public (short, short) GetLeftStick()
  108. {
  109. if (!Enabled)
  110. {
  111. return (0, 0);
  112. }
  113. return GetStick(Left.Stick);
  114. }
  115. public (short, short) GetRightStick()
  116. {
  117. if (!Enabled)
  118. {
  119. return (0, 0);
  120. }
  121. return GetStick(Right.Stick);
  122. }
  123. private (short, short) GetStick(ControllerInputId joystick)
  124. {
  125. GamePadState gpState = GamePad.GetState(Index);
  126. switch (joystick)
  127. {
  128. case ControllerInputId.LJoystick:
  129. return ApplyDeadzone(gpState.ThumbSticks.Left);
  130. case ControllerInputId.RJoystick:
  131. return ApplyDeadzone(gpState.ThumbSticks.Right);
  132. default:
  133. return (0, 0);
  134. }
  135. }
  136. private (short, short) ApplyDeadzone(Vector2 axis)
  137. {
  138. return (ClampAxis(MathF.Abs(axis.X) > Deadzone ? axis.X : 0f),
  139. ClampAxis(MathF.Abs(axis.Y) > Deadzone ? axis.Y : 0f));
  140. }
  141. private static short ClampAxis(float value)
  142. {
  143. if (value <= -short.MaxValue)
  144. {
  145. return -short.MaxValue;
  146. }
  147. else
  148. {
  149. return (short)(value * short.MaxValue);
  150. }
  151. }
  152. private bool IsPressed(GamePadState gpState, ControllerInputId button)
  153. {
  154. switch (button)
  155. {
  156. case ControllerInputId.A: return gpState.Buttons.A == ButtonState.Pressed;
  157. case ControllerInputId.B: return gpState.Buttons.B == ButtonState.Pressed;
  158. case ControllerInputId.X: return gpState.Buttons.X == ButtonState.Pressed;
  159. case ControllerInputId.Y: return gpState.Buttons.Y == ButtonState.Pressed;
  160. case ControllerInputId.LStick: return gpState.Buttons.LeftStick == ButtonState.Pressed;
  161. case ControllerInputId.RStick: return gpState.Buttons.RightStick == ButtonState.Pressed;
  162. case ControllerInputId.LShoulder: return gpState.Buttons.LeftShoulder == ButtonState.Pressed;
  163. case ControllerInputId.RShoulder: return gpState.Buttons.RightShoulder == ButtonState.Pressed;
  164. case ControllerInputId.DPadUp: return gpState.DPad.Up == ButtonState.Pressed;
  165. case ControllerInputId.DPadDown: return gpState.DPad.Down == ButtonState.Pressed;
  166. case ControllerInputId.DPadLeft: return gpState.DPad.Left == ButtonState.Pressed;
  167. case ControllerInputId.DPadRight: return gpState.DPad.Right == ButtonState.Pressed;
  168. case ControllerInputId.Start: return gpState.Buttons.Start == ButtonState.Pressed;
  169. case ControllerInputId.Back: return gpState.Buttons.Back == ButtonState.Pressed;
  170. case ControllerInputId.LTrigger: return gpState.Triggers.Left >= TriggerThreshold;
  171. case ControllerInputId.RTrigger: return gpState.Triggers.Right >= TriggerThreshold;
  172. //Using thumbsticks as buttons is not common, but it would be nice not to ignore them
  173. case ControllerInputId.LJoystick:
  174. return gpState.ThumbSticks.Left.X >= Deadzone ||
  175. gpState.ThumbSticks.Left.Y >= Deadzone;
  176. case ControllerInputId.RJoystick:
  177. return gpState.ThumbSticks.Right.X >= Deadzone ||
  178. gpState.ThumbSticks.Right.Y >= Deadzone;
  179. default:
  180. return false;
  181. }
  182. }
  183. }
  184. }