app.monkey2 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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. @see [[ActiveWindow]], [[MouseX]], [[MouseY]]
  203. #end
  204. Property MouseLocation:Vec2i()
  205. Return _mouseLocation
  206. End
  207. #rem monkeydoc @hidden
  208. #end
  209. Property ModalView:View()
  210. Return _modalView
  211. End
  212. #rem monkeydoc Approximate frames per second rendering rate.
  213. #end
  214. Property FPS:Float()
  215. Return _fps
  216. End
  217. #rem monkeydoc Number of milliseconds app has been running.
  218. Deprecated! Just use std.time.Millisecs()
  219. #end
  220. Property Millisecs:Int()
  221. Return std.time.Millisecs()
  222. End
  223. #rem monkeydoc Puts the app to sleep.
  224. Note: Use of this method is not recommended, as the app will be unresponsive while sleeping!
  225. #end
  226. Method Sleep( seconds:Double )
  227. Local timeout:=Now()+seconds
  228. Repeat
  229. Local sleep:=timeout-Now()
  230. If sleep>10
  231. time.Sleep( sleep )
  232. UpdateWindows()
  233. Else If sleep>0
  234. time.Sleep( sleep )
  235. Else
  236. Return
  237. Endif
  238. Forever
  239. End
  240. #If __DESKTOP_TARGET__
  241. #rem monkeydoc @hidden
  242. #end
  243. Method WaitIdle()
  244. Local future:=New Future<Bool>
  245. Idle+=Lambda()
  246. future.Set( True )
  247. End
  248. future.Get()
  249. End
  250. #Endif
  251. #rem monkeydoc @hidden
  252. #end
  253. Method GetConfig:String( name:String,defValue:String )
  254. If _config.Contains( name ) Return _config[name]
  255. Return defValue
  256. End
  257. #rem monkeydoc @hidden
  258. #end
  259. Method GetDisplayModes:DisplayMode[]()
  260. Local n:=SDL_GetNumDisplayModes( 0 )
  261. Local modes:=New DisplayMode[n]
  262. For Local i:=0 Until n
  263. Local mode:SDL_DisplayMode
  264. SDL_GetDisplayMode( 0,i,Varptr mode )
  265. modes[i].width=mode.w
  266. modes[i].height=mode.h
  267. modes[i].hertz=mode.refresh_rate
  268. Next
  269. Return modes
  270. End
  271. #rem monkeydoc @hidden
  272. #end
  273. Method BeginModal( view:View )
  274. _modalStack.Push( _modalView )
  275. _modalView=view
  276. RequestRender()
  277. End
  278. #rem monkeydoc @hidden
  279. #end
  280. Method EndModal()
  281. _modalView=_modalStack.Pop()
  282. RequestRender()
  283. End
  284. #rem monkeydoc Terminate the app.
  285. #end
  286. Method Terminate()
  287. libc.exit_( 0 )
  288. End
  289. #rem monkeydoc Request that the app render itself.
  290. #end
  291. Method RequestRender()
  292. _requestRender=True
  293. End
  294. #rem monkeydoc @hidden
  295. #end
  296. Method MainLoop()
  297. If Not _requestRender
  298. SDL_WaitEvent( Null )
  299. Endif
  300. UpdateEvents()
  301. UpdateWindows()
  302. End
  303. #rem monkeydoc @hidden
  304. #end
  305. Method IsActive:Bool( view:View )
  306. Return view And view.Active And (Not _modalView Or view.IsChildOf( _modalView ))
  307. End
  308. #rem monkeydoc @hidden
  309. #end
  310. Method ActiveViewAtMouseLocation:View()
  311. If Not _window Return Null
  312. Local view:=_window.FindViewAtWindowPoint( _mouseLocation )
  313. If IsActive( view ) Return view
  314. Return Null
  315. End
  316. #rem monkeydoc @hidden
  317. #end
  318. Method UpdateWindows()
  319. If _frozen Return
  320. Local render:=_requestRender
  321. _requestRender=False
  322. If render UpdateFPS()
  323. For Local window:=Eachin Window.VisibleWindows()
  324. window.UpdateWindow( render )
  325. End
  326. If _mouseView And Not IsActive( _mouseView )
  327. SendMouseEvent( EventType.MouseUp,_mouseView )
  328. _mouseView=Null
  329. Endif
  330. If _hoverView And Not IsActive( _hoverView )
  331. SendMouseEvent( EventType.MouseLeave,_hoverView )
  332. _hoverView=Null
  333. Endif
  334. If Not _hoverView And Not _touchMouse
  335. _hoverView=ActiveViewAtMouseLocation()
  336. If _mouseView And _hoverView<>_mouseView _hoverView=Null
  337. If _hoverView SendMouseEvent( EventType.MouseEnter,_hoverView )
  338. Endif
  339. End
  340. #rem monkeydoc @hidden
  341. #end
  342. Function EmscriptenMainLoop()
  343. App._requestRender=True
  344. App.MainLoop()
  345. End
  346. #rem monkeydoc Run the app.
  347. #end
  348. Method Run()
  349. #if __DESKTOP_TARGET__
  350. SDL_AddEventWatch( _EventFilter,Null )
  351. #endif
  352. RequestRender()
  353. #If __TARGET__="emscripten"
  354. emscripten_set_main_loop( EmscriptenMainLoop,0,1 )
  355. #Else
  356. Repeat
  357. MainLoop()
  358. Forever
  359. #Endif
  360. End
  361. Private
  362. Field _config:StringMap<String>
  363. Field _touchMouse:Bool=False 'Whether mouse is really touch
  364. Field _captureMouse:Bool=False 'Whether to use SDL_CaptureMouse
  365. Field _res:=New ResourceManager
  366. Field _defaultFont:Font
  367. Field _theme:Theme
  368. Field _active:Bool
  369. Field _activeWindow:Window
  370. Field _frozen:Bool
  371. Field _keyView:View
  372. Field _hoverView:View
  373. Field _mouseView:View
  374. Field _requestRender:Bool
  375. Field _fps:Float
  376. Field _fpsFrames:Int
  377. Field _fpsMillis:Int
  378. Field _window:Window
  379. Field _key:Key
  380. Field _rawKey:Key
  381. Field _keyChar:String
  382. Field _modifiers:Modifier
  383. Field _mouseButton:MouseButton
  384. Field _mouseLocation:Vec2i
  385. Field _mouseWheel:Vec2i
  386. Field _mouseClicks:Int=0
  387. Field _finger:Int
  388. Field _fingerPressure:Float
  389. Field _fingerCoords:Vec2f
  390. Field _modalView:View
  391. Field _modalStack:=New Stack<View>
  392. Method UpdateFPS()
  393. _fpsFrames+=1
  394. Local elapsed:=App.Millisecs-_fpsMillis
  395. If elapsed>=250
  396. _fps=Round( _fpsFrames/(elapsed/1000.0) )
  397. _fpsMillis+=elapsed
  398. _fpsFrames=0
  399. Endif
  400. End
  401. Method UpdateEvents()
  402. Keyboard.Update()
  403. Mouse.Update()
  404. Touch.Update()
  405. Local event:SDL_Event
  406. While SDL_PollEvent( Varptr event )
  407. DispatchEvent( Varptr event )
  408. Wend
  409. Local idle:=Idle
  410. Idle=Null
  411. idle()
  412. End
  413. Method SendKeyEvent( type:EventType )
  414. Local view:=KeyView
  415. Local event:=New KeyEvent( type,view,_key,_rawKey,_modifiers,_keyChar )
  416. KeyEventFilter( event )
  417. If event.Eaten Return
  418. If view view.SendKeyEvent( event )
  419. End
  420. Method SendMouseEvent( type:EventType,view:View )
  421. Local location:=view.TransformWindowPointToView( _mouseLocation )
  422. Local event:=New MouseEvent( type,view,location,_mouseButton,_mouseWheel,_modifiers,_mouseClicks )
  423. MouseEventFilter( event )
  424. If event.Eaten Return
  425. view.SendMouseEvent( event )
  426. If event.Eaten Return
  427. Select type
  428. Case EventType.MouseDown
  429. Select _mouseButton
  430. Case MouseButton.Left
  431. SendMouseEvent( EventType.MouseClick,view )
  432. If _mouseClicks And Not (_mouseClicks & 1)
  433. SendMouseEvent( EventType.MouseDoubleClick,view )
  434. End
  435. Case MouseButton.Right
  436. SendMouseEvent( EventType.MouseRightClick,view )
  437. End
  438. End
  439. End
  440. Method SendTouchEvent( type:EventType )
  441. Local window:=_activeWindow
  442. If Not window Return
  443. Local p:=New Vec2i( _fingerCoords.x * window.Frame.Width,_fingerCoords.y * window.Frame.Height )
  444. Local location:=window.TransformWindowPointToView( p )
  445. window.SendTouchEvent( New TouchEvent( type,_activeWindow,location,_finger,_fingerPressure ) )
  446. End
  447. Method SendWindowEvent( type:EventType )
  448. Local event:=New WindowEvent( type,_window )
  449. _window.SendWindowEvent( event )
  450. End
  451. Method DispatchEvent( event:SDL_Event Ptr )
  452. SdlEventFilter( event )
  453. Keyboard.SendEvent( event )
  454. ' Mouse.SendEvent( event )
  455. Touch.SendEvent( event )
  456. Select event->type
  457. #If __TARGET__="macos"
  458. Case SDL_QUIT
  459. If _activeWindow _activeWindow.SendWindowEvent( New WindowEvent( EventType.WindowClose,_activeWindow ) )
  460. #Endif
  461. Case SDL_KEYDOWN
  462. Local kevent:=Cast<SDL_KeyboardEvent Ptr>( event )
  463. _window=Window.WindowForID( kevent->windowID )
  464. If Not _window Return
  465. _key=Keyboard.KeyCodeToKey( Int( kevent->keysym.sym ) )
  466. _rawKey=Keyboard.ScanCodeToRawKey( Int( kevent->keysym.scancode ) )
  467. _modifiers=Cast<Modifier>( kevent->keysym.mod_ )
  468. _keyChar=Keyboard.KeyName( _key )
  469. If kevent->repeat_
  470. SendKeyEvent( EventType.KeyRepeat )
  471. Else
  472. SendKeyEvent( EventType.KeyDown )
  473. Endif
  474. Case SDL_KEYUP
  475. Local kevent:=Cast<SDL_KeyboardEvent Ptr>( event )
  476. _window=Window.WindowForID( kevent->windowID )
  477. If Not _window Return
  478. _key=Keyboard.KeyCodeToKey( Int( kevent->keysym.sym ) )
  479. _rawKey=Keyboard.ScanCodeToRawKey( Int( kevent->keysym.scancode ) )
  480. _modifiers=Cast<Modifier>( kevent->keysym.mod_ )
  481. _keyChar=Keyboard.KeyName( _key )
  482. SendKeyEvent( EventType.KeyUp )
  483. Case SDL_TEXTINPUT
  484. Local tevent:=Cast<SDL_TextInputEvent Ptr>( event )
  485. _window=Window.WindowForID( tevent->windowID )
  486. If Not _window Return
  487. _keyChar=String.FromCString( tevent->text )
  488. SendKeyEvent( EventType.KeyChar )
  489. Case SDL_MOUSEBUTTONDOWN
  490. Local mevent:=Cast<SDL_MouseButtonEvent Ptr>( event )
  491. _window=Window.WindowForID( mevent->windowID )
  492. If Not _window Return
  493. _mouseLocation=_window.MouseScale * New Vec2i( mevent->x,mevent->y )
  494. _mouseButton=Cast<MouseButton>( mevent->button )
  495. If Not _mouseView
  496. Local mouseView:=ActiveViewAtMouseLocation()
  497. If mouseView
  498. If _touchMouse
  499. _hoverView=mouseView
  500. SendMouseEvent( EventType.MouseEnter,_hoverView )
  501. Endif
  502. If _captureMouse SDL_CaptureMouse( SDL_TRUE )
  503. _mouseView=mouseView
  504. _mouseClicks=mevent->clicks
  505. SendMouseEvent( EventType.MouseDown,_mouseView )
  506. _mouseClicks=0
  507. Endif
  508. Endif
  509. Case SDL_MOUSEBUTTONUP
  510. Local mevent:=Cast<SDL_MouseButtonEvent Ptr>( event )
  511. _window=Window.WindowForID( mevent->windowID )
  512. If Not _window Return
  513. _mouseLocation=_window.MouseScale * New Vec2i( mevent->x,mevent->y )
  514. _mouseButton=Cast<MouseButton>( mevent->button )
  515. If _mouseView
  516. If _captureMouse SDL_CaptureMouse( SDL_FALSE )
  517. SendMouseEvent( EventType.MouseUp,_mouseView )
  518. _mouseButton=Null
  519. _mouseView=Null
  520. If _touchMouse
  521. SendMouseEvent( EventType.MouseLeave,_hoverView )
  522. _hoverView=Null
  523. Endif
  524. Endif
  525. Case SDL_MOUSEMOTION
  526. Local mevent:=Cast<SDL_MouseMotionEvent Ptr>( event )
  527. _window=Window.WindowForID( mevent->windowID )
  528. If Not _window Return
  529. _mouseLocation=_window.MouseScale * New Vec2i( mevent->x,mevent->y )
  530. If Not _touchMouse
  531. Local hoverView:=ActiveViewAtMouseLocation()
  532. If _mouseView And hoverView<>_mouseView hoverView=Null
  533. If hoverView<>_hoverView
  534. If _hoverView SendMouseEvent( EventType.MouseLeave,_hoverView )
  535. _hoverView=hoverView
  536. If _hoverView SendMouseEvent( EventType.MouseEnter,_hoverView )
  537. Endif
  538. Endif
  539. If _mouseView
  540. SendMouseEvent( EventType.MouseMove,_mouseView )
  541. Else If _hoverView
  542. SendMouseEvent( EventType.MouseMove,_hoverView )
  543. Endif
  544. Case SDL_MOUSEWHEEL
  545. Local mevent:=Cast<SDL_MouseWheelEvent Ptr>( event )
  546. _window=Window.WindowForID( mevent->windowID )
  547. If Not _window Return
  548. _mouseWheel=New Vec2i( mevent->x,mevent->y )
  549. If _mouseView
  550. SendMouseEvent( EventType.MouseWheel,_mouseView )
  551. Else If _hoverView
  552. SendMouseEvent( EventType.MouseWheel,_hoverView )
  553. Endif
  554. Case SDL_FINGERDOWN
  555. Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
  556. _finger=tevent->fingerId
  557. _fingerPressure=tevent->pressure
  558. _fingerCoords=New Vec2f( tevent->x,tevent->y )
  559. SendTouchEvent( EventType.TouchDown )
  560. Case SDL_FINGERUP
  561. Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
  562. _finger=tevent->fingerId
  563. _fingerPressure=tevent->pressure
  564. _fingerCoords=New Vec2f( tevent->x,tevent->y )
  565. SendTouchEvent( EventType.TouchUp )
  566. Case SDL_FINGERMOTION
  567. Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
  568. _finger=tevent->fingerId
  569. _fingerPressure=tevent->pressure
  570. _fingerCoords=New Vec2f( tevent->x,tevent->y )
  571. SendTouchEvent( EventType.TouchMove )
  572. Case SDL_WINDOWEVENT
  573. Local wevent:=Cast<SDL_WindowEvent Ptr>( event )
  574. _window=Window.WindowForID( wevent->windowID )
  575. If Not _window Return
  576. Select wevent->event
  577. Case SDL_WINDOWEVENT_CLOSE
  578. SendWindowEvent( EventType.WindowClose )
  579. Case SDL_WINDOWEVENT_MAXIMIZED
  580. SendWindowEvent( EventType.WindowMaximized )
  581. Case SDL_WINDOWEVENT_MINIMIZED
  582. SendWindowEvent( EventType.WindowMinimized )
  583. Case SDL_WINDOWEVENT_RESTORED
  584. SendWindowEvent( EventType.WindowRestored )
  585. Case SDL_WINDOWEVENT_FOCUS_GAINED
  586. Print "SDL_WINDOWEVENT_FOCUS_GAINED"
  587. Local active:=_active
  588. _activeWindow=_window
  589. _active=True
  590. SendWindowEvent( EventType.WindowGainedFocus )
  591. If active<>_active Activated()
  592. Case SDL_WINDOWEVENT_FOCUS_LOST
  593. Print "SDL_WINDOWEVENT_FOCUS_LOST"
  594. Local active:=_active
  595. _active=False
  596. If _mouseView And Not _captureMouse
  597. SendMouseEvent( EventType.MouseUp,_mouseView )
  598. _mouseView=Null
  599. Endif
  600. If _hoverView
  601. SendMouseEvent( EventType.MouseLeave,_hoverView )
  602. _hoverView=Null
  603. Endif
  604. SendWindowEvent( EventType.WindowLostFocus )
  605. If active<>_active Deactivated()
  606. Case SDL_WINDOWEVENT_LEAVE
  607. If _mouseView And Not _captureMouse
  608. SendMouseEvent( EventType.MouseUp,_mouseView )
  609. _mouseView=Null
  610. Endif
  611. If _hoverView
  612. SendMouseEvent( EventType.MouseLeave,_hoverView )
  613. _hoverView=Null
  614. Endif
  615. End
  616. Case SDL_USEREVENT
  617. Local uevent:=Cast<SDL_UserEvent Ptr>( event )
  618. Local event:=Cast<AsyncEvent Ptr>( uevent->data1 )
  619. event->Dispatch()
  620. Case SDL_DROPFILE
  621. Local devent:=Cast<SDL_DropEvent Ptr>( event )
  622. Local path:=String.FromCString( devent->file ).Replace( "\","/" )
  623. SDL_free( devent->file )
  624. FileDropped( path )
  625. Case SDL_RENDER_TARGETS_RESET
  626. Print "SDL_RENDER_TARGETS_RESET"
  627. RequestRender()
  628. Case SDL_RENDER_DEVICE_RESET
  629. Print "SDL_RENDER_DEVICE_RESET"
  630. mojo.graphics.glutil.glGraphicsSeq+=1
  631. #if __TARGET__="ios"
  632. Case SDL_APP_TERMINATING
  633. 'Terminate the app.
  634. 'Shut everything down before returning from this function.
  635. Case SDL_APP_LOWMEMORY
  636. 'You will get this when your app is paused and iOS wants more memory.
  637. 'Release as much memory as possible.
  638. Case SDL_APP_WILLENTERBACKGROUND
  639. 'Prepare your app to go into the background. Stop loops, etc.
  640. 'This gets called when the user hits the home button, or gets a call.
  641. Print "SDL_APP_WILLENTERBACKGROUND"
  642. _frozen=True
  643. Case SDL_APP_DIDENTERBACKGROUND
  644. 'This will get called if the user accepted whatever sent your app to the background.
  645. 'If the user got a phone call and canceled it, you'll instead get an SDL_APP_DIDENTERFOREGROUND event and restart your loops.
  646. 'When you get this, you have 5 seconds to save all your state or the app will be terminated.
  647. 'Your app is NOT active at this point.
  648. Case SDL_APP_WILLENTERFOREGROUND
  649. 'This call happens when your app is coming back to the foreground.
  650. 'Restore all your state here.
  651. Case SDL_APP_DIDENTERFOREGROUND
  652. 'Restart your loops here.
  653. 'Your app is interactive and getting CPU again.
  654. Print "SDL_APP_DIDENTERFOREGROUND"
  655. RequestRender()
  656. _frozen=False
  657. #Endif
  658. End
  659. End
  660. Function _EventFilter:Int( userData:Void Ptr,event:SDL_Event Ptr )
  661. Return App.EventFilter( userData,event )
  662. End
  663. Method EventFilter:Int( userData:Void Ptr,event:SDL_Event Ptr )
  664. Select event[0].type
  665. Case SDL_WINDOWEVENT
  666. Local wevent:=Cast<SDL_WindowEvent Ptr>( event )
  667. _window=Window.WindowForID( wevent->windowID )
  668. If Not _window Return 1
  669. Select wevent->event
  670. Case SDL_WINDOWEVENT_MOVED
  671. SdlEventFilter( event )
  672. SendWindowEvent( EventType.WindowMoved )
  673. Return 0
  674. Case SDL_WINDOWEVENT_RESIZED
  675. SdlEventFilter( event )
  676. SendWindowEvent( EventType.WindowResized )
  677. UpdateWindows()
  678. Return 0
  679. End
  680. End
  681. Return 1
  682. End
  683. End