1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-23 05:08:34 +01:00
MLEM/MLEM.Ui/Elements/Paragraph.cs

135 lines
5.4 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-08-13 23:54:29 +02:00
using MLEM.Textures;
2019-08-24 00:07:54 +02:00
using MLEM.Ui.Format;
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;
2019-08-24 00:07:54 +02:00
private Dictionary<int, FormattingCode> codeLocations;
private IGenericFont regularFont;
private IGenericFont boldFont;
private IGenericFont italicFont;
2019-08-09 19:28:48 +02:00
2019-08-13 23:54:29 +02:00
public NinePatch Background;
public Color BackgroundColor;
public Color TextColor = Color.White;
2019-08-10 21:37:10 +02:00
public 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;
this.SetAreaDirty();
}
2019-08-09 19:28:48 +02:00
}
}
public bool AutoAdjustWidth;
2019-08-16 19:08:36 +02:00
public TextCallback GetTextCallback;
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-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)) {
2019-08-09 19:28:48 +02:00
this.text = text;
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
}
protected override Point CalcActualSize(Rectangle parentArea) {
var size = base.CalcActualSize(parentArea);
2019-08-24 00:07:54 +02:00
var sc = this.TextScale * this.Scale;
this.splitText = this.regularFont.SplitString(this.text.RemoveFormatting(), size.X - this.ScaledPadding.X * 2, sc);
2019-08-24 00:07:54 +02:00
this.codeLocations = this.text.GetFormattingCodes();
2019-08-09 19:28:48 +02:00
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);
2019-08-09 19:28:48 +02:00
}
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);
}
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) {
if (this.Background != null)
batch.Draw(this.Background, this.Area.OffsetCopy(offset), this.BackgroundColor * alpha);
2019-08-13 23:54:29 +02:00
2019-08-09 19:28:48 +02:00
var pos = this.DisplayArea.Location.ToVector2();
var off = offset.ToVector2();
2019-08-24 00:07:54 +02:00
var sc = this.TextScale * this.Scale;
// 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 + off, this.TextColor * alpha, 0, Vector2.Zero, sc, SpriteEffects.None, 0);
2019-08-24 00:07:54 +02:00
} else {
// if we have formatting codes, we need to go through each index and see how it should be drawn
var characterCounter = 0;
var currColor = this.TextColor;
var currFont = this.regularFont;
var innerOffset = new Vector2();
foreach (var c in this.splitText) {
// check if the current character's index has a formatting code
this.codeLocations.TryGetValue(characterCounter, out var code);
if (code != null) {
// if so, apply it
if (code.IsColorCode) {
currColor = code.Color;
} else {
switch (code.Style) {
case TextStyle.Regular:
currFont = this.regularFont;
break;
case TextStyle.Bold:
currFont = this.boldFont;
break;
case TextStyle.Italic:
currFont = this.italicFont;
break;
2019-08-24 00:07:54 +02:00
}
}
}
characterCounter++;
var cSt = c.ToString();
if (c == '\n') {
innerOffset.X = 0;
innerOffset.Y += this.regularFont.LineHeight * sc;
} else {
currFont.DrawString(batch, cSt, pos + off + innerOffset, currColor * alpha, 0, Vector2.Zero, sc, SpriteEffects.None, 0);
innerOffset.X += this.regularFont.MeasureString(cSt).X * sc;
}
2019-08-09 19:28:48 +02:00
}
}
base.Draw(time, batch, alpha, offset);
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 = style.TextScale;
2019-08-24 00:07:54 +02:00
this.regularFont = style.Font;
this.boldFont = style.BoldFont ?? style.Font;
this.italicFont = style.ItalicFont ?? style.Font;
2019-08-10 21:37:10 +02:00
}
2019-08-16 19:08:36 +02:00
public delegate string TextCallback(Paragraph paragraph);
2019-08-09 19:28:48 +02:00
}
}