Esempio n. 1
0
        public static PackedSprites PackSprites(IList <Bitmap> sourceSprites)
        {
            if (sourceSprites.Count == 0)
            {
                throw new ArgumentException(nameof(sourceSprites));
            }

            var sprites = sourceSprites
                          .Select((a, i) => new PackedSprite {
                Width = a.Width + 2, Height = a.Height + 2, Bitmap = a, Index = i
            })
                          .OrderBy(a => a.Height * 1024 + a.Width)
                          .ToList();

            var outputWidth  = CalculateOutputWidth(sprites);
            var outputHeight = 0;

            for (var i = 0; i < sprites.Count; i++)
            {
                PositionSprite(sprites, i, outputWidth);
                outputHeight = Math.Max(outputHeight, sprites[i].Y + sprites[i].Height);
            }

            sprites = sprites.OrderBy(a => a.Index).ToList();

            var output = new PackedSprites
            {
                OutputBitmap    = new Bitmap(outputWidth, outputHeight),
                SpritePositions = new Dictionary <Bitmap, Rectangle>()
            };

            foreach (var sprite in sprites)
            {
                var source = sprite.Bitmap;

                var x = sprite.X;
                var y = sprite.Y;

                var w = source.Width;
                var h = source.Height;

                for (var i = 0; i < w; i++)
                {
                    for (var j = 0; j < h; j++)
                    {
                        output.OutputBitmap.SetPixel(x + 1 + i, y + 1 + j, source.GetPixel(i, j));
                    }
                }

                output.SpritePositions[source] = new Rectangle(x + 1, y + 1, w, h);
            }

            return(output);
        }
Esempio n. 2
0
        protected override YAMLMappingNode ExportYAMLRoot(IExportContainer container)
        {
            YAMLMappingNode node = base.ExportYAMLRoot(container);

            if (IsReadEditorData(container.ExportFlags))
            {
                node.Add(EditorDataName, GetEditorData(container.Flags).ExportYAML(container));
                node.Add(MasterAtlasName, MasterAtlas.ExportYAML(container));
            }
            node.Add(PackedSpritesName, PackedSprites.ExportYAML(container));
            node.Add(PackedSpriteNamesToIndexName, PackedSpriteNamesToIndex.ExportYAML());
            if (IsReadRenderDataMap(container.ExportFlags))
            {
                node.Add(RenderDataMapName, RenderDataMap.ExportYAML(container));
            }
            node.Add(TagName, Tag);
            node.Add(IsVariantName, IsVariant);
            return(node);
        }