Transposer/Entities/TextDisplay.cs
namespace Sandbox.Transposer;
/// <summary>
/// Text renderer that uses font animation data to draw strings.
/// Each character is a named animation in the font sprite type (e.g. "a", "1", "+").
/// Supports scale, colour override with alpha, and newlines.
///
/// Original font is 6×9 pixels per glyph with 1px spacing.
/// </summary>
public class TextDisplay : Entity
{
protected string _text;
public string Text { set => _text = value; }
protected Color _color;
public Color TextColor { set => _color = value; }
protected int _letterWidth;
protected int _letterHeight;
protected string _fontName;
protected int _spacing = 1;
protected int _scale;
public TextDisplay( string text, TransposerScene scene, int x, int y, string fontName, Color color, int scale = 1 )
{
_text = text;
_scene = scene;
_fontName = fontName;
_type = _fontName;
_collideable = false;
PixelPoint size = GetAnimSize( _type, "a" );
_letterWidth = size.X;
_letterHeight = size.Y;
PixelX = x;
PixelY = y;
_color = color;
SetOverriddenColor( _color );
SetOverrideAlpha( true );
_depth = Globals.DEPTH_TEXT;
_scale = scale;
_hitbox = new PixelRect( 0, 0, ((_letterWidth + 1) * text.Length - 1) * _scale, _letterHeight * _scale );
}
public override void Draw()
{
base.Draw();
int currentX = PixelX;
int currentY = PixelY;
for ( int i = 0; i < _text.Length; i++ )
{
char c = _text[i];
if ( c == '\n' )
{
currentY -= _letterHeight * _scale;
currentX = PixelX;
continue;
}
// Space — just advance without drawing.
if ( c == ' ' )
{
currentX += (_letterWidth + _spacing) * _scale;
continue;
}
List<PixelData> pixelDataList = GetPixelDataList( _fontName, c.ToString() );
if ( pixelDataList is not null )
DrawPixels( pixelDataList, currentX, currentY, _scale );
currentX += (_letterWidth + _spacing) * _scale;
}
}
}