refactor: make MapView abstract, remove MapRenderCallback

This commit is contained in:
Hannes Janetzek 2013-07-26 00:56:17 +02:00
parent 3ab9751128
commit 136db862d2
5 changed files with 124 additions and 203 deletions

View File

@ -22,7 +22,6 @@ import org.oscim.backend.CanvasAdapter;
import org.oscim.backend.GLAdapter; import org.oscim.backend.GLAdapter;
import org.oscim.backend.Log; import org.oscim.backend.Log;
import org.oscim.core.Tile; import org.oscim.core.Tile;
import org.oscim.view.MapRenderCallback;
import org.oscim.view.MapView; import org.oscim.view.MapView;
import android.content.Context; import android.content.Context;
@ -36,7 +35,7 @@ import com.badlogic.gdx.backends.android.AndroidGL20;
* A MapView shows a map on the display of the device. It handles all user input * A MapView shows a map on the display of the device. It handles all user input
* and touch gestures to move and zoom the map. * and touch gestures to move and zoom the map.
*/ */
public class AndroidMapView extends RelativeLayout implements MapRenderCallback { public class AndroidMapView extends RelativeLayout {
final static String TAG = AndroidMapView.class.getName(); final static String TAG = AndroidMapView.class.getName();
@ -47,16 +46,18 @@ public class AndroidMapView extends RelativeLayout implements MapRenderCallback
public boolean mCompassEnabled = false; public boolean mCompassEnabled = false;
public boolean enablePagedFling = false; public boolean enablePagedFling = false;
private final GLView mGLView;
private final Compass mCompass; private final Compass mCompass;
private int mWidth; private int mWidth;
private int mHeight; private int mHeight;
private boolean mInitialized;
private final MapView mMapView; private final MapView mMapView;
final GLView mGLView;
boolean mPausing = false;
boolean mInitialized = false;
static { static {
System.loadLibrary("vtm-jni"); System.loadLibrary("vtm-jni");
//System.loadLibrary("tessellate"); //System.loadLibrary("tessellate");
@ -97,7 +98,6 @@ public class AndroidMapView extends RelativeLayout implements MapRenderCallback
AssetAdapter.g = new AndroidAssetAdapter(context); AssetAdapter.g = new AndroidAssetAdapter(context);
this.setWillNotDraw(true); this.setWillNotDraw(true);
DisplayMetrics metrics = getResources().getDisplayMetrics(); DisplayMetrics metrics = getResources().getDisplayMetrics();
@ -108,7 +108,63 @@ public class AndroidMapView extends RelativeLayout implements MapRenderCallback
MapActivity mapActivity = (MapActivity) context; MapActivity mapActivity = (MapActivity) context;
mMapView = new MapView(this); final AndroidMapView m = this;
mMapView = new MapView(){
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;
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;
}
}
};
mGLView = new GLView(context, mMapView); mGLView = new GLView(context, mMapView);
@ -122,8 +178,8 @@ public class AndroidMapView extends RelativeLayout implements MapRenderCallback
addView(mGLView, params); addView(mGLView, params);
clearMap(); mMapView.clearMap();
updateMap(false); mMapView.updateMap(false);
} }
public MapView getMap() { public MapView getMap() {
@ -135,7 +191,6 @@ public class AndroidMapView extends RelativeLayout implements MapRenderCallback
//mLayerManager.destroy(); //mLayerManager.destroy();
} }
private boolean mPausing = false;
void onPause() { void onPause() {
mPausing = true; mPausing = true;
@ -162,10 +217,9 @@ public class AndroidMapView extends RelativeLayout implements MapRenderCallback
mMotionEvent.wrap(motionEvent); mMotionEvent.wrap(motionEvent);
//return mMapView.handleMotionEvent(mMotionEvent);
return mMapView.getLayerManager().handleMotionEvent(mMotionEvent); return mMapView.getLayerManager().handleMotionEvent(mMotionEvent);
} }
// synchronized ??? // synchronized ???
@Override @Override
protected void onSizeChanged(int width, int height, protected void onSizeChanged(int width, int height,
@ -183,70 +237,6 @@ public class AndroidMapView extends RelativeLayout implements MapRenderCallback
mMapView.getMapViewPosition().setViewport(width, height); mMapView.getMapViewPosition().setViewport(width, height);
} }
/* private */boolean mWaitRedraw;
private final Runnable mRedrawRequest = new Runnable() {
@Override
public void run() {
mWaitRedraw = false;
redrawMapInternal(false);
}
};
/**
* Request to redraw the map when a global state like position,
* datasource or theme has changed. This will trigger a call
* to onUpdate() for all Layers.
*
* @param requestRender
* also request to draw a frame
*/
@Override
public void updateMap(boolean requestRender) {
if (requestRender && !mClearMap && !mPausing && mInitialized)
mGLView.requestRender();
if (!mWaitRedraw) {
mWaitRedraw = true;
post(mRedrawRequest);
}
}
private boolean mClearMap;
public void clearMap() {
mClearMap = true;
}
/**
* Request to render a frame. Use this for animations.
*/
@Override
public void renderMap() {
if (mClearMap)
updateMap(false);
else
mGLView.requestRender();
}
/**
* Update all Layers on Main thread.
*
* @param forceRedraw also render frame
* FIXME (does nothing atm)
*/
void redrawMapInternal(boolean forceRedraw) {
if (forceRedraw && !mClearMap)
mGLView.requestRender();
mMapView.updateLayers();
if (mClearMap) {
mGLView.requestRender();
mClearMap = false;
}
}
public void enableRotation(boolean enable) { public void enableRotation(boolean enable) {
mRotationEnabled = enable; mRotationEnabled = enable;

View File

@ -60,7 +60,6 @@ public abstract class MapActivity extends Activity {
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
mMapView.onPause();
Editor editor = getSharedPreferences(PREFERENCES_FILE, MODE_PRIVATE).edit(); Editor editor = getSharedPreferences(PREFERENCES_FILE, MODE_PRIVATE).edit();
editor.clear(); editor.clear();
@ -84,13 +83,11 @@ public abstract class MapActivity extends Activity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
mMapView.onResume();
} }
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
mMapView.onStop();
} }
/** /**

View File

@ -10,9 +10,7 @@ import org.oscim.layers.tile.vector.MapTileLayer;
import org.oscim.renderer.GLRenderer; import org.oscim.renderer.GLRenderer;
import org.oscim.renderer.GLState; import org.oscim.renderer.GLState;
import org.oscim.theme.InternalRenderTheme; import org.oscim.theme.InternalRenderTheme;
import org.oscim.theme.ThemeLoader;
import org.oscim.tilesource.TileSource; import org.oscim.tilesource.TileSource;
import org.oscim.view.MapRenderCallback;
import org.oscim.view.MapView; import org.oscim.view.MapView;
import org.oscim.view.MapViewPosition; import org.oscim.view.MapViewPosition;
@ -27,16 +25,47 @@ import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener; import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
public class GdxMap implements ApplicationListener, MapRenderCallback { public class GdxMap implements ApplicationListener {
protected final MapView mMapView; protected final MapView mMapView;
private final GLRenderer mMapRenderer; private final GLRenderer mMapRenderer;
private final TileSource mTileSource; private final TileSource mTileSource;
boolean mRenderRequest;
public GdxMap(TileSource tileSource) { public GdxMap(TileSource tileSource) {
AssetAdapter.g = new GdxAssetAdapter(); AssetAdapter.g = new GdxAssetAdapter();
mTileSource = tileSource; mTileSource = tileSource;
mMapView = new MapView(this);
mMapView = new MapView() {
@Override
public int getWidth() {
return mWidth;
}
@Override
public int getHeight() {
return mHeight;
}
@Override
public void updateMap(boolean forceRender) {
if (!mWaitRedraw) {
mWaitRedraw = true;
Gdx.app.postRunnable(mRedrawRequest);
}
}
@Override
public void render() {
mRenderRequest = true;
if (mClearMap)
updateMap(false);
else
Gdx.graphics.requestRendering();
}
};
mMapRenderer = new GLRenderer(mMapView); mMapRenderer = new GLRenderer(mMapView);
} }
@ -45,6 +74,8 @@ public class GdxMap implements ApplicationListener, MapRenderCallback {
// BitmapFont font; // BitmapFont font;
MapTileLayer mMapLayer; MapTileLayer mMapLayer;
int mHeight, mWidth;
@Override @Override
public void create() { public void create() {
@ -142,8 +173,7 @@ public class GdxMap implements ApplicationListener, MapRenderCallback {
mMapView.getMapViewPosition().setViewport(w, h); mMapView.getMapViewPosition().setViewport(w, h);
mMapRenderer.onSurfaceChanged(w, h); mMapRenderer.onSurfaceChanged(w, h);
mMapView.render();
renderMap();
} }
@Override @Override
@ -163,22 +193,11 @@ public class GdxMap implements ApplicationListener, MapRenderCallback {
void redrawMapInternal(boolean forceRedraw) { void redrawMapInternal(boolean forceRedraw) {
GLState.blend(false); GLState.blend(false);
GLState.test(false, false); GLState.test(false, false);
// if (forceRedraw && !mClearMap)
// Gdx.graphics.requestRendering();
mMapView.updateLayers(); mMapView.updateLayers();
// if (mClearMap) {
mRenderRequest = true; mRenderRequest = true;
Gdx.graphics.requestRendering(); Gdx.graphics.requestRendering();
mClearMap = false;
// }
}
private boolean mClearMap;
public void clearMap() {
mClearMap = true;
} }
/* private */boolean mWaitRedraw; /* private */boolean mWaitRedraw;
@ -190,39 +209,6 @@ public class GdxMap implements ApplicationListener, MapRenderCallback {
} }
}; };
@Override
public void updateMap(boolean forceRender) {
if (!mWaitRedraw) {
mWaitRedraw = true;
Gdx.app.postRunnable(mRedrawRequest);
}
}
private boolean mRenderRequest;
@Override
public void renderMap() {
mRenderRequest = true;
if (mClearMap)
updateMap(false);
else
Gdx.graphics.requestRendering();
}
int mHeight, mWidth;
@Override
public int getWidth() {
return mWidth;
}
@Override
public int getHeight() {
return mHeight;
}
class TouchHandler implements InputProcessor { class TouchHandler implements InputProcessor {
private MapViewPosition mMapPosition; private MapViewPosition mMapPosition;
@ -266,11 +252,6 @@ public class GdxMap implements ApplicationListener, MapRenderCallback {
mMapView.updateMap(true); mMapView.updateMap(true);
break; break;
// case Input.Keys.R:
// mMapLayer.reloadTheme();
// mMapView.updateMap(false);
// break;
case Input.Keys.D: case Input.Keys.D:
mMapView.setTheme(InternalRenderTheme.DEFAULT); mMapView.setTheme(InternalRenderTheme.DEFAULT);
mMapView.updateMap(false); mMapView.updateMap(false);
@ -339,19 +320,19 @@ public class GdxMap implements ApplicationListener, MapRenderCallback {
changed = true; changed = true;
} }
// if (!(mActiveRotate || mActiveTilt || mActiveScale)) { // if (!(mActiveRotate || mActiveTilt || mActiveScale)) {
// int dx = screenX - mPosX; // int dx = screenX - mPosX;
// int dy = screenY - mPosY; // int dy = screenY - mPosY;
// if (Math.abs(dx) > 0 || Math.abs(dy) > 0) { // if (Math.abs(dx) > 0 || Math.abs(dy) > 0) {
// mMapPosition.moveMap(dx, dy); // mMapPosition.moveMap(dx, dy);
// mPosX = screenX; // mPosX = screenX;
// mPosY = screenY; // mPosY = screenY;
// changed = true; // changed = true;
// } // }
// } // }
if (changed) { if (changed) {
updateMap(true); mMapView.updateMap(true);
} }
return true; return true;
} }
@ -375,7 +356,7 @@ public class GdxMap implements ApplicationListener, MapRenderCallback {
mMapPosition.animateZoom(1.1f, 150); mMapPosition.animateZoom(1.1f, 150);
//mMapPosition.scaleMap(1.1f, fx, fy); //mMapPosition.scaleMap(1.1f, fx, fy);
} }
updateMap(false); mMapView.updateMap(false);
return true; return true;
} }
@ -454,7 +435,7 @@ public class GdxMap implements ApplicationListener, MapRenderCallback {
return true; return true;
mMapPosition.moveMap(deltaX, deltaY); mMapPosition.moveMap(deltaX, deltaY);
updateMap(true); mMapView.updateMap(true);
return false; return false;
} }

