PageRenderTime 69ms CodeModel.GetById 8ms app.highlight 12ms RepoModel.GetById 47ms app.codeStats 0ms

/XSpriterPipelineExtensions/XmlHelpers.cs

https://bitbucket.org/dylanwolf/xspriter
C# | 204 lines | 180 code | 22 blank | 2 comment | 20 complexity | da42a8d507c6d1713e49eac8c6c014e6 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Diagnostics;
  4using System.Drawing;
  5using System.IO;
  6using System.Linq;
  7using System.Text;
  8using System.Xml.Linq;
  9using Microsoft.Xna.Framework;
 10
 11namespace FuncWorks.XNA.XSpriter
 12{
 13    static class XmlHelpers
 14    {
 15        public static ImageContent FromImageXml(XElement file, string basePath)
 16        {
 17            ImageContent img = new ImageContent();
 18
 19            img.TextureName = file.Attribute("name").Value;
 20
 21            // Get dimensions
 22            string sourceName = Path.Combine(basePath, file.Attribute("name").Value);
 23            using (Bitmap bitmap = (Bitmap)System.Drawing.Image.FromFile(sourceName))
 24            {
 25                img.Dimensions.X = bitmap.Width;
 26                img.Dimensions.Y = bitmap.Height;
 27            }
 28
 29            // Adjust pivot from UV to pixel coordinates and XNA screen space
 30            img.Pivot.X = img.Dimensions.X*GetFloatAttribute(file, "pivot_x", 0);
 31            img.Pivot.Y = img.Dimensions.Y*(1-GetFloatAttribute(file, "pivot_y", 1));
 32
 33            return img;
 34        }
 35
 36        public static Bone FromBoneXml(XElement xml)
 37        {
 38            Vector2 position = new Vector2(
 39                GetFloatAttribute(xml, "x", 0),
 40                -GetFloatAttribute(xml, "y", 0)
 41                );
 42
 43            Vector2 scale = new Vector2(
 44                GetFloatAttribute(xml, "scale_x", 1),
 45                GetFloatAttribute(xml, "scale_y", 1)
 46                );
 47
 48
 49            Bone bone = new Bone()
 50                            {
 51                                Id = GetInt32Attribute(xml, "bone", 0),
 52                                Parent = GetInt32Attribute(xml, "parent", -1),
 53                                Angle = -MathHelper.ToRadians(GetFloatAttribute(xml, "angle", 0)),
 54                                Scale = scale,
 55                                Clockwise =
 56                                    xml.Parent.Attribute("spin") != null &&
 57                                    xml.Parent.Attribute("spin").Value == "-1",
 58                                Position = position,
 59                                Name =
 60                                    (xml.Parent.Parent.Name == "timeline" &&
 61                                        xml.Parent.Parent.Attribute("name") != null)
 62                                        ? xml.Parent.Parent.Attribute("name").Value
 63                                        : null
 64                            };
 65            return bone;
 66        }
 67
 68        public static FrameImageContent FromObjectXml(XElement xml, ImageContent[][] textures, bool tweened)
 69        {
 70            int folderId = int.Parse(xml.Attribute("folder").Value);
 71            int fileId = int.Parse(xml.Attribute("file").Value);
 72
 73            Vector2 pivot = Vector2.Zero;
 74            if (xml.Attribute("pivot_x") != null || xml.Attribute("pivot_y") != null)
 75            {
 76                pivot = new Vector2(
 77                    GetFloatAttribute(xml, "pivot_x", 0) * textures[folderId][fileId].Dimensions.X,
 78                    (1 - GetFloatAttribute(xml, "pivot_y", 1)) * textures[folderId][fileId].Dimensions.Y
 79                    );
 80            }
 81            else
 82            {
 83                try
 84                {
 85                    pivot =
 86                        textures[int.Parse(xml.Attribute("folder").Value)][int.Parse(xml.Attribute("file").Value)].Pivot;
 87                }
 88                catch
 89                {
 90                    
 91                }
 92            }
 93
 94            Vector2 position = new Vector2(
 95                GetFloatAttribute(xml, "x", 0),
 96                -GetFloatAttribute(xml, "y", 0)
 97                );
 98
 99            Vector2 scale = new Vector2(
100                GetFloatAttribute(xml, "scale_x", 1),
101                GetFloatAttribute(xml, "scale_y", 1)
102                );
103
104            FrameImageContent frameImage = new FrameImageContent()
105                {
106                    Angle = -MathHelper.ToRadians(GetFloatAttribute(xml, "angle", 0)),
107                    Pivot = pivot,
108                    Position = position,
109                    TextureFolder = folderId,
110                    TextureFile = fileId,
111                    TextureName = textures[folderId][fileId].TextureName,
112                    Clockwise = xml.Parent.Attribute("spin") != null && xml.Parent.Attribute("spin").Value == "-1",
113                    Tweened = tweened,
114                    ZIndex = GetInt32Attribute(xml, "z_index", 0),
115                    Scale = scale
116                };
117
118            return frameImage;
119        }
120
121        public static Int32 GetInt32Attribute(XElement xml, string attributeName, Int32 defaultValue)
122        {
123            try
124            {
125                return xml.Attribute(attributeName) != null
126                           ? Int32.Parse(xml.Attribute(attributeName).Value)
127                           : defaultValue;
128            }
129            catch (Exception ex)
130            {
131                throw new FormatException(String.Format("Could not convert attribute {0} value '{1}' to Int32: {2}: {3}",
132                    attributeName,
133                    xml.Attribute(attributeName) != null ? xml.Attribute(attributeName).Value : "NULL",
134                    ex.GetType().FullName, ex.Message));
135            }
136        }
137
138        public static Int32? GetNullableInt32Attribute(XElement xml, string attributeName)
139        {
140            try
141            {
142                return xml.Attribute(attributeName) != null
143                           ? (Int32?)Int32.Parse(xml.Attribute(attributeName).Value)
144                           : null;
145            }
146            catch (Exception)
147            {
148
149                return null;
150            }
151        }
152
153        public static float GetFloatAttribute(XElement xml, string attributeName, int defaultValue)
154        {
155            try
156            {
157                return xml.Attribute(attributeName) != null
158                           ? float.Parse(xml.Attribute(attributeName).Value)
159                           : defaultValue;
160            }
161            catch (Exception ex)
162            {
163                throw new FormatException(String.Format("Could not convert attribute {0} value '{1}' to float: {2}: {3}",
164                    attributeName,
165                    xml.Attribute(attributeName) != null ? xml.Attribute(attributeName).Value : "NULL",
166                    ex.GetType().FullName, ex.Message));
167            }
168        }
169
170        public static Int64 GetInt64Attribute(XElement xml, string attributeName, Int64 defaultValue)
171        {
172            try
173            {
174                return xml.Attribute(attributeName) != null
175                           ? Int64.Parse(xml.Attribute(attributeName).Value)
176                           : defaultValue;
177            }
178            catch (Exception ex)
179            {
180                throw new FormatException(String.Format("Could not convert attribute {0} value '{1}' to Int64: {2}: {3}",
181                    attributeName,
182                    xml.Attribute(attributeName) != null ? xml.Attribute(attributeName).Value : "NULL",
183                    ex.GetType().FullName, ex.Message));
184            }
185        }
186
187        public static bool GetBoolAttribute(XElement xml, string attributeName, bool defaultValue)
188        {
189            try
190            {
191                return xml.Attribute(attributeName) != null
192                           ? bool.Parse(xml.Attribute(attributeName).Value)
193                           : defaultValue;
194            }
195            catch (Exception ex)
196            {
197                throw new FormatException(String.Format("Could not convert attribute {0} value '{1}' to bool: {2}: {3}",
198                    attributeName,
199                    xml.Attribute(attributeName) != null ? xml.Attribute(attributeName).Value : "NULL",
200                    ex.GetType().FullName, ex.Message));
201            }
202        }
203    }
204}