示例#1
0
        public Tile Add(TextureResource texture)
        {
            if (texture.Width != TileWidth || texture.Height != TileHeight)
            {
                throw new ArgumentException("Supplied texture does not have tile dimensions", "texture");
            }

            Tile tile = new PhysicalTile()
            {
                Pool = _pool
            };

            if (ShouldExpandTexture())
            {
                ExpandTexture();
            }

            TileCoord coord = _openLocations[_openLocations.Count - 1];

            _openLocations.RemoveAt(_openLocations.Count - 1);

            _tileSource.Set(texture, new Point(coord.X * TileWidth, coord.Y * TileHeight));
            _texturePool.Invalidate(_tileSource.Uid);

            _locations[tile.Uid] = coord;

            Add(tile);

            return(tile);
        }
示例#2
0
        private void ReduceTexture()
        {
            int width  = _tileSource.Width;
            int height = _tileSource.Height;

            if (width == height)
            {
                height /= 2;
            }
            else
            {
                width /= 2;
            }

            Queue <KeyValuePair <Guid, TileCoord> > locs = new Queue <KeyValuePair <Guid, TileCoord> >();

            foreach (KeyValuePair <Guid, TileCoord> kv in _locations)
            {
                locs.Enqueue(kv);
            }

            TextureResource newTex = new TextureResource(width, height);

            int factorX = newTex.Width / TileWidth;
            int factorY = newTex.Height / TileHeight;

            _locations.Clear();
            _openLocations.Clear();

            for (int y = 0; y < factorY; y++)
            {
                for (int x = 0; x < factorX; x++)
                {
                    if (locs.Count == 0)
                    {
                        _openLocations.Add(new TileCoord(x, y));
                        continue;
                    }

                    KeyValuePair <Guid, TileCoord> loc = locs.Dequeue();
                    Rectangle src = new Rectangle(loc.Value.X * TileWidth, loc.Value.Y * TileHeight, TileWidth, TileHeight);

                    newTex.Set(_tileSource.Crop(src), new Point(x * TileWidth, y * TileHeight));

                    _locations[loc.Key] = new TileCoord(x, y);
                }
            }

            _texturePool.RemoveResource(_tileSource.Uid);
            _tileSource = newTex;
            _texturePool.AddResource(_tileSource);
        }
        public override TextureResource MakePreview(IList <TileProxy> tiles, int tileWidth, int tileHeight, int maxWidth, int maxHeight)
        {
            if (tiles.Count < SlotCount)
            {
                return(null);
            }
            for (int i = 0; i < SlotCount; i++)
            {
                if (tiles[i] == null)
                {
                    return(null);
                }
            }

            TextureResource resource = new TextureResource(maxWidth, maxHeight);

            int tilesWide     = Math.Min(3, Math.Max(1, maxWidth / tileWidth));
            int tilesHigh     = Math.Min(3, Math.Max(1, maxHeight / tileHeight));
            int previewWidth  = Math.Min(maxWidth, tilesWide * tileWidth);
            int previewHeight = Math.Min(maxHeight, tilesHigh * tileHeight);
            int previewX      = (maxWidth - previewWidth) / 2;
            int previewY      = (maxHeight - previewHeight) / 2;

            Tile[,] previewTiles = new Tile[3, 3] {
                { tiles[0].Tile, tiles[1].Tile, tiles[2].Tile },
                { tiles[12].Tile, tiles[13].Tile, tiles[14].Tile },
                { tiles[24].Tile, tiles[25].Tile, tiles[26].Tile },
            };

            for (int y = 0; y < tilesHigh; y++)
            {
                for (int x = 0; x < tilesWide; x++)
                {
                    if (previewTiles[y, x] != null)
                    {
                        TextureResource tex = previewTiles[y, x].Pool.Tiles.GetTileTexture(previewTiles[y, x].Uid);
                        resource.Set(tex, new Point(previewX + x * tileWidth, previewY + y * tileHeight));
                    }
                }
            }

            return(resource);
        }
示例#4
0
        private void ExpandTexture()
        {
            int width  = _tileSource.Width;
            int height = _tileSource.Height;

            if (width == height)
            {
                width *= 2;
            }
            else
            {
                height *= 2;
            }

            TextureResource newTex = new TextureResource(width, height);

            newTex.Set(_tileSource, new Point(0, 0));

            int factorX = newTex.Width / TileWidth;
            int factorY = newTex.Height / TileHeight;
            int threshX = _tileSource.Width / TileWidth;
            int threshY = _tileSource.Height / TileHeight;

            for (int y = 0; y < factorY; y++)
            {
                for (int x = 0; x < factorX; x++)
                {
                    if (x >= threshX || y >= threshY)
                    {
                        _openLocations.Add(new TileCoord(x, y));
                    }
                }
            }

            _texturePool.RemoveResource(_tileSource.Uid);
            _tileSource = newTex;
            _texturePool.AddResource(_tileSource);
        }
示例#5
0
 private void ClearTransparentColor(TextureResource resource)
 {
     resource.Set(_originalResource, Point.Zero);
 }