コード例 #1
0
        public async static Task ExportStylesheet(IEnumerable <SpriteFragment> fragments, SpriteDocument doc, SpriteGenerator 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;");
                sb.AppendLine($"\tdisplay: block;");
                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
                {
                    sb.AppendLine(GetSelector(fragment.ID, mainClass, doc.Stylesheet) + " {");
                    sb.AppendLine($"\twidth: {fragment.Width}px;");
                    sb.AppendLine($"\theight: {fragment.Height}px;");
                    sb.AppendLine($"\tdisplay: block;");
                    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);
            }
        }
コード例 #2
0
        /// <summary>
        /// Add image files to the sprite.
        /// </summary>
        /// <param name="files">A list of relative file paths.</param>
        private void AddImages(IEnumerable <string> files)
        {
            var dic = new Dictionary <string, string>();

            foreach (var file in files)
            {
                string name     = SpriteHelpers.GetIdentifier(file);
                string relative = SpriteHelpers.MakeRelative(FileName, file);

                if (dic.ContainsKey(name))
                {
                    name += "_" + Guid.NewGuid().ToString().Replace("-", string.Empty);
                }

                dic.Add(name, relative);
            }

            Images = dic;
        }
コード例 #3
0
        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);

            if (doc.AppendCacheBustSpriteSuffix)
            {
                using (HashAlgorithm sha = new SHA256Managed())
                {
                    byte[] spriteContent = File.ReadAllBytes(doc.FileName + doc.OutputExtension);
                    string spriteHash    = Convert.ToBase64String(sha.ComputeHash(spriteContent));
                    bgUrl += $"?hash={WebUtility.UrlEncode(spriteHash)}";
                }
            }

            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);
            }
        }