acdream/tests/AcDream.UI.Abstractions.Tests/Input/InputDispatcherCaptureTests.cs
Erik f42c164b90 feat(ui): #25 Phase K.3 — Settings panel + click-to-rebind + Phase K shipped
Phase K final commit. Settings panel with click-to-rebind UX on top of
the K.1+K.2 input architecture, plus the roadmap / ISSUES / memory
updates that retire Phase K.

InputDispatcher gains BeginCapture / CancelCapture / IsCapturing /
SetBindings — modal capture suppresses normal action firing for the
next chord. Esc cancels (returns sentinel default chord); modifier-only
keys don't complete capture; non-modifier key down with current
modifier mask completes.

IPanelRenderer + ImGuiPanelRenderer + FakePanelRenderer gain
BeginMainMenuBar / EndMainMenuBar / BeginMenu / EndMenu / MenuItem
primitives.

SettingsVM owns a draft copy of KeyBindings with explicit Save /
Cancel / Reset semantics. Click-to-rebind enters dispatcher capture
mode; on chord captured, conflict-detect against draft (excluding the
action being rebound itself); surface a ConflictPrompt when the chord
collides; ResolveConflict(replace=true|false) commits or reverts.
ResetActionToDefault restores a single action to RetailDefaults();
ResetAllToDefaults rebuilds the entire draft. Save invokes the
onSave callback (which writes JSON + swaps the live dispatcher's
bindings).

SettingsPanel renders 8 retail-keymap-categorized CollapsingHeader
sections (Movement, Postures, Camera, Combat, UI panels, Chat,
Hotbar, Emotes). Per action: name + current binding(s) summary +
"Rebind"/"Reset" buttons. Conflict prompt at the top when pending.
Save / Cancel / "Reset all to retail defaults" at the top.

GameWindow registers SettingsPanel + wires F11 →
ToggleOptionsPanel → IsVisible toggle, plus a top-of-frame ImGui
MainMenuBar with View → Settings/Vitals/Chat/Debug entries (calls
ImGui directly — the abstraction methods exist for backend
portability but the host doesn't own a menu-bar surface).

Tests: +37 across InputDispatcherCaptureTests (7),
IPanelRendererMainMenuBarTests (9), SettingsVMTests (13),
SettingsPanelTests (8). Solution total 1220 green.

Roadmap (docs/plans/2026-04-11-roadmap.md) appends Phase K shipped
section after Phase J with K.1a–K.3 commit SHAs. ISSUES.md files
Phase L deferred work as #L.1–#L.8 (hotbar UI, spellbook favorites,
combat-mode dispatch, F-key panels, floating chat windows, UI layout
save/load, joystick bindings, plugin input subscription) and adds
#21–#25 to Recently closed. project_input_pipeline.md updated to
shipped state. CLAUDE.md gets an input-pipeline reference.

Closes Phase K.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-26 09:44:56 +02:00

149 lines
5.2 KiB
C#

