Pointer.js 8.2 KB

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