View File

@ -1,23 +0,0 @@
/*
* Copyright 2013 Hannes Janetzek
*
* 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.view;
public interface MapRenderCallback {
void updateMap(boolean forceRender);
void renderMap();
int getWidth();
int getHeight();
}

View File

@ -31,9 +31,8 @@ import org.oscim.theme.IRenderTheme;
import org.oscim.theme.InternalRenderTheme; import org.oscim.theme.InternalRenderTheme;
import org.oscim.theme.ThemeLoader; import org.oscim.theme.ThemeLoader;
import org.oscim.tilesource.TileSource; import org.oscim.tilesource.TileSource;
import org.oscim.utils.GlUtils;
public class MapView { public abstract class MapView {
private static final String TAG = MapView.class.getName(); private static final String TAG = MapView.class.getName();
@ -43,10 +42,8 @@ public class MapView {
private final MapPosition mMapPosition; private final MapPosition mMapPosition;
private DebugSettings mDebugSettings; private DebugSettings mDebugSettings;
private final MapRenderCallback mMapRenderCallback;
public MapView(MapRenderCallback mapRenderCallback) { public MapView() {
mMapRenderCallback = mapRenderCallback;
mMapViewPosition = new MapViewPosition(this); mMapViewPosition = new MapViewPosition(this);
mMapPosition = new MapPosition(); mMapPosition = new MapPosition();
@ -111,11 +108,13 @@ public class MapView {
* *
* @param forceRedraw pass true to render next frame * @param forceRedraw pass true to render next frame
*/ */
public void updateMap(boolean forceRedraw) { public abstract void updateMap(boolean forceRedraw);
mMapRenderCallback.updateMap(forceRedraw);
}
boolean mClearMap;
public abstract int getWidth();
public abstract int getHeight();
protected boolean mClearMap;
/** /**
* Request to clear all layers before rendering next frame * Request to clear all layers before rendering next frame
@ -126,11 +125,9 @@ public class MapView {
/** /**
* Request to render a frame. Request will be handled on main * Request to render a frame. Request will be handled on main
* thread. * thread. Use this for animations.
*/ */
public void render() { public abstract void render();
mMapRenderCallback.renderMap();
}
/** /**
* Do not call directly! This function is run on main-loop * Do not call directly! This function is run on main-loop
@ -200,28 +197,7 @@ public class MapView {
return mMapViewPosition.getViewBox(); return mMapViewPosition.getViewBox();
} }
public void onPause() {
// TODO Auto-generated method stub
}
public void onResume() {
// TODO Auto-generated method stub
}
public void onStop() {
// TODO Auto-generated method stub
}
public int getWidth() {
return mMapRenderCallback.getWidth();
}
public int getHeight() {
return mMapRenderCallback.getHeight();
}
} }