-
Notifications
You must be signed in to change notification settings - Fork 1
/
core_input_gestures.php
149 lines (123 loc) · 5.27 KB
/
core_input_gestures.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
use Nawarian\Raylib\Raylib;
use Nawarian\Raylib\Types\{
Color,
Rectangle
};
use function Nawarian\Raylib\{
BeginDrawing,
CheckCollisionPointRec,
ClearBackground,
CloseWindow,
DrawCircleV,
DrawRectangle,
DrawRectangleLines,
DrawRectangleRec,
DrawText,
EndDrawing,
Fade,
GetGestureDetected,
GetTouchPosition,
InitWindow,
SetTargetFPS,
WindowShouldClose
};
const MAX_GESTURE_STRINGS = 20;
// Initialization
//--------------------------------------------------------------------------------------
$screenWidth = 800;
$screenHeight = 450;
InitWindow($screenWidth, $screenHeight, "raylib [core] example - input gestures");
$touchArea = new Rectangle(220, 10, $screenWidth - 230, $screenHeight - 20);
$gesturesCount = 0;
$gestureStrings = array_fill(0, MAX_GESTURE_STRINGS, []);
$currentGesture = Raylib::GESTURE_NONE;
//SetGesturesEnabled(0b0000000000001001); // Enable only some gestures to be detected
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) { // Detect window close button or ESC key
// Update
//----------------------------------------------------------------------------------
// Reset gestures strings
if ($gesturesCount > MAX_GESTURE_STRINGS) {
$gestureStrings = array_fill(0, MAX_GESTURE_STRINGS, '');
$gesturesCount = 0;
}
$lastGesture = $currentGesture;
$currentGesture = GetGestureDetected();
$touchPosition = GetTouchPosition(0);
if (CheckCollisionPointRec($touchPosition, $touchArea) && ($currentGesture !== Raylib::GESTURE_NONE)) {
if ($currentGesture !== $lastGesture) {
// Store gesture string
switch ($currentGesture) {
case Raylib::GESTURE_TAP:
$gestureStrings[$gesturesCount] = 'GESTURE TAP';
break;
case Raylib::GESTURE_DOUBLETAP:
$gestureStrings[$gesturesCount] = 'GESTURE DOUBLETAP';
break;
case Raylib::GESTURE_HOLD:
$gestureStrings[$gesturesCount] = 'GESTURE HOLD';
break;
case Raylib::GESTURE_DRAG:
$gestureStrings[$gesturesCount] = 'GESTURE DRAG';
break;
case Raylib::GESTURE_SWIPE_RIGHT:
$gestureStrings[$gesturesCount] = 'GESTURE SWIPE RIGHT';
break;
case Raylib::GESTURE_SWIPE_LEFT:
$gestureStrings[$gesturesCount] = 'GESTURE SWIPE LEFT';
break;
case Raylib::GESTURE_SWIPE_UP:
$gestureStrings[$gesturesCount] = 'GESTURE SWIPE UP';
break;
case Raylib::GESTURE_SWIPE_DOWN:
$gestureStrings[$gesturesCount] = 'GESTURE SWIPE DOWN';
break;
case Raylib::GESTURE_PINCH_IN:
$gestureStrings[$gesturesCount] = 'GESTURE PINCH IN';
break;
case Raylib::GESTURE_PINCH_OUT:
$gestureStrings[$gesturesCount] = 'GESTURE PINCH OUT';
break;
}
$gesturesCount++;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color::rayWhite());
DrawRectangleRec($touchArea, Color::gray());
DrawRectangle(225, 15, $screenWidth - 240, $screenHeight - 30, Color::rayWhite());
DrawText("GESTURES TEST AREA", $screenWidth - 270, $screenHeight - 40, 20, Fade(Color::gray(), 0.5));
// phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
for ($i = 0; $i < $gesturesCount; ++$i) {
if ($i % 2 === 0) {
DrawRectangle(10, 30 + 20 * $i, 200, 20, Fade(Color::lightGray(), 0.5));
} else {
DrawRectangle(10, 30 + 20 * $i, 200, 20, Fade(Color::lightGray(), 0.3));
}
if ($i < $gesturesCount - 1 && $gestureStrings[$i]) {
DrawText($gestureStrings[$i], 35, 36 + 20 * $i, 10, Color::darkGray());
} elseif ($gestureStrings[$i]) {
DrawText($gestureStrings[$i], 35, 36 + 20 * $i, 10, Color::maroon());
}
}
DrawRectangleLines(10, 29, 200, $screenHeight - 50, Color::gray());
DrawText("DETECTED GESTURES", 50, 15, 10, Color::gray());
if ($currentGesture !== Raylib::GESTURE_NONE) {
DrawCircleV($touchPosition, 30, Color::maroon());
}
// phpcs:enable Generic.WhiteSpace.ScopeIndent.IncorrectExact
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------