xboxGamepad.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import { Observable } from "../Misc/observable";
  2. import { Gamepad } from "../Gamepads/gamepad";
  3. /**
  4. * Defines supported buttons for XBox360 compatible gamepads
  5. */
  6. export enum Xbox360Button {
  7. /** A */
  8. A = 0,
  9. /** B */
  10. B = 1,
  11. /** X */
  12. X = 2,
  13. /** Y */
  14. Y = 3,
  15. /** Left button */
  16. LB = 4,
  17. /** Right button */
  18. RB = 5,
  19. /** Back */
  20. Back = 8,
  21. /** Start */
  22. Start = 9,
  23. /** Left stick */
  24. LeftStick = 10,
  25. /** Right stick */
  26. RightStick = 11
  27. }
  28. /** Defines values for XBox360 DPad */
  29. export enum Xbox360Dpad {
  30. /** Up */
  31. Up = 12,
  32. /** Down */
  33. Down = 13,
  34. /** Left */
  35. Left = 14,
  36. /** Right */
  37. Right = 15
  38. }
  39. /**
  40. * Defines a XBox360 gamepad
  41. */
  42. export class Xbox360Pad extends Gamepad {
  43. private _leftTrigger: number = 0;
  44. private _rightTrigger: number = 0;
  45. private _onlefttriggerchanged: (value: number) => void;
  46. private _onrighttriggerchanged: (value: number) => void;
  47. private _onbuttondown: (buttonPressed: Xbox360Button) => void;
  48. private _onbuttonup: (buttonReleased: Xbox360Button) => void;
  49. private _ondpaddown: (dPadPressed: Xbox360Dpad) => void;
  50. private _ondpadup: (dPadReleased: Xbox360Dpad) => void;
  51. /** Observable raised when a button is pressed */
  52. public onButtonDownObservable = new Observable<Xbox360Button>();
  53. /** Observable raised when a button is released */
  54. public onButtonUpObservable = new Observable<Xbox360Button>();
  55. /** Observable raised when a pad is pressed */
  56. public onPadDownObservable = new Observable<Xbox360Dpad>();
  57. /** Observable raised when a pad is released */
  58. public onPadUpObservable = new Observable<Xbox360Dpad>();
  59. private _buttonA: number = 0;
  60. private _buttonB: number = 0;
  61. private _buttonX: number = 0;
  62. private _buttonY: number = 0;
  63. private _buttonBack: number = 0;
  64. private _buttonStart: number = 0;
  65. private _buttonLB: number = 0;
  66. private _buttonRB: number = 0;
  67. private _buttonLeftStick: number = 0;
  68. private _buttonRightStick: number = 0;
  69. private _dPadUp: number = 0;
  70. private _dPadDown: number = 0;
  71. private _dPadLeft: number = 0;
  72. private _dPadRight: number = 0;
  73. private _isXboxOnePad: boolean = false;
  74. /**
  75. * Creates a new XBox360 gamepad object
  76. * @param id defines the id of this gamepad
  77. * @param index defines its index
  78. * @param gamepad defines the internal HTML gamepad object
  79. * @param xboxOne defines if it is a XBox One gamepad
  80. */
  81. constructor(id: string, index: number, gamepad: any, xboxOne: boolean = false) {
  82. super(id, index, gamepad, 0, 1, 2, 3);
  83. this.type = Gamepad.XBOX;
  84. this._isXboxOnePad = xboxOne;
  85. }
  86. /**
  87. * Defines the callback to call when left trigger is pressed
  88. * @param callback defines the callback to use
  89. */
  90. public onlefttriggerchanged(callback: (value: number) => void) {
  91. this._onlefttriggerchanged = callback;
  92. }
  93. /**
  94. * Defines the callback to call when right trigger is pressed
  95. * @param callback defines the callback to use
  96. */
  97. public onrighttriggerchanged(callback: (value: number) => void) {
  98. this._onrighttriggerchanged = callback;
  99. }
  100. /**
  101. * Gets the left trigger value
  102. */
  103. public get leftTrigger(): number {
  104. return this._leftTrigger;
  105. }
  106. /**
  107. * Sets the left trigger value
  108. */
  109. public set leftTrigger(newValue: number) {
  110. if (this._onlefttriggerchanged && this._leftTrigger !== newValue) {
  111. this._onlefttriggerchanged(newValue);
  112. }
  113. this._leftTrigger = newValue;
  114. }
  115. /**
  116. * Gets the right trigger value
  117. */
  118. public get rightTrigger(): number {
  119. return this._rightTrigger;
  120. }
  121. /**
  122. * Sets the right trigger value
  123. */
  124. public set rightTrigger(newValue: number) {
  125. if (this._onrighttriggerchanged && this._rightTrigger !== newValue) {
  126. this._onrighttriggerchanged(newValue);
  127. }
  128. this._rightTrigger = newValue;
  129. }
  130. /**
  131. * Defines the callback to call when a button is pressed
  132. * @param callback defines the callback to use
  133. */
  134. public onbuttondown(callback: (buttonPressed: Xbox360Button) => void) {
  135. this._onbuttondown = callback;
  136. }
  137. /**
  138. * Defines the callback to call when a button is released
  139. * @param callback defines the callback to use
  140. */
  141. public onbuttonup(callback: (buttonReleased: Xbox360Button) => void) {
  142. this._onbuttonup = callback;
  143. }
  144. /**
  145. * Defines the callback to call when a pad is pressed
  146. * @param callback defines the callback to use
  147. */
  148. public ondpaddown(callback: (dPadPressed: Xbox360Dpad) => void) {
  149. this._ondpaddown = callback;
  150. }
  151. /**
  152. * Defines the callback to call when a pad is released
  153. * @param callback defines the callback to use
  154. */
  155. public ondpadup(callback: (dPadReleased: Xbox360Dpad) => void) {
  156. this._ondpadup = callback;
  157. }
  158. private _setButtonValue(newValue: number, currentValue: number, buttonType: Xbox360Button): number {
  159. if (newValue !== currentValue) {
  160. if (newValue === 1) {
  161. if (this._onbuttondown) {
  162. this._onbuttondown(buttonType);
  163. }
  164. this.onButtonDownObservable.notifyObservers(buttonType);
  165. }
  166. if (newValue === 0) {
  167. if (this._onbuttonup) {
  168. this._onbuttonup(buttonType);
  169. }
  170. this.onButtonUpObservable.notifyObservers(buttonType);
  171. }
  172. }
  173. return newValue;
  174. }
  175. private _setDPadValue(newValue: number, currentValue: number, buttonType: Xbox360Dpad): number {
  176. if (newValue !== currentValue) {
  177. if (newValue === 1) {
  178. if (this._ondpaddown) {
  179. this._ondpaddown(buttonType);
  180. }
  181. this.onPadDownObservable.notifyObservers(buttonType);
  182. }
  183. if (newValue === 0) {
  184. if (this._ondpadup) {
  185. this._ondpadup(buttonType);
  186. }
  187. this.onPadUpObservable.notifyObservers(buttonType);
  188. }
  189. }
  190. return newValue;
  191. }
  192. /**
  193. * Gets the value of the `A` button
  194. */
  195. public get buttonA(): number {
  196. return this._buttonA;
  197. }
  198. /**
  199. * Sets the value of the `A` button
  200. */
  201. public set buttonA(value) {
  202. this._buttonA = this._setButtonValue(value, this._buttonA, Xbox360Button.A);
  203. }
  204. /**
  205. * Gets the value of the `B` button
  206. */
  207. public get buttonB(): number {
  208. return this._buttonB;
  209. }
  210. /**
  211. * Sets the value of the `B` button
  212. */
  213. public set buttonB(value) {
  214. this._buttonB = this._setButtonValue(value, this._buttonB, Xbox360Button.B);
  215. }
  216. /**
  217. * Gets the value of the `X` button
  218. */
  219. public get buttonX(): number {
  220. return this._buttonX;
  221. }
  222. /**
  223. * Sets the value of the `X` button
  224. */
  225. public set buttonX(value) {
  226. this._buttonX = this._setButtonValue(value, this._buttonX, Xbox360Button.X);
  227. }
  228. /**
  229. * Gets the value of the `Y` button
  230. */
  231. public get buttonY(): number {
  232. return this._buttonY;
  233. }
  234. /**
  235. * Sets the value of the `Y` button
  236. */
  237. public set buttonY(value) {
  238. this._buttonY = this._setButtonValue(value, this._buttonY, Xbox360Button.Y);
  239. }
  240. /**
  241. * Gets the value of the `Start` button
  242. */
  243. public get buttonStart(): number {
  244. return this._buttonStart;
  245. }
  246. /**
  247. * Sets the value of the `Start` button
  248. */
  249. public set buttonStart(value) {
  250. this._buttonStart = this._setButtonValue(value, this._buttonStart, Xbox360Button.Start);
  251. }
  252. /**
  253. * Gets the value of the `Back` button
  254. */
  255. public get buttonBack(): number {
  256. return this._buttonBack;
  257. }
  258. /**
  259. * Sets the value of the `Back` button
  260. */
  261. public set buttonBack(value) {
  262. this._buttonBack = this._setButtonValue(value, this._buttonBack, Xbox360Button.Back);
  263. }
  264. /**
  265. * Gets the value of the `Left` button
  266. */
  267. public get buttonLB(): number {
  268. return this._buttonLB;
  269. }
  270. /**
  271. * Sets the value of the `Left` button
  272. */
  273. public set buttonLB(value) {
  274. this._buttonLB = this._setButtonValue(value, this._buttonLB, Xbox360Button.LB);
  275. }
  276. /**
  277. * Gets the value of the `Right` button
  278. */
  279. public get buttonRB(): number {
  280. return this._buttonRB;
  281. }
  282. /**
  283. * Sets the value of the `Right` button
  284. */
  285. public set buttonRB(value) {
  286. this._buttonRB = this._setButtonValue(value, this._buttonRB, Xbox360Button.RB);
  287. }
  288. /**
  289. * Gets the value of the Left joystick
  290. */
  291. public get buttonLeftStick(): number {
  292. return this._buttonLeftStick;
  293. }
  294. /**
  295. * Sets the value of the Left joystick
  296. */
  297. public set buttonLeftStick(value) {
  298. this._buttonLeftStick = this._setButtonValue(value, this._buttonLeftStick, Xbox360Button.LeftStick);
  299. }
  300. /**
  301. * Gets the value of the Right joystick
  302. */
  303. public get buttonRightStick(): number {
  304. return this._buttonRightStick;
  305. }
  306. /**
  307. * Sets the value of the Right joystick
  308. */
  309. public set buttonRightStick(value) {
  310. this._buttonRightStick = this._setButtonValue(value, this._buttonRightStick, Xbox360Button.RightStick);
  311. }
  312. /**
  313. * Gets the value of D-pad up
  314. */
  315. public get dPadUp(): number {
  316. return this._dPadUp;
  317. }
  318. /**
  319. * Sets the value of D-pad up
  320. */
  321. public set dPadUp(value) {
  322. this._dPadUp = this._setDPadValue(value, this._dPadUp, Xbox360Dpad.Up);
  323. }
  324. /**
  325. * Gets the value of D-pad down
  326. */
  327. public get dPadDown(): number {
  328. return this._dPadDown;
  329. }
  330. /**
  331. * Sets the value of D-pad down
  332. */
  333. public set dPadDown(value) {
  334. this._dPadDown = this._setDPadValue(value, this._dPadDown, Xbox360Dpad.Down);
  335. }
  336. /**
  337. * Gets the value of D-pad left
  338. */
  339. public get dPadLeft(): number {
  340. return this._dPadLeft;
  341. }
  342. /**
  343. * Sets the value of D-pad left
  344. */
  345. public set dPadLeft(value) {
  346. this._dPadLeft = this._setDPadValue(value, this._dPadLeft, Xbox360Dpad.Left);
  347. }
  348. /**
  349. * Gets the value of D-pad right
  350. */
  351. public get dPadRight(): number {
  352. return this._dPadRight;
  353. }
  354. /**
  355. * Sets the value of D-pad right
  356. */
  357. public set dPadRight(value) {
  358. this._dPadRight = this._setDPadValue(value, this._dPadRight, Xbox360Dpad.Right);
  359. }
  360. /**
  361. * Force the gamepad to synchronize with device values
  362. */
  363. public update() {
  364. super.update();
  365. if (this._isXboxOnePad) {
  366. this.buttonA = this.browserGamepad.buttons[0].value;
  367. this.buttonB = this.browserGamepad.buttons[1].value;
  368. this.buttonX = this.browserGamepad.buttons[2].value;
  369. this.buttonY = this.browserGamepad.buttons[3].value;
  370. this.buttonLB = this.browserGamepad.buttons[4].value;
  371. this.buttonRB = this.browserGamepad.buttons[5].value;
  372. this.leftTrigger = this.browserGamepad.buttons[6].value;
  373. this.rightTrigger = this.browserGamepad.buttons[7].value;
  374. this.buttonBack = this.browserGamepad.buttons[8].value;
  375. this.buttonStart = this.browserGamepad.buttons[9].value;
  376. this.buttonLeftStick = this.browserGamepad.buttons[10].value;
  377. this.buttonRightStick = this.browserGamepad.buttons[11].value;
  378. this.dPadUp = this.browserGamepad.buttons[12].value;
  379. this.dPadDown = this.browserGamepad.buttons[13].value;
  380. this.dPadLeft = this.browserGamepad.buttons[14].value;
  381. this.dPadRight = this.browserGamepad.buttons[15].value;
  382. } else {
  383. this.buttonA = this.browserGamepad.buttons[0].value;
  384. this.buttonB = this.browserGamepad.buttons[1].value;
  385. this.buttonX = this.browserGamepad.buttons[2].value;
  386. this.buttonY = this.browserGamepad.buttons[3].value;
  387. this.buttonLB = this.browserGamepad.buttons[4].value;
  388. this.buttonRB = this.browserGamepad.buttons[5].value;
  389. this.leftTrigger = this.browserGamepad.buttons[6].value;
  390. this.rightTrigger = this.browserGamepad.buttons[7].value;
  391. this.buttonBack = this.browserGamepad.buttons[8].value;
  392. this.buttonStart = this.browserGamepad.buttons[9].value;
  393. this.buttonLeftStick = this.browserGamepad.buttons[10].value;
  394. this.buttonRightStick = this.browserGamepad.buttons[11].value;
  395. this.dPadUp = this.browserGamepad.buttons[12].value;
  396. this.dPadDown = this.browserGamepad.buttons[13].value;
  397. this.dPadLeft = this.browserGamepad.buttons[14].value;
  398. this.dPadRight = this.browserGamepad.buttons[15].value;
  399. }
  400. }
  401. /**
  402. * Disposes the gamepad
  403. */
  404. public dispose() {
  405. super.dispose();
  406. this.onButtonDownObservable.clear();
  407. this.onButtonUpObservable.clear();
  408. this.onPadDownObservable.clear();
  409. this.onPadUpObservable.clear();
  410. }
  411. }