2
0

core_input_gamepad.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [core] example - Gamepad input
  4. --
  5. -- NOTE: This example requires a Gamepad connected to the system
  6. -- raylib is configured to work with Xbox 360 gamepad, check raylib.h for buttons configuration
  7. --
  8. -- This example has been created using raylib 1.6 (www.raylib.com)
  9. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. --
  11. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  12. --
  13. -------------------------------------------------------------------------------------------
  14. -- Initialization
  15. -------------------------------------------------------------------------------------------
  16. local screenWidth = 800
  17. local screenHeight = 450
  18. InitWindow(screenWidth, screenHeight, "raylib [core] example - gamepad input")
  19. local texPs3Pad = LoadTexture("resources/ps3.png")
  20. local texXboxPad = LoadTexture("resources/xbox.png")
  21. SetTargetFPS(60) -- Set target frames-per-second
  22. -------------------------------------------------------------------------------------------
  23. -- Main game loop
  24. while not WindowShouldClose() do -- Detect window close button or ESC key
  25. -- Update
  26. ---------------------------------------------------------------------------------------
  27. -- ...
  28. ---------------------------------------------------------------------------------------
  29. -- Draw
  30. ---------------------------------------------------------------------------------------
  31. BeginDrawing()
  32. ClearBackground(RAYWHITE)
  33. if (IsGamepadAvailable(GAMEPAD.PLAYER1)) then
  34. DrawText(string.format("GP1: %s", GetGamepadName(GAMEPAD.PLAYER1)), 10, 10, 10, BLACK)
  35. if (IsGamepadName(GAMEPAD.PLAYER1, "Xbox 360 Controller")) then
  36. DrawTexture(texXboxPad, 0, 0, DARKGRAY)
  37. -- Draw buttons: xbox home
  38. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_HOME)) then DrawCircle(394, 89, 19, RED) end
  39. -- Draw buttons: basic
  40. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_START)) then DrawCircle(436, 150, 9, RED) end
  41. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_SELECT)) then DrawCircle(352, 150, 9, RED) end
  42. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_X)) then DrawCircle(501, 151, 15, BLUE) end
  43. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_A)) then DrawCircle(536, 187, 15, LIME) end
  44. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_B)) then DrawCircle(572, 151, 15, MAROON) end
  45. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_Y)) then DrawCircle(536, 115, 15, GOLD) end
  46. -- Draw buttons: d-pad
  47. DrawRectangle(317, 202, 19, 71, BLACK)
  48. DrawRectangle(293, 228, 69, 19, BLACK)
  49. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_UP)) then DrawRectangle(317, 202, 19, 26, RED) end
  50. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_DOWN)) then DrawRectangle(317, 202 + 45, 19, 26, RED) end
  51. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_LEFT)) then DrawRectangle(292, 228, 25, 19, RED) end
  52. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_RIGHT)) then DrawRectangle(292 + 44, 228, 26, 19, RED) end
  53. -- Draw buttons: left-right back
  54. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_LB)) then DrawCircle(259, 61, 20, RED) end
  55. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.XBOX_BUTTON_RB)) then DrawCircle(536, 61, 20, RED) end
  56. -- Draw axis: left joystick
  57. DrawCircle(259, 152, 39, BLACK)
  58. DrawCircle(259, 152, 34, LIGHTGRAY)
  59. DrawCircle(259 + (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_LEFT_X)*20),
  60. 152 - (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_LEFT_Y)*20), 25, BLACK)
  61. -- Draw axis: right joystick
  62. DrawCircle(461, 237, 38, BLACK)
  63. DrawCircle(461, 237, 33, LIGHTGRAY)
  64. DrawCircle(461 + (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_RIGHT_X)*20),
  65. 237 - (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_RIGHT_Y)*20), 25, BLACK)
  66. -- Draw axis: left-right triggers
  67. DrawRectangle(170, 30, 15, 70, GRAY)
  68. DrawRectangle(604, 30, 15, 70, GRAY)
  69. DrawRectangle(170, 30, 15, (((1.0 + GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_LT))/2.0)*70), RED)
  70. DrawRectangle(604, 30, 15, (((1.0 + GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_RT))/2.0)*70), RED)
  71. --DrawText(FormatText("Xbox axis LT: %02.02f", GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_LT)), 10, 40, 10, BLACK)
  72. --DrawText(FormatText("Xbox axis RT: %02.02f", GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_RT)), 10, 60, 10, BLACK)
  73. elseif (IsGamepadName(GAMEPAD.PLAYER1, "PLAYSTATION(R)3 Controller")) then
  74. DrawTexture(texPs3Pad, 0, 0, DARKGRAY)
  75. -- Draw buttons: ps
  76. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_PS)) then DrawCircle(396, 222, 13, RED) end
  77. -- Draw buttons: basic
  78. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_SELECT)) then DrawRectangle(328, 170, 32, 13, RED) end
  79. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_START)) then DrawTriangle((Vector2){ 436, 168 }, (Vector2){ 436, 185 }, (Vector2){ 464, 177 }, RED) end
  80. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_TRIANGLE)) then DrawCircle(557, 144, 13, LIME) end
  81. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_CIRCLE)) then DrawCircle(586, 173, 13, RED) end
  82. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_CROSS)) then DrawCircle(557, 203, 13, VIOLET) end
  83. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_SQUARE)) then DrawCircle(527, 173, 13, PINK) end
  84. -- Draw buttons: d-pad
  85. DrawRectangle(225, 132, 24, 84, BLACK)
  86. DrawRectangle(195, 161, 84, 25, BLACK)
  87. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_UP)) then DrawRectangle(225, 132, 24, 29, RED) end
  88. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_DOWN)) then DrawRectangle(225, 132 + 54, 24, 30, RED) end
  89. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_LEFT)) then DrawRectangle(195, 161, 30, 25, RED) end
  90. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_RIGHT)) then DrawRectangle(195 + 54, 161, 30, 25, RED) end
  91. -- Draw buttons: left-right back buttons
  92. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_L1)) then DrawCircle(239, 82, 20, RED) end
  93. if (IsGamepadButtonDown(GAMEPAD.PLAYER1, GAMEPAD.PS3_BUTTON_R1)) then DrawCircle(557, 82, 20, RED) end
  94. -- Draw axis: left joystick
  95. DrawCircle(319, 255, 35, BLACK)
  96. DrawCircle(319, 255, 31, LIGHTGRAY)
  97. DrawCircle(319 + (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.PS3_AXIS_LEFT_X)*20),
  98. 255 + (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.PS3_AXIS_LEFT_Y)*20), 25, BLACK)
  99. -- Draw axis: right joystick
  100. DrawCircle(475, 255, 35, BLACK)
  101. DrawCircle(475, 255, 31, LIGHTGRAY)
  102. DrawCircle(475 + (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.PS3_AXIS_RIGHT_X)*20),
  103. 255 + (GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.PS3_AXIS_RIGHT_Y)*20), 25, BLACK)
  104. -- Draw axis: left-right triggers
  105. DrawRectangle(169, 48, 15, 70, GRAY)
  106. DrawRectangle(611, 48, 15, 70, GRAY)
  107. DrawRectangle(169, 48, 15, (((1.0 - GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.PS3_AXIS_L2))/2.0)*70), RED)
  108. DrawRectangle(611, 48, 15, (((1.0 - GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.PS3_AXIS_R2))/2.0)*70), RED)
  109. else
  110. DrawText("- GENERIC GAMEPAD -", 280, 180, 20, GRAY)
  111. -- TODO: Draw generic gamepad
  112. end
  113. DrawText(string.format("DETECTED AXIS [%i]:", GetGamepadAxisCount(GAMEPAD.PLAYER1)), 10, 50, 10, MAROON)
  114. for i = 1, GetGamepadAxisCount(GAMEPAD.PLAYER1) do -- Iterate along all the rectangles
  115. DrawText(string.format("AXIS %i: %.02f", i, GetGamepadAxisMovement(GAMEPAD.PLAYER1, i)), 20, 70 + 20*i, 10, DARKGRAY)
  116. end
  117. if (GetGamepadButtonPressed() ~= -1) then DrawText(string.format("DETECTED BUTTON: %i", GetGamepadButtonPressed()), 10, 430, 10, RED)
  118. else DrawText("DETECTED BUTTON: NONE", 10, 430, 10, GRAY) end
  119. else
  120. DrawText("GP1: NOT DETECTED", 10, 10, 10, GRAY)
  121. DrawTexture(texXboxPad, 0, 0, LIGHTGRAY)
  122. end
  123. EndDrawing()
  124. ---------------------------------------------------------------------------------------
  125. end
  126. -- De-Initialization
  127. -------------------------------------------------------------------------------------------
  128. UnloadTexture(texPs3Pad) -- Unload gamepad texture
  129. UnloadTexture(texXboxPad) -- Unload gamepad texture
  130. CloseWindow() -- Close window and OpenGL context
  131. -------------------------------------------------------------------------------------------