Example #1
0
        public PropertyDict(XContainer xmlProp, TmxTypeDefaults defaults = null)
        {
            if (defaults != null)
            {
                foreach (KeyValuePair <string, string> keyPair in defaults)
                {
                    //Fill default values for any keys that aren't present.
                    if (!ContainsKey(keyPair.Key))
                    {
                        Add(keyPair.Key, keyPair.Value);
                    }
                }
            }

            if (xmlProp == null)
            {
                return;
            }

            foreach (var p in xmlProp.Elements("property"))
            {
                string pname, pval;

                pname = p.Attribute("name").Value;
                try {
                    pval = p.Attribute("value").Value;
                } catch (System.NullReferenceException) {
                    // Fallback to element value if no "value"
                    pval = p.Value;
                }

                Add(pname, pval);
            }
        }
Example #2
0
        public TmxObject(XElement xObject, Dictionary <string, TmxTypeDefaults> defaults = null)
        {
            Id       = (int?)xObject.Attribute("id") ?? 0;
            Name     = (string)xObject.Attribute("name") ?? String.Empty;
            X        = (double)xObject.Attribute("x");
            Y        = (double)xObject.Attribute("y");
            Width    = (double?)xObject.Attribute("width") ?? 0.0;
            Height   = (double?)xObject.Attribute("height") ?? 0.0;
            Type     = (string)xObject.Attribute("type") ?? String.Empty;
            Visible  = (bool?)xObject.Attribute("visible") ?? true;
            Rotation = (double?)xObject.Attribute("rotation") ?? 0.0;

            // Assess object type and assign appropriate content
            var xGid      = xObject.Attribute("gid");
            var xEllipse  = xObject.Element("ellipse");
            var xPolygon  = xObject.Element("polygon");
            var xPolyline = xObject.Element("polyline");

            if (xGid != null)
            {
                Tile = new TmxLayerTile((uint)xGid,
                                        Convert.ToInt32(Math.Round(X)),
                                        Convert.ToInt32(Math.Round(X)));
                ObjectType = TmxObjectType.Tile;
            }
            else if (xEllipse != null)
            {
                ObjectType = TmxObjectType.Ellipse;
            }
            else if (xPolygon != null)
            {
                Points     = ParsePoints(xPolygon);
                ObjectType = TmxObjectType.Polygon;
            }
            else if (xPolyline != null)
            {
                Points     = ParsePoints(xPolyline);
                ObjectType = TmxObjectType.Polyline;
            }
            else
            {
                ObjectType = TmxObjectType.Basic;
            }

            var xText = xObject.Element("text");

            if (xText != null)
            {
                Text = new TmxText(xText);
            }

            TmxTypeDefaults typeDefaults = null;

            defaults.TryGetValue(Type, out typeDefaults);

            Properties = new PropertyDict(xObject.Element("properties"), typeDefaults);
        }
Example #3
0
        public TmxMap(string filename, string objectTypeFile)
        {
            if (objectTypeFile != null)
            {
                XDocument xTypesDoc    = ReadXml(objectTypeFile);
                var       xObjectTypes = xTypesDoc.Element("objecttypes");
                foreach (var ot in xObjectTypes.Elements("objecttype"))
                {
                    string typeName = ot.Attribute("name").Value;

                    TmxTypeDefaults defaults = new TmxTypeDefaults();
                    DefaultValues.Add(typeName, defaults);
                    foreach (var op in ot.Elements("property"))
                    {
                        string name  = op.Attribute("name").Value;
                        string value = op.Attribute("default")?.Value ?? "";
                        defaults.Add(name, value);
                    }
                }
            }

            Load(ReadXml(filename));
        }