1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-29 03:23:37 +02:00
MLEM/MLEM.Ui/Elements/Paragraph.cs

85 lines
3.2 KiB
C#
Raw Normal View History

2019-08-09 19:28:48 +02:00
using System;
using System.Collections.Generic;
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-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 float lineHeight;
2019-08-13 23:54:29 +02:00
private float longestLineLength;
2019-08-09 19:28:48 +02:00
private string[] splitText;
2019-08-10 21:37:10 +02:00
private IGenericFont font;
2019-08-09 19:28:48 +02:00
private readonly bool centerText;
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 {
this.text = value;
2019-08-12 14:44:42 +02:00
this.SetAreaDirty();
2019-08-09 19:28:48 +02:00
}
}
2019-08-10 18:41:56 +02:00
public Paragraph(Anchor anchor, float width, string text, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) {
2019-08-09 19:28:48 +02:00
this.text = text;
2019-08-10 21:37:10 +02:00
this.font = font;
2019-08-09 19:28:48 +02:00
this.centerText = centerText;
this.IgnoresMouse = true;
2019-08-09 19:28:48 +02:00
}
protected override Point CalcActualSize(Rectangle parentArea) {
var size = base.CalcActualSize(parentArea);
this.splitText = this.font.SplitString(this.text, size.X, this.TextScale * this.Scale).ToArray();
2019-08-09 19:28:48 +02:00
this.lineHeight = 0;
2019-08-13 23:54:29 +02:00
this.longestLineLength = 0;
2019-08-09 19:28:48 +02:00
var height = 0F;
foreach (var strg in this.splitText) {
2019-08-13 23:54:29 +02:00
var strgScale = this.font.MeasureString(strg) * this.TextScale * this.Scale;
height += strgScale.Y + 1;
if (strgScale.Y > this.lineHeight)
this.lineHeight = strgScale.Y;
if (strgScale.X > this.longestLineLength)
this.longestLineLength = strgScale.X;
2019-08-09 19:28:48 +02:00
}
return new Point(size.X, height.Ceil());
}
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) {
2019-08-13 23:54:29 +02:00
if (this.Background != null) {
var backgroundArea = new Rectangle(this.Area.X + offset.X, this.Area.Y + offset.Y, this.longestLineLength.Ceil() + this.ScaledPadding.X * 2, this.Area.Height + this.ScaledPadding.Y * 2);
batch.Draw(this.Background, backgroundArea, this.BackgroundColor * alpha);
}
2019-08-09 19:28:48 +02:00
var pos = this.DisplayArea.Location.ToVector2();
var off = offset.ToVector2();
2019-08-09 19:28:48 +02:00
foreach (var line in this.splitText) {
if (this.centerText) {
2019-08-13 23:54:29 +02:00
this.font.DrawCenteredString(batch, line, pos + off + new Vector2(this.DisplayArea.Width / 2, 0), this.TextScale * this.Scale, this.TextColor * alpha);
2019-08-09 19:28:48 +02:00
} else {
2019-08-13 23:54:29 +02:00
this.font.DrawString(batch, line, pos + off, this.TextColor * alpha, 0, Vector2.Zero, this.TextScale * this.Scale, SpriteEffects.None, 0);
2019-08-09 19:28:48 +02:00
}
off.Y += this.lineHeight + 1;
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;
this.font = style.Font;
}
2019-08-09 19:28:48 +02:00
}
}