app.monkey2 20 KB

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