1 package tim.prune.gui.map;
4 * Class to hold the current position of the map
6 public class MapPosition
8 /** Width and height of each tile of map */
9 private static final int MAP_TILE_SIZE = 256;
11 /** x position (scale depends on zoom) */
12 private long _xPosition = 0L;
13 /** y position (scale depends on zoom) */
14 private long _yPosition = 0L;
16 /** Zoom level, from 2 to 15 */
17 private int _zoom = 12;
18 /** Factor to zoom by, 2 to the power of zoom */
19 private int _zoomFactor = 1 << _zoom;
23 * Zoom and pan to show the selected area
24 * @param inMinX minimum transformed X
25 * @param inMaxX maximum transformed X
26 * @param inMinY minimum transformed Y
27 * @param inMaxY maximum transformed Y
28 * @param inWidth width of display
29 * @param inHeight height of display
31 public void zoomToXY(double inMinX, double inMaxX, double inMinY, double inMaxY, int inWidth, int inHeight)
33 // System.out.println("Zooming to " + inMinX + ", " + inMaxX + ", " + inMinY + ", " + inMaxY + "; width=" + inWidth + ", height=" + inHeight);
34 double diffX = Math.abs(inMaxX - inMinX);
35 double diffY = Math.abs(inMaxY - inMinY);
36 // Find out what zoom level to go to
37 int requiredZoom = -1;
38 for (int currZoom = 15; currZoom >= 2; currZoom--)
40 if (transformToPixels(diffX, currZoom) < inWidth
41 && transformToPixels(diffY, currZoom) < inHeight)
43 requiredZoom = currZoom;
47 if (requiredZoom < 2) requiredZoom = 2;
50 _zoomFactor = 1 << _zoom;
51 _xPosition = transformToPixels((inMinX + inMaxX) / 2.0);
52 _yPosition = transformToPixels((inMinY + inMaxY) / 2.0);
56 * Zoom and pan to show the selected area
57 * @param inMinX minimum pixels X
58 * @param inMaxX maximum pixels X
59 * @param inMinY minimum pixels Y
60 * @param inMaxY maximum pixels Y
61 * @param inWidth width of display
62 * @param inHeight height of display
64 public void zoomToPixels(int inMinX, int inMaxX, int inMinY, int inMaxY, int inWidth, int inHeight)
66 // System.out.println("Current position is " + _xPosition + ", " + _yPosition);
67 int diffX = Math.abs(inMaxX - inMinX);
68 int diffY = Math.abs(inMaxY - inMinY);
69 // Find out what zoom level to go to
70 int requiredZoom = -1;
72 for (int currZoom = 16; currZoom >= _zoom; currZoom--)
74 multFactor = 1 << (currZoom - _zoom);
75 if ((diffX * multFactor) < inWidth && (diffY * multFactor) < inHeight)
77 requiredZoom = currZoom;
82 _zoomFactor = 1 << _zoom;
84 _xPosition = (_xPosition - inWidth/2 + (inMinX + inMaxX) / 2) * multFactor;
85 _yPosition = (_yPosition - inHeight/2 + (inMinY + inMaxY) / 2) * multFactor;
89 * Transform a given coordinate into pixels using the current zoom value
90 * @param inValue value to transform
93 private long transformToPixels(double inValue)
95 return transformToPixels(inValue, _zoom);
99 * Transform a given coordinate into pixels using the specified zoom value
100 * @param inValue value to transform
101 * @param inZoom zoom value to use
104 private static long transformToPixels(double inValue, int inZoom)
106 return (long) (inValue * MAP_TILE_SIZE * (1 << inZoom));
110 * Convert pixels back into x coordinates
111 * @param inPixelX x coordinate on screen
112 * @param inWidth current width of window
113 * @return x coordinate
115 public double getXFromPixels(int inPixelX, int inWidth)
117 return ((inPixelX - inWidth/2) + _xPosition) * 1.0 / MAP_TILE_SIZE / _zoomFactor;
121 * Convert pixels back into y coordinates
122 * @param inPixelY y coordinate on screen
123 * @param inHeight current height of window
124 * @return y coordinate
126 public double getYFromPixels(int inPixelY, int inHeight)
128 return ((inPixelY - inHeight/2) + _yPosition) * 1.0 / MAP_TILE_SIZE / _zoomFactor;
132 * Get the horizontal offset from the centre
133 * @param inValue value to transform
134 * @return number of pixels right (+ve) or left (-ve) from the centre
136 public int getXFromCentre(double inValue)
138 return (int) (transformToPixels(inValue) - _xPosition);
142 * Get the vertical offset from the centre
143 * @param inValue value to transform
144 * @return number of pixels up (+ve) or down (-ve) from the centre
146 public int getYFromCentre(double inValue)
148 return (int) (transformToPixels(inValue) - _yPosition);
152 * Convert a pixel value into a bounds value for sensitivity
153 * @param inPixels number of pixels
154 * @return bounds value to use for x,y checking
156 public double getBoundsFromPixels(int inPixels) {
157 return (inPixels * 1.0 / MAP_TILE_SIZE / _zoomFactor);
161 * Get the leftmost, rightmost, upper and lower index boundaries for the tiles to display
162 * @param inWidth width of window
163 * @param inHeight height of window
164 * @return tile indices as array left, right, up, down
166 public int[] getTileIndices(int inWidth, int inHeight)
168 int[] result = new int[4];
169 result[0] = getTileIndex(_xPosition - inWidth/2);
170 result[1] = getTileIndex(_xPosition + inWidth/2);
171 result[2] = getTileIndex(_yPosition - inHeight/2);
172 result[3] = getTileIndex(_yPosition + inHeight/2);
177 * Get the pixel offsets for the display
178 * @param inWidth width of window
179 * @param inHeight height of window
180 * @return offsets as x, y
182 public int[] getDisplayOffsets(int inWidth, int inHeight)
184 int[] result = new int[2];
185 result[0] = getDisplayOffset(_xPosition - inWidth/2);
186 result[1] = getDisplayOffset(_yPosition - inHeight/2);
191 * @return x index of the centre tile
193 public int getCentreTileX()
195 return getTileIndex(_xPosition);
199 * @return y index of the centre tile
201 public int getCentreTileY()
203 return getTileIndex(_yPosition);
207 * @param inPosition position of point
208 * @return tile index for that point
210 private int getTileIndex(long inPosition)
212 return (int) (inPosition / MAP_TILE_SIZE);
216 * @param inPosition position of point
217 * @return pixel offset for that point
219 private int getDisplayOffset(long inPosition)
221 return (int) (inPosition % MAP_TILE_SIZE);
222 // Maybe >> 8 would be slightly faster?
233 _zoomFactor = 1 << _zoom;
242 public void zoomOut()
247 _zoomFactor = 1 << _zoom;
254 * @return current zoom level
262 * Pan map by the specified amount
263 * @param inDeltaX amount to pan right
264 * @param inDeltaY amount to pan down
266 public void pan(int inDeltaX, int inDeltaY)
268 // TODO: Check bounds?
269 _xPosition += inDeltaX;
270 _yPosition += inDeltaY;