app.monkey2 20 KB

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