building layer fade animation

This commit is contained in:
Hannes Janetzek 2013-07-10 12:04:18 +02:00
parent 98aa29104f
commit 2965dbcaeb
2 changed files with 75 additions and 109 deletions

View File

@ -14,9 +14,12 @@
*/ */
package org.oscim.layers.overlay; package org.oscim.layers.overlay;
import org.oscim.backend.Log;
import org.oscim.backend.input.MotionEvent; import org.oscim.backend.input.MotionEvent;
import org.oscim.core.MapPosition; import org.oscim.core.MapPosition;
import org.oscim.renderer.GLRenderer.Matrices;
import org.oscim.renderer.layers.ExtrusionRenderLayer; import org.oscim.renderer.layers.ExtrusionRenderLayer;
import org.oscim.utils.FastMath;
import org.oscim.view.MapView; import org.oscim.view.MapView;
//import android.os.CountDownTimer; //import android.os.CountDownTimer;
@ -31,119 +34,76 @@ public class BuildingOverlay extends Overlay {
public BuildingOverlay(MapView mapView, org.oscim.layers.tile.TileRenderLayer tileRenderLayer) { public BuildingOverlay(MapView mapView, org.oscim.layers.tile.TileRenderLayer tileRenderLayer) {
super(mapView); super(mapView);
mExtLayer = new ExtrusionRenderLayer(mapView, tileRenderLayer); mExtLayer = new ExtrusionRenderLayer(mapView, tileRenderLayer) {
private long mStartTime;
@Override
public void update(MapPosition pos, boolean changed, Matrices m) {
boolean show = pos.scale >= (1 << MIN_ZOOM);
if (show) {
if (mAlpha < 1) {
long now = System.currentTimeMillis();
if (mStartTime == 0) {
mStartTime = now;
}
float a = (now - mStartTime) / mFadeTime;
mAlpha = FastMath.clamp(a, 0, 1);
mMapView.render();
} else
mStartTime = 0;
} else {
if (mAlpha > 0) {
long now = System.currentTimeMillis();
if (mStartTime == 0) {
mStartTime = now + 100;
}
long diff = (now - mStartTime);
if (diff > 0) {
float a = 1 - diff / mFadeTime;
mAlpha = FastMath.clamp(a, 0, 1);
}
mMapView.render();
} else
mStartTime = 0;
}
//Log.d(TAG, show + " > " + mAlpha);
super.update(pos, changed, m);
}
};
mLayer = mExtLayer; mLayer = mExtLayer;
} }
private int multi; //private int multi;
private final float mFadeTime = 300; private final float mFadeTime = 500;
private float mAlpha = 1;
private final static int MIN_ZOOM = 17; private final static int MIN_ZOOM = 17;
@Override @Override
public boolean onTouchEvent(MotionEvent e) { public boolean onTouchEvent(MotionEvent e) {
// int action = e.getAction() & MotionEvent.ACTION_MASK; // int action = e.getAction() & MotionEvent.ACTION_MASK;
// if (action == MotionEvent.ACTION_POINTER_DOWN) { // if (action == MotionEvent.ACTION_POINTER_DOWN) {
// multi++; // multi++;
// } else if (action == MotionEvent.ACTION_POINTER_UP) { // } else if (action == MotionEvent.ACTION_POINTER_UP) {
// multi--; // multi--;
// if (!mActive && mAlpha > 0) { // if (!mActive && mAlpha > 0) {
// // finish hiding // // finish hiding
// //Log.d(TAG, "add multi hide timer " + mAlpha); // //Log.d(TAG, "add multi hide timer " + mAlpha);
// addShowTimer(mFadeTime * mAlpha, false); // addShowTimer(mFadeTime * mAlpha, false);
// } // }
// } else if (action == MotionEvent.ACTION_CANCEL) { // } else if (action == MotionEvent.ACTION_CANCEL) {
// multi = 0; // multi = 0;
// Log.d(TAG, "cancel " + multi); // Log.d(TAG, "cancel " + multi);
// if (mTimer != null) { // if (mTimer != null) {
// mTimer.cancel(); // mTimer.cancel();
// mTimer = null; // mTimer = null;
// } // }
// } // }
return false; return false;
} }
private boolean mActive = false;
@Override
public void onUpdate(MapPosition mapPosition, boolean changed, boolean clear) {
boolean show = mapPosition.scale >= (1 << MIN_ZOOM);
if (show && mActive)
return;
// if (show) {
// // start showing
// //Log.d(TAG, "add show timer " + mAlpha);
// addShowTimer(mFadeTime * (1 - mAlpha), true);
// } else if (mActive) {
// // indicate hiding
// if (multi > 0) {
// //Log.d(TAG, "add fade timer " + mAlpha);
// addFadeTimer(mFadeTime * mAlpha, false);
// } else {
// //Log.d(TAG, "add hide timer " + mAlpha);
// addShowTimer(mFadeTime * mAlpha, false);
// }
// }
mActive = show;
}
void fade(float duration, long tick, boolean dir, float max) {
float a;
if (dir)
a = (1 - max) + (1 - (tick / duration)) * max;
else
a = (1 - max) + (tick / duration) * max;
//Log.d(TAG, "fade " + dir + " " + tick + "\t" + a);
mAlpha = a;
mExtLayer.setAlpha(a);
mMapView.render();
}
// /* package */CountDownTimer mTimer;
//
// private void addFadeTimer(final float ms, final boolean dir) {
// if (mTimer != null)
// mTimer.cancel();
//
// mTimer = new CountDownTimer((long) ms, 16) {
// @Override
// public void onTick(long tick) {
// fade(ms, tick, dir, 0.2f);
// }
//
// @Override
// public void onFinish() {
// fade(ms, 0, dir, 0.2f);
// mTimer = null;
// }
// }.start();
// }
//
// private void addShowTimer(final float ms, final boolean dir) {
// final float d = mFadeTime;
// if (mTimer != null)
// mTimer.cancel();
//
// mTimer = new CountDownTimer((long) ms, 16) {
// @Override
// public void onTick(long tick) {
// fade(d, tick, dir, 1);
// }
//
// @Override
// public void onFinish() {
// fade(d, 0, dir, 1);
// mTimer = null;
//
// }
// }.start();
// }
} }

