xboxGamepad.js 13 KB

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