@utsp/input 0.1.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Thomas Piquet
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # @utsp/input
2
+
3
+ > ⚠️ **PROTOTYPE - NOT READY FOR PRODUCTION**
4
+ >
5
+ > This package is currently in early development and should **NOT** be used in production.
6
+ > The API is unstable and subject to breaking changes without notice.
7
+
8
+ Comprehensive input management system for UTSP (Universal Text Stream Protocol) - keyboard, mouse, gamepad, and touch support.
9
+
10
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
11
+
12
+ ## ⚠️ Development Status
13
+
14
+ **This is a prototype package under active development.**
15
+
16
+ - ❌ No stable API
17
+ - ❌ No documentation available yet
18
+ - ❌ Breaking changes expected
19
+ - ❌ Not recommended for production use
20
+
21
+ **Please check back later for updates or watch the repository for release announcements.**
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ npm install @utsp/input
27
+ ```
28
+
29
+ ## Repository
30
+
31
+ - [GitHub](https://github.com/thp-software/utsp)
32
+ - [Issues](https://github.com/thp-software/utsp/issues)
33
+
34
+ ## License
35
+
36
+ MIT © 2025 Thomas Piquet
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";var G=Object.defineProperty;var ae=Object.getOwnPropertyDescriptor;var re=Object.getOwnPropertyNames;var ue=Object.prototype.hasOwnProperty;var le=(c,s,e)=>s in c?G(c,s,{enumerable:!0,configurable:!0,writable:!0,value:e}):c[s]=e;var m=(c,s)=>G(c,"name",{value:s,configurable:!0});var de=(c,s)=>{for(var e in s)G(c,e,{get:s[e],enumerable:!0})},ce=(c,s,e,t)=>{if(s&&typeof s=="object"||typeof s=="function")for(let o of re(s))!ue.call(c,o)&&o!==e&&G(c,o,{get:()=>s[o],enumerable:!(t=ae(s,o))||t.enumerable});return c};var he=c=>ce(G({},"__esModule",{value:!0}),c);var d=(c,s,e)=>(le(c,typeof s!="symbol"?s+"":s,e),e);var pe={};de(pe,{BaseInputs:()=>g,GamepadInput:()=>v.GamepadInput,GamepadInputs:()=>I,InputCollector:()=>L,InputDeviceType:()=>v.InputDeviceType,InputEventType:()=>$,KeyboardInput:()=>v.KeyboardInput,KeyboardInputs:()=>E,MobileInputs:()=>k,MouseInput:()=>v.MouseInput,MouseInputs:()=>T,TouchInput:()=>v.TouchInput,UnifiedInputRouter:()=>K,Vector2:()=>R.Vector2,Vector3:()=>R.Vector3,isInputs:()=>q,useGamepadInputs:()=>ee,useKeyboardInputs:()=>Q,useMobileInputs:()=>_,useMouseInputs:()=>Z});module.exports=he(pe);var R=require("@utsp/types"),v=require("@utsp/types");var $=(p=>(p.KeyDown="keydown",p.KeyUp="keyup",p.MouseDown="mousedown",p.MouseUp="mouseup",p.MouseMove="mousemove",p.MouseWheel="mousewheel",p.TouchStart="touchstart",p.TouchEnd="touchend",p.TouchMove="touchmove",p.GamepadConnected="gamepadconnected",p.GamepadDisconnected="gamepaddisconnected",p.GamepadButton="gamepadbutton",p.GamepadAxis="gamepadaxis",p))($||{}),W=class W{constructor(s=window,e={}){d(this,"isActive",!1);d(this,"targetElement");d(this,"config");d(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(o){this.config.debug&&console.error(`Error in ${s} callback:`,o)}})}log(...s){this.config.debug&&console.warn("[InputSystem]",...s)}};m(W,"BaseInputs");var g=W;function q(c){return typeof c=="object"&&c!==null&&"start"in c&&"stop"in c&&"reset"in c&&"isListening"in c&&"destroy"in c}m(q,"isInputs");var V=class V extends g{constructor(e=window,t={}){super(e,t);d(this,"keys",{});d(this,"keyboardCallbacks",{});d(this,"textInputsThisFrame",[]);d(this,"boundHandlers");this.boundHandlers={keyDown:this.handleKeyDown.bind(this),keyUp:this.handleKeyUp.bind(this)}}start(){this.isActive||(this.targetElement.addEventListener("keydown",this.boundHandlers.keyDown),this.targetElement.addEventListener("keyup",this.boundHandlers.keyUp),this.isActive=!0)}stop(){this.isActive&&(this.targetElement.removeEventListener("keydown",this.boundHandlers.keyDown),this.targetElement.removeEventListener("keyup",this.boundHandlers.keyUp),this.isActive=!1)}reset(){this.keys={}}resetDelta(){}poll(){for(let e in this.keys)this.keys[e].justPressed=!1,this.keys[e].justReleased=!1;this.textInputsThisFrame=[]}getTextInputs(){return this.textInputsThisFrame}isKeyPressed(e){return this.keys[e]?.pressed||!1}isKeyJustPressed(e){return this.keys[e]?.justPressed||!1}isKeyJustReleased(e){return this.keys[e]?.justReleased||!1}getKeysPressed(){return Object.keys(this.keys).filter(e=>this.keys[e].pressed)}getKeysJustPressed(){return Object.keys(this.keys).filter(e=>this.keys[e].justPressed)}getKeysJustReleased(){return Object.keys(this.keys).filter(e=>this.keys[e].justReleased)}isAnyKeyPressed(){return Object.values(this.keys).some(e=>e.pressed)}setKeyboardCallbacks(e){this.keyboardCallbacks={...this.keyboardCallbacks,...e}}clearKeyboardCallbacks(){this.keyboardCallbacks={}}setCallbacks(e){this.isKeyboardCallbacks(e)&&this.setKeyboardCallbacks(e)}isKeyboardCallbacks(e){return typeof e=="object"&&e!==null&&("onKeyDown"in e||"onKeyUp"in e)}handleKeyDown(e){try{let t=e.code,o=/^F([1-9]|1[0-2])$/.test(t),n=(e.ctrlKey||e.metaKey)&&(t==="KeyT"||t==="KeyW"||t==="KeyR"||t==="KeyN"||t==="Tab");this.config.preventDefault&&!o&&!n&&e.preventDefault(),this.config.stopPropagation&&!o&&!n&&e.stopPropagation(),this.keys[t]||(this.keys[t]={pressed:!1,justPressed:!1,justReleased:!1}),this.keys[t].pressed||(this.keys[t].justPressed=!0,this.keyboardCallbacks.onKeyDown?.(t,e)),this.keys[t].pressed=!0,this.keys[t].justReleased=!1;let i=e.key;i.length===1&&!e.ctrlKey&&!e.metaKey&&this.textInputsThisFrame.push(i)}catch(t){console.error("Error in keyboard keydown handler:",t)}}handleKeyUp(e){try{let t=e.code,o=/^F([1-9]|1[0-2])$/.test(t),n=(e.ctrlKey||e.metaKey)&&(t==="KeyT"||t==="KeyW"||t==="KeyR"||t==="KeyN"||t==="Tab");this.config.preventDefault&&!o&&!n&&e.preventDefault(),this.config.stopPropagation&&!o&&!n&&e.stopPropagation(),this.keys[t]||(this.keys[t]={pressed:!1,justPressed:!1,justReleased:!1}),this.keys[t].pressed&&(this.keys[t].justReleased=!0,this.keyboardCallbacks.onKeyUp?.(t,e)),this.keys[t].pressed=!1,this.keys[t].justPressed=!1}catch(t){console.error("Error in keyboard keyup handler:",t)}}};m(V,"KeyboardInputs");var E=V;function Q(c,s){let e=new E(c);return s&&e.setCallbacks(s),e.start(),e}m(Q,"useKeyboardInputs");var C=require("@utsp/types");var U=class U extends g{constructor(e=window,t={}){super(e,t);d(this,"mouseButtons",{left:{pressed:!1,justPressed:!1,justReleased:!1},middle:{pressed:!1,justPressed:!1,justReleased:!1},right:{pressed:!1,justPressed:!1,justReleased:!1}});d(this,"mousePosition",C.Vector2.zero());d(this,"mouseDelta",C.Vector2.zero());d(this,"wheelDelta",0);d(this,"mouseCallbacks",{});d(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=C.Vector2.zero(),this.mouseDelta=C.Vector2.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=C.Vector2.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)}handleMouseDown(e){try{switch(this.config.preventDefault&&e.preventDefault(),this.config.stopPropagation&&e.stopPropagation(),e.button){case 0:this.mouseButtons.left.pressed||(this.mouseButtons.left.justPressed=!0),this.mouseButtons.left.pressed=!0,this.mouseButtons.left.justReleased=!1,this.mouseCallbacks.onMouseDown?.("left",this.mousePosition,e);break;case 1:this.mouseButtons.middle.pressed||(this.mouseButtons.middle.justPressed=!0),this.mouseButtons.middle.pressed=!0,this.mouseButtons.middle.justReleased=!1,this.mouseCallbacks.onMouseDown?.("middle",this.mousePosition,e);break;case 2:this.mouseButtons.right.pressed||(this.mouseButtons.right.justPressed=!0),this.mouseButtons.right.pressed=!0,this.mouseButtons.right.justReleased=!1,this.mouseCallbacks.onMouseDown?.("right",this.mousePosition,e);break}}catch(t){console.error("Error in mouse down handler:",t)}}handleMouseUp(e){try{switch(this.config.preventDefault&&e.preventDefault(),this.config.stopPropagation&&e.stopPropagation(),e.button){case 0:this.mouseButtons.left.pressed&&(this.mouseButtons.left.justReleased=!0),this.mouseButtons.left.pressed=!1,this.mouseButtons.left.justPressed=!1,this.mouseCallbacks.onMouseUp?.("left",this.mousePosition,e);break;case 1:this.mouseButtons.middle.pressed&&(this.mouseButtons.middle.justReleased=!0),this.mouseButtons.middle.pressed=!1,this.mouseButtons.middle.justPressed=!1,this.mouseCallbacks.onMouseUp?.("middle",this.mousePosition,e);break;case 2:this.mouseButtons.right.pressed&&(this.mouseButtons.right.justReleased=!0),this.mouseButtons.right.pressed=!1,this.mouseButtons.right.justPressed=!1,this.mouseCallbacks.onMouseUp?.("right",this.mousePosition,e);break}}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 C.Vector2(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)}}};m(U,"MouseInputs");var T=U;function Z(c,s){let e=new T(c);return s&&e.setCallbacks(s),e.start(),e}m(Z,"useMouseInputs");var z=require("@utsp/types");var Y=class Y extends g{constructor(e=window,t={}){super(e,t);d(this,"touches",new Map);d(this,"nativeToInternal",new Map);d(this,"mobileCallbacks",{});d(this,"mobileConfig");d(this,"maxTouches",10);d(this,"lastLogTime",0);d(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),o=[];for(let n of t){if(this.touches.size>=this.maxTouches)break;let i=-1;for(let h=0;h<this.maxTouches;h++)if(!this.touches.has(h)){i=h;break}if(i===-1)continue;let a=new z.Vector2(n.clientX,n.clientY),l={id:i,nativeId:n.identifier,position:a.clone(),startPosition:a.clone(),active:!0,justTouched:!0,justReleased:!1};this.touches.set(i,l),this.nativeToInternal.set(n.identifier,i),o.push(l),console.warn(`\u{1F446} Touch START - ID: ${i}`)}this.mobileCallbacks.onTouchStart?.(o,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),o=[];for(let n of t){let i=this.nativeToInternal.get(n.identifier);if(i!==void 0){let a=this.touches.get(i);a&&(a.justReleased=!0,a.justTouched=!1,o.push({...a}))}}for(let n of t){let i=this.nativeToInternal.get(n.identifier);i!==void 0&&(this.touches.delete(i),this.nativeToInternal.delete(n.identifier),console.warn(`\u{1F447} Touch END - ID: ${i}`))}this.mobileCallbacks.onTouchEnd?.(o,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 o of t){let n=this.nativeToInternal.get(o.identifier);if(n!==void 0){let i=this.touches.get(n);if(i){let a=i.position.clone();i.position=new z.Vector2(o.clientX,o.clientY),this.mobileConfig.debug&&(!this.lastLogTime||Date.now()-this.lastLogTime>200)&&console.warn(`\u{1F504} Updated touch ${i.id} (native ${o.identifier}) - ${a.x.toFixed(1)},${a.y.toFixed(1)} \u2192 ${o.clientX.toFixed(1)},${o.clientY.toFixed(1)}`)}}else this.mobileConfig.debug&&console.warn(`\u26A0\uFE0F Touch move for unknown native ID ${o.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=[],o=Array.from(e.changedTouches);for(let n of o){let i=this.nativeToInternal.get(n.identifier);if(i!==void 0){let a=this.touches.get(i);a&&(a.justReleased=!0,a.justTouched=!1,t.push({...a}))}}for(let n of o){let i=this.nativeToInternal.get(n.identifier);i!==void 0&&(this.touches.delete(i),this.nativeToInternal.delete(n.identifier),this.mobileConfig.debug&&console.warn(`\u274C Touch CANCEL - ID: ${i}, nativeId: ${n.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)}}};m(Y,"MobileInputs");var k=Y;function _(c,s,e){console.warn("[MobileInputs] useMobileInputs() is deprecated and causes memory leaks. Use useMemo + useEffect instead.");let t=new k(c,e);return s&&t.setCallbacks(s),t.start(),t}m(_,"useMobileInputs");var X=class X extends g{constructor(e={}){super(window,e);d(this,"gamepads",new Map);d(this,"previousGamepads",new Map);d(this,"gamepadCallbacks",{});d(this,"gamepadConfig");d(this,"pollingIntervalId",null);d(this,"rafId",null);d(this,"boundHandlers");this.gamepadConfig={targetElement:window,maxGamepads:e.maxGamepads??4,axisDeadzone:e.axisDeadzone??.1,pollingInterval:e.pollingInterval??16,autoPolling:e.autoPolling??!0,enabled:e.enabled??!0,debug:e.debug??!1,preventDefault:e.preventDefault??!1,stopPropagation:e.stopPropagation??!1},this.boundHandlers={connected:this.handleGamepadConnected.bind(this),disconnected:this.handleGamepadDisconnected.bind(this)}}start(){this.isActive||(window.addEventListener("gamepadconnected",this.boundHandlers.connected),window.addEventListener("gamepaddisconnected",this.boundHandlers.disconnected),this.gamepadConfig.autoPolling&&this.startPolling(),this.isActive=!0,this.log("GamepadInputs started"))}stop(){this.isActive&&(window.removeEventListener("gamepadconnected",this.boundHandlers.connected),window.removeEventListener("gamepaddisconnected",this.boundHandlers.disconnected),this.stopPolling(),this.isActive=!1,this.log("GamepadInputs stopped"))}reset(){this.gamepads.clear(),this.previousGamepads.clear()}resetDelta(){this.previousGamepads.clear(),this.gamepads.forEach((e,t)=>{this.previousGamepads.set(t,this.cloneGamepadState(e))})}startPolling(){if(this.pollingIntervalId!==null||this.rafId!==null)return;let e=m(()=>{this.poll(),this.rafId=requestAnimationFrame(e)},"poll");this.rafId=requestAnimationFrame(e),this.log("Polling started")}stopPolling(){this.rafId!==null&&(cancelAnimationFrame(this.rafId),this.rafId=null),this.pollingIntervalId!==null&&(clearInterval(this.pollingIntervalId),this.pollingIntervalId=null),this.log("Polling stopped")}poll(){let e=navigator.getGamepads?navigator.getGamepads():[];for(let t=0;t<e.length&&t<this.gamepadConfig.maxGamepads;t++){let o=e[t];o&&this.updateGamepadState(o)}}getConnectedGamepadCount(){return this.gamepads.size}isGamepadConnected(e){return this.gamepads.has(e)&&this.gamepads.get(e).connected}getGamepadState(e){return this.gamepads.get(e)||null}getAllGamepads(){return Array.from(this.gamepads.values()).filter(e=>e.connected)}isButtonPressed(e,t){if(!Number.isInteger(e)||e<0)throw new Error(`Invalid gamepadIndex: ${e}. Must be a non-negative integer.`);if(!Number.isInteger(t)||t<0)throw new Error(`Invalid buttonIndex: ${t}. Must be a non-negative integer.`);let o=this.gamepads.get(e);return!o||!o.connected||t>=o.buttons.length?!1:o.buttons[t].pressed}isButtonJustPressed(e,t){if(!Number.isInteger(e)||e<0)throw new Error(`Invalid gamepadIndex: ${e}. Must be a non-negative integer.`);if(!Number.isInteger(t)||t<0)throw new Error(`Invalid buttonIndex: ${t}. Must be a non-negative integer.`);let o=this.gamepads.get(e);return!o||!o.connected||t>=o.buttons.length?!1:o.buttons[t].justPressed}isButtonJustReleased(e,t){if(!Number.isInteger(e)||e<0)throw new Error(`Invalid gamepadIndex: ${e}. Must be a non-negative integer.`);if(!Number.isInteger(t)||t<0)throw new Error(`Invalid buttonIndex: ${t}. Must be a non-negative integer.`);let o=this.gamepads.get(e);return!o||!o.connected||t>=o.buttons.length?!1:o.buttons[t].justReleased}getButtonValue(e,t){if(!Number.isInteger(e)||e<0)throw new Error(`Invalid gamepadIndex: ${e}. Must be a non-negative integer.`);if(!Number.isInteger(t)||t<0)throw new Error(`Invalid buttonIndex: ${t}. Must be a non-negative integer.`);let o=this.gamepads.get(e);return!o||!o.connected||t>=o.buttons.length?0:o.buttons[t].value}getAxis(e,t){if(!Number.isInteger(e)||e<0)throw new Error(`Invalid gamepadIndex: ${e}. Must be a non-negative integer.`);if(!Number.isInteger(t)||t<0)throw new Error(`Invalid axisIndex: ${t}. Must be a non-negative integer.`);let o=this.gamepads.get(e);if(!o||!o.connected||t>=o.axes.length)return 0;let n=o.axes[t];return this.applyDeadzone(n)}getAxisRaw(e,t){if(!Number.isInteger(e)||e<0)throw new Error(`Invalid gamepadIndex: ${e}. Must be a non-negative integer.`);if(!Number.isInteger(t)||t<0)throw new Error(`Invalid axisIndex: ${t}. Must be a non-negative integer.`);let o=this.gamepads.get(e);return!o||!o.connected||t>=o.axes.length?0:o.axes[t]}getLeftStick(e){return{x:this.getAxis(e,0),y:this.getAxis(e,1)}}getRightStick(e){return{x:this.getAxis(e,2),y:this.getAxis(e,3)}}setGamepadCallbacks(e){this.gamepadCallbacks={...this.gamepadCallbacks,...e}}clearGamepadCallbacks(){this.gamepadCallbacks={}}setCallbacks(e){this.isGamepadCallbacks(e)&&this.setGamepadCallbacks(e)}isGamepadCallbacks(e){return typeof e=="object"&&e!==null&&("onGamepadConnected"in e||"onGamepadDisconnected"in e||"onButtonDown"in e||"onButtonUp"in e||"onAxisMove"in e)}updateGamepadState(e){let t=this.gamepads.get(e.index),o=Array.from(e.buttons).map((a,l)=>{let h=t?.buttons[l],w=a.pressed,b=h?.pressed??!1;return{pressed:w,justPressed:w&&!b,justReleased:!w&&b,value:a.value,touched:a.touched??!1}}),n=Array.from(e.axes),i={id:e.id,index:e.index,connected:e.connected,buttons:o,axes:n,timestamp:e.timestamp,mapping:e.mapping};this.gamepads.set(e.index,i),this.triggerCallbacks(i,t)}triggerCallbacks(e,t){try{e.buttons.forEach((o,n)=>{o.justPressed&&this.gamepadCallbacks.onButtonDown?.(e.index,n,o.value),o.justReleased&&this.gamepadCallbacks.onButtonUp?.(e.index,n)}),e.axes.forEach((o,n)=>{let i=t?.axes[n]??0,a=this.applyDeadzone(o),l=this.applyDeadzone(i);Math.abs(a-l)>.01&&this.gamepadCallbacks.onAxisMove?.(e.index,n,a)})}catch(o){this.log("Error in gamepad callbacks:",o)}}handleGamepadConnected(e){try{this.log(`Gamepad connected: ${e.gamepad.id} at index ${e.gamepad.index}`);let t={id:e.gamepad.id,index:e.gamepad.index,connected:!0,buttons:Array.from(e.gamepad.buttons).map(o=>({pressed:o.pressed,justPressed:!1,justReleased:!1,value:o.value,touched:o.touched??!1})),axes:Array.from(e.gamepad.axes),timestamp:e.gamepad.timestamp,mapping:e.gamepad.mapping};this.gamepads.set(e.gamepad.index,t),this.gamepadCallbacks.onGamepadConnected?.(t)}catch(t){this.log("Error handling gamepad connected:",t)}}handleGamepadDisconnected(e){try{this.log(`Gamepad disconnected at index ${e.gamepad.index}`),this.gamepads.delete(e.gamepad.index),this.previousGamepads.delete(e.gamepad.index),this.gamepadCallbacks.onGamepadDisconnected?.(e.gamepad.index)}catch(t){this.log("Error handling gamepad disconnected:",t)}}applyDeadzone(e){let t=this.gamepadConfig.axisDeadzone;return Math.abs(e)<t?0:(e>0?1:-1)*((Math.abs(e)-t)/(1-t))}cloneGamepadState(e){return{...e,buttons:e.buttons.map(t=>({...t})),axes:[...e.axes]}}};m(X,"GamepadInputs");var I=X;function ee(c,s){let e=new I(s);return c&&e.setGamepadCallbacks(c),e.start(),e}m(ee,"useGamepadInputs");var r=require("@utsp/types");var N=class N{constructor(s={}){d(this,"keyboard",null);d(this,"mouse",null);d(this,"gamepad",null);d(this,"mobile",null);d(this,"config");this.config={enableKeyboardMouse:s.enableKeyboardMouse??!0,enableGamepad:s.enableGamepad??!0,enableMobile:s.enableMobile??!1,targetElement:s.targetElement??window,mobileTargetElement:s.mobileTargetElement??s.targetElement??window,debug:s.debug??!1,keyboardConfig:{preventDefault:!1,stopPropagation:!1,...s.keyboardConfig},mouseConfig:{preventDefault:!1,stopPropagation:!1,...s.mouseConfig},mobileConfig:{preventDefault:!0,passive:!1,maxTouches:10,...s.mobileConfig}},this.initialize()}initialize(){this.config.enableKeyboardMouse&&(this.keyboard=new E(this.config.targetElement,{preventDefault:this.config.keyboardConfig?.preventDefault??!1,stopPropagation:this.config.keyboardConfig?.stopPropagation??!1,debug:this.config.debug}),this.keyboard.start(),this.mouse=new T(this.config.targetElement,{preventDefault:this.config.mouseConfig?.preventDefault??!1,stopPropagation:this.config.mouseConfig?.stopPropagation??!1,debug:this.config.debug}),this.mouse.start(),this.log("Keyboard/Mouse initialized")),this.config.enableGamepad&&(this.gamepad=new I({debug:this.config.debug}),this.gamepad.start(),this.log("Gamepad initialized")),this.config.enableMobile&&(this.config.mobileTargetElement?(this.mobile=new k(this.config.mobileTargetElement,this.config.mobileConfig),this.mobile.start(),this.log("Mobile/Touch initialized")):this.warn("Mobile input enabled but no canvas provided - mobile input will be disabled until setMobileTarget() is called"))}getButton(s,e){if(!(0,r.isButton)({device:s,input:e}))return this.warn(`Input ${e} on device ${s} is not a button`),!1;switch(s){case r.InputDeviceType.Keyboard:return this.getKeyboardButton(e);case r.InputDeviceType.Mouse:return this.getMouseButton(e);case r.InputDeviceType.Gamepad:return this.getGamepadButton(e);case r.InputDeviceType.Touch:return this.getTouchButton(e);default:return this.warn(`Unknown device type: ${s}`),!1}}getButtonJustPressed(s,e){if(!(0,r.isButton)({device:s,input:e}))return this.warn(`Input ${e} on device ${s} is not a button`),!1;switch(s){case r.InputDeviceType.Keyboard:return this.getKeyboardButtonJustPressed(e);case r.InputDeviceType.Mouse:return this.getMouseButtonJustPressed(e);case r.InputDeviceType.Gamepad:return this.getGamepadButtonJustPressed(e);case r.InputDeviceType.Touch:return this.getTouchButtonJustPressed(e);default:return this.warn(`Unknown device type: ${s}`),!1}}getButtonJustReleased(s,e){if(!(0,r.isButton)({device:s,input:e}))return this.warn(`Input ${e} on device ${s} is not a button`),!1;switch(s){case r.InputDeviceType.Keyboard:return this.getKeyboardButtonJustReleased(e);case r.InputDeviceType.Mouse:return this.getMouseButtonJustReleased(e);case r.InputDeviceType.Gamepad:return this.getGamepadButtonJustReleased(e);case r.InputDeviceType.Touch:return this.getTouchButtonJustReleased(e);default:return this.warn(`Unknown device type: ${s}`),!1}}getAxis(s,e){if(!(0,r.isAxis)({device:s,input:e}))return this.warn(`Input ${e} on device ${s} is not an axis`),0;switch(s){case r.InputDeviceType.Keyboard:return this.warn("Keyboard does not have axis inputs"),0;case r.InputDeviceType.Mouse:return this.getMouseAxis(e);case r.InputDeviceType.Gamepad:return this.getGamepadAxis(e);case r.InputDeviceType.Touch:return this.getTouchAxis(e);default:return this.warn(`Unknown device type: ${s}`),0}}getKeyboardButton(s){if(!this.keyboard)return!1;let e=r.KeyboardInput[s];return this.keyboard.isKeyPressed(e)}getKeyboardButtonJustPressed(s){if(!this.keyboard)return!1;let e=r.KeyboardInput[s];return this.keyboard.isKeyJustPressed(e)}getKeyboardButtonJustReleased(s){if(!this.keyboard)return!1;let e=r.KeyboardInput[s];return this.keyboard.isKeyJustReleased(e)}getMouseButton(s){if(!this.mouse)return!1;switch(s){case r.MouseInput.LeftButton:return this.mouse.isLeftMousePressed();case r.MouseInput.RightButton:return this.mouse.isRightMousePressed();case r.MouseInput.MiddleButton:return this.mouse.isMiddleMousePressed();default:return!1}}getMouseButtonJustPressed(s){if(!this.mouse)return!1;switch(s){case r.MouseInput.LeftButton:return this.mouse.isLeftMouseJustPressed();case r.MouseInput.RightButton:return this.mouse.isRightMouseJustPressed();case r.MouseInput.MiddleButton:return this.mouse.isMiddleMouseJustPressed();default:return!1}}getMouseButtonJustReleased(s){if(!this.mouse)return!1;switch(s){case r.MouseInput.LeftButton:return this.mouse.isLeftMouseJustReleased();case r.MouseInput.RightButton:return this.mouse.isRightMouseJustReleased();case r.MouseInput.MiddleButton:return this.mouse.isMiddleMouseJustReleased();default:return!1}}getGamepadButton(s){return!this.gamepad||s>=100?!1:this.gamepad.isButtonPressed(0,s)}getGamepadButtonJustPressed(s){return!this.gamepad||s>=100?!1:this.gamepad.isButtonJustPressed(0,s)}getGamepadButtonJustReleased(s){return!this.gamepad||s>=100?!1:this.gamepad.isButtonJustReleased(0,s)}getTouchButton(s){return this.mobile?s>=100?!1:this.mobile.isTouchActive?.(s)??!1:(console.warn("\u{1F6AB} MobileInputs not initialized!"),!1)}getTouchButtonJustPressed(s){return!this.mobile||s>=100?!1:this.mobile.isTouchJustStarted?.(s)??!1}getTouchButtonJustReleased(s){return!this.mobile||s>=100?!1:this.mobile.isTouchJustReleased?.(s)??!1}getMouseAxis(s){if(!this.mouse)return 0;switch(s){case r.MouseInput.PositionX:return this.mouse.getMousePosition().x;case r.MouseInput.PositionY:return this.mouse.getMousePosition().y;case r.MouseInput.DeltaX:return this.mouse.getMouseDelta().x;case r.MouseInput.DeltaY:return this.mouse.getMouseDelta().y;case r.MouseInput.WheelDeltaY:return this.mouse.getWheelDelta();default:return 0}}getGamepadAxis(s){if(!this.gamepad||s<100)return 0;let e=s-100;return this.gamepad.getAxis(0,e)}getTouchAxis(s){if(!this.mobile)return 0;if(s>=100&&s<200){let e=Math.floor((s-100)/2),t=(s-100)%2===0,o=this.mobile.getTouchPosition?.(e);return this.config.debug&&o&&console.warn(`\u{1F3AF} getTouchAxis(${s}) - touchIndex: ${e}, ${t?"X":"Y"}: ${t?o.x:o.y}`),o?t?o.x:o.y:0}return 0}getMousePosition(){if(!this.mouse)return null;let s=this.mouse.getMousePosition();return{x:s.x,y:s.y}}setMobileTarget(s){if(!s){this.warn("setMobileTarget() called with null/undefined canvas - mobile input will remain disabled");return}if(this.config.mobileTargetElement=s,!this.config.enableMobile){this.warn("setMobileTarget() called but mobile input is disabled in config");return}this.mobile&&this.mobile.stop(),this.mobile=new k(s,this.config.mobileConfig),this.mobile.start()}getTextInputs(){return this.keyboard?this.keyboard.getTextInputs():[]}start(){this.keyboard?.start(),this.mouse?.start(),this.log("Input router started")}stop(){this.keyboard?.stop(),this.mouse?.stop(),this.log("Input router stopped")}reset(){this.keyboard?.reset(),this.mouse?.reset(),this.log("Input router reset")}poll(){this.keyboard?.poll(),this.mouse?.poll(),this.gamepad?.poll(),this.mobile?.poll()}isListening(){return this.keyboard!==null&&this.config.enableKeyboardMouse||this.gamepad!==null&&this.config.enableGamepad||this.mobile!==null&&this.config.enableMobile}destroy(){this.keyboard?.destroy(),this.mouse?.destroy(),this.keyboard=null,this.mouse=null,this.gamepad=null,this.mobile=null,this.log("Input router destroyed")}log(s){this.config.debug&&console.warn(`[InputRouter] ${s}`)}warn(s){console.warn(`[InputRouter] ${s}`)}};m(N,"UnifiedInputRouter");var K=N;var u=require("@utsp/types");var F=class F{static collectAxisSources(s,e){let t=new Map;for(let o of s)for(let n of o.sources){if(t.has(n.sourceId))continue;let i=0,a=!1;if(n.type===u.InputDeviceType.Keyboard&&n.negativeKey!==void 0&&n.positiveKey!==void 0){let l=e.getButton(u.InputDeviceType.Keyboard,n.negativeKey);e.getButton(u.InputDeviceType.Keyboard,n.positiveKey)&&(i+=1),l&&(i-=1),a=!0}else if(n.type===u.InputDeviceType.Mouse&&n.mouseAxis!==void 0)i=e.getAxis(u.InputDeviceType.Mouse,n.mouseAxis),a=!0;else if(n.type===u.InputDeviceType.Gamepad&&n.axis!==void 0)i=e.getAxis(u.InputDeviceType.Gamepad,n.axis),a=!0;else if(n.type===u.InputDeviceType.Touch&&n.touchId!==void 0&&n.touchAxis!==void 0){let l=100+n.touchId*2,h=n.touchAxis==="x"?l:l+1;i=e.getAxis(u.InputDeviceType.Touch,h),a=!0}a&&t.set(n.sourceId,i)}return t}static collectButtonSources(s,e){let t=new Map;for(let o of s)for(let n of o.sources){if(t.has(n.sourceId))continue;let i=!1,a=!1;n.type===u.InputDeviceType.Keyboard&&n.key!==void 0?(i=e.getButton(u.InputDeviceType.Keyboard,n.key),a=!0):n.type===u.InputDeviceType.Mouse&&n.mouseButton!==void 0?(i=e.getButton(u.InputDeviceType.Mouse,n.mouseButton),a=!0):n.type===u.InputDeviceType.Gamepad&&n.button!==void 0?(i=e.getButton(u.InputDeviceType.Gamepad,n.button),a=!0):n.type===u.InputDeviceType.Touch&&n.touchButton!==void 0&&(i=e.getButton(u.InputDeviceType.Touch,n.touchButton),a=!0),a&&t.set(n.sourceId,i)}return t}static collectButtonSourcesWithTransitions(s,e){let t=new Map;for(let o of s)for(let n of o.sources){if(t.has(n.sourceId))continue;let i=!1,a=!1,l=!1,h=!1;n.type===u.InputDeviceType.Keyboard&&n.key!==void 0?(i=e.getButton(u.InputDeviceType.Keyboard,n.key),a=e.getButtonJustPressed?.(u.InputDeviceType.Keyboard,n.key)??!1,l=e.getButtonJustReleased?.(u.InputDeviceType.Keyboard,n.key)??!1,h=!0):n.type===u.InputDeviceType.Mouse&&n.mouseButton!==void 0?(i=e.getButton(u.InputDeviceType.Mouse,n.mouseButton),a=e.getButtonJustPressed?.(u.InputDeviceType.Mouse,n.mouseButton)??!1,l=e.getButtonJustReleased?.(u.InputDeviceType.Mouse,n.mouseButton)??!1,h=!0):n.type===u.InputDeviceType.Gamepad&&n.button!==void 0?(i=e.getButton(u.InputDeviceType.Gamepad,n.button),a=e.getButtonJustPressed?.(u.InputDeviceType.Gamepad,n.button)??!1,l=e.getButtonJustReleased?.(u.InputDeviceType.Gamepad,n.button)??!1,h=!0):n.type===u.InputDeviceType.Touch&&n.touchButton!==void 0&&(i=e.getButton(u.InputDeviceType.Touch,n.touchButton),a=e.getButtonJustPressed?.(u.InputDeviceType.Touch,n.touchButton)??!1,l=e.getButtonJustReleased?.(u.InputDeviceType.Touch,n.touchButton)??!1,h=!0),h&&t.set(n.sourceId,{pressed:i,justPressed:a,justReleased:l})}return t}static collectMousePosition(s,e,t,o,n){let i=s.getAxis(u.InputDeviceType.Mouse,u.MouseInput.PositionX),a=s.getAxis(u.InputDeviceType.Mouse,u.MouseInput.PositionY),l=e.getBoundingClientRect(),h=e.width/e.height,w=l.width/l.height,b,f,p,y;h>w?(b=l.width,f=l.width/h,p=0,y=(l.height-f)/2):(b=l.height*h,f=l.height,p=(l.width-b)/2,y=0);let x=n?.offsetX||0,P=n?.offsetY||0;p+=x,y+=P,b-=2*x,f-=2*P;let B=i-l.left-p,D=a-l.top-y,M=b/t,A=f/o,O=Math.floor(B/M),S=Math.floor(D/A),j=B>=0&&B<b&&D>=0&&D<f;return{x:Math.max(0,Math.min(t-1,O)),y:Math.max(0,Math.min(o-1,S)),over:j}}static collectTouchPositions(s,e,t,o,n=10,i){let a=[],l=e.getBoundingClientRect(),h=e.width/e.height,w=l.width/l.height,b,f,p,y;h>w?(b=l.width,f=l.width/h,p=0,y=(l.height-f)/2):(b=l.height*h,f=l.height,p=(l.width-b)/2,y=0);let x=i?.offsetX||0,P=i?.offsetY||0;p+=x,y+=P,b-=2*x,f-=2*P;let B=b/t,D=f/o;for(let M=0;M<n;M++){let A=M;if(s.getButton(u.InputDeviceType.Touch,A)){let S=100+M*2,j=101+M*2,te=s.getAxis(u.InputDeviceType.Touch,S),se=s.getAxis(u.InputDeviceType.Touch,j),H=te-l.left-p,J=se-l.top-y,oe=Math.floor(H/B),ne=Math.floor(J/D),ie=H>=0&&H<b&&J>=0&&J<f;a.push({id:M,x:Math.max(0,Math.min(t-1,oe)),y:Math.max(0,Math.min(o-1,ne)),over:ie})}}return a}static collectTextInputs(s){return typeof s.getTextInputs=="function"?s.getTextInputs():[]}};m(F,"InputCollector");var L=F;