gamecontroller.monkey2 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. Namespace mojo.input
  2. #rem monkeydoc @hidden
  3. #end
  4. Enum GameControllerButton 'uses SDL values
  5. A=0
  6. B=1
  7. X=2
  8. Y=3
  9. Back=4
  10. Guide=5
  11. Start=6
  12. LeftStick=7
  13. RightStick=8
  14. LeftShoulder=9
  15. RightShoulder=10
  16. DpadUp=11
  17. DpadDown=12
  18. DpadLeft=13
  19. DpadRight=14
  20. End
  21. #rem monkeydoc @hidden
  22. #end
  23. Enum GameControllerAxis 'uses SDL values
  24. LeftX=0
  25. LeftY=1
  26. RightX=2
  27. RightY=3
  28. LeftTrigger=4
  29. RightTrigger=5
  30. End
  31. #rem monkeydoc @hidden
  32. #end
  33. Class GameController Extends InputDevice
  34. Method New( axisDevice:InputDevice,buttonDevice:InputDevice,_pointerDevice:InputDevice )
  35. _axisDevice=axisDevice
  36. _buttonDevice=buttonDevice
  37. _pointerDevice=pointerDevice
  38. For Local i:=0 Until _axisMap.Length
  39. _axisMap[i]=i
  40. Next
  41. For Local i:=0 Until _buttonMap.Length
  42. _buttonMap[i]=i
  43. Next
  44. For Local i:=0 Until _pointerMap.Length
  45. _pointerMap[i]=i
  46. Next
  47. End
  48. Method GetAxis:Float( axis:GameControllerAxis ) Override
  49. If _axisDevice Return _axisDevice.GetAxis( _axisMap[axis] )
  50. Return 0
  51. End
  52. Method GetButton:Bool( button:GameControllerButton ) Override
  53. If _buttonDevice Return _buttonDevice.GetButton( _buttonMap[button] )
  54. Return False
  55. End
  56. Method GetPointer:Vec2i( pointer:Int ) Override
  57. If _pointerDevice Return _pointerDevice.GetPointer( _pointerMap[pointer] )
  58. End
  59. Method MapAxis( axis:GameControllerAxis,deviceAxis:Int )
  60. _axisMap[axis]=deviceAxis
  61. End
  62. Method MapButton( button:GameControllerButton,deviceButton:Int )
  63. _buttonMap[button]=deviceButton
  64. End
  65. Method MapPointer( pointer:Int,devicePointer:Int )
  66. _pointerMap[pointer]=devicePointer
  67. End
  68. Function WASD:GameController()
  69. If Not _wasd
  70. _wasd=New GameController( Null,Keyboard )
  71. _wasd.MapButton( GameControllerButton.DpadLeft,Key.A )
  72. _wasd.MapButton( GameControllerButton.DpadRight,Key.D )
  73. _wasd.MapButton( GameControllerButton.DpadUp,Key.W )
  74. _wasd.MapButton( GameControllerButton.DpadDown,Key.S )
  75. Endif
  76. Return _wasd
  77. End
  78. Private
  79. Global _wasd:GameController
  80. Field _axisDevice:InputDevice
  81. Field _buttonDevice:InputDevice
  82. Field _pointerDevice:InputDevice
  83. Field _axisMap:=New Int[6]
  84. Field _buttonMap:=New Int[16]
  85. Field _pointerMap:=New Int[10]
  86. End