public LoadSprite ( System.Xml.Linq.XElement spriteValues, string graphicDirectory, string &reason ) : bool | ||
spriteValues | System.Xml.Linq.XElement | |
graphicDirectory | string | |
reason | string | |
return | bool |
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;
}
public static bool Initialize(DirectoryInfo directory, out string reason) { if (_initalized) { reason = null; return true; } Sprites = new Dictionary<string, BaseSprite>(); string file = Path.Combine(directory.FullName, "sprites.xml"); string graphicDirectory = Path.Combine(directory.FullName, "graphics"); if (!File.Exists(file)) { reason = "Sprites.xml file does not exist"; return false; } XDocument doc = XDocument.Load(file); XElement root = doc.Element("Sprites"); foreach (XElement sprite in root.Elements()) { var newSprite = new BaseSprite(); if (!newSprite.LoadSprite(sprite, graphicDirectory, out reason)) { return false; } Sprites.Add(newSprite.Name, newSprite); } _initalized = true; reason = null; return true; }