REVIEWED: example: core_input_gestures_testbed, follow default structure

This commit is contained in:
Ray
2025-09-14 10:04:24 +02:00
parent 9931ddd27a
commit c9b1f2ce54

View File

@@ -19,56 +19,14 @@
#include <math.h> // Required for the protractor angle graphic drawing
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h> // Required for the Web/HTML5
#endif
#define GESTURE_LOG_SIZE 20
#define MAX_TOUCH_COUNT 32
//--------------------------------------------------------------------------------------
// Global Variables Definition
//--------------------------------------------------------------------------------------
static int screenWidth = 800; // Update depending on web canvas
static const int screenHeight = 450;
static Vector2 messagePosition = { 160, 7 };
// Last gesture variables definitions
static int lastGesture = 0;
static Vector2 lastGesturePosition = { 165, 130 };
// Gesture log variables definitions
// NOTE: The gesture log uses an array (as an inverted circular queue) to store the performed gestures
static char gestureLog[GESTURE_LOG_SIZE][12] = { "" };
// NOTE: The index for the inverted circular queue (moving from last to first direction, then looping around)
static int gestureLogIndex = GESTURE_LOG_SIZE;
static int previousGesture = 0;
// 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
static int logMode = 1;
static Color gestureColor = { 0, 0, 0, 255 };
static Rectangle logButton1 = { 53, 7, 48, 26 };
static Rectangle logButton2 = { 108, 7, 36, 26 };
static Vector2 gestureLogPosition = { 10, 10 };
// Protractor variables definitions
static float angleLength = 90.0f;
static float currentAngleDegrees = 0.0f;
static Vector2 finalVector = { 0.0f, 0.0f };
static char currentAngleStr[7] = "";
static Vector2 protractorPosition = { 266.0f, 315.0f };
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
static void UpdateDrawFrame(void); // Update and Draw one frame
static char const *GetGestureName(int i);
static Color GetGestureColor(int i);
static char const *GetGestureName(int gesture); // Get text string for gesture value
static Color GetGestureColor(int gesture); // Get color for gesture value
//------------------------------------------------------------------------------------
// Program main entry point
@@ -77,33 +35,48 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - input gestures testbed");
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
Vector2 messagePosition = { 160, 7 };
// Last gesture variables definitions
int lastGesture = 0;
Vector2 lastGesturePosition = { 165, 130 };
// Gesture log variables definitions
// NOTE: The gesture log uses an array (as an inverted circular queue) to store the performed gestures
char gestureLog[GESTURE_LOG_SIZE][12] = { "" };
// NOTE: The index for the inverted circular queue (moving from last to first direction, then looping around)
int gestureLogIndex = GESTURE_LOG_SIZE;
int previousGesture = 0;
// 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
int logMode = 1;
Color gestureColor = { 0, 0, 0, 255 };
Rectangle logButton1 = { 53, 7, 48, 26 };
Rectangle logButton2 = { 108, 7, 36, 26 };
Vector2 gestureLogPosition = { 10, 10 };
// Protractor variables definitions
float angleLength = 90.0f;
float currentAngleDegrees = 0.0f;
Vector2 finalVector = { 0.0f, 0.0f };
char currentAngleStr[7] = "";
Vector2 protractorPosition = { 266.0f, 315.0f };
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
UpdateDrawFrame();
}
#endif
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
static void UpdateDrawFrame(void)
{
// Update
//--------------------------------------------------------------------------------------
@@ -176,25 +149,18 @@ static void UpdateDrawFrame(void)
}
// Handle protractor
if (currentGesture > 255) // aka Pinch In and Pinch Out
{
currentAngleDegrees = currentPitchDegrees;
}
else if (currentGesture > 15) // aka Swipe Right, Swipe Left, Swipe Up and Swipe Down
{
currentAngleDegrees = currentDragDegrees;
}
else if (currentGesture > 0) // aka Tap, Doubletap, Hold and Grab
{
currentAngleDegrees = 0.0f;
}
if (currentGesture > 255) currentAngleDegrees = currentPitchDegrees; // Pinch In and Pinch Out
else if (currentGesture > 15) currentAngleDegrees = currentDragDegrees; // Swipe Right, Swipe Left, Swipe Up and Swipe Down
else if (currentGesture > 0) currentAngleDegrees = 0.0f; // Tap, Doubletap, Hold and Grab
float currentAngleRadians = ((currentAngleDegrees + 90.0f)*PI/180); // Convert the current angle to Radians
finalVector = (Vector2){ (angleLength*sinf(currentAngleRadians)) + protractorPosition.x, (angleLength*cosf(currentAngleRadians)) + protractorPosition.y }; // Calculate the final vector for display
// Calculate the final vector for display
finalVector = (Vector2){ (angleLength*sinf(currentAngleRadians)) + protractorPosition.x,
(angleLength*cosf(currentAngleRadians)) + protractorPosition.y };
// Handle touch and mouse pointer points
Vector2 touchPosition[MAX_TOUCH_COUNT] = { 0 };
Vector2 mousePosition = {0, 0};
Vector2 mousePosition = { 0 };
if (currentGesture != GESTURE_NONE)
{
if (touchCount != 0)
@@ -298,9 +264,21 @@ static void UpdateDrawFrame(void)
//--------------------------------------------------------------------------------------
}
static char const *GetGestureName(int i)
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
// Get text string for gesture value
static char const *GetGestureName(int gesture)
{
switch (i)
switch (gesture)
{
case 0: return "None"; break;
case 1: return "Tap"; break;
@@ -317,9 +295,10 @@ static char const *GetGestureName(int i)
}
}
static Color GetGestureColor(int i)
// Get color for gesture value
static Color GetGestureColor(int gesture)
{
switch (i)
switch (gesture)
{
case 0: return BLACK; break;
case 1: return BLUE; break;