babylon.gamepads.ts 21 KB

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