diff --git a/Demos/Content/Content.mgcb b/Demos/Content/Content.mgcb index 8ef5041..4949f21 100644 --- a/Demos/Content/Content.mgcb +++ b/Demos/Content/Content.mgcb @@ -41,6 +41,27 @@ /processorParam:TextureFormat=Compressed /build:Fonts/TestFontItalic.spritefont +#begin Fonts/Roboto.spritefont +/importer:FontDescriptionImporter +/processor:FontDescriptionProcessor +/processorParam:PremultiplyAlpha=True +/processorParam:TextureFormat=Compressed +/build:Fonts/Roboto.spritefont + +#begin Fonts/RobotoBold.spritefont +/importer:FontDescriptionImporter +/processor:FontDescriptionProcessor +/processorParam:PremultiplyAlpha=True +/processorParam:TextureFormat=Compressed +/build:Fonts/RobotoBold.spritefont + +#begin Fonts/RobotoItalic.spritefont +/importer:FontDescriptionImporter +/processor:FontDescriptionProcessor +/processorParam:PremultiplyAlpha=True +/processorParam:TextureFormat=Compressed +/build:Fonts/RobotoItalic.spritefont + #begin Markdown.md /copy:Markdown.md diff --git a/Demos/Content/Fonts/Roboto.spritefont b/Demos/Content/Fonts/Roboto.spritefont new file mode 100644 index 0000000..39fc6e0 --- /dev/null +++ b/Demos/Content/Fonts/Roboto.spritefont @@ -0,0 +1,60 @@ + + + + + + + RobotoRegular.ttf + + + 32 + + + 0 + + + true + + + + + + * + + + + + + ɏ + + + + diff --git a/Demos/Content/Fonts/RobotoBold.spritefont b/Demos/Content/Fonts/RobotoBold.spritefont new file mode 100644 index 0000000..b2e6112 --- /dev/null +++ b/Demos/Content/Fonts/RobotoBold.spritefont @@ -0,0 +1,60 @@ + + + + + + + RobotoBold.ttf + + + 32 + + + 0 + + + true + + + + + + * + + + + + + ɏ + + + + diff --git a/Demos/Content/Fonts/RobotoBold.ttf b/Demos/Content/Fonts/RobotoBold.ttf new file mode 100644 index 0000000..68822ca Binary files /dev/null and b/Demos/Content/Fonts/RobotoBold.ttf differ diff --git a/Demos/Content/Fonts/RobotoItalic.spritefont b/Demos/Content/Fonts/RobotoItalic.spritefont new file mode 100644 index 0000000..2da7e47 --- /dev/null +++ b/Demos/Content/Fonts/RobotoItalic.spritefont @@ -0,0 +1,60 @@ + + + + + + + RobotoItalic.ttf + + + 32 + + + 0 + + + true + + + + + + * + + + + + + ɏ + + + + diff --git a/Demos/Content/Fonts/RobotoItalic.ttf b/Demos/Content/Fonts/RobotoItalic.ttf new file mode 100644 index 0000000..2041cbc Binary files /dev/null and b/Demos/Content/Fonts/RobotoItalic.ttf differ diff --git a/Demos/Content/Fonts/RobotoRegular.ttf b/Demos/Content/Fonts/RobotoRegular.ttf new file mode 100644 index 0000000..0e58508 Binary files /dev/null and b/Demos/Content/Fonts/RobotoRegular.ttf differ diff --git a/Demos/TextFormattingDemo.cs b/Demos/TextFormattingDemo.cs index 312d4e9..2ba6a02 100644 --- a/Demos/TextFormattingDemo.cs +++ b/Demos/TextFormattingDemo.cs @@ -20,13 +20,14 @@ namespace Demos { "You can also use animations like a wobbly one, as well as create custom ones using the Code class.\n\n" + "You can also display icons in your text, and use superscript or subscript formatting!\n\n" + "Additionally, the text formatter has various methods for interacting with the text, like custom behaviors when hovering over certain parts, and more."; - private const float Scale = 0.5F; - private const float Width = 0.9F; + private const float DefaultScale = 0.5F; + private const float WidthMultiplier = 0.9F; private TextFormatter formatter; private TokenizedString tokenizedText; private GenericFont font; private bool drawBounds; + private float scale = TextFormattingDemo.DefaultScale; public TextFormattingDemo(MlemGame game) : base(game) {} @@ -38,9 +39,9 @@ namespace Demos { // GenericFont and its subtypes are wrappers around various font classes, including SpriteFont, MonoGame.Extended's BitmapFont and FontStashSharp // supplying a bold and italic version of the font here allows for the bold and italic formatting codes to be used this.font = new GenericSpriteFont( - Demo.LoadContent("Fonts/TestFont"), - Demo.LoadContent("Fonts/TestFontBold"), - Demo.LoadContent("Fonts/TestFontItalic")); + Demo.LoadContent("Fonts/Roboto"), + Demo.LoadContent("Fonts/RobotoBold"), + Demo.LoadContent("Fonts/RobotoItalic")); // adding the image code used in the example to it var testTexture = Demo.LoadContent("Textures/Test"); @@ -49,7 +50,7 @@ namespace Demos { // tokenizing our text and splitting it to fit the screen // we specify our text alignment here too, so that all data is cached correctly for display this.tokenizedText = this.formatter.Tokenize(this.font, TextFormattingDemo.Text, TextAlignment.Center); - this.tokenizedText.Split(this.font, this.GraphicsDevice.Viewport.Width * TextFormattingDemo.Width, TextFormattingDemo.Scale, TextAlignment.Center); + this.tokenizedText.Split(this.font, this.GraphicsDevice.Viewport.Width * TextFormattingDemo.WidthMultiplier, this.scale, TextAlignment.Center); } public override void DoDraw(GameTime time) { @@ -58,7 +59,7 @@ namespace Demos { // we draw the tokenized text in the center of the screen // since the text is already center-aligned, we only need to align it on the y axis here - var size = this.tokenizedText.GetArea(Vector2.Zero, TextFormattingDemo.Scale).Size; + var size = this.tokenizedText.GetArea(Vector2.Zero, this.scale).Size; var pos = new Vector2(this.GraphicsDevice.Viewport.Width / 2, (this.GraphicsDevice.Viewport.Height - size.Y) / 2); // draw bounds, which can be toggled with B in this demo @@ -66,13 +67,13 @@ namespace Demos { var blank = this.SpriteBatch.GetBlankTexture(); this.SpriteBatch.Draw(blank, new RectangleF(pos - new Vector2(size.X / 2, 0), size), Color.Red * 0.25F); foreach (var token in this.tokenizedText.Tokens) { - foreach (var area in token.GetArea(pos, TextFormattingDemo.Scale)) + foreach (var area in token.GetArea(pos, this.scale)) this.SpriteBatch.Draw(blank, area, Color.Black * 0.25F); } } // draw the text itself - this.tokenizedText.Draw(time, this.SpriteBatch, pos, this.font, Color.White, TextFormattingDemo.Scale, 0); + this.tokenizedText.Draw(time, this.SpriteBatch, pos, this.font, Color.White, this.scale, 0); this.SpriteBatch.End(); } @@ -90,9 +91,13 @@ namespace Demos { } private void OnResize(object sender, EventArgs e) { + // scale our text based on window size + var viewport = new Rectangle(0, 0, this.Game.Window.ClientBounds.Width, this.Game.Window.ClientBounds.Height); + this.scale = TextFormattingDemo.DefaultScale * Math.Min(viewport.Width / 1280F, viewport.Height / 720F); + // re-split our text if the window resizes, since it depends on the window size // this doesn't require re-tokenization of the text, since TokenizedString also stores the un-split version - this.tokenizedText.Split(this.font, this.GraphicsDevice.Viewport.Width * TextFormattingDemo.Width, TextFormattingDemo.Scale, TextAlignment.Center); + this.tokenizedText.Split(this.font, this.GraphicsDevice.Viewport.Width * TextFormattingDemo.WidthMultiplier, this.scale, TextAlignment.Center); } }