dualShockGamepad.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. import { Observable } from "../Misc/observable";
  2. import { Gamepad } from "./gamepad";
  3. /**
  4. * Defines supported buttons for DualShock compatible gamepads
  5. */
  6. export enum DualShockButton {
  7. /** Cross */
  8. Cross,
  9. /** Circle */
  10. Circle,
  11. /** Square */
  12. Square,
  13. /** Triangle */
  14. Triangle,
  15. /** Options */
  16. Options,
  17. /** Share */
  18. Share,
  19. /** L1 */
  20. L1,
  21. /** R1 */
  22. R1,
  23. /** Left stick */
  24. LeftStick,
  25. /** Right stick */
  26. RightStick
  27. }
  28. /** Defines values for DualShock DPad */
  29. export enum DualShockDpad {
  30. /** Up */
  31. Up,
  32. /** Down */
  33. Down,
  34. /** Left */
  35. Left,
  36. /** Right */
  37. Right
  38. }
  39. /**
  40. * Defines a DualShock gamepad
  41. */
  42. export class DualShockPad extends Gamepad {
  43. private _leftTrigger: number = 0;
  44. private _rightTrigger: number = 0;
  45. private _onlefttriggerchanged: (value: number) => void;
  46. private _onrighttriggerchanged: (value: number) => void;
  47. private _onbuttondown: (buttonPressed: DualShockButton) => void;
  48. private _onbuttonup: (buttonReleased: DualShockButton) => void;
  49. private _ondpaddown: (dPadPressed: DualShockDpad) => void;
  50. private _ondpadup: (dPadReleased: DualShockDpad) => void;
  51. /** Observable raised when a button is pressed */
  52. public onButtonDownObservable = new Observable<DualShockButton>();
  53. /** Observable raised when a button is released */
  54. public onButtonUpObservable = new Observable<DualShockButton>();
  55. /** Observable raised when a pad is pressed */
  56. public onPadDownObservable = new Observable<DualShockDpad>();
  57. /** Observable raised when a pad is released */
  58. public onPadUpObservable = new Observable<DualShockDpad>();
  59. private _buttonCross: number = 0;
  60. private _buttonCircle: number = 0;
  61. private _buttonSquare: number = 0;
  62. private _buttonTriangle: number = 0;
  63. private _buttonShare: number = 0;
  64. private _buttonOptions: number = 0;
  65. private _buttonL1: number = 0;
  66. private _buttonR1: number = 0;
  67. private _buttonLeftStick: number = 0;
  68. private _buttonRightStick: number = 0;
  69. private _dPadUp: number = 0;
  70. private _dPadDown: number = 0;
  71. private _dPadLeft: number = 0;
  72. private _dPadRight: number = 0;
  73. /**
  74. * Creates a new DualShock gamepad object
  75. * @param id defines the id of this gamepad
  76. * @param index defines its index
  77. * @param gamepad defines the internal HTML gamepad object
  78. */
  79. constructor(id: string, index: number, gamepad: any) {
  80. super(id.replace("STANDARD GAMEPAD", "SONY PLAYSTATION DUALSHOCK"), index, gamepad, 0, 1, 2, 3);
  81. this.type = Gamepad.DUALSHOCK;
  82. }
  83. /**
  84. * Defines the callback to call when left trigger is pressed
  85. * @param callback defines the callback to use
  86. */
  87. public onlefttriggerchanged(callback: (value: number) => void) {
  88. this._onlefttriggerchanged = callback;
  89. }
  90. /**
  91. * Defines the callback to call when right trigger is pressed
  92. * @param callback defines the callback to use
  93. */
  94. public onrighttriggerchanged(callback: (value: number) => void) {
  95. this._onrighttriggerchanged = callback;
  96. }
  97. /**
  98. * Gets the left trigger value
  99. */
  100. public get leftTrigger(): number {
  101. return this._leftTrigger;
  102. }
  103. /**
  104. * Sets the left trigger value
  105. */
  106. public set leftTrigger(newValue: number) {
  107. if (this._onlefttriggerchanged && this._leftTrigger !== newValue) {
  108. this._onlefttriggerchanged(newValue);
  109. }
  110. this._leftTrigger = newValue;
  111. }
  112. /**
  113. * Gets the right trigger value
  114. */
  115. public get rightTrigger(): number {
  116. return this._rightTrigger;
  117. }
  118. /**
  119. * Sets the right trigger value
  120. */
  121. public set rightTrigger(newValue: number) {
  122. if (this._onrighttriggerchanged && this._rightTrigger !== newValue) {
  123. this._onrighttriggerchanged(newValue);
  124. }
  125. this._rightTrigger = newValue;
  126. }
  127. /**
  128. * Defines the callback to call when a button is pressed
  129. * @param callback defines the callback to use
  130. */
  131. public onbuttondown(callback: (buttonPressed: DualShockButton) => void) {
  132. this._onbuttondown = callback;
  133. }
  134. /**
  135. * Defines the callback to call when a button is released
  136. * @param callback defines the callback to use
  137. */
  138. public onbuttonup(callback: (buttonReleased: DualShockButton) => void) {
  139. this._onbuttonup = callback;
  140. }
  141. /**
  142. * Defines the callback to call when a pad is pressed
  143. * @param callback defines the callback to use
  144. */
  145. public ondpaddown(callback: (dPadPressed: DualShockDpad) => void) {
  146. this._ondpaddown = callback;
  147. }
  148. /**
  149. * Defines the callback to call when a pad is released
  150. * @param callback defines the callback to use
  151. */
  152. public ondpadup(callback: (dPadReleased: DualShockDpad) => void) {
  153. this._ondpadup = callback;
  154. }
  155. private _setButtonValue(newValue: number, currentValue: number, buttonType: DualShockButton): number {
  156. if (newValue !== currentValue) {
  157. if (newValue === 1) {
  158. if (this._onbuttondown) {
  159. this._onbuttondown(buttonType);
  160. }
  161. this.onButtonDownObservable.notifyObservers(buttonType);
  162. }
  163. if (newValue === 0) {
  164. if (this._onbuttonup) {
  165. this._onbuttonup(buttonType);
  166. }
  167. this.onButtonUpObservable.notifyObservers(buttonType);
  168. }
  169. }
  170. return newValue;
  171. }
  172. private _setDPadValue(newValue: number, currentValue: number, buttonType: DualShockDpad): number {
  173. if (newValue !== currentValue) {
  174. if (newValue === 1) {
  175. if (this._ondpaddown) {
  176. this._ondpaddown(buttonType);
  177. }
  178. this.onPadDownObservable.notifyObservers(buttonType);
  179. }
  180. if (newValue === 0) {
  181. if (this._ondpadup) {
  182. this._ondpadup(buttonType);
  183. }
  184. this.onPadUpObservable.notifyObservers(buttonType);
  185. }
  186. }
  187. return newValue;
  188. }
  189. /**
  190. * Gets the value of the `Cross` button
  191. */
  192. public get buttonCross(): number {
  193. return this._buttonCross;
  194. }
  195. /**
  196. * Sets the value of the `Cross` button
  197. */
  198. public set buttonCross(value) {
  199. this._buttonCross = this._setButtonValue(value, this._buttonCross, DualShockButton.Cross);
  200. }
  201. /**
  202. * Gets the value of the `Circle` button
  203. */
  204. public get buttonCircle(): number {
  205. return this._buttonCircle;
  206. }
  207. /**
  208. * Sets the value of the `Circle` button
  209. */
  210. public set buttonCircle(value) {
  211. this._buttonCircle = this._setButtonValue(value, this._buttonCircle, DualShockButton.Circle);
  212. }
  213. /**
  214. * Gets the value of the `Square` button
  215. */
  216. public get buttonSquare(): number {
  217. return this._buttonSquare;
  218. }
  219. /**
  220. * Sets the value of the `Square` button
  221. */
  222. public set buttonSquare(value) {
  223. this._buttonSquare = this._setButtonValue(value, this._buttonSquare, DualShockButton.Square);
  224. }
  225. /**
  226. * Gets the value of the `Triangle` button
  227. */
  228. public get buttonTriangle(): number {
  229. return this._buttonTriangle;
  230. }
  231. /**
  232. * Sets the value of the `Triangle` button
  233. */
  234. public set buttonTriangle(value) {
  235. this._buttonTriangle = this._setButtonValue(value, this._buttonTriangle, DualShockButton.Triangle);
  236. }
  237. /**
  238. * Gets the value of the `Options` button
  239. */
  240. public get buttonOptions(): number {
  241. return this._buttonOptions;
  242. }
  243. /**
  244. * Sets the value of the `Options` button
  245. */
  246. public set buttonOptions(value) {
  247. this._buttonOptions = this._setButtonValue(value, this._buttonOptions, DualShockButton.Options);
  248. }
  249. /**
  250. * Gets the value of the `Share` button
  251. */
  252. public get buttonShare(): number {
  253. return this._buttonShare;
  254. }
  255. /**
  256. * Sets the value of the `Share` button
  257. */
  258. public set buttonShare(value) {
  259. this._buttonShare = this._setButtonValue(value, this._buttonShare, DualShockButton.Share);
  260. }
  261. /**
  262. * Gets the value of the `L1` button
  263. */
  264. public get buttonL1(): number {
  265. return this._buttonL1;
  266. }
  267. /**
  268. * Sets the value of the `L1` button
  269. */
  270. public set buttonL1(value) {
  271. this._buttonL1 = this._setButtonValue(value, this._buttonL1, DualShockButton.L1);
  272. }
  273. /**
  274. * Gets the value of the `R1` button
  275. */
  276. public get buttonR1(): number {
  277. return this._buttonR1;
  278. }
  279. /**
  280. * Sets the value of the `R1` button
  281. */
  282. public set buttonR1(value) {
  283. this._buttonR1 = this._setButtonValue(value, this._buttonR1, DualShockButton.R1);
  284. }
  285. /**
  286. * Gets the value of the Left joystick
  287. */
  288. public get buttonLeftStick(): number {
  289. return this._buttonLeftStick;
  290. }
  291. /**
  292. * Sets the value of the Left joystick
  293. */
  294. public set buttonLeftStick(value) {
  295. this._buttonLeftStick = this._setButtonValue(value, this._buttonLeftStick, DualShockButton.LeftStick);
  296. }
  297. /**
  298. * Gets the value of the Right joystick
  299. */
  300. public get buttonRightStick(): number {
  301. return this._buttonRightStick;
  302. }
  303. /**
  304. * Sets the value of the Right joystick
  305. */
  306. public set buttonRightStick(value) {
  307. this._buttonRightStick = this._setButtonValue(value, this._buttonRightStick, DualShockButton.RightStick);
  308. }
  309. /**
  310. * Gets the value of D-pad up
  311. */
  312. public get dPadUp(): number {
  313. return this._dPadUp;
  314. }
  315. /**
  316. * Sets the value of D-pad up
  317. */
  318. public set dPadUp(value) {
  319. this._dPadUp = this._setDPadValue(value, this._dPadUp, DualShockDpad.Up);
  320. }
  321. /**
  322. * Gets the value of D-pad down
  323. */
  324. public get dPadDown(): number {
  325. return this._dPadDown;
  326. }
  327. /**
  328. * Sets the value of D-pad down
  329. */
  330. public set dPadDown(value) {
  331. this._dPadDown = this._setDPadValue(value, this._dPadDown, DualShockDpad.Down);
  332. }
  333. /**
  334. * Gets the value of D-pad left
  335. */
  336. public get dPadLeft(): number {
  337. return this._dPadLeft;
  338. }
  339. /**
  340. * Sets the value of D-pad left
  341. */
  342. public set dPadLeft(value) {
  343. this._dPadLeft = this._setDPadValue(value, this._dPadLeft, DualShockDpad.Left);
  344. }
  345. /**
  346. * Gets the value of D-pad right
  347. */
  348. public get dPadRight(): number {
  349. return this._dPadRight;
  350. }
  351. /**
  352. * Sets the value of D-pad right
  353. */
  354. public set dPadRight(value) {
  355. this._dPadRight = this._setDPadValue(value, this._dPadRight, DualShockDpad.Right);
  356. }
  357. /**
  358. * Force the gamepad to synchronize with device values
  359. */
  360. public update() {
  361. super.update();
  362. this.buttonCross = this.browserGamepad.buttons[0].value;
  363. this.buttonCircle = this.browserGamepad.buttons[1].value;
  364. this.buttonSquare = this.browserGamepad.buttons[2].value;
  365. this.buttonTriangle = this.browserGamepad.buttons[3].value;
  366. this.buttonL1 = this.browserGamepad.buttons[4].value;
  367. this.buttonR1 = this.browserGamepad.buttons[5].value;
  368. this.leftTrigger = this.browserGamepad.buttons[6].value;
  369. this.rightTrigger = this.browserGamepad.buttons[7].value;
  370. this.buttonShare = this.browserGamepad.buttons[8].value;
  371. this.buttonOptions = this.browserGamepad.buttons[9].value;
  372. this.buttonLeftStick = this.browserGamepad.buttons[10].value;
  373. this.buttonRightStick = this.browserGamepad.buttons[11].value;
  374. this.dPadUp = this.browserGamepad.buttons[12].value;
  375. this.dPadDown = this.browserGamepad.buttons[13].value;
  376. this.dPadLeft = this.browserGamepad.buttons[14].value;
  377. this.dPadRight = this.browserGamepad.buttons[15].value;
  378. }
  379. /**
  380. * Disposes the gamepad
  381. */
  382. public dispose() {
  383. super.dispose();
  384. this.onButtonDownObservable.clear();
  385. this.onButtonUpObservable.clear();
  386. this.onPadDownObservable.clear();
  387. this.onPadUpObservable.clear();
  388. }
  389. }