babylon.xboxGamepad.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. module BABYLON {
  2. export enum Xbox360Button {
  3. A,
  4. B,
  5. X,
  6. Y,
  7. Start,
  8. Back,
  9. LB,
  10. RB,
  11. LeftStick,
  12. RightStick
  13. }
  14. export enum Xbox360Dpad {
  15. Up,
  16. Down,
  17. Left,
  18. Right
  19. }
  20. export class Xbox360Pad extends Gamepad {
  21. private _leftTrigger: number = 0;
  22. private _rightTrigger: number = 0;
  23. private _onlefttriggerchanged: (value: number) => void;
  24. private _onrighttriggerchanged: (value: number) => void;
  25. private _onbuttondown: (buttonPressed: Xbox360Button) => void;
  26. private _onbuttonup: (buttonReleased: Xbox360Button) => void;
  27. private _ondpaddown: (dPadPressed: Xbox360Dpad) => void;
  28. private _ondpadup: (dPadReleased: Xbox360Dpad) => void;
  29. public onButtonDownObservable = new Observable<Xbox360Button>();
  30. public onButtonUpObservable = new Observable<Xbox360Button>();
  31. public onPadDownObservable = new Observable<Xbox360Dpad>();
  32. public onPadUpObservable = new Observable<Xbox360Dpad>();
  33. private _buttonA: number = 0;
  34. private _buttonB: number = 0;
  35. private _buttonX: number = 0;
  36. private _buttonY: number = 0;
  37. private _buttonBack: number = 0;
  38. private _buttonStart: number = 0;
  39. private _buttonLB: number = 0;
  40. private _buttonRB: number = 0;
  41. private _buttonLeftStick: number = 0;
  42. private _buttonRightStick: number = 0;
  43. private _dPadUp: number = 0;
  44. private _dPadDown: number = 0;
  45. private _dPadLeft: number = 0;
  46. private _dPadRight: number = 0;
  47. private _isXboxOnePad: boolean = false;
  48. constructor(id: string, index: number, gamepad: any, xboxOne: boolean = false) {
  49. super(id, index, gamepad, 0, 1, 2, 3);
  50. this.type = Gamepad.XBOX;
  51. this._isXboxOnePad = xboxOne;
  52. }
  53. public onlefttriggerchanged(callback: (value: number) => void) {
  54. this._onlefttriggerchanged = callback;
  55. }
  56. public onrighttriggerchanged(callback: (value: number) => void) {
  57. this._onrighttriggerchanged = callback;
  58. }
  59. public get leftTrigger(): number {
  60. return this._leftTrigger;
  61. }
  62. public set leftTrigger(newValue: number) {
  63. if (this._onlefttriggerchanged && this._leftTrigger !== newValue) {
  64. this._onlefttriggerchanged(newValue);
  65. }
  66. this._leftTrigger = newValue;
  67. }
  68. public get rightTrigger(): number {
  69. return this._rightTrigger;
  70. }
  71. public set rightTrigger(newValue: number) {
  72. if (this._onrighttriggerchanged && this._rightTrigger !== newValue) {
  73. this._onrighttriggerchanged(newValue);
  74. }
  75. this._rightTrigger = newValue;
  76. }
  77. public onbuttondown(callback: (buttonPressed: Xbox360Button) => void) {
  78. this._onbuttondown = callback;
  79. }
  80. public onbuttonup(callback: (buttonReleased: Xbox360Button) => void) {
  81. this._onbuttonup = callback;
  82. }
  83. public ondpaddown(callback: (dPadPressed: Xbox360Dpad) => void) {
  84. this._ondpaddown = callback;
  85. }
  86. public ondpadup(callback: (dPadReleased: Xbox360Dpad) => void) {
  87. this._ondpadup = callback;
  88. }
  89. private _setButtonValue(newValue: number, currentValue: number, buttonType: Xbox360Button): number {
  90. if (newValue !== currentValue) {
  91. if (newValue === 1) {
  92. if (this._onbuttondown) {
  93. this._onbuttondown(buttonType);
  94. }
  95. this.onButtonDownObservable.notifyObservers(buttonType);
  96. }
  97. if (newValue === 0) {
  98. if (this._onbuttonup) {
  99. this._onbuttonup(buttonType);
  100. }
  101. this.onButtonUpObservable.notifyObservers(buttonType);
  102. }
  103. }
  104. return newValue;
  105. }
  106. private _setDPadValue(newValue: number, currentValue: number, buttonType: Xbox360Dpad): number {
  107. if (newValue !== currentValue) {
  108. if (newValue === 1) {
  109. if (this._ondpaddown) {
  110. this._ondpaddown(buttonType);
  111. }
  112. this.onPadDownObservable.notifyObservers(buttonType);
  113. }
  114. if (newValue === 0) {
  115. if (this._ondpadup) {
  116. this._ondpadup(buttonType);
  117. }
  118. this.onPadUpObservable.notifyObservers(buttonType);
  119. }
  120. }
  121. return newValue;
  122. }
  123. public get buttonA(): number {
  124. return this._buttonA;
  125. }
  126. public set buttonA(value) {
  127. this._buttonA = this._setButtonValue(value, this._buttonA, Xbox360Button.A);
  128. }
  129. public get buttonB(): number {
  130. return this._buttonB;
  131. }
  132. public set buttonB(value) {
  133. this._buttonB = this._setButtonValue(value, this._buttonB, Xbox360Button.B);
  134. }
  135. public get buttonX(): number {
  136. return this._buttonX;
  137. }
  138. public set buttonX(value) {
  139. this._buttonX = this._setButtonValue(value, this._buttonX, Xbox360Button.X);
  140. }
  141. public get buttonY(): number {
  142. return this._buttonY;
  143. }
  144. public set buttonY(value) {
  145. this._buttonY = this._setButtonValue(value, this._buttonY, Xbox360Button.Y);
  146. }
  147. public get buttonStart(): number {
  148. return this._buttonStart;
  149. }
  150. public set buttonStart(value) {
  151. this._buttonStart = this._setButtonValue(value, this._buttonStart, Xbox360Button.Start);
  152. }
  153. public get buttonBack(): number {
  154. return this._buttonBack;
  155. }
  156. public set buttonBack(value) {
  157. this._buttonBack = this._setButtonValue(value, this._buttonBack, Xbox360Button.Back);
  158. }
  159. public get buttonLB(): number {
  160. return this._buttonLB;
  161. }
  162. public set buttonLB(value) {
  163. this._buttonLB = this._setButtonValue(value, this._buttonLB, Xbox360Button.LB);
  164. }
  165. public get buttonRB(): number {
  166. return this._buttonRB;
  167. }
  168. public set buttonRB(value) {
  169. this._buttonRB = this._setButtonValue(value, this._buttonRB, Xbox360Button.RB);
  170. }
  171. public get buttonLeftStick(): number {
  172. return this._buttonLeftStick;
  173. }
  174. public set buttonLeftStick(value) {
  175. this._buttonLeftStick = this._setButtonValue(value, this._buttonLeftStick, Xbox360Button.LeftStick);
  176. }
  177. public get buttonRightStick(): number {
  178. return this._buttonRightStick;
  179. }
  180. public set buttonRightStick(value) {
  181. this._buttonRightStick = this._setButtonValue(value, this._buttonRightStick, Xbox360Button.RightStick);
  182. }
  183. public get dPadUp(): number {
  184. return this._dPadUp;
  185. }
  186. public set dPadUp(value) {
  187. this._dPadUp = this._setDPadValue(value, this._dPadUp, Xbox360Dpad.Up);
  188. }
  189. public get dPadDown(): number {
  190. return this._dPadDown;
  191. }
  192. public set dPadDown(value) {
  193. this._dPadDown = this._setDPadValue(value, this._dPadDown, Xbox360Dpad.Down);
  194. }
  195. public get dPadLeft(): number {
  196. return this._dPadLeft;
  197. }
  198. public set dPadLeft(value) {
  199. this._dPadLeft = this._setDPadValue(value, this._dPadLeft, Xbox360Dpad.Left);
  200. }
  201. public get dPadRight(): number {
  202. return this._dPadRight;
  203. }
  204. public set dPadRight(value) {
  205. this._dPadRight = this._setDPadValue(value, this._dPadRight, Xbox360Dpad.Right);
  206. }
  207. public update() {
  208. super.update();
  209. if (this._isXboxOnePad) {
  210. this.buttonA = this.browserGamepad.buttons[0].value;
  211. this.buttonB = this.browserGamepad.buttons[1].value;
  212. this.buttonX = this.browserGamepad.buttons[2].value;
  213. this.buttonY = this.browserGamepad.buttons[3].value;
  214. this.buttonLB = this.browserGamepad.buttons[4].value;
  215. this.buttonRB = this.browserGamepad.buttons[5].value;
  216. this.leftTrigger = this.browserGamepad.axes[2];
  217. this.rightTrigger = this.browserGamepad.axes[5];
  218. this.buttonBack = this.browserGamepad.buttons[9].value;
  219. this.buttonStart = this.browserGamepad.buttons[8].value;
  220. this.buttonLeftStick = this.browserGamepad.buttons[6].value;
  221. this.buttonRightStick = this.browserGamepad.buttons[7].value;
  222. this.dPadUp = this.browserGamepad.buttons[11].value;
  223. this.dPadDown = this.browserGamepad.buttons[12].value;
  224. this.dPadLeft = this.browserGamepad.buttons[13].value;
  225. this.dPadRight = this.browserGamepad.buttons[14].value;
  226. } else {
  227. this.buttonA = this.browserGamepad.buttons[0].value;
  228. this.buttonB = this.browserGamepad.buttons[1].value;
  229. this.buttonX = this.browserGamepad.buttons[2].value;
  230. this.buttonY = this.browserGamepad.buttons[3].value;
  231. this.buttonLB = this.browserGamepad.buttons[4].value;
  232. this.buttonRB = this.browserGamepad.buttons[5].value;
  233. this.leftTrigger = this.browserGamepad.buttons[6].value;
  234. this.rightTrigger = this.browserGamepad.buttons[7].value;
  235. this.buttonBack = this.browserGamepad.buttons[8].value;
  236. this.buttonStart = this.browserGamepad.buttons[9].value;
  237. this.buttonLeftStick = this.browserGamepad.buttons[10].value;
  238. this.buttonRightStick = this.browserGamepad.buttons[11].value;
  239. this.dPadUp = this.browserGamepad.buttons[12].value;
  240. this.dPadDown = this.browserGamepad.buttons[13].value;
  241. this.dPadLeft = this.browserGamepad.buttons[14].value;
  242. this.dPadRight = this.browserGamepad.buttons[15].value;
  243. }
  244. }
  245. }
  246. }