app.monkey2 25 KB

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