@n3rdw1z4rd/core 0.18.5 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -10
- package/dist/astar.d.ts +44 -0
- package/dist/astar.js +118 -0
- package/dist/canvas-renderer.d.ts +154 -0
- package/dist/canvas-renderer.js +200 -0
- package/dist/clock.d.ts +69 -0
- package/dist/clock.js +108 -0
- package/dist/color-palette.d.ts +25 -0
- package/dist/color-palette.js +112 -0
- package/dist/color.d.ts +90 -0
- package/dist/color.js +224 -0
- package/dist/css/main.css +1 -3
- package/dist/disposable.d.ts +27 -0
- package/dist/disposable.js +32 -0
- package/dist/dungeon-generator.d.ts +142 -0
- package/dist/dungeon-generator.js +288 -0
- package/dist/ecs.d.ts +87 -0
- package/dist/ecs.js +101 -0
- package/dist/emitter.d.ts +40 -0
- package/dist/emitter.js +54 -0
- package/dist/event-queue.d.ts +29 -0
- package/dist/event-queue.js +40 -0
- package/dist/index.cjs +3813 -0
- package/dist/index.d.cts +2655 -0
- package/dist/index.d.ts +35 -873
- package/dist/index.js +34 -3981
- package/dist/input/action-map.d.ts +79 -0
- package/dist/input/action-map.js +131 -0
- package/dist/input/bindings.d.ts +273 -0
- package/dist/input/bindings.js +250 -0
- package/dist/input/gestures.d.ts +108 -0
- package/dist/input/gestures.js +159 -0
- package/dist/input/index.d.ts +5 -0
- package/dist/input/index.js +5 -0
- package/dist/input/input.d.ts +297 -0
- package/dist/input/input.js +695 -0
- package/dist/log.d.ts +8 -0
- package/dist/log.js +9 -0
- package/dist/map2d.d.ts +83 -0
- package/dist/map2d.js +120 -0
- package/dist/map3d-CHDJiq5T.d.cts +236 -0
- package/dist/map3d.d.ts +94 -0
- package/dist/map3d.js +148 -0
- package/dist/math.d.ts +176 -0
- package/dist/math.js +206 -0
- package/dist/misc.d.ts +46 -0
- package/dist/misc.js +71 -0
- package/dist/observable.d.ts +43 -0
- package/dist/observable.js +53 -0
- package/dist/particle-renderer.d.ts +47 -0
- package/dist/particle-renderer.js +82 -0
- package/dist/particle-system-2d.d.ts +60 -0
- package/dist/particle-system-2d.js +147 -0
- package/dist/random.d.ts +46 -0
- package/dist/random.js +74 -0
- package/dist/rectangle.d.ts +44 -0
- package/dist/rectangle.js +69 -0
- package/dist/rng.d.ts +72 -0
- package/dist/rng.js +152 -0
- package/dist/signal.d.ts +34 -0
- package/dist/signal.js +41 -0
- package/dist/spatial-partition-2d.d.ts +42 -0
- package/dist/spatial-partition-2d.js +52 -0
- package/dist/spatial-partition-3d-Ba-VBveM.cjs +1324 -0
- package/dist/spatial-partition-3d.d.ts +44 -0
- package/dist/spatial-partition-3d.js +57 -0
- package/dist/three/asteroid-mesh.d.ts +41 -0
- package/dist/three/asteroid-mesh.js +44 -0
- package/dist/three/camera-rig.d.ts +140 -0
- package/dist/three/camera-rig.js +197 -0
- package/dist/three/cube-sphere-geometry.d.ts +55 -0
- package/dist/three/cube-sphere-geometry.js +239 -0
- package/dist/three/ecs.d.ts +75 -0
- package/dist/three/ecs.js +64 -0
- package/dist/three/index.d.ts +12 -0
- package/dist/three/index.js +12 -0
- package/dist/three/particle-system-3d.d.ts +53 -0
- package/dist/three/particle-system-3d.js +224 -0
- package/dist/three/renderer.d.ts +38 -0
- package/dist/three/renderer.js +57 -0
- package/dist/three/starfield.d.ts +23 -0
- package/dist/three/starfield.js +64 -0
- package/dist/three/texture-atlas.d.ts +76 -0
- package/dist/three/texture-atlas.js +84 -0
- package/dist/three/texture-utils.d.ts +31 -0
- package/dist/three/texture-utils.js +44 -0
- package/dist/three/voxel-geometry.d.ts +72 -0
- package/dist/three/voxel-geometry.js +231 -0
- package/dist/three/voxel-mesh.d.ts +67 -0
- package/dist/three/voxel-mesh.js +71 -0
- package/dist/three.cjs +1307 -0
- package/dist/three.d.cts +655 -0
- package/dist/tilemap.d.ts +17 -0
- package/dist/tilemap.js +26 -0
- package/dist/types.d.ts +34 -0
- package/dist/types.js +0 -0
- package/dist/vector.d.ts +78 -0
- package/dist/vector.js +169 -0
- package/dist/webgl-renderer.d.ts +89 -0
- package/dist/webgl-renderer.js +140 -0
- package/dist/webgl.d.ts +357 -0
- package/dist/webgl.js +628 -0
- package/dist/worker-interface.d.ts +30 -0
- package/dist/worker-interface.js +39 -0
- package/package.json +55 -13
- package/dist/css/index.ts +0 -1
- package/dist/index.css +0 -104
- package/dist/index.d.mts +0 -873
- package/dist/index.mjs +0 -3977
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
//#region src/input/bindings.ts
|
|
2
|
+
/**
|
|
3
|
+
* W3C "standard" gamepad mapping, in button-index order (Xbox names; on PlayStation A = Cross,
|
|
4
|
+
* B = Circle, X = Square, Y = Triangle).
|
|
5
|
+
*
|
|
6
|
+
* @category Input
|
|
7
|
+
*/
|
|
8
|
+
const GAMEPAD_BUTTONS = [
|
|
9
|
+
"A",
|
|
10
|
+
"B",
|
|
11
|
+
"X",
|
|
12
|
+
"Y",
|
|
13
|
+
"LB",
|
|
14
|
+
"RB",
|
|
15
|
+
"LT",
|
|
16
|
+
"RT",
|
|
17
|
+
"Select",
|
|
18
|
+
"Start",
|
|
19
|
+
"LS",
|
|
20
|
+
"RS",
|
|
21
|
+
"DpadUp",
|
|
22
|
+
"DpadDown",
|
|
23
|
+
"DpadLeft",
|
|
24
|
+
"DpadRight",
|
|
25
|
+
"Home"
|
|
26
|
+
];
|
|
27
|
+
/**
|
|
28
|
+
* Standard-mapping gamepad stick axes, in axis-index order; Y is positive downwards.
|
|
29
|
+
*
|
|
30
|
+
* @category Input
|
|
31
|
+
*/
|
|
32
|
+
const GAMEPAD_AXES = [
|
|
33
|
+
"leftX",
|
|
34
|
+
"leftY",
|
|
35
|
+
"rightX",
|
|
36
|
+
"rightY"
|
|
37
|
+
];
|
|
38
|
+
/**
|
|
39
|
+
* The touch gestures recognized by {@link GestureRecognizer}.
|
|
40
|
+
*
|
|
41
|
+
* @category Input
|
|
42
|
+
*/
|
|
43
|
+
const GESTURES = [
|
|
44
|
+
"tap",
|
|
45
|
+
"doubleTap",
|
|
46
|
+
"longPress",
|
|
47
|
+
"swipeLeft",
|
|
48
|
+
"swipeRight",
|
|
49
|
+
"swipeUp",
|
|
50
|
+
"swipeDown",
|
|
51
|
+
"pinchIn",
|
|
52
|
+
"pinchOut"
|
|
53
|
+
];
|
|
54
|
+
/**
|
|
55
|
+
* WebXR "xr-standard" controller buttons, in button-index order; A/X and B/Y are the
|
|
56
|
+
* primary/secondary face buttons of each hand.
|
|
57
|
+
*
|
|
58
|
+
* @category Input
|
|
59
|
+
*/
|
|
60
|
+
const XR_BUTTONS = [
|
|
61
|
+
"trigger",
|
|
62
|
+
"squeeze",
|
|
63
|
+
"touchpad",
|
|
64
|
+
"thumbstick",
|
|
65
|
+
"primary",
|
|
66
|
+
"secondary"
|
|
67
|
+
];
|
|
68
|
+
/**
|
|
69
|
+
* WebXR "xr-standard" controller axes (touchpad, thumbstick), in axis-index order; Y is positive downwards.
|
|
70
|
+
*
|
|
71
|
+
* @category Input
|
|
72
|
+
*/
|
|
73
|
+
const XR_AXES = [
|
|
74
|
+
"padX",
|
|
75
|
+
"padY",
|
|
76
|
+
"thumbX",
|
|
77
|
+
"thumbY"
|
|
78
|
+
];
|
|
79
|
+
/**
|
|
80
|
+
* A physical key, by `KeyboardEvent.code` (layout independent: 'KeyW' is W on QWERTY, Z on AZERTY).
|
|
81
|
+
*
|
|
82
|
+
* @category Input
|
|
83
|
+
*/
|
|
84
|
+
const key = (code, modifiers) => modifiers ? {
|
|
85
|
+
type: "key",
|
|
86
|
+
code,
|
|
87
|
+
modifiers
|
|
88
|
+
} : {
|
|
89
|
+
type: "key",
|
|
90
|
+
code
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* A mouse (or pen) button.
|
|
94
|
+
*
|
|
95
|
+
* @category Input
|
|
96
|
+
*/
|
|
97
|
+
const mouse = (button) => ({
|
|
98
|
+
type: "mouse",
|
|
99
|
+
button
|
|
100
|
+
});
|
|
101
|
+
/**
|
|
102
|
+
* A wheel direction: pressed for one frame whenever the wheel moves that way.
|
|
103
|
+
*
|
|
104
|
+
* @category Input
|
|
105
|
+
*/
|
|
106
|
+
const wheel = (direction) => ({
|
|
107
|
+
type: "wheel",
|
|
108
|
+
direction
|
|
109
|
+
});
|
|
110
|
+
/**
|
|
111
|
+
* A gamepad button (standard mapping).
|
|
112
|
+
*
|
|
113
|
+
* @category Input
|
|
114
|
+
*/
|
|
115
|
+
const pad = (button) => ({
|
|
116
|
+
type: "pad",
|
|
117
|
+
button
|
|
118
|
+
});
|
|
119
|
+
/**
|
|
120
|
+
* One direction of a gamepad stick axis, e.g. padAxis('leftX', -1) for "left stick left".
|
|
121
|
+
*
|
|
122
|
+
* @category Input
|
|
123
|
+
*/
|
|
124
|
+
const padAxis = (axis, direction) => ({
|
|
125
|
+
type: "padAxis",
|
|
126
|
+
axis,
|
|
127
|
+
direction
|
|
128
|
+
});
|
|
129
|
+
/**
|
|
130
|
+
* A touch gesture.
|
|
131
|
+
*
|
|
132
|
+
* @category Input
|
|
133
|
+
*/
|
|
134
|
+
const gesture = (name) => ({
|
|
135
|
+
type: "gesture",
|
|
136
|
+
gesture: name
|
|
137
|
+
});
|
|
138
|
+
/**
|
|
139
|
+
* A VR controller button.
|
|
140
|
+
*
|
|
141
|
+
* @category Input
|
|
142
|
+
*/
|
|
143
|
+
const xr = (hand, button) => ({
|
|
144
|
+
type: "xr",
|
|
145
|
+
hand,
|
|
146
|
+
button
|
|
147
|
+
});
|
|
148
|
+
/**
|
|
149
|
+
* One direction of a VR controller axis.
|
|
150
|
+
*
|
|
151
|
+
* @category Input
|
|
152
|
+
*/
|
|
153
|
+
const xrAxis = (hand, axis, direction) => ({
|
|
154
|
+
type: "xrAxis",
|
|
155
|
+
hand,
|
|
156
|
+
axis,
|
|
157
|
+
direction
|
|
158
|
+
});
|
|
159
|
+
/**
|
|
160
|
+
* Identifies the physical control behind a binding (modifiers aside), e.g. `'key:KeyW'` or
|
|
161
|
+
* `'padAxis:leftX:-1'`.
|
|
162
|
+
*
|
|
163
|
+
* @category Input
|
|
164
|
+
*/
|
|
165
|
+
function controlId(binding) {
|
|
166
|
+
switch (binding.type) {
|
|
167
|
+
case "key": return `key:${binding.code}`;
|
|
168
|
+
case "mouse": return `mouse:${binding.button}`;
|
|
169
|
+
case "wheel": return `wheel:${binding.direction}`;
|
|
170
|
+
case "pad": return `pad:${binding.button}`;
|
|
171
|
+
case "padAxis": return `padAxis:${binding.axis}:${binding.direction}`;
|
|
172
|
+
case "gesture": return `gesture:${binding.gesture}`;
|
|
173
|
+
case "xr": return `xr:${binding.hand}:${binding.button}`;
|
|
174
|
+
case "xrAxis": return `xrAxis:${binding.hand}:${binding.axis}:${binding.direction}`;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Whether two bindings refer to the same control; key bindings must also require the same modifiers
|
|
179
|
+
* (a missing modifier counts as `false`).
|
|
180
|
+
*
|
|
181
|
+
* @category Input
|
|
182
|
+
*/
|
|
183
|
+
function bindingsEqual(a, b) {
|
|
184
|
+
if (controlId(a) !== controlId(b)) return false;
|
|
185
|
+
if (a.type !== "key" || b.type !== "key") return true;
|
|
186
|
+
const ma = a.modifiers ?? {};
|
|
187
|
+
const mb = b.modifiers ?? {};
|
|
188
|
+
return !!ma.shift === !!mb.shift && !!ma.ctrl === !!mb.ctrl && !!ma.alt === !!mb.alt && !!ma.meta === !!mb.meta;
|
|
189
|
+
}
|
|
190
|
+
const KEY_NAMES = {
|
|
191
|
+
Space: "Space",
|
|
192
|
+
Enter: "Enter",
|
|
193
|
+
NumpadEnter: "Numpad Enter",
|
|
194
|
+
Escape: "Esc",
|
|
195
|
+
Tab: "Tab",
|
|
196
|
+
Backspace: "Backspace",
|
|
197
|
+
ArrowUp: "↑",
|
|
198
|
+
ArrowDown: "↓",
|
|
199
|
+
ArrowLeft: "←",
|
|
200
|
+
ArrowRight: "→",
|
|
201
|
+
ShiftLeft: "Left Shift",
|
|
202
|
+
ShiftRight: "Right Shift",
|
|
203
|
+
ControlLeft: "Left Ctrl",
|
|
204
|
+
ControlRight: "Right Ctrl",
|
|
205
|
+
AltLeft: "Left Alt",
|
|
206
|
+
AltRight: "Right Alt",
|
|
207
|
+
MetaLeft: "Left Meta",
|
|
208
|
+
MetaRight: "Right Meta"
|
|
209
|
+
};
|
|
210
|
+
const AXIS_NAMES = {
|
|
211
|
+
leftX: ["Left Stick Left", "Left Stick Right"],
|
|
212
|
+
leftY: ["Left Stick Up", "Left Stick Down"],
|
|
213
|
+
rightX: ["Right Stick Left", "Right Stick Right"],
|
|
214
|
+
rightY: ["Right Stick Up", "Right Stick Down"],
|
|
215
|
+
padX: ["Touchpad Left", "Touchpad Right"],
|
|
216
|
+
padY: ["Touchpad Up", "Touchpad Down"],
|
|
217
|
+
thumbX: ["Thumbstick Left", "Thumbstick Right"],
|
|
218
|
+
thumbY: ["Thumbstick Up", "Thumbstick Down"]
|
|
219
|
+
};
|
|
220
|
+
const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
221
|
+
/**
|
|
222
|
+
* A readable name for a binding, e.g. for "Press Space to jump" prompts. Pass `keyLabels` (from
|
|
223
|
+
* {@link Input.loadKeyboardLayout}) to name keys as printed on the player's keyboard.
|
|
224
|
+
*
|
|
225
|
+
* @category Input
|
|
226
|
+
*/
|
|
227
|
+
function bindingLabel(binding, keyLabels) {
|
|
228
|
+
switch (binding.type) {
|
|
229
|
+
case "key": {
|
|
230
|
+
const m = binding.modifiers ?? {};
|
|
231
|
+
const name = keyLabels?.get(binding.code)?.toUpperCase() ?? KEY_NAMES[binding.code] ?? binding.code.replace(/^Key|^Digit/, "").replace(/^Numpad(.+)/, "Numpad $1");
|
|
232
|
+
return [
|
|
233
|
+
m.ctrl && "Ctrl",
|
|
234
|
+
m.alt && "Alt",
|
|
235
|
+
m.shift && "Shift",
|
|
236
|
+
m.meta && "Meta",
|
|
237
|
+
name
|
|
238
|
+
].filter(Boolean).join("+");
|
|
239
|
+
}
|
|
240
|
+
case "mouse": return `Mouse ${capitalize(binding.button)}`;
|
|
241
|
+
case "wheel": return `Wheel ${capitalize(binding.direction)}`;
|
|
242
|
+
case "pad": return `Gamepad ${binding.button}`;
|
|
243
|
+
case "padAxis": return AXIS_NAMES[binding.axis][binding.direction > 0 ? 1 : 0];
|
|
244
|
+
case "gesture": return capitalize(binding.gesture.replace(/([A-Z])/g, " $1").toLowerCase());
|
|
245
|
+
case "xr": return `${capitalize(binding.hand)} ${capitalize(binding.button)}`;
|
|
246
|
+
case "xrAxis": return `${capitalize(binding.hand)} ${AXIS_NAMES[binding.axis][binding.direction > 0 ? 1 : 0]}`;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
//#endregion
|
|
250
|
+
export { GAMEPAD_AXES, GAMEPAD_BUTTONS, GESTURES, XR_AXES, XR_BUTTONS, bindingLabel, bindingsEqual, controlId, gesture, key, mouse, pad, padAxis, wheel, xr, xrAxis };
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Gesture } from "./bindings.js";
|
|
2
|
+
//#region src/input/gestures.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* A finger currently on the screen, as reported by {@link GestureRecognizer.touches}.
|
|
5
|
+
*
|
|
6
|
+
* @property y current position, pixels
|
|
7
|
+
* @property startY where the finger went down, pixels
|
|
8
|
+
* @category Input
|
|
9
|
+
*/
|
|
10
|
+
export interface TouchPoint {
|
|
11
|
+
/** pointer id, from `PointerEvent.pointerId` */
|
|
12
|
+
id: number;
|
|
13
|
+
/** current position, pixels relative to the input target */
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
/** where the finger went down, pixels */
|
|
17
|
+
startX: number;
|
|
18
|
+
startY: number;
|
|
19
|
+
/** when the finger went down, ms */
|
|
20
|
+
startTime: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Tuning values for {@link GestureRecognizer}.
|
|
24
|
+
*
|
|
25
|
+
* @category Input
|
|
26
|
+
*/
|
|
27
|
+
export interface GestureThresholds {
|
|
28
|
+
/** ms, default 250 */
|
|
29
|
+
tapMaxDuration: number;
|
|
30
|
+
/** px; also how far a long press may drift. Default 10 */
|
|
31
|
+
tapMaxMovement: number;
|
|
32
|
+
/** ms between the two taps, default 300 */
|
|
33
|
+
doubleTapMaxDelay: number;
|
|
34
|
+
/** px between the two taps, default 30 */
|
|
35
|
+
doubleTapMaxDistance: number;
|
|
36
|
+
/** ms, default 500 */
|
|
37
|
+
longPressDuration: number;
|
|
38
|
+
/** px, default 50 */
|
|
39
|
+
swipeMinDistance: number;
|
|
40
|
+
/** ms, default 500 */
|
|
41
|
+
swipeMaxDuration: number;
|
|
42
|
+
/** relative scale change per frame that counts as pinching (0.005 = 0.5%, the default) */
|
|
43
|
+
pinchMinChange: number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* What {@link GestureRecognizer.update} recognized since the previous update.
|
|
47
|
+
*
|
|
48
|
+
* @category Input
|
|
49
|
+
*/
|
|
50
|
+
export interface GestureFrame {
|
|
51
|
+
/** recognized since the last update */
|
|
52
|
+
gestures: Gesture[];
|
|
53
|
+
/** two-finger scale change this frame (1 = none, > 1 = fingers apart) */
|
|
54
|
+
pinch: number;
|
|
55
|
+
/** two-finger movement this frame, px */
|
|
56
|
+
pan: {
|
|
57
|
+
/** px, positive = right */
|
|
58
|
+
x: number;
|
|
59
|
+
/** px, positive = down */
|
|
60
|
+
y: number;
|
|
61
|
+
};
|
|
62
|
+
/** two-finger rotation this frame, radians (clockwise on screen) */
|
|
63
|
+
rotate: number;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Turns touch points into gestures: tap, double tap, long press, swipes (one finger) and
|
|
67
|
+
* pinch / pan / rotate (two fingers). Feed it with down / move / up, call update() once per frame.
|
|
68
|
+
* Times are in ms and positions in pixels, from any consistent clock and origin. {@link Input} drives
|
|
69
|
+
* one for you; use this class directly only for custom touch handling.
|
|
70
|
+
*
|
|
71
|
+
* Tap, double tap and swipe are reported when the finger lifts, long press while it is still held;
|
|
72
|
+
* a finger that was ever down together with another one never produces one-finger gestures.
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* const recognizer = new GestureRecognizer({ longPressDuration: 400 });
|
|
76
|
+
* el.addEventListener('pointerdown', e => recognizer.down(e.pointerId, e.offsetX, e.offsetY, performance.now()));
|
|
77
|
+
* // ... move / up / cancel likewise
|
|
78
|
+
*
|
|
79
|
+
* const { gestures, pinch } = recognizer.update(performance.now());
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @category Input
|
|
83
|
+
*/
|
|
84
|
+
export declare class GestureRecognizer {
|
|
85
|
+
/** current thresholds; may be changed at any time */
|
|
86
|
+
readonly thresholds: GestureThresholds;
|
|
87
|
+
private readonly _touches;
|
|
88
|
+
private _pending;
|
|
89
|
+
private _lastTap?;
|
|
90
|
+
private _pair?;
|
|
91
|
+
/** Copies of the fingers currently down. */
|
|
92
|
+
get touches(): TouchPoint[];
|
|
93
|
+
/** @param thresholds overrides for the default thresholds */
|
|
94
|
+
constructor(thresholds?: Partial<GestureThresholds>);
|
|
95
|
+
/** A finger touched down. */
|
|
96
|
+
down(id: number, x: number, y: number, time: number): void;
|
|
97
|
+
/** A finger moved; unknown ids are ignored. */
|
|
98
|
+
move(id: number, x: number, y: number): void;
|
|
99
|
+
/** A finger lifted; may queue a tap, double tap or swipe for the next update. */
|
|
100
|
+
up(id: number, x: number, y: number, time: number): void;
|
|
101
|
+
/** Forgets a finger without recognizing anything (e.g. on `pointercancel`). */
|
|
102
|
+
cancel(id: number): void;
|
|
103
|
+
/** Forgets all fingers, gestures not yet reported, and the first tap of a pending double tap (e.g. on blur). */
|
|
104
|
+
clear(): void;
|
|
105
|
+
/** Returns what happened since the last update. `time` is the current time in ms. */
|
|
106
|
+
update(time: number): GestureFrame;
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
//#region src/input/gestures.ts
|
|
2
|
+
const DEFAULT_THRESHOLDS = {
|
|
3
|
+
tapMaxDuration: 250,
|
|
4
|
+
tapMaxMovement: 10,
|
|
5
|
+
doubleTapMaxDelay: 300,
|
|
6
|
+
doubleTapMaxDistance: 30,
|
|
7
|
+
longPressDuration: 500,
|
|
8
|
+
swipeMinDistance: 50,
|
|
9
|
+
swipeMaxDuration: 500,
|
|
10
|
+
pinchMinChange: .005
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Turns touch points into gestures: tap, double tap, long press, swipes (one finger) and
|
|
14
|
+
* pinch / pan / rotate (two fingers). Feed it with down / move / up, call update() once per frame.
|
|
15
|
+
* Times are in ms and positions in pixels, from any consistent clock and origin. {@link Input} drives
|
|
16
|
+
* one for you; use this class directly only for custom touch handling.
|
|
17
|
+
*
|
|
18
|
+
* Tap, double tap and swipe are reported when the finger lifts, long press while it is still held;
|
|
19
|
+
* a finger that was ever down together with another one never produces one-finger gestures.
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* const recognizer = new GestureRecognizer({ longPressDuration: 400 });
|
|
23
|
+
* el.addEventListener('pointerdown', e => recognizer.down(e.pointerId, e.offsetX, e.offsetY, performance.now()));
|
|
24
|
+
* // ... move / up / cancel likewise
|
|
25
|
+
*
|
|
26
|
+
* const { gestures, pinch } = recognizer.update(performance.now());
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @category Input
|
|
30
|
+
*/
|
|
31
|
+
var GestureRecognizer = class {
|
|
32
|
+
/** current thresholds; may be changed at any time */
|
|
33
|
+
thresholds = { ...DEFAULT_THRESHOLDS };
|
|
34
|
+
_touches = /* @__PURE__ */ new Map();
|
|
35
|
+
_pending = [];
|
|
36
|
+
_lastTap;
|
|
37
|
+
_pair;
|
|
38
|
+
/** Copies of the fingers currently down. */
|
|
39
|
+
get touches() {
|
|
40
|
+
return [...this._touches.values()].map(({ id, x, y, startX, startY, startTime }) => ({
|
|
41
|
+
id,
|
|
42
|
+
x,
|
|
43
|
+
y,
|
|
44
|
+
startX,
|
|
45
|
+
startY,
|
|
46
|
+
startTime
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
/** @param thresholds overrides for the default thresholds */
|
|
50
|
+
constructor(thresholds = {}) {
|
|
51
|
+
Object.assign(this.thresholds, thresholds);
|
|
52
|
+
}
|
|
53
|
+
/** A finger touched down. */
|
|
54
|
+
down(id, x, y, time) {
|
|
55
|
+
const multi = this._touches.size > 0;
|
|
56
|
+
for (const touch of this._touches.values()) touch.multi = true;
|
|
57
|
+
this._touches.set(id, {
|
|
58
|
+
id,
|
|
59
|
+
x,
|
|
60
|
+
y,
|
|
61
|
+
startX: x,
|
|
62
|
+
startY: y,
|
|
63
|
+
startTime: time,
|
|
64
|
+
maxMovement: 0,
|
|
65
|
+
multi,
|
|
66
|
+
longPressed: false
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/** A finger moved; unknown ids are ignored. */
|
|
70
|
+
move(id, x, y) {
|
|
71
|
+
const touch = this._touches.get(id);
|
|
72
|
+
if (!touch) return;
|
|
73
|
+
touch.x = x;
|
|
74
|
+
touch.y = y;
|
|
75
|
+
touch.maxMovement = Math.max(touch.maxMovement, Math.hypot(x - touch.startX, y - touch.startY));
|
|
76
|
+
}
|
|
77
|
+
/** A finger lifted; may queue a tap, double tap or swipe for the next update. */
|
|
78
|
+
up(id, x, y, time) {
|
|
79
|
+
const touch = this._touches.get(id);
|
|
80
|
+
if (!touch) return;
|
|
81
|
+
this.move(id, x, y);
|
|
82
|
+
this._touches.delete(id);
|
|
83
|
+
if (touch.multi || touch.longPressed) return;
|
|
84
|
+
const t = this.thresholds;
|
|
85
|
+
const duration = time - touch.startTime;
|
|
86
|
+
const dx = x - touch.startX;
|
|
87
|
+
const dy = y - touch.startY;
|
|
88
|
+
const distance = Math.hypot(dx, dy);
|
|
89
|
+
if (duration <= t.tapMaxDuration && touch.maxMovement <= t.tapMaxMovement) {
|
|
90
|
+
this._pending.push("tap");
|
|
91
|
+
const last = this._lastTap;
|
|
92
|
+
if (last && time - last.time <= t.doubleTapMaxDelay && Math.hypot(x - last.x, y - last.y) <= t.doubleTapMaxDistance) {
|
|
93
|
+
this._pending.push("doubleTap");
|
|
94
|
+
this._lastTap = void 0;
|
|
95
|
+
} else this._lastTap = {
|
|
96
|
+
x,
|
|
97
|
+
y,
|
|
98
|
+
time
|
|
99
|
+
};
|
|
100
|
+
} else if (distance >= t.swipeMinDistance && duration <= t.swipeMaxDuration) this._pending.push(Math.abs(dx) >= Math.abs(dy) ? dx > 0 ? "swipeRight" : "swipeLeft" : dy > 0 ? "swipeDown" : "swipeUp");
|
|
101
|
+
}
|
|
102
|
+
/** Forgets a finger without recognizing anything (e.g. on `pointercancel`). */
|
|
103
|
+
cancel(id) {
|
|
104
|
+
this._touches.delete(id);
|
|
105
|
+
}
|
|
106
|
+
/** Forgets all fingers, gestures not yet reported, and the first tap of a pending double tap (e.g. on blur). */
|
|
107
|
+
clear() {
|
|
108
|
+
this._touches.clear();
|
|
109
|
+
this._pair = void 0;
|
|
110
|
+
this._pending = [];
|
|
111
|
+
this._lastTap = void 0;
|
|
112
|
+
}
|
|
113
|
+
/** Returns what happened since the last update. `time` is the current time in ms. */
|
|
114
|
+
update(time) {
|
|
115
|
+
const frame = {
|
|
116
|
+
gestures: [],
|
|
117
|
+
pinch: 1,
|
|
118
|
+
pan: {
|
|
119
|
+
x: 0,
|
|
120
|
+
y: 0
|
|
121
|
+
},
|
|
122
|
+
rotate: 0
|
|
123
|
+
};
|
|
124
|
+
for (const touch of this._touches.values()) if (!touch.multi && !touch.longPressed && touch.maxMovement <= this.thresholds.tapMaxMovement && time - touch.startTime >= this.thresholds.longPressDuration) {
|
|
125
|
+
touch.longPressed = true;
|
|
126
|
+
this._pending.push("longPress");
|
|
127
|
+
}
|
|
128
|
+
const [a, b] = [...this._touches.values()];
|
|
129
|
+
if (a && b) {
|
|
130
|
+
const distance = Math.hypot(b.x - a.x, b.y - a.y);
|
|
131
|
+
const angle = Math.atan2(b.y - a.y, b.x - a.x);
|
|
132
|
+
const cx = (a.x + b.x) / 2;
|
|
133
|
+
const cy = (a.y + b.y) / 2;
|
|
134
|
+
const pair = this._pair;
|
|
135
|
+
if (pair && pair.a === a.id && pair.b === b.id) {
|
|
136
|
+
frame.pinch = pair.distance > 0 ? distance / pair.distance : 1;
|
|
137
|
+
frame.pan = {
|
|
138
|
+
x: cx - pair.cx,
|
|
139
|
+
y: cy - pair.cy
|
|
140
|
+
};
|
|
141
|
+
frame.rotate = Math.atan2(Math.sin(angle - pair.angle), Math.cos(angle - pair.angle));
|
|
142
|
+
if (Math.abs(frame.pinch - 1) >= this.thresholds.pinchMinChange) this._pending.push(frame.pinch > 1 ? "pinchOut" : "pinchIn");
|
|
143
|
+
}
|
|
144
|
+
this._pair = {
|
|
145
|
+
a: a.id,
|
|
146
|
+
b: b.id,
|
|
147
|
+
distance,
|
|
148
|
+
angle,
|
|
149
|
+
cx,
|
|
150
|
+
cy
|
|
151
|
+
};
|
|
152
|
+
} else this._pair = void 0;
|
|
153
|
+
frame.gestures = this._pending;
|
|
154
|
+
this._pending = [];
|
|
155
|
+
return frame;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
//#endregion
|
|
159
|
+
export { GestureRecognizer };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Binding, GAMEPAD_AXES, GAMEPAD_BUTTONS, GESTURES, GamepadAxis, GamepadButton, Gesture, GestureBinding, KeyBinding, KeyModifiers, MouseBinding, MouseButton, PadAxisBinding, PadBinding, WheelBinding, WheelDirection, XRAxis, XRAxisBinding, XRBinding, XRButton, XRHand, XR_AXES, XR_BUTTONS, bindingLabel, bindingsEqual, controlId, gesture, key, mouse, pad, padAxis, wheel, xr, xrAxis } from "./bindings.js";
|
|
2
|
+
import { Action, ActionMap, ActionMapJSON, DEFAULT_DEADZONE } from "./action-map.js";
|
|
3
|
+
import { GestureFrame, GestureRecognizer, GestureThresholds, TouchPoint } from "./gestures.js";
|
|
4
|
+
import { ActionPhase, CaptureOptions, DeviceQuery, GamepadInfo, Input, InputDevice, InputDeviceType, InputOptions, PointerState, XRSessionLike } from "./input.js";
|
|
5
|
+
export { Action, ActionMap, ActionMapJSON, ActionPhase, Binding, CaptureOptions, DEFAULT_DEADZONE, DeviceQuery, GAMEPAD_AXES, GAMEPAD_BUTTONS, GESTURES, GamepadAxis, GamepadButton, GamepadInfo, Gesture, GestureBinding, GestureFrame, GestureRecognizer, GestureThresholds, Input, InputDevice, InputDeviceType, InputOptions, KeyBinding, KeyModifiers, MouseBinding, MouseButton, PadAxisBinding, PadBinding, PointerState, TouchPoint, WheelBinding, WheelDirection, XRAxis, XRAxisBinding, XRBinding, XRButton, XRHand, XRSessionLike, XR_AXES, XR_BUTTONS, bindingLabel, bindingsEqual, controlId, gesture, key, mouse, pad, padAxis, wheel, xr, xrAxis };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { GAMEPAD_AXES, GAMEPAD_BUTTONS, GESTURES, XR_AXES, XR_BUTTONS, bindingLabel, bindingsEqual, controlId, gesture, key, mouse, pad, padAxis, wheel, xr, xrAxis } from "./bindings.js";
|
|
2
|
+
import { ActionMap, DEFAULT_DEADZONE } from "./action-map.js";
|
|
3
|
+
import { GestureRecognizer } from "./gestures.js";
|
|
4
|
+
import { Input } from "./input.js";
|
|
5
|
+
export { ActionMap, DEFAULT_DEADZONE, GAMEPAD_AXES, GAMEPAD_BUTTONS, GESTURES, GestureRecognizer, Input, XR_AXES, XR_BUTTONS, bindingLabel, bindingsEqual, controlId, gesture, key, mouse, pad, padAxis, wheel, xr, xrAxis };
|