Exemplo n.º 1
0
        /// <summary>
        /// Generates an image sprite based on the specified <see cref="SpriteDocument"/>.
        /// </summary>
        public async Task Generate(SpriteDocument doc)
        {
            var images = GetImages(doc);

            int width  = doc.Orientation == Orientation.Vertical ? images.Values.Max(i => i.Width) + (doc.Padding * 2) : images.Values.Sum(i => i.Width) + (doc.Padding * images.Count) + doc.Padding;
            int height = doc.Orientation == Orientation.Vertical ? images.Values.Sum(img => img.Height) + (doc.Padding * images.Count) + doc.Padding : images.Values.Max(img => img.Height) + (doc.Padding * 2);

            List <SpriteFragment> fragments = new List <SpriteFragment>();

            using (var bitmap = new Bitmap(width, height))
            {
                bitmap.SetResolution(doc.Dpi, doc.Dpi);

                using (Graphics canvas = Graphics.FromImage(bitmap))
                {
                    if (doc.Orientation == Orientation.Vertical)
                    {
                        Vertical(images, fragments, canvas, doc.Padding);
                    }
                    else
                    {
                        Horizontal(images, fragments, canvas, doc.Padding);
                    }
                }

                string outputFile = doc.FileName + doc.OutputExtension;

                OnSaving(outputFile, doc);
                bitmap.Save(outputFile, SpriteHelpers.ExtensionFromFormat(doc.Output));
                OnSaved(outputFile, doc);
            }

            await SpriteExporter.ExportStylesheet(fragments, doc, this);
        }
Exemplo n.º 2
0
        private static Dictionary <string, Bitmap> GetImages(SpriteDocument doc)
        {
            var images = new Dictionary <string, Bitmap>();
            var source = doc.ToAbsoluteImages();

            foreach (string ident in source.Keys)
            {
                string file = source[ident];

                if (!File.Exists(file))
                {
                    throw new FileNotFoundException("One or more sprite input files don't exist", file);
                }

                var bitmap = (Bitmap)Image.FromFile(file);
                bitmap.SetResolution(doc.Dpi, doc.Dpi);

                images.Add(ident, bitmap);
            }

            return(images);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Creates a new instance.
 /// </summary>
 public SpriteImageGenerationEventArgs(string fileName, SpriteDocument document)
 {
     FileName = fileName;
     Document = document;
 }
        public async static Task ExportStylesheet(IEnumerable <SpriteFragment> fragments, SpriteDocument doc, SpriteImageGenerator generator)
        {
            if (doc.Stylesheet == Stylesheet.None)
            {
                return;
            }

            var mainClass = SpriteHelpers.GetIdentifier(doc.FileName);

            string outputFile      = doc.FileName + "." + doc.Stylesheet.ToString().ToLowerInvariant();
            var    outputDirectory = Path.GetDirectoryName(outputFile);
            var    bgUrl           = doc.PathPrefix + SpriteHelpers.MakeRelative(outputFile, doc.FileName + doc.OutputExtension);

            StringBuilder sb = new StringBuilder(GetDescription(doc.Stylesheet));

            if (doc.Stylesheet == Stylesheet.Css)
            {
                sb.AppendLine($".{mainClass} {{");
                sb.AppendLine($"\tbackground-image: url('{bgUrl}');");
                sb.AppendLine($"\tbackground-repeat: no-repeat;");
                AddCustomStyles(doc.CustomStyles, sb);
                sb.AppendLine("}");
            }

            foreach (SpriteFragment fragment in fragments)
            {
                if (doc.Stylesheet == Stylesheet.Css)
                {
                    sb.AppendLine($"{GetSelector(fragment.ID, mainClass, doc.Stylesheet)} {{");
                    sb.AppendLine($"\twidth: {fragment.Width}px;");
                    sb.AppendLine($"\theight: {fragment.Height}px;");
                    sb.AppendLine($"\tbackground-position: -{fragment.X}px -{fragment.Y}px;");
                    sb.AppendLine("}");
                }
                else if (doc.Stylesheet == Stylesheet.Styl)
                {
                    sb.AppendLine(GetSelector(fragment.ID, mainClass, doc.Stylesheet));
                    sb.AppendLine($"\twidth {fragment.Width}px");
                    sb.AppendLine($"\theigh {fragment.Height}px");
                    sb.AppendLine($"\tbackground-position -{fragment.X}px -{fragment.Y}px");
                }
                else
                {
                    sb.AppendLine(GetSelector(fragment.ID, mainClass, doc.Stylesheet) + " {");
                    sb.AppendLine($"\twidth: {fragment.Width}px;");
                    sb.AppendLine($"\theight: {fragment.Height}px;");
                    AddCustomStyles(doc.CustomStyles, sb);
                    sb.AppendLine($"\tbackground: url('{bgUrl}') -{fragment.X}px -{fragment.Y}px no-repeat;");
                    sb.AppendLine("}");
                }
            }

            if (File.Exists(outputFile) && File.ReadAllText(outputFile) == sb.ToString())
            {
                return;
            }

            Directory.CreateDirectory(outputDirectory);

            using (var writer = new StreamWriter(outputFile))
            {
                generator.OnSaving(outputFile, doc);
                await writer.WriteAsync(sb.ToString().Replace("-0px", "0"));

                generator.OnSaved(outputFile, doc);
            }
        }
Exemplo n.º 5
0
 internal void OnSaved(string fileName, SpriteDocument doc)
 {
     Saved?.Invoke(this, new SpriteImageGenerationEventArgs(fileName, doc));
 }