2019-08-09 22:04:26 +02:00
|
|
|
using System;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using MLEM.Extensions;
|
|
|
|
using MLEM.Font;
|
|
|
|
|
|
|
|
namespace MLEM.Ui.Elements {
|
|
|
|
public class AutoScaledText : Element {
|
|
|
|
|
|
|
|
private readonly IGenericFont font;
|
|
|
|
private float scale;
|
|
|
|
private string text;
|
|
|
|
|
|
|
|
public string Text {
|
|
|
|
get => this.text;
|
|
|
|
set {
|
|
|
|
this.text = value;
|
|
|
|
this.SetDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public Color Color;
|
|
|
|
|
2019-08-10 13:28:58 +02:00
|
|
|
public AutoScaledText(Anchor anchor, Vector2 size, string text, Color? color = null, IGenericFont font = null) : base(anchor, size) {
|
2019-08-09 22:04:26 +02:00
|
|
|
this.Text = text;
|
2019-08-10 13:28:58 +02:00
|
|
|
this.font = font ?? Paragraph.DefaultFont;
|
2019-08-09 22:04:26 +02:00
|
|
|
this.Color = color ?? Color.White;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void ForceUpdateArea() {
|
|
|
|
base.ForceUpdateArea();
|
|
|
|
|
|
|
|
this.scale = 0;
|
|
|
|
Vector2 measure;
|
|
|
|
do {
|
|
|
|
this.scale += 0.1F;
|
2019-08-10 13:28:58 +02:00
|
|
|
measure = this.font.MeasureString(this.Text) * this.scale;
|
2019-08-09 22:04:26 +02:00
|
|
|
} while (measure.X <= this.DisplayArea.Size.X && measure.Y <= this.DisplayArea.Size.Y);
|
|
|
|
}
|
|
|
|
|
2019-08-10 13:42:18 +02:00
|
|
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha) {
|
2019-08-09 22:04:26 +02:00
|
|
|
var pos = this.DisplayArea.Location.ToVector2() + this.DisplayArea.Size.ToVector2() / 2;
|
2019-08-10 13:42:18 +02:00
|
|
|
this.font.DrawCenteredString(batch, this.Text, pos, this.scale, this.Color * alpha, true, true);
|
|
|
|
base.Draw(time, batch, alpha);
|
2019-08-09 22:04:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|