babylon.gamepads.js 28 KB

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