core_input_gestures_testbed.c 17 KB

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