dualShockGamepad.js 12 KB

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