public static TextureRegionAtlas FromXml(string xml, Func <string, Texture2D> textureGetter) { var doc = XDocument.Parse(xml); var root = doc.Root; var result = new TextureRegionAtlas(); var imageFileAttr = root.Attribute(ImageName); if (imageFileAttr == null) { throw new Exception("Mandatory attribute 'ImageFile' doesnt exist"); } result.Image = imageFileAttr.Value; var texture = textureGetter(result.Image); foreach (XElement entry in root.Elements()) { var id = entry.Attribute(BaseContext.IdName).Value; var bounds = new Rectangle( int.Parse(entry.Attribute(LeftName).Value), int.Parse(entry.Attribute(TopName).Value), int.Parse(entry.Attribute(WidthName).Value), int.Parse(entry.Attribute(HeightName).Value) ); var isNinePatch = entry.Name == NinePatchRegionName; TextureRegion region; if (!isNinePatch) { region = new TextureRegion(texture, bounds); } else { var padding = new Thickness { Left = int.Parse(entry.Attribute(NinePatchLeftName).Value), Top = int.Parse(entry.Attribute(NinePatchTopName).Value), Right = int.Parse(entry.Attribute(NinePatchRightName).Value), Bottom = int.Parse(entry.Attribute(NinePatchBottomName).Value) }; region = new NinePatchRegion(texture, bounds, padding); } result[id] = region; } return(result); }
public static TextureRegionAtlas FromGDX(string data, Func <string, Texture2D> textureLoader) { GDXPageData pageData = null; var spriteDatas = new Dictionary <string, GDXSpriteData>(); using (var textReader = new StringReader(data)) { GDXSpriteData spriteData = null; while (true) { var s = textReader.ReadLine(); if (s == null) { break; } s = s.Trim(); if (string.IsNullOrEmpty(s)) { // New PageData pageData = null; continue; } if (pageData == null) { var texture = textureLoader(s); if (texture == null) { throw new Exception(string.Format("Unable to resolve texture {0}", s)); } pageData = new GDXPageData { Texture = texture }; continue; } if (!s.Contains(":")) { spriteData = new GDXSpriteData { PageData = pageData, Name = s }; spriteDatas[s] = spriteData; continue; } var parts = s.Split(':'); var key = parts[0].Trim().ToLower(); var value = parts[1].Trim(); switch (key) { case "format": SurfaceFormat format; if (!Enum.TryParse(value, out format)) { #if !STRIDE format = SurfaceFormat.Color; #else format = SurfaceFormat.R8G8B8A8_SNorm; #endif } pageData.Format = format; break; case "filter": parts = value.Split(','); var minFilter = (GDXTextureFilter)Enum.Parse(typeof(GDXTextureFilter), parts[0].Trim()); var magFilter = (GDXTextureFilter)Enum.Parse(typeof(GDXTextureFilter), parts[1].Trim()); pageData.Filter = FromGDXFilters(minFilter, magFilter); break; case "repeat": if (value == "x") { pageData.UWrap = TextureAddressMode.Wrap; } else if (value == "y") { pageData.VWrap = TextureAddressMode.Wrap; } else if (value == "xy") { pageData.UWrap = TextureAddressMode.Wrap; pageData.VWrap = TextureAddressMode.Wrap; } break; case "rotate": bool isRotated; bool.TryParse(value, out isRotated); spriteData.IsRotated = isRotated; break; case "xy": parts = value.Split(','); spriteData.SourceRectangle.X = int.Parse(parts[0].Trim()); spriteData.SourceRectangle.Y = int.Parse(parts[1].Trim()); break; case "size": if (spriteData == null) { continue; } parts = value.Split(','); spriteData.SourceRectangle.Width = int.Parse(parts[0].Trim()); spriteData.SourceRectangle.Height = int.Parse(parts[1].Trim()); break; case "orig": parts = value.Split(','); spriteData.OriginalSize = new Point(int.Parse(parts[0].Trim()), int.Parse(parts[1].Trim())); break; case "offset": parts = value.Split(','); spriteData.Offset = new Point(int.Parse(parts[0].Trim()), int.Parse(parts[1].Trim())); break; case "split": parts = value.Split(','); var split = new Thickness { Left = int.Parse(parts[0].Trim()), Right = int.Parse(parts[1].Trim()), Top = int.Parse(parts[2].Trim()), Bottom = int.Parse(parts[3].Trim()) }; spriteData.Split = split; break; } } } var result = new TextureRegionAtlas(); var regions = result.Regions; foreach (var sd in spriteDatas) { var texture = sd.Value.PageData.Texture; var bounds = sd.Value.SourceRectangle; TextureRegion IRenderable; if (!sd.Value.Split.HasValue) { IRenderable = new TextureRegion(texture, bounds); } else { IRenderable = new NinePatchRegion(texture, bounds, sd.Value.Split.Value); } regions[sd.Key] = IRenderable; } return(result); }