app.monkey2 20 KB

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