Pointer.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. import {EventManager} from "../utils/EventManager.js";
  2. import {Vector2} from "../math/Vector2.js";
  3. import {Key} from "./Key.js";
  4. /**
  5. * Pointer object is used to called input from the user, works for booth mouse or touch screens.
  6. *
  7. * It is responsible for synchronizing user input with the render of the graphics.
  8. *
  9. * @class
  10. * @param {Element} domElement DOM element to create the pointer events.
  11. * @param {Element} canvas Canvas DOM element where the content is being drawn.
  12. */
  13. function Pointer(domElement, canvas)
  14. {
  15. //Raw data
  16. this._keys = new Array(5);
  17. this._position = new Vector2(0, 0);
  18. this._positionUpdated = false;
  19. this._delta = new Vector2(0, 0);
  20. this._wheel = 0;
  21. this._wheelUpdated = false;
  22. this._doubleClicked = new Array(5);
  23. /**
  24. * Array with pointer buttons status.
  25. *
  26. * @type {number[]}
  27. */
  28. this.keys = new Array(5);
  29. /**
  30. * Pointer position inside of the window (coordinates in window space).
  31. *
  32. * This value is accumulated from multiple mouse triggered events between updated.
  33. *
  34. * @type {Vector2}
  35. */
  36. this.position = new Vector2(0, 0);
  37. /**
  38. * Pointer movement (coordinates in window space). Since the last update.
  39. *
  40. * This value is accumulated from multiple mouse triggered events between updated.
  41. *
  42. * @type {Vector2}
  43. */
  44. this.delta = new Vector2(0, 0);
  45. /**
  46. * Pointer scroll wheel movement, since the last update.
  47. *
  48. * @type {number}
  49. */
  50. this.wheel = 0;
  51. /**
  52. * Indicates a button of the pointer was double clicked.
  53. *
  54. * @type {boolean}
  55. */
  56. this.doubleClicked = new Array(5);
  57. /**
  58. * DOM element where to attach the pointer events.
  59. *
  60. * @type {Element}
  61. */
  62. this.domElement = (domElement !== undefined) ? domElement : window;
  63. /**
  64. * Canvas attached to this pointer instance used to calculate position and delta in element space coordinates.
  65. *
  66. * @type {Element}
  67. */
  68. this.canvas = null;
  69. if(canvas !== undefined)
  70. {
  71. this.setCanvas(canvas);
  72. }
  73. /**
  74. * Event manager responsible for updating the raw data variables.
  75. *
  76. * Different events are used depending on the host platform.
  77. *
  78. * When the update method is called the raw data is reset.
  79. *
  80. * @type {EventManager}
  81. */
  82. this.events = new EventManager();
  83. //Initialize key instances
  84. for(var i = 0; i < 5; i++)
  85. {
  86. this._doubleClicked[i] = false;
  87. this.doubleClicked[i] = false;
  88. this._keys[i] = new Key();
  89. this.keys[i] = new Key();
  90. }
  91. //Self pointer
  92. var self = this;
  93. //Scroll wheel
  94. if(window.onmousewheel !== undefined)
  95. {
  96. //Chrome, edge
  97. this.events.add(this.domElement, "mousewheel", function(event)
  98. {
  99. self._wheel = event.deltaY;
  100. self._wheelUpdated = true;
  101. });
  102. }
  103. else if(window.addEventListener !== undefined)
  104. {
  105. //Firefox
  106. this.events.add(this.domElement, "DOMMouseScroll", function(event)
  107. {
  108. self._wheel = event.detail * 30;
  109. self._wheelUpdated = true;
  110. });
  111. }
  112. else
  113. {
  114. this.events.add(this.domElement, "wheel", function(event)
  115. {
  116. self._wheel = event.deltaY;
  117. self._wheelUpdated = true;
  118. });
  119. }
  120. //Touchscreen input events
  121. if(window.ontouchstart !== undefined || navigator.msMaxTouchPoints > 0)
  122. {
  123. //Auxiliar variables to calculate touch delta
  124. var lastTouch = new Vector2(0, 0);
  125. //Touch start event
  126. this.events.add(this.domElement, "touchstart", function(event)
  127. {
  128. var touch = event.touches[0];
  129. self.updatePosition(touch.clientX, touch.clientY, 0, 0);
  130. self.updateKey(Pointer.LEFT, Key.DOWN);
  131. lastTouch.set(touch.clientX, touch.clientY);
  132. });
  133. //Touch end event
  134. this.events.add(this.domElement, "touchend", function(event)
  135. {
  136. self.updateKey(Pointer.LEFT, Key.UP);
  137. });
  138. //Touch cancel event
  139. this.events.add(this.domElement, "touchcancel", function(event)
  140. {
  141. self.updateKey(Pointer.LEFT, Key.UP);
  142. });
  143. //Touch move event
  144. this.events.add(document.body, "touchmove", function(event)
  145. {
  146. var touch = event.touches[0];
  147. self.updatePosition(touch.clientX, touch.clientY, touch.clientX - lastTouch.x, touch.clientY - lastTouch.y);
  148. lastTouch.set(touch.clientX, touch.clientY);
  149. });
  150. }
  151. //Move
  152. this.events.add(this.domElement, "mousemove", function(event)
  153. {
  154. self.updatePosition(event.clientX, event.clientY, event.movementX, event.movementY);
  155. });
  156. //Button pressed
  157. this.events.add(this.domElement, "mousedown", function(event)
  158. {
  159. self.updateKey(event.which - 1, Key.DOWN);
  160. });
  161. //Button released
  162. this.events.add(this.domElement, "mouseup", function(event)
  163. {
  164. self.updateKey(event.which - 1, Key.UP);
  165. });
  166. //Drag start
  167. this.events.add(this.domElement, "dragstart", function(event)
  168. {
  169. self.updateKey(event.which - 1, Key.UP);
  170. });
  171. //Pointer double click
  172. this.events.add(this.domElement, "dblclick", function(event)
  173. {
  174. self._doubleClicked[event.which - 1] = true;
  175. });
  176. this.create();
  177. }
  178. Pointer.prototype = Pointer;
  179. Pointer.prototype.constructor = Pointer;
  180. /**
  181. * Left pointer button.
  182. *
  183. * @static
  184. * @type {number}
  185. */
  186. Pointer.LEFT = 0;
  187. /**
  188. * Middle pointer button.
  189. *
  190. * @static
  191. * @type {number}
  192. */
  193. Pointer.MIDDLE = 1;
  194. /**
  195. * Right pointer button.
  196. *
  197. * @static
  198. * @type {number}
  199. */
  200. Pointer.RIGHT = 2;
  201. /**
  202. * Back pointer navigation button.
  203. *
  204. * @static
  205. * @type {number}
  206. */
  207. Pointer.BACK = 3;
  208. /**
  209. * Forward pointer navigation button.
  210. *
  211. * @static
  212. * @type {number}
  213. */
  214. Pointer.FORWARD = 4;
  215. /**
  216. * Element to be used for coordinates calculation relative to that canvas.
  217. *
  218. * @param {DOM} element Canvas to be attached to the Pointer instance
  219. */
  220. Pointer.setCanvas = function(element)
  221. {
  222. this.canvas = element;
  223. element.pointerInside = false;
  224. element.addEventListener("mouseenter", function()
  225. {
  226. this.pointerInside = true;
  227. });
  228. element.addEventListener("mouseleave", function()
  229. {
  230. this.pointerInside = false;
  231. });
  232. };
  233. /**
  234. * Check if pointer is inside attached canvas (updated async).
  235. *
  236. * @return {boolean} True if pointer is currently inside the canvas
  237. */
  238. Pointer.insideCanvas = function()
  239. {
  240. return this.canvas !== null && this.canvas.pointerInside;
  241. };
  242. /**
  243. * Check if pointer button is currently pressed.
  244. *
  245. * @param {Number} button Button to check status of
  246. * @return {boolean} True if button is currently pressed
  247. */
  248. Pointer.buttonPressed = function(button)
  249. {
  250. return this.keys[button].pressed;
  251. };
  252. /**
  253. * Check if pointer button was double clicked.
  254. *
  255. * @param {Number} button Button to check status of
  256. * @return {boolean} True if some pointer button was just double clicked
  257. */
  258. Pointer.buttonDoubleClicked = function(button)
  259. {
  260. return this.doubleClicked[button];
  261. };
  262. /**
  263. * Check if a pointer button was just pressed.
  264. *
  265. * @param {Number} button Button to check status of
  266. * @return {boolean} True if button was just pressed
  267. */
  268. Pointer.buttonJustPressed = function(button)
  269. {
  270. return this.keys[button].justPressed;
  271. };
  272. /**
  273. * Check if a pointer button was just released.
  274. *
  275. * @param {Number} button Button to check status of
  276. * @return {boolean} True if button was just released
  277. */
  278. Pointer.buttonJustReleased = function(button)
  279. {
  280. return this.keys[button].justReleased;
  281. };
  282. /**
  283. * Update pointer position.
  284. *
  285. * @param {Number} x
  286. * @param {Number} y
  287. * @param {Number} xDiff
  288. * @param {Number} yDiff
  289. */
  290. Pointer.updatePosition = function(x, y, xDiff, yDiff)
  291. {
  292. if(this.canvas !== null)
  293. {
  294. var rect = this.canvas.getBoundingClientRect();
  295. x -= rect.left;
  296. y -= rect.top;
  297. }
  298. this._position.set(x, y);
  299. this._delta.x += xDiff;
  300. this._delta.y += yDiff;
  301. this._positionUpdated = true;
  302. };
  303. /**
  304. * Update a pointer button.
  305. *
  306. * @param {Number} button
  307. * @param {Number} action
  308. */
  309. Pointer.updateKey = function(button, action)
  310. {
  311. if(button > -1)
  312. {
  313. this._keys[button].update(action);
  314. }
  315. };
  316. /**
  317. * Update pointer buttons state, position, wheel and delta synchronously.
  318. *
  319. * Should be called every frame on the update loop before reading any values from the pointer.
  320. */
  321. Pointer.update = function()
  322. {
  323. //Update pointer keys state
  324. for(var i = 0; i < 5; i++)
  325. {
  326. if(this._keys[i].justPressed && this.keys[i].justPressed)
  327. {
  328. this._keys[i].justPressed = false;
  329. }
  330. if(this._keys[i].justReleased && this.keys[i].justReleased)
  331. {
  332. this._keys[i].justReleased = false;
  333. }
  334. this.keys[i].set(this._keys[i].justPressed, this._keys[i].pressed, this._keys[i].justReleased);
  335. //Update pointer double click
  336. if(this._doubleClicked[i] === true)
  337. {
  338. this.doubleClicked[i] = true;
  339. this._doubleClicked[i] = false;
  340. }
  341. else
  342. {
  343. this.doubleClicked[i] = false;
  344. }
  345. }
  346. //Update pointer wheel
  347. if(this._wheelUpdated)
  348. {
  349. this.wheel = this._wheel;
  350. this._wheelUpdated = false;
  351. }
  352. else
  353. {
  354. this.wheel = 0;
  355. }
  356. //Update pointer Position if needed
  357. if(this._positionUpdated)
  358. {
  359. this.delta.copy(this._delta);
  360. this.position.copy(this._position);
  361. this._delta.set(0,0);
  362. this._positionUpdated = false;
  363. }
  364. else
  365. {
  366. this.delta.x = 0;
  367. this.delta.y = 0;
  368. }
  369. };
  370. /**
  371. * Create pointer events to collect input data.
  372. *
  373. * Should be called before using the pointer object.
  374. */
  375. Pointer.create = function()
  376. {
  377. this.events.create();
  378. };
  379. /**
  380. * Dispose pointer events, should be called after the objects is no longer required.
  381. *
  382. * If not called leaves the window events created leaving a memory/code leak.
  383. */
  384. Pointer.dispose = function()
  385. {
  386. this.events.destroy();
  387. };
  388. export {Pointer};