Refactor MapTextLayer

This commit is contained in:
Alex Sytnyk 2018-07-25 17:01:06 +03:00
parent ffd478431b
commit 72df431b64

View file

@ -19,16 +19,14 @@ import gnu.trove.set.hash.TIntHashSet;
public class MapTextLayer extends OsmandMapLayer { public class MapTextLayer extends OsmandMapLayer {
private Map<OsmandMapLayer, private static final int TEXT_WRAP = 15;
Collection<?>> textObjects = new LinkedHashMap<>(); private static final int TEXT_LINES = 3;
public static final int TEXT_WRAP = 15;
public static final int TEXT_LINES = 3; private Map<OsmandMapLayer, Collection<?>> textObjects = new LinkedHashMap<>();
private Paint paintTextIcon; private Paint paintTextIcon;
private OsmandMapTileView view; private OsmandMapTileView view;
private boolean alwaysVisible; private boolean alwaysVisible;
public interface MapTextProvider<T> { public interface MapTextProvider<T> {
LatLon getTextLocation(T o); LatLon getTextLocation(T o);
@ -72,14 +70,13 @@ public class MapTextLayer extends OsmandMapLayer {
for (OsmandMapLayer l : textObjects.keySet()) { for (OsmandMapLayer l : textObjects.keySet()) {
if (view.isLayerVisible(l)) { if (view.isLayerVisible(l)) {
for (Object o : textObjects.get(l)) { for (Object o : textObjects.get(l)) {
LatLon location = ((MapTextProvider) l).getTextLocation(o); MapTextProvider provider = (MapTextProvider) l;
int x = (int) tileBox.getPixXFromLatLon(location.getLatitude(), location LatLon location = provider.getTextLocation(o);
.getLongitude()); int x = (int) tileBox.getPixXFromLatLon(location.getLatitude(), location.getLongitude());
int y = (int) tileBox.getPixYFromLatLon(location.getLatitude(), location int y = (int) tileBox.getPixYFromLatLon(location.getLatitude(), location.getLongitude());
.getLongitude());
int tx = tileBox.getPixXFromLonNoRot(location.getLongitude()); int tx = tileBox.getPixXFromLonNoRot(location.getLongitude());
int ty = tileBox.getPixYFromLatNoRot(location.getLatitude()); int ty = tileBox.getPixYFromLatNoRot(location.getLatitude());
String name = ((MapTextProvider) l).getText(o); String name = provider.getText(o);
if (name != null && name.length() > 0) { if (name != null && name.length() > 0) {
int lines = 0; int lines = 0;
while (lines < TEXT_LINES) { while (lines < TEXT_LINES) {
@ -89,11 +86,8 @@ public class MapTextLayer extends OsmandMapLayer {
} }
lines++; lines++;
} }
if (lines == 0) { if (lines != 0) {
// drawWrappedText(canvas, "...", paintTextIcon.getTextSize(), x, y + r + 2 + int r = provider.getTextShift(o, tileBox);
// paintTextIcon.getTextSize() / 2, 1);
} else {
int r = ((MapTextProvider) l).getTextShift(o, tileBox);
drawWrappedText(canvas, name, paintTextIcon.getTextSize(), x, drawWrappedText(canvas, name, paintTextIcon.getTextSize(), x,
y + r + 2 + paintTextIcon.getTextSize() / 2, lines); y + r + 2 + paintTextIcon.getTextSize() / 2, lines);
while (lines > 0) { while (lines > 0) {
@ -146,8 +140,6 @@ public class MapTextLayer extends OsmandMapLayer {
} }
line++; line++;
} }
} else { } else {
drawShadowText(cv, text, x, y); drawShadowText(cv, text, x, y);
@ -174,15 +166,13 @@ public class MapTextLayer extends OsmandMapLayer {
paintTextIcon.setTextSize(13 * v.getDensity()); paintTextIcon.setTextSize(13 * v.getDensity());
paintTextIcon.setTextAlign(Align.CENTER); paintTextIcon.setTextAlign(Align.CENTER);
paintTextIcon.setAntiAlias(true); paintTextIcon.setAntiAlias(true);
Map<OsmandMapLayer, Collection<?>> textObjectsLoc = new TreeMap<OsmandMapLayer, Collection<?>>(new Comparator<OsmandMapLayer>() { Map<OsmandMapLayer, Collection<?>> textObjectsLoc = new TreeMap<>(new Comparator<OsmandMapLayer>() {
@Override @Override
public int compare(OsmandMapLayer lhs, OsmandMapLayer rhs) { public int compare(OsmandMapLayer lhs, OsmandMapLayer rhs) {
if (view != null) { if (view != null) {
float z1 = view.getZorder(lhs); float z1 = view.getZorder(lhs);
float z2 = view.getZorder(rhs); float z2 = view.getZorder(rhs);
return Float.compare(z1, z2); return Float.compare(z1, z2);
} }
return 0; return 0;
} }
@ -203,5 +193,4 @@ public class MapTextLayer extends OsmandMapLayer {
public boolean drawInScreenPixels() { public boolean drawInScreenPixels() {
return true; return true;
} }
} }