From bf820262959ae716da3790669040e2fdac0646d8 Mon Sep 17 00:00:00 2001 From: saschawillems Date: Sat, 25 Mar 2017 11:51:32 +0100 Subject: [PATCH] Android touch double tap detection (Refs #173) [skip ci] --- base/vulkanexamplebase.cpp | 25 ++++++++++++++++++++----- vulkanExamples.sln | 1 + 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index 261ade0e..5c786bd1 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -1181,6 +1181,9 @@ int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent* switch (action) { case AMOTION_EVENT_ACTION_UP: { + vulkanExample->lastTapTime = AMotionEvent_getEventTime(event); + vulkanExample->touchPos.x = AMotionEvent_getX(event, 0); + vulkanExample->touchPos.y = AMotionEvent_getY(event, 0); vulkanExample->touchTimer = 0.0; vulkanExample->touchDown = false; vulkanExample->camera.keys.up = false; @@ -1188,9 +1191,22 @@ int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent* break; } case AMOTION_EVENT_ACTION_DOWN: { + // Detect double tap + int64_t eventTime = AMotionEvent_getEventTime(event); + if (eventTime - vulkanExample->lastTapTime <= vks::android::DOUBLE_TAP_TIMEOUT) { + float deadZone = (160.f / vks::android::screenDensity) * vks::android::DOUBLE_TAP_SLOP * vks::android::DOUBLE_TAP_SLOP; + float x = AMotionEvent_getX(event, 0) - vulkanExample->touchPos.x; + float y = AMotionEvent_getY(event, 0) - vulkanExample->touchPos.y; + if ((x * x + y * y) < deadZone) { + vulkanExample->keyPressed(TOUCH_DOUBLE_TAP); + vulkanExample->touchDown = false; + } + } + else { + vulkanExample->touchDown = true; + } vulkanExample->touchPos.x = AMotionEvent_getX(event, 0); vulkanExample->touchPos.y = AMotionEvent_getY(event, 0); - vulkanExample->touchDown = true; break; } case AMOTION_EVENT_ACTION_MOVE: { @@ -1203,14 +1219,13 @@ int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent* vulkanExample->camera.rotate(glm::vec3(deltaX, 0.0f, 0.0f)); vulkanExample->camera.rotate(glm::vec3(0.0f, -deltaY, 0.0f)); - vulkanExample->rotation.x += deltaX; - vulkanExample->rotation.y -= deltaY; + vulkanExample->rotation.x += deltaX; + vulkanExample->rotation.y -= deltaY; - vulkanExample->viewChanged(); + vulkanExample->viewChanged(); vulkanExample->touchPos.x = eventX; vulkanExample->touchPos.y = eventY; - break; } default: diff --git a/vulkanExamples.sln b/vulkanExamples.sln index ea954495..d0a91afb 100644 --- a/vulkanExamples.sln +++ b/vulkanExamples.sln @@ -77,6 +77,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Base", "Base", "{09B9A54B-F ProjectSection(SolutionItems) = preProject base\camera.hpp = base\camera.hpp base\frustum.hpp = base\frustum.hpp + base\keycodes.hpp = base\keycodes.hpp base\threadpool.hpp = base\threadpool.hpp base\vulkanandroid.cpp = base\vulkanandroid.cpp base\vulkanandroid.h = base\vulkanandroid.h