diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index 5af1e73..0fcda97 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -14,7 +14,8 @@ namespace MLEM.Ui.Elements { public class Paragraph : Element { private string text; - private readonly FormattedString formattedText = new FormattedString(); + private string splitText; + private Dictionary codeLocations; private IGenericFont regularFont; private IGenericFont boldFont; private IGenericFont italicFont; @@ -53,9 +54,10 @@ namespace MLEM.Ui.Elements { var size = base.CalcActualSize(parentArea); var sc = this.TextScale * this.Scale; - this.formattedText.Value = this.regularFont.SplitString(this.text.RemoveFormatting(), size.X - this.ScaledPadding.X * 2, sc); + this.splitText = this.regularFont.SplitString(this.text.RemoveFormatting(), size.X - this.ScaledPadding.X * 2, sc); + this.codeLocations = this.text.GetFormattingCodes(); - var textDims = this.regularFont.MeasureString(this.formattedText) * sc; + var textDims = this.regularFont.MeasureString(this.splitText) * sc; return new Point(this.AutoAdjustWidth ? textDims.X.Ceil() + this.ScaledPadding.X * 2 : size.X, textDims.Y.Ceil() + this.ScaledPadding.Y * 2); } @@ -72,7 +74,14 @@ namespace MLEM.Ui.Elements { var pos = this.DisplayArea.Location.ToVector2(); var sc = this.TextScale * this.Scale; - this.formattedText.Draw(this.regularFont, batch, pos, this.TextColor * alpha, sc, this.boldFont, this.italicFont, 0, this.TimeIntoAnimation); + + // 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); + } base.Draw(time, batch, alpha); } diff --git a/MLEM/Formatting/FormattedString.cs b/MLEM/Formatting/FormattedString.cs deleted file mode 100644 index 604f706..0000000 --- a/MLEM/Formatting/FormattedString.cs +++ /dev/null @@ -1,51 +0,0 @@ -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 { - var val = value ?? string.Empty; - if (this.value != val) { - this.value = val; - this.isDirty = true; - } - } - } - private Dictionary codeLocations; - private string textWithoutFormatting; - private bool isDirty = true; - - 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