app.monkey2 20 KB

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