1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-17 19:20:07 +01:00
MLEM/MLEM.Ui/Elements/Paragraph.cs

127 lines
5.5 KiB
C#
Raw Normal View History

2019-08-09 19:28:48 +02:00
using System;
using System.Collections.Generic;
2019-08-16 19:08:36 +02:00
using System.Diagnostics;
2019-08-09 19:28:48 +02:00
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Font;
2019-09-06 12:20:53 +02:00
using MLEM.Formatting;
using MLEM.Misc;
2019-08-13 23:54:29 +02:00
using MLEM.Textures;
2019-08-10 21:37:10 +02:00
using MLEM.Ui.Style;
2019-08-09 19:28:48 +02:00
namespace MLEM.Ui.Elements {
public class Paragraph : Element {
private string text;
private string splitText;
2020-05-15 19:55:59 +02:00
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public FormattingCodeCollection Formatting;
2020-03-28 22:25:06 +01:00
public StyleProp<GenericFont> RegularFont;
public StyleProp<GenericFont> BoldFont;
public StyleProp<GenericFont> ItalicFont;
2020-05-15 19:55:59 +02:00
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public StyleProp<FormatSettings> FormatSettings;
2020-05-15 19:55:59 +02:00
public readonly TextFormatter Formatter;
public TokenizedString TokenizedText { get; private set; }
public StyleProp<Color> TextColor;
public StyleProp<float> TextScale;
2019-08-09 19:28:48 +02:00
public string Text {
get => this.text;
set {
2019-08-16 19:08:36 +02:00
if (this.text != value) {
this.text = value;
2019-09-13 11:53:28 +02:00
this.IsHidden = string.IsNullOrWhiteSpace(this.text);
2019-08-16 19:08:36 +02:00
this.SetAreaDirty();
}
2019-08-09 19:28:48 +02:00
}
}
public bool AutoAdjustWidth;
2019-08-16 19:08:36 +02:00
public TextCallback GetTextCallback;
2020-05-15 19:55:59 +02:00
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public TextModifier RenderedTextModifier = text => text;
public TimeSpan TimeIntoAnimation;
2019-08-16 19:08:36 +02:00
2019-08-24 00:07:54 +02:00
public Paragraph(Anchor anchor, float width, TextCallback textCallback, bool centerText = false)
: this(anchor, width, "", centerText) {
2019-08-16 19:08:36 +02:00
this.GetTextCallback = textCallback;
this.Text = textCallback(this);
if (this.Text == null)
this.IsHidden = true;
2019-08-16 19:08:36 +02:00
}
2019-08-09 19:28:48 +02:00
2019-08-24 00:07:54 +02:00
public Paragraph(Anchor anchor, float width, string text, bool centerText = false) : base(anchor, new Vector2(width, 0)) {
this.Text = text;
if (this.Text == null)
this.IsHidden = true;
2019-08-24 00:07:54 +02:00
this.AutoAdjustWidth = centerText;
2019-08-28 18:27:17 +02:00
this.CanBeSelected = false;
this.CanBeMoused = false;
2020-05-15 19:55:59 +02:00
this.Formatter = new TextFormatter(() => this.BoldFont, () => this.ItalicFont);
2019-08-09 19:28:48 +02:00
}
protected override Vector2 CalcActualSize(RectangleF parentArea) {
2019-08-09 19:28:48 +02:00
var size = base.CalcActualSize(parentArea);
2019-08-24 00:07:54 +02:00
var sc = this.TextScale * this.Scale;
2020-05-15 19:55:59 +02:00
// old formatting stuff
this.splitText = this.RegularFont.Value.SplitString(this.text.RemoveFormatting(this.RegularFont.Value), size.X - this.ScaledPadding.Width, sc);
this.Formatting = this.text.GetFormattingCodes(this.RegularFont.Value);
2020-05-15 19:55:59 +02:00
if (this.Formatting.Count > 0) {
var textDims = this.RegularFont.Value.MeasureString(this.splitText) * sc;
return new Vector2(this.AutoAdjustWidth ? textDims.X + this.ScaledPadding.Width : size.X, textDims.Y + this.ScaledPadding.Height);
}
2019-08-09 19:28:48 +02:00
2020-05-15 19:55:59 +02:00
this.TokenizedText = this.Formatter.Tokenize(this.RegularFont, this.text);
this.TokenizedText.Split(this.RegularFont, size.X - this.ScaledPadding.Width, sc);
var dims = this.TokenizedText.Measure(this.RegularFont) * sc;
return new Vector2(this.AutoAdjustWidth ? dims.X + this.ScaledPadding.Width : size.X, dims.Y + this.ScaledPadding.Height);
2019-08-09 19:28:48 +02:00
}
public override void ForceUpdateArea() {
if (this.GetTextCallback != null)
this.Text = this.GetTextCallback(this);
base.ForceUpdateArea();
}
2019-08-16 19:08:36 +02:00
public override void Update(GameTime time) {
base.Update(time);
if (this.GetTextCallback != null)
this.Text = this.GetTextCallback(this);
this.TimeIntoAnimation += time.ElapsedGameTime;
2020-05-15 19:55:59 +02:00
this.TokenizedText?.Update(time);
2019-08-16 19:08:36 +02:00
}
public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
var pos = this.DisplayArea.Location;
2019-08-24 00:07:54 +02:00
var sc = this.TextScale * this.Scale;
2019-11-05 13:28:41 +01:00
var color = this.TextColor.OrDefault(Color.White) * alpha;
2020-05-15 19:55:59 +02:00
// legacy formatting stuff
if (this.Formatting.Count > 0) {
var toRender = this.RenderedTextModifier(this.splitText);
this.RegularFont.Value.DrawFormattedString(batch, pos, toRender, this.Formatting, color, sc, this.BoldFont.Value, this.ItalicFont.Value, 0, this.TimeIntoAnimation, this.FormatSettings);
2020-05-15 19:55:59 +02:00
} else {
this.TokenizedText.Draw(time, batch, pos, this.RegularFont, color, sc, 0);
}
base.Draw(time, batch, alpha, blendState, samplerState, matrix);
2019-08-09 19:28:48 +02:00
}
2019-08-10 21:37:10 +02:00
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.TextScale.SetFromStyle(style.TextScale);
this.RegularFont.SetFromStyle(style.Font);
this.BoldFont.SetFromStyle(style.BoldFont ?? style.Font);
this.ItalicFont.SetFromStyle(style.ItalicFont ?? style.Font);
this.FormatSettings.SetFromStyle(style.FormatSettings);
2019-08-10 21:37:10 +02:00
}
2019-08-16 19:08:36 +02:00
public delegate string TextCallback(Paragraph paragraph);
2020-05-15 19:55:59 +02:00
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public delegate string TextModifier(string text);
2019-08-09 19:28:48 +02:00
}
}