コード例 #1
0
        public SpriteTree InsertSprite(Sprite sprite)
        {
            if (Left != null || Right != null)
            {
                var node = Left.InsertSprite(sprite);
                if (node != null)
                {
                    return(node);
                }

                return(Right.InsertSprite(sprite));
            }
            else
            {
                int spriteWidth  = ( int )sprite.Width + 1;
                int spriteHeight = ( int )sprite.Height + 1;

                if (Sprite != null)
                {
                    return(null);
                }

                if (spriteWidth > Width || spriteHeight > Height)
                {
                    return(null);
                }

                if (spriteWidth == Width && spriteHeight == Height)
                {
                    Sprite = sprite;
                    return(this);
                }

                Left  = new SpriteTree();
                Right = new SpriteTree();

                int w = Width - spriteWidth;
                int h = Height - spriteHeight;

                if (w > h)
                {
                    Left.X      = X;
                    Left.Y      = Y;
                    Left.Width  = spriteWidth;
                    Left.Height = Height;

                    Right.X      = X + spriteWidth;
                    Right.Y      = Y;
                    Right.Width  = Width - spriteWidth;
                    Right.Height = Height;
                }

                else
                {
                    Left.X      = X;
                    Left.Y      = Y;
                    Left.Width  = Width;
                    Left.Height = spriteHeight;

                    Right.X      = X;
                    Right.Y      = Y + spriteHeight;
                    Right.Width  = Width;
                    Right.Height = Height - spriteHeight;
                }

                return(Left.InsertSprite(sprite));
            }
        }
コード例 #2
0
        private static PackSpritesReturnData OrganizeSprites(int width, int height, int maxSize, List <Sprite> sprites)
        {
            var perfectFit = sprites.FirstOrDefault(
                x => x.Width == maxSize && x.Height == maxSize);

            if (perfectFit != null)
            {
                perfectFit.X = 0;
                perfectFit.Y = 0;

                var unFitSprites = new List <Sprite>(sprites);
                unFitSprites.Remove(perfectFit);

                return(new PackSpritesReturnData
                {
                    OrganizedSprites = new List <Sprite>()
                    {
                        perfectFit
                    },
                    UnorganizedSprites = unFitSprites,
                    Width = maxSize,
                    Height = maxSize,
                });
            }

            bool allOrganized = false;

            while (!allOrganized)
            {
                var spriteTree = new SpriteTree
                {
                    Width  = width,
                    Height = height
                };

                allOrganized = true;

                var unFitSprites = new List <Sprite>();
                foreach (var sprite in sprites)
                {
                    if (spriteTree.InsertSprite(sprite) == null)
                    {
                        allOrganized = false;
                        unFitSprites.Add(sprite);
                    }
                }

                if (allOrganized)
                {
                    spriteTree.UpdateSprites();
                }
                else
                {
                    if (width < maxSize || height < maxSize)
                    {
                        if (height < width)
                        {
                            height *= 2;
                        }
                        else
                        {
                            width *= 2;
                        }
                    }

                    else
                    {
                        spriteTree.UpdateSprites();

                        return(new PackSpritesReturnData
                        {
                            OrganizedSprites = sprites.Except(unFitSprites).ToList(),
                            UnorganizedSprites = unFitSprites,
                            Width = width,
                            Height = height,
                        });
                    }
                }
            }

            return(new PackSpritesReturnData
            {
                OrganizedSprites = sprites,
                UnorganizedSprites = new List <Sprite>(),
                Width = width,
                Height = height,
            });
        }