babylon.gamepads.js 21 KB

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