1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-29 11:33:37 +02:00
MLEM/MLEM.Ui/Elements/Paragraph.cs
2019-08-15 16:29:41 +02:00

82 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Font;
using MLEM.Textures;
using MLEM.Ui.Style;
namespace MLEM.Ui.Elements {
public class Paragraph : Element {
private string text;
private float lineHeight;
private float longestLineLength;
private string[] splitText;
private IGenericFont font;
private readonly bool centerText;
public NinePatch Background;
public Color BackgroundColor;
public Color TextColor = Color.White;
public float TextScale;
public string Text {
get => this.text;
set {
this.text = value;
this.SetAreaDirty();
}
}
public bool AutoAdjustWidth;
public Paragraph(Anchor anchor, float width, string text, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) {
this.text = text;
this.font = font;
this.centerText = centerText;
this.IgnoresMouse = true;
}
protected override Point CalcActualSize(Rectangle parentArea) {
var size = base.CalcActualSize(parentArea);
this.splitText = this.font.SplitString(this.text, size.X - this.ScaledPadding.X * 2, this.TextScale * this.Scale).ToArray();
this.lineHeight = 0;
this.longestLineLength = 0;
foreach (var strg in this.splitText) {
var strgScale = this.font.MeasureString(strg) * this.TextScale * this.Scale;
if (strgScale.Y + 1 > this.lineHeight)
this.lineHeight = strgScale.Y + 1;
if (strgScale.X > this.longestLineLength)
this.longestLineLength = strgScale.X;
}
return new Point(this.AutoAdjustWidth ? this.longestLineLength.Ceil() + this.ScaledPadding.X * 2 : size.X, (this.lineHeight * this.splitText.Length).Ceil() + this.ScaledPadding.Y * 2);
}
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);
var pos = this.DisplayArea.Location.ToVector2();
var off = offset.ToVector2();
foreach (var line in this.splitText) {
if (this.centerText) {
this.font.DrawCenteredString(batch, line, pos + off + new Vector2(this.DisplayArea.Width / 2, 0), this.TextScale * this.Scale, this.TextColor * alpha);
} else {
this.font.DrawString(batch, line, pos + off, this.TextColor * alpha, 0, Vector2.Zero, this.TextScale * this.Scale, SpriteEffects.None, 0);
}
off.Y += this.lineHeight;
}
base.Draw(time, batch, alpha, offset);
}
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.TextScale = style.TextScale;
this.font = style.Font;
}
}
}