gamecontroller.monkey2 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. Namespace mojo.input
  2. #rem monkeydoc @hidden
  3. #end
  4. Enum GameControllerAxis 'uses SDL values
  5. LeftX=0
  6. LeftY=1
  7. RightX=2
  8. RightY=3
  9. LeftTrigger=4
  10. RightTrigger=5
  11. End
  12. #rem monkeydoc @hidden
  13. #end
  14. Enum GameControllerButton 'uses SDL values
  15. A=0
  16. B=1
  17. X=2
  18. Y=3
  19. Back=4
  20. Guide=5
  21. Start=6
  22. LeftStick=7
  23. RightStick=8
  24. LeftShoulder=9
  25. RightShoulder=10
  26. DpadUp=11
  27. DpadDown=12
  28. DpadLeft=13
  29. DpadRight=14
  30. End
  31. #rem monkeydoc @hidden
  32. #end
  33. Class GameController
  34. #rem monkeydoc True if game controller is attached.
  35. #end
  36. Property Attached:Bool()
  37. If _discarded Return False
  38. If SDL_GameControllerGetAttached( _sdlcontroller ) Return True
  39. Discard()
  40. Return False
  41. End
  42. #rem monkeydoc Name of game controller
  43. #end
  44. Property Name:String()
  45. If _discarded Return ""
  46. Return SDL_GameControllerName( _sdlcontroller )
  47. End
  48. #rem monkeydoc Gets game controller axis value in the range -1 to 1.
  49. #end
  50. Method GetAxis:Float( axis:GameControllerAxis )
  51. If _discarded Return 0
  52. Return (Float(SDL_GameControllerGetAxis( _sdlcontroller,Cast<SDL_GameControllerAxis>( Int(axis) ) ) )+32768)/32767.5-1
  53. End
  54. #rem monkeydoc Check up/down state of a game controller button.
  55. Returns true if button has been pressed since the last call to [[ButtonPressed]].
  56. #end
  57. Method ButtonDown:Bool( button:GameControllerButton )
  58. If _discarded Return False
  59. Return SDL_GameControllerGetButton( _sdlcontroller,Cast<SDL_GameControllerButton>( Int(button) ) )
  60. End
  61. #rem monkeydoc Gets joystick mapping.
  62. #end
  63. Method GetMapping:String()
  64. If _discarded Return ""
  65. Local mapping:=SDL_GameControllerMapping( _sdlcontroller )
  66. Local r:=String.FromCString( mapping )
  67. SDL_free( mapping )
  68. Return r
  69. End
  70. #rem monkeydoc Closes a game controller.
  71. #end
  72. Method Close()
  73. If _discarded Return
  74. _refs-=1
  75. If Not _refs Discard()
  76. End
  77. #rem monkeydoc Adds a GameController mapping.
  78. Returns 1 if mapping added, 0 if mapping updated, -1 if error.
  79. https://wiki.libsdl.org/SDL_GameControllerAddMapping
  80. #end
  81. Function AddMapping:Int( mapping:String )
  82. Return SDL_GameControllerAddMapping( mapping )
  83. End
  84. #rem monkeydoc Loads game controller mappings from a file.
  85. Returns the number of mappings added.
  86. #end
  87. Function AddMappingsFromFile:Int( path:String )
  88. Local n:=0
  89. For Local mapping:=Eachin LoadString( path,True ).Split( "~n" )
  90. mapping=mapping.Trim()
  91. If Not mapping Or mapping.StartsWith( "#" ) Continue
  92. n+=AddMapping( mapping )>0 ? 1 Else 0
  93. Next
  94. Return n
  95. End
  96. #rem
  97. Function GetAxisName:String( axis:GameControllerAxis )
  98. Return SDL_GameControllerGetStringForAxis( Cast<SDL_GameControllerAxis>( Int(axis) ) )
  99. End
  100. Function GetButtonName:String( button:GameControllerButton )
  101. Return SDL_GameControllerGetStringForButton( Cast<SDL_GameControllerButton>( int(button) ) )
  102. End
  103. #end
  104. Function Open:GameController( index:Int )
  105. If index<0 Or index>=MaxControllers Return Null
  106. Local controller:=_controllers[index]
  107. If controller?.Attached
  108. controller._refs+=1
  109. Return controller
  110. End
  111. For Local devid:=0 Until JoystickDevice.NumJoysticks()
  112. If Not SDL_IsGameController( devid ) Continue
  113. Local sdlcontroller:=SDL_GameControllerOpen( devid )
  114. If Not sdlcontroller Continue
  115. Local inst:=SDL_JoystickInstanceID( SDL_GameControllerGetJoystick( sdlcontroller ) )
  116. If _opened[inst]
  117. SDL_GameControllerClose( sdlcontroller )
  118. Continue
  119. Endif
  120. Local controller:=New GameController( index,sdlcontroller,inst )
  121. Return controller
  122. Next
  123. Return Null
  124. End
  125. Private
  126. Const MaxControllers:=8
  127. Global _opened:=New IntMap<GameController>
  128. Global _controllers:=New GameController[MaxControllers]
  129. Field _refs:=1
  130. Field _discarded:=False
  131. Field _index:Int
  132. Field _sdlcontroller:SDL_GameController Ptr
  133. Field _inst:Int
  134. Field _hits:=New Bool[16]
  135. Method New( index:Int,sdlcontroller:SDL_GameController Ptr,inst:Int )
  136. _index=index
  137. _sdlcontroller=sdlcontroller
  138. _inst=inst
  139. _controllers[_index]=Self
  140. _opened[_inst]=Self
  141. End
  142. Method Discard()
  143. if _discarded Return
  144. SDL_GameControllerClose( _sdlcontroller )
  145. _controllers[_index]=Null
  146. _opened.Remove( _inst )
  147. _discarded=True
  148. End
  149. End