app.monkey2 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. #Import "native/async.cpp"
  2. Namespace mojo.app
  3. '#Import "assets/Roboto-Regular.ttf@/mojo"
  4. '#Import "assets/RobotoMono-Regular.ttf@/mojo"
  5. #rem monkeydoc The global AppInstance instance.
  6. #end
  7. Global App:AppInstance
  8. #rem monkeydoc @hidden
  9. #end
  10. Struct DisplayMode
  11. Field width:Int
  12. Field height:Int
  13. Field hertz:Int
  14. End
  15. #rem monkeydoc The AppInstance class.
  16. The AppInstance class is mainly reponsible for running the app 'event loop', but also provides several utility functions for managing
  17. the application.
  18. A global instance of the AppInstance class is stored in the [[App]] global variable, so you can use any member of the AppInstance simply
  19. by prefixing it with 'App.', eg: App.MilliSecs
  20. #end
  21. Class AppInstance
  22. #rem monkeydoc Idle signal.
  23. Invoked when the app becomes idle.
  24. This is reset to null after being invoked.
  25. #end
  26. Field Idle:Void()
  27. #rem monkeydoc @hidden
  28. #end
  29. Field NextIdle:Void()
  30. #rem monkeydoc @hidden
  31. #end
  32. Field ThemeChanged:Void()
  33. #rem monkeydoc Invoked when app is activated.
  34. #end
  35. Field Activated:Void()
  36. #rem monkeydoc Invoked when app is deactivated.
  37. #end
  38. Field Deactivated:Void()
  39. #rem monkeydoc Key event filter.
  40. To prevent the event from being sent to a view, a filter can eat the event using [[Event.Eat]].
  41. Functions should check if the event has already been 'eaten' by checking the event's [[Event.Eaten]] property before processing the event.
  42. #end
  43. Field KeyEventFilter:Void( event:KeyEvent )
  44. #rem monkeydoc MouseEvent filter.
  45. To prevent the event from being sent to a view, a filter can eat the event using [[Event.Eat]].
  46. Functions should check if the event has already been 'eaten' by checking the event's [[Event.Eaten]] property before processing the event.
  47. #end
  48. Field MouseEventFilter:Void( event:MouseEvent )
  49. #rem monkeydoc Create a new app instance.
  50. #end
  51. Method New( config:StringMap<String> =Null )
  52. App=Self
  53. If Not config config=New StringMap<String>
  54. _config=config
  55. #if __TARGET__="android"
  56. _touchMouse=True
  57. #endif
  58. SDL_Init( SDL_INIT_EVERYTHING & ~SDL_INIT_AUDIO )
  59. Keyboard.Init()
  60. Mouse.Init()
  61. Audio.Init()
  62. #if __TARGET__="android"
  63. SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION,2 )
  64. SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION,0 )
  65. SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK,SDL_GL_CONTEXT_PROFILE_ES )
  66. #endif
  67. #if __TARGET__="desktop" and __HOSTOS__="windows"
  68. Local gl_major:=Int( GetConfig( "GL_context_major_version",-1 ) )
  69. Local gl_minor:=Int( GetConfig( "GL_context_major_version",-1 ) )
  70. Local gl_profile:Int
  71. Select GetConfig( "GL_context_profile","es" )
  72. Case "core"
  73. gl_profile=SDL_GL_CONTEXT_PROFILE_CORE
  74. Case "compatibility"
  75. gl_profile=SDL_GL_CONTEXT_PROFILE_COMPATIBILITY
  76. Default
  77. gl_profile=SDL_GL_CONTEXT_PROFILE_ES
  78. If gl_major=-1 gl_major=2
  79. If gl_minor=-1 gl_minor=0
  80. End
  81. SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK,gl_profile )
  82. If gl_major<>-1 SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION,gl_major )
  83. If gl_minor<>-1 SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION,gl_minor )
  84. #Endif
  85. #if __TARGET__="desktop"
  86. SDL_GL_SetAttribute( SDL_GL_SHARE_WITH_CURRENT_CONTEXT,1 )
  87. SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER,1 )
  88. SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE,Int( GetConfig( "GL_depth_buffer_enabled",0 ) ) )
  89. SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE,Int( GetConfig( "GL_stencil_buffer_enabled",0 ) ) )
  90. 'create dummy window/context
  91. Local _sdlWindow:=SDL_CreateWindow( "<dummy>",0,0,0,0,SDL_WINDOW_HIDDEN|SDL_WINDOW_OPENGL )
  92. Assert( _sdlWindow,"FATAL ERROR: SDL_CreateWindow failed" )
  93. Local _sdlGLContext:=SDL_GL_CreateContext( _sdlWindow )
  94. Assert( _sdlGLContext,"FATAL ERROR: SDL_GL_CreateContext failed" )
  95. SDL_GL_MakeCurrent( _sdlWindow,_sdlGLContext )
  96. #Endif
  97. _defaultFont=Font.Open( "asset::fonts/DejaVuSans.ttf",16 )
  98. _theme=Theme.Load( GetConfig( "initialTheme","asset::themes/default.json" ) )
  99. End
  100. #rem monkeydoc Fallback font.
  101. #end
  102. Property DefaultFont:Font()
  103. Return _defaultFont
  104. End
  105. #rem monkeydoc The current theme.
  106. #end
  107. Property Theme:Theme()
  108. Return _theme
  109. Setter( theme:Theme )
  110. _theme=theme
  111. ThemeChanged()
  112. End
  113. #rem monkeydoc True if clipboard text is empty.
  114. This is faster than checking whether [[ClipboardText]] returns an empty string.
  115. #end
  116. Property ClipboardTextEmpty:Bool()
  117. Return SDL_HasClipboardText()=SDL_FALSE
  118. End
  119. #rem monkeydoc Clipboard text.
  120. #end
  121. Property ClipboardText:String()
  122. If SDL_HasClipboardText()=SDL_FALSE Return ""
  123. Local p:=SDL_GetClipboardText()
  124. Local str:=String.FromUtf8String( p )
  125. 'fix windows eols
  126. str=str.Replace( "~r~n","~n" )
  127. str=str.Replace( "~r","~n" )
  128. SDL_free( p )
  129. Return str
  130. Setter( text:String )
  131. SDL_SetClipboardText( text )
  132. End
  133. #rem monkeydoc The current key view.
  134. The key view is the view key events are sent to.
  135. #end
  136. Property KeyView:View()
  137. If IsActive( _keyView ) Return _keyView
  138. If _modalView Return _modalView
  139. Return _activeWindow
  140. Setter( keyView:View )
  141. _keyView=keyView
  142. End
  143. #rem monkeydoc The current mouse view.
  144. The mouse view is the view that the mouse is currently 'dragging'.
  145. #end
  146. Property MouseView:View()
  147. Return _mouseView
  148. End
  149. #rem monkeydoc The current hover view.
  150. The hover view is the view that the mouse is currently 'hovering' over.
  151. #end
  152. Property HoverView:View()
  153. Return _hoverView
  154. End
  155. #rem monkeydoc The desktop size
  156. #end
  157. Property DesktopSize:Vec2i()
  158. #If __TARGET__="emscripten"
  159. Return New Vec2i( 1280,960 )
  160. #Else
  161. Local dm:SDL_DisplayMode
  162. If SDL_GetDesktopDisplayMode( 0,Varptr dm ) Return New Vec2i
  163. Return New Vec2i( dm.w,dm.h )
  164. #Endif
  165. End
  166. #rem monkeydoc True if app is active.
  167. An app is active if any of its windows has system input focus.
  168. #end
  169. Property Active:Bool()
  170. Return _active
  171. End
  172. #rem monkeydoc The currently active window.
  173. The active window is the window that has system input focus.
  174. #end
  175. Property ActiveWindow:Window()
  176. If Not _activeWindow
  177. Local windows:=Window.VisibleWindows()
  178. If windows _activeWindow=windows[0]
  179. Endif
  180. Return _activeWindow
  181. End
  182. #rem monkeydoc Approximate frames per second rendering rate.
  183. #end
  184. Property FPS:Float()
  185. Return _fps
  186. End
  187. #rem monkeydoc Number of milliseconds app has been running.
  188. This property uses the high precision system timer if possible.
  189. #end
  190. Property Millisecs:Int()
  191. Return SDL_GetTicks()
  192. End
  193. #rem monkeydoc Mouse location relative to the active window.
  194. @see [[ActiveWindow]], [[MouseX]], [[MouseY]]
  195. #end
  196. Property MouseLocation:Vec2i()
  197. Return _mouseLocation
  198. End
  199. Property ModalView:View()
  200. Return _modalView
  201. End
  202. #if __TARGET__="desktop"
  203. #rem monkeydoc @hidden
  204. #end
  205. Method WaitIdle()
  206. Local future:=New Future<Bool>
  207. Idle+=Lambda()
  208. future.Set( True )
  209. End
  210. future.Get()
  211. End
  212. #endif
  213. #rem monkeydoc @hidden
  214. #end
  215. Method GetConfig:String( name:String,defValue:String )
  216. If _config.Contains( name ) Return _config[name]
  217. Return defValue
  218. End
  219. #rem monkeydoc @hidden
  220. #end
  221. Method GetDisplayModes:DisplayMode[]()
  222. Local n:=SDL_GetNumDisplayModes( 0 )
  223. Local modes:=New DisplayMode[n]
  224. For Local i:=0 Until n
  225. Local mode:SDL_DisplayMode
  226. SDL_GetDisplayMode( 0,i,Varptr mode )
  227. modes[i].width=mode.w
  228. modes[i].height=mode.h
  229. modes[i].hertz=mode.refresh_rate
  230. Next
  231. Return modes
  232. End
  233. #rem monkeydoc @hidden
  234. #end
  235. Method BeginModal( view:View )
  236. _modalStack.Push( _modalView )
  237. _modalView=view
  238. RequestRender()
  239. End
  240. #rem monkeydoc @hidden
  241. #end
  242. Method EndModal()
  243. _modalView=_modalStack.Pop()
  244. RequestRender()
  245. End
  246. #rem monkeydoc Terminate the app.
  247. #end
  248. Method Terminate()
  249. libc.exit_( 0 )
  250. End
  251. #rem monkeydoc Request that the app render itself.
  252. #end
  253. Method RequestRender()
  254. _requestRender=True
  255. End
  256. #rem @hidden
  257. #end
  258. Method MainLoop()
  259. If Not _requestRender
  260. SDL_WaitEvent( Null )
  261. Endif
  262. UpdateEvents()
  263. UpdateWindows()
  264. End
  265. #rem @hiddden
  266. #end
  267. Method IsActive:Bool( view:View )
  268. Return view And view.Active And (Not _modalView Or view.IsChildOf( _modalView ))
  269. End
  270. #rem @hiddden
  271. #end
  272. Method ActiveViewAtMouseLocation:View()
  273. If Not _window Return Null
  274. Local view:=_window.FindViewAtWindowPoint( _mouseLocation )
  275. If IsActive( view ) Return view
  276. Return Null
  277. End
  278. #rem @hidden
  279. #end
  280. Method UpdateWindows()
  281. Local render:=_requestRender
  282. _requestRender=False
  283. If render UpdateFPS()
  284. For Local window:=Eachin Window.VisibleWindows()
  285. window.UpdateWindow( render )
  286. End
  287. If _mouseView And Not IsActive( _mouseView )
  288. SendMouseEvent( EventType.MouseUp,_mouseView )
  289. _mouseView=Null
  290. Endif
  291. If _hoverView And Not IsActive( _hoverView )
  292. SendMouseEvent( EventType.MouseLeave,_hoverView )
  293. _hoverView=Null
  294. Endif
  295. If Not _hoverView And Not _touchMouse
  296. _hoverView=ActiveViewAtMouseLocation()
  297. If _mouseView And _hoverView<>_mouseView _hoverView=Null
  298. If _hoverView SendMouseEvent( EventType.MouseEnter,_hoverView )
  299. Endif
  300. End
  301. #rem monkeydoc @hidden
  302. #end
  303. Function EmscriptenMainLoop()
  304. App._requestRender=True
  305. App.MainLoop()
  306. End
  307. #rem monkeydoc Run the app.
  308. #end
  309. Method Run()
  310. #if __TARGET__="desktop"
  311. SDL_AddEventWatch( _EventFilter,Null )
  312. #endif
  313. RequestRender()
  314. #If __TARGET__="emscripten"
  315. emscripten_set_main_loop( EmscriptenMainLoop,0,1 )
  316. #Else
  317. Repeat
  318. MainLoop()
  319. Forever
  320. #Endif
  321. End
  322. Private
  323. Field _config:StringMap<String>
  324. Field _touchMouse:Bool=False 'mouse is really touch...
  325. Field _defaultFont:Font
  326. Field _theme:Theme
  327. Field _active:Bool
  328. Field _activeWindow:Window
  329. Field _keyView:View
  330. Field _hoverView:View
  331. Field _mouseView:View
  332. Field _requestRender:Bool
  333. Field _fps:Float
  334. Field _fpsFrames:Int
  335. Field _fpsMillis:Int
  336. Field _window:Window
  337. Field _key:Key
  338. Field _rawKey:Key
  339. Field _keyChar:String
  340. Field _modifiers:Modifier
  341. Field _mouseButton:MouseButton
  342. Field _mouseLocation:Vec2i
  343. Field _mouseWheel:Vec2i
  344. Field _mouseClicks:Int=0
  345. Field _modalView:View
  346. Field _modalStack:=New Stack<View>
  347. Field _polling:Bool
  348. Global _nextCallbackId:Int
  349. Global _asyncCallbacks:=New IntMap<Void()>
  350. Global _disabledCallbacks:=New IntMap<Bool>
  351. Method UpdateFPS()
  352. _fpsFrames+=1
  353. Local elapsed:=App.Millisecs-_fpsMillis
  354. If elapsed>=250
  355. _fps=Round( _fpsFrames/(elapsed/1000.0) )
  356. _fpsMillis+=elapsed
  357. _fpsFrames=0
  358. Endif
  359. End
  360. Method UpdateEvents()
  361. Local event:SDL_Event
  362. _polling=True
  363. While SDL_PollEvent( Varptr event )
  364. Keyboard.SendEvent( Varptr event )
  365. Mouse.SendEvent( Varptr event )
  366. DispatchEvent( Varptr event )
  367. Wend
  368. _polling=False
  369. Local idle:=Idle
  370. Idle=NextIdle
  371. NextIdle=Null
  372. idle()
  373. End
  374. Method SendKeyEvent( type:EventType )
  375. Local view:=KeyView
  376. Local event:=New KeyEvent( type,view,_key,_rawKey,_modifiers,_keyChar )
  377. KeyEventFilter( event )
  378. If event.Eaten Return
  379. If view view.SendKeyEvent( event )
  380. End
  381. Method SendMouseEvent( type:EventType,view:View )
  382. Local location:=view.TransformWindowPointToView( _mouseLocation )
  383. Local event:=New MouseEvent( type,view,location,_mouseButton,_mouseWheel,_modifiers,_mouseClicks )
  384. MouseEventFilter( event )
  385. If event.Eaten Return
  386. view.SendMouseEvent( event )
  387. If event.Eaten Return
  388. Select type
  389. Case EventType.MouseDown
  390. Select _mouseButton
  391. Case MouseButton.Left
  392. SendMouseEvent( EventType.MouseClick,view )
  393. If _mouseClicks And Not (_mouseClicks & 1)
  394. SendMouseEvent( EventType.MouseDoubleClick,view )
  395. End
  396. Case MouseButton.Right
  397. SendMouseEvent( EventType.MouseRightClick,view )
  398. End
  399. End
  400. End
  401. Method SendWindowEvent( type:EventType )
  402. Local event:=New WindowEvent( type,_window )
  403. _window.SendWindowEvent( event )
  404. End
  405. Method DispatchEvent( event:SDL_Event Ptr )
  406. Select event->type
  407. Case SDL_KEYDOWN
  408. Local kevent:=Cast<SDL_KeyboardEvent Ptr>( event )
  409. _window=Window.WindowForID( kevent->windowID )
  410. If Not _window Return
  411. _key=Keyboard.KeyCodeToKey( Int( kevent->keysym.sym ) )
  412. _rawKey=Keyboard.ScanCodeToRawKey( Int( kevent->keysym.scancode ) )
  413. ' _modifiers=Keyboard.Modifiers
  414. _keyChar=Keyboard.KeyName( _key )
  415. If kevent->repeat_
  416. SendKeyEvent( EventType.KeyRepeat )
  417. Else
  418. SendKeyEvent( EventType.KeyDown )
  419. Endif
  420. _modifiers=Keyboard.Modifiers
  421. Case SDL_KEYUP
  422. Local kevent:=Cast<SDL_KeyboardEvent Ptr>( event )
  423. _window=Window.WindowForID( kevent->windowID )
  424. If Not _window Return
  425. _key=Keyboard.KeyCodeToKey( Int( kevent->keysym.sym ) )
  426. _rawKey=Keyboard.ScanCodeToRawKey( Int( kevent->keysym.scancode ) )
  427. ' _modifiers=Keyboard.Modifiers
  428. _keyChar=Keyboard.KeyName( _key )
  429. SendKeyEvent( EventType.KeyUp )
  430. _modifiers=Keyboard.Modifiers
  431. Case SDL_TEXTINPUT
  432. Local tevent:=Cast<SDL_TextInputEvent Ptr>( event )
  433. _window=Window.WindowForID( tevent->windowID )
  434. If Not _window Return
  435. _keyChar=String.FromChar( tevent->text[0] )
  436. SendKeyEvent( EventType.KeyChar )
  437. Case SDL_MOUSEBUTTONDOWN
  438. Local mevent:=Cast<SDL_MouseButtonEvent Ptr>( event )
  439. _window=Window.WindowForID( mevent->windowID )
  440. If Not _window Return
  441. _mouseLocation=New Vec2i( mevent->x,mevent->y )
  442. _mouseButton=Cast<MouseButton>( mevent->button )
  443. If Not _mouseView
  444. Local view:=ActiveViewAtMouseLocation()
  445. If view
  446. If _touchMouse
  447. _hoverView=view
  448. SendMouseEvent( EventType.MouseEnter,_hoverView )
  449. Endif
  450. SDL_CaptureMouse( SDL_TRUE )
  451. _mouseView=view
  452. _mouseClicks=mevent->clicks
  453. SendMouseEvent( EventType.MouseDown,_mouseView )
  454. _mouseClicks=0
  455. Endif
  456. Endif
  457. Case SDL_MOUSEBUTTONUP
  458. Local mevent:=Cast<SDL_MouseButtonEvent Ptr>( event )
  459. _window=Window.WindowForID( mevent->windowID )
  460. If Not _window Return
  461. _mouseLocation=New Vec2i( mevent->x,mevent->y )
  462. _mouseButton=Cast<MouseButton>( mevent->button )
  463. If _mouseView
  464. SendMouseEvent( EventType.MouseUp,_mouseView )
  465. SDL_CaptureMouse( SDL_FALSE )
  466. _mouseView=Null
  467. _mouseButton=Null
  468. If _touchMouse
  469. SendMouseEvent( EventType.MouseLeave,_hoverView )
  470. _hoverView=Null
  471. Endif
  472. Endif
  473. Case SDL_MOUSEMOTION
  474. Local mevent:=Cast<SDL_MouseMotionEvent Ptr>( event )
  475. _window=Window.WindowForID( mevent->windowID )
  476. If Not _window Return
  477. _mouseLocation=New Vec2i( mevent->x,mevent->y )
  478. If _mouseView
  479. SendMouseEvent( EventType.MouseMove,_mouseView )
  480. Else If Not _touchMouse
  481. Local view:=ActiveViewAtMouseLocation()
  482. If view<>_hoverView
  483. If _hoverView SendMouseEvent( EventType.MouseLeave,_hoverView )
  484. _hoverView=view
  485. If _hoverView SendMouseEvent( EventType.MouseEnter,_hoverView )
  486. Endif
  487. If _hoverView SendMouseEvent( EventType.MouseMove,_hoverView )
  488. Endif
  489. #rem
  490. If Not _touchMouse Or _mouseView
  491. Local mevent:=Cast<SDL_MouseMotionEvent Ptr>( event )
  492. _window=Window.WindowForID( mevent->windowID )
  493. If Not _window Return
  494. _mouseLocation=New Vec2i( mevent->x,mevent->y )
  495. Local view:=ActiveViewAtMouseLocation()
  496. If _mouseView And view<>_mouseView view=Null
  497. If view<>_hoverView
  498. If _hoverView SendMouseEvent( EventType.MouseLeave,_hoverView )
  499. _hoverView=view
  500. If _hoverView SendMouseEvent( EventType.MouseEnter,_hoverView )
  501. Endif
  502. If _mouseView
  503. SendMouseEvent( EventType.MouseMove,_mouseView )
  504. Else If _hoverView
  505. SendMouseEvent( EventType.MouseMove,_hoverView )
  506. Endif
  507. Endif
  508. #end
  509. Case SDL_MOUSEWHEEL
  510. Local mevent:=Cast<SDL_MouseWheelEvent Ptr>( event )
  511. _window=Window.WindowForID( mevent->windowID )
  512. If Not _window Return
  513. _mouseWheel=New Vec2i( mevent->x,mevent->y )
  514. If _mouseView
  515. SendMouseEvent( EventType.MouseWheel,_mouseView )
  516. Else If _hoverView
  517. SendMouseEvent( EventType.MouseWheel,_hoverView )
  518. Endif
  519. Case SDL_WINDOWEVENT
  520. Local wevent:=Cast<SDL_WindowEvent Ptr>( event )
  521. _window=Window.WindowForID( wevent->windowID )
  522. If Not _window Return
  523. Select wevent->event
  524. Case SDL_WINDOWEVENT_CLOSE
  525. SendWindowEvent( EventType.WindowClose )
  526. Case SDL_WINDOWEVENT_MOVED
  527. Case SDL_WINDOWEVENT_RESIZED
  528. Case SDL_WINDOWEVENT_SIZE_CHANGED
  529. Case SDL_WINDOWEVENT_FOCUS_GAINED
  530. Print "SDL_WINDOWEVENT_FOCUS_GAINED"
  531. Local active:=_active
  532. _activeWindow=_window
  533. _active=True
  534. SendWindowEvent( EventType.WindowGainedFocus )
  535. If active<>_active Activated()
  536. Case SDL_WINDOWEVENT_FOCUS_LOST
  537. Print "SDL_WINDOWEVENT_FOCUS_LOST"
  538. Local active:=_active
  539. ' _activeWindow=Null 'too dangerous for now!
  540. _active=False
  541. SendWindowEvent( EventType.WindowLostFocus )
  542. If active<>_active Deactivated()
  543. Case SDL_WINDOWEVENT_LEAVE
  544. If _hoverView
  545. SendMouseEvent( EventType.MouseLeave,_hoverView )
  546. _hoverView=Null
  547. Endif
  548. End
  549. Case SDL_USEREVENT
  550. Local t:=Cast<SDL_UserEvent Ptr>( event )
  551. Local code:=t[0].code
  552. Local id:=code & $3fffffff
  553. If code & $40000000
  554. Local func:=_asyncCallbacks[ id ] 'null if removed
  555. If code & $80000000 RemoveAsyncCallback( id )
  556. If Not _disabledCallbacks[id] func()
  557. Else If code & $80000000
  558. RemoveAsyncCallback( id )
  559. Endif
  560. Case SDL_RENDER_TARGETS_RESET
  561. Print "SDL_RENDER_TARGETS_RESET"
  562. RequestRender()
  563. Case SDL_RENDER_DEVICE_RESET
  564. Print "SDL_RENDER_DEVICE_RESET"
  565. mojo.graphics.glutil.glGraphicsSeq+=1
  566. End
  567. End
  568. Function _EventFilter:Int( userData:Void Ptr,event:SDL_Event Ptr )
  569. Return App.EventFilter( userData,event )
  570. End
  571. Method EventFilter:Int( userData:Void Ptr,event:SDL_Event Ptr )
  572. Select event[0].type
  573. Case SDL_WINDOWEVENT
  574. Local wevent:=Cast<SDL_WindowEvent Ptr>( event )
  575. _window=Window.WindowForID( wevent->windowID )
  576. If Not _window Return 1
  577. Select wevent->event
  578. Case SDL_WINDOWEVENT_MOVED
  579. SendWindowEvent( EventType.WindowMoved )
  580. Return 0
  581. Case SDL_WINDOWEVENT_RESIZED
  582. SendWindowEvent( EventType.WindowResized )
  583. UpdateWindows()
  584. Return 0
  585. End
  586. End
  587. Return 1
  588. End
  589. Function AddAsyncCallback:Int( func:Void() )
  590. _nextCallbackId+=1
  591. Local id:=_nextCallbackId
  592. _asyncCallbacks[id]=func
  593. Return id
  594. End
  595. Function RemoveAsyncCallback( id:Int )
  596. _disabledCallbacks.Remove( id )
  597. _asyncCallbacks.Remove( id )
  598. End
  599. Function EnableAsyncCallback( id:Int )
  600. _disabledCallbacks.Remove( id )
  601. End
  602. Function DisableAsyncCallback( id:Int )
  603. If _asyncCallbacks.Contains( id ) _disabledCallbacks[id]=True
  604. End
  605. End