@utsp/input 0.10.0-nightly.20251211194839.e68c16e → 0.10.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/dist/core.cjs +1 -0
- package/dist/core.d.ts +200 -0
- package/dist/core.mjs +1 -0
- package/dist/desktop.cjs +1 -0
- package/dist/desktop.d.ts +540 -0
- package/dist/desktop.mjs +1 -0
- package/dist/gamepad.cjs +1 -0
- package/dist/gamepad.d.ts +563 -0
- package/dist/gamepad.mjs +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +369 -5
- package/dist/index.mjs +1 -1
- package/dist/keyboard.cjs +1 -0
- package/dist/keyboard.d.ts +341 -0
- package/dist/keyboard.mjs +1 -0
- package/dist/mouse.cjs +1 -0
- package/dist/mouse.d.ts +376 -0
- package/dist/mouse.mjs +1 -0
- package/dist/touch.cjs +1 -0
- package/dist/touch.d.ts +572 -0
- package/dist/touch.mjs +1 -0
- package/package.json +54 -2
package/dist/mouse.d.ts
ADDED
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import { Vector2, Vector3 } from '@utsp/types';
|
|
2
|
+
export { InputDeviceType, MouseInput, Vector2 } from '@utsp/types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base interface for all input management systems
|
|
6
|
+
* This provides a common API for keyboard, mouse, gamepad, and touch inputs
|
|
7
|
+
*/
|
|
8
|
+
interface IInputs {
|
|
9
|
+
/**
|
|
10
|
+
* Start listening to input events
|
|
11
|
+
*/
|
|
12
|
+
start(): void;
|
|
13
|
+
/**
|
|
14
|
+
* Stop listening to input events and cleanup
|
|
15
|
+
*/
|
|
16
|
+
stop(): void;
|
|
17
|
+
/**
|
|
18
|
+
* Reset all input states to default
|
|
19
|
+
*/
|
|
20
|
+
reset(): void;
|
|
21
|
+
/**
|
|
22
|
+
* Check if the input system is currently active/listening
|
|
23
|
+
*/
|
|
24
|
+
isListening(): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Cleanup and destroy the instance, removing all event listeners
|
|
27
|
+
*/
|
|
28
|
+
destroy(): void;
|
|
29
|
+
/**
|
|
30
|
+
* Reset delta values (call at the end of each frame/update)
|
|
31
|
+
*/
|
|
32
|
+
resetDelta(): void;
|
|
33
|
+
/**
|
|
34
|
+
* Set or update event callbacks
|
|
35
|
+
*/
|
|
36
|
+
setCallbacks(callbacks: unknown): void;
|
|
37
|
+
/**
|
|
38
|
+
* Remove all event callbacks
|
|
39
|
+
*/
|
|
40
|
+
clearCallbacks(): void;
|
|
41
|
+
/**
|
|
42
|
+
* Check if a key is currently pressed (Keyboard)
|
|
43
|
+
*/
|
|
44
|
+
isKeyPressed?(key: string): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Check if left mouse button is pressed (Mouse)
|
|
47
|
+
*/
|
|
48
|
+
isLeftMousePressed?(): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Get mouse position (Mouse)
|
|
51
|
+
*/
|
|
52
|
+
getMousePosition?(): Vector2 | null;
|
|
53
|
+
/**
|
|
54
|
+
* Get mouse movement delta (Mouse)
|
|
55
|
+
*/
|
|
56
|
+
getMouseDelta?(): Vector2 | null;
|
|
57
|
+
/**
|
|
58
|
+
* Get wheel delta (Mouse)
|
|
59
|
+
*/
|
|
60
|
+
getWheelDelta?(): Vector2 | null;
|
|
61
|
+
/**
|
|
62
|
+
* Check if a button is pressed (Gamepad, TVRemote)
|
|
63
|
+
*/
|
|
64
|
+
isButtonPressed?(button: string | number): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Get axis value (Gamepad, TVRemote)
|
|
67
|
+
*/
|
|
68
|
+
getAxis?(axis: number): number | null;
|
|
69
|
+
/**
|
|
70
|
+
* Get motion/gyroscope data (TVRemote, Mobile)
|
|
71
|
+
*/
|
|
72
|
+
getMotionData?(): MotionData | null;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Motion/Gyroscope data interface
|
|
76
|
+
*/
|
|
77
|
+
interface MotionData extends Vector3 {
|
|
78
|
+
timestamp?: number;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Common button state interface
|
|
82
|
+
*/
|
|
83
|
+
interface ButtonState {
|
|
84
|
+
pressed: boolean;
|
|
85
|
+
justPressed: boolean;
|
|
86
|
+
justReleased: boolean;
|
|
87
|
+
timestamp?: number;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Input event types enumeration
|
|
91
|
+
*/
|
|
92
|
+
declare enum InputEventType {
|
|
93
|
+
KeyDown = "keydown",
|
|
94
|
+
KeyUp = "keyup",
|
|
95
|
+
MouseDown = "mousedown",
|
|
96
|
+
MouseUp = "mouseup",
|
|
97
|
+
MouseMove = "mousemove",
|
|
98
|
+
MouseWheel = "mousewheel",
|
|
99
|
+
TouchStart = "touchstart",
|
|
100
|
+
TouchEnd = "touchend",
|
|
101
|
+
TouchMove = "touchmove",
|
|
102
|
+
GamepadConnected = "gamepadconnected",
|
|
103
|
+
GamepadDisconnected = "gamepaddisconnected",
|
|
104
|
+
GamepadButton = "gamepadbutton",
|
|
105
|
+
GamepadAxis = "gamepadaxis"
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Configuration options for input systems
|
|
109
|
+
*/
|
|
110
|
+
interface InputConfig {
|
|
111
|
+
/**
|
|
112
|
+
* Target element to attach listeners to (default: window)
|
|
113
|
+
*/
|
|
114
|
+
targetElement?: HTMLElement | Window;
|
|
115
|
+
/**
|
|
116
|
+
* Enable/disable specific input types
|
|
117
|
+
*/
|
|
118
|
+
enabled?: boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Prevent default browser behaviors
|
|
121
|
+
*/
|
|
122
|
+
preventDefault?: boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Stop event propagation
|
|
125
|
+
*/
|
|
126
|
+
stopPropagation?: boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Enable debug logging
|
|
129
|
+
*/
|
|
130
|
+
debug?: boolean;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Abstract base class for input systems
|
|
134
|
+
* All input classes should extend this to maintain consistency
|
|
135
|
+
*/
|
|
136
|
+
declare abstract class BaseInputs implements IInputs {
|
|
137
|
+
protected isActive: boolean;
|
|
138
|
+
protected targetElement: HTMLElement | Window;
|
|
139
|
+
protected config: InputConfig;
|
|
140
|
+
protected callbacks: Record<string, ((...args: any[]) => void)[]>;
|
|
141
|
+
constructor(targetElement?: HTMLElement | Window, config?: InputConfig);
|
|
142
|
+
abstract start(): void;
|
|
143
|
+
abstract stop(): void;
|
|
144
|
+
abstract reset(): void;
|
|
145
|
+
abstract resetDelta(): void;
|
|
146
|
+
isListening(): boolean;
|
|
147
|
+
destroy(): void;
|
|
148
|
+
setCallbacks(callbacks: unknown): void;
|
|
149
|
+
clearCallbacks(): void;
|
|
150
|
+
/**
|
|
151
|
+
* Get the target element
|
|
152
|
+
*/
|
|
153
|
+
getTargetElement(): HTMLElement | Window;
|
|
154
|
+
/**
|
|
155
|
+
* Enable the input system
|
|
156
|
+
*/
|
|
157
|
+
enable(): void;
|
|
158
|
+
/**
|
|
159
|
+
* Disable the input system
|
|
160
|
+
*/
|
|
161
|
+
disable(): void;
|
|
162
|
+
/**
|
|
163
|
+
* Check if the input system is enabled
|
|
164
|
+
*/
|
|
165
|
+
isEnabled(): boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Emit an event to all registered callbacks
|
|
168
|
+
*/
|
|
169
|
+
protected emit(eventType: string, ...args: unknown[]): void;
|
|
170
|
+
/**
|
|
171
|
+
* Log debug messages if debug mode is enabled
|
|
172
|
+
*/
|
|
173
|
+
protected log(...args: unknown[]): void;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* MouseInputs - Mouse input management
|
|
178
|
+
* Handles mouse events with support for vanilla JS, React, and other frameworks
|
|
179
|
+
*/
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Mouse button state with press/release detection
|
|
183
|
+
*/
|
|
184
|
+
interface MouseButtonState {
|
|
185
|
+
pressed: boolean;
|
|
186
|
+
justPressed: boolean;
|
|
187
|
+
justReleased: boolean;
|
|
188
|
+
}
|
|
189
|
+
interface MouseButtons {
|
|
190
|
+
left: MouseButtonState;
|
|
191
|
+
middle: MouseButtonState;
|
|
192
|
+
right: MouseButtonState;
|
|
193
|
+
}
|
|
194
|
+
interface MouseCallbacks {
|
|
195
|
+
onMouseDown?: (button: string, position: Vector2, event: MouseEvent) => void;
|
|
196
|
+
onMouseUp?: (button: string, position: Vector2, event: MouseEvent) => void;
|
|
197
|
+
onMouseMove?: (position: Vector2, delta: Vector2, event: MouseEvent) => void;
|
|
198
|
+
onMouseWheel?: (delta: number, event: WheelEvent) => void;
|
|
199
|
+
onMouseEnter?: (event: MouseEvent) => void;
|
|
200
|
+
onMouseLeave?: (event: MouseEvent) => void;
|
|
201
|
+
}
|
|
202
|
+
declare class MouseInputs extends BaseInputs {
|
|
203
|
+
private mouseButtons;
|
|
204
|
+
private mousePosition;
|
|
205
|
+
private mouseDelta;
|
|
206
|
+
private wheelDelta;
|
|
207
|
+
private mouseCallbacks;
|
|
208
|
+
private boundHandlers;
|
|
209
|
+
constructor(targetElement?: HTMLElement | Window, config?: InputConfig);
|
|
210
|
+
start(): void;
|
|
211
|
+
stop(): void;
|
|
212
|
+
reset(): void;
|
|
213
|
+
/**
|
|
214
|
+
* Poll - Reset transient states (justPressed/justReleased)
|
|
215
|
+
* Should be called once per frame AFTER collecting input
|
|
216
|
+
*/
|
|
217
|
+
poll(): void;
|
|
218
|
+
resetDelta(): void;
|
|
219
|
+
/**
|
|
220
|
+
* Check if left mouse button is pressed
|
|
221
|
+
*
|
|
222
|
+
* @returns True if left button is pressed, false otherwise
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```typescript
|
|
226
|
+
* if (mouse.isLeftMousePressed()) {
|
|
227
|
+
* console.log('Left click detected');
|
|
228
|
+
* }
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
isLeftMousePressed(): boolean;
|
|
232
|
+
/**
|
|
233
|
+
* Check if left mouse button was just pressed this frame
|
|
234
|
+
*
|
|
235
|
+
* @returns True if button was just pressed (transition false→true), false otherwise
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```typescript
|
|
239
|
+
* if (mouse.isLeftMouseJustPressed()) {
|
|
240
|
+
* fireWeapon(); // Fires once per click
|
|
241
|
+
* }
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
isLeftMouseJustPressed(): boolean;
|
|
245
|
+
/**
|
|
246
|
+
* Check if left mouse button was just released this frame
|
|
247
|
+
*
|
|
248
|
+
* @returns True if button was just released (transition true→false), false otherwise
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```typescript
|
|
252
|
+
* if (mouse.isLeftMouseJustReleased()) {
|
|
253
|
+
* releaseChargedAttack();
|
|
254
|
+
* }
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
isLeftMouseJustReleased(): boolean;
|
|
258
|
+
/**
|
|
259
|
+
* Check if right mouse button is pressed
|
|
260
|
+
*
|
|
261
|
+
* @returns True if right button is pressed, false otherwise
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```typescript
|
|
265
|
+
* if (mouse.isRightMousePressed()) {
|
|
266
|
+
* showContextMenu();
|
|
267
|
+
* }
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
isRightMousePressed(): boolean;
|
|
271
|
+
/**
|
|
272
|
+
* Check if right mouse button was just pressed this frame
|
|
273
|
+
*/
|
|
274
|
+
isRightMouseJustPressed(): boolean;
|
|
275
|
+
/**
|
|
276
|
+
* Check if right mouse button was just released this frame
|
|
277
|
+
*/
|
|
278
|
+
isRightMouseJustReleased(): boolean;
|
|
279
|
+
/**
|
|
280
|
+
* Check if middle mouse button is pressed
|
|
281
|
+
*
|
|
282
|
+
* @returns True if middle button is pressed, false otherwise
|
|
283
|
+
*/
|
|
284
|
+
isMiddleMousePressed(): boolean;
|
|
285
|
+
/**
|
|
286
|
+
* Check if middle mouse button was just pressed this frame
|
|
287
|
+
*/
|
|
288
|
+
isMiddleMouseJustPressed(): boolean;
|
|
289
|
+
/**
|
|
290
|
+
* Check if middle mouse button was just released this frame
|
|
291
|
+
*/
|
|
292
|
+
isMiddleMouseJustReleased(): boolean;
|
|
293
|
+
/**
|
|
294
|
+
* Get current mouse position
|
|
295
|
+
*
|
|
296
|
+
* @returns Vector2 with x and y coordinates in pixels
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```typescript
|
|
300
|
+
* const pos = mouse.getMousePosition();
|
|
301
|
+
* console.log(`Mouse at (${pos.x}, ${pos.y})`);
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
getMousePosition(): Vector2;
|
|
305
|
+
/**
|
|
306
|
+
* Get mouse movement delta since last frame
|
|
307
|
+
*
|
|
308
|
+
* @returns Vector2 with dx and dy in pixels
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* ```typescript
|
|
312
|
+
* const delta = mouse.getMouseDelta();
|
|
313
|
+
* camera.rotate(delta.x * sensitivity, delta.y * sensitivity);
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
getMouseDelta(): Vector2;
|
|
317
|
+
/**
|
|
318
|
+
* Get mouse wheel scroll delta
|
|
319
|
+
*
|
|
320
|
+
* @returns Wheel delta (positive = scroll down, negative = scroll up)
|
|
321
|
+
*
|
|
322
|
+
* @example
|
|
323
|
+
* ```typescript
|
|
324
|
+
* const wheel = mouse.getWheelDelta();
|
|
325
|
+
* if (wheel !== 0) {
|
|
326
|
+
* camera.zoom(wheel > 0 ? -1 : 1);
|
|
327
|
+
* }
|
|
328
|
+
* ```
|
|
329
|
+
*/
|
|
330
|
+
getWheelDelta(): number;
|
|
331
|
+
/**
|
|
332
|
+
* Set mouse-specific callbacks
|
|
333
|
+
*
|
|
334
|
+
* @param callbacks - Object containing callback functions
|
|
335
|
+
*
|
|
336
|
+
* @example
|
|
337
|
+
* ```typescript
|
|
338
|
+
* mouse.setMouseCallbacks({
|
|
339
|
+
* onMouseDown: (button, position, event) => {
|
|
340
|
+
* console.log(`${button} button pressed at`, position);
|
|
341
|
+
* },
|
|
342
|
+
* onMouseMove: (position, delta, event) => {
|
|
343
|
+
* console.log('Mouse moved by', delta);
|
|
344
|
+
* },
|
|
345
|
+
* onMouseWheel: (delta, event) => {
|
|
346
|
+
* console.log('Wheel scrolled:', delta);
|
|
347
|
+
* },
|
|
348
|
+
* });
|
|
349
|
+
* ```
|
|
350
|
+
*/
|
|
351
|
+
setMouseCallbacks(callbacks: MouseCallbacks): void;
|
|
352
|
+
clearMouseCallbacks(): void;
|
|
353
|
+
setCallbacks(callbacks: MouseCallbacks | unknown): void;
|
|
354
|
+
private isMouseCallbacks;
|
|
355
|
+
/** Button index to name mapping */
|
|
356
|
+
private static readonly BUTTON_NAMES;
|
|
357
|
+
/**
|
|
358
|
+
* Get button state by event.button index
|
|
359
|
+
* @returns Button state and name, or null for unknown buttons
|
|
360
|
+
*/
|
|
361
|
+
private getButtonByIndex;
|
|
362
|
+
private handleMouseDown;
|
|
363
|
+
private handleMouseUp;
|
|
364
|
+
private handleMouseMove;
|
|
365
|
+
private handleWheel;
|
|
366
|
+
private handleMouseEnter;
|
|
367
|
+
private handleMouseLeave;
|
|
368
|
+
private handleContextMenu;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* React hook for mouse inputs
|
|
372
|
+
*/
|
|
373
|
+
declare function useMouseInputs(targetElement?: HTMLElement | Window, callbacks?: MouseCallbacks): MouseInputs;
|
|
374
|
+
|
|
375
|
+
export { BaseInputs, InputEventType, MouseInputs, useMouseInputs };
|
|
376
|
+
export type { ButtonState, IInputs, InputConfig, MouseButtonState, MouseButtons, MouseCallbacks };
|
package/dist/mouse.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var h=Object.defineProperty;var v=(i,s,e)=>s in i?h(i,s,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[s]=e;var a=(i,s)=>h(i,"name",{value:s,configurable:!0});var n=(i,s,e)=>(v(i,typeof s!="symbol"?s+"":s,e),e);import{Vector2 as B}from"@utsp/types";import{InputDeviceType as x,MouseInput as j}from"@utsp/types";var m=(o=>(o.KeyDown="keydown",o.KeyUp="keyup",o.MouseDown="mousedown",o.MouseUp="mouseup",o.MouseMove="mousemove",o.MouseWheel="mousewheel",o.TouchStart="touchstart",o.TouchEnd="touchend",o.TouchMove="touchmove",o.GamepadConnected="gamepadconnected",o.GamepadDisconnected="gamepaddisconnected",o.GamepadButton="gamepadbutton",o.GamepadAxis="gamepadaxis",o))(m||{}),c=class c{constructor(s=window,e={}){n(this,"isActive",!1);n(this,"targetElement");n(this,"config");n(this,"callbacks",{});this.targetElement=s,this.config={enabled:!0,preventDefault:!1,stopPropagation:!1,debug:!1,...e}}isListening(){return this.isActive}destroy(){this.stop(),this.clearCallbacks()}setCallbacks(s){typeof s=="object"&&s!==null&&Object.assign(this.callbacks,s)}clearCallbacks(){this.callbacks={}}getTargetElement(){return this.targetElement}enable(){this.config.enabled=!0,this.isActive||this.start()}disable(){this.config.enabled=!1,this.isActive&&this.stop()}isEnabled(){return this.config.enabled??!0}emit(s,...e){this.callbacks[s]&&this.callbacks[s].forEach(t=>{try{t(...e)}catch(p){this.config.debug&&console.error(`Error in ${s} callback:`,p)}})}log(...s){this.config.debug&&console.warn("[InputSystem]",...s)}};a(c,"BaseInputs");var u=c;import{Vector2 as r}from"@utsp/types";var l=class l extends u{constructor(e=window,t={}){super(e,t);n(this,"mouseButtons",{left:{pressed:!1,justPressed:!1,justReleased:!1},middle:{pressed:!1,justPressed:!1,justReleased:!1},right:{pressed:!1,justPressed:!1,justReleased:!1}});n(this,"mousePosition",r.zero());n(this,"mouseDelta",r.zero());n(this,"wheelDelta",0);n(this,"mouseCallbacks",{});n(this,"boundHandlers");this.boundHandlers={mouseDown:this.handleMouseDown.bind(this),mouseUp:this.handleMouseUp.bind(this),mouseMove:this.handleMouseMove.bind(this),wheel:this.handleWheel.bind(this),mouseEnter:this.handleMouseEnter.bind(this),mouseLeave:this.handleMouseLeave.bind(this),contextMenu:this.handleContextMenu.bind(this)}}start(){this.isActive||(this.targetElement.addEventListener("mousedown",this.boundHandlers.mouseDown),this.targetElement.addEventListener("mouseup",this.boundHandlers.mouseUp),this.targetElement.addEventListener("mousemove",this.boundHandlers.mouseMove),this.targetElement.addEventListener("wheel",this.boundHandlers.wheel),this.targetElement.addEventListener("mouseenter",this.boundHandlers.mouseEnter),this.targetElement.addEventListener("mouseleave",this.boundHandlers.mouseLeave),this.targetElement.addEventListener("contextmenu",this.boundHandlers.contextMenu),this.isActive=!0)}stop(){this.isActive&&(this.targetElement.removeEventListener("mousedown",this.boundHandlers.mouseDown),this.targetElement.removeEventListener("mouseup",this.boundHandlers.mouseUp),this.targetElement.removeEventListener("mousemove",this.boundHandlers.mouseMove),this.targetElement.removeEventListener("wheel",this.boundHandlers.wheel),this.targetElement.removeEventListener("mouseenter",this.boundHandlers.mouseEnter),this.targetElement.removeEventListener("mouseleave",this.boundHandlers.mouseLeave),this.targetElement.removeEventListener("contextmenu",this.boundHandlers.contextMenu),this.isActive=!1)}reset(){this.mouseButtons={left:{pressed:!1,justPressed:!1,justReleased:!1},middle:{pressed:!1,justPressed:!1,justReleased:!1},right:{pressed:!1,justPressed:!1,justReleased:!1}},this.mousePosition=r.zero(),this.mouseDelta=r.zero(),this.wheelDelta=0}poll(){this.mouseButtons.left.justPressed=!1,this.mouseButtons.left.justReleased=!1,this.mouseButtons.middle.justPressed=!1,this.mouseButtons.middle.justReleased=!1,this.mouseButtons.right.justPressed=!1,this.mouseButtons.right.justReleased=!1,this.resetDelta()}resetDelta(){this.mouseDelta=r.zero(),this.wheelDelta=0}isLeftMousePressed(){return this.mouseButtons.left.pressed}isLeftMouseJustPressed(){return this.mouseButtons.left.justPressed}isLeftMouseJustReleased(){return this.mouseButtons.left.justReleased}isRightMousePressed(){return this.mouseButtons.right.pressed}isRightMouseJustPressed(){return this.mouseButtons.right.justPressed}isRightMouseJustReleased(){return this.mouseButtons.right.justReleased}isMiddleMousePressed(){return this.mouseButtons.middle.pressed}isMiddleMouseJustPressed(){return this.mouseButtons.middle.justPressed}isMiddleMouseJustReleased(){return this.mouseButtons.middle.justReleased}getMousePosition(){return this.mousePosition.clone()}getMouseDelta(){return this.mouseDelta.clone()}getWheelDelta(){return this.wheelDelta}setMouseCallbacks(e){this.mouseCallbacks={...this.mouseCallbacks,...e}}clearMouseCallbacks(){this.mouseCallbacks={}}setCallbacks(e){this.isMouseCallbacks(e)&&this.setMouseCallbacks(e)}isMouseCallbacks(e){return typeof e=="object"&&e!==null&&("onMouseDown"in e||"onMouseUp"in e||"onMouseMove"in e||"onMouseWheel"in e||"onMouseEnter"in e||"onMouseLeave"in e)}getButtonByIndex(e){let t=l.BUTTON_NAMES[e];return t?{state:this.mouseButtons[t],name:t}:null}handleMouseDown(e){try{this.config.preventDefault&&e.preventDefault(),this.config.stopPropagation&&e.stopPropagation();let t=this.getButtonByIndex(e.button);t&&(t.state.pressed||(t.state.justPressed=!0),t.state.pressed=!0,t.state.justReleased=!1,this.mouseCallbacks.onMouseDown?.(t.name,this.mousePosition,e))}catch(t){console.error("Error in mouse down handler:",t)}}handleMouseUp(e){try{this.config.preventDefault&&e.preventDefault(),this.config.stopPropagation&&e.stopPropagation();let t=this.getButtonByIndex(e.button);t&&(t.state.pressed&&(t.state.justReleased=!0),t.state.pressed=!1,t.state.justPressed=!1,this.mouseCallbacks.onMouseUp?.(t.name,this.mousePosition,e))}catch(t){console.error("Error in mouse up handler:",t)}}handleMouseMove(e){try{this.config.preventDefault&&e.preventDefault(),this.config.stopPropagation&&e.stopPropagation();let t=new r(e.clientX,e.clientY);this.mouseDelta=t.subtract(this.mousePosition),this.mousePosition=t,this.mouseCallbacks.onMouseMove?.(this.mousePosition,this.mouseDelta,e)}catch(t){console.error("Error in mouse move handler:",t)}}handleWheel(e){try{this.config.preventDefault&&e.preventDefault(),this.config.stopPropagation&&e.stopPropagation(),this.wheelDelta=e.deltaY,this.mouseCallbacks.onMouseWheel?.(this.wheelDelta,e)}catch(t){console.error("Error in mouse wheel handler:",t)}}handleMouseEnter(e){try{this.mouseCallbacks.onMouseEnter?.(e)}catch(t){console.error("Error in mouse enter handler:",t)}}handleMouseLeave(e){try{this.mouseCallbacks.onMouseLeave?.(e)}catch(t){console.error("Error in mouse leave handler:",t)}}handleContextMenu(e){try{e.preventDefault(),this.config.stopPropagation&&e.stopPropagation()}catch(t){console.error("Error in context menu handler:",t)}}};a(l,"MouseInputs"),n(l,"BUTTON_NAMES",["left","middle","right"]);var d=l;function b(i,s){let e=new d(i);return s&&e.setCallbacks(s),e.start(),e}a(b,"useMouseInputs");export{u as BaseInputs,x as InputDeviceType,m as InputEventType,j as MouseInput,d as MouseInputs,B as Vector2,b as useMouseInputs};
|
package/dist/touch.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var d=Object.defineProperty;var V=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var D=(a,o,e)=>o in a?d(a,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[o]=e;var h=(a,o)=>d(a,"name",{value:o,configurable:!0});var A=(a,o)=>{for(var e in o)d(a,e,{get:o[e],enumerable:!0})},H=(a,o,e,t)=>{if(o&&typeof o=="object"||typeof o=="function")for(let i of I(o))!L.call(a,i)&&i!==e&&d(a,i,{get:()=>o[i],enumerable:!(t=V(o,i))||t.enumerable});return a};var S=a=>H(d({},"__esModule",{value:!0}),a);var r=(a,o,e)=>(D(a,typeof o!="symbol"?o+"":o,e),e);var $={};A($,{BaseInputs:()=>b,InputDeviceType:()=>m.InputDeviceType,InputEventType:()=>g,MobileInputs:()=>p,MobileVibration:()=>f,TouchInput:()=>m.TouchInput,Vector2:()=>P.Vector2,VibrationPatterns:()=>l,getMobileVibration:()=>k,useMobileInputs:()=>x});module.exports=S($);var P=require("@utsp/types"),m=require("@utsp/types");var g=(u=>(u.KeyDown="keydown",u.KeyUp="keyup",u.MouseDown="mousedown",u.MouseUp="mouseup",u.MouseMove="mousemove",u.MouseWheel="mousewheel",u.TouchStart="touchstart",u.TouchEnd="touchend",u.TouchMove="touchmove",u.GamepadConnected="gamepadconnected",u.GamepadDisconnected="gamepaddisconnected",u.GamepadButton="gamepadbutton",u.GamepadAxis="gamepadaxis",u))(g||{}),T=class T{constructor(o=window,e={}){r(this,"isActive",!1);r(this,"targetElement");r(this,"config");r(this,"callbacks",{});this.targetElement=o,this.config={enabled:!0,preventDefault:!1,stopPropagation:!1,debug:!1,...e}}isListening(){return this.isActive}destroy(){this.stop(),this.clearCallbacks()}setCallbacks(o){typeof o=="object"&&o!==null&&Object.assign(this.callbacks,o)}clearCallbacks(){this.callbacks={}}getTargetElement(){return this.targetElement}enable(){this.config.enabled=!0,this.isActive||this.start()}disable(){this.config.enabled=!1,this.isActive&&this.stop()}isEnabled(){return this.config.enabled??!0}emit(o,...e){this.callbacks[o]&&this.callbacks[o].forEach(t=>{try{t(...e)}catch(i){this.config.debug&&console.error(`Error in ${o} callback:`,i)}})}log(...o){this.config.debug&&console.warn("[InputSystem]",...o)}};h(T,"BaseInputs");var b=T;var C=require("@utsp/types");var M=class M extends b{constructor(e=window,t={}){super(e,t);r(this,"touches",new Map);r(this,"nativeToInternal",new Map);r(this,"mobileCallbacks",{});r(this,"mobileConfig");r(this,"maxTouches",10);r(this,"lastLogTime",0);r(this,"boundHandlers");this.mobileConfig={targetElement:e,preventDefault:t.preventDefault??!0,passive:t.passive??!1,maxTouches:t.maxTouches??10,enabled:t.enabled??!0,debug:t.debug??!1,stopPropagation:t.stopPropagation??!1},this.maxTouches=this.mobileConfig.maxTouches,this.mobileConfig.preventDefault&&this.mobileConfig.passive&&(console.warn("[MobileInputs] passive=true prevents preventDefault() from working! Setting passive=false."),this.mobileConfig.passive=!1),this.boundHandlers={touchStart:this.handleTouchStart.bind(this),touchEnd:this.handleTouchEnd.bind(this),touchMove:this.handleTouchMove.bind(this),touchCancel:this.handleTouchCancel.bind(this),orientationChange:this.handleOrientationChange.bind(this)}}start(){if(this.isActive)return;let e={passive:!this.mobileConfig.preventDefault&&this.mobileConfig.passive};this.targetElement.addEventListener("touchstart",this.boundHandlers.touchStart,e),this.targetElement.addEventListener("touchend",this.boundHandlers.touchEnd,e),this.targetElement.addEventListener("touchmove",this.boundHandlers.touchMove,e),this.targetElement.addEventListener("touchcancel",this.boundHandlers.touchCancel,e),window.addEventListener("orientationchange",this.boundHandlers.orientationChange),this.isActive=!0}stop(){this.isActive&&(this.targetElement.removeEventListener("touchstart",this.boundHandlers.touchStart),this.targetElement.removeEventListener("touchend",this.boundHandlers.touchEnd),this.targetElement.removeEventListener("touchmove",this.boundHandlers.touchMove),this.targetElement.removeEventListener("touchcancel",this.boundHandlers.touchCancel),window.removeEventListener("orientationchange",this.boundHandlers.orientationChange),this.isActive=!1)}reset(){this.touches.clear(),this.nativeToInternal.clear()}resetDelta(){}poll(){for(let e of this.touches.values())e.justTouched=!1,e.justReleased=!1}isTouchActive(e){return e<0||e>=this.maxTouches?!1:this.touches.has(e)}getTouchPosition(e){if(e<0||e>=this.maxTouches)return null;let t=this.touches.get(e);return t?t.position.clone():null}getTouchStartPosition(e){if(e<0||e>=this.maxTouches)return null;let t=this.touches.get(e);return t?t.startPosition.clone():null}getTouchDelta(e){if(e<0||e>=this.maxTouches)return null;let t=this.touches.get(e);return t?t.position.subtract(t.startPosition):null}getTouchCount(){return this.touches.size}isTouchJustStarted(e){if(e<0||e>=this.maxTouches)return!1;let t=this.touches.get(e);return t?t.justTouched:!1}isTouchJustReleased(e){if(e<0||e>=this.maxTouches)return!1;let t=this.touches.get(e);return t?t.justReleased:!1}isAnyTouchJustStarted(){for(let e of this.touches.values())if(e.justTouched)return!0;return!1}getAllTouches(){return Array.from(this.touches.values())}setMobileCallbacks(e){this.mobileCallbacks={...this.mobileCallbacks,...e}}clearMobileCallbacks(){this.mobileCallbacks={}}setCallbacks(e){this.isMobileCallbacks(e)&&this.setMobileCallbacks(e)}isMobileCallbacks(e){return typeof e=="object"&&e!==null&&("onTouchStart"in e||"onTouchEnd"in e||"onTouchMove"in e||"onTouchCancel"in e||"onOrientationChange"in e)}handleTouchStart(e){try{this.mobileConfig.preventDefault&&e.preventDefault();let t=Array.from(e.changedTouches),i=[];for(let s of t){if(this.touches.size>=this.maxTouches)break;let n=-1;for(let v=0;v<this.maxTouches;v++)if(!this.touches.has(v)){n=v;break}if(n===-1)continue;let c=new C.Vector2(s.clientX,s.clientY),w={id:n,nativeId:s.identifier,position:c.clone(),startPosition:c.clone(),active:!0,justTouched:!0,justReleased:!1};this.touches.set(n,w),this.nativeToInternal.set(s.identifier,n),i.push(w),console.warn(`\u{1F446} Touch START - ID: ${n}`)}this.mobileCallbacks.onTouchStart?.(i,e)}catch(t){console.error("Error in touch start handler:",t)}}handleTouchEnd(e){try{this.mobileConfig.preventDefault&&e.preventDefault();let t=Array.from(e.changedTouches),i=[];for(let s of t){let n=this.nativeToInternal.get(s.identifier);if(n!==void 0){let c=this.touches.get(n);c&&(c.justReleased=!0,c.justTouched=!1,i.push({...c}))}}for(let s of t){let n=this.nativeToInternal.get(s.identifier);n!==void 0&&(this.touches.delete(n),this.nativeToInternal.delete(s.identifier),console.warn(`\u{1F447} Touch END - ID: ${n}`))}this.mobileCallbacks.onTouchEnd?.(i,e)}catch(t){console.error("Error in touch end handler:",t)}}handleTouchMove(e){try{let t=Array.from(e.changedTouches);this.mobileConfig.debug&&(!this.lastLogTime||Date.now()-this.lastLogTime>200)&&(console.warn(`\u{1F504} TouchMove event - ${t.length} touches changed`),this.lastLogTime=Date.now());for(let i of t){let s=this.nativeToInternal.get(i.identifier);if(s!==void 0){let n=this.touches.get(s);if(n){let c=n.position.clone();n.position=new C.Vector2(i.clientX,i.clientY),this.mobileConfig.debug&&(!this.lastLogTime||Date.now()-this.lastLogTime>200)&&console.warn(`\u{1F504} Updated touch ${n.id} (native ${i.identifier}) - ${c.x.toFixed(1)},${c.y.toFixed(1)} \u2192 ${i.clientX.toFixed(1)},${i.clientY.toFixed(1)}`)}}else this.mobileConfig.debug&&console.warn(`\u26A0\uFE0F Touch move for unknown native ID ${i.identifier}`)}this.mobileCallbacks.onTouchMove?.(this.getAllTouches(),e)}catch(t){console.error("Error in touch move handler:",t)}}handleTouchCancel(e){try{this.mobileConfig.preventDefault&&e.preventDefault();let t=[],i=Array.from(e.changedTouches);for(let s of i){let n=this.nativeToInternal.get(s.identifier);if(n!==void 0){let c=this.touches.get(n);c&&(c.justReleased=!0,c.justTouched=!1,t.push({...c}))}}for(let s of i){let n=this.nativeToInternal.get(s.identifier);n!==void 0&&(this.touches.delete(n),this.nativeToInternal.delete(s.identifier),this.mobileConfig.debug&&console.warn(`\u274C Touch CANCEL - ID: ${n}, nativeId: ${s.identifier}`))}this.mobileCallbacks.onTouchCancel?.(t,e)}catch(t){console.error("Error in touch cancel handler:",t)}}handleOrientationChange(e){try{let t=(window.screen.orientation&&window.screen.orientation.angle)??window.orientation??0;this.mobileCallbacks.onOrientationChange?.(t)}catch(t){console.error("Error in orientation change handler:",t)}}};h(M,"MobileInputs");var p=M;function x(a,o,e){console.warn("[MobileInputs] useMobileInputs() is deprecated and causes memory leaks. Use useMemo + useEffect instead.");let t=new p(a,e);return o&&t.setCallbacks(o),t.start(),t}h(x,"useMobileInputs");var l={tap:10,mediumTap:25,heavyTap:50,success:[30,50,30],error:[50,30,50,30,50],warning:[100],selection:5,impactLight:15,impactMedium:30,impactHeavy:50,notification:[100,100,100,100,100],sos:[100,30,100,30,100,30,200,30,200,30,200,30,100,30,100,30,100]},y=class y{constructor(o={}){r(this,"config");r(this,"supported",!1);r(this,"userActivated",!1);this.config={enabled:o.enabled??!0,debug:o.debug??!1,intensity:Math.max(0,Math.min(1,o.intensity??1))},this.supported=typeof navigator<"u"&&"vibrate"in navigator,this.config.debug&&console.log(`[MobileVibration] Vibration API supported: ${this.supported}`)}isSupported(){return this.supported}isEnabled(){return this.config.enabled&&this.supported}setEnabled(o){this.config.enabled=o,this.config.debug&&console.log(`[MobileVibration] Enabled: ${o}`)}setIntensity(o){this.config.intensity=Math.max(0,Math.min(1,o)),this.config.debug&&console.log(`[MobileVibration] Intensity set to: ${this.config.intensity}`)}getIntensity(){return this.config.intensity}vibrate(o){if(!this.isEnabled())return!1;try{let e=this.scalePattern(o),t=typeof e=="number"?e:[...e],i=navigator.vibrate(t);return this.config.debug&&console.log(`[MobileVibration] Vibrate: ${JSON.stringify(e)} -> ${i}`),i&&(this.userActivated=!0),i}catch(e){return this.config.debug&&console.error("[MobileVibration] Vibration failed:",e),!1}}cancel(){this.supported&&(navigator.vibrate(0),this.config.debug&&console.log("[MobileVibration] Vibration cancelled"))}hasUserActivation(){return this.userActivated}tap(){return this.vibrate(l.tap)}mediumTap(){return this.vibrate(l.mediumTap)}heavyTap(){return this.vibrate(l.heavyTap)}success(){return this.vibrate(l.success)}error(){return this.vibrate(l.error)}warning(){return this.vibrate(l.warning)}selection(){return this.vibrate(l.selection)}impactLight(){return this.vibrate(l.impactLight)}impactMedium(){return this.vibrate(l.impactMedium)}impactHeavy(){return this.vibrate(l.impactHeavy)}notification(){return this.vibrate(l.notification)}scalePattern(o){return this.config.intensity===1?o:typeof o=="number"?Math.round(o*this.config.intensity):o.map((e,t)=>t%2===0?Math.round(e*this.config.intensity):e)}};h(y,"MobileVibration");var f=y,E=null;function k(){return E||(E=new f),E}h(k,"getMobileVibration");
|