Merge pull request #748 from mapsforge/location

Location renderers: allow fixed center position
This commit is contained in:
Emux 2019-10-18 14:12:33 +03:00 committed by GitHub
commit 4866abe38e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 35 deletions

View File

@ -1,4 +1,5 @@
/* /*
* Copyright 2014 Hannes Janetzek
* Copyright 2016-2019 devemux86 * Copyright 2016-2019 devemux86
* Copyright 2017 Luca Osten * Copyright 2017 Luca Osten
* Copyright 2018 Izumi Kawashima * Copyright 2018 Izumi Kawashima
@ -74,21 +75,49 @@ public class ViewController extends Viewport {
} }
/** /**
* Get pivot horizontal / vertical relative to view center. * Get pivot horizontal / vertical relative to view center in [-1, 1].
* e.g. pivotY 0.5 is usually preferred for navigation, moving center to 25% of view height. * e.g. pivotY 0.5 is usually preferred for navigation, moving center to 25% of view height.
* Range is [-1, 1].
*/ */
public float[] getMapViewCenter() { public float[] getMapViewCenter() {
return new float[]{mPivotX, mPivotY}; return new float[]{mPivotX, mPivotY};
} }
/** /**
* Set pivot horizontal / vertical relative to view center. * Get pivot horizontal relative to view center in [-1, 1].
*/
public float getMapViewCenterX() {
return mPivotX;
}
/**
* Get pivot vertical relative to view center in [-1, 1].
* e.g. pivotY 0.5 is usually preferred for navigation, moving center to 25% of view height.
*/
public float getMapViewCenterY() {
return mPivotY;
}
/**
* Set pivot horizontal / vertical relative to view center in [-1, 1].
* e.g. pivotY 0.5 is usually preferred for navigation, moving center to 25% of view height. * e.g. pivotY 0.5 is usually preferred for navigation, moving center to 25% of view height.
* Range is [-1, 1].
*/ */
public void setMapViewCenter(float pivotX, float pivotY) { public void setMapViewCenter(float pivotX, float pivotY) {
setMapViewCenterX(pivotX);
setMapViewCenterY(pivotY);
}
/**
* Set pivot horizontal relative to view center in [-1, 1].
*/
public void setMapViewCenterX(float pivotX) {
mPivotX = FastMath.clamp(pivotX, -1, 1) * 0.5f; mPivotX = FastMath.clamp(pivotX, -1, 1) * 0.5f;
}
/**
* Set pivot horizontal / vertical relative to view center in [-1, 1].
* e.g. pivotY 0.5 is usually preferred for navigation, moving center to 25% of view height.
*/
public void setMapViewCenterY(float pivotY) {
mPivotY = FastMath.clamp(pivotY, -1, 1) * 0.5f; mPivotY = FastMath.clamp(pivotY, -1, 1) * 0.5f;
} }

View File

