import { ControllerEvent } from "./ControllerEvent" ; import { ControllerFamily , ControllerSpriteMap } from "./ControllerFamily" ; import { GamepadStyle } from "./GamepadStyle" ; import { Signal } from "../Signal" ; /** Symbol to identify the current/active controller family */ const currentControllerFamilySymbol = Symbol ( "currentControllerFamily" ) ; export class ControllerManager { private static readonly INSTANCE = new ControllerManager ( ) ; public static getInstance ( ) { return ControllerManager . INSTANCE ; } public readonly onButtonDown = new Signal < ControllerEvent > ( ) ; public readonly onButtonUp = new Signal < ControllerEvent > ( ) ; public readonly onButtonPress = new Signal < ControllerEvent > ( ) ; public readonly onControllerFamilyChange = new Signal < ControllerFamily > ( ) ; public selectedGamepadStyle = GamepadStyle . XBOX ; private [ currentControllerFamilySymbol ] : ControllerFamily ; private constructor ( initialControllerFamily = ControllerFamily . KEYBOARD ) { this . currentControllerFamily = initialControllerFamily ; this . onButtonDown . connect ( e => { if ( this . currentControllerFamily !== e . controllerFamily ) { this . currentControllerFamily = e . controllerFamily ; } } ) ; } public set currentControllerFamily ( controllerFamily ) { if ( controllerFamily !== this [ currentControllerFamilySymbol ] ) { this [ currentControllerFamilySymbol ] = controllerFamily ; this . onControllerFamilyChange . emit ( controllerFamily ) ; } } /**
     * Returns the current (a.k.a. most recently used!) controller family.
     * Can be used to determine which tooltips (gamepad buttons or keyboard indicators) to show.
     */ public get currentControllerFamily ( ) : ControllerFamily { return this [ currentControllerFamilySymbol ] ; } public toggleSelectedGamepadStyle ( ) { this . selectedGamepadStyle = this . selectedGamepadStyle === GamepadStyle . XBOX ? GamepadStyle . PLAYSTATION : GamepadStyle . XBOX ; } public get controllerSprite ( ) : ControllerSpriteMap { if ( this . currentControllerFamily === ControllerFamily . GAMEPAD ) { switch ( ControllerManager . getInstance ( ) . selectedGamepadStyle ) { case GamepadStyle . PLAYSTATION : return ControllerSpriteMap . PLAYSTATION ; case GamepadStyle . XBOX : return ControllerSpriteMap . XBOX ; } } return ControllerSpriteMap . KEYBOARD ; } }