From 72df431b6402e46fd9916c8da52330cb47423190 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Wed, 25 Jul 2018 17:01:06 +0300 Subject: [PATCH] Refactor MapTextLayer --- .../net/osmand/plus/views/MapTextLayer.java | 89 ++++++++----------- 1 file changed, 39 insertions(+), 50 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/views/MapTextLayer.java b/OsmAnd/src/net/osmand/plus/views/MapTextLayer.java index 468b390a70..634fc6430c 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapTextLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapTextLayer.java @@ -18,50 +18,48 @@ import java.util.TreeMap; import gnu.trove.set.hash.TIntHashSet; public class MapTextLayer extends OsmandMapLayer { - - private Map> textObjects = new LinkedHashMap<>(); - public static final int TEXT_WRAP = 15; - public static final int TEXT_LINES = 3; + + private static final int TEXT_WRAP = 15; + private static final int TEXT_LINES = 3; + + private Map> textObjects = new LinkedHashMap<>(); private Paint paintTextIcon; private OsmandMapTileView view; private boolean alwaysVisible; - - - + public interface MapTextProvider { - + LatLon getTextLocation(T o); - + int getTextShift(T o, RotatedTileBox rb); - + String getText(T o); } - + public void putData(OsmandMapLayer ml, Collection objects) { - if(objects == null || objects.isEmpty()) { - textObjects.remove(ml); + if (objects == null || objects.isEmpty()) { + textObjects.remove(ml); } else { - if(ml instanceof MapTextProvider) { + if (ml instanceof MapTextProvider) { textObjects.put(ml, objects); } else { throw new IllegalArgumentException(); } } } - + public boolean isAlwaysVisible() { return alwaysVisible; } - + public void setAlwaysVisible(boolean alwaysVisible) { this.alwaysVisible = alwaysVisible; } - + public boolean isVisible() { return view.getSettings().SHOW_POI_LABEL.get() || isAlwaysVisible(); } - + @SuppressWarnings("unchecked") @Override public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) { @@ -72,14 +70,13 @@ public class MapTextLayer extends OsmandMapLayer { for (OsmandMapLayer l : textObjects.keySet()) { if (view.isLayerVisible(l)) { for (Object o : textObjects.get(l)) { - LatLon location = ((MapTextProvider) l).getTextLocation(o); - int x = (int) tileBox.getPixXFromLatLon(location.getLatitude(), location - .getLongitude()); - int y = (int) tileBox.getPixYFromLatLon(location.getLatitude(), location - .getLongitude()); + MapTextProvider provider = (MapTextProvider) l; + LatLon location = provider.getTextLocation(o); + int x = (int) tileBox.getPixXFromLatLon(location.getLatitude(), location.getLongitude()); + int y = (int) tileBox.getPixYFromLatLon(location.getLatitude(), location.getLongitude()); int tx = tileBox.getPixXFromLonNoRot(location.getLongitude()); int ty = tileBox.getPixYFromLatNoRot(location.getLatitude()); - String name = ((MapTextProvider) l).getText(o); + String name = provider.getText(o); if (name != null && name.length() > 0) { int lines = 0; while (lines < TEXT_LINES) { @@ -89,11 +86,8 @@ public class MapTextLayer extends OsmandMapLayer { } lines++; } - if (lines == 0) { - // drawWrappedText(canvas, "...", paintTextIcon.getTextSize(), x, y + r + 2 + - // paintTextIcon.getTextSize() / 2, 1); - } else { - int r = ((MapTextProvider) l).getTextShift(o, tileBox); + if (lines != 0) { + int r = provider.getTextShift(o, tileBox); drawWrappedText(canvas, name, paintTextIcon.getTextSize(), x, y + r + 2 + paintTextIcon.getTextSize() / 2, lines); while (lines > 0) { @@ -113,25 +107,25 @@ public class MapTextLayer extends OsmandMapLayer { // make numbers positive return ((((x + 10000) >> 4) + sx) << 16) | (((y + 10000) >> 4) + sy); } - + private void drawWrappedText(Canvas cv, String text, float textSize, float x, float y, int lines) { - if(text.length() > TEXT_WRAP){ + if (text.length() > TEXT_WRAP) { int start = 0; int end = text.length(); int lastSpace = -1; int line = 0; int pos = 0; int limit = 0; - while(pos < end && (line < lines)){ + while (pos < end && (line < lines)) { lastSpace = -1; limit += TEXT_WRAP; - while(pos < limit && pos < end){ - if(!Character.isLetterOrDigit(text.charAt(pos))){ + while (pos < limit && pos < end) { + if (!Character.isLetterOrDigit(text.charAt(pos))) { lastSpace = pos; } pos++; } - if(lastSpace == -1 || (pos == end)){ + if (lastSpace == -1 || (pos == end)) { drawShadowText(cv, text.substring(start, pos), x, y + line * (textSize + 2)); start = pos; } else { @@ -140,20 +134,18 @@ public class MapTextLayer extends OsmandMapLayer { subtext += ".."; } drawShadowText(cv, subtext, x, y + line * (textSize + 2)); - + start = lastSpace + 1; limit += (start - pos) - 1; } - + line++; - - } } else { drawShadowText(cv, text, x, y); } } - + private void drawShadowText(Canvas cv, String text, float centerX, float centerY) { int c = paintTextIcon.getColor(); paintTextIcon.setStyle(Style.STROKE); @@ -174,16 +166,14 @@ public class MapTextLayer extends OsmandMapLayer { paintTextIcon.setTextSize(13 * v.getDensity()); paintTextIcon.setTextAlign(Align.CENTER); paintTextIcon.setAntiAlias(true); - Map> textObjectsLoc = new TreeMap>(new Comparator() { - + Map> textObjectsLoc = new TreeMap<>(new Comparator() { @Override public int compare(OsmandMapLayer lhs, OsmandMapLayer rhs) { - if(view != null) { - float z1 = view.getZorder(lhs); - float z2 = view.getZorder(rhs); - return Float.compare(z1, z2); - - } + if (view != null) { + float z1 = view.getZorder(lhs); + float z2 = view.getZorder(rhs); + return Float.compare(z1, z2); + } return 0; } }); @@ -203,5 +193,4 @@ public class MapTextLayer extends OsmandMapLayer { public boolean drawInScreenPixels() { return true; } - }