joystick.monkey2 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. Namespace mojo.input
  2. #rem monkeydoc Joystick hat directions.
  3. | JoystickHat value |
  4. |:------------------|
  5. | Centered
  6. | Up
  7. | Right
  8. | Down
  9. | Left
  10. | RightUp
  11. | RightDown
  12. | LeftUp
  13. | LeftDown
  14. #end
  15. Enum JoystickHat 'SDL values...
  16. Centered=0
  17. Up=1
  18. Right=2
  19. Down=4
  20. Left=8
  21. RightUp=Right|Up
  22. RightDown=Right|Down
  23. LeftUp=Left|Up
  24. LeftDown=Left|Down
  25. End
  26. #rem monkeydoc The JoystickDevice class.
  27. #end
  28. Class JoystickDevice Extends InputDevice
  29. #rem monkeydoc @hidden
  30. #end
  31. Property Name:String()
  32. Return _name
  33. End
  34. #rem monkeydoc @hidden
  35. #end
  36. Property GUID:String()
  37. Return _guid
  38. End
  39. #rem monkeydoc The number of axes supported by the joystick.
  40. #end
  41. Property NumAxes:Int()
  42. Return _numAxes
  43. End
  44. #rem monkeydoc The number of balls upported by the joystick.
  45. #end
  46. Property NumBalls:Int()
  47. Return _numBalls
  48. End
  49. #rem monkeydoc The number of buttons supported by the joystick.
  50. #end
  51. Property NumButtons:Int()
  52. Return _numButtons
  53. End
  54. #rem monkeydoc The number of hats supported by the joystick.
  55. #end
  56. Property NumHats:Int()
  57. Return _numHats
  58. End
  59. #rem monkeydoc Gets joystick axis value in the range -1 to 1.
  60. #end
  61. Method GetAxis:Float( axis:Int )
  62. Return (Float(SDL_JoystickGetAxis( _joystick,axis ))+32768)/32767.5-1
  63. End
  64. #rem monkeydoc Gets joystick ball value.
  65. #end
  66. Method GetBall:Vec2i( ball:Int )
  67. Local x:Int,y:Int
  68. SDL_JoystickGetBall( _joystick,ball,Varptr x,Varptr y )
  69. Return New Vec2i( x,y )
  70. End
  71. #rem monkeydoc Gets joystick hat value.
  72. #end
  73. Method GetHat:JoystickHat( hat:Int )
  74. Return Cast<JoystickHat>( SDL_JoystickGetHat( _joystick,hat ) )
  75. End
  76. #rem monkeydoc Check up/down state of a button.
  77. #end
  78. Method ButtonDown:Bool( button:Int )
  79. Return SDL_JoystickGetButton( _joystick,button )
  80. End
  81. #rem monkeydoc Checks is a button has been pressed.
  82. #end
  83. Method ButtonPressed:Bool( button:Int )
  84. If ButtonDown( button )
  85. If _hits[button] Return False
  86. _hits[button]=True
  87. Return True
  88. Endif
  89. _hits[button]=False
  90. Return False
  91. End
  92. #rem monkeydoc Gets the number of joysticks attached.
  93. #end
  94. Function NumJoysticks:Int()
  95. Return Min( SDL_NumJoysticks(),8 )
  96. End
  97. #rem monkeydoc @hidden
  98. #end
  99. Function UpdateJoysticks()
  100. SDL_JoystickUpdate()
  101. End
  102. #rem monkeydoc Opens a joystick device.
  103. @param index Joystick index.
  104. #end
  105. Function Open:JoystickDevice( index:Int )
  106. Assert( index>=0 And index<8 )
  107. Local joystick:=_joysticks[index]
  108. If Not joystick
  109. Local sdlJoystick:=SDL_JoystickOpen( index )
  110. If Not sdlJoystick Return Null
  111. joystick=New JoystickDevice( sdlJoystick )
  112. _joysticks[index]=joystick
  113. Endif
  114. Return joystick
  115. End
  116. Private
  117. Global _joysticks:=New JoystickDevice[8]
  118. Field _joystick:SDL_Joystick Ptr
  119. Field _name:String
  120. Field _guid:String
  121. Field _numAxes:Int
  122. Field _numBalls:Int
  123. Field _numButtons:Int
  124. Field _numHats:Int
  125. Field _hits:=New Bool[32]
  126. Method New( joystick:SDL_Joystick Ptr )
  127. _joystick=joystick
  128. _name=String.FromCString( SDL_JoystickName( _joystick ) )
  129. _numAxes=SDL_JoystickNumAxes( _joystick )
  130. _numBalls=SDL_JoystickNumBalls( _joystick )
  131. _numButtons=SDL_JoystickNumButtons( _joystick )
  132. _numHats=SDL_JoystickNumHats( _joystick )
  133. Local buf:=New Byte[64]
  134. Local guid:=SDL_JoystickGetGUID( _joystick )
  135. SDL_JoystickGetGUIDString( guid,Cast<libc.char_t Ptr>( buf.Data ),buf.Length )
  136. buf[buf.Length-1]=0
  137. _guid=String.FromCString( buf.Data )
  138. End
  139. End