using System.Collections.Generic;
using AcDream.UI.Abstractions.Input;
using Silk.NET.Input;
namespace AcDream.UI.Abstractions.Tests.Input;
/// <summary>
/// K.3: <see cref="InputDispatcher.BeginCapture"/> is the modal-rebind
/// hook used by <c>SettingsPanel</c>. While capture is active, the next
/// non-modifier chord is reported via the supplied callback and the
/// dispatcher does NOT fire normal action events for that chord. Esc
/// cancels capture (callback receives a sentinel <c>default</c> chord).
/// Modifier-only key transitions don't complete capture — the user can
/// dial in Shift / Ctrl / Alt before pressing the trigger key.
/// </summary>
public class InputDispatcherCaptureTests
{
private static (InputDispatcher dispatcher, FakeKeyboardSource kb, FakeMouseSource mouse, KeyBindings bindings, List<(InputAction, ActivationType)> fired)
Build()
{
var kb = new FakeKeyboardSource();
var mouse = new FakeMouseSource();
var bindings = new KeyBindings();
var dispatcher = new InputDispatcher(kb, mouse, bindings);
var fired = new List<(InputAction, ActivationType)>();
dispatcher.Fired += (a, t) => fired.Add((a, t));
return (dispatcher, kb, mouse, bindings, fired);
}
[Fact]
public void BeginCapture_consumes_next_chord_without_firing_actions()
{
var (dispatcher, kb, _, bindings, fired) = Build();
bindings.Add(new Binding(new KeyChord(Key.W, ModifierMask.None), InputAction.MovementForward));
KeyChord? captured = null;
dispatcher.BeginCapture(c => captured = c);
kb.EmitKeyDown(Key.W, ModifierMask.None);
Assert.NotNull(captured);
Assert.Equal(new KeyChord(Key.W, ModifierMask.None), captured!.Value);
// Action did NOT fire — capture suppressed it.
Assert.Empty(fired);
// Capture state cleared after capturing one chord.
Assert.False(dispatcher.IsCapturing);
}
[Fact]
public void BeginCapture_Escape_cancels_with_default_chord()
{
var (dispatcher, kb, _, _, fired) = Build();
KeyChord? captured = null;
bool calledBack = false;
dispatcher.BeginCapture(c => { captured = c; calledBack = true; });
kb.EmitKeyDown(Key.Escape, ModifierMask.None);
Assert.True(calledBack);
Assert.Equal(default(KeyChord), captured!.Value);
Assert.Empty(fired);
Assert.False(dispatcher.IsCapturing);
}
[Fact]
public void BeginCapture_modifier_only_keys_dont_complete_capture()
{
var (dispatcher, kb, _, _, _) = Build();
bool calledBack = false;
dispatcher.BeginCapture(_ => calledBack = true);
// Press Shift alone — should NOT complete capture.
kb.EmitKeyDown(Key.ShiftLeft, ModifierMask.Shift);
Assert.False(calledBack);
Assert.True(dispatcher.IsCapturing);
kb.EmitKeyDown(Key.ControlLeft, ModifierMask.Shift | ModifierMask.Ctrl);
Assert.False(calledBack);
Assert.True(dispatcher.IsCapturing);
// Press a non-modifier key — capture completes with full mods.
KeyChord? captured = null;
dispatcher.CancelCapture(); // reset for clean test
bool fireCalled = false;
dispatcher.BeginCapture(c => { captured = c; fireCalled = true; });
kb.EmitKeyDown(Key.A, ModifierMask.Shift | ModifierMask.Ctrl);
Assert.True(fireCalled);
Assert.Equal(new KeyChord(Key.A, ModifierMask.Shift | ModifierMask.Ctrl), captured!.Value);
}
[Fact]
public void BeginCapture_completes_with_modifier_state()
{
var (dispatcher, kb, _, _, _) = Build();
KeyChord? captured = null;
dispatcher.BeginCapture(c => captured = c);
kb.EmitKeyDown(Key.A, ModifierMask.Ctrl);
Assert.Equal(new KeyChord(Key.A, ModifierMask.Ctrl), captured!.Value);
}
[Fact]
public void CancelCapture_invokes_callback_with_default_chord_and_clears_state()
{
var (dispatcher, _, _, _, _) = Build();
KeyChord? captured = null;
bool calledBack = false;
dispatcher.BeginCapture(c => { captured = c; calledBack = true; });
Assert.True(dispatcher.IsCapturing);
dispatcher.CancelCapture();
Assert.True(calledBack);
Assert.Equal(default(KeyChord), captured!.Value);
Assert.False(dispatcher.IsCapturing);
}
[Fact]
public void IsCapturing_is_false_initially()
{
var (dispatcher, _, _, _, _) = Build();
Assert.False(dispatcher.IsCapturing);
}
[Fact]
public void SetBindings_replaces_active_bindings_table()
{
var (dispatcher, kb, _, _, fired) = Build();
// Original table empty — pressing W fires nothing.
kb.EmitKeyDown(Key.W, ModifierMask.None);
Assert.Empty(fired);
// Swap in a new table that binds W → MovementForward.
var swapped = new KeyBindings();
swapped.Add(new Binding(new KeyChord(Key.W, ModifierMask.None), InputAction.MovementForward));
dispatcher.SetBindings(swapped);
kb.EmitKeyDown(Key.W, ModifierMask.None);
Assert.Single(fired);
Assert.Equal((InputAction.MovementForward, ActivationType.Press), fired[0]);
}
}