private void ParseMap(XDocument doc) { Program.WriteLine("Parsing map root ..."); Program.WriteVerbose(doc.ToString()); XElement map = doc.Element("map"); try { this.Orientation = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation"); this.StaggerAxis = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y); this.StaggerIndex = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd); this.HexSideLength = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0); this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1; this.DrawOrderVertical = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1; this.Width = TmxHelper.GetAttributeAsInt(map, "width"); this.Height = TmxHelper.GetAttributeAsInt(map, "height"); this.TileWidth = TmxHelper.GetAttributeAsInt(map, "tilewidth"); this.TileHeight = TmxHelper.GetAttributeAsInt(map, "tileheight"); this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128)); } catch (Exception e) { TmxException.FromAttributeException(e, map); } // Collect our map properties this.Properties = TmxProperties.FromXml(map); ParseAllTilesets(doc); ParseAllLayers(doc); ParseAllObjectGroups(doc); }
private void ParseMapXml(XDocument doc) { Program.WriteLine("Parsing map root ..."); //Program.WriteVerbose(doc.ToString()); // Some TMX files are far too big (cause out of memory exception) so don't do this XElement map = doc.Element("map"); try { this.Orientation = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation"); this.StaggerAxis = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y); this.StaggerIndex = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd); this.HexSideLength = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0); this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1; this.DrawOrderVertical = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1; this.Width = TmxHelper.GetAttributeAsInt(map, "width"); this.Height = TmxHelper.GetAttributeAsInt(map, "height"); this.TileWidth = TmxHelper.GetAttributeAsInt(map, "tilewidth"); this.TileHeight = TmxHelper.GetAttributeAsInt(map, "tileheight"); this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128)); } catch (Exception e) { TmxException.FromAttributeException(e, map); } // Collect our map properties this.Properties = TmxProperties.FromXml(map); ParseAllTilesets(doc); ParseAllLayers(doc); ParseAllObjectGroups(doc); // Once everything is loaded, take a moment to do additional plumbing ParseCompleted(); }
public static TmxImage FromXml(XElement elemImage) { TmxImage tmxImage = new TmxImage(); tmxImage.Path = TmxHelper.GetAttributeAsFullPath(elemImage, "source"); // Width and height are optional. int width = TmxHelper.GetAttributeAsInt(elemImage, "width", 0); int height = TmxHelper.GetAttributeAsInt(elemImage, "height", 0); // Prefer to use the actual width and height anyway so that UVs do not get jacked using (Image bitmap = Bitmap.FromFile(tmxImage.Path)) { width = bitmap.Width; height = bitmap.Height; } tmxImage.Size = new System.Drawing.Size(width, height); // Some images use a transparency color key instead of alpha (blerg) tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", ""); if (!String.IsNullOrEmpty(tmxImage.TransparentColor) && !tmxImage.TransparentColor.StartsWith("#")) { // The hash makes it an HTML color tmxImage.TransparentColor = "#" + tmxImage.TransparentColor; } return(tmxImage); }
private void ParseMap(XDocument doc) { Program.WriteLine("Parsing map root ..."); Program.WriteVerbose(doc.ToString()); XElement map = doc.Element("map"); try { this.Orientation = TmxHelper.GetAttributeAsString(map, "orientation"); this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1; this.DrawOrderVertical = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1; this.Width = TmxHelper.GetAttributeAsInt(map, "width"); this.Height = TmxHelper.GetAttributeAsInt(map, "height"); this.TileWidth = TmxHelper.GetAttributeAsInt(map, "tilewidth"); this.TileHeight = TmxHelper.GetAttributeAsInt(map, "tileheight"); this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128)); } catch (Exception e) { TmxException.FromAttributeException(e, map); } // We only support orthogonal maps if (this.Orientation != "orthogonal") { TmxException.ThrowFormat("Only orthogonal maps are supported. This map is set to \"{0}\"", this.Orientation); } // Collect our map properties this.Properties = TmxProperties.FromXml(map); ParseAllTilesets(doc); ParseAllLayers(doc); ParseAllObjectGroups(doc); }
private void ParseTilesetFromImageLayer(XElement elemImageLayer) { string tilesetName = TmxHelper.GetAttributeAsString(elemImageLayer, "name"); XElement xmlImage = elemImageLayer.Element("image"); if (xmlImage == null) { Program.WriteWarning("Image Layer '{0}' has no image assigned.", tilesetName); return; } TmxImage tmxImage = TmxImage.FromXml(xmlImage); RegisterImagePath(tmxImage.Path); uint firstId = this.Tiles.Max(t => t.Key) + 1; uint localId = 1; uint globalId = firstId + localId; TmxTile tile = new TmxTile(globalId, localId, tilesetName, tmxImage); tile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height); tile.SetLocationOnSource(0, 0); this.Tiles[tile.GlobalId] = tile; }
public static TmxObject FromXml(XElement xml, TmxObjectGroup tmxObjectGroup, TmxMap tmxMap) { Logger.WriteVerbose("Parsing object ..."); uint attributeAsUInt = TmxHelper.GetAttributeAsUInt(xml, "tid", 0u); if (attributeAsUInt != 0 && tmxMap.Templates.TryGetValue(attributeAsUInt, out TgxTemplate value)) { xml = value.Templatize(xml); tmxMap = value.TemplateGroupMap; } TmxObject tmxObject = null; if (xml.Element("ellipse") != null) { tmxObject = new TmxObjectEllipse(); } else if (xml.Element("polygon") != null) { tmxObject = new TmxObjectPolygon(); } else if (xml.Element("polyline") != null) { tmxObject = new TmxObjectPolyline(); } else if (xml.Attribute("gid") != null) { uint attributeAsUInt2 = TmxHelper.GetAttributeAsUInt(xml, "gid"); attributeAsUInt2 = TmxMath.GetTileIdWithoutFlags(attributeAsUInt2); if (tmxMap.Tiles.ContainsKey(attributeAsUInt2)) { tmxObject = new TmxObjectTile(); } else { Logger.WriteWarning("Tile Id {0} not found in tilesets. Using a rectangle instead.\n{1}", attributeAsUInt2, xml.ToString()); tmxObject = new TmxObjectRectangle(); } } else { tmxObject = new TmxObjectRectangle(); } tmxObject.Id = TmxHelper.GetAttributeAsInt(xml, "id", 0); tmxObject.Name = TmxHelper.GetAttributeAsString(xml, "name", ""); tmxObject.Type = TmxHelper.GetAttributeAsString(xml, "type", ""); tmxObject.Visible = (TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1); tmxObject.ParentObjectGroup = tmxObjectGroup; float attributeAsFloat = TmxHelper.GetAttributeAsFloat(xml, "x"); float attributeAsFloat2 = TmxHelper.GetAttributeAsFloat(xml, "y"); float attributeAsFloat3 = TmxHelper.GetAttributeAsFloat(xml, "width", 0f); float attributeAsFloat4 = TmxHelper.GetAttributeAsFloat(xml, "height", 0f); float attributeAsFloat5 = TmxHelper.GetAttributeAsFloat(xml, "rotation", 0f); tmxObject.Position = new PointF(attributeAsFloat, attributeAsFloat2); tmxObject.Size = new SizeF(attributeAsFloat3, attributeAsFloat4); tmxObject.Rotation = attributeAsFloat5; tmxObject.Properties = TmxProperties.FromXml(xml); tmxObject.InternalFromXml(xml, tmxMap); return(tmxObject); }
public static TmxProperties FromXml(XElement elem) { TmxProperties tmxProperties = new TmxProperties(); var enumerable = from elem1 in elem.Elements("properties") from elem2 in elem1.Elements("property") select new { Name = TmxHelper.GetAttributeAsString(elem2, "name"), Type = TmxHelper.GetAttributeAsEnum(elem2, "type", TmxPropertyType.String), Value = (TmxHelper.GetAttributeAsString(elem2, "value", null) ?? elem2.Value) }; if (enumerable.Count() > 0) { Logger.WriteVerbose("Parse properties ..."); } foreach (var item in enumerable) { tmxProperties.PropertyMap[item.Name] = new TmxProperty { Name = item.Name, Type = item.Type, Value = item.Value }; } return(tmxProperties); }
private void ParseMapXml(XDocument doc) { Logger.WriteVerbose("Parsing map root ..."); XElement xElement = doc.Element("map"); try { Orientation = TmxHelper.GetAttributeAsEnum <MapOrientation>(xElement, "orientation"); StaggerAxis = TmxHelper.GetAttributeAsEnum(xElement, "staggeraxis", MapStaggerAxis.Y); StaggerIndex = TmxHelper.GetAttributeAsEnum(xElement, "staggerindex", MapStaggerIndex.Odd); HexSideLength = TmxHelper.GetAttributeAsInt(xElement, "hexsidelength", 0); DrawOrderHorizontal = (TmxHelper.GetAttributeAsString(xElement, "renderorder", "right-down").Contains("right") ? 1 : (-1)); DrawOrderVertical = (TmxHelper.GetAttributeAsString(xElement, "renderorder", "right-down").Contains("down") ? 1 : (-1)); Width = TmxHelper.GetAttributeAsInt(xElement, "width"); Height = TmxHelper.GetAttributeAsInt(xElement, "height"); TileWidth = TmxHelper.GetAttributeAsInt(xElement, "tilewidth"); TileHeight = TmxHelper.GetAttributeAsInt(xElement, "tileheight"); BackgroundColor = TmxHelper.GetAttributeAsColor(xElement, "backgroundcolor", new Color32(128, 128, 128, 255)); } catch (Exception inner) { TmxException.FromAttributeException(inner, xElement); } Properties = TmxProperties.FromXml(xElement); IsResource = Properties.GetPropertyValueAsBoolean("unity:resource", false); IsResource = (IsResource || !string.IsNullOrEmpty(Properties.GetPropertyValueAsString("unity:resourcePath", null))); ExplicitSortingLayerName = Properties.GetPropertyValueAsString("unity:sortingLayerName", ""); ParseAllTilesets(doc); ParseAllTemplates(doc); LayerNodes = TmxLayerNode.ListFromXml(xElement, null, this); MapSizeInPixels = CalculateMapSizeInPixels(); TmxDisplayOrderVisitor visitor = new TmxDisplayOrderVisitor(); Visit(visitor); }
public static TmxObjectGroup FromXml(XElement xml, TmxMap tmxMap) { Debug.Assert(xml.Name == "objectgroup"); TmxObjectGroup tmxObjectGroup = new TmxObjectGroup(); tmxObjectGroup.Name = TmxHelper.GetAttributeAsString(xml, "name", ""); tmxObjectGroup.Visible = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1; tmxObjectGroup.Color = TmxHelper.GetAttributeAsColor(xml, "color", Color.FromArgb(128, 128, 128)); tmxObjectGroup.Properties = TmxProperties.FromXml(xml); PointF offset = new PointF(0, 0); offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0); offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0); tmxObjectGroup.Offset = offset; // Get all the objects Program.WriteLine("Parsing objects in object group '{0}'", tmxObjectGroup.Name); var objects = from obj in xml.Elements("object") select TmxObject.FromXml(obj, tmxMap); tmxObjectGroup.Objects = objects.ToList(); return(tmxObjectGroup); }
public static TsxTileset FromImageLayerXml(XElement xml, TmxMap tmxMap) { TsxTileset tsxTileset = new TsxTileset(tmxMap); tsxTileset.Name = TmxHelper.GetAttributeAsString(xml, "name"); XElement xElement = xml.Element("image"); if (xElement == null) { Logger.WriteWarning("Image Layer '{0}' has no image assigned.", tsxTileset.Name); return(null); } TmxProperties tmxProperties = TmxProperties.FromXml(xml); string propertyValueAsString = tmxProperties.GetPropertyValueAsString("unity:namePrefix", ""); string propertyValueAsString2 = tmxProperties.GetPropertyValueAsString("unity:namePostfix", ""); TmxImage tmxImage = TmxImage.FromXml(xElement, propertyValueAsString, propertyValueAsString2); uint num = 1u; if (tmxMap.Tiles.Count > 0) { num = tmxMap.Tiles.Max((KeyValuePair <uint, TmxTile> t) => t.Key) + 1; } uint num2 = 1u; uint globalId = num + num2; TmxTile tmxTile = new TmxTile(tmxMap, globalId, num2, tsxTileset.Name, tmxImage); tmxTile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height); tmxTile.SetLocationOnSource(0, 0); tsxTileset.Tiles.Add(tmxTile); tsxTileset.Tiles.ForEach(delegate(TmxTile t) { tmxMap.Tiles.Add(t.GlobalId, t); }); return(tsxTileset); }
private void ParseTilesetFromImageLayer(XElement elemImageLayer) { string tilesetName = TmxHelper.GetAttributeAsString(elemImageLayer, "name"); XElement xmlImage = elemImageLayer.Element("image"); if (xmlImage == null) { Logger.WriteWarning("Image Layer '{0}' has no image assigned.", tilesetName); return; } TmxProperties properties = TmxProperties.FromXml(elemImageLayer); string prefix = properties.GetPropertyValueAsString("unity:namePrefix", ""); string postfix = properties.GetPropertyValueAsString("unity:namePostfix", ""); TmxImage tmxImage = TmxImage.FromXml(xmlImage, prefix, postfix); // The "firstId" is is always one more than all the tiles that we've already parsed (which may be zero) uint firstId = 1; if (this.Tiles.Count > 0) { firstId = this.Tiles.Max(t => t.Key) + 1; } uint localId = 1; uint globalId = firstId + localId; TmxTile tile = new TmxTile(this, globalId, localId, tilesetName, tmxImage); tile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height); tile.SetLocationOnSource(0, 0); this.Tiles[tile.GlobalId] = tile; }
public static TmxObjectGroup FromXml(XElement xml, TmxMap tmxMap) { Debug.Assert(xml.Name == "objectgroup"); TmxObjectGroup tmxObjectGroup = new TmxObjectGroup(); // Order within Xml file is import for layer types tmxObjectGroup.XmlElementIndex = xml.NodesBeforeSelf().Count(); tmxObjectGroup.Name = TmxHelper.GetAttributeAsString(xml, "name", ""); tmxObjectGroup.Visible = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1; tmxObjectGroup.Color = TmxHelper.GetAttributeAsColor(xml, "color", Color.FromArgb(128, 128, 128)); tmxObjectGroup.Properties = TmxProperties.FromXml(xml); PointF offset = new PointF(0, 0); offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0); offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0); tmxObjectGroup.Offset = offset; // Get all the objects Program.WriteLine("Parsing objects in object group '{0}'", tmxObjectGroup.Name); var objects = from obj in xml.Elements("object") select TmxObject.FromXml(obj, tmxObjectGroup, tmxMap); tmxObjectGroup.Objects = objects.ToList(); // Are we using a unity:layer override? tmxObjectGroup.UnityLayerOverrideName = tmxObjectGroup.Properties.GetPropertyValueAsString("unity:layer", ""); return(tmxObjectGroup); }
private void ParseData(XElement elem) { Logger.WriteLine("Parse {0} layer data ...", this.Name); string encoding = TmxHelper.GetAttributeAsString(elem, "encoding", ""); string compression = TmxHelper.GetAttributeAsString(elem, "compression", ""); if (elem.Element("tile") != null) { ParseTileDataAsXml(elem); } else if (encoding == "csv") { ParseTileDataAsCsv(elem); } else if (encoding == "base64" && String.IsNullOrEmpty(compression)) { ParseTileDataAsBase64(elem); } else if (encoding == "base64" && compression == "gzip") { ParseTileDataAsBase64GZip(elem); } else if (encoding == "base64" && compression == "zlib") { ParseTileDataAsBase64Zlib(elem); } else { TmxException.ThrowFormat("Unsupported schema for {0} layer data", this.Name); } }
protected void FromXmlInternal(XElement xml) { // Get common elements amoung layer nodes from xml this.Name = TmxHelper.GetAttributeAsString(xml, "name", ""); this.Visible = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1; this.Opacity = TmxHelper.GetAttributeAsFloat(xml, "opacity", 1); // Get the offset PointF offset = new PointF(0, 0); offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0); offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0); this.Offset = offset; // Get all the properties this.Properties = TmxProperties.FromXml(xml); // Set the "ignore" setting on this object group this.Ignore = this.Properties.GetPropertyValueAsEnum <IgnoreSettings>("unity:ignore", IgnoreSettings.False); // Explicit sorting properties this.ExplicitSortingLayerName = this.Properties.GetPropertyValueAsString("unity:sortingLayerName", ""); if (this.Properties.PropertyMap.ContainsKey("unity:sortingOrder")) { this.ExplicitSortingOrder = this.Properties.GetPropertyValueAsInt("unity:sortingOrder"); } // Are we using a unity:layer override? this.UnityLayerOverrideName = this.Properties.GetPropertyValueAsString("unity:layer", ""); // Add all our children this.LayerNodes = TmxLayerNode.ListFromXml(xml, this, this.TmxMap); }
public static TmxProperties FromXml(XElement elem) { TmxProperties tmxProps = new TmxProperties(); var props = from elem1 in elem.Elements("properties") from elem2 in elem1.Elements("property") select new { Name = TmxHelper.GetAttributeAsString(elem2, "name"), Value = TmxHelper.GetAttributeAsString(elem2, "value"), }; if (props.Count() > 0) { Program.WriteLine("Parse properites ..."); Program.WriteVerbose(elem.Element("properties").ToString()); } foreach (var p in props) { tmxProps.PropertyMap[p.Name] = p.Value; } return(tmxProps); }
public static TmxProperties FromXml(XElement elem) { TmxProperties tmxProps = new TmxProperties(); var props = from elem1 in elem.Elements("properties") from elem2 in elem1.Elements("property") select new { Name = TmxHelper.GetAttributeAsString(elem2, "name"), Type = TmxHelper.GetAttributeAsEnum(elem2, "type", TmxPropertyType.String), // Value may be attribute or inner text Value = TmxHelper.GetAttributeAsString(elem2, "value", null) ?? elem2.Value, }; if (props.Count() > 0) { Logger.WriteVerbose("Parse properites ..."); } foreach (var p in props) { tmxProps.PropertyMap[p.Name] = new TmxProperty { Name = p.Name, Type = p.Type, Value = p.Value }; } return(tmxProps); }
public static TmxImage FromXml(XElement elemImage) { TmxImage tmxImage = new TmxImage(); tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source"); tmxImage.ImageBitmap = (Bitmap)Bitmap.FromFile(tmxImage.AbsolutePath); tmxImage.Size = new System.Drawing.Size(tmxImage.ImageBitmap.Width, tmxImage.ImageBitmap.Height); // Some images use a transparency color key instead of alpha (blerg) tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", ""); if (!String.IsNullOrEmpty(tmxImage.TransparentColor)) { if (!tmxImage.TransparentColor.StartsWith("#")) { // The hash makes it an HTML color tmxImage.TransparentColor = "#" + tmxImage.TransparentColor; } System.Drawing.Color transColor = System.Drawing.ColorTranslator.FromHtml(tmxImage.TransparentColor); tmxImage.ImageBitmap.MakeTransparent(transColor); } return(tmxImage); }
public static TmxImage FromXml(XElement elemImage) { TmxImage tmxImage = new TmxImage(); tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source"); // Get default image size in case we are not opening the file { int width = TmxHelper.GetAttributeAsInt(elemImage, "width", 0); int height = TmxHelper.GetAttributeAsInt(elemImage, "height", 0); tmxImage.Size = new System.Drawing.Size(width, height); } // Do not open the image in Tiled2UnityLite (due to difficulty with GDI+ in some mono installs) #if !TILED_2_UNITY_LITE if (!Tiled2Unity.Settings.IsAutoExporting) { try { tmxImage.ImageBitmap = TmxHelper.FromFileBitmap32bpp(tmxImage.AbsolutePath); } catch (FileNotFoundException fnf) { string msg = String.Format("Image file not found: {0}", tmxImage.AbsolutePath); throw new TmxException(msg, fnf); // Testing for when image files are missing. Just make up an image. //int width = TmxHelper.GetAttributeAsInt(elemImage, "width"); //int height = TmxHelper.GetAttributeAsInt(elemImage, "height"); //tmxImage.ImageBitmap = new TmxHelper.CreateBitmap32bpp(width, height); //using (Graphics g = Graphics.FromImage(tmxImage.ImageBitmap)) //{ // int color32 = tmxImage.AbsolutePath.GetHashCode(); // Color color = Color.FromArgb(color32); // color = Color.FromArgb(255, color); // using (Brush brush = new SolidBrush(color)) // { // g.FillRectangle(brush, new Rectangle(Point.Empty, tmxImage.ImageBitmap.Size)); // } //} } tmxImage.Size = new System.Drawing.Size(tmxImage.ImageBitmap.Width, tmxImage.ImageBitmap.Height); } #endif // Some images use a transparency color key instead of alpha (blerg) tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", ""); #if !TILED_2_UNITY_LITE if (!String.IsNullOrEmpty(tmxImage.TransparentColor) && tmxImage.ImageBitmap != null) { System.Drawing.Color transColor = TmxHelper.ColorFromHtml(tmxImage.TransparentColor); tmxImage.ImageBitmap.MakeTransparent(transColor); } #endif return(tmxImage); }
public static TmxObjectType FromXml(XElement xml) { return(new TmxObjectType { Name = TmxHelper.GetAttributeAsString(xml, "name", ""), Color = TmxHelper.GetAttributeAsColor(xml, "color", new Color32(128, 128, 128, 255)), Properties = TmxObjectTypeProperty.FromObjectTypeXml(xml) }); }
public static TmxObjectType FromXml(XElement xml) { TmxObjectType tmxObjectType = new TmxObjectType(); tmxObjectType.Name = TmxHelper.GetAttributeAsString(xml, "name", ""); tmxObjectType.Color = TmxHelper.GetAttributeAsColor(xml, "color", Color.Gray); tmxObjectType.Properties = TmxObjectTypeProperty.FromObjectTypeXml(xml); return(tmxObjectType); }
public static TgxTemplate FromXml(XElement xml, uint firstId, TmxMap map) { TgxTemplate tgxTemplate = new TgxTemplate(map); tgxTemplate.Name = TmxHelper.GetAttributeAsString(xml, "name"); tgxTemplate.LocalId = TmxHelper.GetAttributeAsUInt(xml, "id"); tgxTemplate.GlobalId = firstId + tgxTemplate.LocalId; tgxTemplate.ObjectXml = xml.Element("object"); return(tgxTemplate); }
public static TmxObject FromXml(XElement xml, TmxObjectGroup tmxObjectGroup, TmxMap tmxMap) { Program.WriteLine("Parsing object ..."); Program.WriteVerbose(xml.ToString()); // What kind of TmxObject are we creating? TmxObject tmxObject = null; if (xml.Element("ellipse") != null) { tmxObject = new TmxObjectEllipse(); } else if (xml.Element("polygon") != null) { tmxObject = new TmxObjectPolygon(); } else if (xml.Element("polyline") != null) { tmxObject = new TmxObjectPolyline(); } else if (xml.Attribute("gid") != null) { tmxObject = new TmxObjectTile(); } else { // Just a rectangle tmxObject = new TmxObjectRectangle(); } // Data found on every object type tmxObject.Name = TmxHelper.GetAttributeAsString(xml, "name", ""); tmxObject.Type = TmxHelper.GetAttributeAsString(xml, "type", ""); tmxObject.Visible = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1; tmxObject.ParentObjectGroup = tmxObjectGroup; float x = TmxHelper.GetAttributeAsFloat(xml, "x"); float y = TmxHelper.GetAttributeAsFloat(xml, "y"); float w = TmxHelper.GetAttributeAsFloat(xml, "width", 0); float h = TmxHelper.GetAttributeAsFloat(xml, "height", 0); float r = TmxHelper.GetAttributeAsFloat(xml, "rotation", 0); tmxObject.Position = new System.Drawing.PointF(x, y); tmxObject.Size = new System.Drawing.SizeF(w, h); tmxObject.Rotation = r; tmxObject.Properties = TmxProperties.FromXml(xml); tmxObject.InternalFromXml(xml, tmxMap); return(tmxObject); }
public static Dictionary <string, TmxObjectTypeProperty> FromObjectTypeXml(XElement xmlObjectType) { Dictionary <string, TmxObjectTypeProperty> dictionary = new Dictionary <string, TmxObjectTypeProperty>(); foreach (XElement item in xmlObjectType.Elements("property")) { TmxObjectTypeProperty tmxObjectTypeProperty = new TmxObjectTypeProperty(); tmxObjectTypeProperty.Name = TmxHelper.GetAttributeAsString(item, "name", ""); tmxObjectTypeProperty.Type = TmxHelper.GetAttributeAsEnum(item, "type", TmxPropertyType.String); tmxObjectTypeProperty.Default = TmxHelper.GetAttributeAsString(item, "default", ""); dictionary.Add(tmxObjectTypeProperty.Name, tmxObjectTypeProperty); } return(dictionary); }
public static TmxImage FromXml(XElement elemImage) { TmxImage tmxImage = new TmxImage(); tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source"); try { tmxImage.ImageBitmap = (Bitmap)Bitmap.FromFile(tmxImage.AbsolutePath); } catch (FileNotFoundException fnf) { string msg = String.Format("Image file not found: {0}", tmxImage.AbsolutePath); throw new TmxException(msg, fnf); // Testing for when image files are missing. Just make up an image. //int width = TmxHelper.GetAttributeAsInt(elemImage, "width"); //int height = TmxHelper.GetAttributeAsInt(elemImage, "height"); //tmxImage.ImageBitmap = new Bitmap(width, height); //using (Graphics g = Graphics.FromImage(tmxImage.ImageBitmap)) //{ // int color32 = tmxImage.AbsolutePath.GetHashCode(); // Color color = Color.FromArgb(color32); // color = Color.FromArgb(255, color); // using (Brush brush = new SolidBrush(color)) // { // g.FillRectangle(brush, new Rectangle(Point.Empty, tmxImage.ImageBitmap.Size)); // } //} } tmxImage.Size = new System.Drawing.Size(tmxImage.ImageBitmap.Width, tmxImage.ImageBitmap.Height); // Some images use a transparency color key instead of alpha (blerg) tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", ""); if (!String.IsNullOrEmpty(tmxImage.TransparentColor)) { if (!tmxImage.TransparentColor.StartsWith("#")) { // The hash makes it an HTML color tmxImage.TransparentColor = "#" + tmxImage.TransparentColor; } System.Drawing.Color transColor = System.Drawing.ColorTranslator.FromHtml(tmxImage.TransparentColor); tmxImage.ImageBitmap.MakeTransparent(transColor); } return(tmxImage); }
private void ParseMapXml(XDocument doc) { Logger.WriteVerbose("Parsing map root ..."); XElement map = doc.Element("map"); try { this.Orientation = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation"); this.StaggerAxis = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y); this.StaggerIndex = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd); this.HexSideLength = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0); this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1; this.DrawOrderVertical = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1; this.Width = TmxHelper.GetAttributeAsInt(map, "width"); this.Height = TmxHelper.GetAttributeAsInt(map, "height"); this.TileWidth = TmxHelper.GetAttributeAsInt(map, "tilewidth"); this.TileHeight = TmxHelper.GetAttributeAsInt(map, "tileheight"); this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128)); } catch (Exception e) { TmxException.FromAttributeException(e, map); } // Collect our map properties this.Properties = TmxProperties.FromXml(map); // Check properties for us being a resource this.IsResource = this.Properties.GetPropertyValueAsBoolean("unity:resource", false); this.IsResource = this.IsResource || !String.IsNullOrEmpty(this.Properties.GetPropertyValueAsString("unity:resourcePath", null)); ParseAllTilesets(doc); // Get all our child layer nodes this.LayerNodes = TmxLayerNode.ListFromXml(map, null, this); // Calcuate the size of the map. Isometric and hex maps make this more complicated than a simple width and height thing. this.MapSizeInPixels = CalculateMapSizeInPixels(); // Visit each node in the map to assign display order TmxDisplayOrderVisitor visitor = new TmxDisplayOrderVisitor(); this.Visit(visitor); }
public void ParseTemplateGroupXml(XElement xml) { Name = TmxHelper.GetAttributeAsString(xml, "name"); NextTemplateId = TmxHelper.GetAttributeAsUInt(xml, "nexttemplateid"); TemplateMap = ParentMap.MakeTemplate(Name); foreach (XElement item in xml.Descendants("tileset")) { TsxTileset.FromXml(item, TemplateMap); } foreach (XElement item2 in xml.Descendants("template")) { TgxTemplate tgxTemplate = TgxTemplate.FromXml(item2, FirstTemplateId, TemplateMap); if (tgxTemplate != null) { Templates.Add(tgxTemplate); } } }
public static TmxLayer FromXml(XElement elem, int layerIndex) { Program.WriteVerbose(elem.ToString()); TmxLayer tmxLayer = new TmxLayer(); // Have to decorate layer names in order to force them into being unique // Also, can't have whitespace in the name because Unity will add underscores tmxLayer.DefaultName = TmxHelper.GetAttributeAsString(elem, "name"); tmxLayer.UniqueName = String.Format("{0}_{1}", tmxLayer.DefaultName, layerIndex.ToString("D2")).Replace(" ", "_"); tmxLayer.Visible = TmxHelper.GetAttributeAsInt(elem, "visible", 1) == 1; tmxLayer.Width = TmxHelper.GetAttributeAsInt(elem, "width"); tmxLayer.Height = TmxHelper.GetAttributeAsInt(elem, "height"); tmxLayer.Properties = TmxProperties.FromXml(elem); tmxLayer.ParseData(elem.Element("data")); return(tmxLayer); }
protected void FromXmlInternal(XElement xml) { Name = TmxHelper.GetAttributeAsString(xml, "name", ""); Visible = (TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1); Opacity = TmxHelper.GetAttributeAsFloat(xml, "opacity", 1f); PointF offset = new PointF(0f, 0f); offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0f); offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0f); Offset = offset; Properties = TmxProperties.FromXml(xml); Ignore = Properties.GetPropertyValueAsEnum("unity:ignore", IgnoreSettings.False); ExplicitSortingLayerName = Properties.GetPropertyValueAsString("unity:sortingLayerName", ""); if (Properties.PropertyMap.ContainsKey("unity:sortingOrder")) { ExplicitSortingOrder = Properties.GetPropertyValueAsInt("unity:sortingOrder"); } UnityLayerOverrideName = Properties.GetPropertyValueAsString("unity:layer", ""); LayerNodes = ListFromXml(xml, this, ParentMap); }
public static TmxImage FromXml(XElement elemImage) { TmxImage tmxImage = new TmxImage(); tmxImage.Path = TmxHelper.GetAttributeAsFullPath(elemImage, "source"); // Width and height are optional. int width = TmxHelper.GetAttributeAsInt(elemImage, "width", 0); int height = TmxHelper.GetAttributeAsInt(elemImage, "height", 0); // Prefer to use the actual width and height anyway so that UVs do not get jacked try { using (Image bitmap = Bitmap.FromFile(tmxImage.Path)) { width = bitmap.Width; height = bitmap.Height; } } // Problem with the image file (too large, or unsupported format). // Warn that there may be issues with the resulting map file. catch { Program.WriteWarning("Image file " + tmxImage.Path + " too large or unsupported format. There may be issues in resulting prefab."); } tmxImage.Size = new System.Drawing.Size(width, height); // Some images use a transparency color key instead of alpha (blerg) tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", ""); if (!String.IsNullOrEmpty(tmxImage.TransparentColor) && !tmxImage.TransparentColor.StartsWith("#")) { // The hash makes it an HTML color tmxImage.TransparentColor = "#" + tmxImage.TransparentColor; } return(tmxImage); }
private void ParseMapXml(XDocument doc) { Logger.WriteLine("Parsing map root ..."); XElement map = doc.Element("map"); try { this.Orientation = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation"); this.StaggerAxis = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y); this.StaggerIndex = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd); this.HexSideLength = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0); this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1; this.DrawOrderVertical = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1; this.Width = TmxHelper.GetAttributeAsInt(map, "width"); this.Height = TmxHelper.GetAttributeAsInt(map, "height"); this.TileWidth = TmxHelper.GetAttributeAsInt(map, "tilewidth"); this.TileHeight = TmxHelper.GetAttributeAsInt(map, "tileheight"); this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128)); } catch (Exception e) { TmxException.FromAttributeException(e, map); } // Collect our map properties this.Properties = TmxProperties.FromXml(map); // Check properties for us being a resource this.IsResource = this.Properties.GetPropertyValueAsBoolean("unity:resource", false); this.IsResource = this.IsResource || !String.IsNullOrEmpty(this.Properties.GetPropertyValueAsString("unity:resourcePath", null)); ParseAllTilesets(doc); ParseAllLayers(doc); ParseAllObjectGroups(doc); // Once everything is loaded, take a moment to do additional plumbing ParseCompleted(); }