using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace spritesheetanimation
{
// Animation frame class
public class Frame
{
// Frame Number
[XmlAttribute("Num")]
public int Num;
// Sub Image X positon in the Sprite Sheet
[XmlAttribute("X")]
public int X;
// Sub Image Y positon in the Sprite Sheet
[XmlAttribute("Y")]
public int Y;
// Sub Image Width
[XmlAttribute("Width")]
public int Width;
// Sub Image Height
[XmlAttribute("Height")]
public int Height;
// The X offset of sub image
[XmlAttribute("OffSetX")]
public int OffsetX;
// The Y offset fo sub image
[XmlAttribute("OffsetY")]
public int OffsetY;
// The duration between two frames
[XmlAttribute("Duration")]
public float Duration;
}
// Animaiton class to hold the name and frames
public class Animation
{
// Animation Name
[XmlAttribute("Name")]
public string Name;
// Animation Frame Rate
[XmlAttribute("FrameRate")]
public int FrameRate;
public bool Loop;
public bool Pingpong;
// The Frames array in an animation
[XmlArray("Frames"), XmlArrayItem("Frame", typeof(Frame))]
public Frame[] Frames;
}
// The Sprite Texture stores the Sprite Sheet path.fr
public class SpriteTexture
{
// The Sprite Sheet texture file path
[XmlAttribute("Path")]
public string Path;
}
// Aniamtion Set contains the Sprite Texture and Animaitons.
[XmlRoot("Animations")]
public class AnimationSet
{
// The sprite texture object
[XmlElement("Texture", typeof(SpriteTexture))]
public SpriteTexture SpriteTexture;
// The animation array in the Animation Set
[XmlElement("Animation", typeof(Animation))]
public Animation[] Animations;
}
// Sprite Animation Manager class
public static class SpriteAnimationManager
{
public static int AnimationCount;
// Read the Sprite Sheet Description information from the description xml file
public static AnimationSet Read(string Filename)
{
AnimationSet animationSet = new AnimationSet();
// Create a XML reader for the sprite sheet animaiton description file
using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(Filename))
{
// Create a XMLSerializer for the AnimationSet
XmlSerializer serializer = new XmlSerializer(typeof(AnimationSet));
// Deserialize the Animation Set from the XmlReader to the animation set object
animationSet = (AnimationSet)serializer.Deserialize(reader);
}
// Count the animations to Animation Count
AnimationCount = animationSet.Animations.Length;
return animationSet;
}
}
}