diff --git a/MLEM/Formatting/TextFormatter.cs b/MLEM/Formatting/TextFormatter.cs index c36065a..9faea0c 100644 --- a/MLEM/Formatting/TextFormatter.cs +++ b/MLEM/Formatting/TextFormatter.cs @@ -57,6 +57,10 @@ namespace MLEM.Formatting { public TokenizedString Tokenize(GenericFont font, string s) { var tokens = new List(); var codes = new List(); + // add the formatting code right at the start of the string + var firstCode = this.GetNextCode(s, 0, 0); + if (firstCode != null) + codes.Add(firstCode); var rawIndex = 0; while (rawIndex < s.Length) { var index = StripFormatting(font, s.Substring(0, rawIndex), tokens.SelectMany(t => t.AppliedCodes)).Length; @@ -82,10 +86,10 @@ namespace MLEM.Formatting { return new TokenizedString(font, s, StripFormatting(font, s, tokens.SelectMany(t => t.AppliedCodes)), tokens.ToArray()); } - private Code GetNextCode(string s, int index) { + private Code GetNextCode(string s, int index, int maxIndex = int.MaxValue) { var (c, m, r) = this.Codes .Select(kv => (c: kv.Value, m: kv.Key.Match(s, index), r: kv.Key)) - .Where(kv => kv.m.Success) + .Where(kv => kv.m.Success && kv.m.Index <= maxIndex) .OrderBy(kv => kv.m.Index) .FirstOrDefault(); return c?.Invoke(this, m, r); diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index 1dc9f6b..375d009 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -124,21 +124,21 @@ 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 = "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 = "This is a test of the underlined formatting code!"; this.tokenized = formatter.Tokenize(font, strg); this.tokenized.Split(font, 400, 5); - /*this.OnDraw += (g, time) => { + this.OnDraw += (g, time) => { this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp); this.SpriteBatch.FillRectangle(new RectangleF(400, 20, 400, 1000), Color.Green); font.DrawString(this.SpriteBatch, this.tokenized.DisplayString, new Vector2(400, 20), Color.White * 0.25F, 0, Vector2.Zero, 5, SpriteEffects.None, 0); this.tokenized.Draw(time, this.SpriteBatch, new Vector2(400, 20), font, Color.White, 5, 0); this.SpriteBatch.DrawGrid(new Vector2(30, 30), new Vector2(40, 60), new Point(10, 5), Color.Yellow, 3); this.SpriteBatch.End(); - };*/ + }; this.OnUpdate += (g, time) => { if (this.InputHandler.IsPressed(Keys.W)) { this.tokenized = formatter.Tokenize(font, strg);