Editor/HotCodeEditor/Ui/HotCodeLogo.cs

UI code for the HotCode logo and widgets. It renders a layered chili logo as a pixmap (badge on white tile or transparent mark), caches generated pixmaps, and provides widgets for the badge, mark, and wordmark text.

File Access
using Sandbox;
using Editor;
using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// The HotCode chili: a pepper with &lt;/&gt; cut out of it and heat rising off it, in flat layers.
/// The layers come from logo/brand.py (HotCodeLogo.Shapes.cs), which also renders the store images,
/// so the editor and the thumbnails always match.
///
/// Drawn two ways:
/// <list type="bullet">
/// <item><b>Badge</b>: on a white rounded tile (window icon, launcher, welcome page, status bar)</item>
/// <item><b>Mark</b>: just the chili on a transparent background</item>
/// </list>
/// </summary>
public static partial class HotCodeLogo
{
	/// <summary>
	/// Material icon name for places that only take an icon name (EditorApp, menus).
	/// </summary>
	public const string IconName = "local_fire_department";

	public static readonly Color Red = Color.Parse( "#ED3B1E" ) ?? Color.Red;
	public static readonly Color Orange = Color.Parse( "#F77806" ) ?? Color.Orange;

	static readonly (byte, byte, byte) TileBytes = (255, 255, 255);

	// Placement inside the square, matching brand.py's icon
	const float OffsetX = 0.04f;
	const float OffsetY = 0.02f;
	const float Scale = 0.94f;
	const float Corner = 0.2f;

	private static readonly Dictionary<(int Size, bool Tile), Pixmap> Cache = new();
	private static LogoLayer[] _layers;
	private static object _layersSource;

	/// <summary>
	/// The mark rendered at a pixel size, cached.
	/// </summary>
	public static Pixmap Render( int size, bool tile )
	{
		// Hotload carries static fields over, so a cache filled by an older logo would outlive it.
		// Layers is rebuilt with the new code, so a different array means the cache is stale.
		if ( !ReferenceEquals( _layersSource, Layers ) )
		{
			Cache.Clear();
			_layers = null;
			_layersSource = Layers;
		}

		size = Math.Clamp( size, 8, 1024 );
		if ( Cache.TryGetValue( (size, tile), out var cached ) ) return cached;

		_layers ??= Layers
			.Select( l => new LogoLayer( (l.R, l.G, l.B), l.Cut, l.Shapes.Select( s => s.Select( p => (p.x, p.y) ).ToArray() ).ToArray() ) )
			.ToArray();

		var pixels = MarkRasterizer.Render( size, _layers, size * OffsetX, size * OffsetY, size * Scale,
			tile ? TileBytes : null, size * Corner );

		var pixmap = new Pixmap( size, size );
		pixmap.UpdateFromPixels( pixels, size, size, ImageFormat.RGBA8888 );
		Cache[(size, tile)] = pixmap;
		return pixmap;
	}

	/// <summary>
	/// The mark on its white tile, filling <paramref name="rect"/>.
	/// </summary>
	public static void PaintBadge( Rect rect ) => Paint.Draw( rect, Render( PixelSize( rect ), tile: true ) );

	/// <summary>
	/// Just the red shapes, filling <paramref name="rect"/>.
	/// </summary>
	public static void PaintMark( Rect rect ) => Paint.Draw( rect, Render( PixelSize( rect ), tile: false ) );

	/// <summary>
	/// Rendered at twice the widget size so it stays sharp on high-DPI screens.
	/// </summary>
	private static int PixelSize( Rect rect ) => (int)MathF.Ceiling( MathF.Max( rect.Width, rect.Height ) * 2 );

	/// <summary>
	/// The two-tone wordmark: "Hot" in red, "Code" in orange, in Inter Black like the store images.
	/// Drawn from <paramref name="x"/>, vertically centred in the row at <paramref name="y"/> with height <paramref name="h"/>.
	/// Returns its width.
	/// </summary>
	public static float PaintWordmark( float x, float y, float h, float size )
	{
		Paint.SetFont( "Inter", size, 900 );
		var hot = Paint.MeasureText( "Hot" ).x;
		var code = Paint.MeasureText( "Code" ).x;

		Paint.SetPen( Red );
		Paint.DrawText( new Rect( x, y, hot + 4, h ), "Hot", TextFlag.LeftCenter );
		Paint.SetPen( Orange );
		Paint.DrawText( new Rect( x + hot, y, code + 4, h ), "Code", TextFlag.LeftCenter );

		return hot + code;
	}

	/// <summary>
	/// The badge as a pixmap, for SetWindowIcon (title bars and the taskbar).
	/// </summary>
	public static Pixmap WindowIcon => Render( 64, tile: true );
}

/// <summary>
/// A widget that just shows the logo: the badge (white tile), or the bare mark.
/// </summary>
public class LogoWidget : Widget
{
	private readonly bool _badge;

	public LogoWidget( Widget parent, float size, bool badge ) : base( parent )
	{
		_badge = badge;
		FixedSize = new Vector2( size, size );
		TransparentForMouseEvents = true;
	}

	protected override void OnPaint()
	{
		if ( _badge ) HotCodeLogo.PaintBadge( LocalRect );
		else HotCodeLogo.PaintMark( LocalRect );
	}
}

/// <summary>
/// The "HotCode" wordmark as a widget.
/// </summary>
public class WordmarkWidget : Widget
{
	private readonly float _size;

	public WordmarkWidget( Widget parent, float size ) : base( parent )
	{
		_size = size;
		Paint.SetFont( "Inter", size, 900 );
		var measured = Paint.MeasureText( "HotCode" );
		FixedSize = new Vector2( measured.x + 8, measured.y + 4 );
		TransparentForMouseEvents = true;
	}

	protected override void OnPaint() => HotCodeLogo.PaintWordmark( 0, 0, Height, _size );
}