babylon.gamepads.js 28 KB

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