1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00

added FormattedString class to make rendering formatted text a little easier

This commit is contained in:
Ellpeck 2019-09-06 18:06:53 +02:00
parent b2b59aa78a
commit 6d236fa52c
2 changed files with 54 additions and 13 deletions

View file

@ -14,8 +14,7 @@ namespace MLEM.Ui.Elements {
public class Paragraph : Element { public class Paragraph : Element {
private string text; private string text;
private string splitText; private readonly FormattedString formattedText = new FormattedString();
private Dictionary<int, FormattingCode> codeLocations;
private IGenericFont regularFont; private IGenericFont regularFont;
private IGenericFont boldFont; private IGenericFont boldFont;
private IGenericFont italicFont; private IGenericFont italicFont;
@ -54,10 +53,9 @@ namespace MLEM.Ui.Elements {
var size = base.CalcActualSize(parentArea); var size = base.CalcActualSize(parentArea);
var sc = this.TextScale * this.Scale; var sc = this.TextScale * this.Scale;
this.splitText = this.regularFont.SplitString(this.text.RemoveFormatting(), size.X - this.ScaledPadding.X * 2, sc); this.formattedText.Value = this.regularFont.SplitString(this.text.RemoveFormatting(), size.X - this.ScaledPadding.X * 2, sc);
this.codeLocations = this.text.GetFormattingCodes();
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); 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 pos = this.DisplayArea.Location.ToVector2();
var sc = this.TextScale * this.Scale; 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); base.Draw(time, batch, alpha);
} }

View file

@ -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<int, FormattingCode> 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);
}
}
}
}