コード例 #1
0
        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;
        }
コード例 #2
0
        public bool this[TiledMapSession session]
        {
            get
            {
                int index = mySessions.IndexOf(session);
                if (index == -1)
                    throw new ArgumentException("TiledMapSession not found in this CompositeMapSession.");
                return mySessionEnabled[index];
            }
            set
            {
                int index = mySessions.IndexOf(session);
                if (index == -1 || mySessionEnabled[index] == value)
                    return;

                this[index] = value;
            }
        }
コード例 #3
0
        public bool this[TiledMapSession session]
        {
            get
            {
                int index = mySessions.IndexOf(session);
                if (index == -1)
                {
                    throw new ArgumentException("TiledMapSession not found in this CompositeMapSession.");
                }
                return(mySessionEnabled[index]);
            }
            set
            {
                int index = mySessions.IndexOf(session);
                if (index == -1 || mySessionEnabled[index] == value)
                {
                    return;
                }

                this[index] = value;
            }
        }
コード例 #4
0
 public IMapDrawable GetBitmapFromStream(TiledMapSession session, Stream stream)
 {
     return LoadBitmap(stream, session.HasAlpha);
 }
コード例 #5
0
 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.msvemap.png"))));
     return base.GetTile(key, renderer, callback, state);
 }
コード例 #6
0
ファイル: TiledMapSession.cs プロジェクト: koush/TiledMaps
 public abstract TileData GetTile(TiledMapSession.Key key, IMapRenderer renderer, System.Threading.WaitCallback callback, object state);
コード例 #7
0
 public IMapDrawable GetBitmapFromStream(TiledMapSession session, Stream stream)
 {
     return(LoadBitmap(stream, session.HasAlpha));
 }
コード例 #8
0
        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;
        }
コード例 #9
0
        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);
        }