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); }
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); }