add pluggable gesture detection

- extract inner Map.Layers class
- extract inner MapView.Map class -> AndroidMap
This commit is contained in:
Hannes Janetzek
2013-12-04 13:09:23 +01:00
parent 68bfa27a99
commit ba52bfddbe
11 changed files with 465 additions and 261 deletions

View File

@@ -0,0 +1,109 @@
/*
* Copyright 2013
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.android;
import org.oscim.map.Map;
import android.widget.RelativeLayout.LayoutParams;
public class AndroidMap extends Map {
private final MapView mMapView;
boolean mWaitRedraw;
final GLView mGLView;
boolean mPausing = false;
private final Runnable mRedrawRequest = new Runnable() {
@Override
public void run() {
mWaitRedraw = false;
redrawMapInternal(false);
}
};
public AndroidMap(MapView mapView) {
super();
mMapView = mapView;
mGLView = new GLView(mapView.getContext(), this);
LayoutParams params =
new LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT);
mapView.addView(mGLView, params);
//mGestureDetector =
}
@Override
public int getWidth() {
return mMapView.getWidth();
}
@Override
public int getHeight() {
return mMapView.getHeight();
}
@Override
public void updateMap(boolean requestRender) {
if (requestRender && !mClearMap && !mPausing) // && mInitialized)
mGLView.requestRender();
if (!mWaitRedraw) {
mWaitRedraw = true;
mMapView.post(mRedrawRequest);
}
}
@Override
public void render() {
if (mClearMap)
updateMap(false);
else
mGLView.requestRender();
}
void redrawMapInternal(boolean forceRedraw) {
boolean clear = mClearMap;
if (forceRedraw && !clear)
mGLView.requestRender();
updateLayers();
if (clear) {
mGLView.requestRender();
mClearMap = false;
}
}
@Override
public boolean post(Runnable runnable) {
return mMapView.post(runnable);
}
@Override
public boolean postDelayed(Runnable action, long delay) {
return mMapView.postDelayed(action, delay);
}
public void pause(boolean pause) {
mPausing = pause;
}
}

View File

@@ -22,6 +22,7 @@ import org.oscim.backend.AssetAdapter;
import org.oscim.backend.CanvasAdapter;
import org.oscim.backend.GLAdapter;
import org.oscim.core.Tile;
import org.oscim.event.Gesture;
import org.oscim.map.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -29,6 +30,9 @@ import org.slf4j.LoggerFactory;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
@@ -40,6 +44,10 @@ public class MapView extends RelativeLayout {
static final Logger log = LoggerFactory.getLogger(MapView.class);
static {
System.loadLibrary("vtm-jni");
}
public static final boolean debugFrameTime = false;
public static final boolean testRegionZoom = false;
@@ -48,21 +56,15 @@ public class MapView extends RelativeLayout {
public boolean enablePagedFling = false;
private final Compass mCompass;
private final GestureDetector mGestureDetector;
private int mWidth;
private int mHeight;
private final Map mMap;
final AndroidMap mMap;
final GLView mGLView;
boolean mPausing = false;
boolean mInitialized = false;
static {
System.loadLibrary("vtm-jni");
//System.loadLibrary("tessellate");
}
/**
* @param context
* the enclosing MapActivity instance.
@@ -87,12 +89,9 @@ public class MapView extends RelativeLayout {
public MapView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
if (!(context instanceof MapActivity)) {
throw new IllegalArgumentException(
"context is not an instance of MapActivity");
}
if (!(context instanceof MapActivity))
throw new IllegalArgumentException("context is not an instance of MapActivity");
//Log.logger = new AndroidLog();
CanvasAdapter.g = AndroidGraphics.INSTANCE;
AssetAdapter.g = new AndroidAssetAdapter(context);
GLAdapter.g = new AndroidGL();
@@ -109,87 +108,64 @@ public class MapView extends RelativeLayout {
MapActivity mapActivity = (MapActivity) context;
final MapView m = this;
mMap = new AndroidMap(this);
mMap = new Map() {
boolean mWaitRedraw;
private final Runnable mRedrawRequest = new Runnable() {
@Override
public void run() {
mWaitRedraw = false;
redrawMapInternal(false);
}
};
@Override
public int getWidth() {
return m.getWidth();
}
@Override
public int getHeight() {
return m.getHeight();
}
@Override
public void updateMap(boolean requestRender) {
if (requestRender && !mClearMap && !mPausing && mInitialized)
mGLView.requestRender();
if (!mWaitRedraw) {
mWaitRedraw = true;
getView().post(mRedrawRequest);
}
}
@Override
public void render() {
if (mClearMap)
updateMap(false);
else
mGLView.requestRender();
}
void redrawMapInternal(boolean forceRedraw) {
boolean clear = mClearMap;
if (forceRedraw && !clear)
mGLView.requestRender();
updateLayers();
if (clear) {
mGLView.requestRender();
mClearMap = false;
}
}
@Override
public boolean post(Runnable runnable) {
return getView().post(runnable);
}
@Override
public boolean postDelayed(Runnable action, long delay) {
return getView().postDelayed(action, delay);
}
};
mGLView = new GLView(context, mMap);
mCompass = new Compass(mapActivity, mMap);
mapActivity.registerMapView(mMap);
LayoutParams params = new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT);
addView(mGLView, params);
mMap.clearMap();
mMap.updateMap(false);
mGestureDetector = new GestureDetector(context, new OnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return mMap.handleGesture(Gesture.TAP, mMotionEvent.wrap(e));
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
mMap.handleGesture(Gesture.LONG_PRESS, mMotionEvent.wrap(e));
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return mMap.handleGesture(Gesture.PRESS, mMotionEvent.wrap(e));
}
});
//mGestureDetector.setOnDoubleTapListener(new OnDoubleTapListener() {
//
// @Override
// public boolean onSingleTapConfirmed(MotionEvent e) {
// return false;
// }
//
// @Override
// public boolean onDoubleTapEvent(MotionEvent e) {
// return false;
// }
//
// @Override
// public boolean onDoubleTap(MotionEvent e) {
// return false;
// }
//});
}
View getView() {
@@ -206,7 +182,7 @@ public class MapView extends RelativeLayout {
}
void onPause() {
mPausing = true;
mMap.pause(true);
if (this.mCompassEnabled)
mCompass.disable();
@@ -217,10 +193,10 @@ public class MapView extends RelativeLayout {
if (this.mCompassEnabled)
mCompass.enable();
mPausing = false;
mMap.pause(false);
}
AndroidMotionEvent mMotionEvent = new AndroidMotionEvent();
final AndroidMotionEvent mMotionEvent = new AndroidMotionEvent();
@Override
public boolean onTouchEvent(android.view.MotionEvent motionEvent) {
@@ -228,6 +204,9 @@ public class MapView extends RelativeLayout {
if (!isClickable())
return false;
if (mGestureDetector.onTouchEvent(motionEvent))
return true;
mMotionEvent.wrap(motionEvent);
mMap.handleMotionEvent(mMotionEvent);

View File

@@ -20,8 +20,9 @@ public class AndroidMotionEvent extends MotionEvent {
android.view.MotionEvent mEvent;
public void wrap(android.view.MotionEvent e) {
public MotionEvent wrap(android.view.MotionEvent e) {
mEvent = e;
return this;
}
@Override