joystick.bmx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. SuperStrict
  2. Rem
  3. bbdoc: User input/Joystick
  4. End Rem
  5. Module Pub.Joystick
  6. ModuleInfo "Version: 1.00"
  7. ModuleInfo "Author: Bruce A Henderson"
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Bruce A Henderson"
  10. ModuleInfo "History: 1.00 Release"
  11. Const JOY_X:Int = 0
  12. Const JOY_Y:Int = 1
  13. Const JOY_Z:Int = 2
  14. Const JOY_R:Int = 3
  15. Const JOY_U:Int = 4
  16. Const JOY_V:Int = 5
  17. Const JOY_YAW:Int = 6
  18. Const JOY_PITCH:Int = 7
  19. Const JOY_ROLL:Int = 8
  20. Const JOY_HAT:Int = 9
  21. Const JOY_WHEEL:Int = 10
  22. Private
  23. Global current_joystick:TJoystickDriver
  24. Public
  25. Function JoyCount:Int()
  26. Assert current_joystick
  27. Return current_joystick.JoyCount()
  28. End Function
  29. Function JoyName:String(port:Int)
  30. Assert current_joystick
  31. Return current_joystick.JoyName(port)
  32. End Function
  33. Rem
  34. bbdoc: Available buttons (on/off controls) on a joystick.
  35. returns: A bitfield representing which buttons are present.
  36. end rem
  37. Function JoyButtonCaps:Int(port:Int)
  38. Assert current_joystick
  39. Return current_joystick.JoyButtonCaps(port)
  40. End Function
  41. Rem
  42. bbdoc: Available axis (proportional controls) on a joystick.
  43. returns: A bitfield representing which axis are available.
  44. about:
  45. The bit positions of the returned value correspond to the following constants defined
  46. in the FreeJoy module:
  47. [ Const JOY_X=0
  48. * Const JOY_Y=1
  49. * Const JOY_Z=2
  50. * Const JOY_R=3
  51. * Const JOY_U=4
  52. * Const JOY_V=5
  53. * Const JOY_YAW=6
  54. * Const JOY_PITCH=7
  55. * Const JOY_ROLL=8
  56. * Const JOY_HAT=9
  57. * Const JOY_WHEEL=10
  58. ]
  59. End Rem
  60. Function JoyAxisCaps:Int(port:Int)
  61. Assert current_joystick
  62. Return current_joystick.JoyAxisCaps(port)
  63. End Function
  64. Rem
  65. bbdoc: Test the status of a joystick button.
  66. returns: True if the button is pressed.
  67. end rem
  68. Function JoyDown:Int( button:Int, port:Int=0 )
  69. Assert current_joystick
  70. Return current_joystick.JoyDown(button, port)
  71. End Function
  72. Rem
  73. bbdoc: Check for a joystick button press
  74. returns: Number of times @button has been hit.
  75. about:
  76. The returned value represents the number of the times @button has been hit since
  77. the last call to #JoyHit with the same specified @button.
  78. End Rem
  79. Function JoyHit:Int( button:Int, port:Int=0 )
  80. Assert current_joystick
  81. Return current_joystick.JoyHit(button, port)
  82. End Function
  83. Rem
  84. bbdoc: Reports the horizontal position of the joystick.
  85. returns: Zero if the joystick is centered, -1 if Left, 1 if Right or a value inbetween.
  86. End Rem
  87. Function JoyX:Float( port:Int=0 )
  88. Assert current_joystick
  89. Return current_joystick.JoyX(port)
  90. End Function
  91. Rem
  92. bbdoc: Reports the vertical position of the joystick.
  93. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  94. End Rem
  95. Function JoyY:Float( port:Int=0 )
  96. Assert current_joystick
  97. Return current_joystick.JoyY(port)
  98. End Function
  99. Rem
  100. bbdoc: Reports the position of the joystick's Z axis if supported.
  101. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  102. End Rem
  103. Function JoyZ:Float( port:Int=0 )
  104. Assert current_joystick
  105. Return current_joystick.JoyZ(port)
  106. End Function
  107. Rem
  108. bbdoc: Reports the position of the joystick's R axis if supported.
  109. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  110. End Rem
  111. Function JoyR:Float( port:Int=0 )
  112. Assert current_joystick
  113. Return current_joystick.JoyR(port)
  114. End Function
  115. Rem
  116. bbdoc: Reports the position of the joystick's U axis if supported.
  117. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  118. about:
  119. The U value of a joystick usually corresponds to a joystick's 'slider' or 'throttle' feature, although this may vary depending on the joystick, and will not be available with all joysticks.
  120. End Rem
  121. Function JoyU:Float( port:Int=0 )
  122. Assert current_joystick
  123. Return current_joystick.JoyU(port)
  124. End Function
  125. Rem
  126. bbdoc: Reports the position of the joystick's V axis if supported.
  127. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  128. about:
  129. The V value of a joystick usually corresponds to a joystick's 'slider' or 'throttle' feature, although this may vary depending on the joystick, and will not be available with all joysticks.
  130. End Rem
  131. Function JoyV:Float( port:Int=0 )
  132. Assert current_joystick
  133. Return current_joystick.JoyV(port)
  134. End Function
  135. Rem
  136. bbdoc: Reports the position of the joystick's YAW axis if supported.
  137. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  138. End Rem
  139. Function JoyYaw:Float( port:Int=0 )
  140. Assert current_joystick
  141. Return current_joystick.JoyYaw(port)
  142. End Function
  143. Rem
  144. bbdoc: Reports the position of the joystick's PITCH axis if supported.
  145. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  146. End Rem
  147. Function JoyPitch:Float( port:Int=0 )
  148. Assert current_joystick
  149. Return current_joystick.JoyPitch(port)
  150. End Function
  151. Rem
  152. bbdoc: Reports the position of the joystick's ROLL axis if supported.
  153. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  154. End Rem
  155. Function JoyRoll:Float( port:Int=0 )
  156. Assert current_joystick
  157. Return current_joystick.JoyRoll(port)
  158. End Function
  159. Rem
  160. bbdoc: Reports the position of the joystick's HAT controller if supported.
  161. returns: -1.0 if the joystick is centered, and values between 0.0, 0.25, 0.5 and 0.75 for the directions Up, Right, Down, Left respectively.
  162. End Rem
  163. Function JoyHat:Float( port:Int=0 )
  164. Assert current_joystick
  165. Return current_joystick.JoyHat(port)
  166. End Function
  167. Rem
  168. bbdoc: Reports the position of the joystick's WHEEL controller if supported.
  169. returns: Zero if the joystick is centered, -1.0 if Left, 1.0 if Right or a value inbetween.
  170. End Rem
  171. Function JoyWheel:Float( port:Int=0 )
  172. Assert current_joystick
  173. Return current_joystick.JoyWheel(port)
  174. End Function
  175. Function JoyType:Int( port:Int=0 )
  176. Assert current_joystick
  177. Return current_joystick.JoyType(port)
  178. End Function
  179. Function JoyXDir:Int( port:Int=0 )
  180. Assert current_joystick
  181. Return current_joystick.JoyXDir(port)
  182. End Function
  183. Function JoyYDir:Int( port:Int=0 )
  184. Assert current_joystick
  185. Return current_joystick.JoyYDir(port)
  186. End Function
  187. Function JoyZDir:Int( port:Int=0 )
  188. Assert current_joystick
  189. Return current_joystick.JoyZDir(port)
  190. End Function
  191. Function JoyUDir:Int( port:Int=0 )
  192. Assert current_joystick
  193. Return current_joystick.JoyUDir(port)
  194. End Function
  195. Function JoyVDir:Int( port:Int=0 )
  196. Assert current_joystick
  197. Return current_joystick.JoyVDir(port)
  198. End Function
  199. Rem
  200. bbdoc: Flush joystick button states.
  201. End Rem
  202. Function FlushJoy( port_mask:Int=~0 )
  203. Assert current_joystick
  204. current_joystick.FlushJoy(port_mask)
  205. End Function
  206. Private
  207. Global joystick_drivers:TJoystickDriver
  208. Public
  209. Rem
  210. bbdoc: Abstract base type for joystick drivers.
  211. about:
  212. To create a new joystick driver, you should extend #TJoystickDriver and implement the #GetName method.
  213. To install your joystick driver, simply create an instance of it using #New.
  214. End Rem
  215. Type TJoystickDriver
  216. Field _succ:TJoystickDriver
  217. Rem
  218. bbdoc: Creates a new instance of a joystick driver.
  219. End Rem
  220. Method New()
  221. _succ=joystick_drivers
  222. joystick_drivers=Self
  223. End Method
  224. Rem
  225. bbdoc: Returns the name of the driver.
  226. End Rem
  227. Method GetName:String() Abstract
  228. Method JoyCount:Int() Abstract
  229. Method JoyName:String(port:Int) Abstract
  230. Method JoyButtonCaps:Int(port:Int) Abstract
  231. Method JoyAxisCaps:Int(port:Int) Abstract
  232. Method JoyDown:Int( button:Int, port:Int=0 ) Abstract
  233. Method JoyHit:Int( button:Int, port:Int=0 ) Abstract
  234. Method JoyX:Float( port:Int=0 ) Abstract
  235. Method JoyY:Float( port:Int=0 ) Abstract
  236. Method JoyZ:Float( port:Int=0 ) Abstract
  237. Method JoyR:Float( port:Int=0 ) Abstract
  238. Method JoyU:Float( port:Int=0 ) Abstract
  239. Method JoyV:Float( port:Int=0 ) Abstract
  240. Method JoyYaw:Float( port:Int=0 ) Abstract
  241. Method JoyPitch:Float( port:Int=0 ) Abstract
  242. Method JoyRoll:Float( port:Int=0 ) Abstract
  243. Method JoyHat:Float( port:Int=0 ) Abstract
  244. Method JoyWheel:Float( port:Int=0 ) Abstract
  245. Method JoyType:Int( port:Int=0 ) Abstract
  246. Method JoyXDir:Int( port:Int=0 ) Abstract
  247. Method JoyYDir:Int( port:Int=0 ) Abstract
  248. Method JoyZDir:Int( port:Int=0 ) Abstract
  249. Method JoyUDir:Int( port:Int=0 ) Abstract
  250. Method JoyVDir:Int( port:Int=0 ) Abstract
  251. Method FlushJoy( port_mask:Int=~0 ) Abstract
  252. End Type
  253. Function GetJoystickDriver:TJoystickDriver(name:String)
  254. Local driver:TJoystickDriver = joystick_drivers
  255. While driver
  256. If driver.GetName().ToLower() = name.ToLower() Then
  257. current_joystick = driver
  258. Return driver
  259. End If
  260. driver = driver._succ
  261. Wend
  262. End Function