mirror of
https://github.com/raysan5/raylib.git
synced 2025-09-27 13:38:30 +00:00
REVIEWED: example: core_input_gestures_testbed
, follow default structure
This commit is contained in:
@@ -19,56 +19,14 @@
|
|||||||
|
|
||||||
#include <math.h> // Required for the protractor angle graphic drawing
|
#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 GESTURE_LOG_SIZE 20
|
||||||
#define MAX_TOUCH_COUNT 32
|
#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
|
// Module Functions Declaration
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
static void UpdateDrawFrame(void); // Update and Draw one frame
|
static char const *GetGestureName(int gesture); // Get text string for gesture value
|
||||||
static char const *GetGestureName(int i);
|
static Color GetGestureColor(int gesture); // Get color for gesture value
|
||||||
static Color GetGestureColor(int i);
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
// Program main entry point
|
// Program main entry point
|
||||||
@@ -77,33 +35,48 @@ int main(void)
|
|||||||
{
|
{
|
||||||
// Initialization
|
// Initialization
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
const int screenWidth = 800;
|
||||||
|
const int screenHeight = 450;
|
||||||
|
|
||||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - input gestures testbed");
|
InitWindow(screenWidth, screenHeight, "raylib [core] example - input gestures testbed");
|
||||||
|
|
||||||
#if defined(PLATFORM_WEB)
|
Vector2 messagePosition = { 160, 7 };
|
||||||
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
|
||||||
#else
|
// 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
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Main game loop
|
// Main game loop
|
||||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
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
|
// Update
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
@@ -176,25 +149,18 @@ static void UpdateDrawFrame(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle protractor
|
// Handle protractor
|
||||||
if (currentGesture > 255) // aka Pinch In and Pinch Out
|
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
|
||||||
currentAngleDegrees = currentPitchDegrees;
|
else if (currentGesture > 0) currentAngleDegrees = 0.0f; // Tap, Doubletap, Hold and Grab
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
float currentAngleRadians = ((currentAngleDegrees + 90.0f)*PI/180); // Convert the current angle to Radians
|
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
|
// Handle touch and mouse pointer points
|
||||||
Vector2 touchPosition[MAX_TOUCH_COUNT] = { 0 };
|
Vector2 touchPosition[MAX_TOUCH_COUNT] = { 0 };
|
||||||
Vector2 mousePosition = {0, 0};
|
Vector2 mousePosition = { 0 };
|
||||||
if (currentGesture != GESTURE_NONE)
|
if (currentGesture != GESTURE_NONE)
|
||||||
{
|
{
|
||||||
if (touchCount != 0)
|
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 0: return "None"; break;
|
||||||
case 1: return "Tap"; 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 0: return BLACK; break;
|
||||||
case 1: return BLUE; break;
|
case 1: return BLUE; break;
|
||||||
|
Reference in New Issue
Block a user