]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/gui/map/MapPosition.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / gui / map / MapPosition.java
index 73c7ed922fe52ea7f0597aa2d75020af9255a1bf..25f05fc1a2a238370d8888ea4b807b7d3f5f8adb 100644 (file)
@@ -9,14 +9,16 @@ public class MapPosition
        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 15 */
+       /** Zoom level, from 2 to max */
        private int _zoom = 12;
        /** Factor to zoom by, 2 to the power of zoom */
        private int _zoomFactor = 1 << _zoom;
+       /** Maximum zoom level */
+       private static final int MAX_ZOOM = 21;
 
 
        /**
@@ -35,7 +37,7 @@ public class MapPosition
                double diffY = Math.abs(inMaxY - inMinY);
                // Find out what zoom level to go to
                int requiredZoom = -1;
-               for (int currZoom = 15; currZoom >= 2; currZoom--)
+               for (int currZoom = MAX_ZOOM; currZoom >= 2; currZoom--)
                {
                        if (transformToPixels(diffX, currZoom) < inWidth
                                && transformToPixels(diffY, currZoom) < inHeight)
@@ -46,12 +48,21 @@ public class MapPosition
                }
                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
@@ -69,7 +80,7 @@ public class MapPosition
                // Find out what zoom level to go to
                int requiredZoom = -1;
                int multFactor = 0;
-               for (int currZoom = 16; currZoom >= _zoom; currZoom--)
+               for (int currZoom = MAX_ZOOM; currZoom >= _zoom; currZoom--)
                {
                        multFactor = 1 << (currZoom - _zoom);
                        if ((diffX * multFactor) < inWidth && (diffY * multFactor) < inHeight)
@@ -78,8 +89,7 @@ public class MapPosition
                                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;
@@ -90,7 +100,7 @@ public class MapPosition
         * @param inValue value to transform
         * @return pixels
         */
-       private long transformToPixels(double inValue)
+       private int transformToPixels(double inValue)
        {
                return transformToPixels(inValue, _zoom);
        }
@@ -101,9 +111,9 @@ public class MapPosition
         * @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));
        }
 
        /**
@@ -135,7 +145,7 @@ public class MapPosition
         */
        public int getXFromCentre(double inValue)
        {
-               return (int) (transformToPixels(inValue) - _xPosition);
+               return transformToPixels(inValue) - _xPosition;
        }
 
        /**
@@ -145,7 +155,7 @@ public class MapPosition
         */
        public int getYFromCentre(double inValue)
        {
-               return (int) (transformToPixels(inValue) - _yPosition);
+               return transformToPixels(inValue) - _yPosition;
        }
 
        /**
@@ -207,19 +217,19 @@ public class MapPosition
         * @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
        }
 
        /**
@@ -227,10 +237,9 @@ public class MapPosition
         */
        public void zoomIn()
        {
-               if (_zoom < 16)
+               if (_zoom < MAX_ZOOM)
                {
-                       _zoom++;
-                       _zoomFactor = 1 << _zoom;
+                       setZoom(_zoom + 1);
                        _xPosition *= 2;
                        _yPosition *= 2;
                }
@@ -241,10 +250,9 @@ public class MapPosition
         */
        public void zoomOut()
        {
-               if (_zoom >= 2)
+               if (_zoom >= 3)
                {
-                       _zoom--;
-                       _zoomFactor = 1 << _zoom;
+                       setZoom(_zoom - 1);
                        _xPosition /= 2;
                        _yPosition /= 2;
                }