Multiple finger events now translate correctly into an SDL_MULTIGESTURE. I still haven't mapped this into anything meaningful, but all least the wiring is now present.

This commit is contained in:
wrenczes@gmail.com
2011-06-11 09:03:21 +00:00
parent 56a8bed0e3
commit b2eaa3cb8d
6 changed files with 69 additions and 46 deletions

View File

@@ -123,9 +123,9 @@ extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
// Touch
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
JNIEnv* env, jclass jcls,
jint action, jfloat x, jfloat y, jfloat p)
jint index, jint action, jfloat x, jfloat y, jfloat p)
{
Android_OnTouch(action, x, y, p);
Android_OnTouch(index, action, x, y, p);
}
// Accelerometer

View File

@@ -20,7 +20,10 @@
*/
#include "SDL_config.h"
#if defined(ANDROID)
#include <android/log.h>
#endif
/* General touch handling code for SDL */

View File

@@ -35,6 +35,8 @@
#define ACTION_MOVE 2
#define ACTION_CANCEL 3
#define ACTION_OUTSIDE 4
#define ACTION_POINTER_DOWN 5
#define ACTION_POINTER_UP 6
static SDL_TouchID gTouchID = 0;
@@ -64,7 +66,7 @@ SDL_Touch* CreateTouchInstance()
return SDL_GetTouch(touch.id);
}
void Android_OnTouch(int action, float x, float y, float p)
void Android_OnTouch(int index, int action, float x, float y, float p)
{
if (!Android_Window)
{
@@ -81,23 +83,32 @@ void Android_OnTouch(int action, float x, float y, float p)
touch = CreateTouchInstance();
}
char buffer[64];
sprintf(buffer, "Touch action: %d x: %f y: %f", action, x, y);
__android_log_write(ANDROID_LOG_DEBUG, "Wagic", buffer);
//char buffer[64];
//sprintf(buffer, "Touch action: %d x: %f y: %f, index: %d", action, x, y, index);
//__android_log_write(ANDROID_LOG_DEBUG, "Wagic", buffer);
switch(action)
{
case ACTION_DOWN:
SDL_SendFingerDown(touch->id, 1, SDL_TRUE, x, y, p);
case ACTION_POINTER_DOWN:
SDL_SendFingerDown(touch->id, index, SDL_TRUE, x, y, p);
//SDL_SendMouseButton(Android_Window, SDL_PRESSED, SDL_BUTTON_LEFT);
break;
case ACTION_MOVE:
SDL_SendTouchMotion(touch->id, 1, SDL_FALSE, x, y, 1);
SDL_SendTouchMotion(touch->id, index, SDL_FALSE, x, y, 1);
break;
case ACTION_UP:
SDL_SendFingerDown(touch->id, 1, SDL_FALSE, x, y, p);
// this means a completed gesture
SDL_SendFingerDown(touch->id, index, SDL_FALSE, x, y, p);
break;
case ACTION_POINTER_UP:
// this is an individual finger up - due to some false hits on finger 0 lifting, only honor this event
// if the finger up is anything BUT finger 0, this prevents false action triggers
if (index > 0)
SDL_SendFingerDown(touch->id, index, SDL_FALSE, x, y, p);
//SDL_SendMouseButton(Android_Window, SDL_RELEASED, SDL_BUTTON_LEFT);
break;
}

View File

@@ -22,6 +22,6 @@
#include "SDL_androidvideo.h"
extern void Android_OnTouch(int action, float x, float y, float p);
extern void Android_OnTouch(int index, int action, float x, float y, float p);
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -189,13 +189,13 @@ public:
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
DebugTrace("Touch Event triggered");
DebugTrace("touchId " << Event->tfinger.touchId);
DebugTrace("fingerId " << Event->tfinger.fingerId);
DebugTrace("state " << Event->tfinger.state);
DebugTrace("x " << Event->tfinger.x << ", y " << Event->tfinger.y);
DebugTrace("dx " << Event->tfinger.dx << ", dy " << Event->tfinger.dy);
DebugTrace("pressure " << Event->tfinger.pressure);
//DebugTrace("Touch Event triggered");
//DebugTrace("touchId " << Event->tfinger.touchId);
//DebugTrace("fingerId " << Event->tfinger.fingerId);
//DebugTrace("state " << Event->tfinger.state);
//DebugTrace("x " << Event->tfinger.x << ", y " << Event->tfinger.y);
//DebugTrace("dx " << Event->tfinger.dx << ", dy " << Event->tfinger.dy);
//DebugTrace("pressure " << Event->tfinger.pressure);
OnTouchEvent(Event->tfinger);
break;
@@ -206,7 +206,7 @@ public:
<< ", y " << Event->mgesture.y
<< ", dTheta " << Event->mgesture.dTheta
<< ", dDist " << Event->mgesture.dDist
<< ", numFinder " << Event->mgesture.numFingers);
<< ", numFingers " << Event->mgesture.numFingers);
break;
}
}
@@ -497,34 +497,39 @@ void SdlApp::OnMouseClicked(const SDL_MouseButtonEvent& event)
void SdlApp::OnTouchEvent(const SDL_TouchFingerEvent& event)
{
if (event.y >= viewPort.y &&
event.y <= viewPort.y + viewPort.h &&
event.x >= viewPort.x &&
event.x <= viewPort.x + viewPort.w)
// only respond to the first finger for mouse type movements - any additional finger
// should be ignored, and will come through instead as a multigesture event
if (event.fingerId == 0)
{
int actualWidth = (int) JRenderer::GetInstance()->GetActualWidth();
int actualHeight = (int) JRenderer::GetInstance()->GetActualHeight();
if (event.y >= viewPort.y &&
event.y <= viewPort.y + viewPort.h &&
event.x >= viewPort.x &&
event.x <= viewPort.x + viewPort.w)
{
int actualWidth = (int) JRenderer::GetInstance()->GetActualWidth();
int actualHeight = (int) JRenderer::GetInstance()->GetActualHeight();
g_engine->LeftClicked(
((event.x - viewPort.x) * SCREEN_WIDTH) / actualWidth,
((event.y - viewPort.y) * SCREEN_HEIGHT) / actualHeight);
g_engine->LeftClicked(
((event.x - viewPort.x) * SCREEN_WIDTH) / actualWidth,
((event.y - viewPort.y) * SCREEN_HEIGHT) / actualHeight);
if (event.type == SDL_FINGERDOWN)
{
mMouseDownX = event.x;
mMouseDownY = event.y;
}
if (event.type == SDL_FINGERDOWN)
{
mMouseDownX = event.x;
mMouseDownY = event.y;
}
#if (defined ANDROID) || (defined IOS)
if (event.type == SDL_FINGERUP)
{
// treat an up finger within 50 pixels of the down finger coords as a double click event
if (abs(mMouseDownX - event.x) < kHitzonePliancy && abs(mMouseDownY - event.y) < kHitzonePliancy)
if (event.type == SDL_FINGERUP)
{
g_engine->HoldKey_NoRepeat(JGE_BTN_OK);
// treat an up finger within 50 pixels of the down finger coords as a double click event
if (abs(mMouseDownX - event.x) < kHitzonePliancy && abs(mMouseDownY - event.y) < kHitzonePliancy)
{
g_engine->HoldKey_NoRepeat(JGE_BTN_OK);
}
}
}
#endif
}
}
}

View File

@@ -93,7 +93,7 @@ public class SDLActivity extends Activity {
public static native void onNativeResize(int x, int y, int format);
public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode);
public static native void onNativeTouch(int action, float x,
public static native void onNativeTouch(int index, int action, float x,
float y, float p);
public static native void onNativeAccel(float x, float y, float z);
public static native void nativeRunAudioThread();
@@ -456,14 +456,18 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
// Touch events
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
float x = event.getX();
float y = event.getY();
float p = event.getPressure();
for (int index = 0; index < event.getPointerCount(); ++index)
{
int action = event.getActionMasked();
float x = event.getX(index);
float y = event.getY(index);
float p = event.getPressure(index);
// TODO: Anything else we need to pass?
SDLActivity.onNativeTouch(action, x, y, p);
return true;
// TODO: Anything else we need to pass?
SDLActivity.onNativeTouch(index, action, x, y, p);
}
return true;
}
// Sensor events