vtm/src/org/oscim/layers/overlay/BuildingOverlay.java
2013-10-09 01:56:02 +02:00

151 lines
3.7 KiB
Java

/*
* 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.layers.overlay;
import org.oscim.core.MapPosition;
import org.oscim.renderer.overlays.ExtrusionOverlay;
import org.oscim.view.MapView;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.MotionEvent;
/**
* @author Hannes Janetzek
*/
public class BuildingOverlay extends Overlay {
private final static String TAG = BuildingOverlay.class.getName();
final ExtrusionOverlay mExtLayer;
public BuildingOverlay(MapView mapView, org.oscim.layers.tile.TileRenderLayer tileRenderLayer) {
super(mapView);
mExtLayer = new ExtrusionOverlay(mapView, tileRenderLayer);
mLayer = mExtLayer;
}
private int multi;
private final float mFadeTime = 300;
private float mAlpha = 1;
private final static int MIN_ZOOM = 17;
@Override
public boolean onTouchEvent(MotionEvent e) {
int action = e.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_POINTER_DOWN) {
multi++;
} else if (action == MotionEvent.ACTION_POINTER_UP) {
multi--;
if (!mActive && mAlpha > 0) {
// finish hiding
//Log.d(TAG, "add multi hide timer " + mAlpha);
addShowTimer(mFadeTime * mAlpha, false);
}
} else if (action == MotionEvent.ACTION_CANCEL) {
multi = 0;
Log.d(TAG, "cancel " + multi);
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
}
return false;
}
private boolean mActive = false;
@Override
public void onUpdate(MapPosition mapPosition, boolean changed) {
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();
}
}