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);
@ -39,10 +37,10 @@ public class MapTextLayer extends OsmandMapLayer {
} }
public void putData(OsmandMapLayer ml, Collection<?> objects) { public void putData(OsmandMapLayer ml, Collection<?> objects) {
if(objects == null || objects.isEmpty()) { if (objects == null || objects.isEmpty()) {
textObjects.remove(ml); textObjects.remove(ml);
} else { } else {
if(ml instanceof MapTextProvider) { if (ml instanceof MapTextProvider) {
textObjects.put(ml, objects); textObjects.put(ml, objects);
} else { } else {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@ -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) {
@ -115,23 +109,23 @@ public class MapTextLayer extends OsmandMapLayer {
} }
private void drawWrappedText(Canvas cv, String text, float textSize, float x, float y, int lines) { 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 start = 0;
int end = text.length(); int end = text.length();
int lastSpace = -1; int lastSpace = -1;
int line = 0; int line = 0;
int pos = 0; int pos = 0;
int limit = 0; int limit = 0;
while(pos < end && (line < lines)){ while (pos < end && (line < lines)) {
lastSpace = -1; lastSpace = -1;
limit += TEXT_WRAP; limit += TEXT_WRAP;
while(pos < limit && pos < end){ while (pos < limit && pos < end) {
if(!Character.isLetterOrDigit(text.charAt(pos))){ if (!Character.isLetterOrDigit(text.charAt(pos))) {
lastSpace = pos; lastSpace = pos;
} }
pos++; pos++;
} }
if(lastSpace == -1 || (pos == end)){ if (lastSpace == -1 || (pos == end)) {
drawShadowText(cv, text.substring(start, pos), x, y + line * (textSize + 2)); drawShadowText(cv, text.substring(start, pos), x, y + line * (textSize + 2));
start = pos; start = pos;
} else { } else {
@ -146,8 +140,6 @@ public class MapTextLayer extends OsmandMapLayer {
} }
line++; line++;
} }
} else { } else {
drawShadowText(cv, text, x, y); drawShadowText(cv, text, x, y);
@ -174,16 +166,14 @@ 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;
} }
} }