babylon.gamepads.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. var __extends = this.__extends || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var Gamepads = (function () {
  10. function Gamepads(ongamedpadconnected) {
  11. var _this = this;
  12. this.babylonGamepads = [];
  13. this.oneGamepadConnected = false;
  14. this.isMonitoring = false;
  15. this.gamepadEventSupported = 'GamepadEvent' in window;
  16. this.gamepadSupportAvailable = (navigator.getGamepads || !!navigator.webkitGetGamepads || !!navigator.msGetGamepads || !!navigator.webkitGamepads);
  17. this.buttonADataURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAA9aSUR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p31CRNbdV8l8e++xD5HPNeHxhx5Bgf18kTN5T1kvjBfEjGjBJCai4gnjHqAnlvqS8e9NeujEjEul/NokDbai4V/2voafHD1S0evdWLeb8ojMNyly5fS//ffbcD0L33j4K4RX4rtMh/UUGLXmr6BWXN9MEFAhYfzmZ6hcXI+TpISRH8061Ui68gTWGUJP4aU9P8ZrB39S+Xkx1ummPSMkbebnJcxU1jm4D5eGhvB7j32HJcpUJHhxLIfxTZpxwGa8eKrHC51a9Tmp+N5P1RsQ01cJAwEflHw8/+pfYn/HgaQ+n7/a1vd6k+BUS2XvVD401TXhu488gQ0r71QUuLJsrWT8mSYtfkBMm0BAmFhNrgDX4oRqqeaJMw4c6TyIv/qPP0Xf8KUJ6sXuP1XluuEEyGsD5TXKgsqBNQvW4RtbnkDb4ttJQlGt/IQqLMJE7tWqOSBZCSrL6dFSqq3AnzhzDC/tewHt5w4nr3suvgN0+P8o3TeegFe3vYDHtj+xhLt/Q3kkeW5d693YuuHXsWHZPcixW4tCwo+trVU9QEs8G6HFqW5kdBiHTu3H64dfxpGuK8r665Tv7tz2D6e/tP23cT0E1OA5QR2iiIbs1i9u/9qTPPC12CtwlIofjZVvW/BZ3LVsC5bPW4u5DQuxaPay2NpRIuy61IkLA+dw8hdHceDUPpw49z9TXUysvWPXtl3bQ4yQtMJ1a18DAsbvRO/atvM5DXXPPbp9yzP8+GXBXTkngKYBdTWvE5RXdm87+HQEfLh2T57UIAdM95Js9+04LKSDbLzG31+Omxpx9xfxKR6AukkhMP0aKuUHsag5VEzE3fGSddsUVu6KFzIE+H/iJry0mX+bu8VfMwTMEDBDwAwBMwTMEHALv/5XgAEASpR5N6rB30UAAAAASUVORK5CYII=";
  18. this._callbackGamepadConnected = ongamedpadconnected;
  19. if (this.gamepadSupportAvailable) {
  20. // Checking if the gamepad connected event is supported (like in Firefox)
  21. if (this.gamepadEventSupported) {
  22. window.addEventListener('gamepadconnected', function (evt) {
  23. _this._onGamepadConnected(evt);
  24. }, false);
  25. window.addEventListener('gamepaddisconnected', function (evt) {
  26. _this._onGamepadDisconnected(evt);
  27. }, false);
  28. } else {
  29. this._startMonitoringGamepads();
  30. }
  31. if (!this.oneGamepadConnected) {
  32. this._insertGamepadDOMInstructions();
  33. }
  34. } else {
  35. this._insertGamepadDOMNotSupported();
  36. }
  37. }
  38. Gamepads.prototype._insertGamepadDOMInstructions = function () {
  39. Gamepads.gamepadDOMInfo = document.createElement("div");
  40. var buttonAImage = document.createElement("img");
  41. buttonAImage.src = this.buttonADataURL;
  42. var spanMessage = document.createElement("span");
  43. spanMessage.innerHTML = "<strong>to activate gamepad</strong>";
  44. Gamepads.gamepadDOMInfo.appendChild(buttonAImage);
  45. Gamepads.gamepadDOMInfo.appendChild(spanMessage);
  46. Gamepads.gamepadDOMInfo.style.position = "absolute";
  47. Gamepads.gamepadDOMInfo.style.width = "100%";
  48. Gamepads.gamepadDOMInfo.style.height = "48px";
  49. Gamepads.gamepadDOMInfo.style.bottom = "0px";
  50. Gamepads.gamepadDOMInfo.style.backgroundColor = "rgba(1, 1, 1, 0.15)";
  51. Gamepads.gamepadDOMInfo.style.textAlign = "center";
  52. Gamepads.gamepadDOMInfo.style.zIndex = "10";
  53. buttonAImage.style.position = "relative";
  54. buttonAImage.style.bottom = "8px";
  55. spanMessage.style.position = "relative";
  56. spanMessage.style.fontSize = "32px";
  57. spanMessage.style.bottom = "32px";
  58. spanMessage.style.color = "green";
  59. document.body.appendChild(Gamepads.gamepadDOMInfo);
  60. };
  61. Gamepads.prototype._insertGamepadDOMNotSupported = function () {
  62. Gamepads.gamepadDOMInfo = document.createElement("div");
  63. var spanMessage = document.createElement("span");
  64. spanMessage.innerHTML = "<strong>gamepad not supported</strong>";
  65. Gamepads.gamepadDOMInfo.appendChild(spanMessage);
  66. Gamepads.gamepadDOMInfo.style.position = "absolute";
  67. Gamepads.gamepadDOMInfo.style.width = "100%";
  68. Gamepads.gamepadDOMInfo.style.height = "40px";
  69. Gamepads.gamepadDOMInfo.style.bottom = "0px";
  70. Gamepads.gamepadDOMInfo.style.backgroundColor = "rgba(1, 1, 1, 0.15)";
  71. Gamepads.gamepadDOMInfo.style.textAlign = "center";
  72. Gamepads.gamepadDOMInfo.style.zIndex = "10";
  73. spanMessage.style.position = "relative";
  74. spanMessage.style.fontSize = "32px";
  75. spanMessage.style.color = "red";
  76. document.body.appendChild(Gamepads.gamepadDOMInfo);
  77. };
  78. Gamepads.prototype._onGamepadConnected = function (evt) {
  79. var newGamepad = this._addNewGamepad(evt.gamepad);
  80. if (this._callbackGamepadConnected)
  81. this._callbackGamepadConnected(newGamepad);
  82. this._startMonitoringGamepads();
  83. };
  84. Gamepads.prototype._addNewGamepad = function (gamepad) {
  85. if (!this.oneGamepadConnected) {
  86. this.oneGamepadConnected = true;
  87. if (Gamepads.gamepadDOMInfo) {
  88. document.body.removeChild(Gamepads.gamepadDOMInfo);
  89. Gamepads.gamepadDOMInfo = null;
  90. }
  91. }
  92. var newGamepad;
  93. if (gamepad.id.search("Xbox 360") !== -1 || gamepad.id.search("xinput") !== -1) {
  94. newGamepad = new BABYLON.Xbox360Pad(gamepad.id, gamepad.index, gamepad);
  95. } else {
  96. newGamepad = new BABYLON.GenericPad(gamepad.id, gamepad.index, gamepad);
  97. }
  98. this.babylonGamepads.push(newGamepad);
  99. return newGamepad;
  100. };
  101. Gamepads.prototype._onGamepadDisconnected = function (evt) {
  102. for (var i in this.babylonGamepads) {
  103. if (this.babylonGamepads[i].index == evt.gamepad.index) {
  104. this.babylonGamepads.splice(i, 1);
  105. break;
  106. }
  107. }
  108. // If no gamepads are left, stop the polling loop.
  109. if (this.babylonGamepads.length == 0) {
  110. this._stopMonitoringGamepads();
  111. }
  112. };
  113. Gamepads.prototype._startMonitoringGamepads = function () {
  114. if (!this.isMonitoring) {
  115. this.isMonitoring = true;
  116. this._checkGamepadsStatus();
  117. }
  118. };
  119. Gamepads.prototype._stopMonitoringGamepads = function () {
  120. this.isMonitoring = false;
  121. };
  122. Gamepads.prototype._checkGamepadsStatus = function () {
  123. var _this = this;
  124. // updating gamepad objects
  125. this._updateGamepadObjects();
  126. for (var i in this.babylonGamepads) {
  127. this.babylonGamepads[i].update();
  128. }
  129. if (this.isMonitoring) {
  130. if (window.requestAnimationFrame) {
  131. window.requestAnimationFrame(function () {
  132. _this._checkGamepadsStatus();
  133. });
  134. } else if (window.mozRequestAnimationFrame) {
  135. window.mozRequestAnimationFrame(function () {
  136. _this._checkGamepadsStatus();
  137. });
  138. } else if (window.webkitRequestAnimationFrame) {
  139. window.webkitRequestAnimationFrame(function () {
  140. _this._checkGamepadsStatus();
  141. });
  142. }
  143. }
  144. };
  145. // This function is called only on Chrome, which does not yet support
  146. // connection/disconnection events, but requires you to monitor
  147. // an array for changes.
  148. Gamepads.prototype._updateGamepadObjects = function () {
  149. var gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
  150. for (var i = 0; i < gamepads.length; i++) {
  151. if (gamepads[i]) {
  152. if (!(gamepads[i].index in this.babylonGamepads)) {
  153. var newGamepad = this._addNewGamepad(gamepads[i]);
  154. if (this._callbackGamepadConnected) {
  155. this._callbackGamepadConnected(newGamepad);
  156. }
  157. } else {
  158. this.babylonGamepads[i].browserGamepad = gamepads[i];
  159. }
  160. }
  161. }
  162. };
  163. return Gamepads;
  164. })();
  165. BABYLON.Gamepads = Gamepads;
  166. var StickValues = (function () {
  167. function StickValues(x, y) {
  168. this.x = x;
  169. this.y = y;
  170. }
  171. return StickValues;
  172. })();
  173. BABYLON.StickValues = StickValues;
  174. var Gamepad = (function () {
  175. function Gamepad(id, index, browserGamepad) {
  176. this.id = id;
  177. this.index = index;
  178. this.browserGamepad = browserGamepad;
  179. if (this.browserGamepad.axes.length >= 2) {
  180. this._leftStick = { x: this.browserGamepad.axes[0], y: this.browserGamepad.axes[1] };
  181. }
  182. if (this.browserGamepad.axes.length >= 4) {
  183. this._rightStick = { x: this.browserGamepad.axes[2], y: this.browserGamepad.axes[3] };
  184. }
  185. }
  186. Gamepad.prototype.onleftstickchanged = function (callback) {
  187. this._onleftstickchanged = callback;
  188. };
  189. Gamepad.prototype.onrightstickchanged = function (callback) {
  190. this._onrightstickchanged = callback;
  191. };
  192. Object.defineProperty(Gamepad.prototype, "leftStick", {
  193. get: function () {
  194. return this._leftStick;
  195. },
  196. set: function (newValues) {
  197. if (this._onleftstickchanged && (this._leftStick.x !== newValues.x || this._leftStick.y !== newValues.y)) {
  198. this._onleftstickchanged(newValues);
  199. }
  200. this._leftStick = newValues;
  201. },
  202. enumerable: true,
  203. configurable: true
  204. });
  205. Object.defineProperty(Gamepad.prototype, "rightStick", {
  206. get: function () {
  207. return this._rightStick;
  208. },
  209. set: function (newValues) {
  210. if (this._onrightstickchanged && (this._rightStick.x !== newValues.x || this._rightStick.y !== newValues.y)) {
  211. this._onrightstickchanged(newValues);
  212. }
  213. this._rightStick = newValues;
  214. },
  215. enumerable: true,
  216. configurable: true
  217. });
  218. Gamepad.prototype.update = function () {
  219. if (this._leftStick) {
  220. this.leftStick = { x: this.browserGamepad.axes[0], y: this.browserGamepad.axes[1] };
  221. }
  222. if (this._rightStick) {
  223. this.rightStick = { x: this.browserGamepad.axes[2], y: this.browserGamepad.axes[3] };
  224. }
  225. };
  226. return Gamepad;
  227. })();
  228. BABYLON.Gamepad = Gamepad;
  229. var GenericPad = (function (_super) {
  230. __extends(GenericPad, _super);
  231. function GenericPad(id, index, gamepad) {
  232. _super.call(this, id, index, gamepad);
  233. this.id = id;
  234. this.index = index;
  235. this.gamepad = gamepad;
  236. this._buttons = new Array(gamepad.buttons.length);
  237. }
  238. GenericPad.prototype.onbuttondown = function (callback) {
  239. this._onbuttondown = callback;
  240. };
  241. GenericPad.prototype.onbuttonup = function (callback) {
  242. this._onbuttonup = callback;
  243. };
  244. GenericPad.prototype._setButtonValue = function (newValue, currentValue, buttonIndex) {
  245. if (newValue !== currentValue) {
  246. if (this._onbuttondown && newValue === 1) {
  247. this._onbuttondown(buttonIndex);
  248. }
  249. if (this._onbuttonup && newValue === 0) {
  250. this._onbuttonup(buttonIndex);
  251. }
  252. }
  253. return newValue;
  254. };
  255. GenericPad.prototype.update = function () {
  256. _super.prototype.update.call(this);
  257. for (var index = 0; index < this._buttons.length; index++) {
  258. this._buttons[index] = this._setButtonValue(this.gamepad.buttons[index].value, this._buttons[index], index);
  259. }
  260. };
  261. return GenericPad;
  262. })(Gamepad);
  263. BABYLON.GenericPad = GenericPad;
  264. (function (Xbox360Button) {
  265. Xbox360Button[Xbox360Button["A"] = 0] = "A";
  266. Xbox360Button[Xbox360Button["B"] = 1] = "B";
  267. Xbox360Button[Xbox360Button["X"] = 2] = "X";
  268. Xbox360Button[Xbox360Button["Y"] = 3] = "Y";
  269. Xbox360Button[Xbox360Button["Start"] = 4] = "Start";
  270. Xbox360Button[Xbox360Button["Back"] = 5] = "Back";
  271. Xbox360Button[Xbox360Button["LB"] = 6] = "LB";
  272. Xbox360Button[Xbox360Button["RB"] = 7] = "RB";
  273. Xbox360Button[Xbox360Button["LeftStick"] = 8] = "LeftStick";
  274. Xbox360Button[Xbox360Button["RightStick"] = 9] = "RightStick";
  275. })(BABYLON.Xbox360Button || (BABYLON.Xbox360Button = {}));
  276. var Xbox360Button = BABYLON.Xbox360Button;
  277. (function (Xbox360Dpad) {
  278. Xbox360Dpad[Xbox360Dpad["Up"] = 0] = "Up";
  279. Xbox360Dpad[Xbox360Dpad["Down"] = 1] = "Down";
  280. Xbox360Dpad[Xbox360Dpad["Left"] = 2] = "Left";
  281. Xbox360Dpad[Xbox360Dpad["Right"] = 3] = "Right";
  282. })(BABYLON.Xbox360Dpad || (BABYLON.Xbox360Dpad = {}));
  283. var Xbox360Dpad = BABYLON.Xbox360Dpad;
  284. var Xbox360Pad = (function (_super) {
  285. __extends(Xbox360Pad, _super);
  286. function Xbox360Pad() {
  287. _super.apply(this, arguments);
  288. this._leftTrigger = 0;
  289. this._rightTrigger = 0;
  290. this._buttonA = 0;
  291. this._buttonB = 0;
  292. this._buttonX = 0;
  293. this._buttonY = 0;
  294. this._buttonBack = 0;
  295. this._buttonStart = 0;
  296. this._buttonLB = 0;
  297. this._buttonRB = 0;
  298. this._buttonLeftStick = 0;
  299. this._buttonRightStick = 0;
  300. this._dPadUp = 0;
  301. this._dPadDown = 0;
  302. this._dPadLeft = 0;
  303. this._dPadRight = 0;
  304. }
  305. Xbox360Pad.prototype.onlefttriggerchanged = function (callback) {
  306. this._onlefttriggerchanged = callback;
  307. };
  308. Xbox360Pad.prototype.onrighttriggerchanged = function (callback) {
  309. this._onrighttriggerchanged = callback;
  310. };
  311. Object.defineProperty(Xbox360Pad.prototype, "leftTrigger", {
  312. get: function () {
  313. return this._leftTrigger;
  314. },
  315. set: function (newValue) {
  316. if (this._onlefttriggerchanged && this._leftTrigger !== newValue) {
  317. this._onlefttriggerchanged(newValue);
  318. }
  319. this._leftTrigger = newValue;
  320. },
  321. enumerable: true,
  322. configurable: true
  323. });
  324. Object.defineProperty(Xbox360Pad.prototype, "rightTrigger", {
  325. get: function () {
  326. return this._rightTrigger;
  327. },
  328. set: function (newValue) {
  329. if (this._onrighttriggerchanged && this._rightTrigger !== newValue) {
  330. this._onrighttriggerchanged(newValue);
  331. }
  332. this._rightTrigger = newValue;
  333. },
  334. enumerable: true,
  335. configurable: true
  336. });
  337. Xbox360Pad.prototype.onbuttondown = function (callback) {
  338. this._onbuttondown = callback;
  339. };
  340. Xbox360Pad.prototype.onbuttonup = function (callback) {
  341. this._onbuttonup = callback;
  342. };
  343. Xbox360Pad.prototype.ondpaddown = function (callback) {
  344. this._ondpaddown = callback;
  345. };
  346. Xbox360Pad.prototype.ondpadup = function (callback) {
  347. this._ondpadup = callback;
  348. };
  349. Xbox360Pad.prototype._setButtonValue = function (newValue, currentValue, buttonType) {
  350. if (newValue !== currentValue) {
  351. if (this._onbuttondown && newValue === 1) {
  352. this._onbuttondown(buttonType);
  353. }
  354. if (this._onbuttonup && newValue === 0) {
  355. this._onbuttonup(buttonType);
  356. }
  357. }
  358. return newValue;
  359. };
  360. Xbox360Pad.prototype._setDPadValue = function (newValue, currentValue, buttonType) {
  361. if (newValue !== currentValue) {
  362. if (this._ondpaddown && newValue === 1) {
  363. this._ondpaddown(buttonType);
  364. }
  365. if (this._ondpadup && newValue === 0) {
  366. this._ondpadup(buttonType);
  367. }
  368. }
  369. return newValue;
  370. };
  371. Object.defineProperty(Xbox360Pad.prototype, "buttonA", {
  372. get: function () {
  373. return this._buttonA;
  374. },
  375. set: function (value) {
  376. this._buttonA = this._setButtonValue(value, this._buttonA, 0 /* A */);
  377. },
  378. enumerable: true,
  379. configurable: true
  380. });
  381. Object.defineProperty(Xbox360Pad.prototype, "buttonB", {
  382. get: function () {
  383. return this._buttonB;
  384. },
  385. set: function (value) {
  386. this._buttonB = this._setButtonValue(value, this._buttonB, 1 /* B */);
  387. },
  388. enumerable: true,
  389. configurable: true
  390. });
  391. Object.defineProperty(Xbox360Pad.prototype, "buttonX", {
  392. get: function () {
  393. return this._buttonX;
  394. },
  395. set: function (value) {
  396. this._buttonX = this._setButtonValue(value, this._buttonX, 2 /* X */);
  397. },
  398. enumerable: true,
  399. configurable: true
  400. });
  401. Object.defineProperty(Xbox360Pad.prototype, "buttonY", {
  402. get: function () {
  403. return this._buttonY;
  404. },
  405. set: function (value) {
  406. this._buttonY = this._setButtonValue(value, this._buttonY, 3 /* Y */);
  407. },
  408. enumerable: true,
  409. configurable: true
  410. });
  411. Object.defineProperty(Xbox360Pad.prototype, "buttonStart", {
  412. get: function () {
  413. return this._buttonStart;
  414. },
  415. set: function (value) {
  416. this._buttonStart = this._setButtonValue(value, this._buttonStart, 4 /* Start */);
  417. },
  418. enumerable: true,
  419. configurable: true
  420. });
  421. Object.defineProperty(Xbox360Pad.prototype, "buttonBack", {
  422. get: function () {
  423. return this._buttonBack;
  424. },
  425. set: function (value) {
  426. this._buttonBack = this._setButtonValue(value, this._buttonBack, 5 /* Back */);
  427. },
  428. enumerable: true,
  429. configurable: true
  430. });
  431. Object.defineProperty(Xbox360Pad.prototype, "buttonLB", {
  432. get: function () {
  433. return this._buttonLB;
  434. },
  435. set: function (value) {
  436. this._buttonLB = this._setButtonValue(value, this._buttonLB, 6 /* LB */);
  437. },
  438. enumerable: true,
  439. configurable: true
  440. });
  441. Object.defineProperty(Xbox360Pad.prototype, "buttonRB", {
  442. get: function () {
  443. return this._buttonRB;
  444. },
  445. set: function (value) {
  446. this._buttonRB = this._setButtonValue(value, this._buttonRB, 7 /* RB */);
  447. },
  448. enumerable: true,
  449. configurable: true
  450. });
  451. Object.defineProperty(Xbox360Pad.prototype, "buttonLeftStick", {
  452. get: function () {
  453. return this._buttonLeftStick;
  454. },
  455. set: function (value) {
  456. this._buttonLeftStick = this._setButtonValue(value, this._buttonLeftStick, 8 /* LeftStick */);
  457. },
  458. enumerable: true,
  459. configurable: true
  460. });
  461. Object.defineProperty(Xbox360Pad.prototype, "buttonRightStick", {
  462. get: function () {
  463. return this._buttonRightStick;
  464. },
  465. set: function (value) {
  466. this._buttonRightStick = this._setButtonValue(value, this._buttonRightStick, 9 /* RightStick */);
  467. },
  468. enumerable: true,
  469. configurable: true
  470. });
  471. Object.defineProperty(Xbox360Pad.prototype, "dPadUp", {
  472. get: function () {
  473. return this._dPadUp;
  474. },
  475. set: function (value) {
  476. this._dPadUp = this._setDPadValue(value, this._dPadUp, 0 /* Up */);
  477. },
  478. enumerable: true,
  479. configurable: true
  480. });
  481. Object.defineProperty(Xbox360Pad.prototype, "dPadDown", {
  482. get: function () {
  483. return this._dPadDown;
  484. },
  485. set: function (value) {
  486. this._dPadDown = this._setDPadValue(value, this._dPadDown, 1 /* Down */);
  487. },
  488. enumerable: true,
  489. configurable: true
  490. });
  491. Object.defineProperty(Xbox360Pad.prototype, "dPadLeft", {
  492. get: function () {
  493. return this._dPadLeft;
  494. },
  495. set: function (value) {
  496. this._dPadLeft = this._setDPadValue(value, this._dPadLeft, 2 /* Left */);
  497. },
  498. enumerable: true,
  499. configurable: true
  500. });
  501. Object.defineProperty(Xbox360Pad.prototype, "dPadRight", {
  502. get: function () {
  503. return this._dPadRight;
  504. },
  505. set: function (value) {
  506. this._dPadRight = this._setDPadValue(value, this._dPadRight, 3 /* Right */);
  507. },
  508. enumerable: true,
  509. configurable: true
  510. });
  511. Xbox360Pad.prototype.update = function () {
  512. _super.prototype.update.call(this);
  513. this.buttonA = this.browserGamepad.buttons[0].value;
  514. this.buttonB = this.browserGamepad.buttons[1].value;
  515. this.buttonX = this.browserGamepad.buttons[2].value;
  516. this.buttonY = this.browserGamepad.buttons[3].value;
  517. this.buttonLB = this.browserGamepad.buttons[4].value;
  518. this.buttonRB = this.browserGamepad.buttons[5].value;
  519. this.leftTrigger = this.browserGamepad.buttons[6].value;
  520. this.rightTrigger = this.browserGamepad.buttons[7].value;
  521. this.buttonBack = this.browserGamepad.buttons[8].value;
  522. this.buttonStart = this.browserGamepad.buttons[9].value;
  523. this.buttonLeftStick = this.browserGamepad.buttons[10].value;
  524. this.buttonRightStick = this.browserGamepad.buttons[11].value;
  525. this.dPadUp = this.browserGamepad.buttons[12].value;
  526. this.dPadDown = this.browserGamepad.buttons[13].value;
  527. this.dPadLeft = this.browserGamepad.buttons[14].value;
  528. this.dPadRight = this.browserGamepad.buttons[15].value;
  529. };
  530. return Xbox360Pad;
  531. })(Gamepad);
  532. BABYLON.Xbox360Pad = Xbox360Pad;
  533. })(BABYLON || (BABYLON = {}));
  534. //# sourceMappingURL=babylon.gamepads.js.map