Exemplo n.º 1
0
        public void Compile()
        {
            ParsedPath     pinboardFileName = Target.InputFiles.Where(f => f.Extension == ".pinboard").First();
            ParsedPath     xnbFileName      = Target.OutputFiles.Where(f => f.Extension == ".xnb").First();
            PinboardFileV1 pinboard         = PinboardFileCache.Load(pinboardFileName);

            Rectangle[] rectangles = new Rectangle[pinboard.RectInfos.Count + 1];

            rectangles[0] = new Rectangle(pinboard.ScreenRectInfo.X, pinboard.ScreenRectInfo.Y, pinboard.ScreenRectInfo.Width, pinboard.ScreenRectInfo.Height);

            for (int i = 0; i < pinboard.RectInfos.Count; i++)
            {
                rectangles[i + 1] = new Rectangle(pinboard.RectInfos[i].X, pinboard.RectInfos[i].Y, pinboard.RectInfos[i].Width, pinboard.RectInfos[i].Height);
            }

            if (!Directory.Exists(xnbFileName.VolumeAndDirectory))
            {
                Directory.CreateDirectory(xnbFileName.VolumeAndDirectory);
            }

            XnbFileWriterV5.WriteFile(rectangles, xnbFileName);
        }
Exemplo n.º 2
0
        public void Compile()
        {
            IEnumerable <ParsedPath> svgFileNames  = Target.InputFiles.Where(f => f.Extension == ".svg");
            ParsedPath            pinboardFileName = Target.InputFiles.Where(f => f.Extension == ".pinboard").First();
            ParsedPath            xnbFileName      = Target.OutputFiles.Where(f => f.Extension == ".xnb").First();
            PinboardFileV1        pinboardFile     = PinboardFileCache.Load(pinboardFileName);
            List <ImagePlacement> placements       = new List <ImagePlacement>();
            string rectangleName = this.Target.Properties.GetRequiredValue("Rectangle");

            PinboardFileV1.RectangleInfo rectInfo = pinboardFile.GetRectangleInfoByName(rectangleName);

            if (rectInfo == null)
            {
                throw new ContentFileException("Rectangle {0} not found in pinboard file {1}".CultureFormat(rectangleName, pinboardFileName));
            }

            string converterName = Target.Properties.GetOptionalValue("Converter", "Inkscape").ToLower();

            if (converterName != "inkscape" && converterName != "rsvg")
            {
                throw new ContentFileException("Unknown SVG converter '{0}'".CultureFormat(converterName));
            }

            ParsedPath combinedPngPathName = xnbFileName.WithExtension(".png");

            try
            {
                int row = 0;

                foreach (var svgFileName in svgFileNames)
                {
                    int col = 0;

                    if (rectInfo == null)
                    {
                        throw new InvalidOperationException(
                                  "Rectangle '{0}' not found in pinboard '{1}'".CultureFormat(rectangleName, pinboardFileName));
                    }

                    // TODO-johnls: This should probably go in an intermediate file directory
                    ParsedPath pngFile = xnbFileName.WithFileAndExtension(String.Format("{0}_{1}_{2}.png",
                                                                                        svgFileName.File, row, col));

                    placements.Add(new ImagePlacement(pngFile,
                                                      new Rectangle(col * rectInfo.Width, row * rectInfo.Height, rectInfo.Width, rectInfo.Height)));

                    switch (converterName)
                    {
                    default:
                    case "rsvg":
                        ImageTools.SvgToPngWithRSvg(svgFileName, pngFile, rectInfo.Width, rectInfo.Height);
                        break;

                    case "inkscape":
                        ImageTools.SvgToPngWithInkscape(svgFileName, pngFile, rectInfo.Width, rectInfo.Height);
                        break;
                    }
                }

                ImageTools.CombinePngs(placements, combinedPngPathName);

                Texture2DContent textureContent;

                ImageTools.CompressPngToTexture2DContent(
                    combinedPngPathName,
                    this.Target.Properties.GetOptionalValue("CompressionType", "None"),
                    out textureContent);

                XnbFileWriterV5.WriteFile(textureContent, xnbFileName);
            }
            finally
            {
                foreach (var placement in placements)
                {
                    if (File.Exists(placement.ImageFile))
                    {
                        File.Delete(placement.ImageFile);
                    }
                }

                if (File.Exists(combinedPngPathName))
                {
                    File.Delete(combinedPngPathName);
                }
            }
        }
Exemplo n.º 3
0
        public void Compile()
        {
            IEnumerable <ParsedPath> svgPaths  = Target.InputFiles.Where(f => f.Extension == ".svg");
            ParsedPath            pinboardPath = Target.InputFiles.Where(f => f.Extension == ".pinboard").First();
            ParsedPath            pngPath      = Target.OutputFiles.Where(f => f.Extension == ".png").First();
            PinboardFileV1        pinboardFile = PinboardFileCache.Load(pinboardPath);
            List <ImagePlacement> placements   = new List <ImagePlacement>();

            string[] rectangleNames;

            Target.Properties.GetRequiredValue("Rectangles", out rectangleNames);

            if (svgPaths.Count() != rectangleNames.Length)
            {
                throw new ContentFileException("Number of .svg files ({0}) does match number of RectangleNames ({1})"
                                               .CultureFormat(svgPaths.Count(), rectangleNames.Length));
            }

            string s = Target.Properties.GetOptionalValue("Rotation", "None");

            ImageRotation rotation;

            if (!Enum.TryParse(s, out rotation))
            {
                throw new ContentFileException("Invalid value '{0}' for given for rotation.  Valid are None, Left, Right, UpsideDown".CultureFormat(s));
            }

            int i = 0;

            try
            {
                if (!Directory.Exists(pngPath.VolumeAndDirectory))
                {
                    Directory.CreateDirectory(pngPath.VolumeAndDirectory);
                }

                foreach (var svgPath in svgPaths)
                {
                    PinboardFileV1.RectangleInfo rectInfo = pinboardFile.GetRectangleInfoByName(rectangleNames[i]);
                    ParsedPath tempPngPath = pngPath.WithFileAndExtension(String.Format("{0}_{1}.png", pngPath.File, i));

                    if (rectInfo == null)
                    {
                        throw new ContentFileException("Rectangle '{0}' not found in pinboard file '{1}'"
                                                       .CultureFormat(rectangleNames[i], pinboardFile));
                    }

                    ImageTools.SvgToPngWithInkscape(svgPath, tempPngPath, rectInfo.Width, rectInfo.Height);

                    placements.Add(new ImagePlacement(
                                       tempPngPath, new Cairo.Rectangle(rectInfo.X, rectInfo.Y, rectInfo.Width, rectInfo.Height)));

                    i++;
                }

                ImageTools.CombinePngs(placements, pngPath);
                ImageTools.RotatePng(pngPath, rotation);
            }
            finally
            {
                foreach (var placement in placements)
                {
                    if (File.Exists(placement.ImageFile))
                    {
                        File.Delete(placement.ImageFile);
                    }
                }
            }
        }