joystick.monkey2 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. Namespace test
  2. #Import "<std>"
  3. #Import "<mojo>"
  4. Using std..
  5. Using mojo..
  6. Function Format:String( v:Float )
  7. Local s:=Sgn(v)
  8. Local n:=Int( Abs(v)*100 )
  9. Return (s>=0 ? "+" Else "-")+(n/100)+"."+(n Mod 100)
  10. End
  11. Class Player
  12. Field id:Int
  13. Field joystick:Joystick
  14. Method New( id:Int )
  15. Self.id=id
  16. Self.joystick=Joystick.Open( id )
  17. End
  18. Method Update( canvas:Canvas )
  19. canvas.DrawText( "Player "+id,0,0 )
  20. 'update joystick state
  21. If joystick And Not joystick.Attached
  22. joystick.Close()
  23. joystick=Null
  24. Endif
  25. If Not joystick
  26. joystick=Joystick.Open( id )
  27. If Not joystick
  28. canvas.DrawText( "No Joystick available",0,16 )
  29. Return
  30. Endif
  31. Endif
  32. 'draw joystick info.
  33. canvas.DrawText( "Name="+joystick.Name,0,16 )
  34. canvas.DrawText( "GUID="+joystick.GUID,0,32 )
  35. For Local axis:=0 Until 6
  36. Local v:=Format( joystick.GetAxis( axis ) )
  37. canvas.DrawText( "A"+axis+"="+v,axis * 80,48 )
  38. Next
  39. For Local button:=0 Until 15
  40. canvas.DrawText( "B"+button+"="+Int( joystick.ButtonDown( button ) ),(button Mod 8)*80,(button/8)*16+64 )
  41. Next
  42. End
  43. End
  44. '***** MainWindow *****
  45. Class MainWindow Extends Window
  46. Field players:=New Player[4]
  47. Method New()
  48. Super.New( "Joystick test",640,480 )
  49. For Local i:=0 Until 4
  50. players[i]=New Player( i )
  51. Next
  52. End
  53. Method OnRender( canvas:Canvas ) Override
  54. RequestRender()
  55. canvas.DrawText( "NumJoysticks="+Joystick.NumJoysticks(),0,0 )
  56. canvas.PushMatrix()
  57. canvas.Translate( 0,16 )
  58. For Local i:=0 Until 4
  59. players[i].Update( canvas )
  60. canvas.Translate( 0,96 )
  61. Next
  62. canvas.PopMatrix()
  63. End
  64. End
  65. Function Main()
  66. New AppInstance
  67. New MainWindow
  68. App.Run()
  69. End