Remove unnecessary code and check arrows visibility before drawing

This commit is contained in:
Vitaliy 2020-07-17 16:04:49 +03:00
parent c091237e77
commit 92a89f6e2a
3 changed files with 112 additions and 116 deletions

View file

@ -407,6 +407,7 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
for (SelectedGpxFile selectedGpxFile : selectedGPXFiles) {
boolean showArrows = selectedGpxFile.getGpxFile().isShowArrows();
if (showArrows) {
QuadRect correctedQuadRect = getCorrectedQuadRect(tileBox.getLatLonBounds());
int color = selectedGpxFile.getGpxFile().getColor(cachedColor);
if (selectedGpxFile.isShowCurrentTrack()) {
color = currentTrackColor;
@ -418,12 +419,23 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
List<Float> ty = new ArrayList<>();
List<Double> distances = new ArrayList<>();
List<Double> angles = new ArrayList<>();
List<GeometryWayStyle> styles = new ArrayList<>();
boolean previousVisible = false;
List<WptPt> points = segment.points;
if (points.size() > 1) {
for (int i = 0; i < points.size(); i++) {
WptPt pt = points.get(i);
addLocation(tileBox, pt, tx, ty, angles, distances);
if (correctedQuadRect.left <= pt.getLongitude()
&& pt.getLongitude() <= correctedQuadRect.right
&& correctedQuadRect.bottom <= pt.getLatitude()
&& pt.getLatitude() <= correctedQuadRect.top) {
addLocation(tileBox, pt.getLatitude(), pt.getLongitude(), null, tx, ty, angles, distances, 0, styles);
previousVisible = true;
} else if (previousVisible) {
addLocation(tileBox, pt.getLatitude(), pt.getLongitude(), null, tx, ty, angles, distances, 0, styles);
previousVisible = false;
}
}
}
drawArrowsOverPath(tx, ty, angles, distances, canvas, tileBox, arrowsWayStyle);
@ -433,29 +445,6 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
}
}
private void addLocation(RotatedTileBox tb, WptPt pt, List<Float> tx, List<Float> ty,
List<Double> angles, List<Double> distances) {
float x = tb.getPixXFromLatLon(pt.getLatitude(), pt.getLongitude());
float y = tb.getPixYFromLatLon(pt.getLatitude(), pt.getLongitude());
float px = x;
float py = y;
int previous = tx.size() - 1;
if (previous >= 0) {
px = tx.get(previous);
py = ty.get(previous);
}
double angle = 0;
if (px != x || py != y) {
double angleRad = Math.atan2(y - py, x - px);
angle = (angleRad * 180 / Math.PI) + 90f;
}
double distSegment = Math.sqrt((y - py) * (y - py) + (x - px) * (x - px));
tx.add(x);
ty.add(y);
angles.add(angle);
distances.add(distSegment);
}
private void drawArrowsOverPath(List<Float> tx, List<Float> ty, List<Double> angles, List<Double> distances,
Canvas canvas, RotatedTileBox tb, GeometryWayStyle wayStyle) {
int pixHeight = tb.getPixHeight();
@ -469,7 +458,7 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
double pxStep = arrowBitmap.getHeight() * 4f * zoomCoef;
double dist = 0;
List<RouteLayer.PathPoint> arrows = new ArrayList<>();
List<PathPoint> arrows = new ArrayList<>();
for (int i = tx.size() - 2; i >= 0; i--) {
float px = tx.get(i);
float py = ty.get(i);
@ -491,14 +480,14 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
float iconx = (float) (px + pdx);
float icony = (float) (py + pdy);
if (isIn(iconx, icony, left, top, right, bottom)) {
arrows.add(new RouteLayer.PathPoint(iconx, icony, angle, wayStyle));
arrows.add(new PathPoint(iconx, icony, angle, wayStyle));
}
dist -= pxStep;
percent -= pxStep / distSegment;
}
}
for (int i = arrows.size() - 1; i >= 0; i--) {
RouteLayer.PathPoint a = arrows.get(i);
PathPoint a = arrows.get(i);
a.draw(canvas, wayContext);
}
}

View file

