core_gestures_detection.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Gestures Detection
  4. *
  5. * This example has been created using raylib 1.4 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2016 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #include <string.h>
  13. #define MAX_GESTURE_STRINGS 20
  14. int main()
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [core] example - gestures detection");
  21. Vector2 touchPosition = { 0, 0 };
  22. Rectangle touchArea = { 220, 10, screenWidth - 230, screenHeight - 20 };
  23. int gesturesCount = 0;
  24. char gestureStrings[MAX_GESTURE_STRINGS][32];
  25. int currentGesture = GESTURE_NONE;
  26. int lastGesture = GESTURE_NONE;
  27. //SetGesturesEnabled(0b0000000000001001); // Enable only some gestures to be detected
  28. SetTargetFPS(30);
  29. //--------------------------------------------------------------------------------------
  30. // Main game loop
  31. while (!WindowShouldClose()) // Detect window close button or ESC key
  32. {
  33. // Update
  34. //----------------------------------------------------------------------------------
  35. lastGesture = currentGesture;
  36. touchPosition = GetTouchPosition(0);
  37. if (CheckCollisionPointRec(touchPosition, touchArea) && IsGestureDetected())
  38. {
  39. currentGesture = GetGestureType();
  40. if (currentGesture != lastGesture)
  41. {
  42. // Store gesture string
  43. switch (currentGesture)
  44. {
  45. case GESTURE_TAP: strcpy(gestureStrings[gesturesCount], "GESTURE TAP"); break;
  46. case GESTURE_DOUBLETAP: strcpy(gestureStrings[gesturesCount], "GESTURE DOUBLETAP"); break;
  47. case GESTURE_HOLD: strcpy(gestureStrings[gesturesCount], "GESTURE HOLD"); break;
  48. case GESTURE_DRAG: strcpy(gestureStrings[gesturesCount], "GESTURE DRAG"); break;
  49. case GESTURE_SWIPE_RIGHT: strcpy(gestureStrings[gesturesCount], "GESTURE SWIPE RIGHT"); break;
  50. case GESTURE_SWIPE_LEFT: strcpy(gestureStrings[gesturesCount], "GESTURE SWIPE LEFT"); break;
  51. case GESTURE_SWIPE_UP: strcpy(gestureStrings[gesturesCount], "GESTURE SWIPE UP"); break;
  52. case GESTURE_SWIPE_DOWN: strcpy(gestureStrings[gesturesCount], "GESTURE SWIPE DOWN"); break;
  53. default: break;
  54. }
  55. gesturesCount++;
  56. // Reset gestures strings
  57. if (gesturesCount >= MAX_GESTURE_STRINGS)
  58. {
  59. for (int i = 0; i < MAX_GESTURE_STRINGS; i++) strcpy(gestureStrings[i], "\0");
  60. gesturesCount = 0;
  61. }
  62. }
  63. }
  64. else currentGesture = GESTURE_NONE;
  65. //----------------------------------------------------------------------------------
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(RAYWHITE);
  70. DrawRectangleRec(touchArea, GRAY);
  71. DrawRectangle(225, 15, screenWidth - 240, screenHeight - 30, RAYWHITE);
  72. DrawText("GESTURES TEST AREA", screenWidth - 270, screenHeight - 40, 20, Fade(GRAY, 0.5f));
  73. for (int i = 0; i < gesturesCount; i++)
  74. {
  75. if (i%2 == 0) DrawRectangle(10, 30 + 20*i, 200, 20, Fade(LIGHTGRAY, 0.5f));
  76. else DrawRectangle(10, 30 + 20*i, 200, 20, Fade(LIGHTGRAY, 0.3f));
  77. if (i < gesturesCount - 1) DrawText(gestureStrings[i], 35, 36 + 20*i, 10, DARKGRAY);
  78. else DrawText(gestureStrings[i], 35, 36 + 20*i, 10, MAROON);
  79. }
  80. DrawRectangleLines(10, 29, 200, screenHeight - 50, GRAY);
  81. DrawText("DETECTED GESTURES", 50, 15, 10, GRAY);
  82. if (currentGesture != GESTURE_NONE) DrawCircleV(touchPosition, 30, MAROON);
  83. EndDrawing();
  84. //----------------------------------------------------------------------------------
  85. }
  86. // De-Initialization
  87. //--------------------------------------------------------------------------------------
  88. CloseWindow(); // Close window and OpenGL context
  89. //--------------------------------------------------------------------------------------
  90. }