core_input_gestures_testbed.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - input gestures testbed
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * Example originally created with raylib 5.0, last time updated with raylib 5.6-dev
  8. *
  9. * Example contributed by ubkp (@ubkp) 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) 2023-2025 ubkp (@ubkp)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include <math.h> // Required for the protractor angle graphic drawing
  19. #define GESTURE_LOG_SIZE 20
  20. #define MAX_TOUCH_COUNT 32
  21. //----------------------------------------------------------------------------------
  22. // Module Functions Declaration
  23. //----------------------------------------------------------------------------------
  24. static char const *GetGestureName(int gesture); // Get text string for gesture value
  25. static Color GetGestureColor(int gesture); // Get color for gesture value
  26. //------------------------------------------------------------------------------------
  27. // Program main entry point
  28. //------------------------------------------------------------------------------------
  29. int main(void)
  30. {
  31. // Initialization
  32. //--------------------------------------------------------------------------------------
  33. const int screenWidth = 800;
  34. const int screenHeight = 450;
  35. InitWindow(screenWidth, screenHeight, "raylib [core] example - input gestures testbed");
  36. Vector2 messagePosition = { 160, 7 };
  37. // Last gesture variables definitions
  38. int lastGesture = 0;
  39. Vector2 lastGesturePosition = { 165, 130 };
  40. // Gesture log variables definitions
  41. // NOTE: The gesture log uses an array (as an inverted circular queue) to store the performed gestures
  42. char gestureLog[GESTURE_LOG_SIZE][12] = { "" };
  43. // NOTE: The index for the inverted circular queue (moving from last to first direction, then looping around)
  44. int gestureLogIndex = GESTURE_LOG_SIZE;
  45. int previousGesture = 0;
  46. // Log mode values:
  47. // - 0 shows repeated events
  48. // - 1 hides repeated events
  49. // - 2 shows repeated events but hide hold events
  50. // - 3 hides repeated events and hide hold events
  51. int logMode = 1;
  52. Color gestureColor = { 0, 0, 0, 255 };
  53. Rectangle logButton1 = { 53, 7, 48, 26 };
  54. Rectangle logButton2 = { 108, 7, 36, 26 };
  55. Vector2 gestureLogPosition = { 10, 10 };
  56. // Protractor variables definitions
  57. float angleLength = 90.0f;
  58. float currentAngleDegrees = 0.0f;
  59. Vector2 finalVector = { 0.0f, 0.0f };
  60. char currentAngleStr[7] = "";
  61. Vector2 protractorPosition = { 266.0f, 315.0f };
  62. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  63. //--------------------------------------------------------------------------------------
  64. // Main game loop
  65. while (!WindowShouldClose()) // Detect window close button or ESC key
  66. {
  67. // Update
  68. //--------------------------------------------------------------------------------------
  69. // Handle common gestures data
  70. int i, ii; // Iterators that will be reused by all for loops
  71. const int currentGesture = GetGestureDetected();
  72. const float currentDragDegrees = GetGestureDragAngle();
  73. const float currentPitchDegrees = GetGesturePinchAngle();
  74. const int touchCount = GetTouchPointCount();
  75. // Handle last gesture
  76. if ((currentGesture != 0) && (currentGesture != 4) && (currentGesture != previousGesture))
  77. lastGesture = currentGesture; // Filter the meaningful gestures (1, 2, 8 to 512) for the display
  78. // Handle gesture log
  79. if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
  80. {
  81. if (CheckCollisionPointRec(GetMousePosition(), logButton1))
  82. {
  83. switch (logMode)
  84. {
  85. case 3: logMode = 2; break;
  86. case 2: logMode = 3; break;
  87. case 1: logMode = 0; break;
  88. default: logMode = 1; break;
  89. }
  90. }
  91. else if (CheckCollisionPointRec(GetMousePosition(), logButton2))
  92. {
  93. switch (logMode)
  94. {
  95. case 3: logMode = 1; break;
  96. case 2: logMode = 0; break;
  97. case 1: logMode = 3; break;
  98. default: logMode = 2; break;
  99. }
  100. }
  101. }
  102. int fillLog = 0; // Gate variable to be used to allow or not the gesture log to be filled
  103. if (currentGesture !=0)
  104. {
  105. if (logMode == 3) // 3 hides repeated events and hide hold events
  106. {
  107. if (((currentGesture != 4) && (currentGesture != previousGesture)) || (currentGesture < 3)) fillLog = 1;
  108. }
  109. else if (logMode == 2) // 2 shows repeated events but hide hold events
  110. {
  111. if (currentGesture != 4) fillLog = 1;
  112. }
  113. else if (logMode == 1) // 1 hides repeated events
  114. {
  115. if (currentGesture != previousGesture) fillLog = 1;
  116. }
  117. else // 0 shows repeated events
  118. {
  119. fillLog = 1;
  120. }
  121. }
  122. if (fillLog) // If one of the conditions from logMode was met, fill the gesture log
  123. {
  124. previousGesture = currentGesture;
  125. gestureColor = GetGestureColor(currentGesture);
  126. if (gestureLogIndex <= 0) gestureLogIndex = GESTURE_LOG_SIZE;
  127. gestureLogIndex--;
  128. // Copy the gesture respective name to the gesture log array
  129. TextCopy(gestureLog[gestureLogIndex], GetGestureName(currentGesture));
  130. }
  131. // Handle protractor
  132. if (currentGesture > 255) currentAngleDegrees = currentPitchDegrees; // Pinch In and Pinch Out
  133. else if (currentGesture > 15) currentAngleDegrees = currentDragDegrees; // Swipe Right, Swipe Left, Swipe Up and Swipe Down
  134. else if (currentGesture > 0) currentAngleDegrees = 0.0f; // Tap, Doubletap, Hold and Grab
  135. float currentAngleRadians = ((currentAngleDegrees + 90.0f)*PI/180); // Convert the current angle to Radians
  136. // Calculate the final vector for display
  137. finalVector = (Vector2){ (angleLength*sinf(currentAngleRadians)) + protractorPosition.x,
  138. (angleLength*cosf(currentAngleRadians)) + protractorPosition.y };
  139. // Handle touch and mouse pointer points
  140. Vector2 touchPosition[MAX_TOUCH_COUNT] = { 0 };
  141. Vector2 mousePosition = { 0 };
  142. if (currentGesture != GESTURE_NONE)
  143. {
  144. if (touchCount != 0)
  145. {
  146. for (i = 0; i < touchCount; i++) touchPosition[i] = GetTouchPosition(i); // Fill the touch positions
  147. }
  148. else mousePosition = GetMousePosition();
  149. }
  150. //--------------------------------------------------------------------------------------
  151. // Draw
  152. //--------------------------------------------------------------------------------------
  153. BeginDrawing();
  154. ClearBackground(RAYWHITE);
  155. // Draw common elements
  156. DrawText("*", messagePosition.x + 5, messagePosition.y + 5, 10, BLACK);
  157. DrawText("Example optimized for Web/HTML5\non Smartphones with Touch Screen.", messagePosition.x + 15, messagePosition.y + 5, 10, BLACK);
  158. DrawText("*", messagePosition.x + 5, messagePosition.y + 35, 10, BLACK);
  159. DrawText("While running on Desktop Web Browsers,\ninspect and turn on Touch Emulation.", messagePosition.x + 15, messagePosition.y + 35, 10, BLACK);
  160. // Draw last gesture
  161. DrawText("Last gesture", lastGesturePosition.x + 33, lastGesturePosition.y - 47, 20, BLACK);
  162. DrawText("Swipe Tap Pinch Touch", lastGesturePosition.x + 17, lastGesturePosition.y - 18, 10, BLACK);
  163. DrawRectangle(lastGesturePosition.x + 20, lastGesturePosition.y, 20, 20, lastGesture == GESTURE_SWIPE_UP ? RED : LIGHTGRAY);
  164. DrawRectangle(lastGesturePosition.x, lastGesturePosition.y + 20, 20, 20, lastGesture == GESTURE_SWIPE_LEFT ? RED : LIGHTGRAY);
  165. DrawRectangle(lastGesturePosition.x + 40, lastGesturePosition.y + 20, 20, 20, lastGesture == GESTURE_SWIPE_RIGHT ? RED : LIGHTGRAY);
  166. DrawRectangle(lastGesturePosition.x + 20, lastGesturePosition.y + 40, 20, 20, lastGesture == GESTURE_SWIPE_DOWN ? RED : LIGHTGRAY);
  167. DrawCircle(lastGesturePosition.x + 80, lastGesturePosition.y + 16, 10, lastGesture == GESTURE_TAP ? BLUE : LIGHTGRAY);
  168. DrawRing( (Vector2){lastGesturePosition.x + 103, lastGesturePosition.y + 16}, 6.0f, 11.0f, 0.0f, 360.0f, 0, lastGesture == GESTURE_DRAG ? LIME : LIGHTGRAY);
  169. DrawCircle(lastGesturePosition.x + 80, lastGesturePosition.y + 43, 10, lastGesture == GESTURE_DOUBLETAP ? SKYBLUE : LIGHTGRAY);
  170. DrawCircle(lastGesturePosition.x + 103, lastGesturePosition.y + 43, 10, lastGesture == GESTURE_DOUBLETAP ? SKYBLUE : LIGHTGRAY);
  171. DrawTriangle((Vector2){ lastGesturePosition.x + 122, lastGesturePosition.y + 16 }, (Vector2){ lastGesturePosition.x + 137, lastGesturePosition.y + 26 }, (Vector2){ lastGesturePosition.x + 137, lastGesturePosition.y + 6 }, lastGesture == GESTURE_PINCH_OUT? ORANGE : LIGHTGRAY);
  172. DrawTriangle((Vector2){ lastGesturePosition.x + 147, lastGesturePosition.y + 6 }, (Vector2){ lastGesturePosition.x + 147, lastGesturePosition.y + 26 }, (Vector2){ lastGesturePosition.x + 162, lastGesturePosition.y + 16 }, lastGesture == GESTURE_PINCH_OUT? ORANGE : LIGHTGRAY);
  173. DrawTriangle((Vector2){ lastGesturePosition.x + 125, lastGesturePosition.y + 33 }, (Vector2){ lastGesturePosition.x + 125, lastGesturePosition.y + 53 }, (Vector2){ lastGesturePosition.x + 140, lastGesturePosition.y + 43 }, lastGesture == GESTURE_PINCH_IN? VIOLET : LIGHTGRAY);
  174. DrawTriangle((Vector2){ lastGesturePosition.x + 144, lastGesturePosition.y + 43 }, (Vector2){ lastGesturePosition.x + 159, lastGesturePosition.y + 53 }, (Vector2){ lastGesturePosition.x + 159, lastGesturePosition.y + 33 }, lastGesture == GESTURE_PINCH_IN? VIOLET : LIGHTGRAY);
  175. for (i = 0; i < 4; i++) DrawCircle(lastGesturePosition.x + 180, lastGesturePosition.y + 7 + i*15, 5, touchCount <= i? LIGHTGRAY : gestureColor);
  176. // Draw gesture log
  177. DrawText("Log", gestureLogPosition.x, gestureLogPosition.y, 20, BLACK);
  178. // Loop in both directions to print the gesture log array in the inverted order (and looping around if the index started somewhere in the middle)
  179. for (i = 0, ii = gestureLogIndex; i < GESTURE_LOG_SIZE; i++, ii = (ii + 1) % GESTURE_LOG_SIZE) DrawText(gestureLog[ii], gestureLogPosition.x, gestureLogPosition.y + 410 - i*20, 20, (i == 0 ? gestureColor : LIGHTGRAY));
  180. Color logButton1Color, logButton2Color;
  181. switch (logMode)
  182. {
  183. case 3: logButton1Color=MAROON; logButton2Color=MAROON; break;
  184. case 2: logButton1Color=GRAY; logButton2Color=MAROON; break;
  185. case 1: logButton1Color=MAROON; logButton2Color=GRAY; break;
  186. default: logButton1Color=GRAY; logButton2Color=GRAY; break;
  187. }
  188. DrawRectangleRec(logButton1, logButton1Color);
  189. DrawText("Hide", logButton1.x + 7, logButton1.y + 3, 10, WHITE);
  190. DrawText("Repeat", logButton1.x + 7, logButton1.y + 13, 10, WHITE);
  191. DrawRectangleRec(logButton2, logButton2Color);
  192. DrawText("Hide", logButton1.x + 62, logButton1.y + 3, 10, WHITE);
  193. DrawText("Hold", logButton1.x + 62, logButton1.y + 13, 10, WHITE);
  194. // Draw protractor
  195. DrawText("Angle", protractorPosition.x + 55, protractorPosition.y + 76, 10, BLACK);
  196. const char *angleString = TextFormat("%f", currentAngleDegrees);
  197. const int angleStringDot = TextFindIndex(angleString, ".");
  198. const char *angleStringTrim = TextSubtext(angleString, 0, angleStringDot + 3);
  199. DrawText( angleStringTrim, protractorPosition.x + 55, protractorPosition.y + 92, 20, gestureColor);
  200. DrawCircle(protractorPosition.x, protractorPosition.y, 80.0f, WHITE);
  201. DrawLineEx((Vector2){ protractorPosition.x - 90, protractorPosition.y }, (Vector2){ protractorPosition.x + 90, protractorPosition.y }, 3.0f, LIGHTGRAY);
  202. DrawLineEx((Vector2){ protractorPosition.x, protractorPosition.y - 90 }, (Vector2){ protractorPosition.x, protractorPosition.y + 90 }, 3.0f, LIGHTGRAY);
  203. DrawLineEx((Vector2){ protractorPosition.x - 80, protractorPosition.y - 45 }, (Vector2){ protractorPosition.x + 80, protractorPosition.y + 45 }, 3.0f, GREEN);
  204. DrawLineEx((Vector2){ protractorPosition.x - 80, protractorPosition.y + 45 }, (Vector2){ protractorPosition.x + 80, protractorPosition.y - 45 }, 3.0f, GREEN);
  205. DrawText("0", protractorPosition.x + 96, protractorPosition.y - 9, 20, BLACK);
  206. DrawText("30", protractorPosition.x + 74, protractorPosition.y - 68, 20, BLACK);
  207. DrawText("90", protractorPosition.x - 11, protractorPosition.y - 110, 20, BLACK);
  208. DrawText("150", protractorPosition.x - 100, protractorPosition.y - 68, 20, BLACK);
  209. DrawText("180", protractorPosition.x - 124, protractorPosition.y - 9, 20, BLACK);
  210. DrawText("210", protractorPosition.x - 100, protractorPosition.y + 50, 20, BLACK);
  211. DrawText("270", protractorPosition.x - 18, protractorPosition.y + 92, 20, BLACK);
  212. DrawText("330", protractorPosition.x + 72, protractorPosition.y + 50, 20, BLACK);
  213. if (currentAngleDegrees != 0.0f) DrawLineEx(protractorPosition, finalVector, 3.0f, gestureColor);
  214. // Draw touch and mouse pointer points
  215. if (currentGesture != GESTURE_NONE)
  216. {
  217. if ( touchCount != 0 )
  218. {
  219. for (i = 0; i < touchCount; i++)
  220. {
  221. DrawCircleV(touchPosition[i], 50.0f, Fade(gestureColor, 0.5f));
  222. DrawCircleV(touchPosition[i], 5.0f, gestureColor);
  223. }
  224. if (touchCount == 2) DrawLineEx(touchPosition[0], touchPosition[1], ((currentGesture == 512)? 8 : 12), gestureColor);
  225. }
  226. else
  227. {
  228. DrawCircleV(mousePosition, 35.0f, Fade(gestureColor, 0.5f));
  229. DrawCircleV(mousePosition, 5.0f, gestureColor);
  230. }
  231. }
  232. EndDrawing();
  233. //--------------------------------------------------------------------------------------
  234. }
  235. // De-Initialization
  236. //--------------------------------------------------------------------------------------
  237. CloseWindow(); // Close window and OpenGL context
  238. //--------------------------------------------------------------------------------------
  239. return 0;
  240. }
  241. //----------------------------------------------------------------------------------
  242. // Module Functions Definition
  243. //----------------------------------------------------------------------------------
  244. // Get text string for gesture value
  245. static char const *GetGestureName(int gesture)
  246. {
  247. switch (gesture)
  248. {
  249. case 0: return "None"; break;
  250. case 1: return "Tap"; break;
  251. case 2: return "Double Tap"; break;
  252. case 4: return "Hold"; break;
  253. case 8: return "Drag"; break;
  254. case 16: return "Swipe Right"; break;
  255. case 32: return "Swipe Left"; break;
  256. case 64: return "Swipe Up"; break;
  257. case 128: return "Swipe Down"; break;
  258. case 256: return "Pinch In"; break;
  259. case 512: return "Pinch Out"; break;
  260. default: return "Unknown"; break;
  261. }
  262. }
  263. // Get color for gesture value
  264. static Color GetGestureColor(int gesture)
  265. {
  266. switch (gesture)
  267. {
  268. case 0: return BLACK; break;
  269. case 1: return BLUE; break;
  270. case 2: return SKYBLUE; break;
  271. case 4: return BLACK; break;
  272. case 8: return LIME; break;
  273. case 16: return RED; break;
  274. case 32: return RED; break;
  275. case 64: return RED; break;
  276. case 128: return RED; break;
  277. case 256: return VIOLET; break;
  278. case 512: return ORANGE; break;
  279. default: return BLACK; break;
  280. }
  281. }