public static TextureRegionAtlas FromGDX(string data, Func <string, Texture2D> textureLoader) { var mode = GDXMode.PageHeader; 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 }; mode = GDXMode.PageHeader; continue; } if (!s.Contains(":")) { spriteData = new GDXSpriteData { PageData = pageData, Name = s }; spriteDatas[s] = spriteData; mode = GDXMode.Sprite; 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)) { format = SurfaceFormat.Color; } 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 PaddingInfo { 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 drawables = new Dictionary <string, TextureRegion>(); foreach (var sd in spriteDatas) { var texture = sd.Value.PageData.Texture; var bounds = sd.Value.SourceRectangle; TextureRegion drawable; if (!sd.Value.Split.HasValue) { drawable = new TextureRegion(texture, bounds); } else { drawable = new NinePatchRegion(texture, bounds, sd.Value.Split.Value); } drawables[sd.Key] = drawable; } return(new TextureRegionAtlas(drawables)); }
public NinePatchRegion(Texture2D texture, Rectangle bounds, Thickness info) : base(texture, bounds) { _info = info; var centerWidth = bounds.Width - info.Left - info.Right; var centerHeight = bounds.Height - info.Top - info.Bottom; var y = bounds.Y; if (info.Top > 0) { if (info.Left > 0) { _topLeft = new TextureRegion(texture, new Rectangle(bounds.X, y, info.Left, info.Top)); } if (centerWidth > 0) { _topCenter = new TextureRegion(texture, new Rectangle(bounds.X + info.Left, y, centerWidth, info.Top)); } if (info.Right > 0) { _topRight = new TextureRegion(texture, new Rectangle(bounds.X + info.Left + centerWidth, y, info.Right, info.Top)); } } y += info.Top; if (centerHeight > 0) { if (info.Left > 0) { _centerLeft = new TextureRegion(texture, new Rectangle(bounds.X, y, info.Left, centerHeight)); } if (centerWidth > 0) { _center = new TextureRegion(texture, new Rectangle(bounds.X + info.Left, y, centerWidth, centerHeight)); } if (info.Right > 0) { _centerRight = new TextureRegion(texture, new Rectangle(bounds.X + info.Left + centerWidth, y, info.Right, centerHeight)); } } y += centerHeight; if (info.Bottom > 0) { if (info.Left > 0) { _bottomLeft = new TextureRegion(texture, new Rectangle(bounds.X, y, info.Left, info.Bottom)); } if (centerWidth > 0) { _bottomCenter = new TextureRegion(texture, new Rectangle(bounds.X + info.Left, y, centerWidth, info.Bottom)); } if (info.Right > 0) { _bottomRight = new TextureRegion(texture, new Rectangle(bounds.X + info.Left + centerWidth, y, info.Right, info.Bottom)); } } }
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": break; case "filter": break; case "repeat": 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 region; if (!sd.Value.Split.HasValue) { region = new TextureRegion(texture, bounds); } else { region = new NinePatchRegion(texture, bounds, sd.Value.Split.Value); } regions[sd.Key] = region; } return(result); }