core_input_actions.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - input actions
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.5, last time updated with raylib 5.6
  8. *
  9. * Example contributed by Jett (@JettMonstersGoBoom) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2025 Jett (@JettMonstersGoBoom)
  15. *
  16. ********************************************************************************************/
  17. // Simple example for decoding input as actions, allowing remapping of input to different keys or gamepad buttons
  18. // For example instead of using `IsKeyDown(KEY_LEFT)`, you can use `IsActionDown(ACTION_LEFT)`
  19. // which can be reassigned to e.g. KEY_A and also assigned to a gamepad button. the action will trigger with either gamepad or keys
  20. #include "raylib.h"
  21. //----------------------------------------------------------------------------------
  22. // Types and Structures Definition
  23. //----------------------------------------------------------------------------------
  24. typedef enum ActionType {
  25. NO_ACTION = 0,
  26. ACTION_UP,
  27. ACTION_DOWN,
  28. ACTION_LEFT,
  29. ACTION_RIGHT,
  30. ACTION_FIRE,
  31. MAX_ACTION
  32. } ActionType;
  33. // Key and button inputs
  34. typedef struct ActionInput {
  35. int key;
  36. int button;
  37. } ActionInput;
  38. //----------------------------------------------------------------------------------
  39. // Global Variables Definition
  40. //----------------------------------------------------------------------------------
  41. static int gamepadIndex = 0; // Gamepad default index
  42. static ActionInput actionInputs[MAX_ACTION] = { 0 };
  43. //----------------------------------------------------------------------------------
  44. // Module Functions Declaration
  45. //----------------------------------------------------------------------------------
  46. static bool IsActionPressed(int action); // Check action key/button pressed
  47. static bool IsActionReleased(int action); // Check action key/button released
  48. static bool IsActionDown(int action); // Check action key/button down
  49. static void SetActionsDefault(void); // Set the "default" keyset
  50. static void SetActionsCursor(void); // Set the "alternate" keyset
  51. //------------------------------------------------------------------------------------
  52. // Program main entry point
  53. //------------------------------------------------------------------------------------
  54. int main(void)
  55. {
  56. // Initialization
  57. //--------------------------------------------------------------------------------------
  58. const int screenWidth = 800;
  59. const int screenHeight = 450;
  60. InitWindow(screenWidth, screenHeight, "raylib [core] example - input actions");
  61. // Set default actions
  62. char actionSet = 0;
  63. SetActionsDefault();
  64. bool releaseAction = false;
  65. Vector2 position = (Vector2){ 400.0f, 200.0f };
  66. Vector2 size = (Vector2){ 40.0f, 40.0f };
  67. SetTargetFPS(60);
  68. //--------------------------------------------------------------------------------------
  69. // Main game loop
  70. while (!WindowShouldClose()) // Detect window close button or ESC key
  71. {
  72. // Update
  73. //----------------------------------------------------------------------------------
  74. gamepadIndex = 0; // Set gamepad being checked
  75. if (IsActionDown(ACTION_UP)) position.y -= 2;
  76. if (IsActionDown(ACTION_DOWN)) position.y += 2;
  77. if (IsActionDown(ACTION_LEFT)) position.x -= 2;
  78. if (IsActionDown(ACTION_RIGHT)) position.x += 2;
  79. if (IsActionPressed(ACTION_FIRE))
  80. {
  81. position.x = (screenWidth-size.x)/2;
  82. position.y = (screenHeight-size.y)/2;
  83. }
  84. // Register release action for one frame
  85. releaseAction = false;
  86. if (IsActionReleased(ACTION_FIRE)) releaseAction = true;
  87. // Switch control scheme by pressing TAB
  88. if (IsKeyPressed(KEY_TAB))
  89. {
  90. actionSet = !actionSet;
  91. if (actionSet == 0) SetActionsDefault();
  92. else SetActionsCursor();
  93. }
  94. //----------------------------------------------------------------------------------
  95. // Draw
  96. //----------------------------------------------------------------------------------
  97. BeginDrawing();
  98. ClearBackground(GRAY);
  99. DrawRectangleV(position, size, releaseAction? BLUE : RED);
  100. DrawText((actionSet == 0)? "Current input set: WASD (default)" : "Current input set: Cursor", 10, 10, 20, WHITE);
  101. DrawText("Use TAB key to toggles Actions keyset", 10, 50, 20, GREEN);
  102. EndDrawing();
  103. //----------------------------------------------------------------------------------
  104. }
  105. // De-Initialization
  106. //--------------------------------------------------------------------------------------
  107. CloseWindow(); // Close window and OpenGL context
  108. //--------------------------------------------------------------------------------------
  109. return 0;
  110. }
  111. //----------------------------------------------------------------------------------
  112. // Module Functions Definition
  113. //----------------------------------------------------------------------------------
  114. // Check action key/button pressed
  115. // NOTE: Combines key pressed and gamepad button pressed in one action
  116. static bool IsActionPressed(int action)
  117. {
  118. bool result = false;
  119. if (action < MAX_ACTION) result = (IsKeyPressed(actionInputs[action].key) || IsGamepadButtonPressed(gamepadIndex, actionInputs[action].button));
  120. return result;
  121. }
  122. // Check action key/button released
  123. // NOTE: Combines key released and gamepad button released in one action
  124. static bool IsActionReleased(int action)
  125. {
  126. bool result = false;
  127. if (action < MAX_ACTION) result = (IsKeyReleased(actionInputs[action].key) || IsGamepadButtonReleased(gamepadIndex, actionInputs[action].button));
  128. return result;
  129. }
  130. // Check action key/button down
  131. // NOTE: Combines key down and gamepad button down in one action
  132. static bool IsActionDown(int action)
  133. {
  134. bool result = false;
  135. if (action < MAX_ACTION) result = (IsKeyDown(actionInputs[action].key) || IsGamepadButtonDown(gamepadIndex, actionInputs[action].button));
  136. return result;
  137. }
  138. // Set the "default" keyset
  139. // NOTE: Here WASD and gamepad buttons on the left side for movement
  140. static void SetActionsDefault(void)
  141. {
  142. actionInputs[ACTION_UP].key = KEY_W;
  143. actionInputs[ACTION_DOWN].key = KEY_S;
  144. actionInputs[ACTION_LEFT].key = KEY_A;
  145. actionInputs[ACTION_RIGHT].key = KEY_D;
  146. actionInputs[ACTION_FIRE].key = KEY_SPACE;
  147. actionInputs[ACTION_UP].button = GAMEPAD_BUTTON_LEFT_FACE_UP;
  148. actionInputs[ACTION_DOWN].button = GAMEPAD_BUTTON_LEFT_FACE_DOWN;
  149. actionInputs[ACTION_LEFT].button = GAMEPAD_BUTTON_LEFT_FACE_LEFT;
  150. actionInputs[ACTION_RIGHT].button = GAMEPAD_BUTTON_LEFT_FACE_RIGHT;
  151. actionInputs[ACTION_FIRE].button = GAMEPAD_BUTTON_RIGHT_FACE_DOWN;
  152. }
  153. // Set the "alternate" keyset
  154. // NOTE: Here cursor keys and gamepad buttons on the right side for movement
  155. static void SetActionsCursor(void)
  156. {
  157. actionInputs[ACTION_UP].key = KEY_UP;
  158. actionInputs[ACTION_DOWN].key = KEY_DOWN;
  159. actionInputs[ACTION_LEFT].key = KEY_LEFT;
  160. actionInputs[ACTION_RIGHT].key = KEY_RIGHT;
  161. actionInputs[ACTION_FIRE].key = KEY_SPACE;
  162. actionInputs[ACTION_UP].button = GAMEPAD_BUTTON_RIGHT_FACE_UP;
  163. actionInputs[ACTION_DOWN].button = GAMEPAD_BUTTON_RIGHT_FACE_DOWN;
  164. actionInputs[ACTION_LEFT].button = GAMEPAD_BUTTON_RIGHT_FACE_LEFT;
  165. actionInputs[ACTION_RIGHT].button = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT;
  166. actionInputs[ACTION_FIRE].button = GAMEPAD_BUTTON_LEFT_FACE_DOWN;
  167. }