public static IEnumerable <SpriteFragment> CreateImage(SpriteDocument sprite, out string imageFile)
        {
            imageFile = Path.ChangeExtension(sprite.FileName, sprite.FileExtension);
            ProjectHelpers.CheckOutFileFromSourceControl(imageFile);

            Dictionary <string, Image> images = GetImages(sprite);

            int width  = sprite.IsVertical ? images.Values.Max(i => i.Width) : images.Values.Sum(i => i.Width);
            int height = sprite.IsVertical ? images.Values.Sum(img => img.Height) : images.Values.Max(img => img.Height);

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

            using (var bitmap = new Bitmap(width, height))
            {
                using (Graphics canvas = Graphics.FromImage(bitmap))
                {
                    if (sprite.IsVertical)
                    {
                        Vertical(images, fragments, canvas);
                    }
                    else
                    {
                        Horizontal(images, fragments, canvas);
                    }
                }

                bitmap.Save(imageFile, PasteImage.GetImageFormat("." + sprite.FileExtension));
            }

            return(fragments);
        }
        private static string GetFileName(string imageFileName, SpriteDocument sprite, ExportFormat format)
        {
            switch (format)
            {
            case ExportFormat.Css:
                if (sprite.CssOutputDirectory != null)
                {
                    return(imageFileName = GetAbsolutePathFromSettings(sprite.CssOutputDirectory, imageFileName, ".css"));
                }
                return(imageFileName + ".css");

            case ExportFormat.Less:
                if (sprite.CssOutputDirectory != null)
                {
                    return(imageFileName = GetAbsolutePathFromSettings(sprite.LessOutputDirectory, imageFileName, ".less"));
                }
                return(imageFileName + ".less");

            case ExportFormat.Scss:
                if (sprite.CssOutputDirectory != null)
                {
                    return(imageFileName = GetAbsolutePathFromSettings(sprite.ScssOutputDirectory, imageFileName, ".scss"));
                }
                return(imageFileName + ".scss");

            case ExportFormat.Json:
                return(imageFileName + ".map");
            }

            return(null);
        }
        public static IEnumerable<SpriteFragment> CreateImage(SpriteDocument sprite, out string imageFile)
        {
            imageFile = Path.ChangeExtension(sprite.FileName, sprite.FileExtension);
            ProjectHelpers.CheckOutFileFromSourceControl(imageFile);

            Dictionary<string, Image> images = GetImages(sprite);

            int width = sprite.IsVertical ? images.Values.Max(i => i.Width) : images.Values.Sum(i => i.Width);
            int height = sprite.IsVertical ? images.Values.Sum(img => img.Height) : images.Values.Max(img => img.Height);

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

            using (var bitmap = new Bitmap(width, height))
            {
                using (Graphics canvas = Graphics.FromImage(bitmap))
                {
                    if (sprite.IsVertical)
                        Vertical(images, fragments, canvas);
                    else
                        Horizontal(images, fragments, canvas);
                }

                bitmap.Save(imageFile, PasteImage.GetImageFormat("." + sprite.FileExtension));
            }

            return fragments;
        }
Exemple #4
0
        private async Task MakeSpriteAsync()
        {
            string spriteFile;

            if (!GetFileName(out spriteFile))
            {
                return;
            }

            try
            {
                SpriteDocument doc = new SpriteDocument(spriteFile, _files.ToArray());

                await doc.WriteBundleRecipe();

                EditorExtensionsPackage.DTE.ItemOperations.OpenFile(spriteFile);

                await GenerateAsync(doc);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                MessageBox.Show("Error generating the sprite. See output window for details");
                _dte.StatusBar.Text = "Sprite was not created";
            }
        }
        private async Task GenerateAsync(SpriteDocument sprite, bool hasUpdated = false)
        {
            _dte.StatusBar.Text = "Generating sprite...";

            ProjectHelpers.AddFileToProject(Path.GetDirectoryName(sprite.FileName), sprite.FileName);

            string imageFile = Path.ChangeExtension(sprite.FileName, sprite.FileExtension);

            if (string.IsNullOrEmpty(sprite.OutputDirectory))
            {
                imageFile = ProjectHelpers.GetAbsolutePathFromSettings(sprite.OutputDirectory, Path.ChangeExtension(sprite.FileName, sprite.FileExtension));
            }

            ProjectHelpers.CreateDirectoryInProject(imageFile);

            IEnumerable <SpriteFragment> fragments = await SpriteGenerator.MakeImage(sprite, imageFile, UpdateSpriteAsync);

            ProjectHelpers.AddFileToProject(sprite.FileName, imageFile);

            if (!hasUpdated)
            {
                WebEssentialsPackage.DTE.ItemOperations.OpenFile(sprite.FileName);
            }

            await Export(fragments, imageFile, sprite);

            if (sprite.Optimize)
            {
                await new ImageCompressor().CompressFilesAsync(imageFile);
            }

            _dte.StatusBar.Text = "Sprite generated";
        }
