forked from melonjs/melonJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer.js
More file actions
386 lines (336 loc) · 11.3 KB
/
Copy pathpointer.js
File metadata and controls
386 lines (336 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import Vector2d from "./../math/vector2.js";
import Bounds from "./../physics/bounds.js";
import game from "./../game.js";
import { globalToLocal } from "./input.js";
import { locked } from "./pointerevent.js";
/**
* a temporary vector object
* @ignore
*/
var tmpVec = new Vector2d();
/**
* @classdesc
* a pointer object, representing a single finger on a touch enabled device.
* @class Pointer
* @augments Bounds
*/
class Pointer extends Bounds {
/**
* @ignore
*/
constructor(x = 0, y = 0, w = 1, h = 1) {
// parent constructor
super();
// initial coordinates/size
this.setMinMax(x, y, x + w, y + h);
/**
* constant for left button
* @public
* @type {number}
* @name LEFT
* @memberof Pointer
*/
this.LEFT = 0;
/**
* constant for middle button
* @public
* @type {number}
* @name MIDDLE
* @memberof Pointer
*/
this.MIDDLE = 1;
/**
* constant for right button
* @public
* @type {number}
* @name RIGHT
* @memberof Pointer
*/
this.RIGHT = 2;
/**
* the originating Event Object
* @public
* @type {PointerEvent|TouchEvent|MouseEvent}
* @name event
* @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent
* @see https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent
* @memberof Pointer
*/
this.event = undefined;
/**
* a string containing the event's type.
* @public
* @type {string}
* @name type
* @see https://developer.mozilla.org/en-US/docs/Web/API/Event/type
* @memberof Pointer
*/
this.type = undefined;
/**
* the button property indicates which button was pressed on the mouse to trigger the event.
* @public
* @type {number}
* @name button
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
* @memberof Pointer
*/
this.button = 0;
/**
* indicates whether or not the pointer device that created the event is the primary pointer.
* @public
* @type {boolean}
* @name isPrimary
* @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/isPrimary
* @memberof Pointer
*/
this.isPrimary = false;
/**
* the horizontal coordinate at which the event occurred, relative to the left edge of the entire document.
* @public
* @type {number}
* @name pageX
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/pageX
* @memberof Pointer
*/
this.pageX = 0;
/**
* the vertical coordinate at which the event occurred, relative to the left edge of the entire document.
* @public
* @type {number}
* @name pageY
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/pageY
* @memberof Pointer
*/
this.pageY = 0;
/**
* the horizontal coordinate within the application's client area at which the event occurred
* @public
* @type {number}
* @name clientX
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/clientX
* @memberof Pointer
*/
this.clientX = 0;
/**
* the vertical coordinate within the application's client area at which the event occurred
* @public
* @type {number}
* @name clientY
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/clientY
* @memberof Pointer
*/
this.clientY = 0;
/**
* the difference in the X coordinate of the pointer since the previous move event
* @public
* @type {number}
* @name movementX
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/movementX
* @memberof Pointer
*/
this.movementX = 0;
/**
* the difference in the Y coordinate of the pointer since the previous move event
* @public
* @type {number}
* @name movementY
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/movementY
* @memberof Pointer
*/
this.movementY = 0;
/**
* an unsigned long representing the unit of the delta values scroll amount
* @public
* @type {number}
* @name deltaMode
* @see https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/deltaMode
* @memberof Pointer
*/
this.deltaMode = 0;
/**
* a double representing the horizontal scroll amount in the Wheel Event deltaMode unit.
* @public
* @type {number}
* @name deltaX
* @see https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/deltaX
* @memberof Pointer
*/
this.deltaX = 0;
/**
* a double representing the vertical scroll amount in the Wheel Event deltaMode unit.
* @public
* @type {number}
* @name deltaY
* @see https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/deltaY
* @memberof Pointer
*/
this.deltaY = 0;
/**
* a double representing the scroll amount in the z-axis, in the Wheel Event deltaMode unit.
* @public
* @type {number}
* @name deltaZ
* @see https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/deltaZ
* @memberof Pointer
*/
this.deltaZ = 0;
/**
* Event normalized X coordinate within the game canvas itself<br>
* <img src="images/event_coord.png"/>
* @public
* @type {number}
* @name gameX
* @memberof Pointer
*/
this.gameX = 0;
/**
* Event normalized Y coordinate within the game canvas itself<br>
* <img src="images/event_coord.png"/>
* @public
* @type {number}
* @name gameY
* @memberof Pointer
*/
this.gameY = 0;
/**
* Event X coordinate relative to the viewport
* @public
* @type {number}
* @name gameScreenX
* @memberof Pointer
*/
this.gameScreenX = 0;
/**
* Event Y coordinate relative to the viewport
* @public
* @type {number}
* @name gameScreenY
* @memberof Pointer
*/
this.gameScreenY = 0;
/**
* Event X coordinate relative to the map
* @public
* @type {number}
* @name gameWorldX
* @memberof Pointer
*/
this.gameWorldX = 0;
/**
* Event Y coordinate relative to the map
* @public
* @type {number}
* @name gameWorldY
* @memberof Pointer
*/
this.gameWorldY = 0;
/**
* Event X coordinate relative to the holding container
* @public
* @type {number}
* @name gameLocalX
* @memberof Pointer
*/
this.gameLocalX = 0;
/**
* Event Y coordinate relative to the holding container
* @public
* @type {number}
* @name gameLocalY
* @memberof Pointer
*/
this.gameLocalY = 0;
/**
* The unique identifier of the contact for a touch, mouse or pen
* @public
* @type {number}
* @name pointerId
* @memberof Pointer
* @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pointerId
*/
this.pointerId = undefined;
/**
* true if not originally a pointer event
* @public
* @type {boolean}
* @name isNormalized
* @memberof Pointer
*/
this.isNormalized = false;
/**
* true if the pointer is currently locked
* @public
* @type {boolean}
* @name locked
* @memberof Pointer
*/
this.locked = false;
// bind list for mouse buttons
this.bind = [ 0, 0, 0 ];
}
/**
* initialize the Pointer object using the given Event Object
* @name Pointer#set
* @private
* @param {Event} event - the original Event object
* @param {number} [pageX=0] - the horizontal coordinate at which the event occurred, relative to the left edge of the entire document
* @param {number} [pageY=0] - the vertical coordinate at which the event occurred, relative to the left edge of the entire document
* @param {number} [clientX=0] - the horizontal coordinate within the application's client area at which the event occurred
* @param {number} [clientY=0] - the vertical coordinate within the application's client area at which the event occurred
* @param {number} [pointerId=1] - the Pointer, Touch or Mouse event Id (1)
*/
setEvent(event, pageX = 0, pageY = 0, clientX = 0, clientY = 0, pointerId = 1) {
// the original event object
this.event = event;
this.pageX = pageX;
this.pageY = pageY;
this.clientX = clientX;
this.clientY = clientY;
// translate to local coordinates
globalToLocal(this.pageX, this.pageY, tmpVec);
this.gameScreenX = this.x = tmpVec.x;
this.gameScreenY = this.y = tmpVec.y;
// true if not originally a pointer event
this.isNormalized = (typeof globalThis.PointerEvent !== "undefined" && !(event instanceof globalThis.PointerEvent));
this.locked = locked;
this.movementX = event.movementX || 0;
this.movementY = event.movementY || 0;
if (event.type === "wheel") {
this.deltaMode = event.deltaMode || 0;
this.deltaX = event.deltaX || 0;
this.deltaY = event.deltaY || 0;
this.deltaZ = event.deltaZ || 0;
} else {
this.deltaMode = 0;
this.deltaX = 0;
this.deltaY = 0;
this.deltaZ = 0;
}
this.pointerId = pointerId;
this.isPrimary = (typeof event.isPrimary !== "undefined") ? event.isPrimary : true;
// in case of touch events, button is not defined
this.button = event.button || 0;
this.type = event.type;
// get the current screen to game world offset
if (typeof game.viewport !== "undefined") {
game.viewport.localToWorld(this.gameScreenX, this.gameScreenY, tmpVec);
}
/* Initialize the two coordinate space properties. */
this.gameWorldX = tmpVec.x;
this.gameWorldY = tmpVec.y;
// get the pointer size
if (this.isNormalized === false) {
// native PointerEvent
this.width = event.width || 1;
this.height = event.height || 1;
} else if (typeof(event.radiusX) === "number") {
// TouchEvent
this.width = (event.radiusX * 2) || 1;
this.height = (event.radiusY * 2) || 1;
} else {
this.width = this.height = 1;
}
}
}
export default Pointer;