]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/map/MemTileCacher.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / gui / map / MemTileCacher.java
1 package tim.prune.gui.map;
2
3 import java.awt.Image;
4
5 /**
6  * Class to act as a memory-based map tile cache
7  * For caching of tiles on disk, see the DiskTileCacher class.
8  */
9 public class MemTileCacher
10 {
11         /** Array of images to hold tiles */
12         private Image[] _tiles = new Image[GRID_SIZE * GRID_SIZE];
13         /** Current zoom level */
14         private int _zoom = -1;
15         /** X coordinate of central tile */
16         private int _tileX = -1;
17         /** Y coordinate of central tile */
18         private int _tileY = -1;
19         /** X coord of grid centre */
20         private int _gridCentreX = 0;
21         /** Y coord of grid centre */
22         private int _gridCentreY = 0;
23
24         /** Grid size */
25         private static final int GRID_SIZE = 15;
26
27         /**
28          * Recentre the map and clear the cache
29          * @param inZoom zoom level
30          * @param inTileX x coord of central tile
31          * @param inTileY y coord of central tile
32          */
33         public void centreMap(int inZoom, int inTileX, int inTileY)
34         {
35                 int shift = Math.max(Math.abs(inTileX-_tileX), Math.abs(inTileY - _tileY));
36                 if (shift == 0) {return;}
37                 // Clear cache if either zoom has changed or map has jumped too far
38                 if (inZoom != _zoom || shift > GRID_SIZE/2)
39                 {
40                         _zoom = inZoom;
41                         clearAll();
42                 }
43                 _gridCentreX = getCacheCoordinate(_gridCentreX + inTileX - _tileX);
44                 _gridCentreY = getCacheCoordinate(_gridCentreY + inTileY - _tileY);
45                 _tileX = inTileX;
46                 _tileY = inTileY;
47                 // Mark boundaries as invalid
48                 for (int i=0; i<GRID_SIZE; i++)
49                 {
50                         _tiles[getArrayIndexNoWrap(_tileX + GRID_SIZE/2 + 1, _tileY + i - GRID_SIZE/2)] = null;
51                         _tiles[getArrayIndexNoWrap(_tileX + i - GRID_SIZE/2, _tileY + GRID_SIZE/2 + 1)] = null;
52                 }
53         }
54
55         /**
56          * Transform a coordinate from map tiles to array coordinates
57          * @param inTile coordinate of tile
58          * @return coordinate in array (wrapping around cache grid)
59          */
60         private static int getCacheCoordinate(int inTile)
61         {
62                 int tile = inTile;
63                 while (tile >= GRID_SIZE) {tile -= GRID_SIZE;}
64                 while (tile < 0) {tile += GRID_SIZE;}
65                 return tile;
66         }
67
68         /**
69          * Get the array index for the given coordinates, including regular lon wrapping
70          * @param inX x coord of tile
71          * @param inY y coord of tile
72          * @return array index
73          */
74         private int getArrayIndex(int inX, int inY)
75         {
76                 final int tileSpan = 1 << _zoom;
77                 int deltaX = (inX - _tileX);
78                 while (deltaX > (tileSpan/2))  {deltaX -= tileSpan;}
79                 while (deltaX < (-tileSpan/2)) {deltaX += tileSpan;}
80
81                 int x = getCacheCoordinate(deltaX + _gridCentreX);
82                 int y = getCacheCoordinate(inY - _tileY + _gridCentreY);
83                 return (x + y * GRID_SIZE);
84         }
85
86         /**
87          * Get the array index for the given coordinates, without wrapping x coords
88          * (used for deletion to avoid deleting the wrong tile)
89          * @param inX x coord of tile
90          * @param inY y coord of tile
91          * @return array index
92          */
93         private int getArrayIndexNoWrap(int inX, int inY)
94         {
95                 int x = getCacheCoordinate(inX - _tileX + _gridCentreX);
96                 int y = getCacheCoordinate(inY - _tileY + _gridCentreY);
97                 return (x + y * GRID_SIZE);
98         }
99
100         /**
101          * Clear all the cached images
102          */
103         public void clearAll()
104         {
105                 for (int i=0; i<_tiles.length; i++) {
106                         _tiles[i] = null;
107                 }
108         }
109
110         /**
111          * @param inX x index of tile
112          * @param inY y index of tile
113          * @return selected tile if already loaded, or null otherwise
114          */
115         public Image getTile(int inX, int inY)
116         {
117                 return _tiles[getArrayIndex(inX, inY)];
118         }
119
120         /**
121          * Save the specified tile at the given coordinates
122          * @param inTile image to save
123          * @param inX x coordinate of tile
124          * @param inY y coordinate of tile
125          * @param inZoom zoom level
126          */
127         public void setTile(Image inTile, int inX, int inY, int inZoom)
128         {
129                 // Ignore images received for a different zoom level
130                 if (inZoom == _zoom) {
131                         _tiles[getArrayIndex(inX, inY)] = inTile;
132                 }
133         }
134 }