From 6d236fa52cb0e342930fdb9bd88f042cddb0c751 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 6 Sep 2019 18:06:53 +0200 Subject: [PATCH] added FormattedString class to make rendering formatted text a little easier --- MLEM.Ui/Elements/Paragraph.cs | 17 +++------- MLEM/Formatting/FormattedString.cs | 50 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 MLEM/Formatting/FormattedString.cs diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index 0fcda97..5af1e73 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -14,8 +14,7 @@ namespace MLEM.Ui.Elements { public class Paragraph : Element { private string text; - private string splitText; - private Dictionary codeLocations; + private readonly FormattedString formattedText = new FormattedString(); private IGenericFont regularFont; private IGenericFont boldFont; private IGenericFont italicFont; @@ -54,10 +53,9 @@ namespace MLEM.Ui.Elements { var size = base.CalcActualSize(parentArea); var sc = this.TextScale * this.Scale; - this.splitText = this.regularFont.SplitString(this.text.RemoveFormatting(), size.X - this.ScaledPadding.X * 2, sc); - this.codeLocations = this.text.GetFormattingCodes(); + this.formattedText.Value = this.regularFont.SplitString(this.text.RemoveFormatting(), size.X - this.ScaledPadding.X * 2, sc); - var textDims = this.regularFont.MeasureString(this.splitText) * sc; + var textDims = this.regularFont.MeasureString(this.formattedText) * sc; return new Point(this.AutoAdjustWidth ? textDims.X.Ceil() + this.ScaledPadding.X * 2 : size.X, textDims.Y.Ceil() + this.ScaledPadding.Y * 2); } @@ -74,14 +72,7 @@ namespace MLEM.Ui.Elements { var pos = this.DisplayArea.Location.ToVector2(); var sc = this.TextScale * this.Scale; - - // if we don't have any formatting codes, then we don't need to do complex drawing - if (this.codeLocations.Count <= 0) { - this.regularFont.DrawString(batch, this.splitText, pos, this.TextColor * alpha, 0, Vector2.Zero, sc, SpriteEffects.None, 0); - } else { - // if we have formatting codes, we should do it - this.regularFont.DrawFormattedString(batch, pos, this.splitText, this.codeLocations, this.TextColor * alpha, sc, this.boldFont, this.italicFont, 0, this.TimeIntoAnimation); - } + this.formattedText.Draw(this.regularFont, batch, pos, this.TextColor * alpha, sc, this.boldFont, this.italicFont, 0, this.TimeIntoAnimation); base.Draw(time, batch, alpha); } diff --git a/MLEM/Formatting/FormattedString.cs b/MLEM/Formatting/FormattedString.cs new file mode 100644 index 0000000..3cc19ff --- /dev/null +++ b/MLEM/Formatting/FormattedString.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MLEM.Font; + +namespace MLEM.Formatting { + public class FormattedString { + + private string value; + public string Value { + get => this.value; + set { + if (this.value != value) { + this.value = value; + this.isDirty = true; + } + } + } + private Dictionary codeLocations; + private string textWithoutFormatting; + private bool isDirty; + + public FormattedString(string s = null) { + this.Value = s; + } + + public static implicit operator string(FormattedString s) { + return s.Value; + } + + private void CheckDirtyState() { + if (this.isDirty) { + this.isDirty = false; + this.codeLocations = this.value.GetFormattingCodes(); + this.textWithoutFormatting = this.value.RemoveFormatting(); + } + } + + public void Draw(IGenericFont regularFont, SpriteBatch batch, Vector2 pos, Color color, float scale, IGenericFont boldFont = null, IGenericFont italicFont = null, float depth = 0, TimeSpan timeIntoAnimation = default) { + this.CheckDirtyState(); + if (this.codeLocations.Count <= 0) { + regularFont.DrawString(batch, this.textWithoutFormatting, pos, color, 0, Vector2.Zero, scale, SpriteEffects.None, 0); + } else { + regularFont.DrawFormattedString(batch, pos, this.textWithoutFormatting, this.codeLocations, color, scale, boldFont, italicFont, depth, timeIntoAnimation); + } + } + + } +} \ No newline at end of file