From 52facd77a902b275a728068919c8adea1b536d23 Mon Sep 17 00:00:00 2001 From: crimean Date: Sat, 19 Jan 2019 19:20:52 +0300 Subject: [PATCH] Added route line styles --- .../net/osmand/plus/views/OsmandMapLayer.java | 24 ++++- .../src/net/osmand/plus/views/RouteLayer.java | 99 ++++++++++--------- 2 files changed, 69 insertions(+), 54 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java b/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java index e024b0dfd4..dd3571f456 100644 --- a/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java @@ -147,15 +147,29 @@ public abstract class OsmandMapLayer { return res; } - public int calculatePath(RotatedTileBox tb, TIntArrayList xs, TIntArrayList ys, List colors, List> paths) { + public static class GeometryWayStyle { + static final int WAY_TYPE_SOLID_LINE = 0; + static final int WAY_TYPE_TRANSPORT_LINE = 1; + static final int WAY_TYPE_WALK_LINE = 2; + + Integer color; + int type; + + public GeometryWayStyle(Integer color, int type) { + this.color = color; + this.type = type; + } + } + + public int calculatePath(RotatedTileBox tb, TIntArrayList xs, TIntArrayList ys, List styles, List> paths) { boolean segmentStarted = false; int prevX = xs.get(0); int prevY = ys.get(0); int height = tb.getPixHeight(); int width = tb.getPixWidth(); int cnt = 0; - boolean hasColors = colors != null && colors.size() == xs.size(); - int color = hasColors ? colors.get(0) : 0; + boolean hasStyles = styles != null && styles.size() == xs.size(); + int color = hasStyles ? styles.get(0).color : 0; Path path = new Path(); boolean prevIn = isIn(prevX, prevY, 0, 0, width, height); for (int i = 1; i < xs.size(); i++) { @@ -199,8 +213,8 @@ public abstract class OsmandMapLayer { prevX = currX; prevY = currY; - if (hasColors) { - int newColor = colors.get(i); + if (hasStyles) { + int newColor = styles.get(i).color; if (color != newColor) { paths.add(new Pair<>(path, color)); path = new Path(); diff --git a/OsmAnd/src/net/osmand/plus/views/RouteLayer.java b/OsmAnd/src/net/osmand/plus/views/RouteLayer.java index dfe1365cd6..d18a947f0b 100644 --- a/OsmAnd/src/net/osmand/plus/views/RouteLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/RouteLayer.java @@ -37,11 +37,11 @@ import net.osmand.router.TransportRoutePlanner.TransportRouteResultSegment; import net.osmand.util.MapUtils; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.TreeMap; import gnu.trove.list.array.TByteArrayList; @@ -432,24 +432,28 @@ public class RouteLayer extends OsmandMapLayer { double mapDensity; TreeMap zooms = new TreeMap<>(); List locations = Collections.emptyList(); - TreeMap colorsMap = new TreeMap<>(); + Map styleMap = Collections.emptyMap(); // cache arrays TIntArrayList tx = new TIntArrayList(); TIntArrayList ty = new TIntArrayList(); List angles = new ArrayList<>(); List distances = new ArrayList<>(); - List colors = new ArrayList<>(); + List styles = new ArrayList<>(); - private int getColor(int index) { - List list = new ArrayList<>(colorsMap.keySet()); - for (int i = list.size() -1; i >= 0; i--) { + private GeometryWayStyle getStyle(int index, GeometryWayStyle defaultWayStyle) { + List list = new ArrayList<>(styleMap.keySet()); + for (int i = list.size() - 1; i >= 0; i--) { int c = list.get(i); if (c <= index) { - return colorsMap.get(c); + return styleMap.get(c); } } - return attrs.paint.getColor(); + return defaultWayStyle; + } + + private boolean isTransportRoute() { + return transportRoute != null; } public void updateRoute(RotatedTileBox tb, RouteCalculationResult route) { @@ -460,7 +464,7 @@ public class RouteLayer extends OsmandMapLayer { } else { locations = route.getImmutableAllLocations(); } - colorsMap.clear(); + styleMap = Collections.emptyMap(); this.mapDensity = tb.getMapDensity(); zooms.clear(); } @@ -471,24 +475,20 @@ public class RouteLayer extends OsmandMapLayer { this.transportRoute = route; if (route == null) { locations = Collections.emptyList(); - colorsMap.clear(); + styleMap = Collections.emptyMap(); } else { LatLon start = transportHelper.getStartLocation(); LatLon end = transportHelper.getEndLocation(); List list = new ArrayList<>(); - List cols = new ArrayList<>(); - calculateTransportResult(start, end, route, list, cols); + List styles = new ArrayList<>(); + calculateTransportResult(start, end, route, list, styles); List locs = new ArrayList<>(); - TreeMap colsMap = new TreeMap<>(); + Map stlMap = new TreeMap<>(); int i = 0; int k = 0; if (list.size() > 0) { for (Way w : list) { - colsMap.put(k, cols.get(i++)); - //Location loc = new Location(""); - //loc.setLatitude(w.getLatitude()); - //loc.setLongitude(w.getLongitude()); - //locs.add(loc); + stlMap.put(k, styles.get(i++)); for (Node n : w.getNodes()) { Location ln = new Location(""); ln.setLatitude(n.getLatitude()); @@ -499,7 +499,7 @@ public class RouteLayer extends OsmandMapLayer { } } locations = locs; - colorsMap = colsMap; + styleMap = stlMap; } this.mapDensity = tb.getMapDensity(); zooms.clear(); @@ -523,12 +523,13 @@ public class RouteLayer extends OsmandMapLayer { List odistances = geometryZoom.getDistances(); clearArrays(); - int color = attrs.paint.getColor(); + GeometryWayStyle defaultWayStyle = new GeometryWayStyle(attrs.paint.getColor(), GeometryWayStyle.WAY_TYPE_SOLID_LINE); + GeometryWayStyle style = defaultWayStyle; boolean previousVisible = false; if (lastProjection != null) { if (leftLongitude <= lastProjection.getLongitude() && lastProjection.getLongitude() <= rightLongitude && bottomLatitude <= lastProjection.getLatitude() && lastProjection.getLatitude() <= topLatitude) { - addLocation(tb, lastProjection, color, tx, ty, angles, distances, 0, colors); + addLocation(tb, lastProjection, style, tx, ty, angles, distances, 0, styles); previousVisible = true; } } @@ -536,8 +537,8 @@ public class RouteLayer extends OsmandMapLayer { int previous = -1; for (int i = currentRoute; i < routeNodes.size(); i++) { Location ls = routeNodes.get(i); - color = getColor(i); - if (simplification.getQuick(i) == 0 && !colorsMap.containsKey(i)) { + style = getStyle(i, defaultWayStyle); + if (simplification.getQuick(i) == 0 && !styleMap.containsKey(i)) { continue; } if (leftLongitude <= ls.getLongitude() && ls.getLongitude() <= rightLongitude && bottomLatitude <= ls.getLatitude() @@ -551,23 +552,23 @@ public class RouteLayer extends OsmandMapLayer { } else if (lastProjection != null) { lt = lastProjection; } - addLocation(tb, lt, color, tx, ty, angles, distances, 0, colors); // first point + addLocation(tb, lt, style, tx, ty, angles, distances, 0, styles); // first point } - addLocation(tb, ls, color, tx, ty, angles, distances, dist, colors); + addLocation(tb, ls, style, tx, ty, angles, distances, dist, styles); previousVisible = true; } else if (previousVisible) { - addLocation(tb, ls, color, tx, ty, angles, distances, previous == -1 ? 0 : odistances.get(i), colors); + addLocation(tb, ls, 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); } - drawRouteSegment(tb, canvas, tx, ty, angles, distances, distToFinish, colors); + drawRouteSegment(tb, canvas, tx, ty, angles, distances, distToFinish, styles); previousVisible = false; clearArrays(); } previous = i; } - drawRouteSegment(tb, canvas, tx, ty, angles, distances, 0, colors); + drawRouteSegment(tb, canvas, tx, ty, angles, distances, 0, styles); } private void clearArrays() { @@ -575,11 +576,11 @@ public class RouteLayer extends OsmandMapLayer { ty.clear(); distances.clear(); angles.clear(); - colors.clear(); + styles.clear(); } - private void addLocation(RotatedTileBox tb, Location ls, int color, TIntArrayList tx, TIntArrayList ty, - List angles, List distances, double dist, List colors) { + private void addLocation(RotatedTileBox tb, Location ls, GeometryWayStyle style, TIntArrayList tx, TIntArrayList ty, + List angles, List distances, double dist, List styles) { float x = tb.getPixXFromLatLon(ls.getLatitude(), ls.getLongitude()); float y = tb.getPixYFromLatLon(ls.getLatitude(), ls.getLongitude()); float px = x; @@ -602,21 +603,21 @@ public class RouteLayer extends OsmandMapLayer { ty.add((int) y); angles.add(angle); distances.add(distSegment); - colors.add(color); + styles.add(style); } } RouteSimplificationGeometry routeGeometry = new RouteSimplificationGeometry(); private void drawRouteSegment(RotatedTileBox tb, Canvas canvas, TIntArrayList tx, TIntArrayList ty, - List angles, List distances, double distToFinish, List colors) { + List angles, List distances, double distToFinish, List styles) { if (tx.size() < 2) { return; } try { List> paths = new ArrayList<>(); canvas.rotate(-tb.getRotate(), tb.getCenterPixelX(), tb.getCenterPixelY()); - calculatePath(tb, tx, ty, colors, paths); + calculatePath(tb, tx, ty, styles, paths); for (Pair pc : paths) { attrs.customColor = pc.second; attrs.drawPath(canvas, pc.first); @@ -802,25 +803,25 @@ public class RouteLayer extends OsmandMapLayer { return false; } - private void calculateTransportResult(LatLon start, LatLon end, TransportRouteResult r, List res, List colors) { + private void calculateTransportResult(LatLon start, LatLon end, TransportRouteResult r, List res, List styles) { if (r != null) { LatLon p = start; TransportRouteResultSegment prev = null; for (TransportRouteResultSegment s : r.getSegments()) { LatLon floc = s.getStart().getLocation(); - addRouteWalk(prev, s, p, floc, res, colors); + addRouteWalk(prev, s, p, floc, res, styles); List geometry = s.getGeometry(); res.addAll(geometry); - addColors(s.route, geometry.size(), colors); + addStyle(s.route, geometry.size(), styles); p = s.getEnd().getLocation(); prev = s; } - addRouteWalk(prev, null, p, end, res, colors); + addRouteWalk(prev, null, p, end, res, styles); } } private void addRouteWalk(TransportRouteResultSegment s1, TransportRouteResultSegment s2, - LatLon start, LatLon end, List res, List colors) { + LatLon start, LatLon end, List res, List styles) { final RouteCalculationResult walkingRouteSegment = transportHelper.getWalkingRouteSegment(s1, s2); if (walkingRouteSegment != null && walkingRouteSegment.getRouteLocations().size() > 0) { final List routeLocations = walkingRouteSegment.getRouteLocations(); @@ -830,7 +831,7 @@ public class RouteLayer extends OsmandMapLayer { way.addNode(new Node(l.getLatitude(), l.getLongitude(), -1)); } res.add(way); - addColors(null, 1, colors); + addStyle(null, 1, styles); } else { double dist = MapUtils.getDistance(start, end); Way way = new Way(-1); @@ -838,26 +839,26 @@ public class RouteLayer extends OsmandMapLayer { way.addNode(new Node(start.getLatitude(), start.getLongitude(), -1)); way.addNode(new Node(end.getLatitude(), end.getLongitude(), -1)); res.add(way); - addColors(null, 1, colors); + addStyle(null, 1, styles); } } - private void addColors(TransportRoute route, int count, List colors) { + private void addStyle(TransportRoute route, int count, List styles) { int color; + int type; if (route == null) { color = attrs.paint.getColor(); + type = GeometryWayStyle.WAY_TYPE_WALK_LINE; } else { TransportStopRoute r = new TransportStopRoute(); r.type = TransportStopType.findType(route.getType()); r.route = route; color = r.getColor(helper.getApplication(), nightMode); + type = GeometryWayStyle.WAY_TYPE_TRANSPORT_LINE; + } + GeometryWayStyle style = new GeometryWayStyle(color, type); + for (int i = 0; i < count; i++) { + styles.add(style); } - addColors(color, count, colors); - } - - private void addColors(int color, int count, List colors) { - Integer[] integers = new Integer[count]; - Arrays.fill(integers, color); - colors.addAll(Arrays.asList(integers)); } }