xspriter /XSpriterPipelineExtensions/XmlHelpers.cs

Language C# Lines 205
MD5 Hash da42a8d507c6d1713e49eac8c6c014e6 Estimated Cost $4,464 (why?)
Repository https://bitbucket.org/dylanwolf/xspriter View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework;

namespace FuncWorks.XNA.XSpriter
{
    static class XmlHelpers
    {
        public static ImageContent FromImageXml(XElement file, string basePath)
        {
            ImageContent img = new ImageContent();

            img.TextureName = file.Attribute("name").Value;

            // Get dimensions
            string sourceName = Path.Combine(basePath, file.Attribute("name").Value);
            using (Bitmap bitmap = (Bitmap)System.Drawing.Image.FromFile(sourceName))
            {
                img.Dimensions.X = bitmap.Width;
                img.Dimensions.Y = bitmap.Height;
            }

            // Adjust pivot from UV to pixel coordinates and XNA screen space
            img.Pivot.X = img.Dimensions.X*GetFloatAttribute(file, "pivot_x", 0);
            img.Pivot.Y = img.Dimensions.Y*(1-GetFloatAttribute(file, "pivot_y", 1));

            return img;
        }

        public static Bone FromBoneXml(XElement xml)
        {
            Vector2 position = new Vector2(
                GetFloatAttribute(xml, "x", 0),
                -GetFloatAttribute(xml, "y", 0)
                );

            Vector2 scale = new Vector2(
                GetFloatAttribute(xml, "scale_x", 1),
                GetFloatAttribute(xml, "scale_y", 1)
                );


            Bone bone = new Bone()
                            {
                                Id = GetInt32Attribute(xml, "bone", 0),
                                Parent = GetInt32Attribute(xml, "parent", -1),
                                Angle = -MathHelper.ToRadians(GetFloatAttribute(xml, "angle", 0)),
                                Scale = scale,
                                Clockwise =
                                    xml.Parent.Attribute("spin") != null &&
                                    xml.Parent.Attribute("spin").Value == "-1",
                                Position = position,
                                Name =
                                    (xml.Parent.Parent.Name == "timeline" &&
                                        xml.Parent.Parent.Attribute("name") != null)
                                        ? xml.Parent.Parent.Attribute("name").Value
                                        : null
                            };
            return bone;
        }

        public static FrameImageContent FromObjectXml(XElement xml, ImageContent[][] textures, bool tweened)
        {
            int folderId = int.Parse(xml.Attribute("folder").Value);
            int fileId = int.Parse(xml.Attribute("file").Value);

            Vector2 pivot = Vector2.Zero;
            if (xml.Attribute("pivot_x") != null || xml.Attribute("pivot_y") != null)
            {
                pivot = new Vector2(
                    GetFloatAttribute(xml, "pivot_x", 0) * textures[folderId][fileId].Dimensions.X,
                    (1 - GetFloatAttribute(xml, "pivot_y", 1)) * textures[folderId][fileId].Dimensions.Y
                    );
            }
            else
            {
                try
                {
                    pivot =
                        textures[int.Parse(xml.Attribute("folder").Value)][int.Parse(xml.Attribute("file").Value)].Pivot;
                }
                catch
                {
                    
                }
            }

            Vector2 position = new Vector2(
                GetFloatAttribute(xml, "x", 0),
                -GetFloatAttribute(xml, "y", 0)
                );

            Vector2 scale = new Vector2(
                GetFloatAttribute(xml, "scale_x", 1),
                GetFloatAttribute(xml, "scale_y", 1)
                );

            FrameImageContent frameImage = new FrameImageContent()
                {
                    Angle = -MathHelper.ToRadians(GetFloatAttribute(xml, "angle", 0)),
                    Pivot = pivot,
                    Position = position,
                    TextureFolder = folderId,
                    TextureFile = fileId,
                    TextureName = textures[folderId][fileId].TextureName,
                    Clockwise = xml.Parent.Attribute("spin") != null && xml.Parent.Attribute("spin").Value == "-1",
                    Tweened = tweened,
                    ZIndex = GetInt32Attribute(xml, "z_index", 0),
                    Scale = scale
                };

            return frameImage;
        }

        public static Int32 GetInt32Attribute(XElement xml, string attributeName, Int32 defaultValue)
        {
            try
            {
                return xml.Attribute(attributeName) != null
                           ? Int32.Parse(xml.Attribute(attributeName).Value)
                           : defaultValue;
            }
            catch (Exception ex)
            {
                throw new FormatException(String.Format("Could not convert attribute {0} value '{1}' to Int32: {2}: {3}",
                    attributeName,
                    xml.Attribute(attributeName) != null ? xml.Attribute(attributeName).Value : "NULL",
                    ex.GetType().FullName, ex.Message));
            }
        }

        public static Int32? GetNullableInt32Attribute(XElement xml, string attributeName)
        {
            try
            {
                return xml.Attribute(attributeName) != null
                           ? (Int32?)Int32.Parse(xml.Attribute(attributeName).Value)
                           : null;
            }
            catch (Exception)
            {

                return null;
            }
        }

        public static float GetFloatAttribute(XElement xml, string attributeName, int defaultValue)
        {
            try
            {
                return xml.Attribute(attributeName) != null
                           ? float.Parse(xml.Attribute(attributeName).Value)
                           : defaultValue;
            }
            catch (Exception ex)
            {
                throw new FormatException(String.Format("Could not convert attribute {0} value '{1}' to float: {2}: {3}",
                    attributeName,
                    xml.Attribute(attributeName) != null ? xml.Attribute(attributeName).Value : "NULL",
                    ex.GetType().FullName, ex.Message));
            }
        }

        public static Int64 GetInt64Attribute(XElement xml, string attributeName, Int64 defaultValue)
        {
            try
            {
                return xml.Attribute(attributeName) != null
                           ? Int64.Parse(xml.Attribute(attributeName).Value)
                           : defaultValue;
            }
            catch (Exception ex)
            {
                throw new FormatException(String.Format("Could not convert attribute {0} value '{1}' to Int64: {2}: {3}",
                    attributeName,
                    xml.Attribute(attributeName) != null ? xml.Attribute(attributeName).Value : "NULL",
                    ex.GetType().FullName, ex.Message));
            }
        }

        public static bool GetBoolAttribute(XElement xml, string attributeName, bool defaultValue)
        {
            try
            {
                return xml.Attribute(attributeName) != null
                           ? bool.Parse(xml.Attribute(attributeName).Value)
                           : defaultValue;
            }
            catch (Exception ex)
            {
                throw new FormatException(String.Format("Could not convert attribute {0} value '{1}' to bool: {2}: {3}",
                    attributeName,
                    xml.Attribute(attributeName) != null ? xml.Attribute(attributeName).Value : "NULL",
                    ex.GetType().FullName, ex.Message));
            }
        }
    }
}
Back to Top