Moved gestures touch functionality back to core

Required gestures module when compiling for Android and Web
This commit is contained in:
Ray 2015-10-30 11:30:32 +01:00
parent 67423ff64b
commit 580c0a7296
3 changed files with 107 additions and 55 deletions

View File

@ -95,6 +95,11 @@
#define DEFAULT_GAMEPAD_DEV "/dev/input/js0"
#endif
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#endif
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
@ -158,6 +163,10 @@ static int renderOffsetY = 0; // Offset Y from render area (must b
static bool fullscreen = false; // Fullscreen mode (useful only for PLATFORM_DESKTOP)
static Matrix downscaleView; // Matrix to downscale view (in case screen size bigger than display size)
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
static Vector2 touchPosition; // Touch position on screen
#endif
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB)
static const char *windowTitle; // Window text title...
@ -247,6 +256,10 @@ static void TakeScreenshot(void);
static void AndroidCommandCallback(struct android_app *app, int32_t cmd); // Process Android activity lifecycle commands
#endif
#if defined(PLATFORM_WEB)
static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const EmscriptenFullscreenChangeEvent *e, void *userData);
#endif
//----------------------------------------------------------------------------------
// Module Functions Definition - Window and OpenGL Context Functions
//----------------------------------------------------------------------------------
@ -279,6 +292,12 @@ void InitWindow(int width, int height, const char *title)
InitGamepad(); // Gamepad init
#endif
#if defined(PLATFORM_WEB)
InitGesturesSystem();
emscripten_set_fullscreenchange_callback(0, 0, 1, EmscriptenFullscreenChangeCallback);
#endif
mousePosition.x = screenWidth/2;
mousePosition.y = screenHeight/2;
@ -335,6 +354,8 @@ void InitWindow(int width, int height, struct android_app *state)
//InitGesturesSystem(app); // NOTE: Must be called by user
InitAssetManager(app->activity->assetManager);
InitGesturesSystem(app);
TraceLog(INFO, "Android app initialized successfully");
@ -499,7 +520,7 @@ void EndDrawing(void)
SwapBuffers(); // Copy back buffer to front buffer
PollInputEvents(); // Poll user events
currentTime = GetTime();
drawTime = currentTime - previousTime;
previousTime = currentTime;
@ -814,7 +835,7 @@ Vector2 GetMousePosition(void)
void SetMousePosition(Vector2 position)
{
mousePosition = position;
#if defined(PLATFORM_DESKTOP)
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
// NOTE: emscripten not implemented
glfwSetCursorPos(window, position.x, position.y);
#endif
@ -969,6 +990,41 @@ bool IsGamepadButtonUp(int gamepad, int button)
}
#endif
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
// Returns touch position X
int GetTouchX(void)
{
return (int)touchPosition.x;
}
// Returns touch position Y
int GetTouchY(void)
{
return (int)touchPosition.y;
}
// Returns touch position XY
// TODO: touch position should be scaled depending on display size and render size
Vector2 GetTouchPosition(void)
{
Vector2 position = touchPosition;
if ((screenWidth > displayWidth) || (screenHeight > displayHeight))
{
// TODO: Seems to work ok but... review!
position.x = position.x*((float)screenWidth/(float)(displayWidth - renderOffsetX)) - renderOffsetX/2;
position.y = position.y*((float)screenHeight/(float)(displayHeight - renderOffsetY)) - renderOffsetY/2;
}
else
{
position.x = position.x*((float)renderWidth/(float)displayWidth) - renderOffsetX/2;
position.y = position.y*((float)renderHeight/(float)displayHeight) - renderOffsetY/2;
}
return position;
}
#endif
//----------------------------------------------------------------------------------
// Module specific Functions Definition
//----------------------------------------------------------------------------------
@ -1547,6 +1603,13 @@ static bool GetMouseButtonStatus(int button)
// Poll (store) all input events
static void PollInputEvents(void)
{
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
// Touch events reading (requires gestures module)
touchPosition = GetRawTouchPosition();
UpdateGestures();
#endif
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
// Mouse input polling
double mouseX;
@ -1940,6 +2003,31 @@ static void SetupFramebufferSize(int displayWidth, int displayHeight)
}
}
#if defined(PLATFORM_WEB)
static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const EmscriptenFullscreenChangeEvent *e, void *userData)
{
//isFullscreen: int e->isFullscreen
//fullscreenEnabled: int e->fullscreenEnabled
//fs element nodeName: (char *) e->nodeName
//fs element id: (char *) e->id
//Current element size: (int) e->elementWidth, (int) e->elementHeight
//Screen size:(int) e->screenWidth, (int) e->screenHeight
if (e->isFullscreen)
{
TraceLog(INFO, "Canvas scaled to fullscreen. ElementSize: (%ix%i), ScreenSize(%ix%i)", e->elementWidth, e->elementHeight, e->screenWidth, e->screenHeight);
}
else
{
TraceLog(INFO, "Canvas scaled to windowed. ElementSize: (%ix%i), ScreenSize(%ix%i)", e->elementWidth, e->elementHeight, e->screenWidth, e->screenHeight);
}
// TODO: Depending on scaling factor (screen vs element), calculate factor to scale mouse/touch input
return 0;
}
#endif
// Plays raylib logo appearing animation
static void LogoAnimation(void)
{

View File

@ -122,7 +122,7 @@ static int currentGesture = GESTURE_NONE;
static unsigned int enabledGestures = 0; // TODO: Currently not in use...
static Vector2 touchPosition;
static Vector2 rawTouchPosition;
//----------------------------------------------------------------------------------
// Module specific Functions Declaration
@ -147,55 +147,15 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event)
// Module Functions Definition
//----------------------------------------------------------------------------------
// Returns touch position X
int GetTouchX(void)
// Get touch position (could require further processing depending on display size)
Vector2 GetRawTouchPosition(void)
{
return (int)touchPosition.x;
}
// Returns touch position Y
int GetTouchY(void)
{
return (int)touchPosition.y;
}
// Returns touch position XY
// TODO: touch position should be scaled depending on display size and render size
Vector2 GetTouchPosition(void)
{
Vector2 position = touchPosition;
/*
if ((screenWidth > displayWidth) || (screenHeight > displayHeight))
{
// TODO: Seems to work ok but... review!
position.x = position.x*((float)screenWidth / (float)(displayWidth - renderOffsetX)) - renderOffsetX/2;
position.y = position.y*((float)screenHeight / (float)(displayHeight - renderOffsetY)) - renderOffsetY/2;
}
else
{
position.x = position.x*((float)renderWidth / (float)displayWidth) - renderOffsetX/2;
position.y = position.y*((float)renderHeight / (float)displayHeight) - renderOffsetY/2;
}
*/
return position;
return rawTouchPosition;
}
// Check if a gesture have been detected
bool IsGestureDetected(void)
{
/*
if (currentGesture == GESTURE_DRAG) TraceLog(INFO, "DRAG");
else if (currentGesture == GESTURE_TAP) TraceLog(INFO, "TAP");
else if (currentGesture == GESTURE_DOUBLETAP) TraceLog(INFO, "DOUBLE");
else if (currentGesture == GESTURE_HOLD) TraceLog(INFO, "HOLD");
else if (currentGesture == GESTURE_SWIPE_RIGHT) TraceLog(INFO, "RIGHT");
else if (currentGesture == GESTURE_SWIPE_UP) TraceLog(INFO, "UP");
else if (currentGesture == GESTURE_SWIPE_LEFT) TraceLog(INFO, "LEFT");
else if (currentGesture == GESTURE_SWIPE_DOWN) TraceLog(INFO, "DOWN");
else if (currentGesture == GESTURE_PINCH_IN) TraceLog(INFO, "PINCH IN");
else if (currentGesture == GESTURE_PINCH_OUT) TraceLog(INFO, "PINCH OUT");
*/
if (currentGesture != GESTURE_NONE) return true;
else return false;
}
@ -382,7 +342,7 @@ static void ProcessMotionEvent(GestureEvent event)
{
lastDragPosition = endDragPosition;
endDragPosition = touchPosition;
endDragPosition = rawTouchPosition;
//endDragPosition.x = AMotionEvent_getX(event, 0);
//endDragPosition.y = AMotionEvent_getY(event, 0);
@ -594,8 +554,8 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event)
if (type == AINPUT_EVENT_TYPE_MOTION)
{
touchPosition.x = AMotionEvent_getX(event, 0);
touchPosition.y = AMotionEvent_getY(event, 0);
rawTouchPosition.x = AMotionEvent_getX(event, 0);
rawTouchPosition.y = AMotionEvent_getY(event, 0);
}
else if (type == AINPUT_EVENT_TYPE_KEY)
{
@ -676,10 +636,13 @@ static EM_BOOL EmscriptenInputCallback(int eventType, const EmscriptenTouchEvent
gestureEvent.pointCount = touchEvent->numTouches;
// Position
gestureEvent.position[0] = (Vector2){ touchEvent->touches[0].canvasX, touchEvent->touches[0].canvasY };
gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].canvasX, touchEvent->touches[1].canvasY };
//gestureEvent.position[0] = (Vector2){ touchEvent->touches[0].canvasX, touchEvent->touches[0].canvasY };
//gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].canvasX, touchEvent->touches[1].canvasY };
gestureEvent.position[0] = (Vector2){ touchEvent->touches[0].targetX, touchEvent->touches[0].targetY };
gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].targetX, touchEvent->touches[1].targetY };
printf("EVENT DETECTED!\n");
touchPosition = gestureEvent.position[0];
rawTouchPosition = gestureEvent.position[0];
ProcessMotionEvent(gestureEvent);

View File

@ -458,13 +458,14 @@ bool IsGamepadButtonUp(int gamepad, int button); // Detect if a gamepad b
#endif
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
//------------------------------------------------------------------------------------
// Gestures and Touch Handling Functions (Module: gestures)
//------------------------------------------------------------------------------------
int GetTouchX(void); // Returns touch position X (relative to screen size)
int GetTouchY(void); // Returns touch position Y (relative to screen size)
Vector2 GetTouchPosition(void); // Returns touch position XY (relative to screen size)
//------------------------------------------------------------------------------------
// Gestures and Touch Handling Functions (Module: gestures)
//------------------------------------------------------------------------------------
Vector2 GetRawTouchPosition(void); // Gewt touch position (raw)
#if defined(PLATFORM_WEB)
void InitGesturesSystem(void); // Init gestures system (web)
#elif defined(PLATFORM_ANDROID)