Skip to content

Commit

Permalink
Added support for many missing stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBoneJarmer committed Sep 14, 2021
1 parent 69068b8 commit 31a0db4
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/TiledCS.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Ruben Labruyere</Authors>
<Company>Ruben Labruyere</Company>
<PackageId>TiledCS</PackageId>
Expand Down
122 changes: 99 additions & 23 deletions src/TiledMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Xml;

namespace TiledCS
Expand Down Expand Up @@ -39,6 +38,10 @@ public class TiledMap
/// </summary>
public TiledLayer[] Layers { get; set; }
/// <summary>
/// Returns an array of groups or null if none were defined
/// </summary>
public TiledGroup[] Groups { get; set; }
/// <summary>
/// Returns the defined map orientation as a string
/// </summary>
public string Orientation { get; set; }
Expand Down Expand Up @@ -116,8 +119,10 @@ public void ParseXml(string xml)
var nodeMap = document.SelectSingleNode("map");
var nodesProperty = nodeMap.SelectNodes("properties/property");
var nodesLayer = nodeMap.SelectNodes("layer");
var nodesImageLayer = nodeMap.SelectNodes("imagelayer");
var nodesObjectGroup = nodeMap.SelectNodes("objectgroup");
var nodesTileset = nodeMap.SelectNodes("tileset");
var nodesGroup = nodeMap.SelectNodes("group");

this.TiledVersion = nodeMap.Attributes["tiledversion"].Value;
this.Orientation = nodeMap.Attributes["orientation"].Value;
Expand All @@ -130,7 +135,8 @@ public void ParseXml(string xml)

if (nodesProperty != null) Properties = ParseProperties(nodesProperty);
if (nodesTileset != null) Tilesets = ParseTilesets(nodesTileset);
if (nodesLayer != null) Layers = ParseLayers(nodesLayer, nodesObjectGroup);
if (nodesLayer != null) Layers = ParseLayers(nodesLayer, nodesObjectGroup, nodesImageLayer);
if (nodesGroup != null) Groups = ParseGroups(nodesGroup);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -176,16 +182,49 @@ private TiledMapTileset[] ParseTilesets(XmlNodeList nodeList)
return result.ToArray();
}

