JoyConController.cs 8.0 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 JoyConControllerLeft
  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 JoyConControllerRight
  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 JoyConController
  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 JoyConControllerLeft Left { private set; get; }
  60. public JoyConControllerRight Right { private set; get; }
  61. public JoyConController(
  62. bool Enabled,
  63. int Index,
  64. float Deadzone,
  65. float TriggerThreshold,
  66. JoyConControllerLeft Left,
  67. JoyConControllerRight Right)
  68. {
  69. this.Enabled = Enabled;
  70. this.Index = Index;
  71. this.Deadzone = Deadzone;
  72. this.TriggerThreshold = TriggerThreshold;
  73. this.Left = Left;
  74. this.Right = Right;
  75. //Unmapped controllers are problematic, skip them
  76. if (GamePad.GetName(Index) == "Unmapped Controller")
  77. {
  78. this.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.KEY_DUP;
  90. if (IsPressed(GpState, Left.DPadDown)) Buttons |= HidControllerButtons.KEY_DDOWN;
  91. if (IsPressed(GpState, Left.DPadLeft)) Buttons |= HidControllerButtons.KEY_DLEFT;
  92. if (IsPressed(GpState, Left.DPadRight)) Buttons |= HidControllerButtons.KEY_DRIGHT;
  93. if (IsPressed(GpState, Left.StickButton)) Buttons |= HidControllerButtons.KEY_LSTICK;
  94. if (IsPressed(GpState, Left.ButtonMinus)) Buttons |= HidControllerButtons.KEY_MINUS;
  95. if (IsPressed(GpState, Left.ButtonL)) Buttons |= HidControllerButtons.KEY_L;
  96. if (IsPressed(GpState, Left.ButtonZL)) Buttons |= HidControllerButtons.KEY_ZL;
  97. if (IsPressed(GpState, Right.ButtonA)) Buttons |= HidControllerButtons.KEY_A;
  98. if (IsPressed(GpState, Right.ButtonB)) Buttons |= HidControllerButtons.KEY_B;
  99. if (IsPressed(GpState, Right.ButtonX)) Buttons |= HidControllerButtons.KEY_X;
  100. if (IsPressed(GpState, Right.ButtonY)) Buttons |= HidControllerButtons.KEY_Y;
  101. if (IsPressed(GpState, Right.StickButton)) Buttons |= HidControllerButtons.KEY_RSTICK;
  102. if (IsPressed(GpState, Right.ButtonPlus)) Buttons |= HidControllerButtons.KEY_PLUS;
  103. if (IsPressed(GpState, Right.ButtonR)) Buttons |= HidControllerButtons.KEY_R;
  104. if (IsPressed(GpState, Right.ButtonZR)) Buttons |= HidControllerButtons.KEY_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. }