1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-04 14:39:10 +01:00
MLEM/MLEM.Ui/Elements/Paragraph.cs

100 lines
3.8 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-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 {
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;
public Paragraph(Anchor anchor, float width, TextCallback textCallback, bool centerText = false, IGenericFont font = null)
: this(anchor, width, "", centerText, font) {
this.GetTextCallback = textCallback;
this.Text = textCallback(this);
}
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.ScaledPadding.X * 2, 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
foreach (var strg in this.splitText) {
2019-08-13 23:54:29 +02:00
var strgScale = this.font.MeasureString(strg) * this.TextScale * this.Scale;
2019-08-15 16:29:41 +02:00
if (strgScale.Y + 1 > this.lineHeight)
this.lineHeight = strgScale.Y + 1;
2019-08-13 23:54:29 +02:00
if (strgScale.X > this.longestLineLength)
this.longestLineLength = strgScale.X;
2019-08-09 19:28:48 +02:00
}
2019-08-15 16:29:41 +02:00
return new Point(this.AutoAdjustWidth ? this.longestLineLength.Ceil() + this.ScaledPadding.X * 2 : size.X, (this.lineHeight * this.splitText.Length).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-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
}
2019-08-15 16:29:41 +02:00
off.Y += this.lineHeight;
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-16 19:08:36 +02:00
public delegate string TextCallback(Paragraph paragraph);
2019-08-09 19:28:48 +02:00
}
}