Refactor GPXLayer and FavouritesLayer

This commit is contained in:
Alex Sytnyk 2018-09-26 20:56:13 +03:00
parent ad3384d4b2
commit 2d1f55a798
3 changed files with 66 additions and 75 deletions

View file

@ -115,14 +115,6 @@ public class FavoriteImageDrawable extends Drawable {
}
}
public void drawBitmapInCenter(Canvas canvas, int x, int y) {
int dx = x - getIntrinsicWidth() / 2;
int dy = y - getIntrinsicHeight() / 2;
canvas.translate(dx, dy);
draw(canvas);
canvas.translate(-dx, -dy);
}
public void drawBitmapInCenter(Canvas canvas, float x, float y) {
float dx = x - getIntrinsicWidth() / 2f;
float dy = y - getIntrinsicHeight() / 2f;

View file

@ -12,6 +12,8 @@ import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Pair;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
@ -19,6 +21,7 @@ import net.osmand.data.QuadRect;
import net.osmand.data.QuadTree;
import net.osmand.data.RotatedTileBox;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.FavouritesDbHelper.FavoriteGroup;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.OsmandSettings;
@ -52,10 +55,6 @@ public class FavouritesLayer extends OsmandMapLayer implements ContextMenuLayer.
protected String getObjName() {
return view.getContext().getString(R.string.favorite);
}
protected List<? extends FavouritePoint> getPoints() {
return favorites.getFavouritePoints();
}
@Override
public void initLayer(OsmandMapTileView view) {
@ -90,22 +89,15 @@ public class FavouritesLayer extends OsmandMapLayer implements ContextMenuLayer.
public boolean drawInScreenPixels() {
return true;
}
@Override
public void onDraw(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) {
if (contextMenuLayer.getMoveableObject() instanceof FavouritePoint) {
FavouritePoint objectInMotion = (FavouritePoint) contextMenuLayer.getMoveableObject();
FavoriteImageDrawable fid;
MapMarker mapMarker = mapMarkersHelper.getMapMarker(objectInMotion);
if (mapMarker != null) {
fid = FavoriteImageDrawable.getOrCreateSyncedIcon(view.getContext(), objectInMotion.getColor());
} else {
fid = FavoriteImageDrawable.getOrCreate(view.getContext(),
objectInMotion.getColor(), true);
}
PointF pf = contextMenuLayer.getMovableCenterPoint(tileBox);
fid.drawBitmapInCenter(canvas, pf.x, pf.y);
MapMarker mapMarker = mapMarkersHelper.getMapMarker(objectInMotion);
drawBigPoint(canvas, objectInMotion, pf.x, pf.y, mapMarker);
}
}
@ -120,31 +112,44 @@ public class FavouritesLayer extends OsmandMapLayer implements ContextMenuLayer.
// request to load
final QuadRect latLonBounds = tileBox.getLatLonBounds();
List<FavouritePoint> fullObjects = new ArrayList<>();
List<LatLon> fullObjectsLatLon = new ArrayList<>();
List<LatLon> smallObjectsLatLon = new ArrayList<>();
for (FavouritePoint o : getPoints()) {
if (!o.isVisible()) {
continue;
}
float x = tileBox.getPixXFromLatLon(o.getLatitude(), o.getLongitude());
float y = tileBox.getPixYFromLatLon(o.getLatitude(), o.getLongitude());
for (FavoriteGroup group : favorites.getFavoriteGroups()) {
List<Pair<FavouritePoint, MapMarker>> fullObjects = new ArrayList<>();
boolean synced = mapMarkersHelper.getMarkersGroup(group) != null;
for (FavouritePoint o : group.points) {
double lat = o.getLatitude();
double lon = o.getLongitude();
if (o.isVisible() && o != contextMenuLayer.getMoveableObject()
&& lat >= latLonBounds.bottom && lat <= latLonBounds.top
&& lon >= latLonBounds.left && lon <= latLonBounds.right) {
MapMarker marker = null;
if (synced) {
if ((marker = mapMarkersHelper.getMapMarker(o)) == null) {
continue;
}
}
cache.add(o);
float x = tileBox.getPixXFromLatLon(lat, lon);
float y = tileBox.getPixYFromLatLon(lat, lon);
if (intersects(boundIntersections, x, y, iconSize, iconSize)) {
@ColorInt
int col = o.getColor() == 0 || o.getColor() == Color.BLACK ? defaultColor : o.getColor();
paintIcon.setColorFilter(new PorterDuffColorFilter(col, PorterDuff.Mode.MULTIPLY));
canvas.drawBitmap(pointSmall, x - pointSmall.getWidth() / 2, y - pointSmall.getHeight() / 2, paintIcon);
smallObjectsLatLon.add(new LatLon(o.getLatitude(), o.getLongitude()));
} else {
fullObjects.add(o);
fullObjectsLatLon.add(new LatLon(o.getLatitude(), o.getLongitude()));
if (intersects(boundIntersections, x, y, iconSize, iconSize)) {
@ColorInt
int col = o.getColor() == 0 || o.getColor() == Color.BLACK ? defaultColor : o.getColor();
paintIcon.setColorFilter(new PorterDuffColorFilter(col, PorterDuff.Mode.MULTIPLY));
canvas.drawBitmap(pointSmall, x - pointSmall.getWidth() / 2, y - pointSmall.getHeight() / 2, paintIcon);
smallObjectsLatLon.add(new LatLon(lat, lon));
} else {
fullObjects.add(new Pair<>(o, marker));
fullObjectsLatLon.add(new LatLon(lat, lon));
}
}
}
}
for (FavouritePoint o : fullObjects) {
if (o != contextMenuLayer.getMoveableObject()) {
MapMarker mapMarker = mapMarkersHelper.getMapMarker(o);
drawPoint(canvas, tileBox, latLonBounds, o, mapMarker);
for (Pair<FavouritePoint, MapMarker> pair : fullObjects) {
FavouritePoint o = pair.first;
float x = tileBox.getPixXFromLatLon(o.getLatitude(), o.getLongitude());
float y = tileBox.getPixYFromLatLon(o.getLatitude(), o.getLongitude());
drawBigPoint(canvas, o, x, y, pair.second);
}
}
this.fullObjectsLatLon = fullObjectsLatLon;
@ -157,20 +162,14 @@ public class FavouritesLayer extends OsmandMapLayer implements ContextMenuLayer.
}
private void drawPoint(Canvas canvas, RotatedTileBox tileBox, final QuadRect latLonBounds, FavouritePoint o, MapMarker mapMarker) {
if (o.isVisible() && o.getLatitude() >= latLonBounds.bottom && o.getLatitude() <= latLonBounds.top && o.getLongitude() >= latLonBounds.left
&& o.getLongitude() <= latLonBounds.right ) {
cache.add(o);
int x = (int) tileBox.getPixXFromLatLon(o.getLatitude(), o.getLongitude());
int y = (int) tileBox.getPixYFromLatLon(o.getLatitude(), o.getLongitude());
FavoriteImageDrawable fid;
if (mapMarker != null) {
fid = FavoriteImageDrawable.getOrCreateSyncedIcon(view.getContext(), o.getColor());
} else {
fid = FavoriteImageDrawable.getOrCreate(view.getContext(), o.getColor(), true);
}
fid.drawBitmapInCenter(canvas, x, y);
private void drawBigPoint(Canvas canvas, FavouritePoint o, float x, float y, @Nullable MapMarker marker) {
FavoriteImageDrawable fid;
if (marker != null) {
fid = FavoriteImageDrawable.getOrCreateSyncedIcon(view.getContext(), o.getColor());
} else {
fid = FavoriteImageDrawable.getOrCreate(view.getContext(), o.getColor(), true);
}
fid.drawBitmapInCenter(canvas, x, y);
}
@ -183,7 +182,7 @@ public class FavouritesLayer extends OsmandMapLayer implements ContextMenuLayer.
int r = getDefaultRadiusPoi(tb);
int ex = (int) point.x;
int ey = (int) point.y;
for (FavouritePoint n : getPoints()) {
for (FavouritePoint n : favorites.getFavouritePoints()) {
getFavFromPoint(tb, res, r, ex, ey, n);
}
}

View file

@ -19,6 +19,8 @@ import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Pair;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.data.QuadRect;
@ -34,6 +36,7 @@ import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.MapMarkersHelper.MapMarkersGroup;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings.CommonPreference;
@ -177,10 +180,11 @@ public class GPXLayer extends OsmandMapLayer implements ContextMenuLayer.IContex
public void onDraw(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) {
if (contextMenuLayer.getMoveableObject() instanceof WptPt) {
WptPt objectInMotion = (WptPt) contextMenuLayer.getMoveableObject();
PointF pf = contextMenuLayer.getMovableCenterPoint(tileBox);
SelectedGpxFile gpxFile = pointFileMap.get(objectInMotion);
if (gpxFile != null) {
drawBigPoint(canvas, objectInMotion, getFileColor(gpxFile), pf.x, pf.y, isSyncedWithMarkers(gpxFile));
PointF pf = contextMenuLayer.getMovableCenterPoint(tileBox);
MapMarker mapMarker = mapMarkersHelper.getMapMarker(objectInMotion);
drawBigPoint(canvas, objectInMotion, getFileColor(gpxFile), pf.x, pf.y, mapMarker);
}
}
}
@ -358,17 +362,16 @@ public class GPXLayer extends OsmandMapLayer implements ContextMenuLayer.IContex
// request to load
final QuadRect latLonBounds = tileBox.getLatLonBounds();
for (SelectedGpxFile g : selectedGPXFiles) {
List<WptPt> pts = getListStarPoints(g);
List<WptPt> fullObjects = new ArrayList<>();
@ColorInt
List<Pair<WptPt, MapMarker>> fullObjects = new ArrayList<>();
int fileColor = getFileColor(g);
boolean synced = isSyncedWithMarkers(g);
for (WptPt o : pts) {
boolean synced = mapMarkersHelper.getMarkersGroup(g.getGpxFile()) != null;
for (WptPt o : getListStarPoints(g)) {
if (o.lat >= latLonBounds.bottom && o.lat <= latLonBounds.top
&& o.lon >= latLonBounds.left && o.lon <= latLonBounds.right
&& o != contextMenuLayer.getMoveableObject()) {
MapMarker marker = null;
if (synced) {
if (mapMarkersHelper.getMapMarker(o) == null) {
if ((marker = mapMarkersHelper.getMapMarker(o)) == null) {
continue;
}
}
@ -383,16 +386,17 @@ public class GPXLayer extends OsmandMapLayer implements ContextMenuLayer.IContex
canvas.drawBitmap(pointSmall, x - pointSmall.getWidth() / 2, y - pointSmall.getHeight() / 2, paintIcon);
smallObjectsLatLon.add(new LatLon(o.lat, o.lon));
} else {
fullObjects.add(o);
fullObjects.add(new Pair<>(o, marker));
fullObjectsLatLon.add(new LatLon(o.lat, o.lon));
}
}
pointFileMap.put(o, g);
}
for (WptPt o : fullObjects) {
for (Pair<WptPt, MapMarker> pair : fullObjects) {
WptPt o = pair.first;
float x = tileBox.getPixXFromLatLon(o.lat, o.lon);
float y = tileBox.getPixYFromLatLon(o.lat, o.lon);
drawBigPoint(canvas, o, fileColor, x, y, synced);
drawBigPoint(canvas, o, fileColor, x, y, pair.second);
}
}
if (trackChartPoints != null) {
@ -457,10 +461,10 @@ public class GPXLayer extends OsmandMapLayer implements ContextMenuLayer.IContex
return g.getColor() == 0 ? defPointColor : g.getColor();
}
private void drawBigPoint(Canvas canvas, WptPt o, int fileColor, float x, float y, boolean synced) {
private void drawBigPoint(Canvas canvas, WptPt o, int fileColor, float x, float y, @Nullable MapMarker marker) {
int pointColor = getPointColor(o, fileColor);
FavoriteImageDrawable fid;
if (synced) {
if (marker != null) {
fid = FavoriteImageDrawable.getOrCreateSyncedIcon(view.getContext(), pointColor);
} else {
fid = FavoriteImageDrawable.getOrCreate(view.getContext(), pointColor, true);
@ -531,10 +535,6 @@ public class GPXLayer extends OsmandMapLayer implements ContextMenuLayer.IContex
return g.getGpxFile().getPoints();
}
private boolean isSyncedWithMarkers(SelectedGpxFile g) {
return mapMarkersHelper.getMarkersGroup(g.getGpxFile()) != null;
}
private boolean calculateBelongs(int ex, int ey, int objx, int objy, int radius) {
return (Math.abs(objx - ex) <= radius * 1.5 && Math.abs(objy - ey) <= radius * 1.5);
// return Math.abs(objx - ex) <= radius && (ey - objy) <= radius / 2 && (objy - ey) <= 3 * radius ;