@n3rdw1z4rd/core 0.19.0 → 0.20.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 +27 -10
- package/dist/canvas-renderer.d.ts +3 -0
- package/dist/canvas-renderer.js +7 -0
- package/dist/index.cjs +449 -155
- package/dist/index.d.cts +233 -49
- package/dist/index.d.ts +5 -4
- package/dist/index.js +4 -3
- package/dist/input/_input.d.ts +25 -0
- package/dist/input/_input.js +97 -0
- package/dist/input/action-map.d.ts +42 -7
- package/dist/input/action-map.js +44 -18
- package/dist/input/bindings.d.ts +1 -1
- package/dist/input/bindings.js +1 -1
- package/dist/input/gestures.d.ts +20 -2
- package/dist/input/gestures.js +34 -8
- package/dist/input/index.d.ts +5 -4
- package/dist/input/index.js +3 -2
- package/dist/input/input-action-map.d.ts +327 -0
- package/dist/input/input-action-map.js +689 -0
- package/dist/input/input.d.ts +77 -272
- package/dist/input/input.js +201 -653
- package/package.json +1 -1
package/dist/input/input.d.ts
CHANGED
|
@@ -1,297 +1,102 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { GestureFrame, GestureRecognizer, TouchPoint } from "./gestures.js";
|
|
4
|
-
import { Observable } from "../observable.js";
|
|
1
|
+
import { Vector } from "../vector.js";
|
|
2
|
+
import { Emitter } from "../emitter.js";
|
|
5
3
|
//#region src/input/input.d.ts
|
|
6
4
|
/**
|
|
7
|
-
*
|
|
5
|
+
* Whether a key or button is down (`state` 1) or up (0), and when that last changed.
|
|
8
6
|
*
|
|
9
7
|
* @category Input
|
|
10
8
|
*/
|
|
11
|
-
export
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
*/
|
|
17
|
-
export type InputDeviceType = 'keyboard' | 'mouse' | 'pen' | 'touch' | 'gamepad' | 'xr';
|
|
18
|
-
/**
|
|
19
|
-
* When an {@link Input.onAction} callback fires: as the action becomes pressed, or released.
|
|
20
|
-
*
|
|
21
|
-
* @category Input
|
|
22
|
-
*/
|
|
23
|
-
export type ActionPhase = 'pressed' | 'released';
|
|
24
|
-
/**
|
|
25
|
-
* Options for the {@link Input} constructor.
|
|
26
|
-
*
|
|
27
|
-
* @category Input
|
|
28
|
-
*/
|
|
29
|
-
export interface InputOptions {
|
|
30
|
-
/** where key events are read; default: window */
|
|
31
|
-
keyboardTarget?: EventTarget;
|
|
32
|
-
/** add the ui_* actions. Default true. */
|
|
33
|
-
defaults?: boolean;
|
|
34
|
-
/** stop bound keys / the wheel from scrolling the page. Default true. */
|
|
35
|
-
preventDefault?: boolean;
|
|
36
|
-
/** no right-click menu on the target. Default true. */
|
|
37
|
-
preventContextMenu?: boolean;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Restricts an action query to one {@link InputDevice}; without it, any device counts.
|
|
41
|
-
*
|
|
42
|
-
* @category Input
|
|
43
|
-
*/
|
|
44
|
-
export interface DeviceQuery {
|
|
45
|
-
/** only this device; default: any */
|
|
46
|
-
device?: InputDevice;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* A connected gamepad, see {@link Input.gamepads}.
|
|
50
|
-
*
|
|
51
|
-
* @category Input
|
|
52
|
-
*/
|
|
53
|
-
export interface GamepadInfo {
|
|
54
|
-
/** gamepad.index + 1 */
|
|
55
|
-
device: InputDevice;
|
|
56
|
-
/** the browser's description of the controller */
|
|
57
|
-
id: string;
|
|
58
|
-
/** uses the standard mapping (button names are reliable); others map by index */
|
|
59
|
-
standard: boolean;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* The parts of a WebXR XRSession that Input reads (structural, so no WebXR typings are required).
|
|
63
|
-
*
|
|
64
|
-
* @category Input
|
|
65
|
-
*/
|
|
66
|
-
export interface XRSessionLike extends EventTarget {
|
|
67
|
-
/** the session's controllers */
|
|
68
|
-
readonly inputSources: Iterable<{
|
|
69
|
-
/** `'left'` or `'right'`; other values are ignored */
|
|
70
|
-
readonly handedness: string;
|
|
71
|
-
/** buttons in {@link XR_BUTTONS} order (value 0..1) and axes in {@link XR_AXES} order (-1..1) */
|
|
72
|
-
readonly gamepad?: {
|
|
73
|
-
/** in {@link XR_BUTTONS} order */
|
|
74
|
-
readonly buttons: readonly {
|
|
75
|
-
/** held down */
|
|
76
|
-
pressed: boolean;
|
|
77
|
-
/** 0..1 (analog for triggers and squeeze) */
|
|
78
|
-
value: number;
|
|
79
|
-
}[];
|
|
80
|
-
/** in {@link XR_AXES} order, -1..1 */
|
|
81
|
-
readonly axes: readonly number[];
|
|
82
|
-
} | null;
|
|
83
|
-
}>;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Mouse or pen state, see {@link Input.pointer}. Positions update on pointer events; deltas and
|
|
87
|
-
* wheel totals on each {@link Input.update}.
|
|
88
|
-
*
|
|
89
|
-
* @property y pixels, relative to the target's top-left, y down
|
|
90
|
-
* @property ndcY -1..1, y up
|
|
91
|
-
* @property deltaY movement during the last frame, pixels
|
|
92
|
-
* @property wheelY wheel movement during the last frame, in pixels (positive = down)
|
|
93
|
-
* @property tiltY pen: degrees
|
|
94
|
-
*
|
|
95
|
-
* @category Input
|
|
96
|
-
*/
|
|
97
|
-
export interface PointerState {
|
|
98
|
-
/** the kind of pointer that last moved or pressed */
|
|
99
|
-
type: 'mouse' | 'pen';
|
|
100
|
-
/** pixels, relative to the target's top-left */
|
|
101
|
-
x: number;
|
|
102
|
-
y: number;
|
|
103
|
-
/** -1..1, y up (for raycasting) */
|
|
104
|
-
ndcX: number;
|
|
105
|
-
ndcY: number;
|
|
106
|
-
/** movement during the last frame, pixels (works under pointer lock) */
|
|
107
|
-
deltaX: number;
|
|
108
|
-
deltaY: number;
|
|
109
|
-
/** wheel movement during the last frame, in pixels */
|
|
110
|
-
wheelX: number;
|
|
111
|
-
wheelY: number;
|
|
112
|
-
/** pen: 0..1 */
|
|
113
|
-
pressure: number;
|
|
114
|
-
/** pen: degrees */
|
|
115
|
-
tiltX: number;
|
|
116
|
-
tiltY: number;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Options for {@link Input.captureBinding}.
|
|
120
|
-
*
|
|
121
|
-
* @category Input
|
|
122
|
-
*/
|
|
123
|
-
export interface CaptureOptions {
|
|
124
|
-
/** KeyboardEvent.code that cancels (resolves null); `null` for none. Default 'Escape'. */
|
|
125
|
-
cancelKey?: string | null;
|
|
126
|
-
/** only accept these kinds of input; others are ignored and still reach actions */
|
|
127
|
-
types?: Binding['type'][];
|
|
9
|
+
export interface InputState {
|
|
10
|
+
/** 1 = down, 0 = up */
|
|
11
|
+
state: number;
|
|
12
|
+
/** `Event.timeStamp` of the last change, ms */
|
|
13
|
+
timeStamp: number;
|
|
128
14
|
}
|
|
129
15
|
/**
|
|
130
|
-
*
|
|
131
|
-
*
|
|
16
|
+
* Event-based input for keyboard, mouse, pen and touch: subscribe with `on()` and query state
|
|
17
|
+
* at any time; no frame loop needed. One shared instance, {@link Input.instance}.
|
|
132
18
|
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
19
|
+
* Events (listeners get the DOM event):
|
|
20
|
+
* - keys, by lowercased `KeyboardEvent.code`: `key_down`, `key_up`, `key_repeat`, `key_pressed`
|
|
21
|
+
* (released within `inputThreshold` ms), and per key `keyw_down`, `keyw_up`, `keyw_pressed`, ...
|
|
22
|
+
* - buttons (mouse buttons; the primary finger or pen is button 0): `button_down`, `button_up`,
|
|
23
|
+
* `button_clicked`, and per button `button0_down`, `button0_up`, `button0_clicked`, ...
|
|
24
|
+
* No click after a drag.
|
|
25
|
+
* - pointer: `pointer_move`; `drag_start`, `drag`, `drag_end` (moved `dragThreshold` px with a
|
|
26
|
+
* button down); `mouse_wheel`; `contextmenu` (see `disableContextMenu()`)
|
|
27
|
+
* - every finger: `touch_down`, `touch_move`, `touch_up`, `touch_cancel`
|
|
28
|
+
* - `blur`: the window lost focus or the tab was hidden; every key and button is released
|
|
135
29
|
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* const input = Input.instance;
|
|
32
|
+
* input.target = canvas;
|
|
139
33
|
*
|
|
140
|
-
*
|
|
34
|
+
* input.on('keyw_down', 'arrowup_down', () => move(0, -1));
|
|
35
|
+
* input.on('button0_clicked', () => select(input.pointerPosition));
|
|
36
|
+
* input.on('pointer_move', () => {
|
|
37
|
+
* if (input.isButtonDown(0)) pan(input.pointerDelta.x, input.pointerDelta.y);
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
141
40
|
*
|
|
142
41
|
* @category Input
|
|
143
42
|
*/
|
|
144
|
-
export declare class Input {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
readonly
|
|
157
|
-
/**
|
|
158
|
-
readonly
|
|
159
|
-
/**
|
|
160
|
-
readonly
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
private _keyLabels?;
|
|
166
|
-
private readonly _target?;
|
|
167
|
-
private readonly _options;
|
|
168
|
-
private readonly _listeners;
|
|
169
|
-
private readonly _controls;
|
|
170
|
-
private readonly _actionStates;
|
|
171
|
-
private readonly _actionListeners;
|
|
172
|
-
private readonly _warned;
|
|
173
|
-
private readonly _pending;
|
|
174
|
-
private _capture?;
|
|
175
|
-
/** The kind of device that produced the most recent input (initially `'keyboard'`). */
|
|
176
|
-
get lastDevice(): InputDeviceType;
|
|
177
|
-
/** Whether the pointer is locked to the target, see {@link Input.lockPointer}. */
|
|
178
|
-
get isPointerLocked(): boolean;
|
|
179
|
-
/**
|
|
180
|
-
* @param target element that receives pointer, wheel and touch input (usually the canvas)
|
|
181
|
-
*/
|
|
182
|
-
constructor(target?: EventTarget, options?: InputOptions);
|
|
183
|
-
/**
|
|
184
|
-
* Call once per frame, before reading input: polls devices and advances "just pressed" /
|
|
185
|
-
* "just released" by one frame. Action callbacks fire from here.
|
|
186
|
-
*/
|
|
187
|
-
update(): void;
|
|
188
|
-
/** Whether the action is held past its deadzone. Unknown actions return false (and warn once). */
|
|
189
|
-
isActionPressed(action: string, query?: DeviceQuery): boolean;
|
|
190
|
-
/** Whether the action became pressed in the last update(). */
|
|
191
|
-
isActionJustPressed(action: string, query?: DeviceQuery): boolean;
|
|
192
|
-
/** Whether the action was released in the last update(). */
|
|
193
|
-
isActionJustReleased(action: string, query?: DeviceQuery): boolean;
|
|
194
|
-
/** 0..1, rescaled past the action's deadzone (analog triggers and sticks give values in between). */
|
|
195
|
-
getActionStrength(action: string, query?: DeviceQuery): number;
|
|
196
|
-
/** 0..1 before the deadzone is applied. */
|
|
197
|
-
getActionRawStrength(action: string, query?: DeviceQuery): number;
|
|
198
|
-
/** -1..1: `positive` minus `negative`. */
|
|
199
|
-
getAxis(negative: string, positive: string, query?: DeviceQuery): number;
|
|
200
|
-
/**
|
|
201
|
-
* A 2D direction from four actions, with a circular deadzone and length at most 1 (diagonals
|
|
202
|
-
* aren't faster). Y is positive towards `positiveY`, as in Godot's get_vector("left", "right", "up", "down").
|
|
203
|
-
*
|
|
204
|
-
* @param options.deadzone default: the average of the four actions' deadzones
|
|
205
|
-
* @returns `x` and `y` in -1..1; `{ x: 0, y: 0 }` inside the deadzone
|
|
206
|
-
*/
|
|
207
|
-
getVector(negativeX: string, positiveX: string, negativeY: string, positiveY: string, options?: DeviceQuery & {
|
|
208
|
-
deadzone?: number;
|
|
209
|
-
}): {
|
|
210
|
-
/** -1..1, positive towards `positiveX` */
|
|
211
|
-
x: number;
|
|
212
|
-
/** -1..1, positive towards `positiveY` */
|
|
213
|
-
y: number;
|
|
214
|
-
};
|
|
215
|
-
/** Calls `callback` when the action is pressed or released (during update()). Returns an unsubscribe function. */
|
|
216
|
-
onAction(action: string, phase: ActionPhase, callback: () => void, query?: DeviceQuery): () => void;
|
|
217
|
-
/** Whether a key (by `KeyboardEvent.code`) is held right now, regardless of actions. */
|
|
218
|
-
isKeyPressed(code: string): boolean;
|
|
219
|
-
/** Whether a mouse or pen button is held right now, regardless of actions. */
|
|
220
|
-
isMouseButtonPressed(button: MouseButton): boolean;
|
|
221
|
-
/**
|
|
222
|
-
* Releases every held key and button, on one device or all. Keyboard, mouse and touch (device 0)
|
|
223
|
-
* are released automatically when the window loses focus, since their release events get lost.
|
|
224
|
-
*/
|
|
225
|
-
releaseAll(device?: InputDevice): void;
|
|
226
|
-
/** Requests pointer lock on the target (browsers only allow it from a user gesture, e.g. a click). */
|
|
227
|
-
lockPointer(): void;
|
|
228
|
-
/** Exits pointer lock if the target holds it. */
|
|
229
|
-
unlockPointer(): void;
|
|
230
|
-
/**
|
|
231
|
-
* Waits for the player's next input and resolves with a binding for it, for rebinding menus.
|
|
232
|
-
* Resolves `null` if the cancel key is pressed or another capture starts. The captured press
|
|
233
|
-
* doesn't trigger any actions.
|
|
234
|
-
*/
|
|
235
|
-
captureBinding(options?: CaptureOptions): Promise<Binding | null>;
|
|
236
|
-
/** Ends a pending {@link Input.captureBinding}, resolving it with `null`. */
|
|
237
|
-
cancelCapture(): void;
|
|
43
|
+
export declare class Input extends Emitter {
|
|
44
|
+
private static _instance?;
|
|
45
|
+
/** the shared instance, created on first use */
|
|
46
|
+
static get instance(): Input;
|
|
47
|
+
private _state;
|
|
48
|
+
private _windowListeners;
|
|
49
|
+
private _targetListeners;
|
|
50
|
+
private _target;
|
|
51
|
+
private _contextMenuDisabled;
|
|
52
|
+
private _dragStart?;
|
|
53
|
+
private _dragging;
|
|
54
|
+
/** pixels from the target's top-left */
|
|
55
|
+
readonly pointerPosition: Vector;
|
|
56
|
+
/** -1..1 across the target, y up */
|
|
57
|
+
readonly pointerPositionNdc: Vector;
|
|
58
|
+
/** pixels moved by the last pointer event (mouse, pen and touch alike) */
|
|
59
|
+
readonly pointerDelta: Vector;
|
|
60
|
+
/** ms; a key or button released within this is also `_pressed` / `_clicked` */
|
|
61
|
+
inputThreshold: number;
|
|
62
|
+
/** px; moving further than this with a button down starts a drag */
|
|
63
|
+
dragThreshold: number;
|
|
238
64
|
/**
|
|
239
|
-
*
|
|
240
|
-
*
|
|
65
|
+
* Where pointer, wheel and context-menu events are read and positions are measured, default
|
|
66
|
+
* `window`; usually the canvas. Keys are always read from `window`.
|
|
241
67
|
*/
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
|
|
245
|
-
/**
|
|
68
|
+
get target(): EventTarget;
|
|
69
|
+
/** moves the pointer, wheel and context-menu listeners to `target` */
|
|
70
|
+
set target(target: EventTarget);
|
|
71
|
+
/** a drag is in progress */
|
|
72
|
+
get isDragging(): boolean;
|
|
73
|
+
private constructor();
|
|
74
|
+
/** Stops the right-click menu on the target; `contextmenu` is still emitted. */
|
|
75
|
+
disableContextMenu(): void;
|
|
76
|
+
/** Removes every DOM listener; the next {@link Input.instance} is a new one. */
|
|
246
77
|
dispose(): void;
|
|
78
|
+
private _listenToTarget;
|
|
247
79
|
private _listen;
|
|
248
|
-
private
|
|
249
|
-
private
|
|
250
|
-
private
|
|
251
|
-
private
|
|
252
|
-
private
|
|
253
|
-
/** Updates a held control (key, button, axis direction: 0..1); `binding` describes it for captureBinding(). */
|
|
254
|
-
protected _setControl(device: InputDevice, binding: Binding, value: number): void;
|
|
255
|
-
/** A one-frame press with no held state: wheel steps, gestures. */
|
|
256
|
-
protected _pulse(device: InputDevice, binding: Binding): void;
|
|
257
|
-
private _tryCapture;
|
|
258
|
-
private _setLastDevice;
|
|
259
|
-
/**
|
|
260
|
-
* Vibrates a gamepad (where supported). Magnitudes are 0..1; `strong` defaults to 1, `weak` to
|
|
261
|
-
* `strong`, `duration` to 200 ms.
|
|
262
|
-
*/
|
|
263
|
-
rumble(device: InputDevice, options?: {
|
|
264
|
-
strong?: number;
|
|
265
|
-
weak?: number;
|
|
266
|
-
duration?: number;
|
|
267
|
-
}): void;
|
|
268
|
-
private _pollGamepads;
|
|
269
|
-
private _updateGestures;
|
|
270
|
-
private _xrSession?;
|
|
271
|
-
private _xrEnd?;
|
|
272
|
-
private readonly _xrHeld;
|
|
273
|
-
/** The session set by {@link Input.attachXR}, until detached or ended. */
|
|
274
|
-
get xrSession(): XRSessionLike | undefined;
|
|
275
|
-
/**
|
|
276
|
-
* Reads VR controller buttons and sticks from a WebXR session (e.g. `renderer.xr.getSession()`)
|
|
277
|
-
* into the action map. Controller poses stay with three.js (`renderer.xr.getController(i)`).
|
|
278
|
-
* XR controller state only updates during XR frames, so call update() from the XR animation
|
|
279
|
-
* loop (`renderer.setAnimationLoop`). Detaches automatically when the session ends.
|
|
280
|
-
*/
|
|
281
|
-
attachXR(session: XRSessionLike): void;
|
|
282
|
-
/** Stops reading the XR session and releases its controls. */
|
|
283
|
-
detachXR(): void;
|
|
284
|
-
private _pollXR;
|
|
285
|
-
private _releaseXR;
|
|
286
|
-
private _isBound;
|
|
80
|
+
private _down;
|
|
81
|
+
private _up;
|
|
82
|
+
private _anyButtonDown;
|
|
83
|
+
private _updatePointer;
|
|
84
|
+
private _onContextMenu;
|
|
287
85
|
private _onKeyDown;
|
|
288
86
|
private _onKeyUp;
|
|
289
|
-
private _updatePointer;
|
|
290
|
-
private _local;
|
|
291
87
|
private _onPointerDown;
|
|
292
88
|
private _onPointerUp;
|
|
293
|
-
private _onPointerCancel;
|
|
294
89
|
private _onPointerMove;
|
|
90
|
+
private _endDrag;
|
|
295
91
|
private _onWheel;
|
|
92
|
+
private _onBlur;
|
|
93
|
+
/** Whether a key (by `KeyboardEvent.code`, e.g. 'KeyW') is down. */
|
|
94
|
+
isKeyDown(keyCode: string): boolean;
|
|
95
|
+
/** 1 if the key is down, else 0. */
|
|
96
|
+
getKeyState(keyCode: string): number;
|
|
97
|
+
/** Whether a button (`PointerEvent.button`: 0 left / primary finger, 1 middle, 2 right, ...) is down. */
|
|
98
|
+
isButtonDown(buttonNumber: number): boolean;
|
|
99
|
+
/** 1 if the button is down, else 0. */
|
|
100
|
+
getButtonState(buttonNumber: number): number;
|
|
296
101
|
}
|
|
297
102
|
//#endregion
|