UI helper that renders a "value pill" texture for a hand (e.g. Blackjack total or Poker category). It builds a Bitmap with a rounded black background and centered text, caches the resulting Texture per string, and exposes an Aspect constant for sizing.
using System.Collections.Generic;
namespace CardGames;
/// <summary>
/// Builds the little "value pill" texture drawn beside a hand (a Blackjack total, a Poker hand category, …).
/// Same Bitmap → Texture approach as <see cref="SlotRenderer"/>; cached per text so it's only rebuilt on change.
/// </summary>
public static class HandValueRenderer
{
// Reset on hotload so art tweaks show live (consistent with SlotRenderer / GameSettings).
[Sandbox.SkipHotload]
private static readonly Dictionary<string, Texture> Cache = new();
private const TextFlag Centered = TextFlag.Center | TextFlag.DontClip;
/// <summary>Build (or fetch) the value-pill texture for <paramref name="text"/>.</summary>
public static Texture BuildLabel( DeckDefinition deck, string text )
{
text ??= "";
if ( Cache.TryGetValue( text, out var cached ) && cached.IsLoaded )
return cached;
int h = 160, w = 512; // a wide pill; the Decal scales it to world size
var area = new Rect( 0, 0, w, h );
var bmp = new Bitmap( w, h );
bmp.Clear( Color.Transparent );
// A solid black pill with white text.
var bg = new Color( 0f, 0f, 0f, 1f );
bmp.SetFill( bg );
bmp.SetPen( bg, h * 0.04f );
bmp.DrawRoundRect( Inset( area, h * 0.12f ), new Margin( h * 0.5f ) );
// Shrink the font for longer text so categories like "Three of a Kind" still fit the pill.
float size = h * 0.5f;
if ( text.Length > 7 )
size *= 7f / text.Length;
// Nudge the text up a touch so it sits optically centred in the pill.
var textRect = new Rect( area.Left, area.Top - h * 0.05f, area.Width, area.Height );
bmp.DrawText( new TextRendering.Scope( text, Color.Black, size, deck.Font, 600 ), textRect, Centered );
var tex = bmp.ToTexture();
Cache[text] = tex;
return tex;
}
private static Rect Inset( Rect r, float by ) => new( r.Left + by, r.Top + by, r.Width - by * 2, r.Height - by * 2 );
/// <summary>Aspect (width / height) of the pill, so the decal's world size keeps the texture undistorted.</summary>
public static float Aspect => 512f / 160f;
}