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.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"), s => textureAtlas["commodore-64"]), }; // Load stylesheet var stylesheet = Stylesheet.CreateFromSource(assetResolver.ReadAsString("ui_stylesheet.json"), s => textureAtlas[s], s => fonts[s]); Stylesheet.Current = stylesheet; _host = new Desktop(); _allWidgets = new AllWidgets(); _allWidgets._button.Image = textureAtlas["music-off"]; _allWidgets._imageButton.Image = textureAtlas["sound-off"]; _host.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); }
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)); } )); }