acdream/src/AcDream.App/UI/Layout/RetailPanelUiController.cs
Erik 9aaf97e785 Revert "Campaign V slice V4a" - it lost world multisampling
This reverts ceec3bc4. Two independent reasons, either sufficient.

The rendering regression. The slice deleted TextRenderGlStateScope, which
saved GL_MULTISAMPLE and GL_SAMPLE_ALPHA_TO_COVERAGE on entry, disabled them
for the text pass, and restored them on exit (TextRenderGlStateScope.cs:111-112
and 153-154 at the parent commit). Its replacement bakes that state into the
text pipeline but nothing restores it, and GlGpuPassEncoder.Dispose does not
either. Every world renderer is still raw GL at this point in the campaign, so
from the first UI frame onward the world drew with multisampling disabled.

The offline pixel gate caught it: 1,791 of 563,200 compared pixels differed,
0.318% against a 0.001 threshold. The commit message attributed this to
wall-clock-driven ambient animation shifting phase, and committed through the
failure. That explanation does not survive its own control: capturing twice at
the reverted-to commit differs by 19 pixels and twice at the slice's own commit
by 8, while base-versus-head differs by 1,791 - a 224x gap that no shared-noise
source explains. An amplified difference image settles it visually: the changed
pixels are the silhouette edges of every tree, building and rock, with terrain
interiors, water and the entire UI untouched. That is the signature of losing
edge antialiasing, not of animated sprites.

This is the exact failure mode two existing memory notes already warn about -
a mid-frame renderer must set every GL state it uses rather than inherit it,
and issue #52's lesson that a rendering migration must audit per-pass GL state
before declaring itself done.

The scope. The brief was three small leaf renderers plus additive frame-
lifecycle wiring, roughly ten files. The commit changed 334 files with 3,665
insertions and 3,845 deletions, including 323 public-to-internal visibility
conversions across the App assembly, 55 test files, two retired conformance
tests, and a self-described temporary escape hatch for bridging raw-GL viewport
textures. Even without the regression, that is not separable into the part
worth keeping and the part worth dropping.

Reverting rather than patching because the good work here - the RHI frame
lifecycle wiring and a genuine render-state-cache staleness fix - is small
enough to redo cleanly against a tightened spec, while untangling it from 300+
files of unrelated churn is not.

Post-revert: Release build clean, App suite back to 3,843 passed / 3 skipped,
offline pixel gate passing at 19 differing pixels.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-27 18:29:28 +02:00

339 lines
11 KiB
C#

