import keyboardJS from 'keyboardjs' ; function initActions ( canvas ) { keyboardJS . setContext ( 'editor' ) ; keyboardJS . watch ( canvas ) ; canvas . addEventListener ( 'mouseover' , ( ) => canvas . focus ( ) ) ; canvas . addEventListener ( 'blur' , ( ) => { keyboardJS . releaseAllKeys ( ) ; } ) ; keyboardJS . _locale . bindKeyCode ( 300 , [ 'lclick' ] ) ; keyboardJS . _locale . bindKeyCode ( 301 , [ 'mclick' ] ) ; keyboardJS . _locale . bindKeyCode ( 302 , [ 'rclick' ] ) ; keyboardJS . _locale . bindKeyCode ( 303 , [ 'wheelNeg' ] ) ; keyboardJS . _locale . bindKeyCode ( 304 , [ 'wheelPos' ] ) ; canvas . addEventListener ( 'mousedown' , e => keyboardJS . pressKey ( e . button + 300 , e ) ) ; window . addEventListener ( 'mouseup' , e => keyboardJS . releaseKey ( e . button + 300 , e ) ) ; canvas . addEventListener ( 'wheel' , e => { e . preventDefault ( ) ; keyboardJS . pressKey ( Math . sign ( - e . deltaY ) === 1 ? 303 : 304 , e ) ; keyboardJS . releaseKey ( Math . sign ( - e . deltaY ) === 1 ? 303 : 304 , e ) ; } ) ; } param cb function passtroughAllEvents ( cb ) { keyboardJS . setContext ( 'passtrough' ) ; const callback = ( e ) : void => { const stop = cb ( e ) ; if ( stop ) { keyboardJS . unbind ( undefined , callback ) ; keyboardJS . setContext ( 'editor' ) ; } } ; keyboardJS . bind ( undefined , callback ) ; } class Action { public readonly name : string ; private readonly defaultKeyCombo : string ; private m_active = true ; private m_keyCombo : string ; private handlers : { press : ( e ? : keyboardJS . KeyEvent ) => void ; release : ( e ? : keyboardJS . KeyEvent ) => void ; } [ ] = [ ] ; private _pressed = false ; public constructor ( name , defaultKeyCombo ) { this . name = name ; this . defaultKeyCombo = defaultKeyCombo ; this . m_keyCombo = defaultKeyCombo ; this . bind ( { press : ( ) => { this . _pressed = true ; } , release : ( ) => { this . _pressed = false ; } , } ) ; } public get prettyName ( ) : string { return this . name . split ( /(?=[A-Z1-9])/ ) . join ( ' ' ) . replace ( /(\b\w)/ , c => c . toUpperCase ( ) ) ; } private get active ( ) : boolean { return this . m_active ; } private set active ( value ) { for ( const h of this . handlers ) { if ( value ) { keyboardJS . bind ( this . keyCombo , h . press , h . release ) ; } else { keyboardJS . unbind ( this . keyCombo , h . press , h . release ) ; } } this . m_active = value ; } public get keyCombo ( ) : string { return this . m_keyCombo ; } public set keyCombo ( value ) { if ( ! this . active && value . length !== 0 ) { this . active = true ; } if ( value . length === 0 ) { this . active = false ; } else { for ( const h of this . handlers ) { keyboardJS . unbind ( this . keyCombo , h . press , h . release ) ; keyboardJS . bind ( value , h . press , h . release ) ; } } this . m_keyCombo = value ; } public get pressed ( ) : boolean { return this . _pressed ; } public get usesDefaultKeyCombo ( ) : boolean { return this . keyCombo === this . defaultKeyCombo ; } public resetKeyCombo ( ) { this . keyCombo = this . defaultKeyCombo ; } public bind ( opts ) { if ( opts . press === undefined && opts . release === undefined ) return ; opts . once = opts . once || false ; opts . repeat = opts . repeat || false ; let pressHandlerRanOnce = false ; const PRESS = opts . press ? ( e ) => { if ( ! opts . repeat && pressHandlerRanOnce ) return ; pressHandlerRanOnce = true ; if ( e && e . preventDefault ) { e . preventDefault ( ) ; } opts . press ( e ) ; } : undefined ; const RELEASE = opts . release ? ( e ) => { pressHandlerRanOnce = false ; if ( opts . once ) { keyboardJS . unbind ( this . keyCombo , handlerData . press , handlerData . release ) ; this . handlers = this . handlers . filter ( h => h !== handlerData ) ; } opts . release ( e ) ; } : ( ) => { pressHandlerRanOnce = false ; } ; if ( this . active ) { keyboardJS . bind ( this . keyCombo , PRESS , RELEASE ) ; } const handlerData = { press : PRESS , release : RELEASE , } ; this . handlers . push ( handlerData ) ; } public call ( ) { for ( const h of this . handlers ) h . press ( ) ; } } const actions = new Map < string , Action > ( ) ; function registerAction ( name , keyCombo ) { const action = new Action ( name , keyCombo ) ; actions . set ( name , action ) ; return action ; } function callAction ( name ) { const action = actions . get ( name ) ; if ( action ) { action . call ( ) ; } } function isActionActive ( name ) { const action = actions . get ( name ) ; if ( action ) return action . pressed ; return false ; } function forEachAction ( cb ) { for ( const [ name , action ] of actions ) { cb ( action , name ) ; } } function resetKeybinds ( ) { for ( const [ , action ] of actions ) { action . resetKeyCombo ( ) ; } } function importKeybinds ( keybinds ) { if ( ! keybinds ) return ; for ( const [ name , action ] of actions ) { if ( keybinds [ name ] !== undefined ) { action . keyCombo = keybinds [ name ] ; } } } function exportKeybinds ( changedOnly = true ) { const changedKeybinds = { } ; for ( const [ name , action ] of actions ) { if ( ! changedOnly || ! action . usesDefaultKeyCombo ) { changedKeybinds [ name ] = action . keyCombo ; } } return changedKeybinds ; } export { initActions , passtroughAllEvents , registerAction , callAction , isActionActive , forEachAction , resetKeybinds , importKeybinds , exportKeybinds , } ;