Esempio n. 1
0
        public static ExportResult ExportSpriteMapping(SpriteSheet spriteSheet, List <Animation> animations, string path)
        {
            if (spriteSheet.Frames.Count == 0)
            {
                return(ExportResult.Nothing);
            }

            bool spriteSheetFinalIsCopy = false;

            SpriteSheet spriteSheetFinal;

            if (Configuration.PackSpriteSheetWhenExportingSpriteMap)
            {
                SpriteSheetPacker packer = new SpriteSheetPacker();


                if (packer.PackSpriteSheet(spriteSheet, Configuration.ForcePowTwo, Configuration.ForceSquare,
                                           Configuration.Padding, true, Application.Instance.MaxTexSize.Width,
                                           Application.Instance.MaxTexSize.Height, out spriteSheetFinal))
                {
                    string spriteSheetFileName = Path.GetFileName(spriteSheetFinal.FullPath);
                    string pathToExport        = Path.GetDirectoryName(spriteSheetFinal.FullPath);

                    if (pathToExport != null && spriteSheetFileName != null)
                    {
                        spriteSheetFileName = spriteSheetFileName.Insert((spriteSheetFileName.Length - 1) - 3, "_packed");

                        pathToExport = Path.Combine(pathToExport, spriteSheetFileName);
                        spriteSheetFinal.FullPath = pathToExport;


                        spriteSheetFinalIsCopy = true;
                    }
                    else
                    {
                        return(ExportResult.Nothing);
                    }
                }
                else
                {
                    return(ExportResult.Nothing);
                }
            }
            else
            {
                spriteSheetFinal = spriteSheet;
            }

            bool noFrames = false;


            XMLWriter xmlWriter = new XMLWriter(path);

            xmlWriter.CreateDocWithRoot("SpriteMapping", "::Sprite Mapping generated by Sprite Vortex::");

            xmlWriter.OpenNewElement("Texture");
            xmlWriter.AddAttribute("Path", PathHelper.MakeRelativePath(spriteSheetFinal.FullPath, path));
            xmlWriter.AddAttribute("Name", PathHelper.GetFileName(spriteSheetFinal.FullPath));
            xmlWriter.CloseCurrentElement();


            xmlWriter.OpenNewElement("TransparentColors");


            if (!Configuration.OverwriteImageWhenTransparencyModified)
            {
                if (spriteSheet.TransparentColors.Count > 0)
                {
                    foreach (var color in spriteSheetFinal.TransparentColors)
                    {
                        xmlWriter.AddCompleteElementWithAttribute("Color", "Value", color.Argb.ToString());
                    }
                }
            }


            xmlWriter.CloseCurrentElement();

            xmlWriter.OpenNewElement("Sprites");
            foreach (SpriteSheetFrame spriteSheetFrame in spriteSheetFinal.Frames)
            {
                xmlWriter.OpenNewElement("Sprite");
                xmlWriter.AddAttribute("Name", spriteSheetFrame.TagName);
                xmlWriter.AddAttribute("Id", spriteSheetFrame.Id.ToString());

                xmlWriter.OpenNewElement("Coordinates");

                xmlWriter.AddCompleteElmentWithInnerText("X", spriteSheetFrame.X.ToString());
                xmlWriter.AddCompleteElmentWithInnerText("Y", spriteSheetFrame.Y.ToString());
                xmlWriter.AddCompleteElmentWithInnerText("Width", spriteSheetFrame.Width.ToString());
                xmlWriter.AddCompleteElmentWithInnerText("Height", spriteSheetFrame.Height.ToString());

                xmlWriter.CloseCurrentElement();

                xmlWriter.CloseCurrentElement();
            }
            xmlWriter.CloseCurrentElement();

            if (animations != null)
            {
                bool hasFrames = false;
                foreach (var animation in animations)
                {
                    if (animation.Frames.Count > 0)
                    {
                        hasFrames = true;
                        break;
                    }
                }

                if (hasFrames)
                {
                    xmlWriter.OpenNewElement("Animations");

                    foreach (var animation in animations)
                    {
                        xmlWriter.OpenNewElement("Animation");
                        xmlWriter.AddAttribute("Name", animation.Name);
                        xmlWriter.AddAttribute("FrameRate", animation.FrameRate.ToString());
                        xmlWriter.AddAttribute("Loop", animation.Loop.ToString());
                        xmlWriter.AddAttribute("PingPong", animation.PingPong.ToString());

                        xmlWriter.OpenNewElement("Frames");

                        foreach (var frame in animation.Frames)
                        {
                            xmlWriter.OpenNewElement("Frame");
                            xmlWriter.AddAttribute("SpriteId", frame.SpriteFrame.SpriteFrameId.ToString());
                            xmlWriter.AddAttribute("OriginX", frame.OriginX.ToString());
                            xmlWriter.AddAttribute("OriginY", frame.OriginY.ToString());
                            xmlWriter.AddAttribute("OffSetX", frame.OffSetX.ToString());
                            xmlWriter.AddAttribute("OffSetY", frame.OffSetY.ToString());
                            xmlWriter.AddAttribute("Duration", GlobalizationHelper.FormatNumber(frame.FrameDuration));

                            xmlWriter.CloseCurrentElement();
                        }

                        xmlWriter.CloseCurrentElement();

                        xmlWriter.CloseCurrentElement();
                    }
                }
                else
                {
                    noFrames = true;
                }
            }


            if (Configuration.PackSpriteSheetWhenExportingSpriteMap ||
                (Configuration.OverwriteImageWhenTransparencyModified && spriteSheetFinal.TransparentColors.Count > 0))
            {
                ExportSpriteSheetImage(spriteSheetFinal, spriteSheetFinal.FullPath);
            }

            xmlWriter.Save();


            if (spriteSheetFinalIsCopy)
            {
                spriteSheetFinal.Texture.Dispose();
            }


            return(noFrames ? ExportResult.ExportedOnlySpriteMap : ExportResult.ExportedAll);
        }
