1717
1818#include "raylib.h"
1919
20- #include " math.h" // Required for the protractor angle graphic drawing
20+ #include < math.h> // Required for the protractor angle graphic drawing
2121
2222#if defined(PLATFORM_WEB )
2323 #include <emscripten/emscripten.h> // Required for the Web/HTML5
2424#endif
2525
26- //--------------------------------------------------------------------------------------
27- // Global definitions and declarations
28- //--------------------------------------------------------------------------------------
26+ #define GESTURE_LOG_SIZE 20
27+ #define MAX_TOUCH_COUNT 32
2928
30- // Common variables definitions
3129//--------------------------------------------------------------------------------------
32- int screenWidth = 800 ; // Update depending on web canvas
33- const int screenHeight = 450 ;
34- Vector2 messagePosition = { 160 , 7 };
30+ // Global Variables Definition
31+ //--------------------------------------------------------------------------------------
32+ static int screenWidth = 800 ; // Update depending on web canvas
33+ static const int screenHeight = 450 ;
34+ static Vector2 messagePosition = { 160 , 7 };
3535
3636// Last gesture variables definitions
37- //--------------------------------------------------------------------------------------
38- int lastGesture = 0 ;
39- Vector2 lastGesturePosition = { 165 , 130 };
37+ static int lastGesture = 0 ;
38+ static Vector2 lastGesturePosition = { 165 , 130 };
39+
40+ // Gesture log variables definitions
41+ // NOTE: The gesture log uses an array (as an inverted circular queue) to store the performed gestures
42+ static 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+ static int gestureLogIndex = GESTURE_LOG_SIZE ;
45+ static int previousGesture = 0 ;
46+
47+ // Log mode values:
48+ // - 0 shows repeated events
49+ // - 1 hides repeated events
50+ // - 2 shows repeated events but hide hold events
51+ // - 3 hides repeated events and hide hold events
52+ static int logMode = 1 ;
53+
54+ static Color gestureColor = { 0 , 0 , 0 , 255 };
55+ static Rectangle logButton1 = { 53 , 7 , 48 , 26 };
56+ static Rectangle logButton2 = { 108 , 7 , 36 , 26 };
57+ static Vector2 gestureLogPosition = { 10 , 10 };
4058
41- // Gesture log variables definitions and functions declarations
42- //--------------------------------------------------------------------------------------
43- #define GESTURE_LOG_SIZE 20
44- char gestureLog [GESTURE_LOG_SIZE ][12 ] = { "" }; // The gesture log uses an array (as an inverted circular queue) to store the performed gestures
45- int gestureLogIndex = GESTURE_LOG_SIZE ; // The index for the inverted circular queue (moving from last to first direction, then looping around)
46- int previousGesture = 0 ;
59+ // Protractor variables definitions
60+ static float angleLength = 90.0f ;
61+ static float currentAngleDegrees = 0.0f ;
62+ static Vector2 finalVector = { 0.0f , 0.0f };
63+ static char currentAngleStr [7 ] = "" ;
64+ static Vector2 protractorPosition = { 266.0f , 315.0f };
65+
66+ //----------------------------------------------------------------------------------
67+ // Module Functions Declaration
68+ //----------------------------------------------------------------------------------
69+ static void UpdateDrawFrame (void ); // Update and Draw one frame
70+ static char const * GetGestureName (int i );
71+ static Color GetGestureColor (int i );
4772
48- char const * GetGestureName (int i )
73+ //------------------------------------------------------------------------------------
74+ // Program main entry point
75+ //------------------------------------------------------------------------------------
76+ int main (void )
4977{
50- switch (i ) {
51- case 0 : return "None" ; break ;
52- case 1 : return "Tap" ; break ;
53- case 2 : return "Double Tap" ; break ;
54- case 4 : return "Hold" ; break ;
55- case 8 : return "Drag" ; break ;
56- case 16 : return "Swipe Right" ; break ;
57- case 32 : return "Swipe Left" ; break ;
58- case 64 : return "Swipe Up" ; break ;
59- case 128 : return "Swipe Down" ; break ;
60- case 256 : return "Pinch In" ; break ;
61- case 512 : return "Pinch Out" ; break ;
62- default : return "Unknown" ; break ;
63- }
64- }
78+ // Initialization
79+ //--------------------------------------------------------------------------------------
80+ InitWindow (screenWidth , screenHeight , "raylib [core] example - input gestures testbed" );
6581
66- Color GetGestureColor (int i )
67- {
68- switch (i ) {
69- case 0 : return BLACK ; break ;
70- case 1 : return BLUE ; break ;
71- case 2 : return SKYBLUE ; break ;
72- case 4 : return BLACK ; break ;
73- case 8 : return LIME ; break ;
74- case 16 : return RED ; break ;
75- case 32 : return RED ; break ;
76- case 64 : return RED ; break ;
77- case 128 : return RED ; break ;
78- case 256 : return VIOLET ; break ;
79- case 512 : return ORANGE ; break ;
80- default : return BLACK ; break ;
81- }
82- }
82+ #if defined(PLATFORM_WEB )
83+ emscripten_set_main_loop (UpdateDrawFrame , 0 , 1 );
84+ #else
85+ SetTargetFPS (60 ); // Set our game to run at 60 frames-per-second
86+ //--------------------------------------------------------------------------------------
8387
84- 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
88+ // Main game loop
89+ while (!WindowShouldClose ()) // Detect window close button or ESC key
90+ {
91+ UpdateDrawFrame ();
92+ }
93+ #endif
8594
86- Color gestureColor = { 0 , 0 , 0 , 255 };
87- Rectangle logButton1 = { 53 , 7 , 48 , 26 };
88- Rectangle logButton2 = { 108 , 7 , 36 , 26 };
89- Vector2 gestureLogPosition = { 10 , 10 };
95+ // De-Initialization
96+ //--------------------------------------------------------------------------------------
97+ CloseWindow (); // Close window and OpenGL context
98+ //--------------------------------------------------------------------------------------
9099
91- // Protractor variables definitions
92- //--------------------------------------------------------------------------------------
93- float angleLength = 90.0f ;
94- float currentAngleDegrees = 0.0f ;
95- Vector2 finalVector = { 0.0f , 0.0f };
96- char currentAngleStr [7 ] = "" ;
97- Vector2 protractorPosition = { 266.0f , 315.0f };
100+ return 0 ;
101+ }
98102
99- // Update
100- //--------------------------------------------------------------------------------------
101- void Update (void )
103+ //----------------------------------------------------------------------------------
104+ // Module Functions Definition
105+ //----------------------------------------------------------------------------------
106+ static void UpdateDrawFrame (void )
102107{
103- // Handle common
108+ // Update
104109 //--------------------------------------------------------------------------------------
110+ // Handle common gestures data
105111 int i , ii ; // Iterators that will be reused by all for loops
106112 const int currentGesture = GetGestureDetected ();
107113 const float currentDragDegrees = GetGestureDragAngle ();
108114 const float currentPitchDegrees = GetGesturePinchAngle ();
109115 const int touchCount = GetTouchPointCount ();
110116
111117 // Handle last gesture
112- //--------------------------------------------------------------------------------------
113- if (( currentGesture != 0 ) && ( currentGesture != 4 ) && ( currentGesture != previousGesture )) lastGesture = currentGesture ; // Filter the meaningful gestures (1, 2, 8 to 512) for the display
118+ if (( currentGesture != 0 ) && ( currentGesture != 4 ) && ( currentGesture != previousGesture ))
119+ lastGesture = currentGesture ; // Filter the meaningful gestures (1, 2, 8 to 512) for the display
114120
115121 // Handle gesture log
116- //--------------------------------------------------------------------------------------
117122 if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT ))
118123 {
119124 if (CheckCollisionPointRec (GetMousePosition (), logButton1 ))
@@ -171,7 +176,6 @@ void Update(void)
171176 }
172177
173178 // Handle protractor
174- //--------------------------------------------------------------------------------------
175179 if (currentGesture > 255 ) // aka Pinch In and Pinch Out
176180 {
177181 currentAngleDegrees = currentPitchDegrees ;
@@ -189,9 +193,6 @@ void Update(void)
189193 finalVector = (Vector2 ){ (angleLength * sinf (currentAngleRadians )) + protractorPosition .x , (angleLength * cosf (currentAngleRadians )) + protractorPosition .y }; // Calculate the final vector for display
190194
191195 // Handle touch and mouse pointer points
192- //--------------------------------------------------------------------------------------
193- #define MAX_TOUCH_COUNT 32
194-
195196 Vector2 touchPosition [MAX_TOUCH_COUNT ] = { 0 };
196197 Vector2 mousePosition = {0 , 0 };
197198 if (currentGesture != GESTURE_NONE )
@@ -202,22 +203,20 @@ void Update(void)
202203 }
203204 else mousePosition = GetMousePosition ();
204205 }
206+ //--------------------------------------------------------------------------------------
205207
206208 // Draw
207209 //--------------------------------------------------------------------------------------
208210 BeginDrawing ();
209-
210211 ClearBackground (RAYWHITE );
211212
212- // Draw common
213- //--------------------------------------------------------------------------------------
213+ // Draw common elements
214214 DrawText ("*" , messagePosition .x + 5 , messagePosition .y + 5 , 10 , BLACK );
215215 DrawText ("Example optimized for Web/HTML5\non Smartphones with Touch Screen." , messagePosition .x + 15 , messagePosition .y + 5 , 10 , BLACK );
216216 DrawText ("*" , messagePosition .x + 5 , messagePosition .y + 35 , 10 , BLACK );
217217 DrawText ("While running on Desktop Web Browsers,\ninspect and turn on Touch Emulation." , messagePosition .x + 15 , messagePosition .y + 35 , 10 , BLACK );
218218
219219 // Draw last gesture
220- //--------------------------------------------------------------------------------------
221220 DrawText ("Last gesture" , lastGesturePosition .x + 33 , lastGesturePosition .y - 47 , 20 , BLACK );
222221 DrawText ("Swipe Tap Pinch Touch" , lastGesturePosition .x + 17 , lastGesturePosition .y - 18 , 10 , BLACK );
223222 DrawRectangle (lastGesturePosition .x + 20 , lastGesturePosition .y , 20 , 20 , lastGesture == GESTURE_SWIPE_UP ? RED : LIGHTGRAY );
@@ -235,7 +234,6 @@ void Update(void)
235234 for (i = 0 ; i < 4 ; i ++ ) DrawCircle (lastGesturePosition .x + 180 , lastGesturePosition .y + 7 + i * 15 , 5 , touchCount <= i ? LIGHTGRAY : gestureColor );
236235
237236 // Draw gesture log
238- //--------------------------------------------------------------------------------------
239237 DrawText ("Log" , gestureLogPosition .x , gestureLogPosition .y , 20 , BLACK );
240238
241239 // 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)
@@ -256,7 +254,6 @@ void Update(void)
256254 DrawText ("Hold" , logButton1 .x + 62 , logButton1 .y + 13 , 10 , WHITE );
257255
258256 // Draw protractor
259- //--------------------------------------------------------------------------------------
260257 DrawText ("Angle" , protractorPosition .x + 55 , protractorPosition .y + 76 , 10 , BLACK );
261258 const char * angleString = TextFormat ("%f" , currentAngleDegrees );
262259 const int angleStringDot = TextFindIndex (angleString , "." );
@@ -278,7 +275,6 @@ void Update(void)
278275 if (currentAngleDegrees != 0.0f ) DrawLineEx (protractorPosition , finalVector , 3.0f , gestureColor );
279276
280277 // Draw touch and mouse pointer points
281- //--------------------------------------------------------------------------------------
282278 if (currentGesture != GESTURE_NONE )
283279 {
284280 if ( touchCount != 0 )
@@ -300,33 +296,42 @@ void Update(void)
300296
301297 EndDrawing ();
302298 //--------------------------------------------------------------------------------------
303-
304299}
305300
306- //------------------------------------------------------------------------------------
307- // Program main entry point
308- //------------------------------------------------------------------------------------
309- int main (void )
301+ static char const * GetGestureName (int i )
310302{
311- // Initialization
312- //--------------------------------------------------------------------------------------
313- InitWindow (screenWidth , screenHeight , "raylib [core] example - input gestures web" );
314- //--------------------------------------------------------------------------------------
315-
316- // Main game loop
317- //--------------------------------------------------------------------------------------
318- #if defined(PLATFORM_WEB )
319- emscripten_set_main_loop (Update , 0 , 1 );
320- #else
321- SetTargetFPS (60 );
322- while (!WindowShouldClose ()) Update (); // Detect window close button or ESC key
323- #endif
324- //--------------------------------------------------------------------------------------
325-
326- // De-Initialization
327- //--------------------------------------------------------------------------------------
328- CloseWindow (); // Close window and OpenGL context
329- //--------------------------------------------------------------------------------------
303+ switch (i )
304+ {
305+ case 0 : return "None" ; break ;
306+ case 1 : return "Tap" ; break ;
307+ case 2 : return "Double Tap" ; break ;
308+ case 4 : return "Hold" ; break ;
309+ case 8 : return "Drag" ; break ;
310+ case 16 : return "Swipe Right" ; break ;
311+ case 32 : return "Swipe Left" ; break ;
312+ case 64 : return "Swipe Up" ; break ;
313+ case 128 : return "Swipe Down" ; break ;
314+ case 256 : return "Pinch In" ; break ;
315+ case 512 : return "Pinch Out" ; break ;
316+ default : return "Unknown" ; break ;
317+ }
318+ }
330319
331- return 0 ;
320+ static Color GetGestureColor (int i )
321+ {
322+ switch (i )
323+ {
324+ case 0 : return BLACK ; break ;
325+ case 1 : return BLUE ; break ;
326+ case 2 : return SKYBLUE ; break ;
327+ case 4 : return BLACK ; break ;
328+ case 8 : return LIME ; break ;
329+ case 16 : return RED ; break ;
330+ case 32 : return RED ; break ;
331+ case 64 : return RED ; break ;
332+ case 128 : return RED ; break ;
333+ case 256 : return VIOLET ; break ;
334+ case 512 : return ORANGE ; break ;
335+ default : return BLACK ; break ;
336+ }
332337}
0 commit comments