public bool LoadSprite(XElement spriteValues, string graphicDirectory, out string reason)
{
string filename = null;
Name = null;
foreach (var attribute in spriteValues.Attributes())
{
//Get the graphic file
switch (attribute.Name.ToString().ToLower())
{
case "file":
{
filename = attribute.Value;
break;
}
case "name":
{
Name = attribute.Value;
break;
}
case "stretchmode":
{
if (attribute.Value == "repeat")
{
stretchMode = StretchMode.REPEAT;
}
break;
}
}
}
if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(filename))
{
reason = "No sprite name or file location.";
return false;
}
Sprite graphicFile;
if (ImageCache.Images.Contains(Name))
{
graphicFile = new Sprite(Name, ImageCache.Images[Name]);
}
else
{
string path = Path.Combine(graphicDirectory, filename);
if (!File.Exists(path))
{
reason = "Graphic File doesn't exist: " + path;
return false;
}
graphicFile = new Sprite(Name, Image.FromFile(Path.Combine(graphicDirectory, filename)));
}
int frameCount = 0;
foreach (var element in spriteValues.Elements())
{
int x = 0;
int y = 0;
int width = 0;
int height = 0;
int axisX = 0;
int axisY = 0;
string frameLength = null;
//Process each frame
foreach (var attribute in element.Attributes())
{
switch (attribute.Name.ToString().ToLower())
{
case "x":
{
x = int.Parse(attribute.Value);
break;
}
case "y":
{
y = int.Parse(attribute.Value);
break;
}
case "width":
{
width = int.Parse(attribute.Value);
break;
}
case "height":
{
height = int.Parse(attribute.Value);
break;
}
case "framelength":
{
frameLength = attribute.Value;
break;
}
case "axisx":
{
axisX = int.Parse(attribute.Value);
break;
}
case "axisy":
{
axisY = int.Parse(attribute.Value);
break;
}
}
}
var frame = new Sprite(Name + frameCount, graphicFile.Image, x, y, width, height, axisX,
axisY);
if (stretchMode == StretchMode.REPEAT)
{
frame.WrapMode = ImageAddressing.Wrapping;
}
Frames.Add(frame);
FrameLength.Add(frameLength);
frameCount++;
}
reason = null;
return true;
}