private TiledLayer[] ParseLayers(XmlNodeList nodeListLayers, XmlNodeList nodeListObjGroups)
private TiledGroup[] ParseGroups(XmlNodeList nodeListGroups)
{
var result = new List<TiledGroup>();

foreach (XmlNode node in nodeListGroups)
{
var nodesProperty = node.SelectNodes("properties/property");
var nodesGroup = node.SelectNodes("group");
var nodesLayer = node.SelectNodes("layer");
var nodesObjectGroup = node.SelectNodes("objectgroup");
var nodesImageLayer = node.SelectNodes("imagelayer");
var attrVisible = node.Attributes["visible"];
var attrLocked = node.Attributes["locked"];

var tiledGroup = new TiledGroup();
tiledGroup.id = int.Parse(node.Attributes["id"].Value);
tiledGroup.name = node.Attributes["name"].Value;

if (attrVisible != null) tiledGroup.visible = attrVisible.Value == "1";
if (attrLocked != null) tiledGroup.locked = attrLocked.Value == "1";
if (nodesProperty != null) tiledGroup.properties = ParseProperties(nodesProperty);
if (nodesGroup != null) tiledGroup.groups = ParseGroups(nodesGroup);
if (nodesLayer != null) tiledGroup.layers = ParseLayers(nodesLayer, nodesObjectGroup, nodesImageLayer);

result.Add(tiledGroup);
}

return result.ToArray();
}
private TiledLayer[] ParseLayers(XmlNodeList nodesLayer, XmlNodeList nodesObjectGroup, XmlNodeList nodesImageLayer)
{
var result = new List<TiledLayer>();

foreach (XmlNode node in nodeListLayers)
foreach (XmlNode node in nodesLayer)
{
var nodeData = node.SelectSingleNode("data");
var nodesProperty = node.SelectNodes("properties/property");
var encoding = nodeData.Attributes["encoding"].Value;
var attrVisible = node.Attributes["visible"];
var attrLocked = node.Attributes["locked"];
var attrTint = node.Attributes["tintcolor"];
var attrOffsetX = node.Attributes["offsetx"];
var attrOffsetY = node.Attributes["offsety"];

var tiledLayer = new TiledLayer();
tiledLayer.id = int.Parse(node.Attributes["id"].Value);
Expand All @@ -195,14 +234,12 @@ private TiledLayer[] ParseLayers(XmlNodeList nodeListLayers, XmlNodeList nodeLis
tiledLayer.type = "tilelayer";
tiledLayer.visible = true;

if (attrVisible != null)
{
tiledLayer.visible = attrVisible.Value == "1";
}
if (nodesProperty != null)
{
tiledLayer.properties = ParseProperties(nodesProperty);
}
if (attrVisible != null) tiledLayer.visible = attrVisible.Value == "1";
if (attrLocked != null) tiledLayer.locked = attrLocked.Value == "1";
if (attrTint != null) tiledLayer.tintcolor = attrTint.Value;
if (attrOffsetX != null) tiledLayer.offsetX = int.Parse(attrOffsetX.Value);
if (attrOffsetY != null) tiledLayer.offsetY = int.Parse(attrOffsetY.Value);
if (nodesProperty != null) tiledLayer.properties = ParseProperties(nodesProperty);

if (encoding == "csv")
{
Expand Down Expand Up @@ -321,11 +358,15 @@ private TiledLayer[] ParseLayers(XmlNodeList nodeListLayers, XmlNodeList nodeLis
result.Add(tiledLayer);
}

foreach (XmlNode node in nodeListObjGroups)
foreach (XmlNode node in nodesObjectGroup)
{
var nodesProperty = node.SelectNodes("properties/property");
var nodesObject = node.SelectNodes("object");
var attrVisible = node.Attributes["visible"];
var attrLocked = node.Attributes["locked"];
var attrTint = node.Attributes["tintcolor"];
var attrOffsetX = node.Attributes["offsetx"];
var attrOffsetY = node.Attributes["offsety"];

var tiledLayer = new TiledLayer();
tiledLayer.id = int.Parse(node.Attributes["id"].Value);
Expand All @@ -334,20 +375,55 @@ private TiledLayer[] ParseLayers(XmlNodeList nodeListLayers, XmlNodeList nodeLis
tiledLayer.type = "objectgroup";
tiledLayer.visible = true;

if (attrVisible != null)
{
tiledLayer.visible = attrVisible.Value == "1";
}
if (nodesProperty != null)
{
tiledLayer.properties = ParseProperties(nodesProperty);
}
if (attrVisible != null) tiledLayer.visible = attrVisible.Value == "1";
if (attrLocked != null) tiledLayer.locked = attrLocked.Value == "1";
if (attrTint != null) tiledLayer.tintcolor = attrTint.Value;
if (attrOffsetX != null) tiledLayer.offsetX = int.Parse(attrOffsetX.Value);
if (attrOffsetY != null) tiledLayer.offsetY = int.Parse(attrOffsetY.Value);
if (nodesProperty != null) tiledLayer.properties = ParseProperties(nodesProperty);

result.Add(tiledLayer);
}

foreach (XmlNode node in nodesImageLayer)
{
var nodesProperty = node.SelectNodes("properties/property");
var nodeImage = node.SelectSingleNode("image");
var attrVisible = node.Attributes["visible"];
var attrLocked = node.Attributes["locked"];
var attrTint = node.Attributes["tintcolor"];
var attrOffsetX = node.Attributes["offsetx"];
var attrOffsetY = node.Attributes["offsety"];

var tiledLayer = new TiledLayer();
tiledLayer.id = int.Parse(node.Attributes["id"].Value);
tiledLayer.name = node.Attributes["name"].Value;
tiledLayer.type = "imagelayer";
tiledLayer.visible = true;

if (attrVisible != null) tiledLayer.visible = attrVisible.Value == "1";
if (attrLocked != null) tiledLayer.locked = attrLocked.Value == "1";
if (attrTint != null) tiledLayer.tintcolor = attrTint.Value;
if (attrOffsetX != null) tiledLayer.offsetX = int.Parse(attrOffsetX.Value);
if (attrOffsetY != null) tiledLayer.offsetY = int.Parse(attrOffsetY.Value);
if (nodesProperty != null) tiledLayer.properties = ParseProperties(nodesProperty);
if (nodeImage != null) tiledLayer.image = ParseImage(nodeImage);

result.Add(tiledLayer);
}

return result.ToArray();
}

private TiledImage ParseImage(XmlNode node)
{
var tiledImage = new TiledImage();
tiledImage.source = node.Attributes["source"].Value;
tiledImage.width = int.Parse(node.Attributes["width"].Value);
tiledImage.height = int.Parse(node.Attributes["height"].Value);

return tiledImage;
}

private TiledObject[] ParseObjects(XmlNodeList nodeList)
{
Expand Down Expand Up @@ -496,7 +572,7 @@ public int[] GetSourceVector(TiledMapTileset mapTileset, TiledTileset tileset, i
// Update x and y position
tileHor++;

if (tileHor == tileset.ImageWidth / tileset.TileWidth)
if (tileHor == tileset.Image.width / tileset.TileWidth)
{
tileHor = 0;
tileVert++;
Expand Down Expand Up @@ -534,7 +610,7 @@ public TiledSourceRect GetSourceRect(TiledMapTileset mapTileset, TiledTileset ti
// Update x and y position
tileHor++;

if (tileHor == tileset.ImageWidth / tileset.TileWidth)
if (tileHor == tileset.Image.width / tileset.TileWidth)
{
tileHor = 0;
tileVert++;
Expand Down
63 changes: 60 additions & 3 deletions src/TiledModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,26 @@ public class TiledLayer
/// </summary>
public string type;
/// <summary>
/// The tint color set by the user in hex code
/// </summary>
public string tintcolor;
/// <summary>
/// Defines if the layer is visible in the editor
/// </summary>
public bool visible;
/// <summary>
/// Is true when the layer is locked
/// </summary>
public bool locked;
/// <summary>
/// The horizontal offset
/// </summary>
public int offsetX;
/// <summary>
/// The vertical offset
/// </summary>
public int offsetY;
/// <summary>
/// An int array of gid numbers which define which tile is being used where. The length of the array equals the layer width * the layer height. Is null when the layer is not a tilelayer.
/// </summary>
public int[] data;
Expand All @@ -83,6 +99,8 @@ public class TiledLayer
/// The layer properties if set
/// </summary>
public TiledProperty[] properties;

public TiledImage image;
}

/// <summary>
Expand Down Expand Up @@ -162,13 +180,13 @@ public class TiledTile
/// <summary>
/// The individual tile image
/// </summary>
public TiledTileImage image;
public TiledImage image;
}

/// <summary>
/// Represents an image within a tile
/// Represents an image
/// </summary>
public class TiledTileImage
public class TiledImage
{
/// <summary>
/// The image width
Expand Down Expand Up @@ -238,4 +256,43 @@ public class TiledSourceRect
/// </summary>
public int height;
}

/// <summary>
/// Represents a layer or object group
/// </summary>
public class TiledGroup
{
/// <summary>
/// The group's id
/// </summary>
public int id;
/// <summary>
/// The group's name
/// </summary>
public string name;
/// <summary>
/// The group's visibility
/// </summary>
public bool visible;
/// <summary>
/// The group's locked state
/// </summary>
public bool locked;
/// <summary>
/// The group's user properties
/// </summary>
public TiledProperty[] properties;
/// <summary>
/// The group's layers
/// </summary>
public TiledLayer[] layers;
/// <summary>
/// The group's objects
/// </summary>
public TiledObject[] objects;
/// <summary>
/// The group's subgroups
/// </summary>
public TiledGroup[] groups;
}
}
45 changes: 19 additions & 26 deletions src/TiledTileset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,9 @@ public class TiledTileset
/// </summary>
public int Columns { get; set; }
/// <summary>
/// The file path to the image used for this tileset
/// The image definition used by the tileset
/// </summary>
public string Image { get; set; }
/// <summary>
/// The image width in pixels
/// </summary>
public int ImageWidth { get; set; }
/// <summary>
/// The image height in pixels
/// </summary>
public int ImageHeight { get; set; }
public TiledImage Image { get; set; }
/// <summary>
/// The amount of spacing between the tiles in pixels
/// </summary>
Expand Down Expand Up @@ -123,28 +115,19 @@ public void ParseXml(string xml)
var nodesProperty = nodeTileset.SelectNodes("properties/property");
var nodesTerrain = nodeTileset.SelectNodes("terraintypes/terrain");

var attrMargin = nodeTileset.Attributes["margin"];
var attrSpacing = nodeTileset.Attributes["spacing"];

TiledVersion = nodeTileset.Attributes["tiledversion"].Value;
Name = nodeTileset.Attributes["name"]?.Value;
TileWidth = int.Parse(nodeTileset.Attributes["tilewidth"].Value);
TileHeight = int.Parse(nodeTileset.Attributes["tileheight"].Value);
TileCount = int.Parse(nodeTileset.Attributes["tilecount"].Value);
Columns = int.Parse(nodeTileset.Attributes["columns"].Value);

if (nodeTileset.Attributes["margin"] != null)
{
Margin = int.Parse(nodeTileset.Attributes["margin"].Value);
}
if (nodeTileset.Attributes["spacing"] != null)
{
Spacing = int.Parse(nodeTileset.Attributes["spacing"].Value);
}

if (nodeImage != null)
{
Image = nodeImage.Attributes["source"].Value;
ImageWidth = int.Parse(nodeImage.Attributes["width"].Value);
ImageHeight = int.Parse(nodeImage.Attributes["height"].Value);
}
if (attrMargin != null) Margin = int.Parse(nodeTileset.Attributes["margin"].Value);
if (attrSpacing != null) Spacing = int.Parse(nodeTileset.Attributes["spacing"].Value);
if (nodeImage != null) Image = ParseImage(nodeImage);

Tiles = ParseTiles(nodesTile);
Properties = ParseProperties(nodesProperty);
Expand All @@ -156,6 +139,16 @@ public void ParseXml(string xml)
}
}

private TiledImage ParseImage(XmlNode node)
{
var tiledImage = new TiledImage();
tiledImage.source = node.Attributes["source"].Value;
tiledImage.width = int.Parse(node.Attributes["width"].Value);
tiledImage.height = int.Parse(node.Attributes["height"].Value);

return tiledImage;
}

private TiledTileAnimation[] ParseAnimations(XmlNodeList nodeList)
{
var result = new List<TiledTileAnimation>();
Expand Down Expand Up @@ -213,7 +206,7 @@ private TiledTile[] ParseTiles(XmlNodeList nodeList)

if (nodeImage != null)
{
var tileImage = new TiledTileImage();
var tileImage = new TiledImage();
tileImage.width = int.Parse(nodeImage.Attributes["width"].Value);
tileImage.height = int.Parse(nodeImage.Attributes["height"].Value);
tileImage.source = nodeImage.Attributes["source"].Value;
Expand Down

0 comments on commit 31a0db4

Please sign in to comment.