TiledSharp  0.9.1 r0
A .NET C# library for importing Tiled TMX tile maps
 All Classes Namespaces Files Functions Variables Enumerations Properties Pages
Layer.cs
Go to the documentation of this file.
1 // Distributed as part of TiledSharp, Copyright 2012 Marshall Ward
2 // Licensed under the Apache License, Version 2.0
3 // http://www.apache.org/licenses/LICENSE-2.0
4 using System;
5 using System.Collections.Generic;
6 using System.Xml.Linq;
7 using System.IO;
8 using System.IO.Compression;
9 
10 namespace TiledSharp
11 {
12  public class TmxLayer : ITmxElement
13  {
14  public string Name {get; private set;}
15  public double Opacity {get; private set;}
16  public bool Visible {get; private set;}
17 
18  public List<TmxLayerTile> Tiles {get; private set;}
19  public PropertyDict Properties {get; private set;}
20 
21  public TmxLayer(XElement xLayer, int width, int height)
22  {
23  Name = (string)xLayer.Attribute("name");
24  Opacity = (double?)xLayer.Attribute("opacity") ?? 1.0;
25  Visible = (bool?)xLayer.Attribute("visible") ?? true;
26 
27  var xData = xLayer.Element("data");
28  var encoding = (string)xData.Attribute("encoding");
29 
30  Tiles = new List<TmxLayerTile>();
31  if (encoding == "base64")
32  {
33  var decodedStream = new TmxBase64Data(xData);
34  var stream = decodedStream.Data;
35 
36  using (var br = new BinaryReader(stream))
37  for (int j = 0; j < height; j++)
38  for (int i = 0; i < width; i++)
39  Tiles.Add(new TmxLayerTile(br.ReadUInt32(), i, j));
40  }
41  else if (encoding == "csv")
42  {
43  var csvData = (string)xData.Value;
44  int k = 0;
45  foreach (var s in csvData.Split(','))
46  {
47  var gid = uint.Parse(s.Trim());
48  var x = k % width;
49  var y = k / width;
50  Tiles.Add(new TmxLayerTile(gid, x, y));
51  k++;
52  }
53  }
54  else if (encoding == null)
55  {
56  int k = 0;
57  foreach (var e in xData.Elements("tile"))
58  {
59  var gid = (uint)e.Attribute("gid");
60  var x = k % width;
61  var y = k / width;
62  Tiles.Add(new TmxLayerTile(gid, x, y));
63  k++;
64  }
65  }
66  else throw new Exception("TmxLayer: Unknown encoding.");
67 
68  Properties = new PropertyDict(xLayer.Element("properties"));
69  }
70  }
71 
72  public class TmxLayerTile
73  {
74  // Tile flip bit flags
75  const uint FLIPPED_HORIZONTALLY_FLAG = 0x80000000;
76  const uint FLIPPED_VERTICALLY_FLAG = 0x40000000;
77  const uint FLIPPED_DIAGONALLY_FLAG = 0x20000000;
78 
79  public int Gid {get; private set;}
80  public int X {get; private set;}
81  public int Y {get; private set;}
82  public bool HorizontalFlip {get; private set;}
83  public bool VerticalFlip {get; private set;}
84  public bool DiagonalFlip {get; private set;}
85 
86  public TmxLayerTile(uint id, int x, int y)
87  {
88  var rawGid = id;
89  X = x;
90  Y = y;
91 
92  // Scan for tile flip bit flags
93  bool flip;
94 
95  flip = (rawGid & FLIPPED_HORIZONTALLY_FLAG) != 0;
96  HorizontalFlip = flip ? true : false;
97 
98  flip = (rawGid & FLIPPED_VERTICALLY_FLAG) != 0;
99  VerticalFlip = flip ? true : false;
100 
101  flip = (rawGid & FLIPPED_DIAGONALLY_FLAG) != 0;
102  DiagonalFlip = flip ? true : false;
103 
104  // Zero the bit flags
105  rawGid &= ~(FLIPPED_HORIZONTALLY_FLAG |
106  FLIPPED_VERTICALLY_FLAG |
107  FLIPPED_DIAGONALLY_FLAG);
108 
109  // Save GID remainder to int
110  Gid = (int)rawGid;
111  }
112  }
113 }
Base64 data decoder.
Definition: TiledCore.cs:154
string Name
Layer name.
Definition: Layer.cs:14
int Gid
Global tileset ID.
Definition: Layer.cs:79
double Opacity
Alpha transparency of layer.
Definition: Layer.cs:15
PropertyDict Properties
User-defined layer properties.
Definition: Layer.cs:19
bool Visible
True if layer is visible.
Definition: Layer.cs:16
bool HorizontalFlip
Horizontally flipped tile image.
Definition: Layer.cs:82
TMX element interface.
Definition: TiledCore.cs:49
List< TmxLayerTile > Tiles
List of layer map tiles to be rendered.
Definition: Layer.cs:18
Layer tile maps.
Definition: Layer.cs:12
bool DiagonalFlip
Diagonally flipped tile image along top left/bottom right axis.
Definition: Layer.cs:84
Tile elements of a layer map.
Definition: Layer.cs:72
bool VerticalFlip
Vertically flipped tile image.
Definition: Layer.cs:83
int X
X (rightward) position of tile.
Definition: Layer.cs:80
TmxLayer(XElement xLayer, int width, int height)
Tile map layer constructor.
Definition: Layer.cs:21
TmxLayerTile(uint id, int x, int y)
Layer tile constructor.
Definition: Layer.cs:86
int Y
Y (downward) position of tile.
Definition: Layer.cs:81
User-defined property list.
Definition: TiledCore.cs:90