using System;
using System.Collections.Generic;
namespace AcDream.App.UI.Layout;
/// <summary>
/// Retained-window port of retail <c>gmPanelUI::RecvNotice_SetPanelVisibility</c>
/// at <c>0x004BC6F0</c>. The original owns one active child panel and an optional
/// deferred child; this owner applies the same lifecycle to registered retained
/// windows without making the toolbar authoritative for non-toolbar panels.
/// </summary>
public sealed class RetailPanelUiController : IDisposable
{
public const uint RestorePreviousPropertyId = 0x10000049u;
private readonly Func<string, bool> _isVisible;
private readonly Func<string, bool> _show;
private readonly Func<string, bool> _hide;
private readonly Dictionary<uint, PanelEntry> _byPanel = new();
private readonly Dictionary<string, uint> _byWindow = new(StringComparer.Ordinal);
private uint? _activePanel;
private uint? _deferredPanel;
private bool _applying;
private bool _synchronizingGeometry;
private PanelGeometry? _mainPanelGeometry;
private bool _disposed;
public RetailPanelUiController(
Func<string, bool> isVisible,
Func<string, bool> show,
Func<string, bool> hide)
{
_isVisible = isVisible ?? throw new ArgumentNullException(nameof(isVisible));
_show = show ?? throw new ArgumentNullException(nameof(show));
_hide = hide ?? throw new ArgumentNullException(nameof(hide));
}
public uint? ActivePanelId => _activePanel;
/// <summary>
/// Registers one of retail <c>gmPanelUI</c>'s toolbar or detail children.
/// All primary and detail children retain panel-specific content but share
/// one parent geometry. A move or resize of any registered child updates
/// every hidden sibling through typed window handles, so persistence sees
/// the same canonical rectangle too.
/// </summary>
public void RegisterMainPanel(
uint panelId,
string windowName,
RetailWindowHandle window,
bool restorePrevious = false)
{
ArgumentNullException.ThrowIfNull(window);
if (!string.Equals(windowName, window.Name, StringComparison.Ordinal))
throw new ArgumentException(
$"Panel window name '{windowName}' does not match handle '{window.Name}'.",
nameof(windowName));
RegisterCore(
panelId,
windowName,
restorePrevious,
window,
sharesMainPanelGeometry: true);
}
/// <param name="restorePrevious">
/// Effective DAT bool property <c>0x10000049</c>. Retail retains the previous
/// panel only when the newly shown panel has this property and the previous
/// panel does not.
/// </param>
public void Register(uint panelId, string windowName, bool restorePrevious = false)
=> RegisterCore(
panelId,
windowName,
restorePrevious,
window: null,
sharesMainPanelGeometry: false);
private void RegisterCore(
uint panelId,
string windowName,
bool restorePrevious,
RetailWindowHandle? window,
bool sharesMainPanelGeometry)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (panelId == 0) throw new ArgumentOutOfRangeException(nameof(panelId));
ArgumentException.ThrowIfNullOrWhiteSpace(windowName);
if (_byPanel.ContainsKey(panelId) || _byWindow.ContainsKey(windowName))
throw new InvalidOperationException(
$"Panel {panelId} or retained window '{windowName}' is already registered.");
var entry = new PanelEntry(
windowName,
restorePrevious,
window,
sharesMainPanelGeometry);
_byPanel.Add(panelId, entry);
_byWindow.Add(windowName, panelId);
if (window is not null)
{
window.Moved += OnWindowMoved;
window.Resized += OnWindowResized;
}
if (sharesMainPanelGeometry && _mainPanelGeometry is { } geometry)
ApplyWindowGeometry(entry, geometry);
if (_isVisible(windowName))
ObserveWindowVisibility(windowName, visible: true);
}
/// <summary>
/// Registers a panel directly from its fully inherited retail root so the
/// authored deferred-panel behavior cannot be dropped at the mount seam.
/// </summary>
public void Register(uint panelId, string windowName, ElementInfo authoredRoot)
{
ArgumentNullException.ThrowIfNull(authoredRoot);
Register(
panelId,
windowName,
authoredRoot.TryGetEffectiveBool(
RestorePreviousPropertyId,
out bool restorePrevious)
&& restorePrevious);
}
public bool IsPanelVisible(uint panelId)
=> _byPanel.TryGetValue(panelId, out PanelEntry entry)
&& _isVisible(entry.WindowName);
public bool TogglePanel(uint panelId)
{
bool visible = !IsPanelVisible(panelId);
return SetPanelVisibility(panelId, visible) && visible;
}
public bool SetPanelVisibility(uint panelId, bool visible)
{
if (!_byPanel.TryGetValue(panelId, out PanelEntry requested)) return false;
_applying = true;
try
{
if (visible)
{
if (_activePanel == panelId)
{
PrepareMainPanelGeometry(requested);
return _show(requested.WindowName);
}
uint? previousId = _activePanel;
PanelEntry? previous = previousId is uint id
&& _byPanel.TryGetValue(id, out PanelEntry found)
&& _isVisible(found.WindowName)
? found
: null;
if (previous is { } previousEntry)
CaptureMainPanelGeometry(previousEntry, synchronizeSiblings: true);
_deferredPanel = requested.RestorePrevious
&& previous is { RestorePrevious: false }
? previousId
: null;
_activePanel = panelId;
if (previous is not null)
_hide(previous.Value.WindowName);
PrepareMainPanelGeometry(requested);
return _show(requested.WindowName);
}
if (_activePanel != panelId)
{
if (_deferredPanel == panelId) _deferredPanel = null;
return _hide(requested.WindowName);
}
CaptureMainPanelGeometry(requested, synchronizeSiblings: true);
bool hidden = _hide(requested.WindowName);
_activePanel = null;
if (_deferredPanel is not uint deferredId
|| !_byPanel.TryGetValue(deferredId, out PanelEntry deferred))
return hidden;
_deferredPanel = null;
_activePanel = deferredId;
PrepareMainPanelGeometry(deferred);
return _show(deferred.WindowName) || hidden;
}
finally
{
_applying = false;
}
}
/// <summary>
/// Feeds visibility changes made by persistence or another retained-window
/// owner back through the canonical panel lifecycle.
/// </summary>
public void ObserveWindowVisibility(string windowName, bool visible)
{
if (_applying || !_byWindow.TryGetValue(windowName, out uint panelId)) return;
SetPanelVisibility(panelId, visible);
}
private void OnWindowMoved(RetailWindowHandle window)
{
if (_disposed || _synchronizingGeometry) return;
foreach (PanelEntry entry in _byPanel.Values)
{
if (!entry.SharesMainPanelGeometry
|| !ReferenceEquals(entry.Window, window))
continue;
CaptureAndSynchronizeMainPanelGeometry(window);
return;
}
}
private void OnWindowResized(RetailWindowHandle window)
{
if (_disposed || _synchronizingGeometry) return;
foreach (PanelEntry entry in _byPanel.Values)
{
if (!entry.SharesMainPanelGeometry
|| !ReferenceEquals(entry.Window, window))
continue;
CaptureAndSynchronizeMainPanelGeometry(window);
return;
}
}
private void CaptureAndSynchronizeMainPanelGeometry(RetailWindowHandle source)
{
_mainPanelGeometry = new PanelGeometry(
source.Left,
source.Top,
source.Width,
source.Height);
SynchronizeMainPanelSiblings(source);
}
private void CaptureMainPanelGeometry(
PanelEntry entry,
bool synchronizeSiblings)
{
if (!entry.SharesMainPanelGeometry || entry.Window is not { } window)
return;
_mainPanelGeometry = new PanelGeometry(
window.Left,
window.Top,
window.Width,
window.Height);
if (synchronizeSiblings)
SynchronizeMainPanelSiblings(window);
}
private void PrepareMainPanelGeometry(PanelEntry entry)
{
if (!entry.SharesMainPanelGeometry || entry.Window is not { } window)
return;
if (_mainPanelGeometry is not { } geometry)
{
_mainPanelGeometry = new PanelGeometry(
window.Left,
window.Top,
window.Width,
window.Height);
SynchronizeMainPanelSiblings(window);
return;
}
ApplyWindowGeometry(entry, geometry);
}
private void SynchronizeMainPanelSiblings(RetailWindowHandle source)
{
if (_mainPanelGeometry is not { } geometry) return;
_synchronizingGeometry = true;
try
{
foreach (PanelEntry sibling in _byPanel.Values)
{
if (!sibling.SharesMainPanelGeometry
|| sibling.Window is not { } window
|| ReferenceEquals(window, source))
continue;
ApplyWindowGeometry(sibling, geometry);
}
}
finally
{
_synchronizingGeometry = false;
}
}
private static void ApplyWindowGeometry(PanelEntry entry, PanelGeometry geometry)
{
if (entry.Window is not { } window) return;
if (window.Left != geometry.Left || window.Top != geometry.Top)
window.MoveTo(geometry.Left, geometry.Top);
if (window.Width != geometry.Width || window.Height != geometry.Height)
window.ResizeTo(geometry.Width, geometry.Height);
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
foreach (PanelEntry entry in _byPanel.Values)
if (entry.Window is { } window)
{
window.Moved -= OnWindowMoved;
window.Resized -= OnWindowResized;
}
}
private readonly record struct PanelGeometry(
float Left,
float Top,
float Width,
float Height);
private readonly record struct PanelEntry(
string WindowName,
bool RestorePrevious,
RetailWindowHandle? Window,
bool SharesMainPanelGeometry);
}