View File

@ -37,6 +37,8 @@ public class ExtrusionRenderLayer extends RenderLayer {
private final TileRenderLayer mTileLayer; private final TileRenderLayer mTileLayer;
protected float mAlpha = 1;
public ExtrusionRenderLayer(MapView mapView, public ExtrusionRenderLayer(MapView mapView,
org.oscim.layers.tile.TileRenderLayer tileRenderLayer) { org.oscim.layers.tile.TileRenderLayer tileRenderLayer) {
super(mapView); super(mapView);
@ -108,12 +110,21 @@ public class ExtrusionRenderLayer extends RenderLayer {
mTileLayer.getVisibleTiles(mTileSet); mTileLayer.getVisibleTiles(mTileSet);
MapTile[] tiles = mTileSet.tiles; MapTile[] tiles = mTileSet.tiles;
if (mTileSet.cnt == 0) {
mTileLayer.releaseTiles(mTileSet);
isReady = false;
return;
}
// keep a list of tiles available for rendering // keep a list of tiles available for rendering
if (mTiles == null || mTiles.length < mTileSet.cnt * 4) if (mTiles == null || mTiles.length < mTileSet.cnt * 4)
mTiles = new MapTile[mTileSet.cnt * 4]; mTiles = new MapTile[mTileSet.cnt * 4];
int zoom = tiles[0].zoomLevel;
ExtrusionLayer el; ExtrusionLayer el;
if (pos.zoomLevel >= 17) { if (zoom == 17) {
for (int i = 0; i < mTileSet.cnt; i++) { for (int i = 0; i < mTileSet.cnt; i++) {
el = getLayer(tiles[i]); el = getLayer(tiles[i]);
if (el == null) if (el == null)
@ -128,7 +139,7 @@ public class ExtrusionRenderLayer extends RenderLayer {
if (el.compiled) if (el.compiled)
mTiles[activeTiles++] = tiles[i]; mTiles[activeTiles++] = tiles[i];
} }
} else if (pos.zoomLevel == 16) { } else if (zoom == 16) {
// check if proxy children are ready // check if proxy children are ready
for (int i = 0; i < mTileSet.cnt; i++) { for (int i = 0; i < mTileSet.cnt; i++) {
MapTile t = tiles[i]; MapTile t = tiles[i];
@ -351,7 +362,6 @@ public class ExtrusionRenderLayer extends RenderLayer {
private final float _o = 55; private final float _o = 55;
private final float _s = 20; private final float _s = 20;
private final float _l = 8; private final float _l = 8;
private float mAlpha = 1;
private final float[] mColor = { private final float[] mColor = {
// roof color // roof color
_a * ((_r + _l + 1) / 255), _a * ((_r + _l + 1) / 255),
@ -432,8 +442,4 @@ public class ExtrusionRenderLayer extends RenderLayer {
+ " d = -d;" + " d = -d;"
+ " gl_FragColor = vec4(1.0 - d, 1.0 - d, 1.0 - d, 1.0 - d);" + " gl_FragColor = vec4(1.0 - d, 1.0 - d, 1.0 - d, 1.0 - d);"
+ "}"; + "}";
public void setAlpha(float a) {
mAlpha = a;
}
} }