From 6636e50c2a4281e7f3e14e66b920337f5a6fe7f7 Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 18 Jun 2026 22:47:24 +0200 Subject: [PATCH] =?UTF-8?q?feat(D.5.3a):=20selected-object=20meter=20?= =?UTF-8?q?=E2=80=94=20Health=20bar=20+=20name=20on=20the=20action=20bar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Port of gmToolbarUI::HandleSelectionChanged (acclient_2013_pseudo_c.txt:198635). When the player selects a world object the action bar's bottom strip shows the object name + (for player/pet/attackable targets) a live Health meter; deselect clears it. Mana (#140) + stack slider deferred. - SelectedObjectController (new): clear-then-populate on selection change; sets name (UiText child, VitalsController pattern), overlay state (ObjectSelected / StackedItemSelected via UiDatElement.ActiveState), shows the health meter and sends QueryHealth for health targets. Subscribes via a delegate seam (no GameWindow coupling). - GameWindow: _selectedGuid field -> SelectedGuid property + SelectionChanged event (fires on actual change only); 3 write sites converted, reads untouched. All selection-write paths (LMB pick, Tab/Q, despawn-clear via Tick()) run on the render thread, so the event-driven UI mutation is single-threaded. - WorldSession.SendQueryHealth (0x01BF) — wraps SocialActions.BuildQueryHealth. - DatWidgetFactory.BuildMeter: handle the single-image toolbar meter shape (back-track on the element's own DirectState, fill on one Type-3 child). The sprites go in the TILE slot (DrawMode=Normal tiles to full bar geometry per UIElement_Meter::DrawChildren) — a left-cap assignment would gap/clamp a sub-140px sprite. Vitals 3-slice path unchanged. - ToolbarController.HiddenIds: A1 (health) now owned by SelectedObjectController; A2 (mana) + A4 (stack) stay hidden (deferred) so their dat back-tracks don't render as stray empty bars. Adversarial Opus review found + fixed: the mana-meter orphan (A2 left unhidden) and the meter tile-vs-cap render bug (C1). Divergence rows AP-46 (health gate approximation: IsLiveCreatureTarget vs IsPlayer||pet||attackable) + AP-47 (meter shown on select vs on UpdateHealth reply). Spec §5 corrected. Build + full test suite green (2,684 passed / 4 skipped). Health meter render fidelity (full-width fill + fraction mapping) pending the user's visual gate. Co-Authored-By: Claude Opus 4.8 (1M context) --- docs/ISSUES.md | 3 +- .../retail-divergence-register.md | 2 + ...06-18-d53a-selected-object-meter-design.md | 9 +- src/AcDream.App/Rendering/GameWindow.cs | 36 +- src/AcDream.App/UI/Layout/DatWidgetFactory.cs | 84 +++- .../UI/Layout/SelectedObjectController.cs | 208 ++++++++ .../UI/Layout/ToolbarController.cs | 11 +- src/AcDream.Core.Net/WorldSession.cs | 12 + .../UI/Layout/DatWidgetFactoryTests.cs | 41 ++ .../Layout/SelectedObjectControllerTests.cs | 461 ++++++++++++++++++ .../WorldSessionCombatTests.cs | 14 + 11 files changed, 851 insertions(+), 30 deletions(-) create mode 100644 src/AcDream.App/UI/Layout/SelectedObjectController.cs create mode 100644 tests/AcDream.App.Tests/UI/Layout/SelectedObjectControllerTests.cs diff --git a/docs/ISSUES.md b/docs/ISSUES.md index 2982137e..d59ad3c6 100644 --- a/docs/ISSUES.md +++ b/docs/ISSUES.md @@ -48,7 +48,7 @@ Copy this block when adding a new issue: ## #140 — Toolbar interactivity — selected-object display -**Status:** OPEN +**Status:** IN PROGRESS (D.5.3a — health + name landed, pending visual gate; mana + stack slider still deferred) **Severity:** MEDIUM **Filed:** 2026-06-17 **Component:** ui — D.5 toolbar / selection @@ -56,6 +56,7 @@ Copy this block when adding a new issue: **Description:** The action bar (D.5.1) is the retail "selected object" display. Wire the B.4 WorldPicker/selection state to the toolbar's currently-hidden elements: the two meters 0x100001A1 (selected-object Health) / 0x100001A2 (selected-object Mana) + the stack slider 0x100001A4 + the object-name line, so the bar shows what the player has selected in the world. Click-to-use + the peace/war stance indicator already shipped in D.5.1. Promote to roadmap D.5.3 (already listed there). **Root cause / status:** The selection-state wire was deferred out of D.5.1 scope; the meter/slider elements are present in LayoutDesc 0x21000016 but hidden (no backing data). D.5.3 is the planned port. +- **D.5.3a (2026-06-18):** the Health meter (0x100001A1) + the object-name line (0x1000019F) + the overlay state (0x100001A0) are wired via `SelectedObjectController` (port of `gmToolbarUI::HandleSelectionChanged`); `SelectionChanged` event on `GameWindow`; `QueryHealth (0x01BF)` sent on select. Spec/plan: `docs/superpowers/specs|plans/2026-06-18-d53a-*`. **Still deferred:** the Mana meter (0x100001A2 — owned-item-only; no remote-target mana path yet) and the stack entry/slider (0x100001A3/A4 — stack-split UI). Divergence rows AP-46/AP-47. Awaiting the visual gate before closing the health half. **Files:** `src/AcDream.App/UI/Layout/ToolbarController.cs` + the selection/WorldPicker state (see `claude-memory/project_interaction_pipeline.md`). diff --git a/docs/architecture/retail-divergence-register.md b/docs/architecture/retail-divergence-register.md index d30b0b78..c2267360 100644 --- a/docs/architecture/retail-divergence-register.md +++ b/docs/architecture/retail-divergence-register.md @@ -144,6 +144,8 @@ accepted-divergence entries (#96, #49, #50). | AP-41 | Scrollbar thumb 3-slice cap fallback only: single-tile draw (`0x06004C63`) used only when `ThumbTopSprite`/`ThumbBotSprite` are unset; the chat controller passes all three cap ids so the 3-slice path is drawn in practice | `src/AcDream.App/UI/UiScrollbar.cs:35` | The fallback single-tile path is unreachable when caps are bound (chat controller always sets them); the 3-slice path is the active code path | Only if a future caller omits the cap ids will the fallback fire — no visual regression in the chat window | `UIElement_Scrollbar::UpdateLayout @0x4710d0`; cap sprites `0x06004C60` (top) + `0x06004C66` (bottom) from base layout `0x2100003E` | | AP-42 | `UiMenu` item model is flat (label + opaque payload, single-level popup); retail `UIElement_Menu::MakePopup @0x46d310` supports hierarchical nested submenus via recursive popup chain | `src/AcDream.App/UI/UiMenu.cs` | The chat talk-focus menu is single-level (14 rows, 2 columns, no submenu); hierarchy is latent and unreachable through the chat window — no behavioral difference in the current usage | A future menu with nested submenus would render flat (only the top-level items drawn, no drill-down) | `UIElement_Menu::MakePopup` @0x46d310 | | AP-45 | `PublicUpdatePropertyInt (0x02CE)` sequence byte parsed-past but not honored; last update wins (no freshness check against sequence number) | `src/AcDream.Core.Net/Messages/PublicUpdatePropertyInt.cs` | Loopback ACE rarely reorders; latest-wins matches `PrivateUpdateVital`/`UpdatePosition`'s existing non-sequence behavior. Sequence tracking added when needed alongside TS-26. | A reordered 0x02CE on a real network could apply a stale UiEffects value — item icon temporarily shows the wrong effect state, corrected on next update | `PublicUpdatePropertyInt` sequence byte (ACE GameMessagePublicUpdatePropertyInt) | +| AP-46 | Health-meter gate approximation: retail shows the health meter for `IsPlayer() || pet_owner || ObjectIsAttackable()`; acdream uses `IsLiveCreatureTarget` (the `ItemType.Creature` flag) | `src/AcDream.App/UI/Layout/SelectedObjectController.cs` (`HandleSelectionChanged` analogue) | `IsLiveCreatureTarget` is already wired for Tab/Q combat-target gating and is the correct proxy for M1.5 scope (no pet system, no PK); the only practical gap is a friendly non-attackable NPC, which is rare in the ACE dev loop | A friendly (non-attackable) NPC shows a health meter where retail would show name+overlay only — false meter on non-combat NPCs | `gmToolbarUI::HandleSelectionChanged` acclient_2013_pseudo_c.txt:198754 | +| AP-47 | Meter-visible timing: acdream shows the health meter immediately on select; retail shows it from `RecvNotice_UpdateObjectHealth` when the queried value arrives | `src/AcDream.App/UI/Layout/SelectedObjectController.cs` (`OnSelectionChanged`) | Avoids a one-round-trip blank-then-pop; the fill polls `GetHealthPercent` which returns 1.0 until the reply — visually indistinguishable for a full-HP target and self-corrects within one RTT for a damaged target | A freshly-selected off-screen-damaged target reads full for one server round-trip before the `QueryHealth` reply lands | `gmToolbarUI::HandleSelectionChanged` acclient_2013_pseudo_c.txt:198757 | --- diff --git a/docs/superpowers/specs/2026-06-18-d53a-selected-object-meter-design.md b/docs/superpowers/specs/2026-06-18-d53a-selected-object-meter-design.md index 1c22d391..482670b4 100644 --- a/docs/superpowers/specs/2026-06-18-d53a-selected-object-meter-design.md +++ b/docs/superpowers/specs/2026-06-18-d53a-selected-object-meter-design.md @@ -223,9 +223,12 @@ AcDream.App.UI.Layout.SelectedObjectController.Bind( datFont: vitalsDatFont); ``` -Also: remove `0x100001A1` and `0x100001A2` from `ToolbarController.HiddenIds` (single-owner: the -selected-object meters are now owned by `SelectedObjectController`); `0x100001A4` (stack slider) stays -in `HiddenIds` (deferred). Convert the `_selectedGuid` field → the `SelectedGuid` property (unit 1). +Also: remove **only** `0x100001A1` from `ToolbarController.HiddenIds` — the health meter is now owned +by `SelectedObjectController` (it hides A1 at bind, shows on a health-target select). `0x100001A2` +(mana, deferred #140) and `0x100001A4` (stack slider, deferred) **stay** in `HiddenIds`: they have no +controller yet, so they must stay hidden or their dat back-track sprites render as stray empty bars. +(`HiddenIds = { 0x100001A2, 0x100001A4 }`.) Convert the `_selectedGuid` field → the `SelectedGuid` +property (unit 1). ## Data flow diff --git a/src/AcDream.App/Rendering/GameWindow.cs b/src/AcDream.App/Rendering/GameWindow.cs index 5991c83b..b8f65d99 100644 --- a/src/AcDream.App/Rendering/GameWindow.cs +++ b/src/AcDream.App/Rendering/GameWindow.cs @@ -619,6 +619,8 @@ public sealed class GameWindow : IDisposable private AcDream.App.UI.UiHost? _uiHost; // Phase D.5.1 — toolbar controller (kept for lifetime clarity; mirrors _chatWindowController pattern). private AcDream.App.UI.Layout.ToolbarController? _toolbarController; + // Phase D.5.3a — selected-object strip controller (name, overlay state, health meter). + private AcDream.App.UI.Layout.SelectedObjectController? _selectedObjectController; // Phase D.2b Task 9 — plugin UI registrations buffered before OnLoad; drained in OnLoad. private readonly AcDream.App.Plugins.BufferedUiRegistry? _uiRegistry; // Phase I.2: ImGui debug panel ViewModel. Lives for as long as @@ -846,6 +848,21 @@ public sealed class GameWindow : IDisposable private readonly Dictionary _lastSpawnByGuid = new(); // Current selection: written by Q-cycle (combat) and LMB click (interact); cleared on entity despawn. private uint? _selectedGuid; + /// Fires when the selected world object changes (retail gmToolbarUI selection-change event, + /// acclient_2013_pseudo_c.txt:198635). Private: only the internal SelectedObjectController subscribes. + private event Action? SelectionChanged; + /// Currently-selected world object guid. The setter fires only on + /// an actual change (dedup), so all writes go through here; reads may use the field directly. + private uint? SelectedGuid + { + get => _selectedGuid; + set + { + if (_selectedGuid == value) return; + _selectedGuid = value; + SelectionChanged?.Invoke(value); + } + } // B.6/B.7 (2026-05-16): pending close-range action that will be fired // once the local auto-walk overlay reports arrival (body has finished @@ -2003,6 +2020,19 @@ public sealed class GameWindow : IDisposable warDigits: toolbarWarDigits, emptyDigits: toolbarEmptyDigits); + // Phase D.5.3a — selected-object strip (name, overlay state, health meter). + // Analogue of retail gmToolbarUI::HandleSelectionChanged + // (acclient_2013_pseudo_c.txt:198635). + _selectedObjectController = AcDream.App.UI.Layout.SelectedObjectController.Bind( + toolbarLayout, + subscribeSelectionChanged: h => SelectionChanged += h, + isHealthTarget: IsLiveCreatureTarget, + name: g => Objects.Get(g)?.Name, + healthPercent: g => Combat.GetHealthPercent(g), + stackSize: g => (uint)(Objects.Get(g)?.StackSize ?? 0), + sendQueryHealth: g => _liveSession?.SendQueryHealth(g), + datFont: vitalsDatFont); + var toolbarRoot = toolbarLayout.Root; // Wrap the dat content in the universal 8-piece beveled window chrome — // the SAME UiNineSlicePanel used by the vitals and chat windows. The @@ -3708,7 +3738,7 @@ public sealed class GameWindow : IDisposable _entitiesByServerGuid.Remove(serverGuid); _lastSpawnByGuid.Remove(serverGuid); if (_selectedGuid == serverGuid) - _selectedGuid = null; + SelectedGuid = null; if (logDelete) _lightingSink?.UnregisterOwner(existingEntity.Id); @@ -11568,7 +11598,7 @@ public sealed class GameWindow : IDisposable if (picked is uint guid) { - _selectedGuid = guid; + SelectedGuid = guid; string label = DescribeLiveEntity(guid); Console.WriteLine($"[B.4b] pick guid=0x{guid:X8} name={label}"); // B.7 (2026-05-15): one-shot per-pick diagnostic so we can @@ -11958,7 +11988,7 @@ public sealed class GameWindow : IDisposable bestGuid = guid; } - _selectedGuid = bestGuid; + SelectedGuid = bestGuid; if (bestGuid is { } selected) { string label = DescribeLiveEntity(selected); diff --git a/src/AcDream.App/UI/Layout/DatWidgetFactory.cs b/src/AcDream.App/UI/Layout/DatWidgetFactory.cs index 0955aed4..287971fe 100644 --- a/src/AcDream.App/UI/Layout/DatWidgetFactory.cs +++ b/src/AcDream.App/UI/Layout/DatWidgetFactory.cs @@ -90,11 +90,11 @@ public static class DatWidgetFactory // ── Meter ──────────────────────────────────────────────────────────────── /// - /// Builds a and populates its six 3-slice sprite ids by - /// reading the meter's grandchild image elements (format doc §11). + /// Builds a and populates its sprite ids from the meter's + /// child/grandchild elements (format doc §11). Two shapes are handled: /// /// - /// Structure the importer produces for each meter (UIElement_Meter): + /// 3-slice shape (vitals meters — 2 Type-3 containers, each with 3 image grandchildren): /// /// meter (Type 7) /// ├── back-layer container (Type 3, lower ReadOrder — drawn first / behind) @@ -106,13 +106,27 @@ public static class DatWidgetFactory /// │ ├── center image (→ front-tile sprite) /// │ ├── right-cap image (→ front-right sprite) /// │ └── expand overlay (named "ShowDetail"/"HideDetail" only — NO DirectState — IGNORED) - /// └── text label (Type 0) (IGNORED — Fill/Label providers bound by VitalsController in Task 6) + /// └── text label (Type 0) (IGNORED — Fill/Label providers bound by VitalsController) + /// + /// + /// + /// + /// Single-image shape (toolbar selected-object meters 0x100001A1/0x100001A2 — 1 Type-3 + /// child, no grandchildren): the back-track sprite is on the meter element's own DirectState; + /// the fill sprite is on the single Type-3 child's own DirectState. Both are placed in the + /// TILE slot (Back/FrontTile) with left/right caps 0, so tiles + /// them across the full bar geometry (DrawMode=Normal) and clips the fill to the fraction. + /// (retail: gmToolbarUI::HandleSelectionChanged :198635, UIElement_Meter::Initialize :123328) + /// + /// meter (Type 7) [DirectState "" → back-track sprite, e.g. 0x0600193E] + /// └── fill container (Type 3) [DirectState "" → fill sprite, e.g. 0x0600193F] /// /// /// /// /// and are NOT set here. - /// They are bound to the live stat providers in Task 6 (VitalsController). + /// They are bound to the live stat providers by the controller (VitalsController / + /// SelectedObjectController). /// /// private static UiMeter BuildMeter(ElementInfo info, @@ -132,23 +146,53 @@ public static class DatWidgetFactory .OrderBy(c => c.ReadOrder) .ToList(); - if (containers.Count != 2) - Console.WriteLine($"[D.2b] meter 0x{info.Id:X8}: {containers.Count} Type-3 slice containers (expected 2) — bars may render as solid-color fallback."); - - if (containers.Count >= 1) - { - var (l, t, r) = SliceIds(containers[0]); - m.BackLeft = l; - m.BackTile = t; - m.BackRight = r; - } - if (containers.Count >= 2) { - var (l, t, r) = SliceIds(containers[1]); - m.FrontLeft = l; - m.FrontTile = t; - m.FrontRight = r; + // Vitals 3-slice shape: two Type-3 containers each holding 3 grandchild images + // (left-cap / center-tile / right-cap). Back is the lower ReadOrder; front is higher. + var (bl, bt, br) = SliceIds(containers[0]); + m.BackLeft = bl; + m.BackTile = bt; + m.BackRight = br; + + var (fl, ft, fr) = SliceIds(containers[1]); + m.FrontLeft = fl; + m.FrontTile = ft; + m.FrontRight = fr; + } + else if (containers.Count == 1) + { + // Single-image shape used by the toolbar selected-object meters + // (health 0x100001A1, mana 0x100001A2). + // - The back-track sprite lives on the meter ELEMENT's own DirectState ("" key of + // info.StateMedia) — not on any grandchild image. e.g. health back = 0x0600193E. + // - The fill sprite lives on the single Type-3 child's own DirectState ("" key of + // containers[0].StateMedia). e.g. health fill = 0x0600193F. + // The fill child has NO image grandchildren, so SliceIds would return all-zero — + // read the container's StateMedia directly instead. + // + // These go in the TILE slot (not the left-cap slot): the sprites are DrawMode=Normal, + // which retail renders as "tile at native width to fill the full element geometry" + // (format doc §6; the generic UiDatElement.OnDraw Normal path; UIElement_Meter:: + // DrawChildren :123574 clips the child's FULL 140px geometry box to the fill fraction). + // With the sprite on BackLeft instead, UiMeter.DrawHBar would clamp the cap to the + // sprite's NATIVE width (capL = min(nativeW, 140)) — leaving a right-side gap and + // mapping the fill fraction to native width when nativeW < 140. The tile slot makes + // midW = full bar width, so the back tiles across all 140px and the front clips to + // 140*fraction correctly for any native sprite width (left/right caps unused = 0). + // (retail: gmToolbarUI::HandleSelectionChanged :198635 / UIElement_Meter::DrawChildren :123574) + m.BackLeft = 0; + m.BackTile = info.StateMedia.TryGetValue("", out var bm) ? bm.File : 0u; + m.BackRight = 0; + + m.FrontLeft = 0; + m.FrontTile = containers[0].StateMedia.TryGetValue("", out var fm) ? fm.File : 0u; + m.FrontRight = 0; + } + else + { + // Count == 0: no Type-3 containers at all — genuinely malformed meter dat. + Console.WriteLine($"[D.2b] meter 0x{info.Id:X8}: {containers.Count} Type-3 slice containers (expected 1 or 2) — bars may render as solid-color fallback."); } return m; diff --git a/src/AcDream.App/UI/Layout/SelectedObjectController.cs b/src/AcDream.App/UI/Layout/SelectedObjectController.cs new file mode 100644 index 00000000..1e37cddd --- /dev/null +++ b/src/AcDream.App/UI/Layout/SelectedObjectController.cs @@ -0,0 +1,208 @@ +using System; +using System.Numerics; +using AcDream.App.UI; + +namespace AcDream.App.UI.Layout; + +/// +/// Controller for the action bar's selected-object strip (ids 0x1000019F–0x100001A1). +/// Analogue of retail gmToolbarUI::HandleSelectionChanged +/// (docs/research/named-retail/acclient_2013_pseudo_c.txt:198635). +/// +/// +/// On selection change: clears the strip (name, overlay state, health meter), then +/// if a guid is provided it sets the name, puts the overlay field into the appropriate +/// state ("ObjectSelected" or "StackedItemSelected"), and for health-bearing targets +/// shows the health meter and sends a QueryHealth (0x01BF) request. +/// +/// +/// +/// Divergence — meter-visible timing. +/// Retail makes the health meter visible from RecvNotice_UpdateObjectHealth +/// (when the queried value arrives, cite HandleSelectionChanged:198757). +/// acdream shows it immediately on select (fill polls +/// which returns 1.0 until the reply lands). Recorded in the divergence register. +/// +/// +/// +/// Divergence — health-target gate approximation. +/// Retail gates on IsPlayer() || pet_owner || ObjectIsAttackable() +/// (cite HandleSelectionChanged:198754). acdream uses IsLiveCreatureTarget +/// (the ItemType.Creature flag). Recorded in the divergence register. +/// +/// +public sealed class SelectedObjectController +{ + // ── Element ids (toolbar LayoutDesc 0x21000016) ───────────────────────── + /// Selected-object name element id. + public const uint NameId = 0x1000019F; + /// Selected-object overlay field element id (states: ObjectSelected / StackedItemSelected). + public const uint OverlayId = 0x100001A0; + /// Selected-object health meter element id. + public const uint HealthMeterId = 0x100001A1; + + // ── Found elements (any may be null for partial/test layouts) ─────────── + private readonly UiElement? _name; + private readonly UiDatElement? _overlay; + private readonly UiMeter? _healthMeter; + + // ── Captured delegates ─────────────────────────────────────────────────── + private readonly Func _isHealthTarget; + private readonly Func _name_; + private readonly Func _healthPercent; + private readonly Func _stackSize; + private readonly Action _sendQueryHealth; + + // ── Live state (read by closures on the per-frame draw path) ──────────── + private uint? _current; + private string? _currentName; + + /// White label color for the name line. + private static readonly Vector4 NameColor = new(1f, 1f, 1f, 1f); + + private SelectedObjectController( + ImportedLayout layout, + Action> subscribeSelectionChanged, + Func isHealthTarget, + Func name, + Func healthPercent, + Func stackSize, + Action sendQueryHealth, + UiDatFont? datFont) + { + _isHealthTarget = isHealthTarget; + _name_ = name; + _healthPercent = healthPercent; + _stackSize = stackSize; + _sendQueryHealth = sendQueryHealth; + + // Find elements — silently skip absent ones (partial/test layouts). + _name = layout.FindElement(NameId); + _overlay = layout.FindElement(OverlayId) as UiDatElement; + _healthMeter = layout.FindElement(HealthMeterId) as UiMeter; + + // This controller owns the health meter's initial-hidden state. + if (_healthMeter is not null) + { + _healthMeter.Visible = false; + // Fill polls live: _current holds the currently-selected guid (or null). + // Returns 0f when nothing is selected (empty bar), healthPercent(g) otherwise. + _healthMeter.Fill = () => _current is uint g ? _healthPercent(g) : (float?)0f; + } + + // Attach a centered UiText child to the name element for the object name display. + // Mirrors VitalsController.BindMeter's number attach (same decoration style). + if (_name is not null) + { + var label = new UiText + { + Left = 0f, + Top = 0f, + Width = _name.Width, + Height = _name.Height, + Anchors = AnchorEdges.Left | AnchorEdges.Top | AnchorEdges.Right | AnchorEdges.Bottom, + Centered = true, + DatFont = datFont, + ClickThrough = true, + AcceptsFocus = false, + IsEditControl = false, + CapturesPointerDrag = false, + LinesProvider = () => + { + // Returns a single white line when a name is available; empty otherwise. + var n = _currentName; + return string.IsNullOrEmpty(n) + ? Array.Empty() + : new[] { new UiText.Line(n, NameColor) }; + }, + }; + _name.AddChild(label); + } + + // Register the handler LAST so the initial state is fully set up first. + subscribeSelectionChanged(OnSelectionChanged); + } + + /// + /// Create and bind a to . + /// Port of retail gmToolbarUI::HandleSelectionChanged + /// (acclient_2013_pseudo_c.txt:198635). + /// + /// Imported toolbar layout (LayoutDesc 0x21000016). + /// + /// Called once with the controller's handler. + /// Typical host: h => SelectionChanged += h — keeps the controller + /// decoupled from GameWindow. + /// + /// + /// Returns true for guids that should show a health meter (proxy for retail's + /// IsPlayer() || pet_owner || ObjectIsAttackable()). + /// + /// Returns the display name for a given guid (or null if unknown). + /// Returns the health fill fraction [0..1] for a given guid. + /// Returns the stack size for a given guid (0 or 1 = non-stacked). + /// + /// Sends retail QueryHealth (0x01BF); server replies with UpdateHealth (0x01C0). + /// May be a no-op when offline. + /// + /// Dat font for the name label; null = debug bitmap font fallback. + public static SelectedObjectController Bind( + ImportedLayout layout, + Action> subscribeSelectionChanged, + Func isHealthTarget, + Func name, + Func healthPercent, + Func stackSize, + Action sendQueryHealth, + UiDatFont? datFont) + { + return new SelectedObjectController( + layout, subscribeSelectionChanged, + isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, datFont); + } + + /// + /// Port of gmToolbarUI::HandleSelectionChanged + /// (acclient_2013_pseudo_c.txt:198635): + /// clear-then-populate the selected-object strip on any selection change. + /// Registered via subscribeSelectionChanged at bind time; called by + /// GameWindow.SelectionChanged and by the despawn-clear path. + /// + public void OnSelectionChanged(uint? guid) + { + // ── 1. Clear first (retail: UIElement_Text::SetText + m_pSelObjectField->SetState(0) + // + SetVisible(0) on the health meter). ────────────────────────────────────── + if (_healthMeter is not null) _healthMeter.Visible = false; + if (_overlay is not null) _overlay.ActiveState = ""; + _currentName = null; + + // Update the backing current guid so the Fill closure reflects the new state. + _current = guid; + + // ── 2. Selection == null → strip stays cleared, done. ─────────────────────────── + if (guid is null) return; + + uint g = guid.Value; + + // ── 3. Selection != null — populate the strip. ────────────────────────────────── + + // Name (displayed via the UiText child's LinesProvider reading _currentName). + _currentName = _name_(g); + + // Overlay state: "StackedItemSelected" for stacked items, "ObjectSelected" otherwise. + // Retail ref: m_pSelObjectField->SetState(0x1000000b) = "ObjectSelected" + // (acclient_2013_pseudo_c.txt:198754). Stack sprite id 0x06004CF4 confirmed in toolbar dump. + if (_overlay is not null) + _overlay.ActiveState = _stackSize(g) > 1u ? "StackedItemSelected" : "ObjectSelected"; + + // Health meter: visible + QueryHealth for health-bearing targets. + // Divergence: retail shows the meter only when RecvNotice_UpdateObjectHealth arrives; + // acdream shows it immediately (fill reads GetHealthPercent = 1.0 until the reply). + // Retail ref: CM_Combat::Event_QueryHealth (acclient_2013_pseudo_c.txt:198757). + if (_isHealthTarget(g)) + { + if (_healthMeter is not null) _healthMeter.Visible = true; + _sendQueryHealth(g); + } + } +} diff --git a/src/AcDream.App/UI/Layout/ToolbarController.cs b/src/AcDream.App/UI/Layout/ToolbarController.cs index 0cfd9d4c..91f03fad 100644 --- a/src/AcDream.App/UI/Layout/ToolbarController.cs +++ b/src/AcDream.App/UI/Layout/ToolbarController.cs @@ -35,10 +35,15 @@ public sealed class ToolbarController 0x100006BC, 0x100006BD, 0x100006BE, 0x100006BF, }; - // Elements hidden by default in retail gmToolbarUI::PostInit: the selected-object - // vitals meters (health/stamina/mana bars that track your target) and the stack slider. + // Elements hidden by default in retail gmToolbarUI::PostInit. // Ids confirmed from the toolbar LayoutDesc dump. - private static readonly uint[] HiddenIds = { 0x100001A1, 0x100001A2, 0x100001A4 }; + // 0x100001A1 (health meter) is now OWNED by SelectedObjectController (D.5.3a) — + // it hides A1 at bind and shows it on a health-target selection, so A1 is removed + // from here to avoid double-ownership. 0x100001A2 (mana meter) and 0x100001A4 + // (stack slider) are DEFERRED features (mana #140, stack-split UI) with no controller + // yet, so they stay hidden here — otherwise their dat back-track sprites render as + // stray empty bars on the toolbar. + private static readonly uint[] HiddenIds = { 0x100001A2, 0x100001A4 }; // Four mutually-exclusive combat-mode indicator elements — exactly one visible at a time. // Index 0 = NonCombat (peace), 1 = Melee, 2 = Missile, 3 = Magic. diff --git a/src/AcDream.Core.Net/WorldSession.cs b/src/AcDream.Core.Net/WorldSession.cs index 2118ca75..6508084d 100644 --- a/src/AcDream.Core.Net/WorldSession.cs +++ b/src/AcDream.Core.Net/WorldSession.cs @@ -1140,6 +1140,18 @@ public sealed class WorldSession : IDisposable SendGameAction(body); } + /// Send retail QueryHealth (0x01BF). Server replies UpdateHealth (0x01C0). + /// + /// Retail anchor: CM_Combat::Event_QueryHealth / gmToolbarUI::HandleSelectionChanged:198635 + /// (docs/research/named-retail/acclient_2013_pseudo_c.txt). + /// + public void SendQueryHealth(uint targetGuid) + { + uint seq = NextGameActionSequence(); + byte[] body = SocialActions.BuildQueryHealth(seq, targetGuid); + SendGameAction(body); + } + /// Send retail TargetedMeleeAttack (0x0008). public void SendMeleeAttack(uint targetGuid, AttackHeight attackHeight, float powerLevel) { diff --git a/tests/AcDream.App.Tests/UI/Layout/DatWidgetFactoryTests.cs b/tests/AcDream.App.Tests/UI/Layout/DatWidgetFactoryTests.cs index d5079b62..67a3a10a 100644 --- a/tests/AcDream.App.Tests/UI/Layout/DatWidgetFactoryTests.cs +++ b/tests/AcDream.App.Tests/UI/Layout/DatWidgetFactoryTests.cs @@ -136,6 +136,47 @@ public class DatWidgetFactoryTests Assert.IsType(w); } + // ── Test M1: Single-image meter (toolbar selected-object meters) ──────── + // + // The toolbar health/mana meters (0x100001A1 / 0x100001A2) use a DIFFERENT + // shape from the vitals 3-slice meters: the back-track sprite lives on the + // meter ELEMENT's own DirectState ("" key), and there is exactly ONE Type-3 + // child whose own DirectState ("" key) carries the fill sprite. That child + // has no image grandchildren, so SliceIds would return all-zero — the new + // Count==1 branch reads the StateMedia entries directly instead. + // The sprites go in the TILE slot (Back/FrontTile), NOT the cap slot: DrawMode=Normal + // tiles at native width across the full bar geometry (UIElement_Meter::DrawChildren), + // so the back spans all 140px and the fill clips to 140*fraction for any native width. + // Back/FrontLeft + Back/FrontRight must be 0 (no caps on a single-image bar). + + [Fact] + public void BuildMeter_SingleImageShape_ReadsDirectStateFromElementAndFillChild() + { + const uint BackFile = 0x0600193Eu; // health back-track (from toolbar dump) + const uint FillFile = 0x0600193Fu; // health fill (from toolbar dump) + + // Meter element: Type 7, own DirectState = back-track sprite. + var meter = new ElementInfo { Type = 7, Id = 0x100001A1u, Width = 140, Height = 31 }; + meter.StateMedia[""] = (BackFile, 1); + + // Single Type-3 fill container: own DirectState = fill sprite, no grandchildren. + var fillContainer = new ElementInfo { Type = 3, ReadOrder = 1 }; + fillContainer.StateMedia[""] = (FillFile, 1); + meter.Children.Add(fillContainer); + + var e = DatWidgetFactory.Create(meter, NoTex, null); + + var m = Assert.IsType(e); + // Back-track on the meter element's own DirectState, fill on the single child — + // both in the TILE slot so they tile across the full 140px bar (DrawMode=Normal). + Assert.Equal(BackFile, m.BackTile); + Assert.Equal(0u, m.BackLeft); + Assert.Equal(0u, m.BackRight); + Assert.Equal(FillFile, m.FrontTile); + Assert.Equal(0u, m.FrontLeft); + Assert.Equal(0u, m.FrontRight); + } + // ── Test 6: Meter slice extraction (the important one) ─────────────────── /// diff --git a/tests/AcDream.App.Tests/UI/Layout/SelectedObjectControllerTests.cs b/tests/AcDream.App.Tests/UI/Layout/SelectedObjectControllerTests.cs new file mode 100644 index 00000000..fd6a2a93 --- /dev/null +++ b/tests/AcDream.App.Tests/UI/Layout/SelectedObjectControllerTests.cs @@ -0,0 +1,461 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using AcDream.App.UI; +using AcDream.App.UI.Layout; +using Xunit; + +namespace AcDream.App.Tests.UI.Layout; + +/// +/// Unit tests for — the +/// gmToolbarUI::HandleSelectionChanged analogue +/// (acclient_2013_pseudo_c.txt:198635). +/// +/// +/// Layout construction mirrors : build a minimal +/// from a root + a +/// keyed by element id. Elements are constructed +/// directly (no importer / no dat / no GL) so tests are pure in-process. +/// +/// +public class SelectedObjectControllerTests +{ + // ── Shared layout ──────────────────────────────────────────────────────── + + /// + /// Build a minimal toolbar layout containing the three selected-object elements: + /// + /// 0x1000019F → a name container (100×20). + /// 0x100001A0 → a overlay with "ObjectSelected" + /// and "StackedItemSelected" states wired to distinct file ids. + /// 0x100001A1 → a health meter. + /// + /// Additional element ids can be added by the caller for edge-case tests. + /// + private static ( + ImportedLayout layout, + UiPanel nameEl, + UiDatElement overlayEl, + UiMeter healthMeterEl) + FakeLayout() + { + var dict = new Dictionary(); + var root = new UiPanel(); + + // Name element: a plain panel that will have a UiText child attached by the controller. + var nameEl = new UiPanel { Width = 100, Height = 20 }; + dict[SelectedObjectController.NameId] = nameEl; + root.AddChild(nameEl); + + // Overlay element: a UiDatElement with the two named states the controller switches between. + var overlayInfo = new ElementInfo + { + Id = SelectedObjectController.OverlayId, + Type = 3, // Type 3 = container/chrome — the overlay field's dat type + StateMedia = + { + [""] = (0x06000001u, 3), // DirectState (blank) + ["ObjectSelected"] = (0x06001937u, 3), // ObjectSelected sprite id from toolbar dump + ["StackedItemSelected"] = (0x06004CF4u, 3), // StackedItemSelected sprite id + }, + }; + var overlayEl = new UiDatElement(overlayInfo, _ => (0u, 0, 0)); + dict[SelectedObjectController.OverlayId] = overlayEl; + root.AddChild(overlayEl); + + // Health meter element. + var healthMeterEl = new UiMeter { Width = 100, Height = 10, Visible = true }; + dict[SelectedObjectController.HealthMeterId] = healthMeterEl; + root.AddChild(healthMeterEl); + + return (new ImportedLayout(root, dict), nameEl, overlayEl, healthMeterEl); + } + + // ── Recording delegates ────────────────────────────────────────────────── + + /// + /// Build a recording set of delegates. Name, health, stack are keyed by guid; + /// accumulates every guid passed to sendQueryHealth. + /// + private static ( + Action> subscribe, + Action fireSelection, + Func isHealthTarget, + Func name, + Func healthPercent, + Func stackSize, + Action sendQueryHealth, + List queryHealthCalls) + MakeDelegates( + Dictionary healthTargetMap, + Dictionary nameMap, + Dictionary healthMap, + Dictionary stackMap) + { + Action? registeredHandler = null; + var queryHealthCalls = new List(); + + Action> subscribe = h => registeredHandler = h; + Action fireSelection = guid => registeredHandler?.Invoke(guid); + + Func isHealthTarget = g => healthTargetMap.TryGetValue(g, out var v) && v; + Func name = g => nameMap.TryGetValue(g, out var v) ? v : null; + Func healthPercent = g => healthMap.TryGetValue(g, out var v) ? v : 1f; + Func stackSize = g => stackMap.TryGetValue(g, out var v) ? v : 0u; + Action sendQueryHealth = g => queryHealthCalls.Add(g); + + return (subscribe, fireSelection, isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, queryHealthCalls); + } + + // ── B1: Bind initialisation ────────────────────────────────────────────── + + /// + /// After Bind: + /// - the health meter is hidden (controller owns initial-hidden state). + /// - the name element has exactly one UiText child (the name label). + /// + [Fact] + public void Bind_healthMeterHidden_andNameTextChildAttached() + { + var (layout, nameEl, _, healthMeterEl) = FakeLayout(); + var (subscribe, _, isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, _) = + MakeDelegates( + healthTargetMap: new(), + nameMap: new(), + healthMap: new(), + stackMap: new()); + + SelectedObjectController.Bind(layout, subscribe, + isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, datFont: null); + + // Health meter must start hidden. + Assert.False(healthMeterEl.Visible, + "health meter must be Visible=false immediately after Bind"); + + // A UiText child should have been attached to the name element. + var textChild = nameEl.Children.OfType().SingleOrDefault(); + Assert.NotNull(textChild); + Assert.True(textChild!.Centered, "name UiText must be Centered"); + Assert.True(textChild.ClickThrough, "name UiText must be ClickThrough (non-interactive decoration)"); + Assert.False(textChild.AcceptsFocus, "AcceptsFocus must be false on name label"); + Assert.False(textChild.IsEditControl, "IsEditControl must be false on name label"); + Assert.False(textChild.CapturesPointerDrag, "CapturesPointerDrag must be false on name label"); + } + + /// + /// After Bind, the attached UiText's LinesProvider yields no lines (nothing selected yet). + /// + [Fact] + public void Bind_nameLinesProvider_yieldsEmpty_whenNothingSelected() + { + var (layout, nameEl, _, _) = FakeLayout(); + var (subscribe, _, isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, _) = + MakeDelegates(new(), new(), new(), new()); + + SelectedObjectController.Bind(layout, subscribe, + isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, datFont: null); + + var textChild = nameEl.Children.OfType().Single(); + var lines = textChild.LinesProvider(); + Assert.Empty(lines); + } + + // ── H1: Select a health target (creature) ─────────────────────────────── + + /// + /// Selecting a health target (stackSize=1, isHealthTarget=true): + /// - overlay ActiveState == "ObjectSelected" + /// - meter Visible == true + /// - sendQueryHealth invoked exactly once with the guid + /// - name LinesProvider yields a single white line with the expected name + /// + [Fact] + public void SelectHealthTarget_meterVisible_overlayObjectSelected_queryHealthFired() + { + const uint Guid = 0xAA01u; + const string ExpectedName = "Drudge Prowler"; + + var (layout, nameEl, overlayEl, healthMeterEl) = FakeLayout(); + var (subscribe, fireSelection, isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, queryHealthCalls) = + MakeDelegates( + healthTargetMap: new() { [Guid] = true }, + nameMap: new() { [Guid] = ExpectedName }, + healthMap: new() { [Guid] = 0.75f }, + stackMap: new() { [Guid] = 1u }); // stackSize=1 → ObjectSelected + + SelectedObjectController.Bind(layout, subscribe, + isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, datFont: null); + + // Fire the selection. + fireSelection(Guid); + + Assert.True(healthMeterEl.Visible, + "health meter must become Visible after selecting a health target"); + Assert.Equal("ObjectSelected", overlayEl.ActiveState); + Assert.Single(queryHealthCalls); + Assert.Equal(Guid, queryHealthCalls[0]); + + // Name label: the LinesProvider should yield the creature's name as a white line. + var textChild = nameEl.Children.OfType().Single(); + var lines = textChild.LinesProvider(); + Assert.Single(lines); + Assert.Equal(ExpectedName, lines[0].Text); + Assert.Equal(new Vector4(1f, 1f, 1f, 1f), lines[0].Color); + } + + // ── H2: Select a stacked item ──────────────────────────────────────────── + + /// + /// Selecting a stacked item (stackSize > 1): overlay ActiveState == "StackedItemSelected". + /// + [Fact] + public void SelectStackedItem_overlayStackedItemSelected() + { + const uint Guid = 0xBB02u; + + var (layout, _, overlayEl, healthMeterEl) = FakeLayout(); + var (subscribe, fireSelection, isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, _) = + MakeDelegates( + healthTargetMap: new() { [Guid] = false }, + nameMap: new() { [Guid] = "Heal Kits" }, + healthMap: new(), + stackMap: new() { [Guid] = 5u }); // stackSize > 1 + + SelectedObjectController.Bind(layout, subscribe, + isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, datFont: null); + + fireSelection(Guid); + + Assert.Equal("StackedItemSelected", overlayEl.ActiveState); + // Not a health target → meter stays hidden. + Assert.False(healthMeterEl.Visible); + } + + // ── H3: Select a non-health target (friendly NPC / scenery) ───────────── + + /// + /// Selecting a non-health target (isHealthTarget=false): + /// - meter stays hidden + /// - sendQueryHealth NOT invoked + /// - name and overlay are still set + /// + [Fact] + public void SelectNonHealthTarget_meterHidden_noQueryHealth_nameSet() + { + const uint Guid = 0xCC03u; + const string ExpectedName = "Town Crier"; + + var (layout, nameEl, overlayEl, healthMeterEl) = FakeLayout(); + var (subscribe, fireSelection, isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, queryHealthCalls) = + MakeDelegates( + healthTargetMap: new() { [Guid] = false }, + nameMap: new() { [Guid] = ExpectedName }, + healthMap: new(), + stackMap: new() { [Guid] = 0u }); // non-stack → ObjectSelected + + SelectedObjectController.Bind(layout, subscribe, + isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, datFont: null); + + fireSelection(Guid); + + Assert.False(healthMeterEl.Visible, "meter must stay hidden for a non-health target"); + Assert.Empty(queryHealthCalls); // sendQueryHealth must NOT be invoked for a non-health target + + // Overlay and name are still populated. + Assert.Equal("ObjectSelected", overlayEl.ActiveState); + var textChild = nameEl.Children.OfType().Single(); + var lines = textChild.LinesProvider(); + Assert.Single(lines); + Assert.Equal(ExpectedName, lines[0].Text); + } + + // ── H4: Deselect (null) ────────────────────────────────────────────────── + + /// + /// Selecting null clears the strip: + /// - meter Visible == false + /// - overlay ActiveState == "" + /// - name LinesProvider yields empty + /// + [Fact] + public void SelectNull_clearsStrip() + { + const uint Guid = 0xDD04u; + + var (layout, nameEl, overlayEl, healthMeterEl) = FakeLayout(); + var (subscribe, fireSelection, isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, _) = + MakeDelegates( + healthTargetMap: new() { [Guid] = true }, + nameMap: new() { [Guid] = "Wolf" }, + healthMap: new() { [Guid] = 0.5f }, + stackMap: new() { [Guid] = 0u }); + + SelectedObjectController.Bind(layout, subscribe, + isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, datFont: null); + + // First select something... + fireSelection(Guid); + Assert.True(healthMeterEl.Visible); + + // ...then deselect. + fireSelection(null); + + Assert.False(healthMeterEl.Visible, "meter must be hidden after deselect"); + Assert.Equal("", overlayEl.ActiveState); + + var textChild = nameEl.Children.OfType().Single(); + var lines = textChild.LinesProvider(); + Assert.Empty(lines); + } + + // ── H5: Clear → new selection (re-select) ──────────────────────────────── + + /// + /// Selecting one target then another should clear the first and apply the second. + /// + [Fact] + public void ReSelect_differentGuid_clearsFirstThenAppliesSecond() + { + const uint GuidA = 0xEE05u; + const uint GuidB = 0xFF06u; + + var (layout, nameEl, overlayEl, healthMeterEl) = FakeLayout(); + var (subscribe, fireSelection, isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, queryHealthCalls) = + MakeDelegates( + healthTargetMap: new() { [GuidA] = true, [GuidB] = false }, + nameMap: new() { [GuidA] = "Bandit", [GuidB] = "Chest" }, + healthMap: new() { [GuidA] = 1.0f }, + stackMap: new() { [GuidA] = 0u, [GuidB] = 0u }); + + SelectedObjectController.Bind(layout, subscribe, + isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, datFont: null); + + // Select A (health target). + fireSelection(GuidA); + Assert.True(healthMeterEl.Visible); + Assert.Single(queryHealthCalls); + + // Select B (non-health target) — must clear A's state and apply B. + fireSelection(GuidB); + + Assert.False(healthMeterEl.Visible, "health meter must be cleared when switching to non-health target"); + Assert.Equal("ObjectSelected", overlayEl.ActiveState); + // sendQueryHealth must NOT be called again (B is not a health target). + Assert.Single(queryHealthCalls); + + // Name should reflect B. + var textChild = nameEl.Children.OfType().Single(); + var lines = textChild.LinesProvider(); + Assert.Single(lines); + Assert.Equal("Chest", lines[0].Text); + } + + // ── H6: Partial layout (missing elements) ──────────────────────────────── + + /// + /// When elements are absent (partial layout), Bind does not throw and + /// OnSelectionChanged does not throw for any combination. + /// + [Fact] + public void PartialLayout_noElements_doesNotThrow() + { + // Empty layout — none of the three ids are present. + var root = new UiPanel(); + var layout = new ImportedLayout(root, new Dictionary()); + + Action? registeredHandler = null; + var queryHealthCalls = new List(); + + SelectedObjectController.Bind( + layout, + subscribeSelectionChanged: h => registeredHandler = h, + isHealthTarget: _ => true, + name: _ => "Something", + healthPercent: _ => 1f, + stackSize: _ => 0u, + sendQueryHealth: g => queryHealthCalls.Add(g), + datFont: null); + + Assert.NotNull(registeredHandler); + + // Firing selection / deselection on a partial layout must not throw. + var ex = Record.Exception(() => registeredHandler!.Invoke(0x12345678u)); + Assert.Null(ex); + + ex = Record.Exception(() => registeredHandler!.Invoke(null)); + Assert.Null(ex); + + // QueryHealth must still be called (the delegate doesn't depend on the meter element). + Assert.Single(queryHealthCalls); + Assert.Equal(0x12345678u, queryHealthCalls[0]); + } + + // ── H7: Fill closure reflects live healthPercent ───────────────────────── + + /// + /// The meter's Fill closure reads the current guid's health percent from the + /// healthPercent delegate on every poll — so if the server updates the + /// health between polls the fill reflects the new value without re-selecting. + /// + [Fact] + public void HealthMeterFill_reflectsLiveHealthPercent() + { + const uint Guid = 0xAA07u; + float currentHealth = 0.5f; + + var (layout, _, _, healthMeterEl) = FakeLayout(); + var (subscribe, fireSelection, isHealthTarget, name, _, stackSize, sendQueryHealth, _) = + MakeDelegates( + healthTargetMap: new() { [Guid] = true }, + nameMap: new() { [Guid] = "Arwic Banderling" }, + healthMap: new(), // not used here + stackMap: new() { [Guid] = 0u }); + + SelectedObjectController.Bind(layout, subscribe, + isHealthTarget, name, + healthPercent: _ => currentHealth, // reads the captured variable + stackSize, sendQueryHealth, datFont: null); + + fireSelection(Guid); + + // Fill should return the current health value. + Assert.Equal(0.5f, healthMeterEl.Fill()); + + // Simulate server updating health (as if UpdateHealth 0x01C0 arrived). + currentHealth = 0.25f; + Assert.Equal(0.25f, healthMeterEl.Fill()); + } + + // ── H8: Fill returns 0 when nothing is selected ────────────────────────── + + /// + /// After deselect, the meter Fill returns 0f (empty bar) rather than + /// the last selected target's health value. + /// + [Fact] + public void HealthMeterFill_returnsZero_whenNothingSelected() + { + const uint Guid = 0xAA08u; + + var (layout, _, _, healthMeterEl) = FakeLayout(); + var (subscribe, fireSelection, isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, _) = + MakeDelegates( + healthTargetMap: new() { [Guid] = true }, + nameMap: new() { [Guid] = "Spider" }, + healthMap: new() { [Guid] = 0.8f }, + stackMap: new() { [Guid] = 0u }); + + SelectedObjectController.Bind(layout, subscribe, + isHealthTarget, name, healthPercent, stackSize, sendQueryHealth, datFont: null); + + fireSelection(Guid); + Assert.Equal(0.8f, healthMeterEl.Fill()); // sanity check + + fireSelection(null); + // After deselect, Fill() must return 0f (or null coerced to 0f). + var fill = healthMeterEl.Fill(); + Assert.Equal(0f, fill ?? 0f); + } +} diff --git a/tests/AcDream.Core.Net.Tests/WorldSessionCombatTests.cs b/tests/AcDream.Core.Net.Tests/WorldSessionCombatTests.cs index 0bdd0bec..8ebf8ddb 100644 --- a/tests/AcDream.Core.Net.Tests/WorldSessionCombatTests.cs +++ b/tests/AcDream.Core.Net.Tests/WorldSessionCombatTests.cs @@ -74,4 +74,18 @@ public sealed class WorldSessionCombatTests Assert.NotNull(captured); Assert.Equal(AttackTargetRequest.BuildCancel(1), captured); } + + [Fact] + public void SendQueryHealth_UsesRetailQueryHealthBuilder() + { + // Retail anchor: CM_Combat::Event_QueryHealth / gmToolbarUI::HandleSelectionChanged:198635 + using var session = NewSession(); + byte[]? captured = null; + session.GameActionCapture = body => captured = body; + + session.SendQueryHealth(0x50000007u); + + Assert.NotNull(captured); + Assert.Equal(SocialActions.BuildQueryHealth(1, 0x50000007u), captured); + } }