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

Revert "added FormattedString class to make rendering formatted text a little easier"

This reverts commit 6d236fa5
This commit is contained in:
Ellpeck 2019-09-08 16:25:59 +02:00
parent 7d9da6c519
commit 4a7fcd7570
2 changed files with 13 additions and 55 deletions

View file

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

View file

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