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;
|
2019-08-10 21:37:10 +02:00
|
|
|
using MLEM.Ui.Style;
|
2019-08-09 22:04:26 +02:00
|
|
|
|
|
|
|
namespace MLEM.Ui.Elements {
|
|
|
|
public class AutoScaledText : Element {
|
|
|
|
|
2019-08-10 21:37:10 +02:00
|
|
|
private IGenericFont font;
|
2019-08-11 21:24:09 +02:00
|
|
|
private float textScale;
|
2019-08-09 22:04:26 +02:00
|
|
|
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-09 22:04:26 +02:00
|
|
|
|
2019-08-10 18:41:56 +02:00
|
|
|
public AutoScaledText(Anchor anchor, Vector2 size, string text, IGenericFont font = null) : base(anchor, size) {
|
2019-08-09 22:04:26 +02:00
|
|
|
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;
|
2019-08-09 22:04:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void ForceUpdateArea() {
|
|
|
|
base.ForceUpdateArea();
|
|
|
|
|
2019-08-11 21:24:09 +02:00
|
|
|
this.textScale = 0;
|
2019-08-09 22:04:26 +02:00
|
|
|
Vector2 measure;
|
|
|
|
do {
|
2019-08-11 21:24:09 +02:00
|
|
|
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-09 22:04:26 +02:00
|
|
|
}
|
|
|
|
|
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-11 21:24:09 +02:00
|
|
|
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-09 22:04:26 +02:00
|
|
|
}
|
|
|
|
|
2019-08-10 21:37:10 +02:00
|
|
|
protected override void InitStyle(UiStyle style) {
|
|
|
|
base.InitStyle(style);
|
|
|
|
this.font = style.Font;
|
|
|
|
}
|
|
|
|
|
2019-08-09 22:04:26 +02:00
|
|
|
}
|
|
|
|
}
|