You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
5.2 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Input Gestures Detection
  4. *
  5. * Example originally created with raylib 1.4, last time updated with raylib 4.2
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2016-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define MAX_GESTURE_STRINGS 20
  15. //------------------------------------------------------------------------------------
  16. // Program main entry point
  17. //------------------------------------------------------------------------------------
  18. int main(void)
  19. {
  20. // Initialization
  21. //--------------------------------------------------------------------------------------
  22. const int screenWidth = 800;
  23. const int screenHeight = 450;
  24. InitWindow(screenWidth, screenHeight, "raylib [core] example - input gestures");
  25. Vector2 touchPosition = { 0, 0 };
  26. Rectangle touchArea = { 220, 10, screenWidth - 230.0f, screenHeight - 20.0f };
  27. int gesturesCount = 0;
  28. char gestureStrings[MAX_GESTURE_STRINGS][32];
  29. int currentGesture = GESTURE_NONE;
  30. int lastGesture = GESTURE_NONE;
  31. //SetGesturesEnabled(0b0000000000001001); // Enable only some gestures to be detected
  32. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  33. //--------------------------------------------------------------------------------------
  34. // Main game loop
  35. while (!WindowShouldClose()) // Detect window close button or ESC key
  36. {
  37. // Update
  38. //----------------------------------------------------------------------------------
  39. lastGesture = currentGesture;
  40. currentGesture = GetGestureDetected();
  41. touchPosition = GetTouchPosition(0);
  42. if (CheckCollisionPointRec(touchPosition, touchArea) && (currentGesture != GESTURE_NONE))
  43. {
  44. if (currentGesture != lastGesture)
  45. {
  46. // Store gesture string
  47. switch (currentGesture)
  48. {
  49. case GESTURE_TAP: TextCopy(gestureStrings[gesturesCount], "GESTURE TAP"); break;
  50. case GESTURE_DOUBLETAP: TextCopy(gestureStrings[gesturesCount], "GESTURE DOUBLETAP"); break;
  51. case GESTURE_HOLD: TextCopy(gestureStrings[gesturesCount], "GESTURE HOLD"); break;
  52. case GESTURE_DRAG: TextCopy(gestureStrings[gesturesCount], "GESTURE DRAG"); break;
  53. case GESTURE_SWIPE_RIGHT: TextCopy(gestureStrings[gesturesCount], "GESTURE SWIPE RIGHT"); break;
  54. case GESTURE_SWIPE_LEFT: TextCopy(gestureStrings[gesturesCount], "GESTURE SWIPE LEFT"); break;
  55. case GESTURE_SWIPE_UP: TextCopy(gestureStrings[gesturesCount], "GESTURE SWIPE UP"); break;
  56. case GESTURE_SWIPE_DOWN: TextCopy(gestureStrings[gesturesCount], "GESTURE SWIPE DOWN"); break;
  57. case GESTURE_PINCH_IN: TextCopy(gestureStrings[gesturesCount], "GESTURE PINCH IN"); break;
  58. case GESTURE_PINCH_OUT: TextCopy(gestureStrings[gesturesCount], "GESTURE PINCH OUT"); break;
  59. default: break;
  60. }
  61. gesturesCount++;
  62. // Reset gestures strings
  63. if (gesturesCount >= MAX_GESTURE_STRINGS)
  64. {
  65. for (int i = 0; i < MAX_GESTURE_STRINGS; i++) TextCopy(gestureStrings[i], "\0");
  66. gesturesCount = 0;
  67. }
  68. }
  69. }
  70. //----------------------------------------------------------------------------------
  71. // Draw
  72. //----------------------------------------------------------------------------------
  73. BeginDrawing();
  74. ClearBackground(RAYWHITE);
  75. DrawRectangleRec(touchArea, GRAY);
  76. DrawRectangle(225, 15, screenWidth - 240, screenHeight - 30, RAYWHITE);
  77. DrawText("GESTURES TEST AREA", screenWidth - 270, screenHeight - 40, 20, Fade(GRAY, 0.5f));
  78. for (int i = 0; i < gesturesCount; i++)
  79. {
  80. if (i%2 == 0) DrawRectangle(10, 30 + 20*i, 200, 20, Fade(LIGHTGRAY, 0.5f));
  81. else DrawRectangle(10, 30 + 20*i, 200, 20, Fade(LIGHTGRAY, 0.3f));
  82. if (i < gesturesCount - 1) DrawText(gestureStrings[i], 35, 36 + 20*i, 10, DARKGRAY);
  83. else DrawText(gestureStrings[i], 35, 36 + 20*i, 10, MAROON);
  84. }
  85. DrawRectangleLines(10, 29, 200, screenHeight - 50, GRAY);
  86. DrawText("DETECTED GESTURES", 50, 15, 10, GRAY);
  87. if (currentGesture != GESTURE_NONE) DrawCircleV(touchPosition, 30, MAROON);
  88. EndDrawing();
  89. //----------------------------------------------------------------------------------
  90. }
  91. // De-Initialization
  92. //--------------------------------------------------------------------------------------
  93. CloseWindow(); // Close window and OpenGL context
  94. //--------------------------------------------------------------------------------------
  95. }