diff options
| author | raysan5 <raysan5@gmail.com> | 2014-09-19 12:34:25 +0200 |
|---|---|---|
| committer | raysan5 <raysan5@gmail.com> | 2014-09-19 12:34:25 +0200 |
| commit | 42b7b117104b957414c371871a724019b8d0b59a (patch) | |
| tree | 05773c6809b410c0200ce8b94e48839a41d4dc76 /src/core.c | |
| parent | e915812730641e56efa4014eba2406f25b157615 (diff) | |
| download | raylib-42b7b117104b957414c371871a724019b8d0b59a.tar.gz raylib-42b7b117104b957414c371871a724019b8d0b59a.zip | |
Android: Added support for Tap gesture
Diffstat (limited to 'src/core.c')
| -rw-r--r-- | src/core.c | 77 |
1 files changed, 71 insertions, 6 deletions
@@ -108,6 +108,16 @@ static struct android_app *app; // Android activity static struct android_poll_source *source; // Android events polling source static int ident, events; static bool windowReady = false; // Used to detect display initialization + +// Gestures detection variables +static float tapTouchX, tapTouchY; +static bool touchTap = false; +static int32_t touchId; +const int32_t DOUBLE_TAP_TIMEOUT = 300*1000000; +const int32_t DOUBLE_TAP_SLOP = 100; +const int32_t TAP_TIMEOUT = 180*1000000; +const int32_t TOUCH_SLOP = 8; + #elif defined(PLATFORM_RPI) static EGL_DISPMANX_WINDOW_T nativeWindow; // Native window (graphic device) @@ -816,6 +826,11 @@ bool IsGamepadButtonUp(int gamepad, int button) #endif #if defined(PLATFORM_ANDROID) +bool IsScreenTouched(void) +{ + return touchTap; +} + // Returns touch position X int GetTouchX(void) { @@ -958,12 +973,14 @@ static void InitDisplay(int width, int height) { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, // Type of context support -> Required on RPI? //EGL_SURFACE_TYPE, EGL_WINDOW_BIT, // Don't use it on Android! - EGL_BLUE_SIZE, 8, // Alternative: 5 - EGL_GREEN_SIZE, 8, // Alternative: 6 - EGL_RED_SIZE, 8, // Alternative: 5 - //EGL_ALPHA_SIZE, 8, - EGL_DEPTH_SIZE, 8, // NOTE: Required to use Depth testing! - //EGL_SAMPLES, 4, // 4x Antialiasing (Free on MALI GPUs) + EGL_RED_SIZE, 8, // RED color bit depth (alternative: 5) + EGL_GREEN_SIZE, 8, // GREEN color bit depth (alternative: 6) + EGL_BLUE_SIZE, 8, // BLUE color bit depth (alternative: 5) + //EGL_ALPHA_SIZE, 8, // ALPHA bit depth + EGL_DEPTH_SIZE, 8, // Depth buffer size (Required to use Depth testing!) + //EGL_STENCIL_SIZE, 8, // Stencil buffer size + //EGL_SAMPLE_BUFFERS, 1, // Activate MSAA + //EGL_SAMPLES, 4, // 4x Antialiasing (Free on MALI GPUs) EGL_NONE }; @@ -1155,6 +1172,51 @@ static int32_t InputCallback(struct android_app *app, AInputEvent *event) touchY = AMotionEvent_getY(event, 0) * ((float)renderHeight / (float)displayHeight) - renderOffsetY/2; } + // Detect TAP event +/* + if (AMotionEvent_getPointerCount(event) > 1 ) + { + // Only support single touch + return false; + } +*/ + int32_t action = AMotionEvent_getAction(event); + unsigned int flags = action & AMOTION_EVENT_ACTION_MASK; + + switch (flags) + { + case AMOTION_EVENT_ACTION_DOWN: + { + touchId = AMotionEvent_getPointerId(event, 0); + tapTouchX = AMotionEvent_getX(event, 0); + tapTouchY = AMotionEvent_getY(event, 0); + + } break; + case AMOTION_EVENT_ACTION_UP: + { + int64_t eventTime = AMotionEvent_getEventTime(event); + int64_t downTime = AMotionEvent_getDownTime(event); + + if (eventTime - downTime <= TAP_TIMEOUT) + { + if (touchId == AMotionEvent_getPointerId(event, 0)) + { + float x = AMotionEvent_getX(event, 0) - tapTouchX; + float y = AMotionEvent_getY(event, 0) - tapTouchY; + + float densityFactor = 1.0f; + + if ( x*x + y*y < TOUCH_SLOP*TOUCH_SLOP * densityFactor) + { + // TAP Detected + touchTap = true; + } + } + } + break; + } + } + //float AMotionEvent_getX(event, size_t pointer_index); //int32_t AMotionEvent_getButtonState(event); // Pressed buttons //int32_t AMotionEvent_getPointerId(event, size_t pointer_index); @@ -1368,6 +1430,9 @@ static void PollInputEvents(void) // TODO: Check virtual keyboard (?) + // Reset touchTap event + touchTap = false; + // Poll Events (registered events) while ((ident = ALooper_pollAll(0, NULL, &events,(void**)&source)) >= 0) { |
