Kill chrome.xml.

This commit is contained in:
Paul Chote
2011-03-12 22:05:44 +13:00
parent 0cfbeab864
commit f4494d67b2
6 changed files with 29 additions and 488 deletions

View File

@@ -11,12 +11,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using OpenRA.FileFormats;
namespace OpenRA.Graphics
{
public static class ChromeProvider
{
struct Collection
@@ -34,11 +32,14 @@ namespace OpenRA.Graphics
collections = new Dictionary<string, Collection>();
cachedSheets = new Dictionary<string, Sheet>();
cachedSprites = new Dictionary<string, Dictionary<string, Sprite>>();
foreach (var f in chromeFiles)
LoadChromeSource(f);
Save("foo.yaml");
if (chromeFiles.Length == 0)
return;
var chrome = chromeFiles.Select(s => MiniYaml.FromFile(s)).Aggregate(MiniYaml.Merge);
foreach (var c in chrome)
LoadCollection(c.Key, c.Value);
}
public static void Save(string file)
@@ -59,24 +60,16 @@ namespace OpenRA.Graphics
return new MiniYaml(collection.src, root);
}
static void LoadChromeSource(string filename)
static void LoadCollection(string name, MiniYaml yaml)
{
XmlDocument document = new XmlDocument();
document.Load(FileSystem.Open(filename));
foreach (XmlElement eCollection in document.SelectNodes("/chrome/collection"))
LoadChromeForCollection(eCollection);
}
static void LoadChromeForCollection(XmlElement eCollection)
{
string elementName = eCollection.GetAttribute("name");
string defaultSrc = (eCollection.HasAttribute("src") ? eCollection.GetAttribute("src") : null);
var images = eCollection.SelectNodes("./image").OfType<XmlElement>()
.Select(e => new MappedImage(defaultSrc, e))
.ToDictionary(s => s.Name);
Game.modData.LoadScreen.Display();
var collection = new Collection()
{
src = yaml.Value,
regions = yaml.Nodes.ToDictionary(n => n.Key, n => new MappedImage(yaml.Value, n.Value))
};
collections.Add(elementName, new Collection() {src = defaultSrc, regions = images});
collections.Add(name, collection);
}
public static Sprite GetImage(string collection, string image)
@@ -92,15 +85,15 @@ namespace OpenRA.Graphics
throw new InvalidOperationException(
"Collection `{0}` does not have an image `{1}`".F(collection, image));
}
// Cached sheet
Sheet sheet;
if (cachedSheets.ContainsKey(mi.Src))
sheet = cachedSheets[mi.Src];
if (cachedSheets.ContainsKey(mi.src))
sheet = cachedSheets[mi.src];
else
{
sheet = new Sheet(mi.Src);
cachedSheets.Add(mi.Src, sheet);
sheet = new Sheet(mi.src);
cachedSheets.Add(mi.src, sheet);
}
// Cache the sprite

View File

@@ -18,27 +18,15 @@ namespace OpenRA.Graphics
{
class MappedImage
{
public readonly Rectangle rect;
public readonly string Src;
public readonly string Name;
public readonly Rectangle rect = Rectangle.Empty;
public readonly string src;
public MappedImage(string defaultSrc, XmlElement e)
{
Src = (e.HasAttribute("src")) ? e.GetAttribute("src") : defaultSrc;
Name = e.GetAttribute("name");
if (Src == null)
throw new InvalidDataException("Image src missing");
rect = new Rectangle(int.Parse(e.GetAttribute("x")),
int.Parse(e.GetAttribute("y")),
int.Parse(e.GetAttribute("width")),
int.Parse(e.GetAttribute("height")));
}
public MappedImage(string defaultSrc, MiniYaml info)
{
Src = defaultSrc;
FieldLoader.LoadField(this, "rect", info.Value);
FieldLoader.Load(this, info);
if (src == null)
src = defaultSrc;
}
public Sprite GetImage(Sheet s)
@@ -49,8 +37,8 @@ namespace OpenRA.Graphics
public MiniYaml Save(string defaultSrc)
{
var root = new List<MiniYamlNode>();
if (defaultSrc != Src)
root.Add(new MiniYamlNode("src", Src));
if (defaultSrc != src)
root.Add(new MiniYamlNode("src", src));
return new MiniYaml(FieldSaver.FormatValue( this, this.GetType().GetField("rect") ), root);
}