説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Gamepad.cs 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. using System;
  2. using System.ComponentModel;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine.InputSystem.Controls;
  5. using UnityEngine.InputSystem.Haptics;
  6. using UnityEngine.InputSystem.Layouts;
  7. using UnityEngine.InputSystem.LowLevel;
  8. using UnityEngine.InputSystem.Utilities;
  9. using UnityEngine.Scripting;
  10. ////TODO: come up with consistent naming for buttons; (xxxButton? xxx?)
  11. ////REVIEW: should we add a gyro as a standard feature of gamepads?
  12. ////TODO: allow to be used for mouse simulation
  13. namespace UnityEngine.InputSystem.LowLevel
  14. {
  15. /// <summary>
  16. /// Default state layout for gamepads.
  17. /// </summary>
  18. /// <remarks>
  19. /// Be aware that unlike some other devices such as <see cref="Mouse"/> or <see cref="Touchscreen"/>,
  20. /// gamepad devices tend to have wildly varying state formats, i.e. forms in which they internally
  21. /// store their input data. In practice, even on the same platform gamepads will often store
  22. /// their data in different formats. This means that <see cref="GamepadState"/> will often <em>not</em>
  23. /// be the format in which a particular gamepad (such as <see cref="XInput.XInputController"/>,
  24. /// for example) stores its data.
  25. ///
  26. /// If your gamepad data is arriving in a different format, you should extend the "Gamepad" layout and customize its Controls.
  27. ///
  28. /// A real-world example of this is the Xbox Controller on macOS, which is supported through HID. Its layout looks like this:
  29. ///
  30. /// <example>
  31. /// <code>
  32. /// {
  33. /// "name" : "XboxGamepadOSX",
  34. /// "extend" : "Gamepad",
  35. /// "format" : "HID",
  36. /// "device" : { "interface" : "HID", "product" : "Xbox.*Controller" },
  37. /// "controls" : [
  38. /// { "name" : "leftShoulder", "offset" : 2, "bit" : 8 },
  39. /// { "name" : "rightShoulder", "offset" : 2, "bit" : 9 },
  40. /// { "name" : "leftStickPress", "offset" : 2, "bit" : 14 },
  41. /// { "name" : "rightStickPress", "offset" : 2, "bit" : 15 },
  42. /// { "name" : "buttonSouth", "offset" : 2, "bit" : 12 },
  43. /// { "name" : "buttonEast", "offset" : 2, "bit" : 13 },
  44. /// { "name" : "buttonWest", "offset" : 2, "bit" : 14 },
  45. /// { "name" : "buttonNorth", "offset" : 2, "bit" : 15 },
  46. /// { "name" : "dpad", "offset" : 2 },
  47. /// { "name" : "dpad/up", "offset" : 0, "bit" : 8 },
  48. /// { "name" : "dpad/down", "offset" : 0, "bit" : 9 },
  49. /// { "name" : "dpad/left", "offset" : 0, "bit" : 10 },
  50. /// { "name" : "dpad/right", "offset" : 0, "bit" : 11 },
  51. /// { "name" : "start", "offset" : 2, "bit" : 4 },
  52. /// { "name" : "select", "offset" : 2, "bit" : 5 },
  53. /// { "name" : "xbox", "offset" : 2, "bit" : 2, "layout" : "Button" },
  54. /// { "name" : "leftTrigger", "offset" : 4, "format" : "BYTE" },
  55. /// { "name" : "rightTrigger", "offset" : 5, "format" : "BYTE" },
  56. /// { "name" : "leftStick", "offset" : 6, "format" : "VC2S" },
  57. /// { "name" : "leftStick/x", "offset" : 0, "format" : "SHRT", "parameters" : "normalize,normalizeMin=-0.5,normalizeMax=0.5" },
  58. /// { "name" : "leftStick/y", "offset" : 2, "format" : "SHRT", "parameters" : "invert,normalize,normalizeMin=-0.5,normalizeMax=0.5" },
  59. /// { "name" : "rightStick", "offset" : 10, "format" : "VC2S" },
  60. /// { "name" : "rightStick/x", "offset" : 0, "format" : "SHRT", "parameters" : "normalize,normalizeMin=-0.5,normalizeMax=0.5" },
  61. /// { "name" : "rightStick/y", "offset" : 2, "format" : "SHRT", "parameters" : "invert,normalize,normalizeMin=-0.5,normalizeMax=0.5" }
  62. /// ]
  63. /// }
  64. /// </code>
  65. /// </example>
  66. ///
  67. /// The same principle applies if some buttons on your Device are swapped, for example. In this case, you can remap their offsets.
  68. ///
  69. ///
  70. ///
  71. ///
  72. /// </remarks>
  73. /// <seealso cref="Gamepad"/>
  74. // NOTE: Must match GamepadInputState in native.
  75. [StructLayout(LayoutKind.Explicit, Size = 28)]
  76. public struct GamepadState : IInputStateTypeInfo
  77. {
  78. public static FourCC Format => new FourCC('G', 'P', 'A', 'D');
  79. // On Sony consoles, we use the platform defaults as the gamepad-wide short default names.
  80. #if UNITY_PS4 || UNITY_PS5
  81. internal const string ButtonSouthShortDisplayName = "Cross";
  82. internal const string ButtonNorthShortDisplayName = "Triangle";
  83. internal const string ButtonWestShortDisplayName = "Square";
  84. internal const string ButtonEastShortDisplayName = "East";
  85. #elif UNITY_SWITCH
  86. internal const string ButtonSouthShortDisplayName = "B";
  87. internal const string ButtonNorthShortDisplayName = "X";
  88. internal const string ButtonWestShortDisplayName = "Y";
  89. internal const string ButtonEastShortDisplayName = "A";
  90. #else
  91. internal const string ButtonSouthShortDisplayName = "A";
  92. internal const string ButtonNorthShortDisplayName = "Y";
  93. internal const string ButtonWestShortDisplayName = "X";
  94. internal const string ButtonEastShortDisplayName = "B";
  95. #endif
  96. /// <summary>
  97. /// Button bit mask.
  98. /// </summary>
  99. /// <value>Button bit mask.</value>
  100. /// <seealso cref="GamepadButton"/>
  101. /// <seealso cref="Gamepad.buttonSouth"/>
  102. /// <seealso cref="Gamepad.buttonNorth"/>
  103. /// <seealso cref="Gamepad.buttonWest"/>
  104. /// <seealso cref="Gamepad.buttonSouth"/>
  105. /// <seealso cref="Gamepad.leftShoulder"/>
  106. /// <seealso cref="Gamepad.rightShoulder"/>
  107. /// <seealso cref="Gamepad.startButton"/>
  108. /// <seealso cref="Gamepad.selectButton"/>
  109. /// <seealso cref="Gamepad.leftStickButton"/>
  110. /// <seealso cref="Gamepad.rightStickButton"/>
  111. ////REVIEW: do we want the name to correspond to what's actually on the device?
  112. [InputControl(name = "dpad", layout = "Dpad", usage = "Hatswitch", displayName = "D-Pad", format = "BIT", sizeInBits = 4, bit = 0)]
  113. [InputControl(name = "buttonSouth", layout = "Button", bit = (uint)GamepadButton.South, usages = new[] { "PrimaryAction", "Submit" }, aliases = new[] { "a", "cross" }, displayName = "Button South", shortDisplayName = ButtonSouthShortDisplayName)]
  114. [InputControl(name = "buttonWest", layout = "Button", bit = (uint)GamepadButton.West, usage = "SecondaryAction", aliases = new[] { "x", "square" }, displayName = "Button West", shortDisplayName = ButtonWestShortDisplayName)]
  115. [InputControl(name = "buttonNorth", layout = "Button", bit = (uint)GamepadButton.North, aliases = new[] { "y", "triangle" }, displayName = "Button North", shortDisplayName = ButtonNorthShortDisplayName)]
  116. [InputControl(name = "buttonEast", layout = "Button", bit = (uint)GamepadButton.East, usages = new[] { "Back", "Cancel" }, aliases = new[] { "b", "circle" }, displayName = "Button East", shortDisplayName = ButtonEastShortDisplayName)]
  117. ////FIXME: 'Press' naming is inconsistent with 'Button' naming
  118. [InputControl(name = "leftStickPress", layout = "Button", bit = (uint)GamepadButton.LeftStick, displayName = "Left Stick Press")]
  119. [InputControl(name = "rightStickPress", layout = "Button", bit = (uint)GamepadButton.RightStick, displayName = "Right Stick Press")]
  120. [InputControl(name = "leftShoulder", layout = "Button", bit = (uint)GamepadButton.LeftShoulder, displayName = "Left Shoulder", shortDisplayName = "LB")]
  121. [InputControl(name = "rightShoulder", layout = "Button", bit = (uint)GamepadButton.RightShoulder, displayName = "Right Shoulder", shortDisplayName = "RB")]
  122. ////REVIEW: seems like these two should get less ambiguous names as well
  123. [InputControl(name = "start", layout = "Button", bit = (uint)GamepadButton.Start, usage = "Menu", displayName = "Start")]
  124. [InputControl(name = "select", layout = "Button", bit = (uint)GamepadButton.Select, displayName = "Select")]
  125. [FieldOffset(0)]
  126. public uint buttons;
  127. /// <summary>
  128. /// Left stick position. Each axis goes from -1 to 1 with
  129. /// 0 being center position.
  130. /// </summary>
  131. /// <value>Left stick position.</value>
  132. /// <seealso cref="Gamepad.leftStick"/>
  133. [InputControl(layout = "Stick", usage = "Primary2DMotion", processors = "stickDeadzone", displayName = "Left Stick", shortDisplayName = "LS")]
  134. [FieldOffset(4)]
  135. public Vector2 leftStick;
  136. /// <summary>
  137. /// Right stick position. Each axis from -1 to 1 with
  138. /// 0 being center position.
  139. /// </summary>
  140. /// <value>Right stick position.</value>
  141. /// <seealso cref="Gamepad.rightStick"/>
  142. [InputControl(layout = "Stick", usage = "Secondary2DMotion", processors = "stickDeadzone", displayName = "Right Stick", shortDisplayName = "RS")]
  143. [FieldOffset(12)]
  144. public Vector2 rightStick;
  145. ////REVIEW: should left and right trigger get deadzones?
  146. /// <summary>
  147. /// Position of the left trigger. Goes from 0 (not pressed) to 1 (fully pressed).
  148. /// </summary>
  149. /// <value>Position of left trigger.</value>
  150. /// <seealso cref="Gamepad.leftTrigger"/>
  151. [InputControl(layout = "Button", format = "FLT", usage = "SecondaryTrigger", displayName = "Left Trigger", shortDisplayName = "LT")]
  152. [FieldOffset(20)]
  153. public float leftTrigger;
  154. /// <summary>
  155. /// Position of the right trigger. Goes from 0 (not pressed) to 1 (fully pressed).
  156. /// </summary>
  157. /// <value>Position of right trigger.</value>
  158. /// <seealso cref="Gamepad.rightTrigger"/>
  159. [InputControl(layout = "Button", format = "FLT", usage = "SecondaryTrigger", displayName = "Right Trigger", shortDisplayName = "RT")]
  160. [FieldOffset(24)]
  161. public float rightTrigger;
  162. /// <summary>
  163. /// State format tag for GamepadState.
  164. /// </summary>
  165. /// <value>Returns "GPAD".</value>
  166. public FourCC format => Format;
  167. /// <summary>
  168. /// Create a gamepad state with the given buttons being pressed.
  169. /// </summary>
  170. /// <param name="buttons">Buttons to put into pressed state.</param>
  171. /// <exception cref="ArgumentNullException"><paramref name="buttons"/> is <c>null</c>.</exception>
  172. public GamepadState(params GamepadButton[] buttons)
  173. : this()
  174. {
  175. if (buttons == null)
  176. throw new ArgumentNullException(nameof(buttons));
  177. foreach (var button in buttons)
  178. {
  179. Debug.Assert((int)button < 32, $"Expected button < 32, so we fit into the 32 bit wide bitmask");
  180. var bit = 1U << (int)button;
  181. this.buttons |= bit;
  182. }
  183. }
  184. /// <summary>
  185. /// Set the specific buttons to be pressed or unpressed.
  186. /// </summary>
  187. /// <param name="button">A gamepad button.</param>
  188. /// <param name="value">Whether to set <paramref name="button"/> to be pressed or not pressed in
  189. /// <see cref="buttons"/>.</param>
  190. /// <returns>GamepadState with a modified <see cref="buttons"/> mask.</returns>
  191. public GamepadState WithButton(GamepadButton button, bool value = true)
  192. {
  193. Debug.Assert((int)button < 32, $"Expected button < 32, so we fit into the 32 bit wide bitmask");
  194. var bit = 1U << (int)button;
  195. if (value)
  196. buttons |= bit;
  197. else
  198. buttons &= ~bit;
  199. return this;
  200. }
  201. }
  202. ////NOTE: The bit positions here based on the enum value are also used in native.
  203. /// <summary>
  204. /// Enum of common gamepad buttons.
  205. /// </summary>
  206. /// <remarks>
  207. /// Can be used as an array indexer on the <see cref="Gamepad"/> class to get individual button controls.
  208. /// </remarks>
  209. public enum GamepadButton
  210. {
  211. // Dpad buttons. Important to be first in the bitfield as we'll
  212. // point the DpadControl to it.
  213. // IMPORTANT: Order has to match what is expected by DpadControl.
  214. /// <summary>
  215. /// The up button on a gamepad's dpad.
  216. /// </summary>
  217. DpadUp = 0,
  218. /// <summary>
  219. /// The down button on a gamepad's dpad.
  220. /// </summary>
  221. DpadDown = 1,
  222. /// <summary>
  223. /// The left button on a gamepad's dpad.
  224. /// </summary>
  225. DpadLeft = 2,
  226. /// <summary>
  227. /// The right button on a gamepad's dpad.
  228. /// </summary>
  229. DpadRight = 3,
  230. // Face buttons. We go with a north/south/east/west naming as that
  231. // clearly disambiguates where we expect the respective button to be.
  232. /// <summary>
  233. /// The upper action button on a gamepad.
  234. /// </summary>
  235. /// <remarks>
  236. /// Identical to <see cref="Y"/> and <see cref="Triangle"/> which are the Xbox and PlayStation controller names for this button.
  237. /// </remarks>
  238. North = 4,
  239. /// <summary>
  240. /// The right action button on a gamepad.
  241. /// </summary>
  242. /// <remarks>
  243. /// Identical to <see cref="B"/> and <see cref="Circle"/> which are the Xbox and PlayStation controller names for this button.
  244. /// </remarks>
  245. East = 5,
  246. /// <summary>
  247. /// The lower action button on a gamepad.
  248. /// </summary>
  249. /// <remarks>
  250. /// Identical to <see cref="A"/> and <see cref="Cross"/> which are the Xbox and PlayStation controller names for this button.
  251. /// </remarks>
  252. South = 6,
  253. /// <summary>
  254. /// The left action button on a gamepad.
  255. /// </summary>
  256. /// <remarks>
  257. /// Identical to <see cref="X"/> and <see cref="Square"/> which are the Xbox and PlayStation controller names for this button.
  258. /// </remarks>
  259. West = 7,
  260. /// <summary>
  261. /// The button pressed by pressing down the left stick on a gamepad.
  262. /// </summary>
  263. LeftStick = 8,
  264. /// <summary>
  265. /// The button pressed by pressing down the right stick on a gamepad.
  266. /// </summary>
  267. RightStick = 9,
  268. /// <summary>
  269. /// The left shoulder button on a gamepad.
  270. /// </summary>
  271. LeftShoulder = 10,
  272. /// <summary>
  273. /// The right shoulder button on a gamepad.
  274. /// </summary>
  275. RightShoulder = 11,
  276. /// <summary>
  277. /// The start button.
  278. /// </summary>
  279. Start = 12,
  280. /// <summary>
  281. /// The select button.
  282. /// </summary>
  283. Select = 13,
  284. // For values that are not part of the buttons bitmask in GamepadState, assign large values that are outside
  285. // the 32bit bit range.
  286. /// <summary>
  287. /// The left trigger button on a gamepad.
  288. /// </summary>
  289. LeftTrigger = 32,
  290. /// <summary>
  291. /// The right trigger button on a gamepad.
  292. /// </summary>
  293. RightTrigger = 33,
  294. /// <summary>
  295. /// The X button on an Xbox controller.
  296. /// </summary>
  297. /// <remarks>
  298. /// Identical to <see cref="West"/>, which is the generic name of this button.
  299. /// </remarks>
  300. X = West,
  301. /// <summary>
  302. /// The Y button on an Xbox controller.
  303. /// </summary>
  304. /// <remarks>
  305. /// Identical to <see cref="North"/>, which is the generic name of this button.
  306. /// </remarks>
  307. Y = North,
  308. /// <summary>
  309. /// The A button on an Xbox controller.
  310. /// </summary>
  311. /// <remarks>
  312. /// Identical to <see cref="South"/>, which is the generic name of this button.
  313. /// </remarks>
  314. A = South,
  315. /// <summary>
  316. /// The B button on an Xbox controller.
  317. /// </summary>
  318. /// <remarks>
  319. /// Identical to <see cref="East"/>, which is the generic name of this button.
  320. /// </remarks>
  321. B = East,
  322. /// <summary>
  323. /// The cross button on a PlayStation controller.
  324. /// </summary>
  325. /// <remarks>
  326. /// Identical to <see cref="South"/>, which is the generic name of this button.
  327. /// </remarks>
  328. Cross = South,
  329. /// <summary>
  330. /// The square button on a PlayStation controller.
  331. /// </summary>
  332. /// <remarks>
  333. /// Identical to <see cref="West"/>, which is the generic name of this button.
  334. /// </remarks>
  335. Square = West,
  336. /// <summary>
  337. /// The triangle button on a PlayStation controller.
  338. /// </summary>
  339. /// <remarks>
  340. /// Identical to <see cref="North"/>, which is the generic name of this button.
  341. /// </remarks>
  342. Triangle = North,
  343. /// <summary>
  344. /// The circle button on a PlayStation controller.
  345. /// </summary>
  346. /// <remarks>
  347. /// Identical to <see cref="East"/>, which is the generic name of this button.
  348. /// </remarks>
  349. Circle = East,
  350. }
  351. }
  352. namespace UnityEngine.InputSystem
  353. {
  354. /// <summary>
  355. /// An Xbox-style gamepad with two sticks, a D-Pad, four face buttons, two triggers,
  356. /// two shoulder buttons, and two menu buttons that usually sit in the midsection of the gamepad.
  357. /// </summary>
  358. /// <remarks>
  359. /// The Gamepad layout provides a standardized layouts for gamepads. Generally, if a specific
  360. /// device is represented as a Gamepad, the controls, such as the face buttons, are guaranteed
  361. /// to be mapped correctly and consistently. If, based on the set of supported devices available
  362. /// to the input system, this cannot be guaranteed, a given device is usually represented as a
  363. /// generic <see cref="Joystick"/> or as just a plain <see cref="HID.HID"/> instead.
  364. ///
  365. /// <example>
  366. /// <code>
  367. /// // Show all gamepads in the system.
  368. /// Debug.Log(string.Join("\n", Gamepad.all));
  369. ///
  370. /// // Check whether the X button on the current gamepad is pressed.
  371. /// if (Gamepad.current.xButton.wasPressedThisFrame)
  372. /// Debug.Log("Pressed");
  373. ///
  374. /// // Rumble the left motor on the current gamepad slightly.
  375. /// Gamepad.current.SetMotorSpeeds(0.2f, 0.
  376. /// </code>
  377. /// </example>
  378. /// </remarks>
  379. [InputControlLayout(stateType = typeof(GamepadState), isGenericTypeOfDevice = true)]
  380. public class Gamepad : InputDevice, IDualMotorRumble
  381. {
  382. /// <summary>
  383. /// The left face button of the gamepad.
  384. /// </summary>
  385. /// <value>Control representing the X/Square face button.</value>
  386. /// <remarks>
  387. /// On an Xbox controller, this is the X button and on the PS4 controller, this is the
  388. /// square button.
  389. /// </remarks>
  390. /// <seealso cref="xButton"/>
  391. /// <seealso cref="squareButton"/>
  392. public ButtonControl buttonWest { get; protected set; }
  393. /// <summary>
  394. /// The top face button of the gamepad.
  395. /// </summary>
  396. /// <value>Control representing the Y/Triangle face button.</value>
  397. /// <remarks>
  398. /// On an Xbox controller, this is the Y button and on the PS4 controller, this is the
  399. /// triangle button.
  400. /// </remarks>
  401. /// <seealso cref="yButton"/>
  402. /// <seealso cref="triangleButton"/>
  403. public ButtonControl buttonNorth { get; protected set; }
  404. /// <summary>
  405. /// The bottom face button of the gamepad.
  406. /// </summary>
  407. /// <value>Control representing the A/Cross face button.</value>
  408. /// <remarks>
  409. /// On an Xbox controller, this is the A button and on the PS4 controller, this is the
  410. /// cross button.
  411. /// </remarks>
  412. /// <seealso cref="aButton"/>
  413. /// <seealso cref="crossButton"/>
  414. public ButtonControl buttonSouth { get; protected set; }
  415. /// <summary>
  416. /// The right face button of the gamepad.
  417. /// </summary>
  418. /// <value>Control representing the B/Circle face button.</value>
  419. /// <remarks>
  420. /// On an Xbox controller, this is the B button and on the PS4 controller, this is the
  421. /// circle button.
  422. /// </remarks>
  423. /// <seealso cref="bButton"/>
  424. /// <seealso cref="circleButton"/>
  425. public ButtonControl buttonEast { get; protected set; }
  426. /// <summary>
  427. /// The button that gets triggered when <see cref="leftStick"/> is pressed down.
  428. /// </summary>
  429. /// <value>Control representing a click with the left stick.</value>
  430. public ButtonControl leftStickButton { get; protected set; }
  431. /// <summary>
  432. /// The button that gets triggered when <see cref="rightStick"/> is pressed down.
  433. /// </summary>
  434. /// <value>Control representing a click with the right stick.</value>
  435. public ButtonControl rightStickButton { get; protected set; }
  436. /// <summary>
  437. /// The right button in the middle section of the gamepad (called "menu" on Xbox
  438. /// controllers and "options" on PS4 controllers).
  439. /// </summary>
  440. /// <value>Control representing the right button in midsection.</value>
  441. public ButtonControl startButton { get; protected set; }
  442. /// <summary>
  443. /// The left button in the middle section of the gamepad (called "view" on Xbox
  444. /// controllers and "share" on PS4 controllers).
  445. /// </summary>
  446. /// <value>Control representing the left button in midsection.</value>
  447. public ButtonControl selectButton { get; protected set; }
  448. /// <summary>
  449. /// The 4-way directional pad on the gamepad.
  450. /// </summary>
  451. /// <value>Control representing the d-pad.</value>
  452. public DpadControl dpad { get; protected set; }
  453. /// <summary>
  454. /// The left shoulder/bumper button that sits on top of <see cref="leftTrigger"/>.
  455. /// </summary>
  456. /// <value>Control representing the left shoulder button.</value>
  457. /// <remarks>
  458. /// On Xbox controllers, this is usually called "left bumper" whereas on PS4
  459. /// controllers, this button is referred to as "L1".
  460. /// </remarks>
  461. public ButtonControl leftShoulder { get; protected set; }
  462. /// <summary>
  463. /// The right shoulder/bumper button that sits on top of <see cref="rightTrigger"/>.
  464. /// </summary>
  465. /// <value>Control representing the right shoulder button.</value>
  466. /// <remarks>
  467. /// On Xbox controllers, this is usually called "right bumper" whereas on PS4
  468. /// controllers, this button is referred to as "R1".
  469. /// </remarks>
  470. public ButtonControl rightShoulder { get; protected set; }
  471. /// <summary>
  472. /// The left thumbstick on the gamepad.
  473. /// </summary>
  474. /// <value>Control representing the left thumbstick.</value>
  475. public StickControl leftStick { get; protected set; }
  476. /// <summary>
  477. /// The right thumbstick on the gamepad.
  478. /// </summary>
  479. /// <value>Control representing the right thumbstick.</value>
  480. public StickControl rightStick { get; protected set; }
  481. /// <summary>
  482. /// The left trigger button sitting below <see cref="leftShoulder"/>.
  483. /// </summary>
  484. /// <value>Control representing the left trigger button.</value>
  485. /// <remarks>
  486. /// On PS4 controllers, this button is referred to as "L2".
  487. /// </remarks>
  488. public ButtonControl leftTrigger { get; protected set; }
  489. /// <summary>
  490. /// The right trigger button sitting below <see cref="rightShoulder"/>.
  491. /// </summary>
  492. /// <value>Control representing the right trigger button.</value>
  493. /// <remarks>
  494. /// On PS4 controllers, this button is referred to as "R2".
  495. /// </remarks>
  496. public ButtonControl rightTrigger { get; protected set; }
  497. /// <summary>
  498. /// Same as <see cref="buttonSouth"/>. Xbox-style alias.
  499. /// </summary>
  500. /// <value>Same as <see cref="buttonSouth"/>.</value>
  501. public ButtonControl aButton => buttonSouth;
  502. /// <summary>
  503. /// Same as <see cref="buttonEast"/>. Xbox-style alias.
  504. /// </summary>
  505. /// <value>Same as <see cref="buttonEast"/>.</value>
  506. public ButtonControl bButton => buttonEast;
  507. /// <summary>
  508. /// Same as <see cref="buttonWest"/> Xbox-style alias.
  509. /// </summary>
  510. /// <value>Same as <see cref="buttonWest"/>.</value>
  511. public ButtonControl xButton => buttonWest;
  512. /// <summary>
  513. /// Same as <see cref="buttonNorth"/>. Xbox-style alias.
  514. /// </summary>
  515. /// <value>Same as <see cref="buttonNorth"/>.</value>
  516. public ButtonControl yButton => buttonNorth;
  517. /// <summary>
  518. /// Same as <see cref="buttonNorth"/>. PS4-style alias.
  519. /// </summary>
  520. /// <value>Same as <see cref="buttonNorth"/>.</value>
  521. public ButtonControl triangleButton => buttonNorth;
  522. /// <summary>
  523. /// Same as <see cref="buttonWest"/>. PS4-style alias.
  524. /// </summary>
  525. /// <value>Same as <see cref="buttonWest"/>.</value>
  526. public ButtonControl squareButton => buttonWest;
  527. /// <summary>
  528. /// Same as <see cref="buttonEast"/>. PS4-style alias.
  529. /// </summary>
  530. /// <value>Same as <see cref="buttonEast"/>.</value>
  531. public ButtonControl circleButton => buttonEast;
  532. /// <summary>
  533. /// Same as <see cref="buttonSouth"/>. PS4-style alias.
  534. /// </summary>
  535. /// <value>Same as <see cref="buttonSouth"/>.</value>
  536. public ButtonControl crossButton => buttonSouth;
  537. /// <summary>
  538. /// Retrieve a gamepad button by its <see cref="GamepadButton"/> enumeration
  539. /// constant.
  540. /// </summary>
  541. /// <param name="button">Button to retrieve.</param>
  542. /// <exception cref="ArgumentException"><paramref name="button"/> is not a valid gamepad
  543. /// button value.</exception>
  544. public ButtonControl this[GamepadButton button]
  545. {
  546. get
  547. {
  548. switch (button)
  549. {
  550. case GamepadButton.North: return buttonNorth;
  551. case GamepadButton.South: return buttonSouth;
  552. case GamepadButton.East: return buttonEast;
  553. case GamepadButton.West: return buttonWest;
  554. case GamepadButton.Start: return startButton;
  555. case GamepadButton.Select: return selectButton;
  556. case GamepadButton.LeftShoulder: return leftShoulder;
  557. case GamepadButton.RightShoulder: return rightShoulder;
  558. case GamepadButton.LeftTrigger: return leftTrigger;
  559. case GamepadButton.RightTrigger: return rightTrigger;
  560. case GamepadButton.LeftStick: return leftStickButton;
  561. case GamepadButton.RightStick: return rightStickButton;
  562. case GamepadButton.DpadUp: return dpad.up;
  563. case GamepadButton.DpadDown: return dpad.down;
  564. case GamepadButton.DpadLeft: return dpad.left;
  565. case GamepadButton.DpadRight: return dpad.right;
  566. default:
  567. throw new InvalidEnumArgumentException(nameof(button), (int)button, typeof(GamepadButton));
  568. }
  569. }
  570. }
  571. /// <summary>
  572. /// The gamepad last used/connected by the player or <c>null</c> if there is no gamepad connected
  573. /// to the system.
  574. /// </summary>
  575. /// <remarks>
  576. /// When added, a device is automatically made current (see <see cref="InputDevice.MakeCurrent"/>), so
  577. /// when connecting a gamepad, it will also become current. After that, it will only become current again
  578. /// when input change on non-noisy controls (see <see cref="InputControl.noisy"/>) is received.
  579. ///
  580. /// For local multiplayer scenarios (or whenever there are multiple gamepads that need to be usable
  581. /// in a concurrent fashion), it is not recommended to rely on this property. Instead, it is recommended
  582. /// to use <see cref="PlayerInput"/> or <see cref="Users.InputUser"/>.
  583. /// </remarks>
  584. /// <seealso cref="InputDevice.MakeCurrent"/>
  585. /// <seealso cref="all"/>
  586. public static Gamepad current { get; private set; }
  587. /// <summary>
  588. /// A list of gamepads currently connected to the system.
  589. /// </summary>
  590. /// <value>All currently connected gamepads.</value>
  591. /// <remarks>
  592. /// Does not cause GC allocation.
  593. ///
  594. /// Do <em>not</em> hold on to the value returned by this getter but rather query it whenever
  595. /// you need it. Whenever the gamepad setup changes, the value returned by this getter
  596. /// is invalidated.
  597. /// </remarks>
  598. /// <seealso cref="current"/>
  599. public new static ReadOnlyArray<Gamepad> all => new ReadOnlyArray<Gamepad>(s_Gamepads, 0, s_GamepadCount);
  600. /// <inheritdoc />
  601. protected override void FinishSetup()
  602. {
  603. ////REVIEW: what's actually faster/better... storing these in properties or doing the lookup on the fly?
  604. buttonWest = GetChildControl<ButtonControl>("buttonWest");
  605. buttonNorth = GetChildControl<ButtonControl>("buttonNorth");
  606. buttonSouth = GetChildControl<ButtonControl>("buttonSouth");
  607. buttonEast = GetChildControl<ButtonControl>("buttonEast");
  608. startButton = GetChildControl<ButtonControl>("start");
  609. selectButton = GetChildControl<ButtonControl>("select");
  610. leftStickButton = GetChildControl<ButtonControl>("leftStickPress");
  611. rightStickButton = GetChildControl<ButtonControl>("rightStickPress");
  612. dpad = GetChildControl<DpadControl>("dpad");
  613. leftShoulder = GetChildControl<ButtonControl>("leftShoulder");
  614. rightShoulder = GetChildControl<ButtonControl>("rightShoulder");
  615. leftStick = GetChildControl<StickControl>("leftStick");
  616. rightStick = GetChildControl<StickControl>("rightStick");
  617. leftTrigger = GetChildControl<ButtonControl>("leftTrigger");
  618. rightTrigger = GetChildControl<ButtonControl>("rightTrigger");
  619. base.FinishSetup();
  620. }
  621. /// <summary>
  622. /// Make the gamepad the <see cref="current"/> gamepad.
  623. /// </summary>
  624. /// <remarks>
  625. /// This is called automatically by the system when there is input on a gamepad.
  626. /// </remarks>
  627. public override void MakeCurrent()
  628. {
  629. base.MakeCurrent();
  630. current = this;
  631. }
  632. /// <summary>
  633. /// Called when the gamepad is added to the system.
  634. /// </summary>
  635. protected override void OnAdded()
  636. {
  637. ArrayHelpers.AppendWithCapacity(ref s_Gamepads, ref s_GamepadCount, this);
  638. }
  639. /// <summary>
  640. /// Called when the gamepad is removed from the system.
  641. /// </summary>
  642. protected override void OnRemoved()
  643. {
  644. if (current == this)
  645. current = null;
  646. // Remove from `all`.
  647. var index = ArrayHelpers.IndexOfReference(s_Gamepads, this, s_GamepadCount);
  648. if (index != -1)
  649. ArrayHelpers.EraseAtWithCapacity(s_Gamepads, ref s_GamepadCount, index);
  650. else
  651. {
  652. Debug.Assert(false,
  653. $"Gamepad {this} seems to not have been added but is being removed (gamepad list: {string.Join(", ", all)})"); // Put in else to not allocate on normal path.
  654. }
  655. }
  656. /// <summary>
  657. /// Pause rumble effects on the gamepad. Resume with <see cref="ResumeHaptics"/>.
  658. /// </summary>
  659. /// <seealso cref="IDualMotorRumble"/>
  660. public virtual void PauseHaptics()
  661. {
  662. m_Rumble.PauseHaptics(this);
  663. }
  664. /// <summary>
  665. /// Resume rumble affects on the gamepad that have been paused with <see cref="PauseHaptics"/>.
  666. /// </summary>
  667. /// <seealso cref="IDualMotorRumble"/>
  668. public virtual void ResumeHaptics()
  669. {
  670. m_Rumble.ResumeHaptics(this);
  671. }
  672. /// <summary>
  673. /// Reset rumble effects on the gamepad. Puts the gamepad rumble motors back into their
  674. /// default state.
  675. /// </summary>
  676. /// <seealso cref="IDualMotorRumble"/>
  677. public virtual void ResetHaptics()
  678. {
  679. m_Rumble.ResetHaptics(this);
  680. }
  681. /// <inheritdoc />
  682. public virtual void SetMotorSpeeds(float lowFrequency, float highFrequency)
  683. {
  684. m_Rumble.SetMotorSpeeds(this, lowFrequency, highFrequency);
  685. }
  686. private DualMotorRumble m_Rumble;
  687. private static int s_GamepadCount;
  688. private static Gamepad[] s_Gamepads;
  689. }
  690. }