Fixes bug #1506, Changing orientation on the Android device throws off touch scaling

This commit is contained in:
Gabriel Jacobo 2012-08-24 13:10:29 -03:00
parent 5c98584a78
commit 052e84a94b
2 changed files with 17 additions and 7 deletions

View file

@ -449,6 +449,9 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
// Sensors // Sensors
private static SensorManager mSensorManager; private static SensorManager mSensorManager;
// Keep track of the surface size to normalize touch events
private static float mWidth, mHeight;
// Startup // Startup
public SDLSurface(Context context) { public SDLSurface(Context context) {
super(context); super(context);
@ -460,7 +463,11 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
setOnKeyListener(this); setOnKeyListener(this);
setOnTouchListener(this); setOnTouchListener(this);
mSensorManager = (SensorManager)context.getSystemService("sensor"); mSensorManager = (SensorManager)context.getSystemService("sensor");
// Some arbitrary defaults to avoid a potential division by zero
mWidth = 1.0f;
mHeight = 1.0f;
} }
// Called when we have a valid drawing surface // Called when we have a valid drawing surface
@ -529,6 +536,9 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
Log.v("SDL", "pixel format unknown " + format); Log.v("SDL", "pixel format unknown " + format);
break; break;
} }
mWidth = (float) width;
mHeight = (float) height;
SDLActivity.onNativeResize(width, height, sdlFormat); SDLActivity.onNativeResize(width, height, sdlFormat);
Log.v("SDL", "Window size:" + width + "x"+height); Log.v("SDL", "Window size:" + width + "x"+height);
@ -568,8 +578,8 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
int pointerFingerId = event.getPointerId(actionPointerIndex); int pointerFingerId = event.getPointerId(actionPointerIndex);
int action = (event.getAction() & MotionEvent.ACTION_MASK); /* API 8: event.getActionMasked(); */ int action = (event.getAction() & MotionEvent.ACTION_MASK); /* API 8: event.getActionMasked(); */
float x = event.getX(actionPointerIndex); float x = event.getX(actionPointerIndex) / mWidth;
float y = event.getY(actionPointerIndex); float y = event.getY(actionPointerIndex) / mHeight;
float p = event.getPressure(actionPointerIndex); float p = event.getPressure(actionPointerIndex);
if (action == MotionEvent.ACTION_MOVE && pointerCount > 1) { if (action == MotionEvent.ACTION_MOVE && pointerCount > 1) {
@ -577,8 +587,8 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
// changed since prev event. // changed since prev event.
for (int i = 0; i < pointerCount; i++) { for (int i = 0; i < pointerCount; i++) {
pointerFingerId = event.getPointerId(i); pointerFingerId = event.getPointerId(i);
x = event.getX(i); x = event.getX(i) / mWidth;
y = event.getY(i); y = event.getY(i) / mHeight;
p = event.getPressure(i); p = event.getPressure(i);
SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p); SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
} }

View file

@ -55,10 +55,10 @@ void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int actio
memset( &touch, 0, sizeof(touch) ); memset( &touch, 0, sizeof(touch) );
touch.id = touchDeviceId; touch.id = touchDeviceId;
touch.x_min = 0.0f; touch.x_min = 0.0f;
touch.x_max = (float)Android_ScreenWidth; touch.x_max = 1.0f;
touch.native_xres = touch.x_max - touch.x_min; touch.native_xres = touch.x_max - touch.x_min;
touch.y_min = 0.0f; touch.y_min = 0.0f;
touch.y_max = (float)Android_ScreenHeight; touch.y_max = 1.0f;
touch.native_yres = touch.y_max - touch.y_min; touch.native_yres = touch.y_max - touch.y_min;
touch.pressure_min = 0.0f; touch.pressure_min = 0.0f;
touch.pressure_max = 1.0f; touch.pressure_max = 1.0f;