Give terrain, sky, retained UI, portal preparation, and the update/render frame pair explicit single owners. Make shader, texture, text, bindless, and GL construction prefixes checked and retryable so partial failure cannot lose or replay resource ownership. Co-authored-by: Codex <codex@openai.com>
250 lines
9.1 KiB
C#
250 lines
9.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Numerics;
|
|
using AcDream.App.Rendering;
|
|
using Silk.NET.Input;
|
|
using Silk.NET.OpenGL;
|
|
|
|
namespace AcDream.App.UI;
|
|
|
|
/// <summary>
|
|
/// Packages the <see cref="UiRoot"/>, the 2D sprite batcher
|
|
/// (<see cref="Rendering.TextRenderer"/>), and a default font so
|
|
/// <c>GameWindow</c> can wire the retail-style UI in with one
|
|
/// construction and a handful of input callbacks.
|
|
///
|
|
/// Usage (from <c>GameWindow.OnLoad</c>):
|
|
/// <code>
|
|
/// _uiHost = new UiHost(_gl, shadersDir, _debugFont);
|
|
/// _uiHost.Root.WorldMouseFallThrough += (btn, x, y, f) => HandleWorldClick(btn, x, y);
|
|
/// _uiHost.Root.WorldKeyFallThrough += (vk, lp) => HandleHotkey(vk);
|
|
///
|
|
/// foreach (var mouse in _input.Mice)
|
|
/// _uiHost.WireMouse(mouse);
|
|
/// foreach (var kb in _input.Keyboards)
|
|
/// _uiHost.WireKeyboard(kb);
|
|
/// </code>
|
|
///
|
|
/// And per frame (from <c>GameWindow.OnRender</c>):
|
|
/// <code>
|
|
/// _uiHost.Tick(deltaSeconds);
|
|
/// _uiHost.Draw(new Vector2(_window!.Size.X, _window.Size.Y));
|
|
/// </code>
|
|
///
|
|
/// Retail analog: the trio of <c>DAT_00870340</c> (Core, owns fonts/atlases),
|
|
/// <c>DAT_00837ff4</c> (Device, owns input state), <c>DAT_00870c2c</c>
|
|
/// (Keystone root, widget tree). We fuse them into a single host class
|
|
/// because we're not linking to Keystone.
|
|
/// </summary>
|
|
public sealed class UiHost : System.IDisposable
|
|
{
|
|
public UiRoot Root { get; } = new();
|
|
public RetailWindowManager WindowManager => Root.WindowManager;
|
|
public TextRenderer TextRenderer { get; }
|
|
public BitmapFont? DefaultFont { get; set; }
|
|
|
|
/// <summary>The last wired keyboard. Exposed so widgets that need clipboard
|
|
/// access (<see cref="IKeyboard.ClipboardText"/>) or modifier-key state
|
|
/// (<see cref="IKeyboard.IsKeyPressed"/>) — e.g. <see cref="UiText"/>'s
|
|
/// Ctrl+C copy — can reach the device. One-keyboard desktop: last wins.</summary>
|
|
public IKeyboard? Keyboard { get; private set; }
|
|
|
|
private long _startTicks = System.Environment.TickCount64;
|
|
private readonly HostQuiescenceGate _quiescence;
|
|
private readonly List<IRetainedUiInputBinding> _inputBindings = new();
|
|
private ResourceShutdownTransaction? _inputShutdown;
|
|
private ResourceShutdownTransaction? _shutdown;
|
|
private bool _disposeRequested;
|
|
private bool _disposed;
|
|
|
|
internal bool IsDisposalComplete => _disposed;
|
|
|
|
public UiHost(GL gl, string shaderDir, BitmapFont? defaultFont = null)
|
|
: this(gl, shaderDir, defaultFont, new HostQuiescenceGate())
|
|
{
|
|
}
|
|
|
|
internal UiHost(
|
|
GL gl,
|
|
string shaderDir,
|
|
BitmapFont? defaultFont,
|
|
HostQuiescenceGate quiescence)
|
|
{
|
|
_quiescence = quiescence ?? throw new ArgumentNullException(nameof(quiescence));
|
|
TextRenderer = new TextRenderer(gl, shaderDir);
|
|
DefaultFont = defaultFont;
|
|
}
|
|
|
|
// ── Per-frame ──────────────────────────────────────────────────────
|
|
|
|
public void Tick(double deltaSeconds)
|
|
{
|
|
long now = System.Environment.TickCount64 - _startTicks;
|
|
Root.Tick(deltaSeconds, now);
|
|
}
|
|
|
|
public void Draw(Vector2 screenSize)
|
|
{
|
|
// Set UiRoot bounds to full screen so HitTestTopDown works.
|
|
Root.Width = screenSize.X;
|
|
Root.Height = screenSize.Y;
|
|
var ctx = new UiRenderContext(TextRenderer, screenSize, DefaultFont);
|
|
TextRenderer.Begin(screenSize);
|
|
Root.Draw(ctx);
|
|
TextRenderer.Flush(DefaultFont);
|
|
}
|
|
|
|
// ── Input wiring helpers ───────────────────────────────────────────
|
|
|
|
public void WireMouse(IMouse mouse)
|
|
{
|
|
System.ObjectDisposedException.ThrowIf(_disposeRequested || _disposed, this);
|
|
System.ArgumentNullException.ThrowIfNull(mouse);
|
|
|
|
var binding = new RetainedMouseInputBinding(
|
|
new SilkRetainedMouseSurface(mouse),
|
|
Root,
|
|
_quiescence);
|
|
_inputBindings.Add(binding);
|
|
try
|
|
{
|
|
binding.Attach();
|
|
}
|
|
catch
|
|
{
|
|
if (binding.IsDisposalComplete)
|
|
_inputBindings.Remove(binding);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void WireKeyboard(IKeyboard kb)
|
|
{
|
|
System.ObjectDisposedException.ThrowIf(_disposeRequested || _disposed, this);
|
|
System.ArgumentNullException.ThrowIfNull(kb);
|
|
Keyboard = kb; // last wired keyboard wins (one-keyboard desktop)
|
|
var binding = new RetainedKeyboardInputBinding(
|
|
new SilkRetainedKeyboardSurface(kb),
|
|
Root,
|
|
_quiescence);
|
|
_inputBindings.Add(binding);
|
|
try
|
|
{
|
|
binding.Attach();
|
|
}
|
|
catch
|
|
{
|
|
if (binding.IsDisposalComplete)
|
|
_inputBindings.Remove(binding);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stops retained-device delivery without retiring the window tree or GL
|
|
/// renderer. Safe to call before a potentially long live-session close.
|
|
/// </summary>
|
|
public void QuiesceInput()
|
|
{
|
|
_disposeRequested = true;
|
|
foreach (IRetainedUiInputBinding binding in _inputBindings)
|
|
binding.Deactivate();
|
|
Keyboard = null;
|
|
}
|
|
|
|
/// <summary>Physically removes every retained input edge after quiescence.</summary>
|
|
public void DeactivateInput()
|
|
{
|
|
QuiesceInput();
|
|
|
|
_inputShutdown ??= new ResourceShutdownTransaction(
|
|
new ResourceShutdownStage(
|
|
"retained UI input subscriptions",
|
|
_inputBindings
|
|
.AsEnumerable()
|
|
.Reverse()
|
|
.Select((binding, index) => new ResourceShutdownOperation(
|
|
$"input binding {index}",
|
|
binding.Dispose))
|
|
.ToArray()));
|
|
_inputShutdown.CompleteOrThrow();
|
|
if (_inputShutdown.IsComplete)
|
|
_inputBindings.Clear();
|
|
}
|
|
|
|
// ── Window manager forwarders (delegate to UiRoot) ─────────────────
|
|
|
|
/// <summary>Register a top-level window for Show/Hide/Toggle. See <see cref="UiRoot.RegisterWindow"/>.</summary>
|
|
public RetailWindowHandle RegisterWindow(
|
|
string name,
|
|
UiElement window,
|
|
UiElement? contentRoot = null,
|
|
IRetainedPanelController? controller = null,
|
|
IRetainedWindowStateController? stateController = null)
|
|
=> Root.RegisterWindow(name, window, contentRoot, controller, stateController);
|
|
|
|
public bool UnregisterWindow(string name) => Root.UnregisterWindow(name);
|
|
|
|
/// <summary>Show a registered window; returns false if the name is unknown.</summary>
|
|
public bool ShowWindow(string name) => Root.ShowWindow(name);
|
|
|
|
/// <summary>Hide a registered window; returns false if the name is unknown.</summary>
|
|
public bool HideWindow(string name) => Root.HideWindow(name);
|
|
|
|
public bool CloseWindow(string name) => Root.CloseWindow(name);
|
|
|
|
/// <summary>Return the current visibility of a registered window.</summary>
|
|
public bool IsWindowVisible(string name) => Root.IsWindowVisible(name);
|
|
|
|
/// <summary>Toggle a registered window's visibility; returns the new IsVisible.</summary>
|
|
public bool ToggleWindow(string name) => Root.ToggleWindow(name);
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_disposed)
|
|
return;
|
|
|
|
_disposeRequested = true;
|
|
_shutdown ??= CreateShutdownTransaction(
|
|
[DeactivateInput],
|
|
() => { },
|
|
WindowManager.Dispose,
|
|
TextRenderer.Dispose);
|
|
_shutdown.CompleteOrThrow();
|
|
_disposed = _shutdown.IsComplete;
|
|
}
|
|
|
|
internal static ResourceShutdownTransaction CreateShutdownTransaction(
|
|
IReadOnlyList<Action> inputUnsubscribers,
|
|
Action releaseInputState,
|
|
Action disposeWindowManager,
|
|
Action disposeTextRenderer)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(inputUnsubscribers);
|
|
ArgumentNullException.ThrowIfNull(releaseInputState);
|
|
ArgumentNullException.ThrowIfNull(disposeWindowManager);
|
|
ArgumentNullException.ThrowIfNull(disposeTextRenderer);
|
|
|
|
return new ResourceShutdownTransaction(
|
|
new ResourceShutdownStage(
|
|
"retained UI input subscriptions",
|
|
inputUnsubscribers
|
|
.Select((unsubscribe, index) => new ResourceShutdownOperation(
|
|
$"input subscription {index}",
|
|
unsubscribe ?? throw new ArgumentException(
|
|
"Input unsubscriber entries cannot be null.",
|
|
nameof(inputUnsubscribers))))
|
|
.ToArray()),
|
|
new ResourceShutdownStage("retained UI input state",
|
|
[
|
|
new("input state", releaseInputState),
|
|
]),
|
|
new ResourceShutdownStage("retained UI windows",
|
|
[
|
|
new("window manager", disposeWindowManager),
|
|
]),
|
|
new ResourceShutdownStage("retained UI renderer",
|
|
[
|
|
new("text renderer", disposeTextRenderer),
|
|
]));
|
|
}
|
|
}
|