From 8cefea550e340f83e97f9fbcd6ba8fac6387080d Mon Sep 17 00:00:00 2001 From: "kim..ng" Date: Sun, 29 May 2011 20:55:28 +0000 Subject: [PATCH] Android: Fixed race-condition on GLSurfaceView destroy, fixed MouseEvents for flyCam rotation bug git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7536 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../android/com/jme3/app/AndroidHarness.java | 2 +- .../jme3/app/android/AndroidApplication.java | 5 +++ .../com/jme3/input/android/AndroidInput.java | 42 +++++++++++-------- .../com/jme3/system/android/OGLESContext.java | 8 ++-- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/engine/src/android/com/jme3/app/AndroidHarness.java b/engine/src/android/com/jme3/app/AndroidHarness.java index 67af93b60..9b88c15ff 100644 --- a/engine/src/android/com/jme3/app/AndroidHarness.java +++ b/engine/src/android/com/jme3/app/AndroidHarness.java @@ -110,7 +110,7 @@ public class AndroidHarness extends Activity implements DialogInterface.OnClickL @Override protected void onDestroy(){ - super.onDestroy(); + super.onDestroy(); app.stop(); logger.info("onDestroy"); } diff --git a/engine/src/android/com/jme3/app/android/AndroidApplication.java b/engine/src/android/com/jme3/app/android/AndroidApplication.java index 770345d6b..c2e99e1ff 100644 --- a/engine/src/android/com/jme3/app/android/AndroidApplication.java +++ b/engine/src/android/com/jme3/app/android/AndroidApplication.java @@ -33,6 +33,7 @@ package com.jme3.app.android; import java.nio.CharBuffer; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.logging.Logger; import android.app.Activity; import android.app.AlertDialog; @@ -62,6 +63,7 @@ import com.jme3.app.AndroidHarness; @Deprecated public abstract class AndroidApplication extends Application implements DialogInterface.OnClickListener { + protected final static Logger logger = Logger.getLogger(AndroidApplication.class.getName()); protected Node rootNode = new Node("Root Node"); protected Node guiNode = new Node("Gui Node"); @@ -258,6 +260,9 @@ public abstract class AndroidApplication extends Application implements DialogIn } } final String sTrace = s; + + logger.severe(t != null ? t.toString() : "Failed"); + logger.severe((errorMsg != null ? errorMsg + ": " : "") + sTrace); activity.runOnUiThread(new Runnable() { @Override public void run() diff --git a/engine/src/android/com/jme3/input/android/AndroidInput.java b/engine/src/android/com/jme3/input/android/AndroidInput.java index 54d7e5063..0df8313f2 100644 --- a/engine/src/android/com/jme3/input/android/AndroidInput.java +++ b/engine/src/android/com/jme3/input/android/AndroidInput.java @@ -30,7 +30,8 @@ public class AndroidInput extends GLSurfaceView implements KeyInput, MouseInput, private AndroidTouchInputListener listenerTouch = null; private ScaleGestureDetector scaledetector; private GestureDetector detector; - private Vector2f lastPos = new Vector2f(); + private int lastX; + private int lastY; private boolean dragging = false; private List currentEvents = new ArrayList(); @@ -175,22 +176,24 @@ public class AndroidInput extends GLSurfaceView implements KeyInput, MouseInput, this.detector.onTouchEvent(event); this.scaledetector.onTouchEvent(event); + int newX = getWidth() - (int) event.getX(); + int newY = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: - - // Store current pos - lastPos.set(event.getX(),event.getY()); - + if (FIRE_MOUSE_EVENTS) { // Handle mouse events - btn = new MouseButtonEvent(0, true, (int)lastPos.getX(), (int)lastPos.getY()); + btn = new MouseButtonEvent(0, true, newX, newY); btn.setTime(event.getEventTime()); processEvent(btn); } - + // Store current pos + lastX = -1; + lastY = -1; + // Handle gesture events touch = new TouchEvent(TouchEvent.Type.GRABBED, TouchEvent.Operation.NOP,event.getX(),event.getY(),0,0,null); processEvent(touch); @@ -203,14 +206,18 @@ public class AndroidInput extends GLSurfaceView implements KeyInput, MouseInput, if (FIRE_MOUSE_EVENTS) { // Handle mouse events - btn = new MouseButtonEvent(0, false, (int)event.getX(), (int)event.getY()); + btn = new MouseButtonEvent(0, false, newX, newY); btn.setTime(event.getEventTime()); processEvent(btn); - } + } + // Store current pos + lastX = -1; + lastY = -1; + // Handle gesture events if(dragging) { - touch = new TouchEvent(TouchEvent.Type.DRAGGED, TouchEvent.Operation.STOPPED,event.getX(),event.getY(),event.getX()-lastPos.getX(),event.getY()-lastPos.getY(),null); + touch = new TouchEvent(TouchEvent.Type.DRAGGED, TouchEvent.Operation.STOPPED,event.getX(),event.getY(),event.getX()-lastX,event.getY()-lastY,null); processEvent(touch); } touch = new TouchEvent(TouchEvent.Type.RELEASED, TouchEvent.Operation.NOP,event.getX(),event.getY(),0,0,null); @@ -222,22 +229,21 @@ public class AndroidInput extends GLSurfaceView implements KeyInput, MouseInput, if(!scaledetector.isInProgress()) { if(!dragging) - touch = new TouchEvent(TouchEvent.Type.DRAGGED, TouchEvent.Operation.STARTED,event.getX(),event.getY(),event.getX()-lastPos.getX(),event.getY()-lastPos.getY(),null); + touch = new TouchEvent(TouchEvent.Type.DRAGGED, TouchEvent.Operation.STARTED,event.getX(),event.getY(),event.getX()-lastX,event.getY()-lastY,null); else - touch = new TouchEvent(TouchEvent.Type.DRAGGED, TouchEvent.Operation.RUNNING,event.getX(),event.getY(),event.getX()-lastPos.getX(),event.getY()-lastPos.getY(),null); + touch = new TouchEvent(TouchEvent.Type.DRAGGED, TouchEvent.Operation.RUNNING,event.getX(),event.getY(),event.getX()-lastX,event.getY()-lastY,null); processEvent(touch); dragging=true; } if (FIRE_MOUSE_EVENTS) { - int newX = getWidth() - (int) event.getX(); - int newY = (int) event.getY(); + int dx; int dy; - if (lastPos.getX() != -1){ - dx = newX - (int)lastPos.getX(); - dy = newY - (int)lastPos.getY(); + if (lastX != -1){ + dx = newX - lastX; + dy = newY - lastY; }else{ dx = 0; dy = 0; @@ -246,6 +252,8 @@ public class AndroidInput extends GLSurfaceView implements KeyInput, MouseInput, mot.setTime(event.getEventTime()); processEvent(mot); } + lastX = newX; + lastY = newY; bWasHandled = true; break; diff --git a/engine/src/android/com/jme3/system/android/OGLESContext.java b/engine/src/android/com/jme3/system/android/OGLESContext.java index 069300148..8c218a01c 100644 --- a/engine/src/android/com/jme3/system/android/OGLESContext.java +++ b/engine/src/android/com/jme3/system/android/OGLESContext.java @@ -295,8 +295,6 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer public void onDrawFrame(GL10 gl) { - if (!created.get()) - throw new IllegalStateException("onDrawFrame without create"); if (needClose.get()) { @@ -306,6 +304,10 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer if (renderable.get()) { + + if (!created.get()) + throw new IllegalStateException("onDrawFrame without create"); + milliStart = System.currentTimeMillis(); @@ -369,7 +371,7 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer public void destroy() { - destroy(false); + destroy(true); } protected void waitFor(boolean createdVal)