refactor(app): compose platform input and camera startup

This commit is contained in:
Erik 2026-07-22 15:13:04 +02:00
parent adb204560d
commit 1d51e35c14
4 changed files with 877 additions and 88 deletions

View file

@ -1,4 +1,5 @@
using AcDream.Core.Plugins;
using AcDream.App.Composition;
using AcDream.App.Physics;
using AcDream.App.Settings;
using AcDream.App.World;
@ -11,7 +12,10 @@ using Silk.NET.Windowing;
namespace AcDream.App.Rendering;
public sealed class GameWindow : IDisposable
public sealed class GameWindow :
IDisposable,
IGameWindowPlatformPublication<GL, IInputContext>,
IGameWindowHostInputCameraPublication
{
private static double ClientTimerNow() =>
System.Diagnostics.Stopwatch.GetTimestamp()
@ -653,56 +657,96 @@ public sealed class GameWindow : IDisposable
}
}
void IGameWindowPlatformPublication<GL, IInputContext>.PublishGraphics(
GL graphics) =>
PublishCompositionOwner(ref _gl, graphics, "graphics API");
void IGameWindowPlatformPublication<GL, IInputContext>.PublishInput(
IInputContext input) =>
PublishCompositionOwner(ref _input, input, "input context");
void IGameWindowHostInputCameraPublication.PublishGpuFrameFlights(
GpuFrameFlightController value) =>
PublishCompositionOwner(ref _gpuFrameFlights, value, "GPU frame flights");
void IGameWindowHostInputCameraPublication.PublishKeyboardSource(
AcDream.App.Input.SilkKeyboardSource value) =>
PublishCompositionOwner(ref _kbSource, value, "keyboard source");
void IGameWindowHostInputCameraPublication.PublishMouseSource(
AcDream.App.Input.SilkMouseSource value) =>
PublishCompositionOwner(ref _mouseSource, value, "mouse source");
void IGameWindowHostInputCameraPublication.PublishMouseLookCursor(
AcDream.App.Input.IMouseLookCursor value) =>
PublishCompositionOwner(ref _mouseLookCursor, value, "mouse-look cursor");
void IGameWindowHostInputCameraPublication.PublishInputDispatcher(
AcDream.UI.Abstractions.Input.InputDispatcher value) =>
PublishCompositionOwner(ref _inputDispatcher, value, "input dispatcher");
void IGameWindowHostInputCameraPublication.PublishCameraController(
CameraController value) =>
PublishCompositionOwner(ref _cameraController, value, "camera controller");
void IGameWindowHostInputCameraPublication.PublishCameraPointerInput(
AcDream.App.Input.CameraPointerInputController value) =>
PublishCompositionOwner(ref _cameraPointerInput, value, "camera pointer input");
private static void PublishCompositionOwner<T>(
ref T? destination,
T value,
string name)
where T : class
{
ArgumentNullException.ThrowIfNull(value);
if (destination is not null)
throw new InvalidOperationException(
$"The GameWindow composition shell already owns {name}.");
destination = value;
}
[System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_gl), nameof(_input))]
private GameWindowPlatformResult<GL, IInputContext> AcquirePlatform()
{
GameWindowPlatformResult<GL, IInputContext> platform =
GameWindowPlatformAcquisition.Acquire(
() => GL.GetApi(_window!),
static gl => gl.Dispose(),
() => _window!.CreateInput(),
static input => input.Dispose(),
this);
if (_gl is null || _input is null)
throw new InvalidOperationException(
"Platform acquisition returned without publishing both owners.");
return platform;
}
private void OnLoad()
{
// Task 7: wire the physics data cache into the engine so Transition can
// run narrow-phase BSP tests during FindObjCollisions.
_physicsEngine.DataCache = _physicsDataCache;
_gl = GL.GetApi(_window!);
_framebufferResize.BindViewport(
new SilkFramebufferViewportTarget(_gl));
_gpuFrameFlights = new AcDream.App.Rendering.GpuFrameFlightController(_gl);
var worldRenderDiagnostics = new AcDream.App.Rendering.WorldRenderDiagnostics(
new AcDream.App.Rendering.SilkRenderGlStateReader(_gl),
_renderDiagnosticLog);
_input = _window!.CreateInput();
GameWindowPlatformResult<GL, IInputContext> platform = AcquirePlatform();
// Phase K.1b — every keyboard/mouse handler routes through the
// InputDispatcher. The legacy direct kb.KeyDown / mouse.MouseDown
// switches are gone; subscribers below own all game-side reactions.
// We KEEP a direct mouse.MouseMove handler because mouse-delta is
// axis input, not chord input — but with explicit WantCaptureMouse
// gating and the previous "_playerMouseDeltaX +=" line dropped so
// mouse delta NEVER drives character yaw (regression-prevention
// per K.1b plan §D).
var firstKb = _input.Keyboards.FirstOrDefault();
var firstMouse = _input.Mice.FirstOrDefault();
if (firstKb is not null)
{
_kbSource = AcDream.App.Input.SilkKeyboardSource.CreateDetached(
firstKb,
_hostQuiescence);
_kbSource.Attach();
}
if (firstMouse is not null)
{
_mouseSource = AcDream.App.Input.SilkMouseSource.CreateDetached(
firstMouse,
_inputCapture,
_kbSource,
_hostQuiescence);
_mouseSource.Attach();
_mouseLookCursor = new AcDream.App.Input.SilkMouseLookCursor(firstMouse);
}
if (_kbSource is not null && _mouseSource is not null)
{
_inputDispatcher = AcDream.UI.Abstractions.Input.InputDispatcher.CreateDetached(
_kbSource, _mouseSource, _keyBindings);
_inputDispatcher.Attach();
_movementInput.Bind(_inputDispatcher);
_cameraInput.Bind(_inputDispatcher);
}
HostInputCameraResult hostInputCamera =
new HostInputCameraCompositionPhase(
new HostInputCameraDependencies(
_framebufferResize,
_window!.FramebufferSize,
_hostQuiescence,
_inputCapture,
_keyBindings,
_movementInput,
_cameraInput,
_localPlayerMode,
_chaseCameraInput,
_pointerPosition,
_renderDiagnosticLog),
this).Compose(platform);
WorldRenderDiagnostics worldRenderDiagnostics =
hostInputCamera.WorldRenderDiagnostics;
_gl.ClearColor(0.05f, 0.10f, 0.18f, 1.0f);
_gl.Enable(EnableCap.DepthTest);
@ -754,27 +798,6 @@ public sealed class GameWindow : IDisposable
Console.WriteLine("world-hud font: no system monospace font found");
}
var orbit = new OrbitCamera();
var fly = new FlyCamera();
_cameraController = new CameraController(orbit, fly);
_framebufferResize.BindCamera(
new CameraFramebufferTarget(_cameraController));
_framebufferResize.Resize(_window!.FramebufferSize);
if (_mouseSource is not null && firstMouse is not null)
{
_cameraPointerInput = AcDream.App.Input.CameraPointerInputController.Create(
_input.Mice,
_hostQuiescence,
_inputCapture,
_localPlayerMode,
_cameraController,
_chaseCameraInput,
_mouseSource,
_pointerPosition,
new AcDream.App.Input.EnvironmentInputMonotonicClock());
_cameraPointerInput.AttachRaw();
}
_dats = RuntimeDatCollectionFactory.OpenReadOnly(_datDir);
_magicCatalog = AcDream.App.Spells.MagicCatalog.Load(_dats);
SpellBook.InstallMetadata(_magicCatalog.SpellTable);