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 {
private Map<OsmandMapLayer,
Collection<?>> 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<OsmandMapLayer, Collection<?>> textObjects = new LinkedHashMap<>();
private Paint paintTextIcon;
private OsmandMapTileView view;
private boolean alwaysVisible;
public interface MapTextProvider<T> {
LatLon getTextLocation(T o);
@ -39,10 +37,10 @@ public class MapTextLayer extends OsmandMapLayer {
}
public void putData(OsmandMapLayer ml, Collection<?> objects) {
if(objects == null || objects.isEmpty()) {
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();
@ -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) {
@ -115,23 +109,23 @@ public class MapTextLayer extends OsmandMapLayer {
}
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 {
@ -146,8 +140,6 @@ public class MapTextLayer extends OsmandMapLayer {
}
line++;
}
} else {
drawShadowText(cv, text, x, y);
@ -174,16 +166,14 @@ public class MapTextLayer extends OsmandMapLayer {
paintTextIcon.setTextSize(13 * v.getDensity());
paintTextIcon.setTextAlign(Align.CENTER);
paintTextIcon.setAntiAlias(true);
Map<OsmandMapLayer, Collection<?>> textObjectsLoc = new TreeMap<OsmandMapLayer, Collection<?>>(new Comparator<OsmandMapLayer>() {
Map<OsmandMapLayer, Collection<?>> textObjectsLoc = new TreeMap<>(new Comparator<OsmandMapLayer>() {
@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;
}
}