public override void OnLoad() { // Create resource asset resolver var assetResolver = new ResourceAssetResolver(GetType().Assembly, "Myra.Samples.Plugin.LibGDX.Resources."); // Load image containing font & ui spritesheet var colorBuffer = ColorBuffer.FromStream(assetResolver.Open("ui_stylesheet_image.png")); colorBuffer.PremultiplyAlpha(); var texture = colorBuffer.CreateTexture2D(); // Load ui text atlas var textureAtlas = TextureRegionAtlas.FromGDX(assetResolver.ReadAsString("ui_stylesheet_atlas.atlas"), s => texture); // Load ui font(s) var font = SpriteFontHelper.LoadFromFnt(assetResolver.ReadAsString("ui_font.fnt"), textureAtlas["default"]); // Load stylesheet var stylesheet = Stylesheet.CreateFromSource(assetResolver.ReadAsString("ui_stylesheet.json"), s => textureAtlas[s], s => font); Stylesheet.Current = stylesheet; }
static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("ToMyraAtlasConverter converts LibGDX texture atlas to Myra texture atlas."); Console.WriteLine("Usage: ToMyraAtlasConverter <input.atlas> <output.json>"); return; } try { var inputFile = args[0]; var outputFile = args[1]; var inputData = File.ReadAllText(inputFile); var atlas = TextureRegionAtlas.FromGDX(inputData, s => new Texture2D(null, 1, 1)); var json = atlas.ToJson(); File.WriteAllText(outputFile, json); } catch (Exception ex) { Console.WriteLine(ex); } }
protected override void LoadContent() { base.LoadContent(); MyraEnvironment.Game = this; // Create resource asset resolver var assetResolver = new ResourceAssetResolver(GetType().Assembly, "Myra.Samples.CustomUIStylesheet.Resources."); // Load image containing font & ui spritesheet Texture2D texture; using (var stream = assetResolver.Open("ui_stylesheet_atlas.png")) { texture = Texture2D.FromStream(GraphicsDevice, stream); } // Load ui text atlas var textureAtlas = TextureRegionAtlas.FromXml(assetResolver.ReadAsString("ui_stylesheet_atlas.xml"), texture); // Load ui font(s) var region = textureAtlas["commodore-64"]; var fonts = new Dictionary <string, SpriteFont> { ["commodore-64"] = BMFontLoader.LoadText( assetResolver.ReadAsString("commodore-64.fnt"), s => new TextureWithOffset(region.Texture, region.Bounds.Location) ) }; // Load stylesheet var stylesheet = Stylesheet.LoadFromSource( assetResolver.ReadAsString("ui_stylesheet.xml"), s => textureAtlas[s], s => fonts[s]); Stylesheet.Current = stylesheet; _desktop = new Desktop(); _allWidgets = new AllWidgets(); _allWidgets._button.Image = textureAtlas["music-off"]; _allWidgets._imageButton.Image = textureAtlas["sound-off"]; _desktop.Widgets.Add(_allWidgets); }
protected override void LoadContent() { base.LoadContent(); MyraEnvironment.Game = this; // Load image containing font & ui spritesheet var texture = Content.Load <Texture2D>("ui_stylesheet_atlas"); // Create resource asset resolver var assetResolver = new ResourceAssetResolver(GetType().Assembly, "Myra.Samples.CustomUIStylesheet.Resources."); // Load ui text atlas var textureAtlas = TextureRegionAtlas.FromJson(assetResolver.ReadAsString("ui_stylesheet_atlas.json"), texture); // Load ui font(s) var fonts = new Dictionary <string, SpriteFont> { ["commodore-64"] = SpriteFontHelper.LoadFromFnt(assetResolver.ReadAsString("commodore-64.fnt"), textureAtlas["commodore-64"]), }; // Load stylesheet var stylesheet = Stylesheet.CreateFromSource(assetResolver.ReadAsString("ui_stylesheet.json"), s => textureAtlas[s], s => fonts[s]); Stylesheet.Current = stylesheet; // Widget.DrawFrames = true; _host = new Desktop(); _allWidgets = new AllWidgets { Background = new Drawable(textureAtlas["blue"]) }; _allWidgets._button.Image = new Drawable(textureAtlas["music-off"]); _allWidgets._imageButton.Image = new Drawable(textureAtlas["sound-off"]); _host.Widgets.Add(_allWidgets); }
public override TileSet LoadItem(Module module, string id, ObjectData data) { var dataObj = data.Data; var result = new TileSet(); var textureAtlasFolder = Path.GetDirectoryName(data.Source); var textureAtlasFile = Path.Combine(textureAtlasFolder, dataObj.EnsureString("TextureAtlas")); var textureAtlasData = File.ReadAllText(textureAtlasFile); result.TextureAtlas = TextureRegionAtlas.Load(textureAtlasData, n => { using (var stream = File.OpenRead(Path.Combine(textureAtlasFolder, n))) return(Texture2D.FromStream(MyraEnvironment.GraphicsDevice, stream)); } ); var appearancesObj = dataObj.EnsureJObject("Images"); foreach (var pair in appearancesObj) { var appearanceObj = pair.Value.JConvertT <JObject>(); var regionId = appearanceObj.EnsureString("TextureKey"); var color = appearanceObj.EnsureColor("Color"); TextureRegion image; if (!result.TextureAtlas.Regions.TryGetValue(regionId, out image)) { RaiseError("Could not find TextureRegion with id '{0}'.", regionId); } var appearance = new Appearance(color, image); result.Appearances[pair.Key] = appearance; } return(result); }
private Stylesheet StylesheetFromFile(string path) { if (!Path.IsPathRooted(path)) { path = Path.Combine(Path.GetDirectoryName(FilePath), path); } Stylesheet stylesheet; if (_stylesheetCache.TryGetValue(path, out stylesheet)) { return(stylesheet); } var data = File.ReadAllText(path); var doc = XDocument.Parse(data); var root = doc.Root; var textureAtlases = new Dictionary <string, TextureRegionAtlas>(); var fonts = new Dictionary <string, SpriteFont>(); var designer = root.Element("Designer"); if (designer != null) { var folder = Path.GetDirectoryName(path); foreach (var element in designer.Elements()) { if (element.Name == "TextureAtlas") { var atlasPath = BuildPath(folder, element.Attribute("Atlas").Value); var imagePath = BuildPath(folder, element.Attribute("Image").Value); using (var stream = File.OpenRead(imagePath)) { var texture = Texture2D.FromStream(GraphicsDevice, stream); var atlasData = File.ReadAllText(atlasPath); textureAtlases[Path.GetFileName(atlasPath)] = TextureRegionAtlas.FromXml(atlasData, texture); } } else if (element.Name == "Font") { var id = element.Attribute("Id").Value; var fontPath = BuildPath(folder, element.Attribute("File").Value); var fontData = File.ReadAllText(fontPath); fonts[id] = BMFontLoader.LoadText(fontData, s => { if (s.Contains("#")) { var parts = s.Split('#'); var region = textureAtlases[parts[0]][parts[1]]; return(new TextureWithOffset(region.Texture, region.Bounds.Location)); } var imagePath = BuildPath(folder, s); using (var stream = File.OpenRead(imagePath)) { var texture = Texture2D.FromStream(GraphicsDevice, stream); return(new TextureWithOffset(texture)); } }); } } } stylesheet = Stylesheet.LoadFromSource(data, s => { TextureRegion result; foreach (var pair in textureAtlases) { if (pair.Value.Regions.TryGetValue(s, out result)) { return(result); } } throw new Exception(string.Format("Could not find texture region '{0}'", s)); }, s => { SpriteFont result; if (fonts.TryGetValue(s, out result)) { return(result); } throw new Exception(string.Format("Could not find font '{0}'", s)); } ); _stylesheetCache[path] = stylesheet; return(stylesheet); }
private Stylesheet StylesheetFromFile(string path) { var data = File.ReadAllText(path); var root = (Dictionary <string, object>)Json.Deserialize(data); var folder = Path.GetDirectoryName(path); // Load texture atlases var textureAtlases = new Dictionary <string, TextureRegionAtlas>(); Dictionary <string, object> textureAtlasesNode; if (root.GetStyle("textureAtlases", out textureAtlasesNode)) { foreach (var pair in textureAtlasesNode) { var atlasPath = BuildPath(folder, pair.Key.ToString()); var imagePath = BuildPath(folder, pair.Value.ToString()); using (var stream = File.OpenRead(imagePath)) { var texture = Texture2D.FromStream(GraphicsDevice, stream); var atlasData = File.ReadAllText(atlasPath); textureAtlases[pair.Key] = TextureRegionAtlas.FromJson(atlasData, texture); } } } // Load fonts var fonts = new Dictionary <string, SpriteFont>(); Dictionary <string, object> fontsNode; if (root.GetStyle("fonts", out fontsNode)) { foreach (var pair in fontsNode) { var fontPath = BuildPath(folder, pair.Value.ToString()); var fontData = File.ReadAllText(fontPath); fonts[pair.Key] = SpriteFontHelper.LoadFromFnt(fontData, s => { if (s.Contains("#")) { var parts = s.Split('#'); return(textureAtlases[parts[0]][parts[1]]); } var imagePath = BuildPath(folder, s); using (var stream = File.OpenRead(imagePath)) { var texture = Texture2D.FromStream(GraphicsDevice, stream); return(new TextureRegion(texture)); } }); } } return(Stylesheet.CreateFromSource(data, s => { TextureRegion result; foreach (var pair in textureAtlases) { if (pair.Value.Regions.TryGetValue(s, out result)) { return result; } } throw new Exception(string.Format("Could not find texture region '{0}'", s)); }, s => { SpriteFont result; if (fonts.TryGetValue(s, out result)) { return result; } throw new Exception(string.Format("Could not find font '{0}'", s)); } )); }
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 !XENKO 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); }
public static Stylesheet LoadFromSource(string stylesheetXml, TextureRegionAtlas textureRegionAtlas, Dictionary <string, SpriteFontBase> fonts) { var xDoc = XDocument.Parse(stylesheetXml); var colors = new Dictionary <string, Color>(); var colorsNode = xDoc.Root.Element("Colors"); if (colorsNode != null) { foreach (var el in colorsNode.Elements()) { var color = ColorStorage.FromName(el.Attribute("Value").Value); if (color != null) { colors[el.Attribute(BaseContext.IdName).Value] = color.Value; } } } Func <Type, string, object> resourceGetter = (t, name) => { if (typeof(IBrush).IsAssignableFrom(t)) { TextureRegion region; if (!textureRegionAtlas.Regions.TryGetValue(name, out region)) { var color = ColorStorage.FromName(name); if (color != null) { return(new SolidBrush(color.Value)); } } else { return(region); } throw new Exception(string.Format("Could not find parse IBrush '{0}'", name)); } else if (t == typeof(SpriteFontBase)) { return(fonts[name]); } throw new Exception(string.Format("Type {0} isn't supported", t.Name)); }; var result = new Stylesheet { Atlas = textureRegionAtlas, Fonts = fonts }; var loadContext = new LoadContext { Namespaces = new[] { typeof(WidgetStyle).Namespace }, ResourceGetter = resourceGetter, NodesToIgnore = new HashSet <string>(new[] { "Designer", "Colors", "Fonts" }), LegacyClassNames = LegacyClassNames, LegacyPropertyNames = LegacyPropertyNames, Colors = colors }; loadContext.Load(result, xDoc.Root); return(result); }