X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fgui%2Fmap%2FMapTileManager.java;h=75f170a20f57df5ec5e84d3a2afa904a66d247e1;hb=4d5796d02a15808311c09448d79e6e7d1de9d636;hp=f7a281b1022ca8059a02e4fe9aef004431b464fc;hpb=649c5da6ee1bbc590699e11a92316ece2ea8512d;p=GpsPrune.git diff --git a/tim/prune/gui/map/MapTileManager.java b/tim/prune/gui/map/MapTileManager.java index f7a281b..75f170a 100644 --- a/tim/prune/gui/map/MapTileManager.java +++ b/tim/prune/gui/map/MapTileManager.java @@ -1,7 +1,6 @@ package tim.prune.gui.map; import java.awt.Image; -import java.awt.Toolkit; import java.awt.image.ImageObserver; import java.net.MalformedURLException; import java.net.URL; @@ -24,6 +23,8 @@ public class MapTileManager implements ImageObserver private int _numLayers = -1; /** Current zoom level */ private int _zoom = 0; + /** Number of tiles in each direction for this zoom level */ + private int _numTileIndices = 1; /** @@ -33,6 +34,8 @@ public class MapTileManager implements ImageObserver public MapTileManager(MapCanvas inParent) { _parent = inParent; + // Adjust the index of the selected map source + adjustSelectedMap(); resetConfig(); } @@ -45,6 +48,8 @@ public class MapTileManager implements ImageObserver public void centreMap(int inZoom, int inTileX, int inTileY) { _zoom = inZoom; + // Calculate number of tiles = 2^^zoom + _numTileIndices = 1 << _zoom; // Pass params onto all memory cachers if (_tempCaches != null) { for (int i=0; i<_tempCaches.length; i++) { @@ -69,8 +74,9 @@ public class MapTileManager implements ImageObserver public void clearMemoryCaches() { int numLayers = _mapSource.getNumLayers(); - if (_tempCaches == null || _tempCaches.length != numLayers) { - // Ccahers don't match, so need to create the right number of them + if (_tempCaches == null || _tempCaches.length != numLayers) + { + // Cachers don't match, so need to create the right number of them _tempCaches = new MemTileCacher[numLayers]; for (int i=0; i= prevNumFixed || sourceNum >= currNumFixed)) + { + sourceNum += (currNumFixed - prevNumFixed); + Config.setConfigInt(Config.KEY_MAPSOURCE_INDEX, sourceNum); + } + Config.setConfigInt(Config.KEY_NUM_FIXED_MAPS, currNumFixed); + } + /** * @return the number of layers in the map */ @@ -112,6 +138,10 @@ public class MapTileManager implements ImageObserver */ public Image getTile(int inLayer, int inX, int inY) { + if (inY < 0 || inY >= _numTileIndices) return null; + // Wrap tile indices which are too big or too small + inX = ((inX % _numTileIndices) + _numTileIndices) % _numTileIndices; + // Check first in memory cache for tile MemTileCacher tempCache = _tempCaches[inLayer]; // Should probably guard against nulls and array indexes here Image tile = tempCache.getTile(inX, inY); @@ -129,7 +159,7 @@ public class MapTileManager implements ImageObserver if (tile != null) { // Pass tile to memory cache - tempCache.setTile(tile, inX, inY); + tempCache.setTile(tile, inX, inY, _zoom); if (tile.getWidth(this) > 0) {return tile;} return null; } @@ -148,10 +178,9 @@ public class MapTileManager implements ImageObserver else { // Load image asynchronously, using observer - tile = Toolkit.getDefaultToolkit().createImage(tileUrl); - // Pass to memory cache - _tempCaches[inLayer].setTile(tile, inX, inY); - if (tile.getWidth(this) > 0) {return tile;} + // tile = Toolkit.getDefaultToolkit().createImage(tileUrl); + // In order to set the http user agent, need to use a TileDownloader instead + TileDownloader.triggerLoad(this, tileUrl, inLayer, inX, inY, _zoom); } } catch (MalformedURLException urle) {} // ignore @@ -178,4 +207,26 @@ public class MapTileManager implements ImageObserver } return !loaded; } + + /** + * Callback method from TileDownloader to let us know that an image has been loaded + * @param inTile Loaded Image object + * @param inLayer layer index from 0 + * @param inX x coordinate of tile + * @param inY y coordinate of tile + * @param inZoom zoom level of loaded image + */ + public void notifyImageLoaded(Image inTile, int inLayer, int inX, int inY, int inZoom) + { + if (inTile != null) + { + MemTileCacher tempCache = _tempCaches[inLayer]; // Should probably guard against nulls and array indexes here + if (tempCache.getTile(inX, inY) == null) + { + // Check with cache that the zoom level is still valid + tempCache.setTile(inTile, inX, inY, inZoom); + inTile.getWidth(this); // trigger imageUpdate when image is ready + } + } + } }