2013年1月18日金曜日

TiledMapEditor 読み込みコードサンプル (C#)

TiledMapEditorの出力ファイル(csv形式)を読み込むコードを書いてみる。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace TmxLoadTest
{
/// <summary>
/// レイヤーデータ
/// </summary>
public class LayerData
{
private Dictionary<string, string> Properties = new Dictionary<string, string>();
public string Name { get; private set; }
public int Width { get; private set; }
public int Height { get; private set; }
public string Encoding { get; private set; }
public int[,] Data { get; private set; }
/// <summary>
/// コンストラクタ
/// </summary>
public LayerData()
{
}
/// <summary>
/// プロパティを取得
/// </summary>
/// <param name="name"></param>
/// <param name="empty"></param>
/// <returns></returns>
public string GetPropertie(string name, string empty = "")
{
if (!Properties.ContainsKey(name))
{
return Properties[name];
}
return empty;
}
/// <summary>
/// プロパティを数値で取得
/// </summary>
/// <param name="name"></param>
/// <param name="empty"></param>
/// <returns></returns>
public int GetPropertieAsInt(string name, int empty = -1)
{
if (!Properties.ContainsKey(name))
{
int result;
if (int.TryParse(Properties[name], out result))
{
return result;
}
}
return empty;
}
/// <summary>
/// データの設定
/// </summary>
/// <param name="data"></param>
public void SetData(string data)
{
Data = new int[Width, Height];
data = data.Trim('\n');
var lines = data.Split('\n');
for (int y = 0; y < Height; y++)
{
var s = lines[y].Split(',');
for (int x = 0; x < Width; x++)
{
int result;
if (int.TryParse(s[x], out result))
{
Data[x, y] = result;
}
}
}
}
/// <summary>
/// プロパティの読み込み
/// </summary>
/// <param name="r"></param>
private void LoadProperties(XmlReader r)
{
Properties.Clear();
do
{
if (r.NodeType == XmlNodeType.Element)
{
switch (r.LocalName)
{
case "properties":
break;
case "property":
if (r.HasAttributes)
{
string name = "";
string value = "";
for (int i = 0; i < r.AttributeCount; i++)
{
r.MoveToAttribute(i);
switch (r.Name)
{
case "name":
name = r.Value;
break;
case "value":
value = r.Value;
if (!Properties.ContainsKey(name))
{
Properties.Add(name, value);
}
break;
}
}
}
break;
}
}
if (r.NodeType == XmlNodeType.EndElement)
{
if (r.LocalName == "properties")
break;
}
}
while (r.Read());
}
/// <summary>
/// レイヤーの読み込み
/// </summary>
/// <param name="file"></param>
public void Load(string file)
{
var s = new XmlReaderSettings();
s.ConformanceLevel = ConformanceLevel.Document;
using (var r = XmlReader.Create(file, s))
{
Load(r);
}
}
/// <summary>
/// レイヤーの読み込み
/// </summary>
/// <param name="r"></param>
public void Load(XmlReader r)
{
do
{
if (r.NodeType == XmlNodeType.Element)
{
switch (r.LocalName)
{
case "layer":
if (r.HasAttributes)
{
int result;
for (int i = 0; i < r.AttributeCount; i++)
{
r.MoveToAttribute(i);
switch (r.Name)
{
case "name":
Name = r.Value;
break;
case "width":
if (int.TryParse(r.Value, out result))
{
Width = result;
}
break;
case "height":
if (int.TryParse(r.Value, out result))
{
Height = result;
}
break;
}
}
}
break;
case "properties":
LoadProperties(r);
break;
case "data":
if (r.HasAttributes)
{
for (int i = 0; i < r.AttributeCount; i++)
{
r.MoveToAttribute(i);
switch (r.Name)
{
case "encoding":
Encoding = r.Value;
break;
}
}
r.MoveToElement();
}
SetData(r.ReadElementContentAsString());
break;
}
}
if (r.NodeType == XmlNodeType.EndElement)
{
if (r.LocalName == "layer")
break;
}
}
while (r.Read());
}
/// <summary>
/// 確認用
/// </summary>
/// <returns></returns>
public override string ToString()
{
StringBuilder s = new StringBuilder();
s.AppendFormat("Name:{0}\r\n", Name);
s.AppendFormat("Width:{0}\r\n", Width);
s.AppendFormat("Height:{0}\r\n", Height);
s.AppendFormat("Encoding:{0}\r\n", Encoding);
foreach (var p in Properties)
{
s.AppendFormat("{0}:{1}\r\n", p.Key, p.Value);
}
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
s.AppendFormat("{0}", Data[x, y]);
if (x > 0)
{
s.Append(",");
}
}
s.AppendLine();
}
return s.ToString();
}
}
/// <summary>
/// 画像データ
/// </summary>
public class ImageData
{
public string Source { get; private set; }
public int Width { get; private set; }
public int Height { get; private set; }
/// <summary>
/// タイルセットの読み込み
/// </summary>
/// <param name="r"></param>
public void Load(XmlReader r)
{
do
{
if (r.NodeType == XmlNodeType.Element)
{
switch (r.LocalName)
{
case "image":
if (r.HasAttributes)
{
int result;
for (int i = 0; i < r.AttributeCount; i++)
{
r.MoveToAttribute(i);
switch (r.Name)
{
case "source":
Source = r.Value;
break;
case "width":
if (int.TryParse(r.Value, out result))
{
Width = result;
}
break;
case "height":
if (int.TryParse(r.Value, out result))
{
Height = result;
}
break;
}
}
r.MoveToElement();
}
break;
}
}
if (r.NodeType == XmlNodeType.EndElement)
{
if (r.LocalName == "tileset")
break;
}
}
while (r.Read());
}
/// <summary>
/// 確認用
/// </summary>
/// <returns></returns>
public override string ToString()
{
StringBuilder s = new StringBuilder();
s.AppendFormat("Source:{0}\r\n", Source);
s.AppendFormat("Width:{0}\r\n", Width);
s.AppendFormat("Height:{0}\r\n", Height);
return s.ToString();
}
}
/// <summary>
/// タイルセットデータ
/// </summary>
public class TileSetData
{
private Dictionary<string, string> Properties = new Dictionary<string, string>();
public int FierstGid { get; private set; }
public string Name { get; private set; }
public int TileWidth { get; private set; }
public int TileHeight { get; private set; }
public ImageData Image { get; private set; }
/// <summary>
/// コンストラクタ
/// </summary>
public TileSetData()
{
Image = new ImageData();
}
/// <summary>
/// プロパティを取得
/// </summary>
/// <param name="name"></param>
/// <param name="empty"></param>
/// <returns></returns>
public string GetPropertie(string name, string empty = "")
{
if (!Properties.ContainsKey(name))
{
return Properties[name];
}
return empty;
}
/// <summary>
/// プロパティを数値で取得
/// </summary>
/// <param name="name"></param>
/// <param name="empty"></param>
/// <returns></returns>
public int GetPropertieAsInt(string name, int empty = -1)
{
if (!Properties.ContainsKey(name))
{
int result;
if (int.TryParse(Properties[name], out result))
{
return result;
}
}
return empty;
}
/// <summary>
/// プロパティの読み込み
/// </summary>
/// <param name="r"></param>
private void LoadProperties(XmlReader r)
{
Properties.Clear();
do
{
if (r.NodeType == XmlNodeType.Element)
{
switch (r.LocalName)
{
case "properties":
break;
case "property":
if (r.HasAttributes)
{
string name = "";
string value = "";
for (int i = 0; i < r.AttributeCount; i++)
{
r.MoveToAttribute(i);
switch (r.Name)
{
case "name":
name = r.Value;
break;
case "value":
value = r.Value;
if (!Properties.ContainsKey(name))
{
Properties.Add(name, value);
}
break;
}
}
}
break;
}
}
if (r.NodeType == XmlNodeType.EndElement)
{
if (r.LocalName == "properties")
break;
}
}
while (r.Read());
}
/// <summary>
/// タイルセットの読み込み
/// </summary>
/// <param name="file"></param>
public void Load(string file)
{
var s = new XmlReaderSettings();
s.ConformanceLevel = ConformanceLevel.Document;
using (var r = XmlReader.Create(file, s))
{
Load(r);
}
}
/// <summary>
/// タイルセットの読み込み
/// </summary>
/// <param name="r"></param>
public void Load(XmlReader r)
{
do
{
if (r.NodeType == XmlNodeType.Element)
{
switch (r.LocalName)
{
case "tileset":
if (r.HasAttributes)
{
int result;
for (int i = 0; i < r.AttributeCount; i++)
{
r.MoveToAttribute(i);
switch (r.Name)
{
case "firstgid":
if (int.TryParse(r.Value, out result))
{
FierstGid = result;
}
break;
case "name":
Name = r.Value;
break;
case "tilewidth":
if (int.TryParse(r.Value, out result))
{
TileWidth = result;
}
break;
case "tileheight":
if (int.TryParse(r.Value, out result))
{
TileHeight = result;
}
break;
}
}
r.MoveToElement();
}
break;
case "properties":
LoadProperties(r);
break;
case "image":
Image.Load(r);
break;
}
}
if (r.NodeType == XmlNodeType.EndElement)
{
if (r.LocalName == "tileset")
break;
}
}
while (r.Read());
}
/// <summary>
/// 確認用
/// </summary>
/// <returns></returns>
public override string ToString()
{
StringBuilder s = new StringBuilder();
s.AppendFormat("FierstGid:{0}\r\n", FierstGid);
s.AppendFormat("Name:{0}\r\n", Name);
s.AppendFormat("TileWidth:{0}\r\n", TileWidth);
s.AppendFormat("TileHeight:{0}\r\n", TileHeight);
foreach (var p in Properties)
{
s.AppendFormat("{0}:{1}\r\n", p.Key, p.Value);
}
s.Append(Image.ToString());
return s.ToString();
}
}
/// <summary>
/// マップデータ
/// </summary>
public class MapData
{
private Dictionary<string, string> Properties = new Dictionary<string, string>();
public string Version { get; private set; }
public string Orientation { get; private set; }
public int Width { get; private set; }
public int Heigth { get; private set; }
public int TileWidth { get; private set; }
public int TileHeight { get; private set; }
public List<TileSetData> TileSet { get; private set; }
public List<LayerData> Layer { get; private set; }
/// <summary>
/// コンストラクタ
/// </summary>
public MapData()
{
TileSet = new List<TileSetData>();
Layer = new List<LayerData>();
}
/// <summary>
/// プロパティを取得
/// </summary>
/// <param name="name"></param>
/// <param name="empty"></param>
/// <returns></returns>
public string GetPropertie(string name, string empty = "")
{
if (!Properties.ContainsKey(name))
{
return Properties[name];
}
return empty;
}
/// <summary>
/// プロパティを数値で取得
/// </summary>
/// <param name="name"></param>
/// <param name="empty"></param>
/// <returns></returns>
public int GetPropertieAsInt(string name, int empty = -1)
{
if (!Properties.ContainsKey(name))
{
int result;
if (int.TryParse(Properties[name], out result))
{
return result;
}
}
return empty;
}
/// <summary>
/// プロパティの読み込み
/// </summary>
/// <param name="r"></param>
private void LoadProperties(XmlReader r)
{
Properties.Clear();
do
{
if (r.NodeType == XmlNodeType.Element)
{
switch (r.LocalName)
{
case "properties":
break;
case "property":
if (r.HasAttributes)
{
string name = "";
string value = "";
for (int i = 0; i < r.AttributeCount; i++)
{
r.MoveToAttribute(i);
switch (r.Name)
{
case "name":
name = r.Value;
break;
case "value":
value = r.Value;
if (!Properties.ContainsKey(name))
{
Properties.Add(name, value);
}
break;
}
}
}
break;
}
}
if (r.NodeType == XmlNodeType.EndElement)
{
if (r.LocalName == "properties")
break;
}
}
while (r.Read());
}
/// <summary>
/// マップの読み込み
/// </summary>
/// <param name="file"></param>
public void Load(string file)
{
var s = new XmlReaderSettings();
s.ConformanceLevel = ConformanceLevel.Document;
using (var r = XmlReader.Create(file, s))
{
Load(r);
}
}
/// <summary>
/// マップの読み込み
/// </summary>
/// <param name="r"></param>
public void Load(XmlReader r)
{
TileSet.Clear();
Layer.Clear();
do
{
if (r.NodeType == XmlNodeType.Element)
{
switch (r.LocalName)
{
case "map":
if (r.HasAttributes)
{
int result;
for (int i = 0; i < r.AttributeCount; i++)
{
r.MoveToAttribute(i);
switch (r.Name)
{
case "version":
Version = r.Value;
break;
case "orientation":
Orientation = r.Value;
break;
case "width":
if (int.TryParse(r.Value, out result))
{
Width = result;
}
break;
case "height":
if (int.TryParse(r.Value, out result))
{
Heigth = result;
}
break;
case "tilewidth":
if (int.TryParse(r.Value, out result))
{
TileWidth = result;
}
break;
case "tileheight":
if (int.TryParse(r.Value, out result))
{
TileHeight = result;
}
break;
}
}
}
break;
case "properties":
LoadProperties(r);
break;
case "tileset":
TileSet.Add(new TileSetData());
TileSet[TileSet.Count - 1].Load(r);
break;
case "layer":
Layer.Add(new LayerData());
Layer[Layer.Count - 1].Load(r);
break;
}
}
if (r.NodeType == XmlNodeType.EndElement)
{
if (r.LocalName == "map")
break;
}
}
while (r.Read());
}
/// <summary>
/// 確認用
/// </summary>
/// <returns></returns>
public override string ToString()
{
StringBuilder s = new StringBuilder();
s.AppendFormat("Version:{0}\r\n", Version);
s.AppendFormat("Orientation:{0}\r\n", Orientation);
s.AppendFormat("Width:{0}\r\n", Width);
s.AppendFormat("Heigth:{0}\r\n", Heigth);
s.AppendFormat("TileWidth:{0}\r\n", TileWidth);
s.AppendFormat("TileHeight:{0}\r\n", TileHeight);
foreach (var p in Properties)
{
s.AppendFormat("{0}:{1}\r\n", p.Key, p.Value);
}
foreach (var ts in TileSet)
{
s.Append(ts.ToString());
}
foreach (var lay in Layer)
{
s.Append(lay.ToString());
}
return s.ToString();
}
}
}
view raw gistfile1.cs hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