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 int _xPosition = 0;
13 /** y position (scale depends on zoom) */
14 private int _yPosition = 0;
16 /** Zoom level, from 2 to max */
17 private int _zoom = 12;
18 /** Factor to zoom by, 2 to the power of zoom */
19 private int _zoomFactor = 1 << _zoom;
20 /** Maximum zoom level */
21 private static final int MAX_ZOOM = 21;
25 * Zoom and pan to show the selected area
26 * @param inMinX minimum transformed X
27 * @param inMaxX maximum transformed X
28 * @param inMinY minimum transformed Y
29 * @param inMaxY maximum transformed Y
30 * @param inWidth width of display
31 * @param inHeight height of display
33 public void zoomToXY(double inMinX, double inMaxX, double inMinY, double inMaxY, int inWidth, int inHeight)
35 // System.out.println("Zooming to " + inMinX + ", " + inMaxX + ", " + inMinY + ", " + inMaxY + "; width=" + inWidth + ", height=" + inHeight);
36 double diffX = Math.abs(inMaxX - inMinX);
37 double diffY = Math.abs(inMaxY - inMinY);
38 // Find out what zoom level to go to
39 int requiredZoom = -1;
40 for (int currZoom = MAX_ZOOM; currZoom >= 2; currZoom--)
42 if (transformToPixels(diffX, currZoom) < inWidth
43 && transformToPixels(diffY, currZoom) < inHeight)
45 requiredZoom = currZoom;
49 if (requiredZoom < 2) requiredZoom = 2;
51 setZoom(requiredZoom);
52 _xPosition = transformToPixels((inMinX + inMaxX) / 2.0);
53 _yPosition = transformToPixels((inMinY + inMaxY) / 2.0);
57 * Ensure that zoom and zoomFactor remain in sync
58 * @param inZoom zoom level to set
60 private void setZoom(int inZoom)
63 _zoomFactor = 1 << _zoom;
67 * Zoom and pan to show the selected area
68 * @param inMinX minimum pixels X
69 * @param inMaxX maximum pixels X
70 * @param inMinY minimum pixels Y
71 * @param inMaxY maximum pixels Y
72 * @param inWidth width of display
73 * @param inHeight height of display
75 public void zoomToPixels(int inMinX, int inMaxX, int inMinY, int inMaxY, int inWidth, int inHeight)
77 // System.out.println("Current position is " + _xPosition + ", " + _yPosition);
78 int diffX = Math.abs(inMaxX - inMinX);
79 int diffY = Math.abs(inMaxY - inMinY);
80 // Find out what zoom level to go to
81 int requiredZoom = -1;
83 for (int currZoom = MAX_ZOOM; currZoom >= _zoom; currZoom--)
85 multFactor = 1 << (currZoom - _zoom);
86 if ((diffX * multFactor) < inWidth && (diffY * multFactor) < inHeight)
88 requiredZoom = currZoom;
92 setZoom(requiredZoom);
94 _xPosition = (_xPosition - inWidth/2 + (inMinX + inMaxX) / 2) * multFactor;
95 _yPosition = (_yPosition - inHeight/2 + (inMinY + inMaxY) / 2) * multFactor;
99 * Transform a given coordinate into pixels using the current zoom value
100 * @param inValue value to transform
103 private int transformToPixels(double inValue)
105 return transformToPixels(inValue, _zoom);
109 * Transform a given coordinate into pixels using the specified zoom value
110 * @param inValue value to transform
111 * @param inZoom zoom value to use
114 private static int transformToPixels(double inValue, int inZoom)
116 return (int) (inValue * MAP_TILE_SIZE * (1 << inZoom));
120 * Convert pixels back into x coordinates
121 * @param inPixelX x coordinate on screen
122 * @param inWidth current width of window
123 * @return x coordinate
125 public double getXFromPixels(int inPixelX, int inWidth)
127 return ((inPixelX - inWidth/2) + _xPosition) * 1.0 / MAP_TILE_SIZE / _zoomFactor;
131 * Convert pixels back into y coordinates
132 * @param inPixelY y coordinate on screen
133 * @param inHeight current height of window
134 * @return y coordinate
136 public double getYFromPixels(int inPixelY, int inHeight)
138 return ((inPixelY - inHeight/2) + _yPosition) * 1.0 / MAP_TILE_SIZE / _zoomFactor;
142 * Get the horizontal offset from the centre
143 * @param inValue value to transform
144 * @return number of pixels right (+ve) or left (-ve) from the centre
146 public int getXFromCentre(double inValue)
148 return transformToPixels(inValue) - _xPosition;
152 * Get the vertical offset from the centre
153 * @param inValue value to transform
154 * @return number of pixels up (+ve) or down (-ve) from the centre
156 public int getYFromCentre(double inValue)
158 return transformToPixels(inValue) - _yPosition;
162 * Convert a pixel value into a bounds value for sensitivity
163 * @param inPixels number of pixels
164 * @return bounds value to use for x,y checking
166 public double getBoundsFromPixels(int inPixels) {
167 return (inPixels * 1.0 / MAP_TILE_SIZE / _zoomFactor);
171 * Get the leftmost, rightmost, upper and lower index boundaries for the tiles to display
172 * @param inWidth width of window
173 * @param inHeight height of window
174 * @return tile indices as array left, right, up, down
176 public int[] getTileIndices(int inWidth, int inHeight)
178 int[] result = new int[4];
179 result[0] = getTileIndex(_xPosition - inWidth/2);
180 result[1] = getTileIndex(_xPosition + inWidth/2);
181 result[2] = getTileIndex(_yPosition - inHeight/2);
182 result[3] = getTileIndex(_yPosition + inHeight/2);
187 * Get the pixel offsets for the display
188 * @param inWidth width of window
189 * @param inHeight height of window
190 * @return offsets as x, y
192 public int[] getDisplayOffsets(int inWidth, int inHeight)
194 int[] result = new int[2];
195 result[0] = getDisplayOffset(_xPosition - inWidth/2);
196 result[1] = getDisplayOffset(_yPosition - inHeight/2);
201 * @return x index of the centre tile
203 public int getCentreTileX()
205 return getTileIndex(_xPosition);
209 * @return y index of the centre tile
211 public int getCentreTileY()
213 return getTileIndex(_yPosition);
217 * @param inPosition position of point
218 * @return tile index for that point
220 private int getTileIndex(int inPosition)
222 return inPosition / MAP_TILE_SIZE;
226 * @param inPosition position of point
227 * @return pixel offset for that point
229 private int getDisplayOffset(int inPosition)
231 return inPosition % MAP_TILE_SIZE;
232 // I thought that &255 would be slightly faster, but it gives the wrong result
240 if (_zoom < MAX_ZOOM)
251 public void zoomOut()
262 * @return current zoom level
270 * Pan map by the specified amount
271 * @param inDeltaX amount to pan right
272 * @param inDeltaY amount to pan down
274 public void pan(int inDeltaX, int inDeltaY)
276 // TODO: Check bounds?
277 _xPosition += inDeltaX;
278 _yPosition += inDeltaY;