Aquarium/src/Aquarium.Engine/Render/DirectWriteOverlay.cs

229 lines
9.4 KiB
C#
Raw Normal View History

2026-05-08 16:38:39 +01:00
using Vortice.DCommon;
using Vortice.Direct2D1;
using Vortice.DirectWrite;
using Vortice.DXGI;
using Vortice.Mathematics;
2026-05-09 01:04:11 +01:00
using Aquarium.Engine.Render.Ui;
2026-05-08 16:38:39 +01:00
using D2DFactoryType = Vortice.Direct2D1.FactoryType;
using DWriteFactoryType = Vortice.DirectWrite.FactoryType;
namespace Aquarium.Engine.Render;
internal sealed class DirectWriteOverlay : IDisposable
{
private readonly ID2D1Factory direct2DFactory;
2026-05-08 16:58:04 +01:00
private readonly IDWriteFactory6 directWriteFactory;
private readonly IDWriteFontCollection fontCollection;
private readonly IDWriteTypography smallCapsTypography;
2026-05-08 16:38:39 +01:00
private readonly ID2D1RenderTarget renderTarget;
private readonly ID2D1SolidColorBrush primaryTextBrush;
private readonly ID2D1SolidColorBrush quietTextBrush;
2026-05-09 01:04:11 +01:00
private readonly ID2D1SolidColorBrush panelBrush;
private readonly ID2D1SolidColorBrush rowBrush;
2026-05-09 01:46:50 +01:00
private readonly ID2D1SolidColorBrush hoverRowBrush;
2026-05-09 01:56:38 +01:00
private readonly ID2D1SolidColorBrush activeRowBrush;
2026-05-09 01:04:11 +01:00
private readonly ID2D1SolidColorBrush outlineBrush;
private readonly ID2D1SolidColorBrush accentBrush;
2026-05-09 02:04:31 +01:00
private readonly ID2D1SolidColorBrush accentHoverBrush;
private readonly ID2D1SolidColorBrush accentActiveBrush;
2026-05-09 01:04:11 +01:00
private readonly ID2D1SolidColorBrush dimAccentBrush;
2026-05-09 02:04:31 +01:00
private readonly ID2D1SolidColorBrush trackHoverBrush;
private readonly ID2D1SolidColorBrush trackActiveBrush;
2026-05-08 16:38:39 +01:00
private readonly IDWriteTextFormat titleFormat;
private readonly IDWriteTextFormat smallFormat;
2026-05-12 13:31:23 +01:00
private readonly IDWriteTextFormat monospaceFormat;
2026-05-08 16:38:39 +01:00
private readonly int width;
private readonly int height;
public DirectWriteOverlay(IDXGISurface backBufferSurface, int width, int height)
{
this.width = width;
this.height = height;
direct2DFactory = D2D1.D2D1CreateFactory<ID2D1Factory>(D2DFactoryType.SingleThreaded);
2026-05-08 16:58:04 +01:00
directWriteFactory = DWrite.DWriteCreateFactory<IDWriteFactory6>(DWriteFactoryType.Shared);
fontCollection = CreateBrandFontCollection();
smallCapsTypography = directWriteFactory.CreateTypography();
smallCapsTypography.AddFontFeature(new FontFeature
{
NameTag = FontFeatureTag.SmallCapitalsFromCapitals,
Parameter = 1
});
smallCapsTypography.AddFontFeature(new FontFeature
{
NameTag = FontFeatureTag.CapitalSpacing,
Parameter = 1
});
2026-05-08 16:38:39 +01:00
var renderTargetProperties = new RenderTargetProperties(
RenderTargetType.Default,
new PixelFormat(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Ignore),
96.0f,
96.0f,
RenderTargetUsage.None,
FeatureLevel.Default);
renderTarget = direct2DFactory.CreateDxgiSurfaceRenderTarget(backBufferSurface, renderTargetProperties);
renderTarget.AntialiasMode = AntialiasMode.PerPrimitive;
renderTarget.TextAntialiasMode = Vortice.Direct2D1.TextAntialiasMode.Grayscale;
primaryTextBrush = renderTarget.CreateSolidColorBrush(new Color4(0.88f, 0.96f, 1.0f, 0.92f));
quietTextBrush = renderTarget.CreateSolidColorBrush(new Color4(0.54f, 0.68f, 0.72f, 0.72f));
2026-05-09 01:04:11 +01:00
panelBrush = renderTarget.CreateSolidColorBrush(new Color4(0.018f, 0.022f, 0.032f, 0.96f));
rowBrush = renderTarget.CreateSolidColorBrush(new Color4(0.085f, 0.084f, 0.12f, 0.96f));
2026-05-09 01:46:50 +01:00
hoverRowBrush = renderTarget.CreateSolidColorBrush(new Color4(0.13f, 0.13f, 0.18f, 0.98f));
2026-05-09 01:56:38 +01:00
activeRowBrush = renderTarget.CreateSolidColorBrush(new Color4(0.19f, 0.18f, 0.24f, 0.99f));
2026-05-09 01:04:11 +01:00
outlineBrush = renderTarget.CreateSolidColorBrush(new Color4(0.28f, 0.34f, 0.36f, 0.72f));
accentBrush = renderTarget.CreateSolidColorBrush(new Color4(1.0f, 0.38f, 0.055f, 0.96f));
2026-05-09 02:04:31 +01:00
accentHoverBrush = renderTarget.CreateSolidColorBrush(new Color4(1.0f, 0.58f, 0.30f, 0.98f));
accentActiveBrush = renderTarget.CreateSolidColorBrush(new Color4(1.0f, 0.78f, 0.58f, 1.0f));
2026-05-09 01:04:11 +01:00
dimAccentBrush = renderTarget.CreateSolidColorBrush(new Color4(0.20f, 0.20f, 0.22f, 0.95f));
2026-05-09 02:04:31 +01:00
trackHoverBrush = renderTarget.CreateSolidColorBrush(new Color4(0.32f, 0.29f, 0.29f, 0.98f));
trackActiveBrush = renderTarget.CreateSolidColorBrush(new Color4(0.45f, 0.38f, 0.34f, 1.0f));
2026-05-08 16:58:04 +01:00
titleFormat = CreateTextFormat("Montserrat", 18.0f, FontWeight.Thin);
smallFormat = CreateTextFormat("Ubuntu Sans", 11.0f, FontWeight.Regular);
2026-05-12 13:36:50 +01:00
monospaceFormat = CreateTextFormat("Ubuntu Sans Mono", 11.0f, FontWeight.Regular, ParagraphAlignment.Near);
2026-05-08 16:38:39 +01:00
}
2026-05-12 05:32:51 +01:00
public void Render(AquariumFrame frame, int renderDebugMode, DebugUi? debugUi, IReadOnlyList<DebugUi> clientUiPanels)
2026-05-08 16:38:39 +01:00
{
renderTarget.BeginDraw();
2026-05-08 16:58:04 +01:00
DrawHeader(
"directwrite overlay",
2026-05-08 16:38:39 +01:00
titleFormat,
RectFromEdges(18, 14, Math.Min(width - 18, 340), 42),
2026-05-08 16:58:04 +01:00
primaryTextBrush);
2026-05-08 16:38:39 +01:00
renderTarget.DrawText(
$"view r {frame.View.Radius:0.00} cam z {frame.CameraPosition.Z:0.00}",
2026-05-08 16:38:39 +01:00
smallFormat,
RectFromEdges(18, 38, Math.Min(width - 18, 420), 62),
2026-05-08 16:38:39 +01:00
quietTextBrush,
DrawTextOptions.Clip);
2026-05-08 23:23:47 +01:00
if (renderDebugMode > 0)
{
renderTarget.DrawText(
2026-05-10 15:43:03 +01:00
$"render debug {renderDebugMode} F1 cycle debug panel selects",
2026-05-08 23:23:47 +01:00
smallFormat,
RectFromEdges(18, 56, Math.Min(width - 18, 420), 80),
2026-05-08 23:23:47 +01:00
quietTextBrush,
DrawTextOptions.Clip);
}
2026-05-08 16:38:39 +01:00
renderTarget.DrawText(
"crisp text belongs after tonemapping",
smallFormat,
RectFromEdges(18, Math.Max(64, height - 36), Math.Min(width - 18, 420), height - 12),
2026-05-08 16:38:39 +01:00
quietTextBrush,
DrawTextOptions.Clip);
2026-05-09 01:04:11 +01:00
debugUi?.Draw(
renderTarget,
titleFormat,
smallFormat,
2026-05-12 13:31:23 +01:00
monospaceFormat,
2026-05-09 01:04:11 +01:00
panelBrush,
rowBrush,
2026-05-09 01:46:50 +01:00
hoverRowBrush,
2026-05-09 01:56:38 +01:00
activeRowBrush,
2026-05-09 01:04:11 +01:00
outlineBrush,
primaryTextBrush,
quietTextBrush,
accentBrush,
2026-05-09 02:04:31 +01:00
accentHoverBrush,
accentActiveBrush,
2026-05-09 01:46:50 +01:00
dimAccentBrush,
2026-05-09 02:04:31 +01:00
trackHoverBrush,
trackActiveBrush,
2026-05-09 01:46:50 +01:00
width,
height);
2026-05-12 05:32:51 +01:00
foreach (var panel in clientUiPanels)
{
panel.Draw(
renderTarget,
titleFormat,
smallFormat,
2026-05-12 13:31:23 +01:00
monospaceFormat,
2026-05-12 05:32:51 +01:00
panelBrush,
rowBrush,
hoverRowBrush,
activeRowBrush,
outlineBrush,
primaryTextBrush,
quietTextBrush,
accentBrush,
accentHoverBrush,
accentActiveBrush,
dimAccentBrush,
trackHoverBrush,
trackActiveBrush,
width,
height);
}
2026-05-08 16:38:39 +01:00
renderTarget.EndDraw();
}
public void Dispose()
{
smallFormat.Dispose();
2026-05-12 13:31:23 +01:00
monospaceFormat.Dispose();
2026-05-08 16:38:39 +01:00
titleFormat.Dispose();
2026-05-08 16:58:04 +01:00
smallCapsTypography.Dispose();
fontCollection.Dispose();
2026-05-09 01:04:11 +01:00
dimAccentBrush.Dispose();
2026-05-09 02:04:31 +01:00
trackActiveBrush.Dispose();
trackHoverBrush.Dispose();
accentActiveBrush.Dispose();
accentHoverBrush.Dispose();
2026-05-09 01:04:11 +01:00
accentBrush.Dispose();
outlineBrush.Dispose();
2026-05-09 01:56:38 +01:00
activeRowBrush.Dispose();
2026-05-09 01:46:50 +01:00
hoverRowBrush.Dispose();
2026-05-09 01:04:11 +01:00
rowBrush.Dispose();
panelBrush.Dispose();
2026-05-08 16:38:39 +01:00
quietTextBrush.Dispose();
primaryTextBrush.Dispose();
renderTarget.Dispose();
directWriteFactory.Dispose();
direct2DFactory.Dispose();
}
2026-05-12 13:36:50 +01:00
private IDWriteTextFormat CreateTextFormat(string familyName, float size, FontWeight weight, ParagraphAlignment paragraphAlignment = ParagraphAlignment.Center)
2026-05-08 16:38:39 +01:00
{
var format = directWriteFactory.CreateTextFormat(
2026-05-08 16:58:04 +01:00
familyName,
fontCollection,
2026-05-08 16:38:39 +01:00
weight,
FontStyle.Normal,
FontStretch.Normal,
size,
"en-us");
format.WordWrapping = WordWrapping.NoWrap;
2026-05-12 13:36:50 +01:00
format.ParagraphAlignment = paragraphAlignment;
2026-05-08 16:38:39 +01:00
return format;
}
2026-05-08 16:58:04 +01:00
private IDWriteFontCollection CreateBrandFontCollection()
{
using var builder = directWriteFactory.CreateFontSetBuilder();
builder.AddFontFile(FontAssetPath("Montserrat[wght].ttf"));
builder.AddFontFile(FontAssetPath("UbuntuSans[wdth,wght].ttf"));
2026-05-12 13:31:23 +01:00
builder.AddFontFile(FontAssetPath("UbuntuSansMono.ttf"));
2026-05-08 16:58:04 +01:00
using var fontSet = builder.CreateFontSet();
return directWriteFactory.CreateFontCollectionFromFontSet(fontSet, FontFamilyModel.Typographic);
}
private void DrawHeader(string text, IDWriteTextFormat format, Rect bounds, ID2D1Brush brush)
{
var displayText = text.ToUpperInvariant();
using var layout = directWriteFactory.CreateTextLayout(displayText, format, bounds.Width, bounds.Height);
layout.SetTypography(smallCapsTypography, new TextRange(0, (uint)displayText.Length));
renderTarget.DrawTextLayout(new System.Numerics.Vector2(bounds.Left, bounds.Top), layout, brush, DrawTextOptions.Clip);
}
private static string FontAssetPath(string fileName)
{
return Path.Combine(AppContext.BaseDirectory, "Assets", "Fonts", fileName);
}
private static Rect RectFromEdges(float left, float top, float right, float bottom)
{
return new Rect(left, top, Math.Max(0.0f, right - left), Math.Max(0.0f, bottom - top));
}
2026-05-08 16:38:39 +01:00
}