Exemple #6
0
        public async Task UpdateSpriteAsync(string spriteFileName, bool isBuild = false)
        {
            if (!spriteFileName.EndsWith(".sprite", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            try
            {
                SpriteDocument doc = await SpriteDocument.FromFile(spriteFileName);

                if (doc == null)
                {
                    throw new Exception();
                }

                if (!isBuild || doc.RunOnBuild)
                {
                    await GenerateAsync(doc, true);
                }
            }
            catch (FileNotFoundException ex)
            {
                Logger.Log(ex.Message);
                Logger.Log("The file '" + Path.GetFileName(ex.Message) + "' does not exist");
                _dte.StatusBar.Text = "Sprite was not created";
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                Logger.Log("Error generating the sprite. See output window for details");
            }
        }
Exemple #7
0
        public async static Task <IEnumerable <SpriteFragment> > MakeImage(SpriteDocument document, string imageFile, Func <string, bool, Task> updateSprite)
        {
            ProjectHelpers.CheckOutFileFromSourceControl(imageFile);

            Dictionary <string, Image> images = await WatchFiles(document, updateSprite);

            int width  = document.IsVertical ? images.Values.Max(i => i.Width) + (document.Margin * 2) : images.Values.Sum(i => i.Width) + (document.Margin * images.Count) + document.Margin;
            int height = document.IsVertical ? images.Values.Sum(img => img.Height) + (document.Margin * images.Count) + document.Margin : images.Values.Max(img => img.Height) + (document.Margin * 2);

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

            using (var bitmap = new Bitmap(width, height))
            {
                using (Graphics canvas = Graphics.FromImage(bitmap))
                {
                    if (document.IsVertical)
                    {
                        Vertical(images, fragments, canvas, document.Margin);
                    }
                    else
                    {
                        Horizontal(images, fragments, canvas, document.Margin);
                    }
                }

                bitmap.Save(imageFile, PasteImage.GetImageFormat("." + document.FileExtension));
            }

            return(fragments);
        }
Exemple #8
0
        private static Dictionary <string, Image> GetImages(SpriteDocument sprite)
        {
            Dictionary <string, Image> images = new Dictionary <string, Image>();

            foreach (string file in sprite.BundleAssets)
            {
                if (!System.IO.File.Exists(file))
                {
                    Logger.Log(string.Format("The image file {0} for sprite {1} was not found", file, sprite.FileName));
                    return(null);
                }

                Image image = Image.FromFile(file);

                // Only touch the resolution of the image if it isn't 96.
                // That way we keep the original image 'as is' in all other cases.
                if (Math.Round(image.VerticalResolution) != 96F || Math.Round(image.HorizontalResolution) != 96F)
                {
                    image = new Bitmap(image);
                }

                images.Add(file, image);
            }

            return(images);
        }
        private async Task GenerateAsync(SpriteDocument sprite, bool hasUpdated = false)
        {
            _dte.StatusBar.Text = "Generating sprite...";

            if (!hasUpdated)
            {
                ProjectHelpers.AddFileToActiveProject(sprite.FileName);
            }

            //Default file name is the sprite name with specified file extension.
            string imageFile = Path.ChangeExtension(sprite.FileName, sprite.FileExtension);

            IEnumerable <SpriteFragment> fragments = await SpriteGenerator.MakeImage(sprite, imageFile, UpdateSpriteAsync);

            if (!hasUpdated)
            {
                ProjectHelpers.AddFileToProject(sprite.FileName, imageFile);
                EditorExtensionsPackage.DTE.ItemOperations.OpenFile(sprite.FileName);
            }

            await Export(fragments, imageFile, sprite);

            if (sprite.Optimize)
            {
                await new ImageCompressor().CompressFilesAsync(imageFile);
            }

            _dte.StatusBar.Text = "Sprite generated";
        }
        private async static Task<string> ExportStylesheet(IEnumerable<SpriteFragment> fragments, SpriteDocument sprite, string imageFile, ExportFormat format)
        {
            string outputFile = GetFileName(imageFile, sprite, format);
            var outputDirectory = Path.GetDirectoryName(outputFile);
            StringBuilder sb = new StringBuilder().AppendLine(GetDescription(format));
            string root = ProjectHelpers.GetRootFolder();

            foreach (SpriteFragment fragment in fragments)
            {
                var rootAbsoluteUrl = FileHelpers.RelativePath(root, fragment.FileName);

                var bgUrl = sprite.UseAbsoluteUrl ? "/" + FileHelpers.RelativePath(root, imageFile) : FileHelpers.RelativePath(outputFile, imageFile);

                sb.AppendLine(GetSelector(rootAbsoluteUrl, sprite, format) + " {");
                sb.AppendLine("/* You may have to set 'display: block' */");
                sb.AppendLine("\twidth: " + fragment.Width + "px;");
                sb.AppendLine("\theight: " + fragment.Height + "px;");
                sb.AppendLine("\tbackground: url('" + bgUrl + "') -" + fragment.X + "px -" + fragment.Y + "px;");
                sb.AppendLine("}");
            }

            bool IsExists = System.IO.Directory.Exists(outputDirectory);
            if (!IsExists)
                System.IO.Directory.CreateDirectory(outputDirectory);

            ProjectHelpers.CheckOutFileFromSourceControl(outputFile);
            await FileHelpers.WriteAllTextRetry(outputFile, sb.ToString().Replace("-0px", "0"));

            return outputFile;
        }
        public async static Task<IEnumerable<SpriteFragment>> MakeImage(SpriteDocument document, string imageFile, Func<string, bool, Task> updateSprite)
        {
            ProjectHelpers.CheckOutFileFromSourceControl(imageFile);

            Dictionary<string, Image> images = await WatchFiles(document, updateSprite);

            int width = document.IsVertical ? images.Values.Max(i => i.Width) : images.Values.Sum(i => i.Width);
            int height = document.IsVertical ? images.Values.Sum(img => img.Height) : images.Values.Max(img => img.Height);

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

            using (var bitmap = new Bitmap(width, height))
            {
                using (Graphics canvas = Graphics.FromImage(bitmap))
                {
                    if (document.IsVertical)
                        Vertical(images, fragments, canvas);
                    else
                        Horizontal(images, fragments, canvas);
                }

                bitmap.Save(imageFile, PasteImage.GetImageFormat("." + document.FileExtension));
            }

            return fragments;
        }
        public async static Task<string> Export(IEnumerable<SpriteFragment> fragments, SpriteDocument sprite, string imageFile, ExportFormat format)
        {
            if (format == ExportFormat.Json)
            {
                return ExportJson(fragments, sprite, imageFile);
            }

            return await ExportStylesheet(fragments, sprite, imageFile, format);
        }
        public async static Task<Dictionary<string, Image>> WatchFiles(SpriteDocument document, Func<string, bool, Task> updateSprite)
        {
            Dictionary<string, Image> images = GetImages(document);

            await new BundleFileObserver().AttachFileObserver(document, document.FileName, updateSprite);

            foreach (string file in images.Keys)
            {
                await new BundleFileObserver().AttachFileObserver(document, file, updateSprite);
            }

            return images;
        }
        public async static Task <Dictionary <string, Image> > WatchFiles(SpriteDocument document, Func <string, bool, Task> updateSprite)
        {
            Dictionary <string, Image> images = GetImages(document);

            await new BundleFileObserver().AttachFileObserver(document.FileName, document.FileName, updateSprite);

            foreach (string file in images.Keys)
            {
                await new BundleFileObserver().AttachFileObserver(file, document.FileName, updateSprite);
            }

            return(images);
        }
        public static SpriteDocument FromFile(string fileName)
        {
            XDocument doc = XDocument.Load(fileName);
            string root = ProjectHelpers.GetProjectFolder(fileName);
            string folder = Path.GetDirectoryName(root);

            var imageFiles = from f in doc.Descendants("file")
                             select ProjectHelpers.ToAbsoluteFilePath(f.Value, root, folder);

            SpriteDocument sprite = new SpriteDocument(fileName, imageFiles.ToArray());
            sprite.Optimize = doc.Descendants("optimize").First().Value.Equals("true", StringComparison.OrdinalIgnoreCase);
            sprite.IsVertical = doc.Descendants("orientation").First().Value.Equals("vertical", StringComparison.OrdinalIgnoreCase);
            sprite.FileExtension = doc.Descendants("outputType").First().Value;

            return sprite;
        }
        public static SpriteDocument FromFile(string fileName)
        {
            XDocument doc    = XDocument.Load(fileName);
            string    root   = ProjectHelpers.GetProjectFolder(fileName);
            string    folder = Path.GetDirectoryName(root);

            var imageFiles = from f in doc.Descendants("file")
                             select ProjectHelpers.ToAbsoluteFilePath(f.Value, root, folder);

            SpriteDocument sprite = new SpriteDocument(fileName, imageFiles.ToArray());

            sprite.Optimize      = doc.Descendants("optimize").First().Value.Equals("true", StringComparison.OrdinalIgnoreCase);
            sprite.IsVertical    = doc.Descendants("orientation").First().Value.Equals("vertical", StringComparison.OrdinalIgnoreCase);
            sprite.FileExtension = doc.Descendants("outputType").First().Value;

            return(sprite);
        }
        private static Dictionary<string, Image> GetImages(SpriteDocument sprite)
        {
            Dictionary<string, Image> images = new Dictionary<string, Image>();

            foreach (string file in sprite.BundleAssets)
            {
                Image image = Image.FromFile(file);

                // Only touch the resolution of the image if it isn't 96. 
                // That way we keep the original image 'as is' in all other cases.
                if (Math.Round(image.VerticalResolution) != 96F || Math.Round(image.HorizontalResolution) != 96F)
                    image = new Bitmap(image);

                images.Add(file, image);
            }

            return images;
        }
Exemple #18
0
        private async Task GenerateAsync(SpriteDocument sprite)
        {
            _dte.StatusBar.Text = "Generating sprite...";

            string imageFile;
            var    fragments = SpriteGenerator.CreateImage(sprite, out imageFile);

            ProjectHelpers.AddFileToActiveProject(sprite.FileName);
            ProjectHelpers.AddFileToProject(sprite.FileName, imageFile);

            await Export(fragments, imageFile, sprite);

            if (sprite.Optimize)
            {
                await new ImageCompressor().CompressFilesAsync(imageFile);
            }

            _dte.StatusBar.Text = "Sprite generated";
        }
Exemple #19
0
        private static Dictionary <string, Image> GetImages(SpriteDocument sprite)
        {
            Dictionary <string, Image> images = new Dictionary <string, Image>();

            foreach (string file in sprite.BundleAssets)
            {
                Image image = Image.FromFile(file);

                // Only touch the resolution of the image if it isn't 96.
                // That way we keep the original image 'as is' in all other cases.
                if (Math.Round(image.VerticalResolution) != 96F || Math.Round(image.HorizontalResolution) != 96F)
                {
                    image = new Bitmap(image);
                }

                images.Add(file, image);
            }

            return(images);
        }
        private async Task MakeSpriteAsync()
        {
            string spriteFile;

            if (!GetFileName(out spriteFile))
                return;

            try
            {
                SpriteDocument doc = new SpriteDocument(spriteFile, _files.ToArray());

                await doc.WriteSpriteRecipe();
                await GenerateAsync(doc);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                MessageBox.Show("Error generating the sprite. See output window for details");
                _dte.StatusBar.Text = "Sprite was not created";
            }
        }
        private async Task CreateSpriteAsync()
        {
            string spriteFile;

            if (!GetFileName(out spriteFile))
                return;

            try
            {
                SpriteDocument doc = new SpriteDocument(spriteFile, _files.ToArray());
                doc.Save();
                EditorExtensionsPackage.DTE.ItemOperations.OpenFile(spriteFile);

                await GenerateAsync(doc);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                MessageBox.Show("Error generating the sprite. See output window for details");
                _dte.StatusBar.Text = "Sprite was not created";
            }
        }
Exemple #22
0
 private async Task UpdateSpriteAsync()
 {
     try
     {
         await Task.WhenAll(_sprites.Select(async file =>
         {
             SpriteDocument doc = SpriteDocument.FromFile(file);
             await GenerateAsync(doc);
         }));
     }
     catch (FileNotFoundException ex)
     {
         Logger.Log(ex.Message);
         MessageBox.Show("The file '" + Path.GetFileName(ex.Message) + "' does not exist");
         _dte.StatusBar.Text = "Sprite was not created";
     }
     catch (Exception ex)
     {
         Logger.Log(ex);
         MessageBox.Show("Error generating the sprite. See output window for details");
     }
 }
Exemple #23
0
        private async Task MakeSpriteAsync()
        {
            string spriteFile;

            if (!GetFileName(out spriteFile))
            {
                return;
            }

            try
            {
                SpriteDocument doc = new SpriteDocument(spriteFile, _files.ToArray());

                await doc.WriteSpriteRecipe();
                await GenerateAsync(doc);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                Logger.Log("Error generating the sprite. See output window for details");
                _dte.StatusBar.Text = "Sprite was not created";
            }
        }
        private static string GetSelector(string fileName, SpriteDocument sprite, ExportFormat format)
        {
            string className = FileHelpers.GetFileNameWithoutExtension(fileName);

            if (sprite.UseFullPathForIdentifierName)
            {
                string withoutExtensionWithDirectoryName = Path.Combine(Path.GetDirectoryName(fileName), className);
                className = string.Join("-", withoutExtensionWithDirectoryName.Split(
                                            new[] { Path.DirectorySeparatorChar,
                                                    Path.AltDirectorySeparatorChar }));
            }

            if (format == ExportFormat.Less)
            {
                return(".sprite-" + className + "()");
            }
            else if (format == ExportFormat.Scss)
            {
                return("@mixin sprite-" + className + "()");
            }

            return("." + className);
        }
        private static string ExportJson(IEnumerable <SpriteFragment> fragments, SpriteDocument sprite, string imageFile)
        {
            string root = ProjectHelpers.GetRootFolder();

            var map = new
            {
                images = fragments.Select(fragment =>
                {
                    var item = new
                    {
                        Name    = "/" + FileHelpers.RelativePath(root, fragment.FileName),
                        Width   = fragment.Width,
                        Height  = fragment.Height,
                        OffsetX = fragment.X,
                        OffsetY = fragment.Y,
                    };

                    return(item);
                })
            };

            string outputFile = GetFileName(imageFile, sprite, ExportFormat.Json);

            ProjectHelpers.CheckOutFileFromSourceControl(outputFile);

            using (StreamWriter sw = new StreamWriter(outputFile))
                using (JsonWriter jw = new JsonTextWriter(sw))
                {
                    jw.Formatting = Formatting.Indented;

                    var serializer = new JsonSerializer();
                    serializer.ContractResolver = new CamelCasePropertyNamesContractResolver();
                    serializer.Serialize(jw, map);
                }

            return(outputFile);
        }
        private static string ExportJson(IEnumerable<SpriteFragment> fragments, SpriteDocument sprite, string imageFile)
        {
            string root = ProjectHelpers.GetRootFolder();

            var map = new
            {
                images = fragments.Select(fragment =>
                {
                    var item = new
                    {
                        Name = "/" + FileHelpers.RelativePath(root, fragment.FileName),
                        Width = fragment.Width,
                        Height = fragment.Height,
                        OffsetX = fragment.X,
                        OffsetY = fragment.Y,
                    };

                    return item;
                })
            };

            string outputFile = GetFileName(imageFile, sprite, ExportFormat.Json);
            ProjectHelpers.CheckOutFileFromSourceControl(outputFile);

            using (StreamWriter sw = new StreamWriter(outputFile))
            using (JsonWriter jw = new JsonTextWriter(sw))
            {
                jw.Formatting = Formatting.Indented;

                var serializer = new JsonSerializer();
                serializer.ContractResolver = new CamelCasePropertyNamesContractResolver();
                serializer.Serialize(jw, map);
            }

            return outputFile;
        }
        private static Dictionary<string, Image> GetImages(SpriteDocument sprite)
        {
            Dictionary<string, Image> images = new Dictionary<string, Image>();

            foreach (string file in sprite.BundleAssets)
            {
                if (!System.IO.File.Exists(file))
                {
                    Logger.Log(string.Format("The image file {0} for sprite {1} was not found", file, sprite.FileName));
                    return null;
                }

                Image image = Image.FromFile(file);

                // Only touch the resolution of the image if it isn't 96. 
                // That way we keep the original image 'as is' in all other cases.
                if (Math.Round(image.VerticalResolution) != 96F || Math.Round(image.HorizontalResolution) != 96F)
                    image = new Bitmap(image);

                images.Add(file, image);
            }

            return images;
        }
        private async Task GenerateAsync(SpriteDocument sprite, bool hasUpdated = false)
        {
            _dte.StatusBar.Text = "Generating sprite...";

            if (!hasUpdated)
                ProjectHelpers.AddFileToActiveProject(sprite.FileName);

            string imageFile = Path.ChangeExtension(sprite.FileName, sprite.FileExtension);

            if (string.IsNullOrEmpty(sprite.OutputDirectory))
                imageFile = ProjectHelpers.GetAbsolutePathFromSettings(sprite.OutputDirectory, Path.ChangeExtension(sprite.FileName, sprite.FileExtension));

            ProjectHelpers.CreateDirectoryInProject(imageFile);

            IEnumerable<SpriteFragment> fragments = await SpriteGenerator.MakeImage(sprite, imageFile, UpdateSpriteAsync);

            if (!hasUpdated)
            {
                ProjectHelpers.AddFileToProject(sprite.FileName, imageFile);
                WebEssentialsPackage.DTE.ItemOperations.OpenFile(sprite.FileName);
            }

            await Export(fragments, imageFile, sprite);

            if (sprite.Optimize)
                await new ImageCompressor().CompressFilesAsync(imageFile);

            _dte.StatusBar.Text = "Sprite generated";
        }
        private async static Task <string> ExportStylesheet(IEnumerable <SpriteFragment> fragments, SpriteDocument sprite, string imageFile, ExportFormat format)
        {
            string        outputFile      = GetFileName(imageFile, sprite, format);
            var           outputDirectory = Path.GetDirectoryName(outputFile);
            StringBuilder sb   = new StringBuilder().AppendLine(GetDescription(format));
            string        root = ProjectHelpers.GetRootFolder();

            foreach (SpriteFragment fragment in fragments)
            {
                var rootAbsoluteUrl = FileHelpers.RelativePath(root, fragment.FileName);

                var bgUrl = sprite.UseAbsoluteUrl ? "/" + FileHelpers.RelativePath(root, imageFile) : FileHelpers.RelativePath(outputFile, imageFile);

                sb.AppendLine(GetSelector(rootAbsoluteUrl, sprite, format) + " {");
                sb.AppendLine("/* You may have to set 'display: block' */");
                sb.AppendLine("\twidth: " + fragment.Width + "px;");
                sb.AppendLine("\theight: " + fragment.Height + "px;");
                sb.AppendLine("\tbackground: url('" + bgUrl + "') -" + fragment.X + "px -" + fragment.Y + "px;");
                sb.AppendLine("}");
            }

            bool IsExists = System.IO.Directory.Exists(outputDirectory);

            if (!IsExists)
            {
                System.IO.Directory.CreateDirectory(outputDirectory);
            }

            ProjectHelpers.CheckOutFileFromSourceControl(outputFile);
            await FileHelpers.WriteAllTextRetry(outputFile, sb.ToString().Replace("-0px", "0"));

            return(outputFile);
        }
        private async Task GenerateAsync(SpriteDocument sprite)
        {
            _dte.StatusBar.Text = "Generating sprite...";

            string imageFile;
            var fragments = SpriteGenerator.CreateImage(sprite, out imageFile);

            ProjectHelpers.AddFileToActiveProject(sprite.FileName);
            ProjectHelpers.AddFileToProject(sprite.FileName, imageFile);

            Export(fragments, imageFile);

            if (sprite.Optimize)
                await new ImageCompressor().CompressFilesAsync(imageFile);

            _dte.StatusBar.Text = "Sprite generated";
        }
        public static SpriteDocument FromFile(string fileName)
        {
            string    root    = ProjectHelpers.GetProjectFolder(fileName);
            string    folder  = Path.GetDirectoryName(root);
            XDocument doc     = XDocument.Load(fileName);
            XElement  element = null;

            var imageFiles = from f in doc.Descendants("file")
                             select ProjectHelpers.ToAbsoluteFilePath(f.Value, root, folder);

            SpriteDocument sprite = new SpriteDocument(fileName, imageFiles.ToArray());

            element = doc.Descendants("optimize").FirstOrDefault();

            if (element != null)
            {
                sprite.Optimize = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);
            }

            element = doc.Descendants("orientation").FirstOrDefault();

            if (element != null)
            {
                sprite.IsVertical = element.Value.Equals("vertical", StringComparison.OrdinalIgnoreCase);
            }

            element = doc.Descendants("outputType").FirstOrDefault();

            if (element != null)
            {
                sprite.FileExtension = element.Value;
            }

            element = doc.Descendants("fullPathForIdentifierName").FirstOrDefault();

            if (element != null)
            {
                sprite.UseFullPathForIdentifierName = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);
            }

            element = doc.Descendants("useAbsoluteUrl").FirstOrDefault();

            if (element != null)
            {
                sprite.UseAbsoluteUrl = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);
            }

            element = doc.Descendants("outputDirectoryForCss").FirstOrDefault();

            if (element != null)
            {
                sprite.CssOutputDirectory = element.Value;
            }

            element = doc.Descendants("outputDirectoryForLess").FirstOrDefault();

            if (element != null)
            {
                sprite.LessOutputDirectory = element.Value;
            }

            element = doc.Descendants("outputDirectoryForScss").FirstOrDefault();

            if (element != null)
            {
                sprite.ScssOutputDirectory = element.Value;
            }

            return(sprite);
        }
        public static SpriteDocument FromFile(string fileName)
        {
            string root = ProjectHelpers.GetProjectFolder(fileName);
            string folder = Path.GetDirectoryName(root);

            if (folder == null || root == null)
                return null;

            XDocument doc = null;

            try
            {
                doc = XDocument.Load(fileName);
            }
            catch (XmlException)
            {
                return null;
            }

            XElement element = null;

            var imageFiles = from f in doc.Descendants("file")
                             select ProjectHelpers.ToAbsoluteFilePath(f.Value, root, folder);

            SpriteDocument sprite = new SpriteDocument(fileName, imageFiles.ToArray());

            element = doc.Descendants("optimize").FirstOrDefault();

            if (element != null)
                sprite.Optimize = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);

            element = doc.Descendants("orientation").FirstOrDefault();

            if (element != null)
                sprite.IsVertical = element.Value.Equals("vertical", StringComparison.OrdinalIgnoreCase);

            element = doc.Descendants("runOnBuild").FirstOrDefault();

            if (element != null)
                sprite.RunOnBuild = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);

            element = doc.Descendants("outputType").FirstOrDefault();

            if (element != null)
                sprite.FileExtension = element.Value;

            element = doc.Descendants("fullPathForIdentifierName").FirstOrDefault();

            if (element != null)
                sprite.UseFullPathForIdentifierName = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);

            element = doc.Descendants("useAbsoluteUrl").FirstOrDefault();

            if (element != null)
                sprite.UseAbsoluteUrl = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);

            element = doc.Descendants("outputDirectoryForCss").FirstOrDefault();

            if (element != null)
                sprite.CssOutputDirectory = element.Value;

            element = doc.Descendants("outputDirectoryForLess").FirstOrDefault();

            if (element != null)
                sprite.LessOutputDirectory = element.Value;

            element = doc.Descendants("outputDirectoryForScss").FirstOrDefault();

            if (element != null)
                sprite.ScssOutputDirectory = element.Value;

            return sprite;
        }
        public static async Task <SpriteDocument> FromFile(string fileName)
        {
            string root   = ProjectHelpers.GetProjectFolder(fileName);
            string folder = Path.GetDirectoryName(root);

            if (folder == null || root == null)
            {
                return(null);
            }

            XDocument doc = null;

            string contents = await FileHelpers.ReadAllTextRetry(fileName);

            try
            {
                doc = XDocument.Parse(contents);
            }
            catch (XmlException)
            {
                return(null);
            }

            XElement element = null;

            IEnumerable <string> imageFiles = ProjectHelpers.GetBundleConstituentFiles(doc.Descendants("file").Select(s => s.Value), root, folder, fileName);

            SpriteDocument sprite = new SpriteDocument(fileName, imageFiles.ToArray());

            element = doc.Descendants("optimize").FirstOrDefault();

            if (element != null)
            {
                sprite.Optimize = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);
            }

            element = doc.Descendants("orientation").FirstOrDefault();

            if (element != null)
            {
                sprite.IsVertical = element.Value.Equals("vertical", StringComparison.OrdinalIgnoreCase);
            }

            element = doc.Descendants("runOnBuild").FirstOrDefault();

            if (element != null)
            {
                sprite.RunOnBuild = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);
            }

            element = doc.Descendants("outputType").FirstOrDefault();

            if (element != null)
            {
                sprite.FileExtension = element.Value;
            }

            element = doc.Descendants("fullPathForIdentifierName").FirstOrDefault();

            if (element != null)
            {
                sprite.UseFullPathForIdentifierName = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);
            }

            element = doc.Descendants("outputDirectory").FirstOrDefault();

            if (element != null)
            {
                sprite.OutputDirectory = element.Value;
            }

            element = doc.Descendants("useAbsoluteUrl").FirstOrDefault();

            if (element != null)
            {
                sprite.UseAbsoluteUrl = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);
            }

            element = doc.Descendants("outputDirectoryForCss").FirstOrDefault();

            if (element != null)
            {
                sprite.CssOutputDirectory = element.Value;
            }

            element = doc.Descendants("outputDirectoryForLess").FirstOrDefault();

            if (element != null)
            {
                sprite.LessOutputDirectory = element.Value;
            }

            element = doc.Descendants("outputDirectoryForScss").FirstOrDefault();

            if (element != null)
            {
                sprite.ScssOutputDirectory = element.Value;
            }

            return(sprite);
        }
        public static async Task<SpriteDocument> FromFile(string fileName)
        {
            string root = ProjectHelpers.GetProjectFolder(fileName);
            string folder = Path.GetDirectoryName(root);

            if (folder == null || root == null)
                return null;

            XDocument doc = null;

            string contents = await FileHelpers.ReadAllTextRetry(fileName);

            try
            {
                doc = XDocument.Parse(contents);
            }
            catch (XmlException)
            {
                return null;
            }

            XElement element = null;

            IEnumerable<string> imageFiles = ProjectHelpers.GetBundleConstituentFiles(doc.Descendants("file").Select(s => s.Value), root, folder, fileName);

            SpriteDocument sprite = new SpriteDocument(fileName, imageFiles.ToArray());

            element = doc.Descendants("optimize").FirstOrDefault();

            if (element != null)
                sprite.Optimize = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);

            element = doc.Descendants("orientation").FirstOrDefault();

            if (element != null)
                sprite.IsVertical = element.Value.Equals("vertical", StringComparison.OrdinalIgnoreCase);

            element = doc.Descendants("runOnBuild").FirstOrDefault();

            if (element != null)
                sprite.RunOnBuild = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);

            element = doc.Descendants("outputType").FirstOrDefault();

            if (element != null)
                sprite.FileExtension = element.Value;

            element = doc.Descendants("fullPathForIdentifierName").FirstOrDefault();

            if (element != null)
                sprite.UseFullPathForIdentifierName = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);

            element = doc.Descendants("outputDirectory").FirstOrDefault();

            if (element != null)
                sprite.OutputDirectory = element.Value;

            element = doc.Descendants("useAbsoluteUrl").FirstOrDefault();

            if (element != null)
                sprite.UseAbsoluteUrl = element.Value.Equals("true", StringComparison.OrdinalIgnoreCase);

            element = doc.Descendants("outputDirectoryForCss").FirstOrDefault();

            if (element != null)
                sprite.CssOutputDirectory = element.Value;

            element = doc.Descendants("outputDirectoryForLess").FirstOrDefault();

            if (element != null)
                sprite.LessOutputDirectory = element.Value;

            element = doc.Descendants("outputDirectoryForScss").FirstOrDefault();

            if (element != null)
                sprite.ScssOutputDirectory = element.Value;

            return sprite;
        }
 public async Task <IBundleDocument> LoadFromFile(string fileName)
 {
     return(await SpriteDocument.FromFile(fileName));
 }
        private async Task GenerateAsync(SpriteDocument sprite, bool hasUpdated = false)
        {
            _dte.StatusBar.Text = "Generating sprite...";

            //Default file name is the sprite name with specified file extension.
            string imageFile = Path.ChangeExtension(sprite.FileName, sprite.FileExtension);

            IEnumerable<SpriteFragment> fragments = await SpriteGenerator.MakeImage(sprite, imageFile, UpdateSpriteAsync);

            if (!hasUpdated)
            {
                ProjectHelpers.AddFileToActiveProject(sprite.FileName);
                ProjectHelpers.AddFileToProject(sprite.FileName, imageFile);
                EditorExtensionsPackage.DTE.ItemOperations.OpenFile(sprite.FileName);
            }

            await Export(fragments, imageFile, sprite);

            if (sprite.Optimize)
                await new ImageCompressor().CompressFilesAsync(imageFile);

            _dte.StatusBar.Text = "Sprite generated";
        }
 private async static Task Export(IEnumerable<SpriteFragment> fragments, string imageFile, SpriteDocument sprite)
 {
     foreach (ExportFormat format in (ExportFormat[])Enum.GetValues(typeof(ExportFormat)))
     {
         string exportFile = await SpriteExporter.Export(fragments, sprite, imageFile, format);
         ProjectHelpers.AddFileToProject(imageFile, exportFile);
     }
 }
        public async static Task <string> Export(IEnumerable <SpriteFragment> fragments, SpriteDocument sprite, string imageFile, ExportFormat format)
        {
            if (format == ExportFormat.Json)
            {
                return(ExportJson(fragments, sprite, imageFile));
            }

            return(await ExportStylesheet(fragments, sprite, imageFile, format));
        }