Esempio n. 2
0
        public static void WriteConfig(ConfigData data, string file)
        {
            XMLWriter configWriter = new XMLWriter(file);

            configWriter.CreateDocWithRoot("Settings", "Configuration definition of Sprite Vortex");
            //-----------------------------------------------------------------------------------------------------
            configWriter.OpenNewElement("GeneralSettings");


            //-----------------------------------------------------------------------------------------------------
            configWriter.AddCompleteElementWithAttribute("CameraSpeed", "Value",
                                                         GlobalizationHelper.FormatNumber(data.CameraSpeed));
            configWriter.AddCompleteElementWithAttribute("BackgroundColor", "Value",
                                                         data.BackgroundColor.Argb.ToString());
            configWriter.AddCompleteElementWithAttribute("FrameRectColor", "Value", data.FrameRectColor.Argb.ToString());
            configWriter.AddCompleteElementWithAttribute("HoverFrameRectColor", "Value",
                                                         data.HoverFrameRectColor.Argb.ToString());
            configWriter.AddCompleteElementWithAttribute("SelectedFrameRectColor", "Value",
                                                         data.SelectedFrameRectColor.Argb.ToString());
            configWriter.AddCompleteElementWithAttribute("TextureFilterMode", "Value",
                                                         Enum.GetName(typeof(TextureFilter), data.TextureFilterMode));
            configWriter.AddCompleteElementWithAttribute("FrameMargin", "Value",
                                                         GlobalizationHelper.FormatNumber(data.FrameMargin));
            //-----------------------------------------------------------------------------------------------------

            configWriter.CloseCurrentElement();
            //-----------------------------------------------------------------------------------------------------
            //-----------------------------------------------------------------------------------------------------

            configWriter.OpenNewElement("CommandSettings");
            //-----------------------------------------------------------------------------------------------------

            configWriter.OpenNewElement("SpriteMarkup");
            configWriter.AddAttribute("Key",
                                      data.SpriteMarkUpControl.Key != null
                                          ? data.SpriteMarkUpControl.Key.ToString()
                                          : "none");
            configWriter.AddAttribute("Button", Enum.GetName(typeof(MouseButton), data.SpriteMarkUpControl.MouseButton));
            configWriter.CloseCurrentElement();
            //-----------------------------------------------------------------------------------------------------
            //-----------------------------------------------------------------------------------------------------
            configWriter.OpenNewElement("DragCamera");
            configWriter.AddAttribute("Key",
                                      data.DragCameraControl.Key != null
                                          ? data.DragCameraControl.Key.ToString()
                                          : "none");
            configWriter.AddAttribute("Button", Enum.GetName(typeof(MouseButton), data.DragCameraControl.MouseButton));
            configWriter.CloseCurrentElement();
            //-----------------------------------------------------------------------------------------------------
            //-----------------------------------------------------------------------------------------------------
            configWriter.OpenNewElement("SelectSprite");
            configWriter.AddAttribute("Key",
                                      data.SelectSpriteControl.Key != null
                                          ? data.SelectSpriteControl.Key.ToString()
                                          : "none");
            configWriter.AddAttribute("Button", Enum.GetName(typeof(MouseButton), data.SelectSpriteControl.MouseButton));
            configWriter.CloseCurrentElement();
            //-----------------------------------------------------------------------------------------------------
            //-----------------------------------------------------------------------------------------------------
            configWriter.OpenNewElement("ViewZoom");
            configWriter.AddAttribute("Key",
                                      data.ViewZoomControl.Key != null ? data.ViewZoomControl.Key.ToString() : "none");
            configWriter.AddAttribute("Button", Enum.GetName(typeof(MouseButton), data.ViewZoomControl.MouseButton));
            configWriter.CloseCurrentElement();
            //-----------------------------------------------------------------------------------------------------


            configWriter.CloseCurrentElement();

            //-----------------------------------------------------------------------------------------------------
            configWriter.OpenNewElement("ExportingSettings");

            //-----------------------------------------------------------------------------------------------------
            configWriter.OpenNewElement("ImageOverwriteWhenTransparencyModified");
            configWriter.AddAttribute("Value", data.OverwriteImageWhenTransparencyModified.ToString());
            configWriter.CloseCurrentElement();
            //-----------------------------------------------------------------------------------------------------

            //-----------------------------------------------------------------------------------------------------
            configWriter.OpenNewElement("PackSpriteSheet");
            configWriter.AddAttribute("Value", data.PackSpriteSheetWhenExportingSpriteMap.ToString());
            configWriter.CloseCurrentElement();
            //-----------------------------------------------------------------------------------------------------

            //-----------------------------------------------------------------------------------------------------
            configWriter.OpenNewElement("ForcePowTwo");
            configWriter.AddAttribute("Value", data.ForcePowTwo.ToString());
            configWriter.CloseCurrentElement();
            //-----------------------------------------------------------------------------------------------------


            //-----------------------------------------------------------------------------------------------------
            configWriter.OpenNewElement("ForceSquare");
            configWriter.AddAttribute("Value", data.ForceSquare.ToString());
            configWriter.CloseCurrentElement();
            //-----------------------------------------------------------------------------------------------------

            //-----------------------------------------------------------------------------------------------------
            configWriter.OpenNewElement("Padding");
            configWriter.AddAttribute("Value", data.Padding.ToString());
            configWriter.CloseCurrentElement();
            //-----------------------------------------------------------------------------------------------------

            configWriter.CloseCurrentElement();

            configWriter.Save();
        }