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 ;
2020-05-15 22:15:24 +02:00
using System.Text.RegularExpressions ;
2019-08-09 19:28:48 +02:00
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 ;
2020-05-15 22:15:24 +02:00
using MLEM.Formatting.Codes ;
using MLEM.Input ;
2019-11-02 14:53:59 +01:00
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 {
2020-05-22 17:02:24 +02:00
/// <summary>
/// A paragraph element for use inside of a <see cref="UiSystem"/>.
/// A paragraph is an element that contains text.
/// A paragraph's text can be formatted using the ui system's <see cref="UiSystem.TextFormatter"/>.
/// </summary>
2019-08-09 19:28:48 +02:00
public class Paragraph : Element {
private string text ;
2019-09-08 16:25:59 +02:00
private string splitText ;
2020-05-15 19:55:59 +02:00
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
2020-02-03 15:38:27 +01:00
public FormattingCodeCollection Formatting ;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The font that this paragraph draws text with.
/// To set its bold and italic font, use <see cref="GenericFont.Bold"/> and <see cref="GenericFont.Italic"/>.
/// </summary>
2020-03-28 22:25:06 +01:00
public StyleProp < GenericFont > RegularFont ;
2020-05-17 00:10:29 +02:00
[Obsolete("Use the new GenericFont.Bold and GenericFont.Italic instead")]
2020-03-28 22:25:06 +01:00
public StyleProp < GenericFont > BoldFont ;
2020-05-17 00:10:29 +02:00
[Obsolete("Use the new GenericFont.Bold and GenericFont.Italic instead")]
2020-03-28 22:25:06 +01:00
public StyleProp < GenericFont > ItalicFont ;
2020-05-15 19:55:59 +02:00
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
2019-12-26 19:30:17 +01:00
public StyleProp < FormatSettings > FormatSettings ;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The tokenized version of the <see cref="Text"/>
/// </summary>
2020-05-15 19:55:59 +02:00
public TokenizedString TokenizedText { get ; private set ; }
2019-10-14 21:28:12 +02:00
2020-05-22 17:02:24 +02:00
/// <summary>
/// The color that the text will be rendered with
/// </summary>
2019-10-14 21:28:12 +02:00
public StyleProp < Color > TextColor ;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The scale that the text will be rendered with
/// </summary>
2019-10-14 21:28:12 +02:00
public StyleProp < float > TextScale ;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The text to render inside of this paragraph.
/// Use <see cref="GetTextCallback"/> if the text changes frequently.
/// </summary>
2019-08-09 19:28:48 +02:00
public string Text {
2020-05-17 00:59:15 +02:00
get {
this . QueryTextCallback ( ) ;
return this . text ;
}
2019-08-09 19:28:48 +02:00
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 ( ) ;
2020-05-17 00:59:15 +02:00
// force text to be re-tokenized
2020-05-17 00:10:29 +02:00
this . TokenizedText = null ;
2019-08-16 19:08:36 +02:00
}
2019-08-09 19:28:48 +02:00
}
}
2020-05-22 17:02:24 +02:00
/// <summary>
/// If this paragraph should automatically adjust its width based on the width of the text within it
/// </summary>
2019-08-15 14:59:15 +02:00
public bool AutoAdjustWidth ;
2020-05-22 17:02:24 +02:00
/// <summary>
/// An event that gets called when this paragraph's <see cref="Text"/> is queried.
/// Use this event for setting this paragraph's text if it changes frequently.
/// </summary>
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")]
2020-02-03 23:42:30 +01:00
public TextModifier RenderedTextModifier = text = > text ;
2020-05-15 22:15:24 +02:00
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
2020-02-03 04:37:14 +01:00
public TimeSpan TimeIntoAnimation ;
2019-08-16 19:08:36 +02:00
2020-05-22 17:02:24 +02:00
/// <summary>
/// Creates a new paragraph with the given settings.
/// </summary>
/// <param name="anchor">The paragraph's anchor</param>
/// <param name="width">The paragraph's width. Note that its height is automatically calculated.</param>
/// <param name="textCallback">The paragraph's text</param>
/// <param name="centerText">Whether the paragraph's width should automatically be calculated based on the text within it.</param>
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 ) ;
2019-09-26 19:35:22 +02:00
if ( this . Text = = null )
this . IsHidden = true ;
2019-08-16 19:08:36 +02:00
}
2019-08-09 19:28:48 +02:00
2020-05-22 17:02:24 +02:00
/// <inheritdoc cref="Paragraph(Anchor,float,TextCallback,bool)"/>
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 ) ) {
2019-09-26 19:35:22 +02:00
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 ;
2019-08-09 19:28:48 +02:00
}
2020-05-22 17:02:24 +02:00
/// <inheritdoc />
2019-11-02 14:53:59 +01:00
protected override Vector2 CalcActualSize ( RectangleF parentArea ) {
2019-08-09 19:28:48 +02:00
var size = base . CalcActualSize ( parentArea ) ;
2020-05-17 00:59:15 +02:00
this . ParseText ( size ) ;
2020-05-15 19:55:59 +02:00
// old formatting stuff
if ( this . Formatting . Count > 0 ) {
2020-05-17 00:59:15 +02:00
var textDims = this . RegularFont . Value . MeasureString ( this . splitText ) * this . TextScale * this . Scale ;
2020-05-15 19:55:59 +02:00
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-17 00:59:15 +02:00
var dims = this . TokenizedText . Measure ( this . RegularFont ) * this . TextScale * this . Scale ;
2020-05-15 19:55:59 +02:00
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
}
2020-05-22 17:02:24 +02:00
/// <inheritdoc />
2019-08-16 19:08:36 +02:00
public override void Update ( GameTime time ) {
2020-05-17 00:59:15 +02:00
this . QueryTextCallback ( ) ;
2019-08-16 19:08:36 +02:00
base . Update ( time ) ;
2020-05-19 21:52:29 +02:00
2020-02-03 04:37:14 +01:00
this . TimeIntoAnimation + = time . ElapsedGameTime ;
2020-05-15 22:15:24 +02:00
2020-05-17 00:10:29 +02:00
if ( this . TokenizedText ! = null )
2020-05-15 22:15:24 +02:00
this . TokenizedText . Update ( time ) ;
2019-08-16 19:08:36 +02:00
}
2020-05-22 17:02:24 +02:00
/// <inheritdoc />
2019-09-20 13:22:05 +02:00
public override void Draw ( GameTime time , SpriteBatch batch , float alpha , BlendState blendState , SamplerState samplerState , Matrix matrix ) {
2019-11-02 14:53:59 +01:00
var pos = this . DisplayArea . Location ;
2019-08-24 00:07:54 +02:00
var sc = this . TextScale * this . Scale ;
2019-09-08 16:25:59 +02:00
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 ) ;
2020-02-03 23:42:30 +01:00
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 ) ;
2019-09-08 16:25:59 +02:00
}
2019-09-20 13:22:05 +02:00
base . Draw ( time , batch , alpha , blendState , samplerState , matrix ) ;
2019-08-09 19:28:48 +02:00
}
2020-05-22 17:02:24 +02:00
/// <inheritdoc />
2019-08-10 21:37:10 +02:00
protected override void InitStyle ( UiStyle style ) {
base . InitStyle ( style ) ;
2019-10-14 21:28:12 +02:00
this . TextScale . SetFromStyle ( style . TextScale ) ;
this . RegularFont . SetFromStyle ( style . Font ) ;
this . BoldFont . SetFromStyle ( style . BoldFont ? ? style . Font ) ;
this . ItalicFont . SetFromStyle ( style . ItalicFont ? ? style . Font ) ;
2019-12-26 19:34:42 +01:00
this . FormatSettings . SetFromStyle ( style . FormatSettings ) ;
2019-08-10 21:37:10 +02:00
}
2020-05-22 17:02:24 +02:00
/// <summary>
/// Parses this paragraph's <see cref="Text"/> into <see cref="TokenizedText"/>.
/// Additionally, this method adds any <see cref="Link"/> elements for tokenized links in the text.
/// </summary>
/// <param name="size">The paragraph's default size</param>
2020-05-17 00:59:15 +02:00
protected virtual void ParseText ( Vector2 size ) {
// old formatting stuff
this . splitText = this . RegularFont . Value . SplitString ( this . Text . RemoveFormatting ( this . RegularFont . Value ) , size . X - this . ScaledPadding . Width , this . TextScale * this . Scale ) ;
this . Formatting = this . Text . GetFormattingCodes ( this . RegularFont . Value ) ;
if ( this . TokenizedText = = null )
this . TokenizedText = this . System . TextFormatter . Tokenize ( this . RegularFont , this . Text ) ;
this . TokenizedText . Split ( this . RegularFont , size . X - this . ScaledPadding . Width , this . TextScale * this . Scale ) ;
var linkTokens = this . TokenizedText . Tokens . Where ( t = > t . AppliedCodes . Any ( c = > c is LinkCode ) ) . ToArray ( ) ;
// this basically checks if there are any tokens that have an area that doesn't have a link element associated with it
if ( linkTokens . Any ( t = > ! t . GetArea ( Vector2 . Zero , this . TextScale ) . All ( a = > this . GetChildren < Link > ( c = > c . PositionOffset = = a . Location & & c . Size = = a . Size ) . Any ( ) ) ) ) {
this . RemoveChildren ( c = > c is Link ) ;
foreach ( var link in linkTokens ) {
var areas = link . GetArea ( Vector2 . Zero , this . TextScale ) . ToArray ( ) ;
2020-05-19 21:52:29 +02:00
var cluster = new Link [ areas . Length ] ;
2020-05-17 00:59:15 +02:00
for ( var i = 0 ; i < areas . Length ; i + + ) {
var area = areas [ i ] ;
2020-05-19 21:52:29 +02:00
cluster [ i ] = this . AddChild ( new Link ( Anchor . TopLeft , link , area . Size , cluster ) {
2020-05-17 00:59:15 +02:00
PositionOffset = area . Location ,
// only allow selecting the first part of a link
CanBeSelected = i = = 0
} ) ;
}
}
}
}
private void QueryTextCallback ( ) {
if ( this . GetTextCallback ! = null )
this . Text = this . GetTextCallback ( this ) ;
}
2020-05-22 17:02:24 +02:00
/// <summary>
/// A delegate method used for <see cref="Paragraph.GetTextCallback"/>
/// </summary>
/// <param name="paragraph">The current paragraph</param>
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")]
2020-02-03 23:42:30 +01:00
public delegate string TextModifier ( string text ) ;
2020-05-22 17:02:24 +02:00
/// <summary>
/// A link is a sub-element of the <see cref="Paragraph"/> that is added onto it as a child for any tokens that contain <see cref="LinkCode"/>, to make them selectable and clickable.
/// </summary>
2020-05-17 00:10:29 +02:00
public class Link : Element {
2020-05-22 17:02:24 +02:00
/// <summary>
/// The token that this link represents
/// </summary>
2020-05-17 00:10:29 +02:00
public readonly Token Token ;
2020-05-22 17:02:24 +02:00
/// <summary>
/// The links that form a cluster for the given token.
/// This only contains more than one element if the tokenized string has previously been <see cref="TokenizedString.Split"/>.
/// </summary>
2020-05-19 21:52:29 +02:00
public readonly Link [ ] LinkCluster ;
2020-05-17 00:10:29 +02:00
2020-05-22 17:02:24 +02:00
/// <summary>
/// Creates a new link element with the given settings
/// </summary>
/// <param name="anchor">The link's anchor</param>
/// <param name="token">The token that this link represents</param>
/// <param name="size">The size of the token</param>
/// <param name="linkCluster">The links that form a cluster for the given token. This only contains more than one element if the tokenized string has previously been split.</param>
2020-05-19 21:52:29 +02:00
public Link ( Anchor anchor , Token token , Vector2 size , Link [ ] linkCluster ) : base ( anchor , size ) {
2020-05-17 00:10:29 +02:00
this . Token = token ;
2020-05-19 21:52:29 +02:00
this . LinkCluster = linkCluster ;
2020-05-27 15:19:17 +02:00
this . OnSelected + = e = > {
foreach ( var link in this . LinkCluster )
link . IsSelected = true ;
} ;
this . OnDeselected + = e = > {
foreach ( var link in this . LinkCluster )
link . IsSelected = false ;
} ;
2020-05-17 00:10:29 +02:00
this . OnPressed + = e = > {
2020-06-09 18:56:01 +02:00
foreach ( var code in token . AppliedCodes . OfType < LinkCode > ( ) )
this . System ? . LinkBehavior ? . Invoke ( code ) ;
2020-05-17 00:10:29 +02:00
} ;
}
}
2019-08-09 19:28:48 +02:00
}
}