app.monkey2 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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. #rem monkeydoc The global AppInstance instance.
  8. #end
  9. Global App:AppInstance
  10. #rem monkeydoc Display mode structure.
  11. Contains information describing a display mode.
  12. See [[AppInstance.DesktopMode]] for more information.
  13. #end
  14. Struct DisplayMode
  15. Field width:Int
  16. Field height:Int
  17. Field depth:Int
  18. Field hertz:Int
  19. Operator To:String()
  20. Return "DisplayMode("+width+","+height+","+depth+","+hertz+")"
  21. End
  22. End
  23. #rem monkeydoc The AppInstance class.
  24. The AppInstance class is mainly reponsible for running the app 'event loop', but also provides several utility functions for managing the application.
  25. 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
  26. There are a number of config settings that can be used to control app behaviour. See [[std:std.filesystem.SetConfig|SetConfig]] for more information about config settings.
  27. | Config setting | Possible values | Default value
  28. |:----------------------------------|:----------------------------------|:-------------
  29. | "MOJO\_OPENGL\_PROFILE" | "es", "compatibility" or "core" | "compatibility" on macos and linux, "es" on all other targets. Uses 'Angle' for es support on windows.
  30. | "MOJO\_OPENGL\_VERSION\_MAJOR" | Open gl major version | 2
  31. | "MOJO\_OPENGL\_VERSION\_MINOR" | Open gl minor version | 0
  32. | "MOJO\_COLOR\_BUFFER\_BITS" | Minimum color bit depth | 8
  33. | "MOJO\_DEPTH\_BUFFER\_BITS" | Minimum depth buffer bit depth | 0
  34. | "MOJO\_STENCIL\_BUFFER\_BITS" | Minimum stencil buffer bit depth | 0
  35. #end
  36. Class AppInstance
  37. #rem monkeydoc Invoked when the app becomes idle.
  38. #end
  39. Field Idle:Void()
  40. #rem monkeydoc Invoked when app is activated.
  41. #end
  42. Field Activated:Void()
  43. #rem monkeydoc Invoked when app is deactivated.
  44. #end
  45. Field Deactivated:Void()
  46. #rem monkeydoc @hidden
  47. #end
  48. Field ThemeChanged:Void()
  49. #rem monkeydoc Invoked when a file is dropped on an app window.
  50. #end
  51. Field FileDropped:Void( path:String )
  52. #rem monkeydoc Key event filter.
  53. To prevent the event from being sent to a view, a filter can eat the event using [[Event.Eat]].
  54. Filter functions should check if the event has already been 'eaten' by checking the event's [[Event.Eaten]] property before processing the event.
  55. #end
  56. Field KeyEventFilter:Void( event:KeyEvent )
  57. #rem monkeydoc MouseEvent filter.
  58. To prevent the event from being sent to a view, a filter can eat the event using [[Event.Eat]].
  59. Filter functions should check if the event has already been 'eaten' by checking the event's [[Event.Eaten]] property before processing the event.
  60. #end
  61. Field MouseEventFilter:Void( event:MouseEvent )
  62. #rem monkeydoc Raw SDL_Event filter.
  63. The filter is called for all SDL events before mojo processes them.
  64. #end
  65. Field SdlEventFilter:Void( event:SDL_Event Ptr )
  66. #rem monkeydoc Creates a new app instance.
  67. Creates a new AppInstance objects and initializes the global [[App]] variable with it.
  68. A runtime error will occur if more than 1 AppInstance is created.
  69. #end
  70. Method New()
  71. App=Self
  72. SDL_Init( SDL_INIT_VIDEO|SDL_INIT_JOYSTICK|SDL_INIT_GAMECONTROLLER )
  73. libc.atexit( SDL_Quit )
  74. SDL_SetHint( "SDL_MOUSE_FOCUS_CLICKTHROUGH","1" )
  75. SDL_FlushEvents( SDL_JOYDEVICEADDED,SDL_JOYDEVICEADDED )
  76. SDL_FlushEvents( SDL_CONTROLLERDEVICEADDED,SDL_CONTROLLERDEVICEADDED )
  77. AppInit()
  78. Keyboard.Init()
  79. Mouse.Init()
  80. Touch.Init()
  81. Audio.Init()
  82. 'Set GL attributes
  83. '
  84. Local gl_profile:Int,gl_major:Int=2,gl_minor:Int=0
  85. Select GetConfig( "MOJO_OPENGL_PROFILE" )
  86. Case "core"
  87. gl_profile=SDL_GL_CONTEXT_PROFILE_CORE
  88. Case "compatibility"
  89. gl_profile=SDL_GL_CONTEXT_PROFILE_COMPATIBILITY
  90. Case "es"
  91. gl_profile=SDL_GL_CONTEXT_PROFILE_ES
  92. Default
  93. #If __TARGET__="macos" Or __TARGET__="linux"
  94. gl_profile=SDL_GL_CONTEXT_PROFILE_COMPATIBILITY 'no gles20 on macos...
  95. #Else
  96. gl_profile=SDL_GL_CONTEXT_PROFILE_ES
  97. #Endif
  98. End
  99. SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK,gl_profile )
  100. SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION,Int( GetConfig( "MOJO_OPENGL_VERSION_MAJOR",gl_major ) ) )
  101. SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION,Int( GetConfig( "MOJO_OPENGL_VERSION_MINOR",gl_minor ) ) )
  102. SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER,1 )
  103. Local n:=Int( GetConfig( "MOJO_COLOR_BUFFER_BITS",8 ) )
  104. SDL_GL_SetAttribute( SDL_GL_RED_SIZE,n )
  105. SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE,n )
  106. SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE,n )
  107. SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE,Int( GetConfig( "MOJO_DEPTH_BUFFER_BITS" ) ) )
  108. SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE,Int( GetConfig( "MOJO_STENCIL_BUFFER_BITS" ) ) )
  109. Local msaa_samples:=Int( GetConfig( "MOJO_MSAA_SAMPLES",0 ) )
  110. If msaa_samples
  111. SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS,1 )
  112. SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES,msaa_samples )
  113. Endif
  114. #If __DESKTOP_TARGET__
  115. 'WIP multiple windows...
  116. SDL_GL_SetAttribute( SDL_GL_SHARE_WITH_CURRENT_CONTEXT,1 )
  117. 'create dummy window/context
  118. Local _sdlWindow:=SDL_CreateWindow( "<dummy>",0,0,0,0,SDL_WINDOW_HIDDEN|SDL_WINDOW_OPENGL )
  119. Assert( _sdlWindow,"FATAL ERROR: SDL_CreateWindow failed" )
  120. Local _sdlGLContext:=SDL_GL_CreateContext( _sdlWindow )
  121. Assert( _sdlGLContext,"FATAL ERROR: SDL_GL_CreateContext failed" )
  122. SDL_GL_MakeCurrent( _sdlWindow,_sdlGLContext )
  123. #Endif
  124. #If __TARGET__="windows" Or __TARGET__="macos" Or __TARGET__="emscripten"
  125. _captureMouse=True 'breaks on linux...
  126. #Endif
  127. #if __MOBILE_TARGET__
  128. _touchMouse=True
  129. #Endif
  130. _defaultFont=_res.OpenFont( "DejaVuSans",16 )
  131. _theme=New Theme
  132. Local themePath:=GetConfig( "MOJO_INITIAL_THEME","default" )
  133. Local themeScale:=Float( GetConfig( "MOJO_INITIAL_THEME_SCALE",1 ) )
  134. _theme.Load( themePath,New Vec2f( themeScale ) )
  135. _theme.ThemeChanged+=Lambda()
  136. ThemeChanged()
  137. RequestRender()
  138. UpdateWindows()
  139. End
  140. #if __DESKTOP_TARGET__
  141. SDL_AddEventWatch( _EventFilter,Null )
  142. #Endif
  143. RequestRender()
  144. End
  145. #rem monkeydoc Fallback font.
  146. #end
  147. Property DefaultFont:Font()
  148. Return _defaultFont
  149. End
  150. #rem monkeydoc The current theme.
  151. #end
  152. Property Theme:Theme()
  153. Return _theme
  154. End
  155. #rem monkeydoc True if clipboard text is empty.
  156. This is faster than checking whether [[ClipboardText]] returns an empty string.
  157. #end
  158. Property ClipboardTextEmpty:Bool()
  159. Return SDL_HasClipboardText()=SDL_FALSE
  160. End
  161. #rem monkeydoc Clipboard text.
  162. #end
  163. Property ClipboardText:String()
  164. If SDL_HasClipboardText()=SDL_FALSE Return ""
  165. Local p:=SDL_GetClipboardText()
  166. Local str:=String.FromCString( p )
  167. 'fix windows eols
  168. str=str.Replace( "~r~n","~n" )
  169. str=str.Replace( "~r","~n" )
  170. SDL_free( p )
  171. Return str
  172. Setter( text:String )
  173. SDL_SetClipboardText( text )
  174. End
  175. #rem monkeydoc The current key view.
  176. The key view is the view key events are sent to.
  177. #end
  178. Property KeyView:View()
  179. If Not _active Return Null
  180. If IsActive( _keyView ) Return _keyView
  181. If _modalView Return _modalView
  182. Return _activeWindow
  183. Setter( keyView:View )
  184. _keyView=keyView
  185. End
  186. #rem monkeydoc The current mouse view.
  187. The mouse view is the view that the mouse is currently 'dragging'.
  188. #end
  189. Property MouseView:View()
  190. Return _mouseView
  191. End
  192. #rem monkeydoc The current hover view.
  193. The hover view is the view that the mouse is currently 'hovering' over.
  194. #end
  195. Property HoverView:View()
  196. Return _hoverView
  197. End
  198. #rem monkeydoc The desktop size.
  199. #end
  200. Property DesktopSize:Vec2i()
  201. Local mode:=DesktopMode
  202. Return New Vec2i( mode.width,mode.height )
  203. End
  204. #rem monkeydoc The desktop display mode.
  205. #end
  206. Property DesktopMode:DisplayMode()
  207. Local sdlMode:SDL_DisplayMode
  208. SDL_GetDesktopDisplayMode( 0,Varptr sdlMode )
  209. Return CreateDisplayMode( Varptr sdlMode )
  210. End
  211. #rem monkeydoc True if app is active.
  212. An app is active if any of its windows has system input focus.
  213. #end
  214. Property Active:Bool()
  215. Return _active
  216. End
  217. #rem monkeydoc The currently active window.
  218. The active window is the window that has system input focus.
  219. #end
  220. Property ActiveWindow:Window()
  221. If Not _activeWindow
  222. Local windows:=Window.VisibleWindows()
  223. If windows _activeWindow=windows[0]
  224. Endif
  225. Return _activeWindow
  226. End
  227. #rem monkeydoc Mouse location relative to the active window.
  228. #end
  229. Property MouseLocation:Vec2i()
  230. Return _mouseLocation
  231. End
  232. #rem monkeydoc @hidden
  233. #end
  234. Property ModalView:View()
  235. Return _modalView
  236. End
  237. #rem monkeydoc Approximate frames per second rendering rate.
  238. #end
  239. Property FPS:Float()
  240. Return _fps
  241. End
  242. #rem monkeydoc Number of milliseconds app has been running.
  243. Deprecated! Just use std.time.Millisecs()
  244. #end
  245. Property Millisecs:Int()
  246. Return std.time.Millisecs()
  247. End
  248. #rem monkeydoc Puts the app to sleep.
  249. Note: Use of this method is not recommended, as the app will be unresponsive while sleeping!
  250. #end
  251. Method Sleep( seconds:Double )
  252. Local timeout:=Now()+seconds
  253. Repeat
  254. Local sleep:=timeout-Now()
  255. If sleep>10
  256. time.Sleep( sleep )
  257. UpdateWindows()
  258. Else If sleep>0
  259. time.Sleep( sleep )
  260. Else
  261. Return
  262. Endif
  263. Forever
  264. End
  265. #If __DESKTOP_TARGET__
  266. #rem monkeydoc @hidden
  267. #end
  268. Method WaitIdle()
  269. Local future:=New Future<Bool>
  270. Idle+=Lambda()
  271. future.Set( True )
  272. End
  273. future.Get()
  274. End
  275. #Endif
  276. #rem monkeydoc Puts app into modal mode.
  277. #end
  278. Method BeginModal( view:View )
  279. _modalStack.Push( _modalView )
  280. _modalView=view
  281. RequestRender()
  282. End
  283. #rem monkeydoc Exits app from modal mode.
  284. #end
  285. Method EndModal()
  286. _modalView=_modalStack.Pop()
  287. RequestRender()
  288. End
  289. #rem monkeydoc Terminate the app.
  290. #end
  291. Method Terminate()
  292. libc.exit_( 0 )
  293. End
  294. #rem monkeydoc Request that the app render itself.
  295. #end
  296. Method RequestRender()
  297. _requestRender=True
  298. End
  299. #rem monkeydoc @hidden
  300. #end
  301. Property Renderable:Bool()
  302. Return _activeWindow And Not _activeWindow.Minimized And Not _renderingSuspended
  303. End
  304. #rem monkeydoc @hidden
  305. #end
  306. Method MainLoop()
  307. If Not _requestRender Or Not Renderable
  308. SDL_WaitEvent( Null )
  309. Endif
  310. UpdateEvents()
  311. UpdateWindows()
  312. End
  313. #rem monkeydoc @hidden
  314. #end
  315. Method IsActive:Bool( view:View )
  316. Return view And view.Active And (Not _modalView Or view.IsChildOf( _modalView ))
  317. End
  318. #rem monkeydoc @hidden
  319. #end
  320. Method ActiveViewAtMouseLocation:View()
  321. If Not _window Return Null
  322. Local view:=_window.FindViewAtWindowPoint( _mouseLocation )
  323. If IsActive( view ) Return view
  324. Return Null
  325. End
  326. #rem monkeydoc @hidden
  327. #end
  328. Method UpdateWindows()
  329. if Not Renderable Return
  330. Local render:=_requestRender
  331. _requestRender=False
  332. If render UpdateFPS()
  333. For Local window:=Eachin Window.VisibleWindows()
  334. window.UpdateWindow( render )
  335. End
  336. If _mouseView And Not IsActive( _mouseView )
  337. SendMouseEvent( EventType.MouseUp,_mouseView )
  338. _mouseView=Null
  339. Endif
  340. If _hoverView And Not IsActive( _hoverView )
  341. SendMouseEvent( EventType.MouseLeave,_hoverView )
  342. _hoverView=Null
  343. Endif
  344. If Not _hoverView And Not _touchMouse
  345. _hoverView=ActiveViewAtMouseLocation()
  346. If _mouseView And _hoverView<>_mouseView _hoverView=Null
  347. If _hoverView SendMouseEvent( EventType.MouseEnter,_hoverView )
  348. Endif
  349. End
  350. #rem monkeydoc Resets polled mouse and keyboard devices.
  351. Clears the keyboard character queue and clears the pressed and released states for all keys and mouse buttons.
  352. #end
  353. Method ResetPolledInput()
  354. Keyboard.Reset()
  355. Mouse.Reset()
  356. End
  357. #rem monkeydoc @hidden
  358. #end
  359. Function EmscriptenMainLoop()
  360. App.RequestRender()
  361. App.MainLoop()
  362. End
  363. #rem monkeydoc Run the app.
  364. #end
  365. Method Run()
  366. #If __TARGET__="emscripten"
  367. emscripten_set_main_loop( EmscriptenMainLoop,0,1 )
  368. #Else
  369. Repeat
  370. MainLoop()
  371. Forever
  372. #Endif
  373. End
  374. #rem monkeydoc @hidden
  375. #end
  376. Method SuspendRendering()
  377. _renderingSuspended+=1
  378. end
  379. #rem monkeydoc @hidden
  380. #end
  381. Method ResumeRendering()
  382. _renderingSuspended=Max( _renderingSuspended-1,0 )
  383. End
  384. #rem monkeydoc Enumerate the available display modes
  385. #end
  386. Method EnumDisplayModes:DisplayMode[]( displayIndex:Int=0 )
  387. Local n:=SDL_GetNumDisplayModes( displayIndex )
  388. If Not n Return Null
  389. Local modes:=New Stack<DisplayMode>
  390. For Local i:=0 Until n
  391. Local sdlMode:SDL_DisplayMode
  392. SDL_GetDisplayMode( displayIndex,i,Varptr sdlMode )
  393. Local mode:=CreateDisplayMode( Varptr sdlMode )
  394. Local found:=False
  395. For Local mode2:=Eachin modes
  396. If mode2.width<>mode.width Or mode2.height<>mode.height Or mode2.hertz<>mode.hertz Continue
  397. found=True
  398. Exit
  399. Next
  400. If Not found modes.Add( mode )
  401. Next
  402. Return modes.ToArray()
  403. End
  404. Internal
  405. Method DispatchEvents()
  406. Local event:SDL_Event
  407. While SDL_PollEvent( Varptr event )
  408. Window.CreateNewWindows()
  409. DispatchEvent( Varptr event )
  410. Wend
  411. End
  412. Method UpdateEvents()
  413. Keyboard.Update()
  414. Mouse.Update()
  415. Touch.Update()
  416. DispatchEvents()
  417. Local idle:=Idle
  418. Idle=Null
  419. idle()
  420. End
  421. Private
  422. Field _touchMouse:Bool=False 'Whether mouse is really touch
  423. Field _captureMouse:Bool=False 'Whether to use SDL_CaptureMouse
  424. Field _res:=New ResourceManager
  425. Field _defaultFont:Font
  426. Field _theme:Theme
  427. Field _active:Bool
  428. Field _activeWindow:Window
  429. Field _renderingSuspended:int
  430. Field _keyView:View
  431. Field _hoverView:View
  432. Field _mouseView:View
  433. Field _requestRender:Bool
  434. Field _fps:Float
  435. Field _fpsFrames:Int
  436. Field _fpsMillis:Int
  437. Field _window:Window
  438. Field _keyDownView:View
  439. Field _key:Key
  440. Field _rawKey:Key
  441. Field _keyChar:String
  442. Field _modifiers:Modifier
  443. Field _mouseButton:MouseButton
  444. Field _mouseLocation:Vec2i
  445. Field _mouseWheel:Vec2i
  446. Field _mouseClicks:Int=0
  447. Field _finger:Int
  448. Field _fingerPressure:Float
  449. Field _fingerCoords:Vec2f
  450. Field _modalView:View
  451. Field _modalStack:=New Stack<View>
  452. Method UpdateFPS()
  453. _fpsFrames+=1
  454. Local elapsed:=App.Millisecs-_fpsMillis
  455. If elapsed>=250
  456. _fps=Round( _fpsFrames/(elapsed/1000.0) )
  457. _fpsMillis+=elapsed
  458. _fpsFrames=0
  459. Endif
  460. End
  461. Method SendKeyEvent( type:EventType )
  462. Local view:=KeyView
  463. Select type
  464. Case EventType.KeyDown
  465. _keyDownView=view
  466. Case EventType.KeyUp
  467. If view<>_keyDownView Return
  468. End
  469. Local event:=New KeyEvent( type,view,_key,_rawKey,_modifiers,_keyChar )
  470. KeyEventFilter( event )
  471. If event.Eaten Or Not view Return
  472. view.SendKeyEvent( event )
  473. End
  474. Method SendMouseEvent( type:EventType,view:View )
  475. Local location:=view.TransformWindowPointToView( _mouseLocation )
  476. Local event:=New MouseEvent( type,view,location,_mouseButton,_mouseWheel,_modifiers,_mouseClicks )
  477. MouseEventFilter( event )
  478. If event.Eaten Return
  479. view.SendMouseEvent( event )
  480. If event.Eaten Return
  481. Select type
  482. Case EventType.MouseDown
  483. Select _mouseButton
  484. Case MouseButton.Left
  485. SendMouseEvent( EventType.MouseClick,view )
  486. If _mouseClicks And Not (_mouseClicks & 1)
  487. SendMouseEvent( EventType.MouseDoubleClick,view )
  488. End
  489. Case MouseButton.Right
  490. SendMouseEvent( EventType.MouseRightClick,view )
  491. End
  492. End
  493. End
  494. Method SendTouchEvent( type:EventType )
  495. Local window:=_activeWindow
  496. If Not window Return
  497. Local p:=New Vec2i( _fingerCoords.x * window.Frame.Width,_fingerCoords.y * window.Frame.Height )
  498. Local location:=window.TransformWindowPointToView( p )
  499. window.SendTouchEvent( New TouchEvent( type,_activeWindow,location,_finger,_fingerPressure ) )
  500. End
  501. Method SendWindowEvent( type:EventType )
  502. Local event:=New WindowEvent( type,_window )
  503. _window.SendWindowEvent( event )
  504. End
  505. Method DispatchEvent( event:SDL_Event Ptr )
  506. SdlEventFilter( event )
  507. JoystickDevice.SendEvent( event )
  508. Keyboard.SendEvent( event )
  509. Mouse.SendEvent( event )
  510. Touch.SendEvent( event )
  511. Select event->type
  512. #If __TARGET__="macos"
  513. Case SDL_QUIT
  514. If _activeWindow _activeWindow.SendWindowEvent( New WindowEvent( EventType.WindowClose,_activeWindow ) )
  515. #Endif
  516. Case SDL_KEYDOWN
  517. Local kevent:=Cast<SDL_KeyboardEvent Ptr>( event )
  518. _window=Window.WindowForID( kevent->windowID )
  519. If Not _window Return
  520. _key=Keyboard.KeyCodeToKey( Int( kevent->keysym.sym ) )
  521. _rawKey=Keyboard.ScanCodeToRawKey( Int( kevent->keysym.scancode ) )
  522. _keyChar=Keyboard.KeyName( _key )
  523. _modifiers=Keyboard.Modifiers
  524. If kevent->repeat_
  525. SendKeyEvent( EventType.KeyRepeat )
  526. Else
  527. SendKeyEvent( EventType.KeyDown )
  528. Endif
  529. Case SDL_KEYUP
  530. Local kevent:=Cast<SDL_KeyboardEvent Ptr>( event )
  531. _window=Window.WindowForID( kevent->windowID )
  532. If Not _window Return
  533. _key=Keyboard.KeyCodeToKey( Int( kevent->keysym.sym ) )
  534. _rawKey=Keyboard.ScanCodeToRawKey( Int( kevent->keysym.scancode ) )
  535. _keyChar=Keyboard.KeyName( _key )
  536. _modifiers=Keyboard.Modifiers
  537. SendKeyEvent( EventType.KeyUp )
  538. Case SDL_TEXTINPUT
  539. Local tevent:=Cast<SDL_TextInputEvent Ptr>( event )
  540. _window=Window.WindowForID( tevent->windowID )
  541. If Not _window Return
  542. _keyChar=String.FromCString( tevent->text )
  543. SendKeyEvent( EventType.KeyChar )
  544. Case SDL_MOUSEBUTTONDOWN
  545. Local mevent:=Cast<SDL_MouseButtonEvent Ptr>( event )
  546. _window=Window.WindowForID( mevent->windowID )
  547. If Not _window Return
  548. _mouseLocation=_window.MouseScale * New Vec2i( mevent->x,mevent->y )
  549. _mouseButton=Cast<MouseButton>( mevent->button )
  550. If Not _mouseView
  551. Local mouseView:=ActiveViewAtMouseLocation()
  552. If mouseView
  553. If _touchMouse
  554. _hoverView=mouseView
  555. SendMouseEvent( EventType.MouseEnter,_hoverView )
  556. Endif
  557. If _captureMouse SDL_CaptureMouse( SDL_TRUE )
  558. _mouseView=mouseView
  559. _mouseClicks=mevent->clicks
  560. SendMouseEvent( EventType.MouseDown,_mouseView )
  561. _mouseClicks=0
  562. Endif
  563. Endif
  564. Case SDL_MOUSEBUTTONUP
  565. Local mevent:=Cast<SDL_MouseButtonEvent Ptr>( event )
  566. _window=Window.WindowForID( mevent->windowID )
  567. If Not _window Return
  568. _mouseLocation=_window.MouseScale * New Vec2i( mevent->x,mevent->y )
  569. _mouseButton=Cast<MouseButton>( mevent->button )
  570. If _mouseView
  571. If _captureMouse SDL_CaptureMouse( SDL_FALSE )
  572. SendMouseEvent( EventType.MouseUp,_mouseView )
  573. _mouseButton=Null
  574. _mouseView=Null
  575. If _touchMouse
  576. SendMouseEvent( EventType.MouseLeave,_hoverView )
  577. _hoverView=Null
  578. Endif
  579. Endif
  580. Case SDL_MOUSEMOTION
  581. Local mevent:=Cast<SDL_MouseMotionEvent Ptr>( event )
  582. _window=Window.WindowForID( mevent->windowID )
  583. If Not _window Return
  584. _mouseLocation=_window.MouseScale * New Vec2i( mevent->x,mevent->y )
  585. If Not _touchMouse
  586. Local hoverView:=ActiveViewAtMouseLocation()
  587. If _mouseView And hoverView<>_mouseView hoverView=Null
  588. If hoverView<>_hoverView
  589. If _hoverView SendMouseEvent( EventType.MouseLeave,_hoverView )
  590. _hoverView=hoverView
  591. If _hoverView SendMouseEvent( EventType.MouseEnter,_hoverView )
  592. Endif
  593. Endif
  594. If _mouseView
  595. SendMouseEvent( EventType.MouseMove,_mouseView )
  596. Else If _hoverView
  597. SendMouseEvent( EventType.MouseMove,_hoverView )
  598. Endif
  599. Case SDL_MOUSEWHEEL
  600. Local mevent:=Cast<SDL_MouseWheelEvent Ptr>( event )
  601. _window=Window.WindowForID( mevent->windowID )
  602. If Not _window Return
  603. _mouseWheel=New Vec2i( mevent->x,mevent->y )
  604. If _mouseView
  605. SendMouseEvent( EventType.MouseWheel,_mouseView )
  606. Else If _hoverView
  607. SendMouseEvent( EventType.MouseWheel,_hoverView )
  608. Endif
  609. Case SDL_FINGERDOWN
  610. Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
  611. _finger=tevent->fingerId
  612. _fingerPressure=tevent->pressure
  613. _fingerCoords=New Vec2f( tevent->x,tevent->y )
  614. SendTouchEvent( EventType.TouchDown )
  615. Case SDL_FINGERUP
  616. Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
  617. _finger=tevent->fingerId
  618. _fingerPressure=tevent->pressure
  619. _fingerCoords=New Vec2f( tevent->x,tevent->y )
  620. SendTouchEvent( EventType.TouchUp )
  621. Case SDL_FINGERMOTION
  622. Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
  623. _finger=tevent->fingerId
  624. _fingerPressure=tevent->pressure
  625. _fingerCoords=New Vec2f( tevent->x,tevent->y )
  626. SendTouchEvent( EventType.TouchMove )
  627. Case SDL_WINDOWEVENT
  628. Local wevent:=Cast<SDL_WindowEvent Ptr>( event )
  629. _window=Window.WindowForID( wevent->windowID )
  630. If Not _window Return
  631. Select wevent->event
  632. Case SDL_WINDOWEVENT_CLOSE
  633. SendWindowEvent( EventType.WindowClose )
  634. Case SDL_WINDOWEVENT_MAXIMIZED
  635. SendWindowEvent( EventType.WindowMaximized )
  636. Case SDL_WINDOWEVENT_MINIMIZED
  637. SendWindowEvent( EventType.WindowMinimized )
  638. Case SDL_WINDOWEVENT_RESTORED
  639. SendWindowEvent( EventType.WindowRestored )
  640. Case SDL_WINDOWEVENT_FOCUS_GAINED
  641. ' Print "SDL_WINDOWEVENT_FOCUS_GAINED"
  642. Local active:=_active
  643. _activeWindow=_window
  644. _active=True
  645. SendWindowEvent( EventType.WindowGainedFocus )
  646. If active<>_active Activated()
  647. Case SDL_WINDOWEVENT_FOCUS_LOST
  648. ' Print "SDL_WINDOWEVENT_FOCUS_LOST"
  649. Local active:=_active
  650. _active=False
  651. If _mouseView And Not _captureMouse
  652. SendMouseEvent( EventType.MouseUp,_mouseView )
  653. _mouseView=Null
  654. Endif
  655. If _hoverView
  656. SendMouseEvent( EventType.MouseLeave,_hoverView )
  657. _hoverView=Null
  658. Endif
  659. SendWindowEvent( EventType.WindowLostFocus )
  660. If active<>_active Deactivated()
  661. Case SDL_WINDOWEVENT_LEAVE
  662. If _mouseView And Not _captureMouse
  663. SendMouseEvent( EventType.MouseUp,_mouseView )
  664. _mouseView=Null
  665. Endif
  666. If _hoverView
  667. SendMouseEvent( EventType.MouseLeave,_hoverView )
  668. _hoverView=Null
  669. Endif
  670. End
  671. Case SDL_USEREVENT
  672. Local uevent:=Cast<SDL_UserEvent Ptr>( event )
  673. Local event:=Cast<AsyncEvent Ptr>( uevent->data1 )
  674. event->Dispatch()
  675. Case SDL_DROPFILE
  676. Local devent:=Cast<SDL_DropEvent Ptr>( event )
  677. Local path:=String.FromCString( devent->file ).Replace( "\","/" )
  678. SDL_free( devent->file )
  679. FileDropped( path )
  680. Case SDL_RENDER_TARGETS_RESET
  681. 'Print "SDL_RENDER_TARGETS_RESET"
  682. RequestRender()
  683. Case SDL_RENDER_DEVICE_RESET
  684. 'Print "SDL_RENDER_DEVICE_RESET"
  685. mojo.graphics.glutil.glGraphicsSeq+=1
  686. #if __TARGET__="ios"
  687. Case SDL_APP_TERMINATING
  688. 'Terminate the app.
  689. 'Shut everything down before returning from this function.
  690. Case SDL_APP_LOWMEMORY
  691. 'You will get this when your app is paused and iOS wants more memory.
  692. 'Release as much memory as possible.
  693. Case SDL_APP_WILLENTERBACKGROUND
  694. 'Prepare your app to go into the background. Stop loops, etc.
  695. 'This gets called when the user hits the home button, or gets a call.
  696. 'Print "SDL_APP_WILLENTERBACKGROUND"
  697. SuspendRendering()
  698. Case SDL_APP_DIDENTERBACKGROUND
  699. 'This will get called if the user accepted whatever sent your app to the background.
  700. 'If the user got a phone call and canceled it, you'll instead get an SDL_APP_DIDENTERFOREGROUND event and restart your loops.
  701. 'When you get this, you have 5 seconds to save all your state or the app will be terminated.
  702. 'Your app is NOT active at this point.
  703. Case SDL_APP_WILLENTERFOREGROUND
  704. 'This call happens when your app is coming back to the foreground.
  705. 'Restore all your state here.
  706. Case SDL_APP_DIDENTERFOREGROUND
  707. 'Restart your loops here.
  708. 'Your app is interactive and getting CPU again.
  709. 'Print "SDL_APP_DIDENTERFOREGROUND"
  710. ResumeRendering()
  711. RequestRender()
  712. #Endif
  713. End
  714. End
  715. Function _EventFilter:Int( userData:Void Ptr,event:SDL_Event Ptr )
  716. Return App.EventFilter( userData,event )
  717. End
  718. Method EventFilter:Int( userData:Void Ptr,event:SDL_Event Ptr )
  719. Select event[0].type
  720. Case SDL_WINDOWEVENT
  721. Local wevent:=Cast<SDL_WindowEvent Ptr>( event )
  722. _window=Window.WindowForID( wevent->windowID )
  723. If Not _window Return 1
  724. Select wevent->event
  725. Case SDL_WINDOWEVENT_MOVED
  726. ' Print "SDL_WINDOWEVENT_MOVED"
  727. SdlEventFilter( event )
  728. SendWindowEvent( EventType.WindowMoved )
  729. Return 0
  730. Case SDL_WINDOWEVENT_RESIZED
  731. ' Print "SDL_WINDOWEVENT_RESIZED"
  732. SdlEventFilter( event )
  733. SendWindowEvent( EventType.WindowResized )
  734. UpdateWindows()
  735. Return 0
  736. End
  737. End
  738. Return 1
  739. End
  740. Method CreateDisplayMode:DisplayMode( sdlMode:SDL_DisplayMode Ptr )
  741. Local mode:=New DisplayMode
  742. #If __TARGET__="emscripten"
  743. mode.width=640
  744. mode.height=480
  745. #else
  746. mode.width=sdlMode->w
  747. mode.height=sdlMode->h
  748. mode.depth=SDL_BYTESPERPIXEL( sdlMode->format )*8
  749. mode.hertz=sdlMode->refresh_rate
  750. #endif
  751. Return mode
  752. End
  753. End