Skip to content

Commit

Permalink
configure viewport extents
Browse files Browse the repository at this point in the history
  • Loading branch information
hjanetzek committed Jun 17, 2015
1 parent d492358 commit ff630fd
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 21 deletions.
10 changes: 4 additions & 6 deletions vtm/src/org/oscim/map/Animator.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void animateTo(long duration, GeoPoint geoPoint,
if (relative)
scale = mStartPos.scale * scale;

scale = clamp(scale, Viewport.MIN_SCALE, Viewport.MAX_SCALE);
scale = mMap.viewport().limitScale(scale);

mDeltaPos.set(longitudeToX(geoPoint.getLongitude()) - mStartPos.x,
latitudeToY(geoPoint.getLatitude()) - mStartPos.y,
Expand All @@ -131,15 +131,13 @@ public void animateTo(long duration, MapPosition pos) {

mMap.getMapPosition(mStartPos);

pos.scale = clamp(pos.scale,
Viewport.MIN_SCALE,
Viewport.MAX_SCALE);
pos.scale = mMap.viewport().limitScale(pos.scale);

mDeltaPos.set(pos.x - mStartPos.x,
pos.y - mStartPos.y,
pos.scale - mStartPos.scale,
pos.bearing - mStartPos.bearing,
clamp(pos.tilt, 0, Viewport.MAX_TILT) - mStartPos.tilt);
mMap.viewport().limitTilt(pos.tilt) - mStartPos.tilt);

animStart(duration, ANIM_MOVE | ANIM_SCALE | ANIM_ROTATE | ANIM_TILT);
}
Expand All @@ -156,7 +154,7 @@ public void animateZoom(long duration, double scaleBy,
scaleBy = mCurPos.scale * scaleBy;

mStartPos.copy(mCurPos);
scaleBy = clamp(scaleBy, Viewport.MIN_SCALE, Viewport.MAX_SCALE);
scaleBy = mMap.viewport().limitScale(scaleBy);

mDeltaPos.scale = scaleBy - mStartPos.scale;

Expand Down
22 changes: 13 additions & 9 deletions vtm/src/org/oscim/map/ViewController.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public boolean scaleMap(float scale, float pivotX, float pivotY) {

double newScale = mPos.scale * scale;

newScale = FastMath.clamp(newScale, MIN_SCALE, MAX_SCALE);
newScale = clamp(newScale, mMinScale, mMaxScale);

if (newScale == mPos.scale)
return false;
Expand Down Expand Up @@ -175,21 +175,21 @@ public void setRotation(double degree) {
degree += 360;

mPos.bearing = (float) degree;

updateMatrices();
}

public boolean tiltMap(float move) {
ThreadUtils.assertMainThread();

return setTilt(mPos.tilt + move);
}

public boolean setTilt(float tilt) {
ThreadUtils.assertMainThread();

tilt = FastMath.clamp(tilt, 0, MAX_TILT);
tilt = limitTilt(tilt);
if (tilt == mPos.tilt)
return false;

mPos.tilt = tilt;
updateMatrices();
return true;
Expand All @@ -198,11 +198,15 @@ public boolean setTilt(float tilt) {
public void setMapPosition(MapPosition mapPosition) {
ThreadUtils.assertMainThread();

mPos.scale = clamp(mapPosition.scale, MIN_SCALE, MAX_SCALE);
mPos.x = mapPosition.x;
mPos.y = mapPosition.y;
mPos.tilt = clamp(mapPosition.tilt, 0, MAX_TILT);
mPos.bearing = mapPosition.bearing;
mPos.copy(mapPosition);
limitPosition(mPos);

// mPos.scale = clamp(mapPosition.scale, mMinScale, mMaxScale);
// mPos.x = mapPosition.x;
// mPos.y = mapPosition.y;
// mPos.tilt = limitTilt(mapPosition.tilt);
// mPos.bearing = mapPosition.bearing;

updateMatrices();
}

Expand Down
86 changes: 80 additions & 6 deletions vtm/src/org/oscim/map/Viewport.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,24 @@
public class Viewport {
//static final Logger log = LoggerFactory.getLogger(Viewport.class);

public final static int MAX_ZOOMLEVEL = 20;
public final static int MIN_ZOOMLEVEL = 2;
private final static int MAX_ZOOMLEVEL = 20;
private final static int MIN_ZOOMLEVEL = 2;
private final static float MIN_TILT = 0;
private final static float MAX_TILT = 65;

public final static double MAX_SCALE = (1 << MAX_ZOOMLEVEL);
public final static double MIN_SCALE = (1 << MIN_ZOOMLEVEL);
protected double mMaxScale = (1 << MAX_ZOOMLEVEL);
protected double mMinScale = (1 << MIN_ZOOMLEVEL);

public final static float MAX_TILT = 65;
protected float mMinTilt = MIN_TILT;
protected float mMaxTilt = MAX_TILT;

protected float mMinBearing = -180;
protected float mMaxBearing = 180;

protected double mMinX = 0;
protected double mMaxX = 1;
protected double mMinY = 0;
protected double mMaxY = 1;

protected final MapPosition mPos = new MapPosition();

Expand Down Expand Up @@ -72,13 +83,76 @@ public class Viewport {
public final static float VIEW_SCALE = (VIEW_NEAR / VIEW_DISTANCE) * 0.5f;

public Viewport() {
mPos.scale = MIN_SCALE;
mPos.scale = mMinScale;
mPos.x = 0.5;
mPos.y = 0.5;
mPos.bearing = 0;
mPos.tilt = 0;
}

public double limitScale(double scale) {
if (scale > mMaxScale)
return mMaxScale;
else if (scale < mMinScale)
return mMinScale;

return scale;
}

public float limitTilt(float tilt) {
if (tilt > mMaxTilt)
return mMaxTilt;
else if (tilt < mMinTilt)
return mMinTilt;

return tilt;
}

public boolean limitPosition(MapPosition pos) {
boolean changed = false;
if (pos.scale > mMaxScale) {
pos.scale = mMaxScale;
changed = true;
} else if (pos.scale < mMinScale) {
pos.scale = mMinScale;
changed = true;
}

if (pos.tilt > mMaxTilt) {
pos.tilt = mMaxTilt;
changed = true;
} else if (pos.tilt < mMinTilt) {
pos.tilt = mMinTilt;
changed = true;
}

if (pos.bearing > mMaxBearing) {
pos.bearing = mMaxBearing;
changed = true;
} else if (pos.bearing < mMinBearing) {
pos.bearing = mMinBearing;
changed = true;
}

if (pos.x > mMaxX) {
pos.x = mMaxX;
changed = true;
} else if (pos.x < mMinX) {
pos.x = mMinX;
changed = true;
}

if (pos.y > mMaxY) {
pos.y = mMaxY;
changed = true;
} else if (pos.y < mMinY) {
pos.y = mMinY;
changed = true;
}

return changed;
}

/**
* Get the current MapPosition.
*
Expand Down

0 comments on commit ff630fd

Please sign in to comment.