/
PointerEventsTarget.ts
194 lines (165 loc) · 5.93 KB
/
PointerEventsTarget.ts
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
/*!
* Copyright 2024 Cognite AS
*/
import { getPixelCoordinatesFromEvent } from './getPixelCoordinatesFromEvent';
import debounce from 'lodash/debounce';
import { Vector2, MOUSE } from 'three';
import { PointerEvents } from './PointerEvents';
const MAX_MOVE_DISTANCE_DURING_CLICK = 8;
const MAX_CLICK_DURATION = 250;
const DOUBLE_CLICK_INTERVAL = 300;
const HOVER_INTERVAL = 100;
/**
* This class fires click, double click, hover end similar events at a PointerEvents
* onClick will fired if it's a single click, and the mouse hasn't move too much
* If onDoubleClick is fired, the onClick will not be fired
* onHover will be fired only if the mouse button is not pressed and not to often
* If mouse, the onDoubleClick and onClick is fired when the left mouse button is pressed
* onPointerDrag will be fired wnen the mouse button is pressed and the mouse is moving
* @beta
*/
export class PointerEventsTarget {
//================================================
// INSTANCE FIELDS
//================================================
private readonly _domElement: HTMLElement;
private readonly _events: PointerEvents;
private _downPosition: Vector2 | undefined = undefined;
private _lastDownTimestamp = 0; // Time of last pointer down event
private _prevDownTimestamp = 0; // Time of previous pointer down event
private _clickCounter = 0; // Incremented at each onPointerDown
private _isLeftDown = false;
//================================================
// INSTANCE PROPERIES
//================================================
private get isEnabled(): boolean {
return this._events.isEnabled;
}
//================================================
// CONTRUCTOR
//================================================
constructor(domElement: HTMLElement, events: PointerEvents) {
this._domElement = domElement;
this._events = events;
}
//================================================
// INSTANCE METHODS: Public
//================================================
public addEventListeners(): void {
this._domElement.addEventListener('pointerdown', this.onPointerDown);
this._domElement.addEventListener('pointermove', this.onPointerHover);
window.addEventListener('pointermove', this.onPointerDrag);
window.addEventListener('pointerup', this.onPointerUp);
}
public removeEventListeners(): void {
this._domElement.removeEventListener('pointerdown', this.onPointerDown);
this._domElement.removeEventListener('pointermove', this.onPointerHover);
window.removeEventListener('pointermove', this.onPointerDrag);
window.removeEventListener('pointerup', this.onPointerUp);
}
//================================================
// EVENTS
//================================================
private readonly onPointerDown = async (event: PointerEvent) => {
if (!this.isEnabled) {
return;
}
event.stopPropagation();
event.preventDefault();
this._domElement.focus();
const leftButton = isTouch(event) || isLeftMouseButton(event);
this._downPosition = getPixelCoordinatesFromEvent(event, this._domElement);
this._prevDownTimestamp = this._lastDownTimestamp;
this._lastDownTimestamp = event.timeStamp;
this._clickCounter++;
await this._events.onPointerDown(event, leftButton);
this._isLeftDown = leftButton;
};
private readonly onPointerHover = debounce((event: PointerEvent) => {
if (!this.isEnabled) {
return;
}
if (!isMouse(event)) {
return;
}
if (isAnyMouseButtonPressed(event)) {
return;
}
this._events.onHover(event);
}, HOVER_INTERVAL);
private readonly onPointerDrag = async (event: PointerEvent) => {
event.stopPropagation();
event.preventDefault();
if (!this.isEnabled) {
return;
}
if (isMouse(event)) {
if (!isAnyMouseButtonPressed(event)) {
return;
}
if (event.movementX === 0 && event.movementY === 0) {
return;
}
}
await this._events.onPointerDrag(event, this._isLeftDown);
};
private readonly onPointerUp = async (event: PointerEvent) => {
if (!this.isEnabled) {
return;
}
event.stopPropagation();
event.preventDefault();
if (this._downPosition === undefined) {
return;
}
await this._events.onPointerUp(event, this._isLeftDown);
if (!this.isProperClick(event)) {
return;
}
if (this.isDoubleClick()) {
await this._events.onDoubleClick(event);
return;
}
const currentClickCounter = this._clickCounter;
setTimeout(() => {
// If the clickCounter has changed, it means that another click event has been fired in the meantime
if (currentClickCounter === this._clickCounter) {
this._events.onClick(event);
}
}, DOUBLE_CLICK_INTERVAL);
};
//================================================
// INSTANCE METHODS: Requests
//================================================
private isProperClick(event: PointerEvent): boolean {
if (this._downPosition === undefined) {
return false;
}
const clickDuration = event.timeStamp - this._lastDownTimestamp;
if (clickDuration >= MAX_CLICK_DURATION) {
return false;
}
const position = getPixelCoordinatesFromEvent(event, this._domElement);
const distance = position.distanceTo(this._downPosition);
return distance < MAX_MOVE_DISTANCE_DURING_CLICK;
}
private isDoubleClick(): boolean {
if (this._prevDownTimestamp <= 0) {
return false;
}
const interval = this._lastDownTimestamp - this._prevDownTimestamp;
return interval < DOUBLE_CLICK_INTERVAL;
}
}
function isAnyMouseButtonPressed(event: PointerEvent): boolean {
return event.buttons !== 0;
}
function isLeftMouseButton(event: PointerEvent): boolean {
return event.button === MOUSE.LEFT;
}
function isMouse(event: PointerEvent): boolean {
return event.pointerType === 'mouse';
}
function isTouch(event: PointerEvent): boolean {
return event.pointerType === 'touch';
}