core_input_gestures_web.c 17 KB

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