namespace NT { /** The message code for mouse down events. */ export const MESSAGE_MOUSE_DOWN = "MOUSE_DOWN" ; /** The message code for mouse up events. */ export const MESSAGE_MOUSE_UP = "MOUSE_UP" ; /** Defines key codes for keyboard keys. */ export enum Keys { /** The left arrow key */ LEFT = 37 , /** The up arrow key */ UP = 38 , /** The right arrow key */ RIGHT = 39 , /** The down arrow key */ DOWN = 40 } /** Contains mouse state data to be used throughout the engine. */ export class MouseContext { /** Indicates if the left mouse button is down. Default: false. */ public leftDown : boolean ; /** Indicates if the right mouse button is down. Default: false. */ public rightDown : boolean ; /** The mouse position. */ public position : Vector2 ; public constructor ( leftDown = false , rightDown = false , position ) { this . leftDown = leftDown ; this . rightDown = rightDown ; this . position = position ; } } /** Manages all input from devices such as the mouse and keyboard. */ export class InputManager { private static _keys : boolean [ ] = [ ] ; private static _previousMouseX : number ; private static _previousMouseY : number ; private static _mouseX : number ; private static _mouseY : number ; private static _leftDown : boolean = false ; private static _rightDown : boolean = false ; private static _resolutionScale : Vector2 = Vector2 . one ; param viewport public static Initialize ( viewport ) { for ( let i = 0 ; i < 255 ; ++ i ) { InputManager . _keys [ i ] = false ; } window . addEventListener ( "keydown" , InputManager . onKeyDown ) ; window . addEventListener ( "keyup" , InputManager . onKeyUp ) ; viewport . addEventListener ( "mousemove" , InputManager . onMouseMove ) ; viewport . addEventListener ( "mousedown" , InputManager . onMouseDown ) ; viewport . addEventListener ( "mouseup" , InputManager . onMouseUp ) ; } param key public static isKeyDown ( key ) { return InputManager . _keys [ key ] ; } /** Gets the current mouse position. */ public static getMousePosition ( ) { return new Vector2 ( this . _mouseX , this . _mouseY ) ; } /** Gets the current mouse position. */ public static getPreviousMousePosition ( ) { return new Vector2 ( this . _previousMouseX , this . _previousMouseY ) ; } param scale public static setResolutionScale ( scale ) { InputManager . _resolutionScale . copyFrom ( scale ) ; } private static onKeyDown ( event ) { InputManager . _keys [ event . keyCode ] = true ; return true ; } private static onKeyUp ( event ) { InputManager . _keys [ event . keyCode ] = false ; return true ; } private static onMouseMove ( event ) { InputManager . _previousMouseX = InputManager . _mouseX ; InputManager . _previousMouseY = InputManager . _mouseY ; let rect = ( event . target as HTMLElement ) . getBoundingClientRect ( ) ; InputManager . _mouseX = ( event . clientX - Math . round ( rect . left ) ) * ( 1 / InputManager . _resolutionScale . x ) ; InputManager . _mouseY = ( event . clientY - Math . round ( rect . top ) ) * ( 1 / InputManager . _resolutionScale . y ) ; } private static onMouseDown ( event ) { if ( event . button === 0 ) { this . _leftDown = true ; } else if ( event . button === 2 ) { this . _rightDown = true ; } Message . send ( MESSAGE_MOUSE_DOWN , this , new MouseContext ( InputManager . _leftDown , InputManager . _rightDown , InputManager . getMousePosition ( ) ) ) ; } private static onMouseUp ( event ) { if ( event . button === 0 ) { this . _leftDown = false ; } else if ( event . button === 2 ) { this . _rightDown = false ; } Message . send ( MESSAGE_MOUSE_UP , this , new MouseContext ( InputManager . _leftDown , InputManager . _rightDown , InputManager . getMousePosition ( ) ) ) ; } } }