TiledSharp  0.9.1 r0
A .NET C# library for importing Tiled TMX tile maps
All Classes Namespaces Files Functions Variables Enumerations Properties Pages
ObjectGroup.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.Globalization;
7 using System.Xml.Linq;
8 
9 namespace TiledSharp
10 {
11  public class TmxObjectGroup : ITmxElement
12  {
13  public string Name {get; private set;}
14  public TmxColor Color {get; private set;}
15  public double Opacity {get; private set;}
16  public bool Visible {get; private set;}
17 
18  public TmxList<TmxObject> Objects {get; private set;}
19  public PropertyDict Properties {get; private set;}
20 
21  public TmxObjectGroup(XElement xObjectGroup)
22  {
23  Name = (string)xObjectGroup.Attribute("name");
24  Color = new TmxColor(xObjectGroup.Attribute("color"));
25  Opacity = (double?)xObjectGroup.Attribute("opacity") ?? 1.0;
26  Visible = (bool?)xObjectGroup.Attribute("visible") ?? true;
27 
28  Objects = new TmxList<TmxObject>();
29  foreach (var e in xObjectGroup.Elements("object"))
30  Objects.Add(new TmxObject(e));
31 
32  Properties = new PropertyDict(xObjectGroup.Element("properties"));
33  }
34 
35  public class TmxObject : ITmxElement
36  {
37  // Many TmxObjectTypes are distinguished by null values in fields
38  // It might be smart to subclass TmxObject
39  public string Name {get; private set;}
40  public TmxObjectType ObjectType {get; private set;}
41  public string Type {get; private set;}
42  public int X {get; private set;}
43  public int Y {get; private set;}
44  public int Width {get; private set;}
45  public int Height {get; private set;}
46  public double Rotation {get; private set;}
47  public TmxLayerTile Tile {get; private set;}
48  public bool Visible {get; private set;}
49 
50  public List<Tuple<int,int>> Points {get; private set;}
51  public PropertyDict Properties {get; private set;}
52 
53  public TmxObject(XElement xObject)
54  {
55  Name = (string)xObject.Attribute("name") ?? "";
56  Type = (string)xObject.Attribute("type");
57  X = (int)xObject.Attribute("x");
58  Y = (int)xObject.Attribute("y");
59  Visible = (bool?)xObject.Attribute("visible") ?? true;
60  Width = (int?)xObject.Attribute("width") ?? 0;
61  Height = (int?)xObject.Attribute("height") ?? 0;
62  Rotation = (double?)xObject.Attribute("rotation") ?? 0.0;
63 
64  // Assess object type and assign appropriate content
65  var xGid = xObject.Attribute("gid");
66  var xEllipse = xObject.Element("ellipse");
67  var xPolygon = xObject.Element("polygon");
68  var xPolyline = xObject.Element("polyline");
69 
70  if (xGid != null)
71  {
72  Tile = new TmxLayerTile((uint)xGid, X, Y);
73  ObjectType = TmxObjectType.Tile;
74  }
75  else if (xEllipse != null)
76  {
77  ObjectType = TmxObjectType.Ellipse;
78  }
79  else if (xPolygon != null)
80  {
81  Points = ParsePoints(xPolygon);
82  ObjectType = TmxObjectType.Polygon;
83  }
84  else if (xPolyline != null)
85  {
86  Points = ParsePoints(xPolyline);
87  ObjectType = TmxObjectType.Polyline;
88  }
89  else ObjectType = TmxObjectType.Basic;
90 
91  Properties = new PropertyDict(xObject.Element("properties"));
92  }
93 
94  public List<Tuple<int, int>> ParsePoints(XElement xPoints)
95  {
96  var points = new List<Tuple<int, int>>();
97 
98  var pointString = (string)xPoints.Attribute("points");
99  var pointStringPair = pointString.Split(' ');
100  foreach (var s in pointStringPair)
101  {
102  var pt = s.Split(',');
103  var x = int.Parse(pt[0]);
104  var y = int.Parse(pt[1]);
105  points.Add(Tuple.Create<int, int>(x, y));
106  }
107  return points;
108  }
109  }
110 
111  public enum TmxObjectType : byte
112  {
113  Basic,
114  Tile,
115  Ellipse,
116  Polygon,
117  Polyline
118  }
119  }
120 }
PropertyDict Properties
User-defined object properties.
Definition: ObjectGroup.cs:51
RGB color components.
Definition: TiledCore.cs:136
TmxObjectType ObjectType
Tiled object type.
Definition: ObjectGroup.cs:40
int X
X (rightward) object position in pixels.
Definition: ObjectGroup.cs:42
string Name
Object layer name.
Definition: ObjectGroup.cs:13
string Type
User-defined object type.
Definition: ObjectGroup.cs:41
int Y
Y (downward) object position in pixels.
Definition: ObjectGroup.cs:43
TMX element interface.
Definition: TiledCore.cs:49
TmxObjectGroup(XElement xObjectGroup)
Object layer constructor.
Definition: ObjectGroup.cs:21
List< Tuple< int, int > > Points
List of (X,Y) object vertices in pixels.
Definition: ObjectGroup.cs:50
int Height
Object height in pixels.
Definition: ObjectGroup.cs:45
int Width
Object width in pixels.
Definition: ObjectGroup.cs:44
Tile elements of a layer map.
Definition: Layer.cs:72
bool Visible
True if object layer is visible.
Definition: ObjectGroup.cs:16
TmxObject(XElement xObject)
Object constructor.
Definition: ObjectGroup.cs:53
double Rotation
Clockwise rotation of object in degrees.
Definition: ObjectGroup.cs:46
PropertyDict Properties
User-defined object layer properties.
Definition: ObjectGroup.cs:19
Map object layer.
Definition: ObjectGroup.cs:11
TmxColor Color
Object layer elements color.
Definition: ObjectGroup.cs:14
TmxObjectType
Tiled object type.
Definition: ObjectGroup.cs:111
TmxList< TmxObject > Objects
List of objects in object layer.
Definition: ObjectGroup.cs:18
User-defined property list.
Definition: TiledCore.cs:90
double Opacity
Alpha transparency of object layer.
Definition: ObjectGroup.cs:15
List< Tuple< int, int > > ParsePoints(XElement xPoints)
Parse vertex point string into (X,Y) coordinate pairs.
Definition: ObjectGroup.cs:94
TmxLayerTile Tile
Object tile reference.
Definition: ObjectGroup.cs:47
bool Visible
True if object is visible.
Definition: ObjectGroup.cs:48