Fixes bug #1506, Changing orientation on the Android device throws off touch scaling
authorGabriel Jacobo <gabomdq@gmail.com>
Fri, 24 Aug 2012 13:10:29 -0300
changeset 64002ed5671bc5e0
parent 6399 3fb0c82693af
child 6401 a4bd9821a796
Fixes bug #1506, Changing orientation on the Android device throws off touch scaling
android-project/src/org/libsdl/app/SDLActivity.java
src/video/android/SDL_androidtouch.c
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Fri Aug 24 11:56:21 2012 -0300
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Fri Aug 24 13:10:29 2012 -0300
     1.3 @@ -449,6 +449,9 @@
     1.4      // Sensors
     1.5      private static SensorManager mSensorManager;
     1.6  
     1.7 +    // Keep track of the surface size to normalize touch events
     1.8 +    private static float mWidth, mHeight;
     1.9 +
    1.10      // Startup    
    1.11      public SDLSurface(Context context) {
    1.12          super(context);
    1.13 @@ -460,7 +463,11 @@
    1.14          setOnKeyListener(this); 
    1.15          setOnTouchListener(this);   
    1.16  
    1.17 -        mSensorManager = (SensorManager)context.getSystemService("sensor");  
    1.18 +        mSensorManager = (SensorManager)context.getSystemService("sensor");
    1.19 +
    1.20 +        // Some arbitrary defaults to avoid a potential division by zero
    1.21 +        mWidth = 1.0f;
    1.22 +        mHeight = 1.0f;
    1.23      }
    1.24  
    1.25      // Called when we have a valid drawing surface
    1.26 @@ -529,6 +536,9 @@
    1.27              Log.v("SDL", "pixel format unknown " + format);
    1.28              break;
    1.29          }
    1.30 +
    1.31 +        mWidth = (float) width;
    1.32 +        mHeight = (float) height;
    1.33          SDLActivity.onNativeResize(width, height, sdlFormat);
    1.34          Log.v("SDL", "Window size:" + width + "x"+height);
    1.35  
    1.36 @@ -568,8 +578,8 @@
    1.37               int pointerFingerId = event.getPointerId(actionPointerIndex);
    1.38               int action = (event.getAction() & MotionEvent.ACTION_MASK); /* API 8: event.getActionMasked(); */
    1.39  
    1.40 -             float x = event.getX(actionPointerIndex);
    1.41 -             float y = event.getY(actionPointerIndex);
    1.42 +             float x = event.getX(actionPointerIndex) / mWidth;
    1.43 +             float y = event.getY(actionPointerIndex) / mHeight;
    1.44               float p = event.getPressure(actionPointerIndex);
    1.45  
    1.46               if (action == MotionEvent.ACTION_MOVE && pointerCount > 1) {
    1.47 @@ -577,8 +587,8 @@
    1.48                  // changed since prev event.
    1.49                  for (int i = 0; i < pointerCount; i++) {
    1.50                      pointerFingerId = event.getPointerId(i);
    1.51 -                    x = event.getX(i);
    1.52 -                    y = event.getY(i);
    1.53 +                    x = event.getX(i) / mWidth;
    1.54 +                    y = event.getY(i) / mHeight;
    1.55                      p = event.getPressure(i);
    1.56                      SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
    1.57                  }
     2.1 --- a/src/video/android/SDL_androidtouch.c	Fri Aug 24 11:56:21 2012 -0300
     2.2 +++ b/src/video/android/SDL_androidtouch.c	Fri Aug 24 13:10:29 2012 -0300
     2.3 @@ -55,10 +55,10 @@
     2.4          memset( &touch, 0, sizeof(touch) );
     2.5          touch.id = touchDeviceId;
     2.6          touch.x_min = 0.0f;
     2.7 -        touch.x_max = (float)Android_ScreenWidth;
     2.8 +        touch.x_max = 1.0f;
     2.9          touch.native_xres = touch.x_max - touch.x_min;
    2.10          touch.y_min = 0.0f;
    2.11 -        touch.y_max = (float)Android_ScreenHeight;
    2.12 +        touch.y_max = 1.0f;
    2.13          touch.native_yres = touch.y_max - touch.y_min;
    2.14          touch.pressure_min = 0.0f;
    2.15          touch.pressure_max = 1.0f;