Use SkTouchGesture to handle pinch events in Android SampleApp.
authorScroggo <Scroggo@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 17 Jun 2011 12:46:17 +0000 (12:46 +0000)
committerScroggo <Scroggo@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 17 Jun 2011 12:46:17 +0000 (12:46 +0000)
http://codereview.appspot.com/4631043/

git-svn-id: http://skia.googlecode.com/svn/trunk@1624 2bbb7eff-a529-9590-31e7-b0007b416f81

android_sample/SampleApp/jni/sample-jni.cpp
android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java
android_sample/SampleApp/src/com/skia/sampleapp/SampleView.java
samplecode/SampleApp.cpp
samplecode/SampleApp.h

index e9772dd..4ba3b9d 100644 (file)
@@ -132,7 +132,7 @@ JNIEXPORT bool JNICALL Java_com_skia_sampleapp_SampleApp_handleKeyDown(
 JNIEXPORT bool JNICALL Java_com_skia_sampleapp_SampleApp_handleKeyUp(
         JNIEnv* env, jobject thiz, jint keyCode);
 JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_handleClick(
-        JNIEnv* env, jobject thiz, jint x, jint y, jint state);
+        JNIEnv* env, jobject thiz, jint owner, jfloat x, jfloat y, jint state);
 JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_createOSWindow(
         JNIEnv* env, jobject thiz, jobject jsampleView);
 JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_setZoomCenter(
@@ -168,7 +168,7 @@ JNIEXPORT bool JNICALL Java_com_skia_sampleapp_SampleApp_handleKeyUp(JNIEnv* env
 }
 
 JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_handleClick(JNIEnv* env,
-        jobject thiz, jint x, jint y, jint jstate)
+        jobject thiz, jint owner, jfloat x, jfloat y, jint jstate)
 {
     SkView::Click::State state;
     switch(jstate) {
@@ -186,7 +186,7 @@ JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_handleClick(JNIEnv* env
             SkDebugf("motion event ignored\n");
             return;
     }
-    gWindow->handleClick(x, y, state);
+    gWindow->handleTouch(owner, x, y, state);
 }
 
 JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_updateSize(JNIEnv* env,
index b116010..370e143 100644 (file)
@@ -208,11 +208,9 @@ public class SampleApp extends Activity
     // Currently depends on init having already been called.
     native void createOSWindow(SampleView view);
     native void updateSize(int w, int h);
-    native void handleClick(int x, int y, int state);
+    native void handleClick(int owner, float x, float y, int state);
     native boolean handleKeyDown(int key, int uni);
     native boolean handleKeyUp(int key);
-    native void zoom(float factor);
-    native void setZoomCenter(float x, float y);
     native void nextSample(boolean previous);
     native void toggleRendering();
     native void toggleSlideshow();
index 5ea36a8..ab4e5c0 100644 (file)
@@ -23,19 +23,15 @@ import android.opengl.GLSurfaceView;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
-import android.util.AttributeSet;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
-import android.view.ScaleGestureDetector;
-import android.view.ScaleGestureDetector.OnScaleGestureListener;
 
 import javax.microedition.khronos.egl.EGLConfig;
 import javax.microedition.khronos.opengles.GL10;
 
-public class SampleView extends GLSurfaceView implements OnScaleGestureListener {
+public class SampleView extends GLSurfaceView {
 
     private final SampleApp mApp;
-    private ScaleGestureDetector mDetector;
 
     public SampleView(SampleApp app) {
         super(app);
@@ -44,7 +40,6 @@ public class SampleView extends GLSurfaceView implements OnScaleGestureListener
         setEGLConfigChooser(8,8,8,8,0,8);
         setRenderer(new SampleView.Renderer());
         setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
-        mDetector = new ScaleGestureDetector(app, this);
     }
 
     // Called by JNI
@@ -60,58 +55,31 @@ public class SampleView extends GLSurfaceView implements OnScaleGestureListener
 
     @Override
     public boolean onTouchEvent(MotionEvent event) {
-        mDetector.onTouchEvent(event);
-        if (mDetector.isInProgress()) {
-            return true;
-        }
-
-        final int x = (int) event.getX();
-        final int y = (int) event.getY();
-        final int action = event.getAction();
-        queueEvent(new Runnable() {
-           @Override
-           public void run() {
-               mApp.handleClick(x, y, action);
-           }
-        });
-
-        return true;
-    }
-
-    // ScaleGestureDetector.OnScaleGestureListener implementation
-    @Override
-    public boolean onScaleBegin(ScaleGestureDetector detector) {
-        final float x = detector.getFocusX();
-        final float y = detector.getFocusY();
-        queueEvent(new Runnable() {
-            @Override
-            public void run() {
-                mApp.setZoomCenter(x, y);
+        int count = event.getPointerCount();
+        for (int i = 0; i < count; i++) {
+            final float x = event.getX(i);
+            final float y = event.getY(i);
+            final int owner = event.getPointerId(i);
+            int action = event.getAction() & MotionEvent.ACTION_MASK;
+            switch (action) {
+                case MotionEvent.ACTION_POINTER_UP:
+                    action = MotionEvent.ACTION_UP;
+                    break;
+                case MotionEvent.ACTION_POINTER_DOWN:
+                    action = MotionEvent.ACTION_DOWN;
+                    break;
+                default:
+                    break;
             }
-        });
-        return true;
-    }
-
-    @Override
-    public boolean onScale(ScaleGestureDetector detector) {
-        if (detector.getScaleFactor() != 1) {
-            final float difference = detector.getCurrentSpan()
-                    - detector.getPreviousSpan();
+            final int finalAction = action;
             queueEvent(new Runnable() {
-                @Override
-                public void run() {
-                    mApp.zoom(difference * .01f);
-                }
+               @Override
+               public void run() {
+                   mApp.handleClick(owner, x, y, finalAction);
+               }
             });
-
-            return true;
         }
-        return false;
-    }
-
-    @Override
-    public void onScaleEnd(ScaleGestureDetector detector) {
-
+        return true;
     }
 
     private class Renderer implements GLSurfaceView.Renderer {
index f0c4e59..c003e4a 100644 (file)
@@ -1175,6 +1175,37 @@ SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
     return new GestureClick(this);
 }
 
+union IntPtr {
+    int     fInt;
+    void*   fPtr;
+};
+
+static void* int2ptr(int n) {
+    IntPtr data;
+    data.fInt = n;
+    return data.fPtr;
+}
+
+bool SampleWindow::handleTouch(int ownerId, float x, float y, SkView::Click::State state) {
+    void* click = int2ptr(ownerId);
+    switch(state) {
+        case SkView::Click::kDown_State:
+            fGesture.touchBegin(click, x, y);
+            break;
+        case SkView::Click::kMoved_State:
+            fGesture.touchMoved(click, x, y);
+            this->inval(NULL);
+            break;
+        case SkView::Click::kUp_State:
+            fGesture.touchEnd(click);
+            this->inval(NULL);
+            break;
+        default:
+            return false;
+    }
+    return true;
+}
+
 bool SampleWindow::onClick(Click* click) {
     if (GestureClick::IsGesture(click)) {
         float x = SkScalarToFloat(click->fCurr.fX);
index 37a3d09..25e3964 100644 (file)
@@ -58,6 +58,8 @@ public:
     void changeZoomLevel(float delta);
     bool nextSample();
     bool previousSample();
+    bool handleTouch(int ownerId, float x, float y,
+            SkView::Click::State state);
 
 protected:
     virtual void onDraw(SkCanvas* canvas);