Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep the order of the layers #104

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/TiledCS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
Expand Down
33 changes: 12 additions & 21 deletions src/TiledMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,8 @@ public void ParseXml(string xml)
document.LoadXml(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");
var attrParallaxOriginX = nodeMap.Attributes["parallaxoriginx"];
Expand All @@ -177,7 +175,7 @@ public void ParseXml(string xml)

if (nodesProperty != null) Properties = ParseProperties(nodesProperty);
if (nodesTileset != null) Tilesets = ParseTilesets(nodesTileset);
if (nodesLayer != null) Layers = ParseLayers(nodesLayer, nodesObjectGroup, nodesImageLayer);
Layers = ParseLayers(nodeMap);
if (nodesGroup != null) Groups = ParseGroups(nodesGroup);
if (attrParallaxOriginX != null) ParallaxOriginX = float.Parse(attrParallaxOriginX.Value, CultureInfo.InvariantCulture);
if (attrParallaxOriginY != null) ParallaxOriginY = float.Parse(attrParallaxOriginY.Value, CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -260,33 +258,26 @@ private TiledGroup[] ParseGroups(XmlNodeList nodeListGroups)
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);
if (nodesLayer != null) tiledGroup.layers = ParseLayers(node);

result.Add(tiledGroup);
}

return result.ToArray();
}

private TiledLayer[] ParseLayers(XmlNodeList nodesLayer, XmlNodeList nodesObjectGroup, XmlNodeList nodesImageLayer)
private TiledLayer[] ParseLayers(XmlNode nodeMap)
{
var result = new List<TiledLayer>();

foreach (XmlNode node in nodesLayer)
{
result.Add(ParseLayer(node, TiledLayerType.TileLayer));
}

foreach (XmlNode node in nodesObjectGroup)
foreach (XmlElement childNode in nodeMap.ChildNodes)
{
result.Add(ParseLayer(node, TiledLayerType.ObjectLayer));
if (childNode.Name == "layer")
result.Add(ParseLayer(childNode, TiledLayerType.TileLayer));
if (childNode.Name == "objectgroup")
result.Add(ParseLayer(childNode, TiledLayerType.ObjectLayer));
if (childNode.Name == "imagelayer")
result.Add(ParseLayer(childNode, TiledLayerType.ImageLayer));
}

foreach (XmlNode node in nodesImageLayer)
{
result.Add(ParseLayer(node, TiledLayerType.ImageLayer));
}

return result.ToArray();
}

Expand Down Expand Up @@ -658,7 +649,7 @@ public Dictionary<int, TiledTileset> GetTiledTilesets(string src)

if (File.Exists(path))
{
tilesets.Add(mapTileset.firstgid, new TiledTileset(path));
tilesets.Add(mapTileset.firstgid, new TiledTileset(path, mapTileset.source));
}
else
{
Expand Down
10 changes: 9 additions & 1 deletion src/TiledTileset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public class TiledTileset
/// </summary>
public TiledOffset Offset { get; set; }

/// <summary>
/// The local path to the tileset
/// </summary>
public string Source { get; set; }

/// <summary>
/// Returns an empty instance of TiledTileset
/// </summary>
Expand All @@ -78,8 +83,9 @@ public TiledTileset()
/// Loads a tileset in TSX format and parses it
/// </summary>
/// <param name="path">The file path of the TSX file</param>
/// <param name="source">The local file path from the map of the TSX file</param>
/// <exception cref="TiledException">Thrown when the file could not be found or parsed</exception>
public TiledTileset(string path)
public TiledTileset(string path, string source)
{
// Check the file
if (!File.Exists(path))
Expand All @@ -97,6 +103,8 @@ public TiledTileset(string path)
{
throw new TiledException("Unsupported file format");
}

Source = source;
}

/// <summary>
Expand Down