]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/map/MemTileCacher.java
69b5f831b19f3ad65a7a97c2f9a96e51d3f5756e
[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[getArrayIndex(_tileX + GRID_SIZE/2 + 1, _tileY + i - GRID_SIZE/2)] = null;
51                         _tiles[getArrayIndex(_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
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                 //System.out.println("Getting array index for (" + inX + ", " + inY + ") where the centre is at ("  + _tileX + ", " + _tileY
77                 //      + ") and grid coords (" + _gridCentreX + ", " + _gridCentreY + ")");
78                 int x = getCacheCoordinate(inX - _tileX + _gridCentreX);
79                 int y = getCacheCoordinate(inY - _tileY + _gridCentreY);
80                 //System.out.println("Transformed to (" + x + ", " + y + ")");
81                 return (x + y * GRID_SIZE);
82         }
83
84         /**
85          * Clear all the cached images
86          */
87         public void clearAll()
88         {
89                 // Clear all images if zoom changed
90                 for (int i=0; i<_tiles.length; i++) {
91                         _tiles[i] = null;
92                 }
93         }
94
95         /**
96          * @param inX x index of tile
97          * @param inY y index of tile
98          * @return selected tile if already loaded, or null otherwise
99          */
100         public Image getTile(int inX, int inY)
101         {
102                 return _tiles[getArrayIndex(inX, inY)];
103         }
104
105         /**
106          * Save the specified tile at the given coordinates
107          * @param inTile image to save
108          * @param inX x coordinate of tile
109          * @param inY y coordinate of tile
110          */
111         public void setTile(Image inTile, int inX, int inY)
112         {
113                 _tiles[getArrayIndex(inX, inY)] = inTile;
114         }
115 }