import { BaseConsoleButton } from "../BaseConsoleButton" ; import { InputManager , InputManagerEvent , InputManagerEventData } from "flibs/dist/index" ; import { KeyboardTools , StringTools } from "fcore/dist/index" ; import { CaptuerKeyButtonEvent } from "./CaptureKeyButtonEvent" ; import { FC } from "../../FC" ; export class CaptureKeyButton extends BaseConsoleButton { private captureKey : string ; private captureCode : number ; private _isClicked : boolean ; constructor ( ) { super ( ) ; } protected construction ( ) { super . construction ( ) ; } protected addListeners ( ) { super . addListeners ( ) ; this . eventListenerHelper . addEventListener ( InputManager . instance , InputManagerEvent . KEY_PRESS , this . onKeyPress ) ; } protected onClick ( ) { super . onClick ( ) ; this . isClicked = ! this . isClicked ; } protected onKeyPress ( data ) { if ( this . isClicked ) { this . isClicked = false ; this . captureCode = KeyboardTools . getCharCodeFromKeyPressEvent ( data . nativeEvent ) ; this . captureKey = KeyboardTools . getCharFromKeyPressEvent ( data . nativeEvent ) ; this . commitData ( ) ; } else if ( this . captureCode ) { if ( KeyboardTools . getCharCodeFromKeyPressEvent ( data . nativeEvent ) == this . captureCode ) { this . dispatchEvent ( CaptuerKeyButtonEvent . CAPTURE_KEY_PRESS ) ; } } } protected commitData ( ) { super . commitData ( ) ; if ( this . isClicked ) { this . label = FC . config . localization . captureKeyBtnPressedLabel ; } else if ( this . captureKey ) { this . label = StringTools . substituteList ( FC . config . localization . captureKeyBtnNormalLabel , this . captureKey ) ; } else { this . label = StringTools . substituteList ( FC . config . localization . captureKeyBtnNormalLabel , FC . config . localization . captureKeyBtnNoKeyHelpText ) ; } } protected arrange ( ) { super . arrange ( ) ; } get isClicked ( ) : boolean { return this . _isClicked ; } set isClicked ( value ) { if ( value == this . isClicked ) { return ; } this . _isClicked = value ; this . commitData ( ) ; } }