app.monkey2 22 KB

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