sdlsystem.bmx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. ' Copyright (c) 2014-2022 Bruce A Henderson
  2. '
  3. ' This software is provided 'as-is', without any express or implied
  4. ' warranty. In no event will the authors be held liable for any damages
  5. ' arising from the use of this software.
  6. '
  7. ' Permission is granted to anyone to use this software for any purpose,
  8. ' including commercial applications, and to alter it and redistribute it
  9. ' freely, subject to the following restrictions:
  10. '
  11. ' 1. The origin of this software must not be misrepresented; you must not
  12. ' claim that you wrote the original software. If you use this software
  13. ' in a product, an acknowledgment in the product documentation would be
  14. ' appreciated but is not required.
  15. '
  16. ' 2. Altered source versions must be plainly marked as such, and must not be
  17. ' misrepresented as being the original software.
  18. '
  19. ' 3. This notice may not be removed or altered from any source
  20. ' distribution.
  21. '
  22. SuperStrict
  23. Rem
  24. bbdoc: SDL System driver
  25. End Rem
  26. Module SDL.SDLSystem
  27. Import SDL.SDL
  28. Import BRL.System
  29. Import BRL.LinkedList
  30. Import Pub.NFD
  31. Import Brl.StringBuilder
  32. Import "common.bmx"
  33. Global _sdl_WarpMouse(x:Int, y:Int)
  34. Type TSDLSystemDriver Extends TSystemDriver
  35. Field _eventFilterCallback:Int(data:Object, event:Int)
  36. Field _eventFilterUserData:Object
  37. Method New()
  38. SDL_Init(SDL_INIT_EVENTS)
  39. bmx_SDL_SetEventFilter(Self)
  40. OnEnd(SDL_Quit)
  41. End Method
  42. Method Poll() Override
  43. bmx_SDL_Poll()
  44. End Method
  45. Method Wait() Override
  46. bmx_SDL_WaitEvent()
  47. End Method
  48. Method Emit( osevent:Byte Ptr,source:Object )
  49. ' TODO
  50. End Method
  51. Method SetMouseVisible( visible:Int ) Override
  52. SDL_ShowCursor(visible)
  53. End Method
  54. Method MoveMouse( x:Int,y:Int ) Override
  55. If _sdl_WarpMouse Then
  56. _sdl_WarpMouse(x, y)
  57. End If
  58. End Method
  59. Method Notify( Text:String,serious:Int ) Override
  60. Local res:Int = bmx_SDL_ShowSimpleMessageBox(Text, AppTitle, serious)
  61. ' failed to display message box?
  62. If res Then
  63. WriteStdout Text+"~r~n"
  64. End If
  65. End Method
  66. Method Confirm:Int( Text:String,serious:Int ) Override
  67. Return bmx_SDL_ShowMessageBox_confirm(Text, AppTitle, serious)
  68. End Method
  69. Method Proceed:Int( Text:String,serious:Int ) Override
  70. Return bmx_SDL_ShowMessageBox_proceed(Text, AppTitle, serious)
  71. End Method
  72. Method RequestFile:String( Text:String,exts:String,save:Int,file:String ) Override
  73. ?Not haiku and Not ios
  74. Local requestedFile:String
  75. Local res:Int
  76. Local defaultPath:Byte Ptr
  77. Local outPath:Byte Ptr
  78. Local filterList:Byte Ptr
  79. If file Then
  80. defaultPath = file.ToUTF8String()
  81. End If
  82. If exts Then
  83. Local groups:String[] = exts.Split(";")
  84. Local sb:TStringBuilder = New TStringBuilder
  85. For Local group:String = EachIn groups
  86. Local i:Int = group.Find(":")
  87. Local ext:String
  88. If i > -1 Then
  89. ext = group[i + 1..]
  90. Else
  91. ext = group
  92. End If
  93. If ext <> "*" Then
  94. If sb.Length() Then
  95. sb.Append(";")
  96. End If
  97. sb.Append(ext)
  98. End If
  99. Next
  100. filterList = sb.ToString().ToUTF8String()
  101. End If
  102. If save Then
  103. res = NFD_SaveDialog(filterList, defaultPath, Varptr outPath)
  104. Else
  105. res = NFD_OpenDialog(filterList, defaultPath, Varptr outPath)
  106. End If
  107. If res = 1 And outPath Then
  108. requestedFile = String.FromUTF8String(outPath)
  109. free_(outPath)
  110. End If
  111. If defaultPath Then
  112. MemFree(defaultPath)
  113. End If
  114. If filterList Then
  115. MemFree(filterList)
  116. End If
  117. Return requestedFile
  118. ?
  119. End Method
  120. Method RequestDir:String( Text:String,path:String ) Override
  121. ?Not haiku And Not ios
  122. Local requestedDir:String
  123. Local res:Int
  124. Local defaultPath:Byte Ptr
  125. Local outPath:Byte Ptr
  126. If path Then
  127. defaultPath = path.ToUTF8String()
  128. End If
  129. res = NFD_PickFolder(defaultPath, Varptr outPath)
  130. If res = 1 And outPath Then
  131. requestedDir = String.FromUTF8String(outPath)
  132. free_(outPath)
  133. End If
  134. If defaultPath Then
  135. MemFree(defaultPath)
  136. End If
  137. Return requestedDir
  138. ?
  139. End Method
  140. Method OpenURL:Int( url:String ) Override
  141. Local u:Byte Ptr = url.ToUTF8String()
  142. Local res:Int = SDL_OpenURL(u)
  143. MemFree(u)
  144. If Not res Then
  145. Return True
  146. End If
  147. End Method
  148. Method DesktopWidth:Int(display:Int) Override
  149. Return bmx_SDL_GetDisplayWidth(display)
  150. End Method
  151. Method DesktopHeight:Int(display:Int) Override
  152. Return bmx_SDL_GetDisplayHeight(display)
  153. End Method
  154. Method DesktopDepth:Int(display:Int) Override
  155. Return bmx_SDL_GetDisplayDepth(display)
  156. End Method
  157. Method DesktopHertz:Int(display:Int) Override
  158. Return bmx_SDL_GetDisplayhertz(display)
  159. End Method
  160. Function _eventFilter:Int(driver:TSDLSystemDriver, event:Int) { nomangle }
  161. If driver._eventFilterCallback Then
  162. Return driver._eventFilterCallback(driver._eventFilterUserData, event)
  163. End If
  164. Return 1
  165. End Function
  166. Method Name:String() Override
  167. Return "SDLSystemDriver"
  168. End Method
  169. End Type
  170. Rem
  171. bbdoc:
  172. End Rem
  173. Function SetEventFilterCallback(callback:Int(data:Object, event:Int), data:Object = Null)
  174. TSDLSystemDriver(SystemDriver())._eventFilterCallback = callback
  175. TSDLSystemDriver(SystemDriver())._eventFilterUserData = data
  176. End Function
  177. InitSystemDriver(New TSDLSystemDriver)
  178. Rem
  179. bbdoc: Information about multiple finger gestures.
  180. End Rem
  181. Type TSDLMultiGesture
  182. Rem
  183. bbdoc: The touch device id.
  184. End Rem
  185. Field touchId:Long
  186. Rem
  187. bbdoc: The center of the gesture.
  188. End Rem
  189. Field x:Int
  190. Rem
  191. bbdoc: The center of the gesture.
  192. End Rem
  193. Field y:Int
  194. Rem
  195. bbdoc: The amount that the fingers rotated during this motion.
  196. End Rem
  197. Field dTheta:Float
  198. Rem
  199. bbdoc: The amount that the fingers pinched during this motion.
  200. End Rem
  201. Field dDist:Float
  202. Rem
  203. bbdoc: The number of fingers used in the gesture.
  204. End Rem
  205. Field numFingers:Int
  206. Global _gestures:TList = New TList
  207. Function _getGesture:TSDLMultiGesture(touchId:Long, x:Int, y:Int, dTheta:Float, dDist:Float, numFingers:Int) { nomangle }
  208. Local gesture:TSDLMultiGesture = TSDLMultiGesture(_gestures.RemoveFirst())
  209. If Not gesture Then
  210. gesture = New TSDLMultiGesture
  211. End If
  212. gesture.touchId = touchId
  213. gesture.x = x
  214. gesture.y = y
  215. gesture.dTheta = dTheta
  216. gesture.dDist = dDist
  217. gesture.numFingers = numFingers
  218. Return gesture
  219. End Function
  220. Function _freeGesture(gesture:TSDLMultiGesture) { nomangle }
  221. _gestures.AddLast(gesture)
  222. End Function
  223. End Type
  224. Private
  225. Extern
  226. Function NFD_OpenDialog:Int(filterList:Byte Ptr, defaultPath:Byte Ptr, outPath:Byte Ptr Ptr)
  227. Function NFD_SaveDialog:Int(filterList:Byte Ptr, defaultPath:Byte Ptr, outPath:Byte Ptr Ptr)
  228. Function NFD_PickFolder:Int(defaultPath:Byte Ptr, outPath:Byte Ptr Ptr)
  229. Function free_(buf:Byte Ptr)="void free(void *)!"
  230. End Extern
  231. Rem
  232. bbdoc: Start accepting Unicode text input events.
  233. about: This function will start accepting Unicode text input events in the focused SDL window,
  234. and start emitting #SDLTextInputEvent (SDL_TEXTINPUT) and #SDLTextEditingEvent (SDL_TEXTEDITING) events.
  235. Please use this function in pair with #SDLStopTextInput().
  236. On some platforms using this function activates the screen keyboard.
  237. End Rem
  238. Function SDLStartTextInput()
  239. SDL_StartTextInput()
  240. End Function
  241. Rem
  242. bbdoc: Stop receiving any text input events.
  243. End Rem
  244. Function SDLStopTextInput()
  245. SDL_StopTextInput()
  246. End Function
  247. Rem
  248. bbdoc: Returns if an IME Composite or Candidate window is currently shown.
  249. End Rem
  250. Function SDLIsTextInputShown:Int()
  251. Return SDL_IsTextInputShown()
  252. End Function
  253. Rem
  254. bbdoc: Checks whether or not Unicode text input events are enabled.
  255. returns: #True if text input events are enabled else #False.
  256. End Rem
  257. Function SDLIsTextInputActive:Int()
  258. Return SDL_IsTextInputActive()
  259. End Function
  260. Rem
  261. bbdoc: Dismisses the composition window/IME without disabling the subsystem.
  262. End Rem
  263. Function SDLClearComposition()
  264. SDL_ClearComposition()
  265. End Function
  266. Rem
  267. bbdoc: Checks whether the platform has screen keyboard support.
  268. returns: #True if the platform has some screen keyboard support or #False if not.
  269. End Rem
  270. Function SDLHasScreenKeyboardSupport:Int()
  271. Return SDL_HasScreenKeyboardSupport()
  272. End Function