private static final int MAP_TILE_SIZE = 256;
/** x position (scale depends on zoom) */
- private long _xPosition = 0L;
+ private int _xPosition = 0;
/** y position (scale depends on zoom) */
- private long _yPosition = 0L;
+ private int _yPosition = 0;
/** Zoom level, from 2 to max */
private int _zoom = 12;
}
if (requiredZoom < 2) requiredZoom = 2;
// Set position
- _zoom = requiredZoom;
- _zoomFactor = 1 << _zoom;
+ setZoom(requiredZoom);
_xPosition = transformToPixels((inMinX + inMaxX) / 2.0);
_yPosition = transformToPixels((inMinY + inMaxY) / 2.0);
}
+ /**
+ * Ensure that zoom and zoomFactor remain in sync
+ * @param inZoom zoom level to set
+ */
+ private void setZoom(int inZoom)
+ {
+ _zoom = inZoom;
+ _zoomFactor = 1 << _zoom;
+ }
+
/**
* Zoom and pan to show the selected area
* @param inMinX minimum pixels X
break;
}
}
- _zoom = requiredZoom;
- _zoomFactor = 1 << _zoom;
+ setZoom(requiredZoom);
// Set position
_xPosition = (_xPosition - inWidth/2 + (inMinX + inMaxX) / 2) * multFactor;
_yPosition = (_yPosition - inHeight/2 + (inMinY + inMaxY) / 2) * multFactor;
* @param inValue value to transform
* @return pixels
*/
- private long transformToPixels(double inValue)
+ private int transformToPixels(double inValue)
{
return transformToPixels(inValue, _zoom);
}
* @param inZoom zoom value to use
* @return pixels
*/
- private static long transformToPixels(double inValue, int inZoom)
+ private static int transformToPixels(double inValue, int inZoom)
{
- return (long) (inValue * MAP_TILE_SIZE * (1 << inZoom));
+ return (int) (inValue * MAP_TILE_SIZE * (1 << inZoom));
}
/**
*/
public int getXFromCentre(double inValue)
{
- return (int) (transformToPixels(inValue) - _xPosition);
+ return transformToPixels(inValue) - _xPosition;
}
/**
*/
public int getYFromCentre(double inValue)
{
- return (int) (transformToPixels(inValue) - _yPosition);
+ return transformToPixels(inValue) - _yPosition;
}
/**
* @param inPosition position of point
* @return tile index for that point
*/
- private int getTileIndex(long inPosition)
+ private int getTileIndex(int inPosition)
{
- return (int) (inPosition / MAP_TILE_SIZE);
+ return inPosition / MAP_TILE_SIZE;
}
/**
* @param inPosition position of point
* @return pixel offset for that point
*/
- private int getDisplayOffset(long inPosition)
+ private int getDisplayOffset(int inPosition)
{
- return (int) (inPosition % MAP_TILE_SIZE);
- // Maybe >> 8 would be slightly faster?
+ return inPosition % MAP_TILE_SIZE;
+ // I thought that &255 would be slightly faster, but it gives the wrong result
}
/**
{
if (_zoom < MAX_ZOOM)
{
- _zoom++;
- _zoomFactor = 1 << _zoom;
+ setZoom(_zoom + 1);
_xPosition *= 2;
_yPosition *= 2;
}
*/
public void zoomOut()
{
- if (_zoom >= 2)
+ if (_zoom >= 3)
{
- _zoom--;
- _zoomFactor = 1 << _zoom;
+ setZoom(_zoom - 1);
_xPosition /= 2;
_yPosition /= 2;
}