import { EventDispatcher } from "./EventDispatcher" ; import Event from "./Event" ; export class SceneKeyInteractions { public static create ( ) { return new SceneKeyInteractions ( ) ; } private static EVENT_KEY_DOWN = "keydown" ; private static KEY_CODE_R = 82 ; private static KEY_CODE_L = 76 ; private _onResetCameraEvent : EventDispatcher < void > = new EventDispatcher < void > ( ) ; private _onToggleLegendEvent : EventDispatcher < void > = new EventDispatcher < void > ( ) ; private active : boolean ; private constructor ( active = false ) { window . addEventListener ( SceneKeyInteractions . EVENT_KEY_DOWN , ( event ) => { this . handleKeyDown ( event as KeyboardEvent ) ; } ) ; this . active = active ; } public destroy ( ) { window . removeEventListener ( SceneKeyInteractions . EVENT_KEY_DOWN , ( event ) => { this . handleKeyDown ( event as KeyboardEvent ) ; } ) ; } public addResetCameraEventListener ( callback ) { this . _onResetCameraEvent . addEventListener ( callback ) ; } public addToggleLegendEventListener ( callback ) { this . _onToggleLegendEvent . addEventListener ( callback ) ; } public halt ( ) { this . active = false ; } public resume ( ) { this . active = true ; } public handleKeyDown ( event ) { if ( ! this . active ) { return ; } switch ( event . keyCode ) { case SceneKeyInteractions . KEY_CODE_R : this . _onResetCameraEvent . dispatchEvent ( new Event < void > ( undefined ) ) ; break ; case SceneKeyInteractions . KEY_CODE_L : this . _onToggleLegendEvent . dispatchEvent ( new Event < void > ( undefined ) ) ; break ; default : } } }