public override TileData GetTile(TiledMapSession.Key key, IMapRenderer renderer, System.Threading.WaitCallback callback, object state) { if (key == Key.Root && !TileCache.ContainsKey(key)) { TileCache.Add(Key.Root, new TileData(new StandardBitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("TiledMaps.VirtualEarth.msvesatellite.png")))); } return(base.GetTile(key, renderer, callback, state)); }
public abstract TileData GetTile(TiledMapSession.Key key, IMapRenderer renderer, System.Threading.WaitCallback callback, object state);
public override TileData GetTile(TiledMapSession.Key key, IMapRenderer renderer, System.Threading.WaitCallback callback, object state) { TileData tileData = null; // try to get the tile if (TileCache.TryGetValue(key, out tileData)) { if (tileData != InvalidTile) { return(tileData); } TileCache.Remove(key); } // check if it is in the file cache string tilePath = GetTilePathForKey(key); if (File.Exists(tilePath)) { FileInfo finfo = new FileInfo(tilePath); if (DateTime.Now - finfo.CreationTime > new TimeSpan(2, 0, 0, 0)) { // tile is old, expire it File.Delete(tilePath); } else { TileCache[key] = null; ThreadPool.QueueUserWorkItem((o) => { try { using (FileStream fstream = new FileStream(tilePath, FileMode.Open)) { IMapDrawable bitmap = renderer.GetBitmapFromStream(this, fstream); TileCache[key] = new TileData(bitmap); if (callback != null) { callback(state); } } } catch (Exception) { File.Delete(tilePath); } }); return(null); } } // check if its a bad key Uri uri = GetUriForKey(key); if (uri != null) { // mark tile as being downloaded TileCache[key] = null; GetTileData data = new GetTileData(); data.Renderer = renderer; data.Key = key; data.Callback = callback; data.State = state; data.Uri = uri; ThreadPool.QueueUserWorkItem(new WaitCallback(GetTile), data); //GetTile(data); } return(tileData); }
public override TileData GetTile(TiledMapSession.Key key, IMapRenderer renderer, System.Threading.WaitCallback callback, object state) { // make sure there's something to blend if (mySessions.Count == 0) { return(null); } // see if we need to update this tile at all int currentBlend = 0; if (mySessionBlend.TryGetValue(key, out currentBlend) && currentBlend == myTotalBlend) { return(TileCache[key]); } // see how many tiles we can blend int canBlend = 0; for (int i = 0; i < mySessions.Count; i++) { if (!mySessionEnabled[i]) { continue; } TileData data = mySessions[i].GetTile(key, renderer, callback, state); if (data != null && data.Bitmap != null) { canBlend++; } } // if there's nothing new to blend, don't if (canBlend == currentBlend) { return(null); } mySessionBlend[key] = canBlend; // get the target bitmap ready if (RefreshBitmap == null) { throw new InvalidOperationException("You must provide a RefreshBitmap"); } StandardBitmap refreshBitmap = RefreshBitmap as StandardBitmap; int width = refreshBitmap.Width; int height = refreshBitmap.Height; StandardBitmap bitmap = new StandardBitmap(new Bitmap(width, height)); using (Graphics graphics = Graphics.FromImage(bitmap.Bitmap)) { refreshBitmap.Draw(graphics, new Rectangle(0, 0, width, height), new Rectangle(0, 0, width, height)); } // draw the bitmaps using (Graphics graphics = Graphics.FromImage(bitmap.Bitmap)) { for (int i = 0; i < mySessions.Count; i++) { TiledMapSession session = mySessions[i]; if (!mySessionEnabled[i]) { continue; } TileData tile = mySessions[i].GetTile(key, renderer, callback, state); if (tile == null) { continue; } IGraphicsDrawable tileBitmap = tile.Bitmap as IGraphicsDrawable; tileBitmap.Draw(graphics, new Rectangle(0, 0, 256, 256), new Rectangle(0, 0, 256, 256)); if (ClearBlendedTiles && canBlend == myTotalBlend) { tileBitmap.Dispose(); mySessions[i].TileCache.Remove(key); } } } TileData ret; if (!TileCache.TryGetValue(key, out ret)) { ret = new TileData(); TileCache.Add(key, ret); } else if (ret.Bitmap != null) { ret.Bitmap.Dispose(); ret.Bitmap = null; } // TODO: optimize to do a lock/read load MemoryStream mem = new MemoryStream(); bitmap.Bitmap.Save(mem, System.Drawing.Imaging.ImageFormat.Bmp); mem.Seek(0, SeekOrigin.Begin); ret.Bitmap = myRenderer.LoadBitmap(mem, false); return(ret); }