@@ -3,7 +3,14 @@ import * as THREE from "three";
33import { EventEmitter } from "@posva/event-emitter" ;
44
55// Import Internal Dependencies
6- import * as targets from "./targets/index.js" ;
6+ import * as sources from "./targets/index.js" ;
7+ import {
8+ BrowserWindowAdapter ,
9+ type WindowAdapter
10+ } from "../adapters/index.js" ;
11+ import type {
12+ InputControl
13+ } from "./types.js" ;
714
815export type { MouseEventButton } from "./targets/Mouse.class.js" ;
916
@@ -16,24 +23,26 @@ export interface InputOptions {
1623 * @default false
1724 */
1825 enableOnExit ?: boolean ;
26+ windowAdapter ?: WindowAdapter ;
1927}
2028
2129export type InputMouseAction =
2230 | number
23- | keyof typeof targets . MouseEventButton
31+ | keyof typeof sources . MouseEventButton
2432 | "ANY"
2533 | "NONE" ;
2634
2735export type InputKeyboardAction = string | "ANY" | "NONE" ;
2836
2937export class Input extends EventEmitter < InputEvents > {
3038 #canvas: HTMLCanvasElement ;
39+ #windowAdapter: WindowAdapter ;
3140
32- mouse : targets . Mouse ;
33- touchpad : targets . Touchpad ;
34- gamepad : targets . Gamepad ;
35- fullscreen : targets . Fullscreen ;
36- keyboard : targets . Keyboard ;
41+ mouse : sources . Mouse ;
42+ touchpad : sources . Touchpad ;
43+ gamepad : sources . Gamepad ;
44+ fullscreen : sources . Fullscreen ;
45+ keyboard : sources . Keyboard ;
3746
3847 exited = false ;
3948
@@ -43,26 +52,36 @@ export class Input extends EventEmitter<InputEvents> {
4352 ) {
4453 super ( ) ;
4554 const {
46- enableOnExit = false
55+ enableOnExit = false ,
56+ windowAdapter = new BrowserWindowAdapter ( )
4757 } = options ;
4858
4959 this . #canvas = canvas ;
50- const fullscreen = new targets . Fullscreen ( canvas ) ;
51- this . mouse = new targets . Mouse ( canvas , {
60+ this . #windowAdapter = windowAdapter ;
61+ const fullscreen = new sources . Fullscreen ( {
62+ canvas
63+ } ) ;
64+ this . mouse = new sources . Mouse ( {
65+ canvas,
5266 mouseDownCallback : ( ) => fullscreen . onMouseDown ( ) ,
5367 mouseUpCallback : ( ) => fullscreen . onMouseUp ( )
5468 } ) ;
5569 this . fullscreen = fullscreen ;
56- this . touchpad = new targets . Touchpad ( this . mouse ) ;
57- this . gamepad = new targets . Gamepad ( ) ;
58- this . keyboard = new targets . Keyboard ( ) ;
70+ this . touchpad = new sources . Touchpad ( {
71+ canvas,
72+ mouse : this . mouse
73+ } ) ;
74+ this . gamepad = new sources . Gamepad ( {
75+ navigatorAdapter : this . #windowAdapter. navigator
76+ } ) ;
77+ this . keyboard = new sources . Keyboard ( ) ;
5978
6079 if ( enableOnExit ) {
61- window . onbeforeunload = this . doExitCallback ;
80+ this . #windowAdapter . onbeforeunload = this . doExitCallback ;
6281 }
6382 }
6483
65- #targets ( ) {
84+ #sourceInputs ( ) : InputControl [ ] {
6685 return [
6786 this . mouse ,
6887 this . touchpad ,
@@ -72,17 +91,15 @@ export class Input extends EventEmitter<InputEvents> {
7291 }
7392
7493 connect ( ) {
75- this . #targets( )
76- . forEach ( ( subscriber ) => subscriber . connect ( ) ) ;
77- this . fullscreen ?. connect ( ) ;
78- window . addEventListener ( "blur" , this . onBlur ) ;
94+ [ ...this . #sourceInputs( ) , this . fullscreen ]
95+ . forEach ( ( subscriber ) => subscriber . connect ?.( ) ) ;
96+ this . #windowAdapter. addEventListener ( "blur" , this . onBlur ) ;
7997 }
8098
8199 disconnect ( ) {
82- this . #targets( )
83- . forEach ( ( subscriber ) => subscriber . disconnect ( ) ) ;
84- this . fullscreen ?. disconnect ( ) ;
85- window . removeEventListener ( "blur" , this . onBlur ) ;
100+ [ ...this . #sourceInputs( ) , this . fullscreen ]
101+ . forEach ( ( subscriber ) => subscriber . disconnect ?.( ) ) ;
102+ this . #windowAdapter. removeEventListener ( "blur" , this . onBlur ) ;
86103 }
87104
88105 enterFullscreen ( ) {
@@ -94,8 +111,8 @@ export class Input extends EventEmitter<InputEvents> {
94111 }
95112
96113 update ( ) {
97- this . #targets ( )
98- . forEach ( ( subscriber ) => subscriber ? .update ( ) ) ;
114+ this . #sourceInputs ( )
115+ . forEach ( ( subscriber ) => subscriber . update ( ) ) ;
99116 }
100117
101118 getScreenSize ( ) {
@@ -150,7 +167,7 @@ export class Input extends EventEmitter<InputEvents> {
150167 return this . mouse . buttonsDown . length === 0 ;
151168 }
152169
153- const index = typeof action === "number" ? action : targets . MouseEventButton [ action ] ;
170+ const index = typeof action === "number" ? action : sources . MouseEventButton [ action ] ;
154171
155172 return this . mouse . buttonsDown [ index ] ;
156173 }
@@ -165,7 +182,7 @@ export class Input extends EventEmitter<InputEvents> {
165182 return this . mouse . buttons . every ( ( button ) => ! button . wasJustReleased ) ;
166183 }
167184
168- const index = typeof action === "number" ? action : targets . MouseEventButton [ action ] ;
185+ const index = typeof action === "number" ? action : sources . MouseEventButton [ action ] ;
169186
170187 return this . mouse . buttons [ index ] ?. wasJustReleased ?? false ;
171188 }
@@ -179,7 +196,7 @@ export class Input extends EventEmitter<InputEvents> {
179196 if ( action === "NONE" ) {
180197 return this . mouse . buttons . every ( ( button ) => ! button . wasJustPressed ) ;
181198 }
182- const index = typeof action === "number" ? action : targets . MouseEventButton [ action ] ;
199+ const index = typeof action === "number" ? action : sources . MouseEventButton [ action ] ;
183200
184201 return this . mouse . buttons [ index ] ?. wasJustPressed ?? false ;
185202 }
@@ -232,7 +249,7 @@ export class Input extends EventEmitter<InputEvents> {
232249 vibrate (
233250 pattern : VibratePattern
234251 ) : void {
235- window . navigator . vibrate ( pattern ) ;
252+ this . #windowAdapter . navigator . vibrate ( pattern ) ;
236253 }
237254
238255 isKeyDown (
@@ -392,7 +409,7 @@ export class Input extends EventEmitter<InputEvents> {
392409 }
393410
394411 private onBlur = ( ) => {
395- this . #targets ( )
412+ this . #sourceInputs ( )
396413 . forEach ( ( subscriber ) => subscriber . reset ( ) ) ;
397414 } ;
398415
0 commit comments