2026-03-09 20:05:21 +01:00
|
|
|
using System;
|
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using MonoGameLibrary.Graphics;
|
|
|
|
|
|
|
|
|
|
namespace FNAF_Clone.GUI;
|
|
|
|
|
|
|
|
|
|
public class TextUIElement : UIElement {
|
|
|
|
|
public SpriteFont Font { get; set; }
|
2026-03-11 22:35:30 +01:00
|
|
|
public Alignment CurrentAlignment { get; set; }
|
|
|
|
|
|
|
|
|
|
public string Text{
|
|
|
|
|
get;
|
|
|
|
|
set{
|
|
|
|
|
field = value;
|
|
|
|
|
if(AutoBounds)
|
|
|
|
|
Bounds = (Bounds.Item1, Bounds.Item1 + new Point((int)Measure().X, (int)Measure().Y));
|
|
|
|
|
}
|
|
|
|
|
} = "";
|
|
|
|
|
|
2026-03-09 20:05:21 +01:00
|
|
|
public Color Color{ get; set; } = Color.White;
|
2026-03-11 22:35:30 +01:00
|
|
|
public bool AutoBounds{ get; protected set; } = false;
|
2026-03-09 20:05:21 +01:00
|
|
|
private Vector2 origin;
|
2026-03-11 22:35:30 +01:00
|
|
|
private const float UNIVERSAL_TEXT_SCALE_MULTIPLIER = 0.3f;
|
2026-03-09 20:05:21 +01:00
|
|
|
|
2026-03-11 22:35:30 +01:00
|
|
|
public TextUIElement(Point corner1, SpriteFont font, Alignment alignment = Alignment.LEFT, bool autoBounds = true) : base(corner1, corner1) {
|
|
|
|
|
Font = font;
|
|
|
|
|
AutoBounds = autoBounds;
|
|
|
|
|
Align(alignment);
|
|
|
|
|
}
|
|
|
|
|
public TextUIElement(Point corner1, Point corner2, SpriteFont font, Alignment alignment = Alignment.LEFT) : base(corner1, corner2) {
|
2026-03-09 20:05:21 +01:00
|
|
|
Font = font;
|
|
|
|
|
Align(alignment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Draw(SpriteBatch spriteBatch) {
|
|
|
|
|
base.Draw(spriteBatch);
|
|
|
|
|
align();
|
2026-03-11 22:35:30 +01:00
|
|
|
spriteBatch.DrawString(Font, Text, screenSpaceBounds.Item1.ToVector2(), Color, 0, origin, pixelScaleMultiplier * UNIVERSAL_TEXT_SCALE_MULTIPLIER, SpriteEffects.None, 0);
|
2026-03-09 20:05:21 +01:00
|
|
|
}
|
2026-03-11 22:35:30 +01:00
|
|
|
|
2026-03-09 20:05:21 +01:00
|
|
|
public void Align(Alignment alignment) {
|
2026-03-11 22:35:30 +01:00
|
|
|
CurrentAlignment = alignment;
|
2026-03-09 20:05:21 +01:00
|
|
|
switch (alignment){
|
|
|
|
|
case Alignment.LEFT:
|
|
|
|
|
AlignLeft();
|
|
|
|
|
break;
|
|
|
|
|
case Alignment.RIGHT:
|
|
|
|
|
AlignRight();
|
|
|
|
|
break;
|
|
|
|
|
case Alignment.CENTER:
|
|
|
|
|
AlignCenter();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-11 22:35:30 +01:00
|
|
|
|
|
|
|
|
protected override void UpdateBounds() {
|
|
|
|
|
Point inSpaceOrigin = Bounds.Item1 + origin.ToPoint();
|
|
|
|
|
_bounds = ((Bounds.Item1 - inSpaceOrigin).MultiplyByScalar(ScaleMultiplier) + inSpaceOrigin, (Bounds.Item2 - Bounds.Item1).MultiplyByScalar(ScaleMultiplier) + inSpaceOrigin);
|
|
|
|
|
screenSpaceBounds = (Bounds.Item1.MultiplyByScalar(pixelScaleMultiplier), Bounds.Item2.MultiplyByScalar(pixelScaleMultiplier));
|
|
|
|
|
Align(CurrentAlignment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector2 Measure() => Font.MeasureString(Text) * UNIVERSAL_TEXT_SCALE_MULTIPLIER * ScaleMultiplier;
|
2026-03-09 20:05:21 +01:00
|
|
|
|
|
|
|
|
private void AlignLeft() {
|
|
|
|
|
align = () => origin = Vector2.Zero;
|
|
|
|
|
}
|
|
|
|
|
private void AlignRight() {
|
2026-03-11 22:35:30 +01:00
|
|
|
align = () => origin = Font.MeasureString(Text) * ScaleMultiplier;
|
2026-03-09 20:05:21 +01:00
|
|
|
}
|
|
|
|
|
private void AlignCenter() {
|
2026-03-11 22:35:30 +01:00
|
|
|
align = () => origin = new(Font.MeasureString(Text).X * ScaleMultiplier / 2, 0);
|
2026-03-09 20:05:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Action align;
|
|
|
|
|
|
|
|
|
|
public enum Alignment {
|
|
|
|
|
LEFT, RIGHT, CENTER
|
|
|
|
|
}
|
|
|
|
|
}
|