diff --git a/CHANGELOG.md b/CHANGELOG.md index b0baded..f4f756e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ Additions - Added TokenizedString.GetArea - Added InputHandler.WasPressedForLess and related methods as well as InputHandler.IsPressedIgnoreRepeats - Added RandomExtensions.NextSingle with minimum and maximum values +- Added subscript and superscript formatting codes - **Added the ability to find paths to one of multiple goals using AStar** Improvements diff --git a/Demos/TextFormattingDemo.cs b/Demos/TextFormattingDemo.cs index 3d93262..312d4e9 100644 --- a/Demos/TextFormattingDemo.cs +++ b/Demos/TextFormattingDemo.cs @@ -18,7 +18,7 @@ namespace Demos { "You can write in bold, italics, with an underline, strikethrough, with a drop shadow whose color and offset you can modify in each application of the code, or with various types of combined formatting codes.\n\n" + "You can apply custom colors to text, including all default MonoGame colors and inline custom colors.\n\n" + "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!\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; diff --git a/MLEM/Formatting/Codes/SubSupCode.cs b/MLEM/Formatting/Codes/SubSupCode.cs new file mode 100644 index 0000000..a39f4a4 --- /dev/null +++ b/MLEM/Formatting/Codes/SubSupCode.cs @@ -0,0 +1,24 @@ +using System.Text.RegularExpressions; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MLEM.Font; + +namespace MLEM.Formatting.Codes { + /// + public class SubSupCode : Code { + + private readonly float offset; + + /// + public SubSupCode(Match match, Regex regex, float offset) : base(match, regex) { + this.offset = offset; + } + + /// + public override bool DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { + pos.Y += this.offset * font.LineHeight * scale; + return false; + } + + } +} diff --git a/MLEM/Formatting/TextFormatter.cs b/MLEM/Formatting/TextFormatter.cs index d4589fa..0bfd263 100644 --- a/MLEM/Formatting/TextFormatter.cs +++ b/MLEM/Formatting/TextFormatter.cs @@ -58,6 +58,12 @@ namespace MLEM.Formatting { // control codes this.Codes.Add(new Regex(@""), (f, m, r) => new SimpleEndCode(m, r, m.Groups[1].Value)); + // superscript and subscript codes + this.Codes.Add(new Regex(@""), (f, m, r) => new SubSupCode(m, r, + float.TryParse(m.Groups[1].Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var off) ? off : 0.15F)); + this.Codes.Add(new Regex(@""), (f, m, r) => new SubSupCode(m, r, + float.TryParse(m.Groups[1].Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var off) ? -off : -0.25F)); + // macros this.Macros.Add(new Regex("~"), (f, m, r) => GenericFont.Nbsp.ToString()); this.Macros.Add(new Regex(""), (f, m, r) => '\n'.ToString());