@ -6,6 +6,7 @@ import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Join;
@ -470,6 +471,80 @@ public abstract class OsmandMapLayer {
}
}
public static class PathPoint {
float x;
float y;
double angle;
GeometryWayStyle style;
private Matrix matrix = new Matrix();
PathPoint(float x, float y, double angle, GeometryWayStyle style) {
this.x = x;
this.y = y;
this.angle = angle;
this.style = style;
}
protected Matrix getMatrix() {
return matrix;
}
void draw(Canvas canvas, GeometryWayContext context) {
if (style != null && style.getPointBitmap() != null) {
Bitmap bitmap = style.getPointBitmap();
Integer pointColor = style.getPointColor();
float paintH2 = bitmap.getHeight() / 2f;
float paintW2 = bitmap.getWidth() / 2f;
matrix.reset();
matrix.postRotate((float) angle, paintW2, paintH2);
matrix.postTranslate(x - paintW2, y - paintH2);
if (pointColor != null) {
Paint paint = context.getPaintIconCustom();
paint.setColorFilter(new PorterDuffColorFilter(pointColor, Mode.SRC_IN));
canvas.drawBitmap(bitmap, matrix, paint);
} else {
if (style.hasPaintedPointBitmap()) {
Paint paint = context.getPaintIconCustom();
paint.setColorFilter(null);
canvas.drawBitmap(bitmap, matrix, paint);
} else {
canvas.drawBitmap(bitmap, matrix, context.getPaintIcon());
}
}
}
}
}
protected void addLocation(RotatedTileBox tb, double latitude, double longitude, GeometryWayStyle style,
List<Float> tx, List<Float> ty, List<Double> angles, List<Double> distances,
double dist, List<GeometryWayStyle> styles) {
float x = tb.getPixXFromLatLon(latitude, longitude);
float y = tb.getPixYFromLatLon(latitude, longitude);
float px = x;
float py = y;
int previous = tx.size() - 1;
if (previous >= 0) {
px = tx.get(previous);
py = ty.get(previous);
}
double angle = 0;
if (px != x || py != y) {
double angleRad = Math.atan2(y - py, x - px);
angle = (angleRad * 180 / Math.PI) + 90f;
}
double distSegment = Math.sqrt((y - py) * (y - py) + (x - px) * (x - px));
if (dist != 0) {
distSegment = dist;
}
tx.add(x);
ty.add(y);
angles.add(angle);
distances.add(distSegment);
styles.add(style);
}
public int calculatePath(RotatedTileBox tb, List<Float> xs, List<Float> ys, List<GeometryWayStyle> styles, List<Pair<Path, GeometryWayStyle>> paths) {
boolean segmentStarted = false;
float prevX = xs.get(0);
@ -561,6 +636,20 @@ public abstract class OsmandMapLayer {
return false;
}
public QuadRect getCorrectedQuadRect(QuadRect latlonRect) {
double topLatitude = latlonRect.top;
double leftLongitude = latlonRect.left;
double bottomLatitude = latlonRect.bottom;
double rightLongitude = latlonRect.right;
// double lat = 0;
// double lon = 0;
// this is buggy lat/lon should be 0 but in that case
// it needs to be fixed in case there is no route points in the view bbox
double lat = topLatitude - bottomLatitude + 0.1;
double lon = rightLongitude - leftLongitude + 0.1;
return new QuadRect(leftLongitude - lon, topLatitude + lat, rightLongitude + lon, bottomLatitude - lat);
}
public QuadRect calculateRect(float x, float y, float width, float height) {
QuadRect rf;
double left = x - width / 2.0d;

View file

@ -186,17 +186,8 @@ public class RouteLayer extends OsmandMapLayer implements ContextMenuLayer.ICont
}
final QuadRect latlonRect = cp.getLatLonBounds();
double topLatitude = latlonRect.top;
double leftLongitude = latlonRect.left;
double bottomLatitude = latlonRect.bottom;
double rightLongitude = latlonRect.right;
// double lat = 0;
// double lon = 0;
// this is buggy lat/lon should be 0 but in that case
// it needs to be fixed in case there is no route points in the view bbox
double lat = topLatitude - bottomLatitude + 0.1;
double lon = rightLongitude - leftLongitude + 0.1;
drawLocations(tileBox, canvas, topLatitude + lat, leftLongitude - lon, bottomLatitude - lat, rightLongitude + lon);
final QuadRect correctedQuadRect = getCorrectedQuadRect(latlonRect);
drawLocations(tileBox, canvas, correctedQuadRect.top, correctedQuadRect.left, correctedQuadRect.bottom, correctedQuadRect.right);
if (trackChartPoints != null) {
canvas.rotate(-tileBox.getRotate(), tileBox.getCenterPixelX(), tileBox.getCenterPixelY());
@ -372,52 +363,6 @@ public class RouteLayer extends OsmandMapLayer implements ContextMenuLayer.ICont
}
}
static class PathPoint {
float x;
float y;
double angle;
GeometryWayStyle style;
private Matrix matrix = new Matrix();
PathPoint(float x, float y, double angle, GeometryWayStyle style) {
this.x = x;
this.y = y;
this.angle = angle;
this.style = style;
}
protected Matrix getMatrix() {
return matrix;
}
void draw(Canvas canvas, GeometryWayContext context) {
if (style != null && style.getPointBitmap() != null) {
Bitmap bitmap = style.getPointBitmap();
Integer pointColor = style.getPointColor();
float paintH2 = bitmap.getHeight() / 2f;
float paintW2 = bitmap.getWidth() / 2f;
matrix.reset();
matrix.postRotate((float) angle, paintW2, paintH2);
matrix.postTranslate(x - paintW2, y - paintH2);
if (pointColor != null) {
Paint paint = context.getPaintIconCustom();
paint.setColorFilter(new PorterDuffColorFilter(pointColor, Mode.SRC_IN));
canvas.drawBitmap(bitmap, matrix, paint);
} else {
if (style.hasPaintedPointBitmap()) {
Paint paint = context.getPaintIconCustom();
paint.setColorFilter(null);
canvas.drawBitmap(bitmap, matrix, paint);
} else {
canvas.drawBitmap(bitmap, matrix, context.getPaintIcon());
}
}
}
}
}
private static class PathAnchor extends PathPoint {
PathAnchor(float x, float y, GeometryAnchorWayStyle style) {
super(x, y, 0, style);
@ -1003,14 +948,14 @@ public class RouteLayer extends OsmandMapLayer implements ContextMenuLayer.ICont
} else if (lastProjection != null) {
lt = lastProjection;
}
if(lt != null) {
addLocation(tb, lt, style, tx, ty, angles, distances, 0, styles); // first point
if (lt != null) {
addLocation(tb, lt.getLatitude(), lt.getLongitude(), style, tx, ty, angles, distances, 0, styles); // first point
}
}
addLocation(tb, ls, style, tx, ty, angles, distances, dist, styles);
addLocation(tb, ls.getLatitude(), ls.getLongitude(), style, tx, ty, angles, distances, dist, styles);
previousVisible = true;
} else if (previousVisible) {
addLocation(tb, ls, style, tx, ty, angles, distances, previous == -1 ? 0 : odistances.get(i), styles);
addLocation(tb, ls.getLatitude(), ls.getLongitude(), style, tx, ty, angles, distances, previous == -1 ? 0 : odistances.get(i), styles);
double distToFinish = 0;
for(int ki = i + 1; ki < odistances.size(); ki++) {
distToFinish += odistances.get(ki);
@ -1027,7 +972,7 @@ public class RouteLayer extends OsmandMapLayer implements ContextMenuLayer.ICont
private boolean addPoint(RotatedTileBox tb, double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, GeometryWayStyle style, boolean previousVisible, Location lastPoint) {
if (leftLongitude <= lastPoint .getLongitude() && lastPoint .getLongitude() <= rightLongitude
&& bottomLatitude <= lastPoint .getLatitude() && lastPoint .getLatitude() <= topLatitude) {
addLocation(tb, lastPoint, style, tx, ty, angles, distances, 0, styles);
addLocation(tb, lastPoint.getLatitude(), lastPoint.getLongitude(), style, tx, ty, angles, distances, 0, styles);
previousVisible = true;
}
return previousVisible;
@ -1040,33 +985,6 @@ public class RouteLayer extends OsmandMapLayer implements ContextMenuLayer.ICont
angles.clear();
styles.clear();
}
private void addLocation(RotatedTileBox tb, Location ls, GeometryWayStyle style, List<Float> tx, List<Float> ty,
List<Double> angles, List<Double> distances, double dist, List<GeometryWayStyle> styles) {
float x = tb.getPixXFromLatLon(ls.getLatitude(), ls.getLongitude());
float y = tb.getPixYFromLatLon(ls.getLatitude(), ls.getLongitude());
float px = x;
float py = y;
int previous = tx.size() - 1;
if (previous >= 0 && previous < tx.size()) {
px = tx.get(previous);
py = ty.get(previous);
}
double angle = 0;
if (px != x || py != y) {
double angleRad = Math.atan2(y - py, x - px);
angle = (angleRad * 180 / Math.PI) + 90f;
}
double distSegment = Math.sqrt((y - py) * (y - py) + (x - px) * (x - px));
if(dist != 0) {
distSegment = dist;
}
tx.add(x);
ty.add(y);
angles.add(angle);
distances.add(distSegment);
styles.add(style);
}
}
private RouteSimplificationGeometry routeGeometry = new RouteSimplificationGeometry();