Gamepad.cs 30 KB

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