From 9e6154076be777b495e5a9cab93afeff3dd0c899 Mon Sep 17 00:00:00 2001 From: "wrenczes@gmail.com" Date: Sat, 11 Jun 2011 10:30:38 +0000 Subject: [PATCH] Added the concept of a 'flick' gesture. This uses the VelocityTracker object in Android and passes the velocity info down as an SDL joystick trackball event. We can then reinterpret this event in whatever way we like, such as up/down swipes in the deck editor (that was my initial thought, at any rate). TBD: right now every up finger generates this event. We probably want to throttle it to only be an event if the velocity exceeds some arbitrary threshold; similarly, if we generate a flick event, we probably don't want to simultaneously generate a finger up action (ie JGE_BTN_OK). --- .../SDL/src/core/android/SDL_android.cpp | 7 +++++ .../SDL/src/video/android/SDL_androidtouch.c | 14 +++++++++ .../SDL/src/video/android/SDL_androidtouch.h | 2 ++ JGE/src/SDLmain.cpp | 6 +++- .../src/org/libsdl/app/SDLActivity.java | 29 +++++++++++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) diff --git a/JGE/Dependencies/SDL/src/core/android/SDL_android.cpp b/JGE/Dependencies/SDL/src/core/android/SDL_android.cpp index 42a25e85c..5f4df77b8 100644 --- a/JGE/Dependencies/SDL/src/core/android/SDL_android.cpp +++ b/JGE/Dependencies/SDL/src/core/android/SDL_android.cpp @@ -128,6 +128,13 @@ extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch( Android_OnTouch(index, action, x, y, p); } +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeFlickGesture( + JNIEnv* env, jclass jcls, + jfloat xVelocity, jfloat yVelocity) +{ + Android_OnFlickGesture(xVelocity, yVelocity); +} + // Accelerometer extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel( JNIEnv* env, jclass jcls, diff --git a/JGE/Dependencies/SDL/src/video/android/SDL_androidtouch.c b/JGE/Dependencies/SDL/src/video/android/SDL_androidtouch.c index fee671cf0..19e32b0c6 100644 --- a/JGE/Dependencies/SDL/src/video/android/SDL_androidtouch.c +++ b/JGE/Dependencies/SDL/src/video/android/SDL_androidtouch.c @@ -119,4 +119,18 @@ void Android_OnTouch(int index, int action, float x, float y, float p) } } +void Android_OnFlickGesture(float xVelocity, float yVelocity) +{ + // cheap hack, translate this to a joystick ball event instead of its own proper event + + SDL_Event event; + event.jball.type = SDL_JOYBALLMOTION; + event.jball.which = 0; + event.jball.ball = 0; + event.jball.xrel = (int) xVelocity;; + event.jball.yrel = (int) yVelocity; + + SDL_PushEvent(&event); +} + /* vi: set ts=4 sw=4 expandtab: */ diff --git a/JGE/Dependencies/SDL/src/video/android/SDL_androidtouch.h b/JGE/Dependencies/SDL/src/video/android/SDL_androidtouch.h index 939c250c1..685197dd7 100644 --- a/JGE/Dependencies/SDL/src/video/android/SDL_androidtouch.h +++ b/JGE/Dependencies/SDL/src/video/android/SDL_androidtouch.h @@ -24,4 +24,6 @@ extern void Android_OnTouch(int index, int action, float x, float y, float p); +extern void Android_OnFlickGesture(float xVelocity, float yVelocity); + /* vi: set ts=4 sw=4 expandtab: */ diff --git a/JGE/src/SDLmain.cpp b/JGE/src/SDLmain.cpp index 384dfa2d2..df337c513 100644 --- a/JGE/src/SDLmain.cpp +++ b/JGE/src/SDLmain.cpp @@ -208,7 +208,11 @@ public: << ", dDist " << Event->mgesture.dDist << ", numFingers " << Event->mgesture.numFingers); break; - } + + case SDL_JOYBALLMOTION: + DebugTrace("Flick gesture detected"); + break; + } } void OnUpdate(); diff --git a/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java b/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java index e6c4c3d2f..368edf4b8 100644 --- a/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java +++ b/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java @@ -95,6 +95,7 @@ public class SDLActivity extends Activity { public static native void onNativeKeyUp(int keycode); public static native void onNativeTouch(int index, int action, float x, float y, float p); + public static native void onNativeFlickGesture(float xVelocity, float yVelocity); public static native void onNativeAccel(float x, float y, float z); public static native void nativeRunAudioThread(); @@ -245,6 +246,8 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, // Sensors private static SensorManager mSensorManager; + private static VelocityTracker mVelocityTracker; + // Startup public SDLSurface(Context context) { super(context); @@ -467,6 +470,32 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, SDLActivity.onNativeTouch(index, action, x, y, p); } + // account for 'flick' type gestures by monitoring velocity + if (event.getActionIndex() == 0) + { + if (event.getAction() == MotionEvent.ACTION_DOWN) + { + mVelocityTracker = VelocityTracker.obtain(); + mVelocityTracker.clear(); + mVelocityTracker.addMovement(event); + } + else if (event.getAction() == MotionEvent.ACTION_MOVE) + { + mVelocityTracker.addMovement(event); + } + else if (event.getAction() == MotionEvent.ACTION_UP) + { + mVelocityTracker.addMovement(event); + + // calc velocity + mVelocityTracker.computeCurrentVelocity(1000); + + SDLActivity.onNativeFlickGesture(mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity()); + + mVelocityTracker.recycle(); + } + } + return true; }