Support multiple segments

This commit is contained in:
cepprice 2021-04-02 01:26:38 +05:00
parent bf55e86a7e
commit 14209dbd01
4 changed files with 95 additions and 80 deletions

View file

@ -219,14 +219,11 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
if (editingCtx.isInMultiProfileMode()) { if (editingCtx.isInMultiProfileMode()) {
multiProfileGeometryWayContext.updatePaints(settings.isNightMode(), multiProfileLineAttrs); multiProfileGeometryWayContext.updatePaints(settings.isNightMode(), multiProfileLineAttrs);
for (int i = 0; i < editingCtx.getBeforeSegments().size(); i++) { List<TrkSegment> allSegments = new ArrayList<>();
multiProfileGeometry.updateRoute(tb, editingCtx.getRoadSegmentData(), true, editingCtx.getBeforeSegments(), i); allSegments.addAll(editingCtx.getBeforeSegments());
allSegments.addAll(editingCtx.getAfterSegments());
multiProfileGeometry.updateRoute(tb, editingCtx.getRoadSegmentData(), allSegments);
multiProfileGeometry.drawSegments(canvas, tb); multiProfileGeometry.drawSegments(canvas, tb);
}
for (int i = 0; i < editingCtx.getAfterSegments().size(); i++) {
multiProfileGeometry.updateRoute(tb, editingCtx.getRoadSegmentData(), false, editingCtx.getAfterSegments(), i);
multiProfileGeometry.drawSegments(canvas, tb);
}
} else { } else {
multiProfileGeometry.clearWay(); multiProfileGeometry.clearWay();
List<TrkSegment> before = editingCtx.getBeforeTrkSegmentLine(); List<TrkSegment> before = editingCtx.getBeforeTrkSegmentLine();

View file

@ -36,8 +36,7 @@ public class MultiProfileGeometryWay extends GeometryWay<MultiProfileGeometryWay
private static final String DEFAULT_PROFILE_KEY = ApplicationMode.DEFAULT.getStringKey(); private static final String DEFAULT_PROFILE_KEY = ApplicationMode.DEFAULT.getStringKey();
private Map<Pair<WptPt, WptPt>, RoadSegmentData> segmentData; private Map<Pair<WptPt, WptPt>, RoadSegmentData> segmentData;
private List<TrkSegment> beforeSegments; private List<TrkSegment> segments;
private List<TrkSegment> afterSegments;
public MultiProfileGeometryWay(MultiProfileGeometryWayContext context) { public MultiProfileGeometryWay(MultiProfileGeometryWayContext context) {
super(context, new MultiProfileGeometryWayDrawer(context)); super(context, new MultiProfileGeometryWayDrawer(context));
@ -60,29 +59,29 @@ public class MultiProfileGeometryWay extends GeometryWay<MultiProfileGeometryWay
for (int i = 0; i < pathStyles.size(); i++) { for (int i = 0; i < pathStyles.size(); i++) {
Pair<Path, GeometryWayStyle<?>> currPathStyle = pathStyles.get(i); Pair<Path, GeometryWayStyle<?>> currPathStyle = pathStyles.get(i);
if (!((GeometryMultiProfileWayStyle) currPathStyle.second).isGap) {
getDrawer().drawPathBorder(canvas, currPathStyle.first, currPathStyle.second); getDrawer().drawPathBorder(canvas, currPathStyle.first, currPathStyle.second);
getDrawer().drawPath(canvas, currPathStyle.first, currPathStyle.second); getDrawer().drawPath(canvas, currPathStyle.first, currPathStyle.second);
} }
}
getDrawer().drawArrowsOverPath(canvas, tb, tx, ty, angles, distances, distToFinish, styles); getDrawer().drawArrowsOverPath(canvas, tb, tx, ty, angles, distances, distToFinish, styles);
} finally { } finally {
canvas.rotate(tb.getRotate(), tb.getCenterPixelX(), tb.getCenterPixelY()); canvas.rotate(tb.getRotate(), tb.getCenterPixelX(), tb.getCenterPixelY());
} }
} }
public void updateRoute(RotatedTileBox tileBox, Map<Pair<WptPt, WptPt>, RoadSegmentData> segmentData, public void updateRoute(RotatedTileBox tileBox, Map<Pair<WptPt, WptPt>, RoadSegmentData> segmentData, List<TrkSegment> segments) {
boolean before, List<TrkSegment> segments, int segmentIdx) {
boolean shouldUpdateRoute = tileBox.getMapDensity() != getMapDensity() || segmentDataChanged(segmentData) boolean shouldUpdateRoute = tileBox.getMapDensity() != getMapDensity() || segmentDataChanged(segmentData)
|| getSegments(before) != segments || getLocationProvider() == null; || this.segments != segments || getLocationProvider() == null;
if (shouldUpdateRoute) { if (shouldUpdateRoute) {
this.segmentData = segmentData; this.segmentData = segmentData;
setSegments(before, segments); this.segments = segments;
List<WptPt> userPoints = segments.get(segmentIdx).points;
List<Location> locations; List<Location> locations;
Map<Integer, GeometryWayStyle<?>> styleMap; Map<Integer, GeometryWayStyle<?>> styleMap;
List<Way> ways = new ArrayList<>(); List<Way> ways = new ArrayList<>();
List<GeometryWayStyle<?>> styles = new ArrayList<>(); List<GeometryWayStyle<?>> styles = new ArrayList<>();
setStyles(tileBox, userPoints, ways, styles); setStyles(tileBox, segments, ways, styles);
locations = new ArrayList<>(); locations = new ArrayList<>();
styleMap = new TreeMap<>(); styleMap = new TreeMap<>();
@ -105,26 +104,48 @@ public class MultiProfileGeometryWay extends GeometryWay<MultiProfileGeometryWay
} }
} }
private void setStyles(RotatedTileBox tileBox, List<WptPt> userPoints, List<Way> ways, List<GeometryWayStyle<?>> styles) { @Override
MultiProfileGeometryWayContext context = getContext(); public void clearWay() {
super.clearWay();
if (segmentData != null) {
segmentData.clear();
}
}
private void setStyles(RotatedTileBox tileBox, List<TrkSegment> segments, List<Way> ways, List<GeometryWayStyle<?>> styles) {
Path path = new Path(); Path path = new Path();
PathMeasure pathMeasure = new PathMeasure(); PathMeasure pathMeasure = new PathMeasure();
for (int i = 0; i < userPoints.size() - 1; i++) { for (TrkSegment segment : segments) {
WptPt leftPt = userPoints.get(i); List<WptPt> points = segment.points;
Pair<WptPt, WptPt> userLine = new Pair<>(leftPt, userPoints.get(i + 1)); for (int i = 0; i < points.size() - 1; i++) {
setStylesInternal(tileBox, points, i, ways, styles, path, pathMeasure);
}
styles.add(new GeometryMultiProfileWayStyle(getContext(), 0, 0, true));
Way way = new Way(-1);
WptPt last = points.get(points.size() - 1);
way.addNode(new Node(last.lat, last.lon, -1));
ways.add(way);
}
}
private void setStylesInternal(RotatedTileBox tileBox, List<WptPt> points, int idx, List<Way> ways,
List<GeometryWayStyle<?>> styles, Path path, PathMeasure pathMeasure) {
MultiProfileGeometryWayContext context = getContext();
WptPt leftPt = points.get(idx);
Pair<WptPt, WptPt> userLine = new Pair<>(leftPt, points.get(idx + 1));
RoadSegmentData routeBetweenPoints = segmentData.get(userLine); RoadSegmentData routeBetweenPoints = segmentData.get(userLine);
boolean isSecondToLast = idx + 2 == points.size();
Way way = new Way(-1); Way way = new Way(-1);
String currProfileKey = getProfileKey(leftPt); String currProfileKey = getProfileKey(leftPt);
Pair<Integer, Integer> profileData = getProfileData(currProfileKey); Pair<Integer, Integer> profileData = getProfileData(currProfileKey);
GeometryMultiProfileWayStyle style = new GeometryMultiProfileWayStyle( GeometryMultiProfileWayStyle style = new GeometryMultiProfileWayStyle(
getContext(), currProfileKey, profileData.first, profileData.second); getContext(), profileData.first, profileData.second);
styles.add(style); styles.add(style);
ways.add(way); ways.add(way);
path.reset(); path.reset();
boolean isSecondToLast = i + 2 == userPoints.size();
if (routeBetweenPoints == null || Algorithms.isEmpty(routeBetweenPoints.getPoints())) { if (routeBetweenPoints == null || Algorithms.isEmpty(routeBetweenPoints.getPoints())) {
way.addNode(new Node(userLine.first.lat, userLine.first.lon, -1)); way.addNode(new Node(userLine.first.lat, userLine.first.lon, -1));
if (isSecondToLast) { if (isSecondToLast) {
@ -151,7 +172,6 @@ public class MultiProfileGeometryWay extends GeometryWay<MultiProfileGeometryWay
style.setIconLon(tileBox.getLonFromPixel(xy[0], xy[1])); style.setIconLon(tileBox.getLonFromPixel(xy[0], xy[1]));
} }
} }
}
@Override @Override
protected boolean shouldAddLocation(RotatedTileBox tileBox, double leftLon, double rightLon, protected boolean shouldAddLocation(RotatedTileBox tileBox, double leftLon, double rightLon,
@ -181,18 +201,6 @@ public class MultiProfileGeometryWay extends GeometryWay<MultiProfileGeometryWay
return false; return false;
} }
private void setSegments(boolean before, List<TrkSegment> segments) {
if (before) {
this.beforeSegments = segments;
} else {
this.afterSegments = segments;
}
}
private List<TrkSegment> getSegments(boolean before) {
return before ? this.beforeSegments : this.afterSegments;
}
private void movePathToWpt(Path path, RotatedTileBox tileBox, WptPt pt) { private void movePathToWpt(Path path, RotatedTileBox tileBox, WptPt pt) {
path.moveTo(tileBox.getPixXFromLatLon(pt.lat, pt.lon), tileBox.getPixYFromLatLon(pt.lat, pt.lon)); path.moveTo(tileBox.getPixXFromLatLon(pt.lat, pt.lon), tileBox.getPixYFromLatLon(pt.lat, pt.lon));
} }
@ -223,7 +231,6 @@ public class MultiProfileGeometryWay extends GeometryWay<MultiProfileGeometryWay
public static class GeometryMultiProfileWayStyle extends GeometryWayStyle<MultiProfileGeometryWayContext> { public static class GeometryMultiProfileWayStyle extends GeometryWayStyle<MultiProfileGeometryWayContext> {
private final String profileKey;
@ColorInt @ColorInt
private final int lineColor; private final int lineColor;
@ColorInt @ColorInt
@ -231,16 +238,25 @@ public class MultiProfileGeometryWay extends GeometryWay<MultiProfileGeometryWay
@DrawableRes @DrawableRes
private final int profileIconRes; private final int profileIconRes;
private double iconLat; private final boolean isGap;
private double iconLon;
public GeometryMultiProfileWayStyle(MultiProfileGeometryWayContext context, String profileKey, private double iconLat = Double.NaN;
@ColorInt int profileColor, @DrawableRes int profileIconRes) { private double iconLon = Double.NaN;
public GeometryMultiProfileWayStyle(MultiProfileGeometryWayContext context,
@ColorInt int profileColor, @DrawableRes int profileIconRes,
boolean isGap) {
super(context); super(context);
this.profileKey = profileKey;
this.lineColor = profileColor; this.lineColor = profileColor;
this.borderColor = ColorUtils.blendARGB(profileColor, Color.BLACK, 0.2f); this.borderColor = ColorUtils.blendARGB(profileColor, Color.BLACK, 0.2f);
this.profileIconRes = profileIconRes; this.profileIconRes = profileIconRes;
this.isGap = isGap;
}
public GeometryMultiProfileWayStyle(MultiProfileGeometryWayContext context,
@ColorInt int profileColor, @DrawableRes int profileIconRes) {
this(context, profileColor, profileIconRes, false);
} }
@ColorInt @ColorInt
@ -255,7 +271,7 @@ public class MultiProfileGeometryWay extends GeometryWay<MultiProfileGeometryWay
@Override @Override
public Bitmap getPointBitmap() { public Bitmap getPointBitmap() {
return getContext().getProfileIconBitmap(profileKey, profileIconRes, borderColor); return getContext().getProfileIconBitmap(profileIconRes, borderColor);
} }
public void setIconLat(double lat) { public void setIconLat(double lat) {

View file

@ -73,8 +73,8 @@ public class MultiProfileGeometryWayContext extends GeometryWayContext {
} }
@NonNull @NonNull
public Bitmap getProfileIconBitmap(String profileKey, @DrawableRes int iconRes, @ColorInt int color) { public Bitmap getProfileIconBitmap(@DrawableRes int iconRes, @ColorInt int color) {
String key = profileKey + "_" + iconRes + "_" + color; String key = iconRes + "_" + color;
Bitmap bitmap = profileIconsBitmapCache.get(key); Bitmap bitmap = profileIconsBitmapCache.get(key);
if (bitmap == null) { if (bitmap == null) {
bitmap = Bitmap.createBitmap((int) circleSize, (int) circleSize, Bitmap.Config.ARGB_8888); bitmap = Bitmap.createBitmap((int) circleSize, (int) circleSize, Bitmap.Config.ARGB_8888);

View file

@ -34,12 +34,14 @@ public class MultiProfileGeometryWayDrawer extends GeometryWayDrawer<MultiProfil
style = (GeometryMultiProfileWayStyle) styles.get(i); style = (GeometryMultiProfileWayStyle) styles.get(i);
double lat = style.getIconLat(); double lat = style.getIconLat();
double lon = style.getIconLon(); double lon = style.getIconLon();
if (!Double.isNaN(lat) && !Double.isNaN(lon)) {
float x = tb.getPixXFromLatLon(lat, lon) - context.circleSize / 2; float x = tb.getPixXFromLatLon(lat, lon) - context.circleSize / 2;
float y = tb.getPixYFromLatLon(lat, lon) - context.circleSize / 2; float y = tb.getPixYFromLatLon(lat, lon) - context.circleSize / 2;
canvas.drawBitmap(style.getPointBitmap(), x, y, null); canvas.drawBitmap(style.getPointBitmap(), x, y, null);
} }
} }
} }
}
public void drawPathBorder(Canvas canvas, Path path, GeometryWayStyle<?> style) { public void drawPathBorder(Canvas canvas, Path path, GeometryWayStyle<?> style) {
if (style instanceof GeometryMultiProfileWayStyle) { if (style instanceof GeometryMultiProfileWayStyle) {