babylon.gamepads.js 28 KB

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