@ -66,6 +66,7 @@ public class LocationRenderer extends LayerRenderer {
private boolean mRunAnim; private boolean mRunAnim;
private boolean mAnimate = true; private boolean mAnimate = true;
private long mAnimStart; private long mAnimStart;
private boolean mCenter;
private Callback mCallback; private Callback mCallback;
private final float[] mColors = new float[4]; private final float[] mColors = new float[4];
@ -97,6 +98,10 @@ public class LocationRenderer extends LayerRenderer {
mCallback = callback; mCallback = callback;
} }
public void setCenter(boolean center) {
mCenter = center;
}
public void setColor(int color) { public void setColor(int color) {
float a = Color.aToFloat(color); float a = Color.aToFloat(color);
mColors[0] = a * Color.rToFloat(color); mColors[0] = a * Color.rToFloat(color);
@ -168,33 +173,39 @@ public class LocationRenderer extends LayerRenderer {
return; return;
} }
/*if (!v.changed() && isReady()) /*if (!v.changed() && isReady())
return;*/ return;*/
setReady(true); setReady(true);
int width = mMap.getWidth(); int width = mMap.getWidth();
int height = mMap.getHeight(); int height = mMap.getHeight();
// clamp location to a position that can be double x, y;
// savely translated to screen coordinates if (mCenter) {
v.getBBox(mBBox, 0); x = (width >> 1) + width * mMap.viewport().getMapViewCenterX();
y = (height >> 1) + height * mMap.viewport().getMapViewCenterY();
} else {
// clamp location to a position that can be
// safely translated to screen coordinates
v.getBBox(mBBox, 0);
double x = mLocation.x; x = mLocation.x;
double y = mLocation.y; y = mLocation.y;
if (!mBBox.contains(mLocation)) { if (!mBBox.contains(mLocation)) {
x = FastMath.clamp(x, mBBox.xmin, mBBox.xmax); x = FastMath.clamp(x, mBBox.xmin, mBBox.xmax);
y = FastMath.clamp(y, mBBox.ymin, mBBox.ymax); y = FastMath.clamp(y, mBBox.ymin, mBBox.ymax);
}
// get position of Location in pixel relative to
// screen center
v.toScreenPoint(x, y, mScreenPoint);
x = mScreenPoint.x + (width >> 1);
y = mScreenPoint.y + (height >> 1);
} }
// get position of Location in pixel relative to
// screen center
v.toScreenPoint(x, y, mScreenPoint);
x = mScreenPoint.x + width / 2;
y = mScreenPoint.y + height / 2;
// clip position to screen boundaries // clip position to screen boundaries
int visible = 0; int visible = 0;

View File

@ -1,6 +1,6 @@
/* /*
* Copyright 2017-2018 Longri * Copyright 2017-2018 Longri
* Copyright 2018 devemux86 * Copyright 2018-2019 devemux86
* *
* This program is free software: you can redistribute it and/or modify it under the * 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 * terms of the GNU Lesser General Public License as published by the Free Software
@ -98,6 +98,7 @@ public class LocationTextureRenderer extends BucketRenderer {
private boolean runAnim; private boolean runAnim;
private boolean animate = true; private boolean animate = true;
private long animStart; private long animStart;
private boolean center;
private boolean update; private boolean update;
private float bearing; private float bearing;
@ -124,6 +125,10 @@ public class LocationTextureRenderer extends BucketRenderer {
this.billboard = billboard; this.billboard = billboard;
} }
public void setCenter(boolean center) {
this.center = center;
}
public void setIndicatorColor(int color) { public void setIndicatorColor(int color) {
this.viewShedColor = color; this.viewShedColor = color;
} }
@ -194,23 +199,30 @@ public class LocationTextureRenderer extends BucketRenderer {
int width = map.getWidth(); int width = map.getWidth();
int height = map.getHeight(); int height = map.getHeight();
v.getBBox(boundingBox, 0);
double x = mapPoint.x; double x, y;
double y = mapPoint.y; if (center) {
x = (width >> 1) + width * map.viewport().getMapViewCenterX();
y = (height >> 1) + height * map.viewport().getMapViewCenterY();
} else {
v.getBBox(boundingBox, 0);
if (!boundingBox.contains(mapPoint)) { x = mapPoint.x;
x = FastMath.clamp(x, boundingBox.xmin, boundingBox.xmax); y = mapPoint.y;
y = FastMath.clamp(y, boundingBox.ymin, boundingBox.ymax);
if (!boundingBox.contains(mapPoint)) {
x = FastMath.clamp(x, boundingBox.xmin, boundingBox.xmax);
y = FastMath.clamp(y, boundingBox.ymin, boundingBox.ymax);
}
// get position of Location in pixel relative to
// screen center
v.toScreenPoint(x, y, screenPoint);
x = screenPoint.x + width / 2;
y = screenPoint.y + height / 2;
} }
// get position of Location in pixel relative to
// screen center
v.toScreenPoint(x, y, screenPoint);
x = screenPoint.x + width / 2;
y = screenPoint.y + height / 2;
// clip position to screen boundaries // clip position to screen boundaries
int visible = 0; int visible = 0;