Pointer.js 8.2 KB

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