joystick.monkey2 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. Namespace mojo.input
  2. Private
  3. Const DEBUG:=False
  4. Public
  5. #rem monkeydoc Type alias for compatibility.
  6. The name JoystickDevice has been deprecated in favor of plain Joystick.
  7. #end
  8. Alias JoystickDevice:Joystick
  9. #rem monkeydoc Joystick hat directions.
  10. | JoystickHat value |
  11. |:------------------|
  12. | Centered
  13. | Up
  14. | Right
  15. | Down
  16. | Left
  17. | RightUp
  18. | RightDown
  19. | LeftUp
  20. | LeftDown
  21. #end
  22. Enum JoystickHat 'SDL values...
  23. Centered=0
  24. Up=1
  25. Right=2
  26. Down=4
  27. Left=8
  28. RightUp=Right|Up
  29. RightDown=Right|Down
  30. LeftUp=Left|Up
  31. LeftDown=Left|Down
  32. End
  33. #rem monkeydoc The Joystick class.
  34. #end
  35. Class Joystick
  36. #rem monkeydoc True if joystick is currently attached.
  37. #end
  38. Property Attached:Bool()
  39. If _discarded Return False
  40. If SDL_JoystickGetAttached( _sdljoystick ) Return True
  41. Discard()
  42. Return False
  43. End
  44. #rem monkeydoc Joystick device name.
  45. #end
  46. Property Name:String()
  47. If _discarded Return ""
  48. Return SDL_JoystickName( _sdljoystick )
  49. End
  50. #rem monkeydoc Joystick GUID.
  51. #end
  52. Property GUID:String()
  53. If _discarded Return ""
  54. Local buf:=New Byte[64]
  55. Local guid:=SDL_JoystickGetGUID( _sdljoystick )
  56. SDL_JoystickGetGUIDString( guid,Cast<libc.char_t Ptr>( buf.Data ),buf.Length )
  57. buf[buf.Length-1]=0
  58. Return String.FromCString( buf.Data )
  59. End
  60. #rem monkeydoc The number of axes supported by the joystick.
  61. #end
  62. Property NumAxes:Int()
  63. If _discarded Return 0
  64. Return SDL_JoystickNumAxes( _sdljoystick )
  65. End
  66. #rem monkeydoc The number of balls upported by the joystick.
  67. #end
  68. Property NumBalls:Int()
  69. If _discarded Return 0
  70. Return SDL_JoystickNumBalls( _sdljoystick )
  71. End
  72. #rem monkeydoc The number of buttons supported by the joystick.
  73. #end
  74. Property NumButtons:Int()
  75. If _discarded Return 0
  76. Return SDL_JoystickNumButtons( _sdljoystick )
  77. End
  78. #rem monkeydoc The number of hats supported by the joystick.
  79. #end
  80. Property NumHats:Int()
  81. If _discarded Return 0
  82. Return SDL_JoystickNumHats( _sdljoystick )
  83. End
  84. #rem monkeydoc Gets joystick axis value and returns a result in the range -1 to 1.
  85. The `axis` parameter must be in the range 0 (inclusive) to [[NumAxes]] (exclusive).
  86. The returned value is in the range -1.0 to +1.0.
  87. #end
  88. Method GetAxis:Float( axis:Int )
  89. If _discarded Return 0
  90. Return (Float(SDL_JoystickGetAxis( _sdljoystick,axis ))+32768)/32767.5-1
  91. End
  92. #rem monkeydoc Gets joystick ball value.
  93. The `ball` parameter must be in the range 0 (inclusive) to [[NumBalls]] (exclusive).
  94. #end
  95. Method GetBall:Vec2i( ball:Int )
  96. If _discarded Return Null
  97. Local x:Int,y:Int
  98. SDL_JoystickGetBall( _sdljoystick,ball,Varptr x,Varptr y )
  99. Return New Vec2i( x,y )
  100. End
  101. #rem monkeydoc Gets joystick hat value.
  102. The `hat` parameter must be in the range 0 (inclusive) to [[NumHats]] (exclusive).
  103. #end
  104. Method GetHat:JoystickHat( hat:Int )
  105. If _discarded Return JoystickHat.Centered
  106. Return Cast<JoystickHat>( SDL_JoystickGetHat( _sdljoystick,hat ) )
  107. End
  108. #rem monkeydoc Gets button up/down state.
  109. The `button` parameter must be in the range 0 (inclusive) to [[NumButtons]] (exclusive).
  110. The returned value is true if button is down, else false.
  111. #end
  112. Method ButtonDown:Bool( button:Int )
  113. If _discarded Return False
  114. Return SDL_JoystickGetButton( _sdljoystick,button )
  115. End
  116. #rem monkeydoc Checks if a button has been pressed.
  117. The `button` parameter must be in the range 0 (inclusive) to [[NumButtons]] (exclusive).
  118. Returns true if button has been pressed since the last call to [[ButtonPressed]].
  119. #end
  120. Method ButtonPressed:Bool( button:Int )
  121. If _discarded Return False
  122. If ButtonDown( button )
  123. If _hits[button] Return False
  124. _hits[button]=True
  125. Return True
  126. Endif
  127. _hits[button]=False
  128. Return False
  129. End
  130. #rem monkeydoc Closes the joystick.
  131. #end
  132. Method Close()
  133. If _discarded Return
  134. _refs-=1
  135. If Not _refs Discard()
  136. End
  137. #rem monkeydoc Gets the number of attached joysticks.
  138. #end
  139. Function NumJoysticks:Int()
  140. Return Min( SDL_NumJoysticks(),MaxJoysticks )
  141. End
  142. #rem monkeydoc Opens a joystick if possible.
  143. @param index Joystick index.
  144. #end
  145. Function Open:Joystick( index:Int )
  146. If index<0 Or index>=MaxJoysticks Return Null
  147. Local joystick:=_joysticks[index]
  148. If joystick?.Attached
  149. joystick._refs+=1
  150. Return joystick
  151. End
  152. For Local devid:=0 Until NumJoysticks()
  153. Local sdljoystick:=SDL_JoystickOpen( devid )
  154. If Not sdljoystick Continue
  155. Local inst:=SDL_JoystickInstanceID( sdljoystick )
  156. If _opened[inst]
  157. SDL_JoystickClose( sdljoystick )
  158. Continue
  159. Endif
  160. Local joystick:=New Joystick( index,sdljoystick,inst )
  161. Return joystick
  162. Next
  163. Return Null
  164. End
  165. Internal
  166. Function UpdateJoysticks()
  167. SDL_JoystickUpdate()
  168. End
  169. Function SendEvent( event:SDL_Event Ptr )
  170. Select event->type
  171. Case SDL_JOYDEVICEADDED
  172. Local jevent:=Cast<SDL_JoyDeviceEvent Ptr>( event )
  173. If DEBUG Print "SDL_JOYDEVICEADDED, device id="+jevent->which
  174. Case SDL_JOYDEVICEREMOVED
  175. Local jevent:=Cast<SDL_JoyDeviceEvent Ptr>( event )
  176. If DEBUG Print "SDL_JOYDEVICEREMOVED, inst id="+jevent->which
  177. End
  178. End
  179. Private
  180. Const MaxJoysticks:=8
  181. 'currently opened joysticks by instance id.
  182. Global _opened:=New IntMap<Joystick>
  183. 'curently opened joysticks by user id.
  184. Global _joysticks:=New Joystick[MaxJoysticks]
  185. Function GetGUID:String( joystick:SDL_Joystick Ptr )
  186. Local buf:=New Byte[64]
  187. Local guid:=SDL_JoystickGetGUID( joystick )
  188. SDL_JoystickGetGUIDString( guid,Cast<libc.char_t Ptr>( buf.Data ),buf.Length )
  189. buf[buf.Length-1]=0
  190. Return String.FromCString( buf.Data )
  191. End
  192. Field _refs:=1
  193. Field _discarded:Bool
  194. Field _index:Int
  195. Field _sdljoystick:SDL_Joystick Ptr
  196. Field _inst:Int
  197. Field _hits:=New Bool[32]
  198. Method New( index:Int,sdljoystick:SDL_Joystick Ptr,inst:Int )
  199. _index=index
  200. _sdljoystick=sdljoystick
  201. _inst=inst
  202. _joysticks[_index]=Self
  203. _opened[_inst]=Self
  204. End
  205. Method Discard()
  206. If _discarded Return
  207. SDL_JoystickClose( _sdljoystick )
  208. _joysticks[_index]=Null
  209. _opened.Remove( _inst )
  210. _discarded=True
  211. End
  212. End