core_input_gestures_web.c 17 KB

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