1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-04 22:49:08 +01:00
MLEM/MLEM.Ui/Elements/AutoScaledText.cs

53 lines
1.7 KiB
C#
Raw Normal View History

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Font;
2019-08-10 21:37:10 +02:00
using MLEM.Ui.Style;
namespace MLEM.Ui.Elements {
public class AutoScaledText : Element {
2019-08-10 21:37:10 +02:00
private IGenericFont font;
private float textScale;
private string text;
public string Text {
get => this.text;
set {
this.text = value;
this.SetDirty();
}
}
2019-08-10 18:41:56 +02:00
public Color Color = Color.White;
2019-08-10 18:41:56 +02:00
public AutoScaledText(Anchor anchor, Vector2 size, string text, IGenericFont font = null) : base(anchor, size) {
this.Text = text;
2019-08-10 21:37:10 +02:00
this.font = font;
2019-08-10 18:41:56 +02:00
this.IgnoresMouse = true;
}
public override void ForceUpdateArea() {
base.ForceUpdateArea();
this.textScale = 0;
Vector2 measure;
do {
this.textScale += 0.1F;
measure = this.font.MeasureString(this.Text) * this.textScale;
} while (measure.X <= this.DisplayArea.Size.X / this.Scale && measure.Y <= this.DisplayArea.Size.Y / this.Scale);
}
2019-08-10 13:42:18 +02:00
public override void Draw(GameTime time, SpriteBatch batch, float alpha) {
var pos = this.DisplayArea.Location.ToVector2() + this.DisplayArea.Size.ToVector2() / 2;
this.font.DrawCenteredString(batch, this.Text, pos, this.textScale * this.Scale, this.Color * alpha, true, true);
2019-08-10 13:42:18 +02:00
base.Draw(time, batch, alpha);
}
2019-08-10 21:37:10 +02:00
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.font = style.Font;
}
}
}