feat(ui): Audio tab — live volume sliders driving OpenAL engine

Phase L.0 (cont.) — second tab on the Settings shell, in the Easy-wins
build order. Audio is the live-preview poster child: dragging a slider
is audible immediately, Save persists, Cancel reverts and the engine
catches up on the next frame.

AudioSettings record: Master / Music / Sfx / Ambient (all 0..1 floats).
Defaults match the OpenAlAudioEngine constructor values exactly so a
user who never opens the tab gets identical behaviour to the
pre-Phase-L env-var-only world (Master=1.0, Music=0.7, Sfx=1.0,
Ambient=0.8).

SettingsStore grows LoadAudio / SaveAudio + a generic SaveSection
helper that consolidates the unknown-top-level-key preservation logic.
Display and Audio sections coexist in settings.json:
{ "version": 1, "display": { ... }, "audio": { ... } }
Saving one section preserves the other on disk; a future Gameplay /
Chat / Character section drops in the same way without touching
existing data.

SettingsVM gains a parallel audio state machine (audioPersisted /
audioDraft / SetAudio / onSaveAudio callback). HasUnsavedChanges
covers all three buckets now (keybinds + display + audio); Save /
Cancel / ResetAll are atomic across all of them.

GameWindow wiring is the live-preview mechanism — every render frame
pushes the VM's AudioDraft into _audioEngine.MasterVolume etc. Cheap
(four float assignments) and unconditional. SetListener still applies
MasterVolume each frame too via the existing Phase E.2 code path, so
listener gain stays in sync. Persisted audio is applied to the engine
ONCE at startup before the first frame so the user's saved values
take effect before any sound plays — startup-time apply happens during
the same SettingsVM construction site that does the LoadDisplay +
LoadAudio.

SettingsPanel.RenderAudioTab replaces the L.0-shell placeholder — four
SliderFloat calls clamped to [0, 1], plus a footer note explaining the
live-preview UX. The "Coming soon" placeholder test was retargeted
from "Audio" to "Gameplay" since Audio is no longer a placeholder.

16 new tests:
 · AudioSettings record (3) — defaults pin engine constants, value
   equality, with-expressions
 · SettingsStore audio round-trip (5) — missing-file → defaults,
   round-trip all fields, partial-file per-field fallback, save-audio-
   preserves-display, save-display-preserves-audio
 · SettingsVM audio state (5) — initial draft tracks persisted,
   SetAudio marks dirty, Save invokes audio callback, Cancel reverts,
   ResetAllToDefaults covers audio
 · SettingsPanel audio tab (3) — four sliders render only when active,
   no SliderFloat emitted on inactive tabs, slider range is [0, 1]

dotnet build green (0 warnings); dotnet test 1,262 / 1,262 green
(243 Core.Net + 346 UI.Abstractions + 673 Core).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-04-26 17:57:00 +02:00
parent 382f0ad3fa
commit 53b1878c5c
9 changed files with 461 additions and 47 deletions

View file

@ -901,12 +901,24 @@ public sealed class GameWindow : IDisposable
// the same OnLoad path (see _inputDispatcher field).
if (_inputDispatcher is not null)
{
// L.0 — settings.json (display + future audio / gameplay /
// L.0 — settings.json (display + audio + future gameplay /
// chat / character tabs). Coexists with keybinds.json,
// which keeps its own load/save path.
var settingsStore = new AcDream.UI.Abstractions.Panels.Settings.SettingsStore(
AcDream.UI.Abstractions.Panels.Settings.SettingsStore.DefaultPath());
var persistedDisplay = settingsStore.LoadDisplay();
var persistedAudio = settingsStore.LoadAudio();
// Apply persisted audio to the engine BEFORE the panel
// host starts pushing per-frame so the first frame uses
// the user's saved values instead of engine defaults.
if (_audioEngine is not null && _audioEngine.IsAvailable)
{
_audioEngine.MasterVolume = persistedAudio.Master;
_audioEngine.MusicVolume = persistedAudio.Music;
_audioEngine.SfxVolume = persistedAudio.Sfx;
_audioEngine.AmbientVolume = persistedAudio.Ambient;
}
_settingsVm = new AcDream.UI.Abstractions.Panels.Settings.SettingsVM(
persisted: _keyBindings,
@ -941,6 +953,21 @@ public sealed class GameWindow : IDisposable
{
Console.WriteLine($"settings: display save failed: {ex.Message}");
}
},
persistedAudio: persistedAudio,
onSaveAudio: audio =>
{
try
{
settingsStore.SaveAudio(audio);
Console.WriteLine(
"settings: audio saved to "
+ AcDream.UI.Abstractions.Panels.Settings.SettingsStore.DefaultPath());
}
catch (Exception ex)
{
Console.WriteLine($"settings: audio save failed: {ex.Message}");
}
});
_settingsPanel = new AcDream.UI.Abstractions.Panels.Settings.SettingsPanel(_settingsVm);
_panelHost.Register(_settingsPanel);
@ -4105,6 +4132,22 @@ public sealed class GameWindow : IDisposable
System.Numerics.Matrix4x4.Invert(camera.View, out var invView);
var camPos = new System.Numerics.Vector3(invView.M41, invView.M42, invView.M43);
// L.0 Audio tab: push the SettingsVM's live AudioDraft into the
// engine each frame, so volume sliders preview audibly while
// the user drags. Cancel reverts the draft and the engine
// catches up on the very next frame; Save persists to
// settings.json without changing engine state (already
// applied). Cheap enough to run unconditionally on every
// tick — four float assignments.
if (_audioEngine is not null && _audioEngine.IsAvailable && _settingsVm is not null)
{
var a = _settingsVm.AudioDraft;
_audioEngine.MasterVolume = a.Master;
_audioEngine.MusicVolume = a.Music;
_audioEngine.SfxVolume = a.Sfx;
_audioEngine.AmbientVolume = a.Ambient;
}
// Phase E.2 audio: update listener pose so 3D sounds pan/attenuate
// correctly relative to where we're looking.
if (_audioEngine is not null && _audioEngine.IsAvailable)