@n3rdw1z4rd/core 0.19.1 → 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.
@@ -0,0 +1,327 @@
1
+ import { Binding, MouseButton } from "./bindings.js";
2
+ import { ActionMap } from "./action-map.js";
3
+ import { GestureFrame, GestureRecognizer, TouchPoint } from "./gestures.js";
4
+ import { Input } from "./input.js";
5
+ import { Observable } from "../observable.js";
6
+ //#region src/input/input-action-map.d.ts
7
+ /**
8
+ * 0 = keyboard, mouse, pen, touch and VR; 1, 2, ... = gamepads.
9
+ *
10
+ * @category Input
11
+ */
12
+ export type InputDevice = number;
13
+ /**
14
+ * The kind of device the player last used, see {@link InputActionMap.lastDevice}.
15
+ *
16
+ * @category Input
17
+ */
18
+ export type InputDeviceType = 'keyboard' | 'mouse' | 'pen' | 'touch' | 'gamepad' | 'xr';
19
+ /**
20
+ * When an {@link InputActionMap.onAction} callback fires: as the action becomes pressed, or released.
21
+ *
22
+ * @category Input
23
+ */
24
+ export type ActionPhase = 'pressed' | 'released';
25
+ /**
26
+ * Passed to {@link InputActionMap.onAction} callbacks.
27
+ *
28
+ * @category Input
29
+ */
30
+ export interface ActionEvent {
31
+ /** the action's name */
32
+ action: string;
33
+ /** an auto-repeat of a held action (see {@link ActionRepeat}), not a new press */
34
+ repeat: boolean;
35
+ }
36
+ /**
37
+ * Options for the {@link InputActionMap} constructor.
38
+ *
39
+ * @category Input
40
+ */
41
+ export interface InputActionMapOptions {
42
+ /** where events come from, default {@link Input.instance}; set `input.target` to the canvas */
43
+ input?: Input;
44
+ /** add the ui_* actions. Default true. */
45
+ defaults?: boolean;
46
+ /** stop bound keys / the wheel from scrolling the page. Default true. */
47
+ preventDefault?: boolean;
48
+ /** no right-click menu on the target. Default true. */
49
+ preventContextMenu?: boolean;
50
+ }
51
+ /**
52
+ * Restricts an action query to one {@link InputDevice}; without it, any device counts.
53
+ *
54
+ * @category Input
55
+ */
56
+ export interface DeviceQuery {
57
+ /** only this device; default: any */
58
+ device?: InputDevice;
59
+ }
60
+ /**
61
+ * A connected gamepad, see {@link InputActionMap.gamepads}.
62
+ *
63
+ * @category Input
64
+ */
65
+ export interface GamepadInfo {
66
+ /** gamepad.index + 1 */
67
+ device: InputDevice;
68
+ /** the browser's description of the controller */
69
+ id: string;
70
+ /** uses the standard mapping (button names are reliable); others map by index */
71
+ standard: boolean;
72
+ }
73
+ /**
74
+ * The parts of a WebXR XRSession that InputActionMap reads (structural, so no WebXR typings are required).
75
+ *
76
+ * @category Input
77
+ */
78
+ export interface XRSessionLike extends EventTarget {
79
+ /** the session's controllers */
80
+ readonly inputSources: Iterable<{
81
+ /** `'left'` or `'right'`; other values are ignored */
82
+ readonly handedness: string;
83
+ /** buttons in {@link XR_BUTTONS} order (value 0..1) and axes in {@link XR_AXES} order (-1..1) */
84
+ readonly gamepad?: {
85
+ /** in {@link XR_BUTTONS} order */
86
+ readonly buttons: readonly {
87
+ /** held down */
88
+ pressed: boolean;
89
+ /** 0..1 (analog for triggers and squeeze) */
90
+ value: number;
91
+ }[];
92
+ /** in {@link XR_AXES} order, -1..1 */
93
+ readonly axes: readonly number[];
94
+ } | null;
95
+ }>;
96
+ }
97
+ /**
98
+ * Mouse or pen state, see {@link InputActionMap.pointer}. Positions update on pointer events; deltas and
99
+ * wheel totals on each {@link InputActionMap.update}.
100
+ *
101
+ * @property y pixels, relative to the target's top-left, y down
102
+ * @property ndcY -1..1, y up
103
+ * @property deltaY movement during the last frame, pixels
104
+ * @property wheelY wheel movement during the last frame, in pixels (positive = down)
105
+ * @property tiltY pen: degrees
106
+ *
107
+ * @category Input
108
+ */
109
+ export interface PointerState {
110
+ /** the kind of pointer that last moved or pressed */
111
+ type: 'mouse' | 'pen';
112
+ /** pixels, relative to the target's top-left */
113
+ x: number;
114
+ y: number;
115
+ /** -1..1, y up (for raycasting) */
116
+ ndcX: number;
117
+ ndcY: number;
118
+ /** movement during the last frame, pixels (works under pointer lock) */
119
+ deltaX: number;
120
+ deltaY: number;
121
+ /** wheel movement during the last frame, in pixels */
122
+ wheelX: number;
123
+ wheelY: number;
124
+ /** pen: 0..1 */
125
+ pressure: number;
126
+ /** pen: degrees */
127
+ tiltX: number;
128
+ tiltY: number;
129
+ }
130
+ /**
131
+ * Options for {@link InputActionMap.captureBinding}.
132
+ *
133
+ * @category Input
134
+ */
135
+ export interface CaptureOptions {
136
+ /** KeyboardEvent.code that cancels (resolves null); `null` for none. Default 'Escape'. */
137
+ cancelKey?: string | null;
138
+ /** only accept these kinds of input; others are ignored and still reach actions */
139
+ types?: Binding['type'][];
140
+ }
141
+ /**
142
+ * Game input with a Godot-style action map, built on {@link Input}'s events: bind keys, buttons,
143
+ * sticks, gestures and VR controls to named actions, then query actions instead of devices once
144
+ * per frame. Gamepads and VR controllers are polled by `update()`.
145
+ *
146
+ * Input.instance.target = canvas;
147
+ *
148
+ * const controls = new InputActionMap();
149
+ * controls.actions.add('jump', [key('Space'), pad('A')]);
150
+ *
151
+ * // every frame, before game logic:
152
+ * controls.update();
153
+ * if (controls.isActionJustPressed('jump')) player.jump();
154
+ *
155
+ * Call `dispose()` to stop listening.
156
+ *
157
+ * @category Input
158
+ */
159
+ export declare class InputActionMap {
160
+ /** the actions to query; holds the `ui_*` defaults unless {@link InputActionMapOptions.defaults} is false */
161
+ readonly actions: ActionMap;
162
+ /** Thresholds for taps, swipes etc. can be tuned via `gestureRecognizer.thresholds`. */
163
+ readonly gestureRecognizer: GestureRecognizer;
164
+ /**
165
+ * Touch gestures from the last frame: `events` (each recognized gesture and where it happened,
166
+ * e.g. the tap that triggered an action) and the two-finger values (pinch 1 / pan 0 / rotate 0
167
+ * when not gesturing).
168
+ */
169
+ readonly gestures: Omit<GestureFrame, 'gestures'>;
170
+ /** the {@link Input} events are read from */
171
+ readonly input: Input;
172
+ /** Fingers currently on the target. */
173
+ get touches(): TouchPoint[];
174
+ /** the mouse or pen; not updated by touch */
175
+ readonly pointer: PointerState;
176
+ /** notified when {@link InputActionMap.lastDevice} changes, e.g. to switch on-screen button prompts */
177
+ readonly onDeviceChange: Observable<InputDeviceType>;
178
+ /** notified from update() when a gamepad is first seen */
179
+ readonly onGamepadConnected: Observable<GamepadInfo>;
180
+ /** notified from update() when a gamepad goes away; its held controls are released */
181
+ readonly onGamepadDisconnected: Observable<GamepadInfo>;
182
+ private readonly _gamepads;
183
+ /** Connected gamepads, as found by the last update(). */
184
+ get gamepads(): GamepadInfo[];
185
+ private _lastDevice;
186
+ private _keyLabels?;
187
+ private readonly _options;
188
+ private readonly _listeners;
189
+ private readonly _controls;
190
+ private readonly _actionStates;
191
+ private readonly _actionListeners;
192
+ private readonly _warned;
193
+ private readonly _pending;
194
+ private _capture?;
195
+ /** The kind of device that produced the most recent input (initially `'keyboard'`). */
196
+ get lastDevice(): InputDeviceType;
197
+ /** Whether the pointer is locked to the target, see {@link InputActionMap.lockPointer}. */
198
+ get isPointerLocked(): boolean;
199
+ constructor(options?: InputActionMapOptions);
200
+ /**
201
+ * Call once per frame, before reading input: polls devices and advances "just pressed" /
202
+ * "just released" by one frame. Action callbacks fire from here.
203
+ *
204
+ * @param time current time in ms (e.g. the `requestAnimationFrame` timestamp), used for
205
+ * gesture timing and auto-repeat; default `performance.now()`
206
+ */
207
+ update(time?: number): void;
208
+ /** Whether the action is held past its deadzone. Unknown actions return false (and warn once). */
209
+ isActionPressed(action: string, query?: DeviceQuery): boolean;
210
+ /**
211
+ * Whether the action became pressed in the last update(), or auto-repeated while held if the
212
+ * action has {@link Action.repeat} (e.g. to keep walking while a direction is held).
213
+ */
214
+ isActionJustPressed(action: string, query?: DeviceQuery): boolean;
215
+ /** Whether the last update()'s press of the action was an auto-repeat rather than a new press. */
216
+ isActionRepeated(action: string, query?: DeviceQuery): boolean;
217
+ /** Whether the action was released in the last update(). */
218
+ isActionJustReleased(action: string, query?: DeviceQuery): boolean;
219
+ /** 0..1, rescaled past the action's deadzone (analog triggers and sticks give values in between). */
220
+ getActionStrength(action: string, query?: DeviceQuery): number;
221
+ /** 0..1 before the deadzone is applied. */
222
+ getActionRawStrength(action: string, query?: DeviceQuery): number;
223
+ /** -1..1: `positive` minus `negative`. */
224
+ getAxis(negative: string, positive: string, query?: DeviceQuery): number;
225
+ /**
226
+ * A 2D direction from four actions, with a circular deadzone and length at most 1 (diagonals
227
+ * aren't faster). Y is positive towards `positiveY`, as in Godot's get_vector("left", "right", "up", "down").
228
+ *
229
+ * @param options.deadzone default: the average of the four actions' deadzones
230
+ * @returns `x` and `y` in -1..1; `{ x: 0, y: 0 }` inside the deadzone
231
+ */
232
+ getVector(negativeX: string, positiveX: string, negativeY: string, positiveY: string, options?: DeviceQuery & {
233
+ deadzone?: number;
234
+ }): {
235
+ /** -1..1, positive towards `positiveX` */
236
+ x: number;
237
+ /** -1..1, positive towards `positiveY` */
238
+ y: number;
239
+ };
240
+ /**
241
+ * Calls `callback` when the action is pressed or released (during update()). With
242
+ * {@link Action.repeat}, `'pressed'` also fires on each auto-repeat, with `event.repeat` set.
243
+ * Returns an unsubscribe function.
244
+ */
245
+ onAction(action: string, phase: ActionPhase, callback: (event: ActionEvent) => void, query?: DeviceQuery): () => void;
246
+ /** Whether a key (by `KeyboardEvent.code`) is held right now, regardless of actions. */
247
+ isKeyPressed(code: string): boolean;
248
+ /** Whether a mouse or pen button is held right now, regardless of actions. */
249
+ isMouseButtonPressed(button: MouseButton): boolean;
250
+ /**
251
+ * Releases every held key and button, on one device or all. Keyboard, mouse and touch (device 0)
252
+ * are released automatically when the window loses focus, since their release events get lost.
253
+ */
254
+ releaseAll(device?: InputDevice): void;
255
+ /** Requests pointer lock on the target (browsers only allow it from a user gesture, e.g. a click). */
256
+ lockPointer(): void;
257
+ /** Exits pointer lock if the target holds it. */
258
+ unlockPointer(): void;
259
+ /**
260
+ * Waits for the player's next input and resolves with a binding for it, for rebinding menus.
261
+ * Resolves `null` if the cancel key is pressed or another capture starts. The captured press
262
+ * doesn't trigger any actions.
263
+ */
264
+ captureBinding(options?: CaptureOptions): Promise<Binding | null>;
265
+ /** Ends a pending {@link InputActionMap.captureBinding}, resolving it with `null`. */
266
+ cancelCapture(): void;
267
+ /**
268
+ * Loads the keyboard layout (where supported, e.g. Chromium) so {@link bindingLabel} names keys
269
+ * as printed on the player's keyboard: KeyW is "Z" on AZERTY.
270
+ */
271
+ loadKeyboardLayout(): Promise<ReadonlyMap<string, string> | undefined>;
272
+ /** The layout loaded by {@link InputActionMap.loadKeyboardLayout}, if any: `KeyboardEvent.code` to printed key. */
273
+ get keyLabels(): ReadonlyMap<string, string> | undefined;
274
+ /** Removes all event listeners and action callbacks, cancels any capture and detaches XR. */
275
+ dispose(): void;
276
+ private _listen;
277
+ private _state;
278
+ private _advance;
279
+ private _controlValue;
280
+ private _bindingValue;
281
+ private _control;
282
+ /** Updates a held control (key, button, axis direction: 0..1); `binding` describes it for captureBinding(). */
283
+ protected _setControl(device: InputDevice, binding: Binding, value: number): void;
284
+ /** A one-frame press with no held state: wheel steps, gestures. */
285
+ protected _pulse(device: InputDevice, binding: Binding): void;
286
+ private _tryCapture;
287
+ private _setLastDevice;
288
+ /**
289
+ * Vibrates a gamepad (where supported). Magnitudes are 0..1; `strong` defaults to 1, `weak` to
290
+ * `strong`, `duration` to 200 ms.
291
+ */
292
+ rumble(device: InputDevice, options?: {
293
+ strong?: number;
294
+ weak?: number;
295
+ duration?: number;
296
+ }): void;
297
+ private _pollGamepads;
298
+ private _updateGestures;
299
+ private _xrSession?;
300
+ private _xrEnd?;
301
+ private readonly _xrHeld;
302
+ /** The session set by {@link InputActionMap.attachXR}, until detached or ended. */
303
+ get xrSession(): XRSessionLike | undefined;
304
+ /**
305
+ * Reads VR controller buttons and sticks from a WebXR session (e.g. `renderer.xr.getSession()`)
306
+ * into the action map. Controller poses stay with three.js (`renderer.xr.getController(i)`).
307
+ * XR controller state only updates during XR frames, so call update() from the XR animation
308
+ * loop (`renderer.setAnimationLoop`). Detaches automatically when the session ends.
309
+ */
310
+ attachXR(session: XRSessionLike): void;
311
+ /** Stops reading the XR session and releases its controls. */
312
+ detachXR(): void;
313
+ private _pollXR;
314
+ private _releaseXR;
315
+ private _isBound;
316
+ private _onKeyDown;
317
+ private _onKeyUp;
318
+ private _updatePointer;
319
+ private _local;
320
+ private _onButton;
321
+ private _onPointerMove;
322
+ private _onTouchDown;
323
+ private _onTouchMove;
324
+ private _onTouchUp;
325
+ private _onWheel;
326
+ }
327
+ //#endregion