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