Exemple #39
0
        private async static Task Export(IEnumerable <SpriteFragment> fragments, string imageFile, SpriteDocument sprite)
        {
            foreach (ExportFormat format in (ExportFormat[])Enum.GetValues(typeof(ExportFormat)))
            {
                string exportFile = await SpriteExporter.Export(fragments, sprite, imageFile, format);

                ProjectHelpers.AddFileToProject(imageFile, exportFile);
            }
        }
        private static string GetFileName(string imageFileName, SpriteDocument sprite, ExportFormat format)
        {
            switch (format)
            {
                case ExportFormat.Css:
                    if (sprite.CssOutputDirectory != null)
                        return imageFileName = GetAbsolutePathFromSettings(sprite.CssOutputDirectory, imageFileName, ".css");
                    return imageFileName + ".css";
                case ExportFormat.Less:
                    if (sprite.CssOutputDirectory != null)
                        return imageFileName = GetAbsolutePathFromSettings(sprite.LessOutputDirectory, imageFileName, ".less");
                    return imageFileName + ".less";
                case ExportFormat.Scss:
                    if (sprite.CssOutputDirectory != null)
                        return imageFileName = GetAbsolutePathFromSettings(sprite.ScssOutputDirectory, imageFileName, ".scss");
                    return imageFileName + ".scss";
                case ExportFormat.Json:
                    return imageFileName + ".map";
            }

            return null;
        }
        private static string GetSelector(string fileName, SpriteDocument sprite, ExportFormat format)
        {
            string className = FileHelpers.GetFileNameWithoutExtension(fileName);

            if (sprite.UseFullPathForIdentifierName)
            {
                string withoutExtensionWithDirectoryName = Path.Combine(Path.GetDirectoryName(fileName), className);
                className = string.Join("-", withoutExtensionWithDirectoryName.Split(
                                             new[] { Path.DirectorySeparatorChar,
                                                     Path.AltDirectorySeparatorChar }));
            }

            if (format == ExportFormat.Less)
                return ".sprite-" + className + "()";
            else if (format == ExportFormat.Scss)
                return "@mixin sprite-" + className + "()";

            return "." + className;
        }