From 9ce01bc6f2559d42f7cdc02b01dbb68d7f2d1173 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 15 May 2020 21:31:37 +0200 Subject: [PATCH] added underlined formatting code --- MLEM/Formatting/Codes/UnderlineCode.cs | 29 ++++++++++++++++++++++++++ MLEM/Formatting/TextFormatter.cs | 3 ++- Sandbox/GameImpl.cs | 3 ++- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 MLEM/Formatting/Codes/UnderlineCode.cs diff --git a/MLEM/Formatting/Codes/UnderlineCode.cs b/MLEM/Formatting/Codes/UnderlineCode.cs new file mode 100644 index 0000000..955ef23 --- /dev/null +++ b/MLEM/Formatting/Codes/UnderlineCode.cs @@ -0,0 +1,29 @@ +using System.Text.RegularExpressions; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MLEM.Extensions; +using MLEM.Font; +using MLEM.Misc; + +namespace MLEM.Formatting.Codes { + public class UnderlineCode : FontCode { + + private readonly float thickness; + private readonly float yOffset; + + public UnderlineCode(Match match, Regex regex, float thickness, float yOffset) : base(match, regex, null) { + this.thickness = thickness; + this.yOffset = yOffset; + } + + public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { + // don't underline spaces at the end of lines + if (c == ' ' && this.Token.Substring.Length > indexInToken + 1 && this.Token.Substring[indexInToken + 1] == '\n') + return false; + var width = font.MeasureString(cString).X * scale; + batch.Draw(batch.GetBlankTexture(), new RectangleF(pos.X, pos.Y + this.yOffset * font.LineHeight - this.thickness * scale, width, this.thickness * scale), color); + return false; + } + + } +} \ No newline at end of file diff --git a/MLEM/Formatting/TextFormatter.cs b/MLEM/Formatting/TextFormatter.cs index eb1343d..88443fb 100644 --- a/MLEM/Formatting/TextFormatter.cs +++ b/MLEM/Formatting/TextFormatter.cs @@ -19,7 +19,8 @@ namespace MLEM.Formatting { this.Codes.Add(new Regex(""), (f, m, r) => new FontCode(m, r, boldFont?.Invoke())); this.Codes.Add(new Regex(""), (f, m, r) => new FontCode(m, r, italicFont?.Invoke())); this.Codes.Add(new Regex(@""), (f, m, r) => new ShadowCode(m, r, m.Groups[1].Success ? ColorExtensions.FromHex(m.Groups[1].Value) : Color.Black, new Vector2(float.TryParse(m.Groups[2].Value, out var offset) ? offset : 2))); - this.Codes.Add(new Regex(""), (f, m, r) => new FontCode(m, r, null)); + this.Codes.Add(new Regex(""), (f, m, r) => new UnderlineCode(m, r, 2, 0.85F)); + this.Codes.Add(new Regex(""), (f, m, r) => new FontCode(m, r, null)); // color codes foreach (var c in typeof(Color).GetProperties()) { diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index f20b858..58951f3 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -117,8 +117,9 @@ namespace Sandbox { var formatter = new TextFormatter(); formatter.AddImage("Test", new TextureRegion(tex, 0, 8, 24, 24)); //var strg = "This is a formatted string with two bits of formatting! It also includesaverylongwordthatisformattedaswell. Additionally, it wobbles and has a shadow or a weird shadow. We like icons too! "; - var strg = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; + //var strg = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; //var strg = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; + var strg = "This is a test of the underlined formatting code!"; this.tokenized = formatter.Tokenize(font, strg); this.tokenized.Split(font, 400, 1);