using System;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Font;
using MLEM.Formatting;
using MLEM.Formatting.Codes;
using MLEM.Graphics;
using MLEM.Misc;
using MLEM.Ui.Style;
namespace MLEM.Ui.Elements {
///
/// A paragraph element for use inside of a .
/// A paragraph is an element that contains text.
/// A paragraph's text can be formatted using the ui system's .
///
public class Paragraph : Element {
///
/// The font that this paragraph draws text with.
/// To set its bold and italic font, use and .
///
public StyleProp RegularFont {
get => this.regularFont;
set {
this.regularFont = value;
this.SetTextDirty();
}
}
///
/// The tokenized version of the
///
public TokenizedString TokenizedText { get; private set; }
///
/// The color that the text will be rendered with
///
public StyleProp TextColor;
///
/// The scale that the text will be rendered with.
/// To add a multiplier rather than changing the scale directly, use .
///
public StyleProp TextScale;
///
/// A multiplier that will be applied to .
/// To change the text scale itself, use .
///
public float TextScaleMultiplier = 1;
///
/// The text to render inside of this paragraph.
/// Use if the text changes frequently.
///
public string Text {
get {
this.QueryTextCallback();
return this.text;
}
set {
if (this.text != value) {
this.text = value;
this.SetTextDirty();
var force = string.IsNullOrWhiteSpace(this.text);
if (this.forceHide != force) {
this.forceHide = force;
this.SetAreaDirty();
}
}
}
}
///
/// If this paragraph should automatically adjust its width based on the width of the text within it
///
public bool AutoAdjustWidth;
///
/// Whether this paragraph should be truncated instead of split if the displayed 's width exceeds the provided width.
/// When the string is truncated, the is added to its end.
///
public bool TruncateIfLong;
///
/// The ellipsis characters to use if is enabled and the string is truncated.
/// If this is set to an empty string, no ellipsis will be attached to the truncated string.
///
public string Ellipsis = "...";
///
/// An event that gets called when this paragraph's is queried.
/// Use this event for setting this paragraph's text if it changes frequently.
///
public TextCallback GetTextCallback;
///
/// The action that is executed if objects inside of this paragraph are pressed.
/// By default, is executed.
///
public Action LinkAction;
///
/// The that this paragraph's text should be rendered with
///
public StyleProp Alignment {
get => this.alignment;
set {
this.alignment = value;
this.SetTextDirty();
}
}
///
public override bool IsHidden => base.IsHidden || this.forceHide;
private string text;
private StyleProp alignment;
private StyleProp regularFont;
private bool forceHide = true;
///
/// Creates a new paragraph with the given settings.
///
/// The paragraph's anchor
/// The paragraph's width. Note that its height is automatically calculated.
/// The paragraph's text
/// Whether the paragraph's width should automatically be calculated based on the text within it.
public Paragraph(Anchor anchor, float width, TextCallback textCallback, bool autoAdjustWidth = false) : this(anchor, width, string.Empty, autoAdjustWidth) {
this.GetTextCallback = textCallback;
}
///
public Paragraph(Anchor anchor, float width, string text, bool autoAdjustWidth = false) : base(anchor, new Vector2(width, 0)) {
this.Text = text;
this.AutoAdjustWidth = autoAdjustWidth;
this.CanBeSelected = false;
this.CanBeMoused = false;
}
///
protected override Vector2 CalcActualSize(RectangleF parentArea) {
var size = base.CalcActualSize(parentArea);
this.ParseText(size);
var textSize = this.TokenizedText.GetArea(Vector2.Zero, this.TextScale * this.TextScaleMultiplier * this.Scale).Size;
return new Vector2(this.AutoAdjustWidth ? textSize.X + this.ScaledPadding.Width : size.X, textSize.Y + this.ScaledPadding.Height);
}
///
public override void Update(GameTime time) {
this.QueryTextCallback();
base.Update(time);
if (this.TokenizedText != null)
this.TokenizedText.Update(time);
}
///
public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteBatchContext context) {
var pos = this.DisplayArea.Location + new Vector2(this.GetAlignmentOffset(), 0);
var sc = this.TextScale * this.TextScaleMultiplier * this.Scale;
var color = this.TextColor.OrDefault(Color.White) * alpha;
this.TokenizedText.Draw(time, batch, pos, this.RegularFont, color, sc, 0);
base.Draw(time, batch, alpha, context);
}
///
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.RegularFont = this.RegularFont.OrStyle(style.Font ?? throw new NotSupportedException("Paragraphs cannot use ui styles that don't have a font. Please supply a custom font by setting UiStyle.Font."));
this.TextScale = this.TextScale.OrStyle(style.TextScale);
this.TextColor = this.TextColor.OrStyle(style.TextColor);
this.Alignment = this.Alignment.OrStyle(style.TextAlignment);
}
///
/// Parses this paragraph's into .
/// Additionally, this method adds any elements for tokenized links in the text.
///
/// The paragraph's default size
protected virtual void ParseText(Vector2 size) {
if (this.TokenizedText == null) {
// tokenize the text
this.TokenizedText = this.System.TextFormatter.Tokenize(this.RegularFont, this.Text, this.Alignment);
// add links to the paragraph
this.RemoveChildren(c => c is Link);
foreach (var link in this.TokenizedText.Tokens.Where(t => t.AppliedCodes.Any(c => c is LinkCode)))
this.AddChild(new Link(Anchor.TopLeft, link, this.TextScale * this.TextScaleMultiplier));
}
var width = size.X - this.ScaledPadding.Width;
var scale = this.TextScale * this.TextScaleMultiplier * this.Scale;
if (this.TruncateIfLong) {
this.TokenizedText.Truncate(this.RegularFont, width, scale, this.Ellipsis, this.Alignment);
} else {
this.TokenizedText.Split(this.RegularFont, width, scale, this.Alignment);
}
}
///
/// A helper method that causes the to be reset.
/// Additionally, if this paragraph's area has changed enough to warrant it, or if it has any children.
///
protected void SetTextDirty() {
this.TokenizedText = null;
// only set our area dirty if our size changed as a result of this action
if (!this.AreaDirty && !this.CalcActualSize(this.ParentArea).Equals(this.DisplayArea.Size, Element.Epsilon))
this.SetAreaDirty();
}
private void QueryTextCallback() {
if (this.GetTextCallback != null)
this.Text = this.GetTextCallback(this);
}
private float GetAlignmentOffset() {
switch (this.Alignment.Value) {
case TextAlignment.Center:
return this.DisplayArea.Width / 2;
case TextAlignment.Right:
return this.DisplayArea.Width;
}
return 0;
}
///
/// A delegate method used for
///
/// The current paragraph
public delegate string TextCallback(Paragraph paragraph);
///
/// A link is a sub-element of the that is added onto it as a child for any tokens that contain , to make them selectable and clickable.
///
public class Link : Element {
///
/// The token that this link represents
///
public readonly Token Token;
private readonly float textScale;
///
/// Creates a new link element with the given settings
///
/// The link's anchor
/// The token that this link represents
/// The scale that text is rendered with
public Link(Anchor anchor, Token token, float textScale) : base(anchor, Vector2.Zero) {
this.Token = token;
this.textScale = textScale;
this.OnPressed += e => {
foreach (var code in token.AppliedCodes.OfType()) {
if (this.Parent is Paragraph p && p.LinkAction != null) {
p.LinkAction.Invoke(this, code);
} else {
MlemPlatform.Current.OpenLinkOrFile(code.Match.Groups[1].Value);
}
}
};
}
///
public override void ForceUpdateArea() {
// set the position offset and size to the token's first area
var area = this.Token.GetArea(Vector2.Zero, this.textScale).FirstOrDefault();
if (this.Parent is Paragraph p)
area.Location += new Vector2(p.GetAlignmentOffset() / p.Scale, 0);
this.PositionOffset = area.Location;
this.IsHidden = area.IsEmpty;
this.Size = area.Size;
base.ForceUpdateArea();
}
///
public override Element GetElementUnderPos(Vector2 position) {
var ret = base.GetElementUnderPos(position);
if (ret != null)
return ret;
// check if any of our token's parts are hovered
var location = this.Parent.DisplayArea.Location;
if (this.Parent is Paragraph p)
location.X += p.GetAlignmentOffset();
foreach (var rect in this.Token.GetArea(location, this.Scale * this.textScale)) {
if (rect.Contains(this.TransformInverse(position)))
return this;
}
return null;
}
}
}
}