import { World } from '../world/World' ; import { IInputReceiver } from '../interfaces/IInputReceiver' ; import { EntityType } from '../enums/EntityType' ; import { IUpdatable } from '../interfaces/IUpdatable' ; export class InputManager implements IUpdatable { public updateOrder : number = 3 ; public world : World ; public domElement : any ; public pointerLock : any ; public isLocked : boolean ; public inputReceiver : IInputReceiver ; public boundOnMouseDown : ( evt : any ) => void ; public boundOnMouseMove : ( evt : any ) => void ; public boundOnMouseUp : ( evt : any ) => void ; public boundOnMouseWheelMove : ( evt : any ) => void ; public boundOnPointerlockChange : ( evt : any ) => void ; public boundOnPointerlockError : ( evt : any ) => void ; public boundOnKeyDown : ( evt : any ) => void ; public boundOnKeyUp : ( evt : any ) => void ; constructor ( world , domElement ) { this . world = world ; this . pointerLock = world . params . Pointer_Lock ; this . domElement = domElement || document . body ; this . isLocked = false ; this . boundOnMouseDown = ( evt ) => this . onMouseDown ( evt ) ; this . boundOnMouseMove = ( evt ) => this . onMouseMove ( evt ) ; this . boundOnMouseUp = ( evt ) => this . onMouseUp ( evt ) ; this . boundOnMouseWheelMove = ( evt ) => this . onMouseWheelMove ( evt ) ; this . boundOnPointerlockChange = ( evt ) => this . onPointerlockChange ( evt ) ; this . boundOnPointerlockError = ( evt ) => this . onPointerlockError ( evt ) ; this . boundOnKeyDown = ( evt ) => this . onKeyDown ( evt ) ; this . boundOnKeyUp = ( evt ) => this . onKeyUp ( evt ) ; this . domElement . addEventListener ( 'mousedown' , this . boundOnMouseDown , false ) ; document . addEventListener ( 'wheel' , this . boundOnMouseWheelMove , false ) ; document . addEventListener ( 'pointerlockchange' , this . boundOnPointerlockChange , false ) ; document . addEventListener ( 'pointerlockerror' , this . boundOnPointerlockError , false ) ; document . addEventListener ( 'keydown' , this . boundOnKeyDown , false ) ; document . addEventListener ( 'keyup' , this . boundOnKeyUp , false ) ; world . registerUpdatable ( this ) ; } public update ( timestep , unscaledTimeStep ) { if ( this . inputReceiver === undefined && this . world !== undefined && this . world . cameraOperator !== undefined ) { this . setInputReceiver ( this . world . cameraOperator ) ; } this . inputReceiver ?. inputReceiverUpdate ( unscaledTimeStep ) ; } public setInputReceiver ( receiver ) { this . inputReceiver = receiver ; this . inputReceiver . inputReceiverInit ( ) ; } public setPointerLock ( enabled ) { this . pointerLock = enabled ; } public onPointerlockChange ( event ) { if ( document . pointerLockElement === this . domElement ) { this . domElement . addEventListener ( 'mousemove' , this . boundOnMouseMove , false ) ; this . domElement . addEventListener ( 'mouseup' , this . boundOnMouseUp , false ) ; this . isLocked = true ; } else { this . domElement . removeEventListener ( 'mousemove' , this . boundOnMouseMove , false ) ; this . domElement . removeEventListener ( 'mouseup' , this . boundOnMouseUp , false ) ; this . isLocked = false ; } } public onPointerlockError ( event ) { console . error ( 'PointerLockControls: Unable to use Pointer Lock API' ) ; } public onMouseDown ( event ) { if ( this . pointerLock ) { this . domElement . requestPointerLock ( ) ; } else { this . domElement . addEventListener ( 'mousemove' , this . boundOnMouseMove , false ) ; this . domElement . addEventListener ( 'mouseup' , this . boundOnMouseUp , false ) ; } if ( this . inputReceiver !== undefined ) { this . inputReceiver . handleMouseButton ( event , 'mouse' + event . button , true ) ; } } public onMouseMove ( event ) { if ( this . inputReceiver !== undefined ) { this . inputReceiver . handleMouseMove ( event , event . movementX , event . movementY ) ; } } public onMouseUp ( event ) { if ( ! this . pointerLock ) { this . domElement . removeEventListener ( 'mousemove' , this . boundOnMouseMove , false ) ; this . domElement . removeEventListener ( 'mouseup' , this . boundOnMouseUp , false ) ; } if ( this . inputReceiver !== undefined ) { this . inputReceiver . handleMouseButton ( event , 'mouse' + event . button , false ) ; } } public onKeyDown ( event ) { if ( this . inputReceiver !== undefined ) { this . inputReceiver . handleKeyboardEvent ( event , event . code , true ) ; } } public onKeyUp ( event ) { if ( this . inputReceiver !== undefined ) { this . inputReceiver . handleKeyboardEvent ( event , event . code , false ) ; } } public onMouseWheelMove ( event ) { if ( this . inputReceiver !== undefined ) { this . inputReceiver . handleMouseWheel ( event , event . deltaY ) ; } } }