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)) return tileData; // 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 { try { using (FileStream fstream = new FileStream(tilePath, FileMode.Open)) { IMapDrawable bitmap = renderer.GetBitmapFromStream(this, fstream); return TileCache[key] = new TileData(bitmap); } } catch (Exception) { File.Delete(tilePath); } } } // 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) { 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; }
public int DrawMap(IMapRenderer renderer, int x, int y, int width, int height, WaitCallback callback, object state) { int unavailable = 0; // approximate the the top left tile (it may be off by 1), but the loop // below will kept it from being drawn int midX = x + width / 2 - myCenterOffset.X; int midY = y + height / 2 - myCenterOffset.Y; int xTiles = (midX - x) / 256 + 1; int yTiles = (midY - y) / 256 + 1; Key currentXKey = new Key(myCenterTile.X - xTiles, myCenterTile.Y - yTiles, myZoom); int xStart = midX - xTiles * 256; int yStart = midY - yTiles * 256; Rectangle rect = new Rectangle(x, y, width, height); int tickCount = Environment.TickCount; for (int currentX = xStart; currentX < x + width; currentX += 256, currentXKey.X++) { Key key = currentXKey; for (int currentY = yStart; currentY < y + height; currentY += 256, key.Y++) { IMapDrawable tile = null; // find the intersect region of the tile that we are drawing Rectangle tileRect = new Rectangle(currentX, currentY, 256, 256); tileRect.Intersect(rect); Rectangle sourceRect = new Rectangle(tileRect.X - currentX, tileRect.Y - currentY, tileRect.Width, tileRect.Height); // dont draw off the map tiles if (!key.IsValid) { // dont draw gray rect if we're drawing transparent if (!HasAlpha) { renderer.FillRectangle(BackColor, tileRect); } continue; } // first try to get the tile from the tileData TileData tileData = GetTile(key, renderer, callback, state); if (tileData != null) { tile = tileData.Bitmap; tileData.LastUsed = tickCount; } if (tile == null) { // tile not available, so try to generate a tile from child tiles unavailable++; Key childKey = new Key(key.X * 2, key.Y * 2, key.Zoom + 1); Key tl = childKey; Key tr = new Key(childKey.X + 1, childKey.Y, childKey.Zoom); Key br = new Key(childKey.X + 1, childKey.Y + 1, childKey.Zoom); Key bl = new Key(childKey.X, childKey.Y + 1, childKey.Zoom); TileData tld; TileData trd; TileData bld; TileData brd; // see if the children are available // we also need to null check, because they could be loading if (TileCache.TryGetValue(tl, out tld) && TileCache.TryGetValue(tr, out trd) && TileCache.TryGetValue(br, out brd) && TileCache.TryGetValue(bl, out bld) && tld != null && trd != null && bld != null & brd != null && tld.Bitmap != null && trd.Bitmap != null && bld.Bitmap != null && brd.Bitmap != null) { // children are available, so mark them as recently used tld.LastUsed = trd.LastUsed = bld.LastUsed = brd.LastUsed = tickCount; // calculate the destination rects of each child tile Rectangle tlr = new Rectangle(currentX, currentY, 128, 128); Rectangle trr = new Rectangle(currentX + 128, currentY, 128, 128); Rectangle blr = new Rectangle(currentX, currentY + 128, 128, 128); Rectangle brr = new Rectangle(currentX + 128, currentY + 128, 128, 128); tlr.Intersect(rect); trr.Intersect(rect); blr.Intersect(rect); brr.Intersect(rect); // calculate the source rect of each child tile Rectangle tlsr = new Rectangle(tlr.X - currentX, tlr.Y - currentY, tlr.Width * 2, tlr.Height * 2); Rectangle trsr = new Rectangle(trr.X - currentX - 128, trr.Y - currentY, trr.Width * 2, trr.Height * 2); Rectangle blsr = new Rectangle(blr.X - currentX, blr.Y - currentY - 128, blr.Width * 2, blr.Height * 2); Rectangle brsr = new Rectangle(brr.X - currentX - 128, brr.Y - currentY - 128, brr.Width * 2, brr.Height * 2); // don't attempt to draw tiles that we don't need to if (tlsr.Width > 0 && tlsr.Height > 0) { renderer.Draw(tld.Bitmap, tlr, tlsr); } if (trsr.Width > 0 && trsr.Height > 0) { renderer.Draw(trd.Bitmap, trr, trsr); } if (blsr.Width > 0 && blsr.Height > 0) { renderer.Draw(bld.Bitmap, blr, blsr); } if (brsr.Width > 0 && brsr.Height > 0) { renderer.Draw(brd.Bitmap, brr, brsr); } continue; } else { // can't generate from children, so try generating one of the parents Key parent = key; Rectangle parentRect = sourceRect; TileData parentData = null; while (parent.Zoom >= 0 && parentData == null) { parentRect.Width /= 2; parentRect.Height /= 2; parentRect.X /= 2; parentRect.Y /= 2; if (parent.X % 2 == 1) { parentRect.X += 128; } if (parent.Y % 2 == 1) { parentRect.Y += 128; } parent.X /= 2; parent.Y /= 2; parent.Zoom--; TileCache.TryGetValue(parent, out parentData); } if (parentData != null && parentData.Bitmap != null) { // mark this tile as used recently parentData.LastUsed = tickCount; if (tileRect.Width > 0 && tileRect.Height > 0) { renderer.Draw(parentData.Bitmap, tileRect, parentRect); } continue; } else { // tile is being downloaded, and we have no parent or child images we can use to draw a temp // image. let's try to use a refresh bitmap. // tile is not available, and this is a transparent draw, // so dont draw at all if (HasAlpha) { continue; } if ((tile = RefreshBitmap) == null) { renderer.FillRectangle(BackColor, tileRect); continue; } } } } if (tile != null && tileRect.Width > 0 && tileRect.Height > 0) { renderer.Draw(tile, tileRect, sourceRect); } } } int pixelLevelZoom = myZoom + 8; int centerXReference = myCenterTile.X << 8; int centerYReference = myCenterTile.Y << 8; Geocode tlGeo = PointToGeocode(new Point(Math.Max(centerXReference + myCenterOffset.X - width / 2, 0), Math.Max(centerYReference + myCenterOffset.Y - height / 2, 0)), pixelLevelZoom); Geocode brGeo = PointToGeocode(new Point(Math.Min(centerXReference + myCenterOffset.X + width / 2, 1 << pixelLevelZoom), Math.Min(centerYReference + myCenterOffset.Y + height / 2, 1 << pixelLevelZoom)), pixelLevelZoom); int adjustX = midX - centerXReference; int adjustY = midY - centerYReference; foreach (Route route in myRoutes) { List <Point> points = new List <Point>(); Geocode lastOffscreenPoint = Geocode.Null; for (int i = 0; i < route.PolyLine.Length; i++) { Geocode geocode = route.PolyLine[i]; if (myLevelToZoom[route.Levels[i]] > myZoom) { continue; } // check if we're drawing off the screen if (!GeocodeBoxContains(tlGeo, brGeo, geocode)) { // if we're drawing from on screen to off screen, draw it, but note that // we are now off screen if (lastOffscreenPoint == Geocode.Null) { points.Add(GeocodeToScreen(geocode, pixelLevelZoom, adjustX, adjustY)); } lastOffscreenPoint = geocode; continue; } // draw in from off the screen if necessary if (lastOffscreenPoint != Geocode.Null) { points.Add(GeocodeToScreen(lastOffscreenPoint, pixelLevelZoom, adjustX, adjustY)); } // note that we are now in screen space lastOffscreenPoint = Geocode.Null; points.Add(GeocodeToScreen(geocode, pixelLevelZoom, adjustX, adjustY)); } if (points.Count > 1) { renderer.DrawLines(route.LineWidth, Color.Cyan, points.ToArray()); } } foreach (IMapOverlay overlay in Overlays) { DrawAtGeocode(tlGeo, brGeo, renderer, overlay.Geocode, pixelLevelZoom, adjustX + overlay.Offset.X, adjustY + overlay.Offset.Y, overlay.Drawable); } return(unavailable); }
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); }