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.

114 lines
4.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Input 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(void)
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. const int screenWidth = 800;
  19. const int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [core] example - input gestures");
  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(60); // Set our game to run at 60 frames-per-second
  29. //--------------------------------------------------------------------------------------
  30. // Main game loop
  31. while (!WindowShouldClose()) // Detect window close button or ESC key
  32. {
  33. // Update
  34. //----------------------------------------------------------------------------------
  35. lastGesture = currentGesture;
  36. currentGesture = GetGestureDetected();
  37. touchPosition = GetTouchPosition(0);
  38. if (CheckCollisionPointRec(touchPosition, touchArea) && (currentGesture != GESTURE_NONE))
  39. {
  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. case GESTURE_PINCH_IN: strcpy(gestureStrings[gesturesCount], "GESTURE PINCH IN"); break;
  54. case GESTURE_PINCH_OUT: strcpy(gestureStrings[gesturesCount], "GESTURE PINCH OUT"); break;
  55. default: break;
  56. }
  57. gesturesCount++;
  58. // Reset gestures strings
  59. if (gesturesCount >= MAX_GESTURE_STRINGS)
  60. {
  61. for (int i = 0; i < MAX_GESTURE_STRINGS; i++) strcpy(gestureStrings[i], "\0");
  62. gesturesCount = 0;
  63. }
  64. }
  65. }
  66. //----------------------------------------------------------------------------------
  67. // Draw
  68. //----------------------------------------------------------------------------------
  69. BeginDrawing();
  70. ClearBackground(RAYWHITE);
  71. DrawRectangleRec(touchArea, GRAY);
  72. DrawRectangle(225, 15, screenWidth - 240, screenHeight - 30, RAYWHITE);
  73. DrawText("GESTURES TEST AREA", screenWidth - 270, screenHeight - 40, 20, Fade(GRAY, 0.5f));
  74. for (int i = 0; i < gesturesCount; i++)
  75. {
  76. if (i%2 == 0) DrawRectangle(10, 30 + 20*i, 200, 20, Fade(LIGHTGRAY, 0.5f));
  77. else DrawRectangle(10, 30 + 20*i, 200, 20, Fade(LIGHTGRAY, 0.3f));
  78. if (i < gesturesCount - 1) DrawText(gestureStrings[i], 35, 36 + 20*i, 10, DARKGRAY);
  79. else DrawText(gestureStrings[i], 35, 36 + 20*i, 10, MAROON);
  80. }
  81. DrawRectangleLines(10, 29, 200, screenHeight - 50, GRAY);
  82. DrawText("DETECTED GESTURES", 50, 15, 10, GRAY);
  83. if (currentGesture != GESTURE_NONE) DrawCircleV(touchPosition, 30, MAROON);
  84. EndDrawing();
  85. //----------------------------------------------------------------------------------
  86. }
  87. // De-Initialization
  88. //--------------------------------------------------------------------------------------
  89. CloseWindow(); // Close window and OpenGL context
  90. //--------------------------------------------------------------------------------------
  91. }