Skip to content

Commit

Permalink
Scale bar: implement custom positioning, add position offset, #597
Browse files Browse the repository at this point in the history
  • Loading branch information
devemux86 committed May 28, 2015
1 parent beded48 commit b8792e1
Showing 1 changed file with 96 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2010, 2011, 2012, 2013 mapsforge.org
* Copyright 2014 Ludwig M Brinckmann
* Copyright 2014 devemux86
* Copyright 2014, 2015 devemux86
* Copyright 2014 Erik Duisters
*
* This program is free software: you can redistribute it and/or modify it under the
Expand Down Expand Up @@ -32,20 +32,30 @@
* A MapScaleBar displays the ratio of a distance on the map to the corresponding distance on the ground.
*/
public abstract class MapScaleBar {
private static final int MARGIN_BOTTOM = 0;
private static final int MARGIN_LEFT = 5;
public static enum ScaleBarPosition { BOTTOM_CENTER, BOTTOM_LEFT, BOTTOM_RIGHT, TOP_CENTER, TOP_LEFT, TOP_RIGHT }

/**
* Default position of the scale bar.
*/
private static final ScaleBarPosition DEFAULT_SCALE_BAR_POSITION = ScaleBarPosition.BOTTOM_LEFT;

private static final int DEFAULT_HORIZONTAL_MARGIN = 5;
private static final int DEFAULT_VERTICAL_MARGIN = 0;
private static final double LATITUDE_REDRAW_THRESHOLD = 0.2;

private MapViewPosition mapViewPosition;
private MapViewDimension mapViewDimension;
private MapPosition prevMapPosition;
protected DisplayModel displayModel;
protected GraphicFactory graphicFactory;
private Bitmap mapScaleBitmap;
protected final Canvas mapScaleCanvas;
protected final DisplayModel displayModel;
protected DistanceUnitAdapter distanceUnitAdapter;
protected boolean visible;
protected final GraphicFactory graphicFactory;
private final Bitmap mapScaleBitmap;
private final Canvas mapScaleCanvas;
private final MapViewDimension mapViewDimension;
private final MapViewPosition mapViewPosition;
private int marginHorizontal;
private int marginVertical;
private MapPosition prevMapPosition;
protected boolean redrawNeeded;
private ScaleBarPosition scaleBarPosition;
private boolean visible;

/**
* Internal class used by calculateScaleBarLengthAndValue
Expand All @@ -69,6 +79,10 @@ public MapScaleBar(MapViewPosition mapViewPosition, MapViewDimension mapViewDime
this.mapScaleBitmap = graphicFactory.createBitmap((int) (width * this.displayModel.getScaleFactor()),
(int) (height * this.displayModel.getScaleFactor()));

this.marginHorizontal = DEFAULT_HORIZONTAL_MARGIN;
this.marginVertical = DEFAULT_VERTICAL_MARGIN;
this.scaleBarPosition = DEFAULT_SCALE_BAR_POSITION;

this.mapScaleCanvas = graphicFactory.createCanvas();
this.mapScaleCanvas.setBitmap(this.mapScaleBitmap);
this.distanceUnitAdapter = MetricUnitAdapter.INSTANCE;
Expand Down Expand Up @@ -122,6 +136,73 @@ public void setDistanceUnitAdapter(DistanceUnitAdapter distanceUnitAdapter) {
this.redrawNeeded = true;
}

public int getMarginHorizontal() {
return marginHorizontal;
}

public void setMarginHorizontal(int marginHorizontal) {
if (this.marginHorizontal != marginHorizontal) {
this.marginHorizontal = marginHorizontal;
this.redrawNeeded = true;
}
}

public int getMarginVertical() {
return marginVertical;
}

public void setMarginVertical(int marginVertical) {
if (this.marginVertical != marginVertical) {
this.marginVertical = marginVertical;
this.redrawNeeded = true;
}
}

public ScaleBarPosition getScaleBarPosition() {
return scaleBarPosition;
}

public void setScaleBarPosition(ScaleBarPosition scaleBarPosition) {
if (this.scaleBarPosition != scaleBarPosition) {
this.scaleBarPosition = scaleBarPosition;
this.redrawNeeded = true;
}
}

private int calculatePositionLeft(int left, int right, int width) {
switch (scaleBarPosition) {
case BOTTOM_LEFT:
case TOP_LEFT:
return marginHorizontal;

case BOTTOM_CENTER:
case TOP_CENTER:
return (right - left - width) / 2;

case BOTTOM_RIGHT:
case TOP_RIGHT:
return right - left - width - marginHorizontal;
}

throw new IllegalArgumentException("unknown horizontal position: " + scaleBarPosition);
}

private int calculatePositionTop(int top, int bottom, int height) {
switch (scaleBarPosition) {
case TOP_CENTER:
case TOP_LEFT:
case TOP_RIGHT:
return marginVertical;

case BOTTOM_CENTER:
case BOTTOM_LEFT:
case BOTTOM_RIGHT:
return bottom - top - height - marginVertical;
}

throw new IllegalArgumentException("unknown vertical position: " + scaleBarPosition);
}

/**
* Calculates the required length and value of the scalebar
*
Expand Down Expand Up @@ -180,8 +261,10 @@ public void draw(GraphicContext graphicContext) {
this.redrawNeeded = false;
}

int top = this.mapViewDimension.getDimension().height - this.mapScaleBitmap.getHeight() - MARGIN_BOTTOM;
graphicContext.drawBitmap(this.mapScaleBitmap, MARGIN_LEFT, top);
int positionLeft = calculatePositionLeft(0, this.mapViewDimension.getDimension().width, this.mapScaleBitmap.getWidth());
int positionTop = calculatePositionTop(0, this.mapViewDimension.getDimension().height, this.mapScaleBitmap.getHeight());

graphicContext.drawBitmap(this.mapScaleBitmap, positionLeft, positionTop);
}

/**
Expand Down

0 comments on commit b8792e1

Please sign in to comment.