import glut from 'glut' ; class WindowElement { public static setPointerCapture ( pointerId ) { console . log ( `pointer capture ${ pointerId }` ) ; } public static releasePointerCapture ( pointerId ) { console . log ( `releaswe pointer capture ${ pointerId }` ) ; } } export default class WindowEx { static events = { } ; static timerCallbackIndex = 0 ; static timerCallbackMap = { } ; static innerWidth = 640 ; static innerHeight = 480 ; static __drawFunction : any ; public static location = { href : 'file://' } ; constructor ( ) { glut . init ( ) ; glut . initWindowSize ( WindowEx . innerWidth , WindowEx . innerHeight ) ; glut . initDisplayMode ( glut . DOUBLE , glut . DEPTH , glut . RGBA ) ; glut . createWindow ( 'Cool window' ) ; glut . ignoreKeyRepeat ( true ) ; glut . display ( function ( ) { if ( WindowEx . __drawFunction ) { WindowEx . __drawFunction ( ) ; } glut . swapBuffers ( ) ; } ) ; glut . passiveMotion ( function ( x , y ) { console . log ( `passive motion: x = ${ x }, y = ${ y }` ) ; } ) ; glut . mouse ( function ( button , state , x , y ) { console . log ( `mouse: button = ${ button }, state = ${ state }, x = ${ x }, y = ${ y }` ) ; let preventedDefault = false ; const preventDefault = function ( ) { preventedDefault = true ; } ; if ( button < 3 || button > 4 ) { if ( state === 1 ) { const mouseups = WindowEx . events [ 'mouseup' ] ; if ( mouseups ) { for ( const mouseup of mouseups ) { mouseup ( { pointerId : 1 , button , clientX : x , clientY : y , preventDefault , srcElement : WindowElement } ) ; } glut . postRedisplay ( ) ; } } else { const mousedowns = WindowEx . events [ 'mousedown' ] ; if ( mousedowns ) { for ( const mousedown of mousedowns ) { mousedown ( { pointerId : 1 , button , clientX : x , clientY : y , preventDefault , srcElement : WindowElement } ) ; } glut . postRedisplay ( ) ; } } } else { const mousewheels = WindowEx . events [ 'mousewheel' ] ; if ( mousewheels ) { for ( const mousewheel of mousewheels ) { mousewheel ( { pointerId : 1 , type : 'mousewheel' , wheelDelta : button === 3 ? 0.1 : - 0.1 , button , clientX : x , clientY : y , preventDefault , srcElement : WindowElement } ) ; } glut . postRedisplay ( ) ; } } } ) ; glut . motion ( function ( x , y ) { console . log ( `motion: x = ${ x }, y = ${ y }` ) ; let preventedDefault = false ; const preventDefault = function ( ) { preventedDefault = true ; } ; const mousemoves = WindowEx . events [ 'mousemove' ] ; if ( mousemoves ) { for ( const mousemove of mousemoves ) { mousemove ( { pointerId : 1 , clientX : x , clientY : y , preventDefault , srcElement : WindowElement } ) ; } glut . postRedisplay ( ) ; } } ) ; glut . idle ( function ( ) { } ) ; glut . keyboard ( function ( k , x , y ) { console . log ( `keyboard: key = ${ k }, x = ${ x }, y = ${ y }` ) ; let preventedDefault = false ; const preventDefault = function ( ) { preventedDefault = true ; } ; const keydowns = WindowEx . events [ 'keydown' ] ; if ( keydowns ) { for ( const keydown of keydowns ) { keydown ( { pointerId : 1 , keyCode : k , clientX : x , clientY : y , preventDefault , srcElement : WindowElement } ) ; } glut . postRedisplay ( ) ; } } ) ; glut . keyboardUp ( function ( k , x , y ) { console . log ( `keyboard-up: key = ${ k }, x = ${ x }, y = ${ y }` ) ; let preventedDefault = false ; const preventDefault = function ( ) { preventedDefault = true ; } ; const keyups = WindowEx . events [ 'keyup' ] ; if ( keyups ) { for ( const keyup of keyups ) { keyup ( { pointerId : 1 , keyCode : k , clientX : x , clientY : y , preventDefault , srcElement : WindowElement } ) ; } glut . postRedisplay ( ) ; } } ) ; glut . special ( function ( k , x , y ) { console . log ( `special: key = ${ k }, x = ${ x }, y = ${ y }` ) ; let preventedDefault = false ; const preventDefault = function ( ) { preventedDefault = true ; } ; const keydowns = WindowEx . events [ 'keydown' ] ; if ( keydowns ) { for ( const keydown of keydowns ) { keydown ( { pointerId : 1 , keyCode : k - 63 , clientX : x , clientY : y , preventDefault , srcElement : WindowElement } ) ; } glut . postRedisplay ( ) ; } } ) ; glut . specialUp ( function ( k , x , y ) { console . log ( `special-up: key = ${ k }, x = ${ x }, y = ${ y }` ) ; let preventedDefault = false ; const preventDefault = function ( ) { preventedDefault = true ; } ; const keyups = WindowEx . events [ 'keyup' ] ; if ( keyups ) { for ( const keyup of keyups ) { keyup ( { pointerId : 1 , keyCode : k - 63 , clientX : x , clientY : y , preventDefault , srcElement : WindowElement } ) ; } glut . postRedisplay ( ) ; } } ) ; glut . reshape ( function ( w , h ) { WindowEx . innerWidth = w ; WindowEx . innerHeight = h ; const resizes = WindowEx . events [ 'resize' ] ; if ( resizes ) { for ( const resize of resizes ) { resize ( ) ; } glut . postRedisplay ( ) ; } } ) ; } public static postMessage ( message , targetOrigin , transfer ? ) { const messages = WindowEx . events [ 'message' ] ; if ( messages ) { for ( const messageItem of messages ) { messageItem ( message ) ; } } } public static focus ( ) { console . log ( `focus` ) ; let preventedDefault = false ; const preventDefault = function ( ) { preventedDefault = true ; } ; const focuses = WindowEx . events [ 'focus' ] ; if ( focuses ) { for ( const focusItem of focuses ) { focusItem ( { pointerId : 1 , preventDefault , srcElement : WindowElement } ) ; } } } public static addEventListener ( eventName , cb , flag ) { let listeners = this . events [ eventName ] ; if ( ! listeners ) { listeners = [ ] ; this . events [ eventName ] = listeners ; } listeners . push ( cb ) ; } public static removeEventListener ( eventName , cb ) { const listeners = this . events [ eventName ] ; if ( listeners ) { listeners . remove ( cb ) ; } } private static timerCallback ( value ) { const cb = WindowEx . timerCallbackMap [ value ] ; if ( cb === WindowEx . __drawFunction ) { glut . postRedisplay ( ) ; return ; } cb ( ) ; } public static setTimeout ( funct , millisec ) { if ( funct ) { if ( millisec === 16 ) { WindowEx . __drawFunction = funct ; } glut . timer ( millisec , WindowEx . timerCallback , ++ WindowEx . timerCallbackIndex ) ; WindowEx . timerCallbackMap [ WindowEx . timerCallbackIndex ] = funct ; return WindowEx . timerCallbackIndex ; } } public static setImmediate ( funct ) { if ( funct ) { glut . timer ( 0 , WindowEx . timerCallback , ++ WindowEx . timerCallbackIndex ) ; WindowEx . timerCallbackMap [ WindowEx . timerCallbackIndex ] = funct ; return WindowEx . timerCallbackIndex ; } } public static loop ( ) { glut . mainLoop ( ) ; } }