1 | /******************************************************************************************* |
2 | * |
3 | * raylib [core] example - Input Gestures Detection |
4 | * |
5 | * This example has been created using raylib 1.4 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Copyright (c) 2016 Ramon Santamaria (@raysan5) |
9 | * |
10 | ********************************************************************************************/ |
11 | |
12 | #include "raylib.h" |
13 | #include <string.h> |
14 | |
15 | #define MAX_GESTURE_STRINGS 20 |
16 | |
17 | int main(void) |
18 | { |
19 | // Initialization |
20 | //-------------------------------------------------------------------------------------- |
21 | const int screenWidth = 800; |
22 | const int screenHeight = 450; |
23 | |
24 | InitWindow(screenWidth, screenHeight, "raylib [core] example - input gestures" ); |
25 | |
26 | Vector2 touchPosition = { 0, 0 }; |
27 | Rectangle touchArea = { 220, 10, screenWidth - 230, screenHeight - 20 }; |
28 | |
29 | int gesturesCount = 0; |
30 | char gestureStrings[MAX_GESTURE_STRINGS][32]; |
31 | |
32 | int currentGesture = GESTURE_NONE; |
33 | int lastGesture = GESTURE_NONE; |
34 | |
35 | //SetGesturesEnabled(0b0000000000001001); // Enable only some gestures to be detected |
36 | |
37 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
38 | //-------------------------------------------------------------------------------------- |
39 | |
40 | // Main game loop |
41 | while (!WindowShouldClose()) // Detect window close button or ESC key |
42 | { |
43 | // Update |
44 | //---------------------------------------------------------------------------------- |
45 | lastGesture = currentGesture; |
46 | currentGesture = GetGestureDetected(); |
47 | touchPosition = GetTouchPosition(0); |
48 | |
49 | if (CheckCollisionPointRec(touchPosition, touchArea) && (currentGesture != GESTURE_NONE)) |
50 | { |
51 | if (currentGesture != lastGesture) |
52 | { |
53 | // Store gesture string |
54 | switch (currentGesture) |
55 | { |
56 | case GESTURE_TAP: strcpy(gestureStrings[gesturesCount], "GESTURE TAP" ); break; |
57 | case GESTURE_DOUBLETAP: strcpy(gestureStrings[gesturesCount], "GESTURE DOUBLETAP" ); break; |
58 | case GESTURE_HOLD: strcpy(gestureStrings[gesturesCount], "GESTURE HOLD" ); break; |
59 | case GESTURE_DRAG: strcpy(gestureStrings[gesturesCount], "GESTURE DRAG" ); break; |
60 | case GESTURE_SWIPE_RIGHT: strcpy(gestureStrings[gesturesCount], "GESTURE SWIPE RIGHT" ); break; |
61 | case GESTURE_SWIPE_LEFT: strcpy(gestureStrings[gesturesCount], "GESTURE SWIPE LEFT" ); break; |
62 | case GESTURE_SWIPE_UP: strcpy(gestureStrings[gesturesCount], "GESTURE SWIPE UP" ); break; |
63 | case GESTURE_SWIPE_DOWN: strcpy(gestureStrings[gesturesCount], "GESTURE SWIPE DOWN" ); break; |
64 | case GESTURE_PINCH_IN: strcpy(gestureStrings[gesturesCount], "GESTURE PINCH IN" ); break; |
65 | case GESTURE_PINCH_OUT: strcpy(gestureStrings[gesturesCount], "GESTURE PINCH OUT" ); break; |
66 | default: break; |
67 | } |
68 | |
69 | gesturesCount++; |
70 | |
71 | // Reset gestures strings |
72 | if (gesturesCount >= MAX_GESTURE_STRINGS) |
73 | { |
74 | for (int i = 0; i < MAX_GESTURE_STRINGS; i++) strcpy(gestureStrings[i], "\0" ); |
75 | |
76 | gesturesCount = 0; |
77 | } |
78 | } |
79 | } |
80 | //---------------------------------------------------------------------------------- |
81 | |
82 | // Draw |
83 | //---------------------------------------------------------------------------------- |
84 | BeginDrawing(); |
85 | |
86 | ClearBackground(RAYWHITE); |
87 | |
88 | DrawRectangleRec(touchArea, GRAY); |
89 | DrawRectangle(225, 15, screenWidth - 240, screenHeight - 30, RAYWHITE); |
90 | |
91 | DrawText("GESTURES TEST AREA" , screenWidth - 270, screenHeight - 40, 20, Fade(GRAY, 0.5f)); |
92 | |
93 | for (int i = 0; i < gesturesCount; i++) |
94 | { |
95 | if (i%2 == 0) DrawRectangle(10, 30 + 20*i, 200, 20, Fade(LIGHTGRAY, 0.5f)); |
96 | else DrawRectangle(10, 30 + 20*i, 200, 20, Fade(LIGHTGRAY, 0.3f)); |
97 | |
98 | if (i < gesturesCount - 1) DrawText(gestureStrings[i], 35, 36 + 20*i, 10, DARKGRAY); |
99 | else DrawText(gestureStrings[i], 35, 36 + 20*i, 10, MAROON); |
100 | } |
101 | |
102 | DrawRectangleLines(10, 29, 200, screenHeight - 50, GRAY); |
103 | DrawText("DETECTED GESTURES" , 50, 15, 10, GRAY); |
104 | |
105 | if (currentGesture != GESTURE_NONE) DrawCircleV(touchPosition, 30, MAROON); |
106 | |
107 | EndDrawing(); |
108 | //---------------------------------------------------------------------------------- |
109 | } |
110 | |
111 | // De-Initialization |
112 | //-------------------------------------------------------------------------------------- |
113 | CloseWindow(); // Close window and OpenGL context |
114 | //-------------------------------------------------------------------------------------- |
115 | } |