app.monkey2 20 KB

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