TextRenderer, BitmapFont, DebugLineRenderer, and TextureCache's UI-texture
upload path (GetOrUploadRenderSurface/UploadRgba8) now issue every draw and
resource creation through the pinned IGpuDevice/IGpuFrame/IGpuPassEncoder
RHI contract instead of raw GL. This is the RHI's first real production
consumer - V0-V3 only established the contract, GL backend skeleton, and a
shader-dialect migration with no live GL exercise. TextRenderer owns one
IGpuPipeline (ui_text shader, straight-alpha blend, depth disabled) and
allocates a per-bucket ring each Flush; BitmapFont's atlas texture is
created and uploaded via device.CreateTexture/.Upload; DebugLineRenderer
mirrors the same one-pipeline-per-Flush shape for its line-list draws.
World-path TextureCache methods (GetOrUpload, the raw-GL layer-array
upload) are untouched - still legacy GL, still out of scope.
Frame lifecycle: GpuDeviceFrameLifetime (RenderFrameOrchestrator.cs) wraps
IGpuDevice.BeginFrame()/IGpuFrame.End() inside the existing
IRenderFrameLifetime bracket HostInputCameraCompositionPhase already opens
per callback, additively - no frame-graph restructuring. Ported renderers
reach the frame via ICurrentGpuFrameSource, a plain interface (not a
delegate field) so WorldSceneDiagnosticsController keeps passing its
existing "no stored window/delegate" architectural-conformance test.
Two real bugs surfaced by actually exercising the RHI against a live GL
context (nothing here was previously reachable before this slice):
- GlGpuDevice.BeginFrame() now resets the render-state cache every frame.
The cache assumes it is the sole writer of GL program/blend/depth/cull
state, which was true while it had zero real consumers, but every
still-legacy renderer (WbDrawDispatcher, terrain, particles, EnvCells)
mutates that same GL state directly and never informs the cache. Once a
legacy renderer ran between two RHI binds, the cache's belief about the
current GL program went stale, so a later BindPipeline(text shader)
skipped re-issuing glUseProgram and the following push-constant upload
threw GL_INVALID_OPERATION against whatever program was actually bound.
Reset() at the frame boundary is the same defensive move BeginPass
already makes after a forced clear (see its comment); it costs one
redundant state application on the frame's first bind.
- GL_MULTISAMPLE has no representation in the pinned contract. Added a
GL-backend-internal Multisample field to GlRenderStateSnapshot/Changes,
computed from GpuPipelineDescription.SampleCount at BindPipeline time -
mirrors how Vulkan bakes MSAA into the pipeline instead of a separate
toggle.
Collateral, scoped to keep the port real rather than a stub:
- GpuTextureSlot (Unassigned = uint.MaxValue, NOT 0) now flows through
every consumer of TextureCache.GetOrUploadRenderSurface/UploadRgba8 and
TextRenderer.DrawSprite - the entire retained UI layer, since a pervasive
Func<uint,(uint,int,int)> sprite-resolve delegate threads through nearly
every UI element/controller. Every prior `== 0` / `!= 0` "no texture"
check became `.IsAssigned` / `!.IsAssigned`; slot 0 is a real assigned
slot (the device's default white texture), so the old sentinel would
have produced live visual regressions if left in place.
- GpuTextureSlot/IGpuDevice/IGpuFrame are internal, so ~270 previously
public AcDream.App types that touched them (directly or transitively)
are now internal too - safe, since AcDream.App is an exe with no
external project references; only the two test projects consume it, via
InternalsVisibleTo. A handful of unrelated types the sweep caught
(ElementInfo/ImportedLayout's property-bag hierarchy, several enums used
as public [Theory] parameters, CursorFeedbackSnapshot's DragAcceptState)
were reverted back to public where making them internal would have
either cascaded into unrelated files or broken xUnit's public-member
discovery.
- ExternalViewportTextureBridge (new) registers the still-raw-GL FBO
color textures PrivateEntityViewportRenderer/PaperdollViewportRenderer
produce (V4g's scope) into the device's texture table for
UiViewport.TextureHandle, via a temporary
GlGpuDevice.RegisterExternalColorTexture escape hatch (internal, not
part of IGpuDevice) deleted when V4g ports those viewports.
- TextRenderGlStateScope.cs and its test deleted: the pipeline description
now bakes what it used to restore by hand.
- ResourceCleanupGroupTests/GlTextureOwnershipTests: the two source-text
conformance tests keyed to TextRenderer's old multi-resource
construction shape (Shader + per-flight FrameBufferSet array + white
texture + tracked VAO/VBO, all via ResourceCleanupGroup) no longer apply
- that shape is gone, replaced by one IGpuPipeline created through
IGpuDevice. The construction-order test is deleted; the checked-commit
texture-creation check now targets GlGpuTexture (which already used
the same GlResourceCommand.CreateName primitive before this slice).
Gates:
- dotnet build -c Release: 0 warnings, 0 errors (AcDream.App has
TreatWarningsAsErrors).
- dotnet test tests/AcDream.App.Tests -c Release: 3,840 passed / 3
skipped (was 3,843/3 entering this slice - net 3 fewer tests:
TextRendererFailureSafetyTests.cs deleted (2, tested the now-deleted
TextRenderGlStateScope) plus the one retired ResourceCleanupGroupTests
method). Full solution: 8,908 passed / 5 skipped across all nine test
projects.
- Offline pixel gate (tools/run-offline-pixel-gate.ps1, parent ec414d60
vs this commit): differing fraction 0.318% (1,791/563,200 compared
pixels), above the 0.001 threshold. Investigated pixel-by-pixel rather
than waved through: a diff heatmap plus 4x crops at the differing
clusters show zero differences anywhere in the retained UI, terrain,
scenery, or static meshes - every differing pixel sits on continuously-
animated ambient content (flying-insect sprites over the swamp, foliage
sparkle/dew glints) whose exact phase depends on elapsed wall-clock
time, the same category the gate's own sky-masking rationale already
documents and the campaign doc's coverage table explicitly excludes
("Not covered - particles"). Confirming evidence: two same-commit
captures at HEAD compare clean against each other (0.0025%), and two
same-commit captures at the parent compare clean against each other
(0.0044%) - only base-vs-head is consistently elevated, which is what
frame-pacing drift from genuinely new per-frame RHI work (BeginFrame,
ring resets, the render-state reset above) would produce against a
fixed wall-clock capture deadline, not a rendering defect. Recommend a
quick user visual check of this capture pair alongside the automated
result, matching how V2c's particle work was already handled in this
campaign (flagged for user visual confirmation rather than blocked on
an automated gate that cannot cover animated content).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
621 lines
23 KiB
C#
621 lines
23 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using AcDream.App.Spells;
|
|
using AcDream.Core.Combat;
|
|
using AcDream.Core.Items;
|
|
using AcDream.Core.Spells;
|
|
using AcDream.Core.Selection;
|
|
using AcDream.Runtime.Gameplay;
|
|
using AcDream.UI.Abstractions.Input;
|
|
|
|
namespace AcDream.App.UI.Layout;
|
|
|
|
/// <summary>
|
|
/// Retail gmSpellcastingUI binding for the authored magic page inside combat
|
|
/// LayoutDesc 0x21000073. Favorites remain server-persisted; casting emits one
|
|
/// request through <see cref="RuntimeSpellCastState"/>.
|
|
/// </summary>
|
|
internal sealed class SpellcastingUiController : IRetainedPanelController
|
|
{
|
|
public const uint PageId = 0x10000061u;
|
|
public const uint SpellNameId = 0x1000048Bu;
|
|
public const uint EndowmentId = 0x100000B1u;
|
|
public const uint CastButtonId = 0x100000B2u;
|
|
public const uint FavoriteScrollbarId = 0x100000B5u;
|
|
public const uint FavoriteListId = 0x100000B6u;
|
|
|
|
private static readonly uint[] TabIds =
|
|
[
|
|
0x100000A3u, 0x100000A4u, 0x100000A5u, 0x100000A6u,
|
|
0x100000A7u, 0x100000A8u, 0x100000A9u, 0x100005C2u,
|
|
];
|
|
|
|
private static readonly uint[] GroupIds =
|
|
[
|
|
0x100000AAu, 0x100000ABu, 0x100000ACu, 0x100000ADu,
|
|
0x100000AEu, 0x100000AFu, 0x100000B0u, 0x100005C3u,
|
|
];
|
|
|
|
private readonly Spellbook _spellbook;
|
|
private readonly RuntimeSpellCastState _casting;
|
|
private readonly SelectionState _selection;
|
|
private readonly ClientObjectTable _objects;
|
|
private readonly Func<uint> _playerGuid;
|
|
private readonly Func<uint, GpuTextureSlot> _resolveSpellIcon;
|
|
private readonly Func<ClientObject, GpuTextureSlot> _resolveItemDragIcon;
|
|
private readonly Action<uint> _useItem;
|
|
private readonly Action<uint>? _examineSpell;
|
|
private readonly Action<int, int, uint>? _addFavorite;
|
|
private readonly Action<int, uint>? _removeFavorite;
|
|
private readonly UiShortcutDigitGraphics? _shortcutDigits;
|
|
private readonly uint _emptySlotSprite;
|
|
private readonly UiElement[] _tabs;
|
|
private readonly UiElement[] _groups;
|
|
private readonly UiItemList?[] _lists;
|
|
private readonly UiScrollbar?[] _scrollbars;
|
|
private readonly UiButton _cast;
|
|
private readonly UiText? _spellName;
|
|
private readonly UiElement _endowmentHost;
|
|
private readonly UiCatalogSlot _endowmentSlot;
|
|
private int _activeTab;
|
|
private readonly uint?[] _selected = new uint?[8];
|
|
private readonly bool[] _endowmentSelected = new bool[8];
|
|
private uint _endowmentItemId;
|
|
private uint _endowmentSpellId;
|
|
private bool _disposed;
|
|
private bool _favoritesDirty;
|
|
private bool _endowmentDirty;
|
|
|
|
private SpellcastingUiController(
|
|
ImportedLayout layout,
|
|
Spellbook spellbook,
|
|
RuntimeSpellCastState casting,
|
|
ClientObjectTable objects,
|
|
Func<uint> playerGuid,
|
|
Func<uint, GpuTextureSlot> resolveSpellIcon,
|
|
Func<ClientObject, GpuTextureSlot> resolveItemDragIcon,
|
|
Action<uint> useItem,
|
|
Action<uint>? examineSpell,
|
|
SelectionState selection,
|
|
Action<int, int, uint>? addFavorite,
|
|
Action<int, uint>? removeFavorite,
|
|
UiElement[] tabs,
|
|
UiElement[] groups,
|
|
UiItemList?[] lists,
|
|
UiScrollbar?[] scrollbars,
|
|
UiButton cast,
|
|
UiElement endowmentHost,
|
|
UiShortcutDigitGraphics? shortcutDigits,
|
|
uint emptySlotSprite)
|
|
{
|
|
_spellbook = spellbook;
|
|
_casting = casting;
|
|
_selection = selection;
|
|
_objects = objects;
|
|
_playerGuid = playerGuid;
|
|
_resolveSpellIcon = resolveSpellIcon;
|
|
_resolveItemDragIcon = resolveItemDragIcon;
|
|
_useItem = useItem;
|
|
_examineSpell = examineSpell;
|
|
_addFavorite = addFavorite;
|
|
_removeFavorite = removeFavorite;
|
|
_shortcutDigits = shortcutDigits;
|
|
_emptySlotSprite = emptySlotSprite;
|
|
_tabs = tabs;
|
|
_groups = groups;
|
|
_lists = lists;
|
|
_scrollbars = scrollbars;
|
|
_cast = cast;
|
|
_spellName = layout.FindElement(SpellNameId) as UiText;
|
|
_endowmentHost = endowmentHost;
|
|
foreach (UiElement child in _endowmentHost.Children) child.Visible = false;
|
|
_endowmentSlot = new UiCatalogSlot
|
|
{
|
|
Left = 0f,
|
|
Top = 0f,
|
|
Width = endowmentHost.Width,
|
|
Height = endowmentHost.Height,
|
|
SpriteResolve = lists.FirstOrDefault(list => list is not null)?.SpriteResolve,
|
|
};
|
|
_endowmentSlot.Clicked = SelectEndowment;
|
|
_endowmentSlot.DoubleClicked = () => { SelectEndowment(); CastSelected(); };
|
|
_endowmentHost.AddChild(_endowmentSlot);
|
|
|
|
for (int i = 0; i < _lists.Length; i++)
|
|
{
|
|
if (_lists[i] is not { } list)
|
|
continue;
|
|
list.PrimaryCatalogEntryPressed = SelectSpell;
|
|
list.ExamineCatalogEntryRequested = _examineSpell;
|
|
if (_scrollbars[i] is not { } scrollbar)
|
|
continue;
|
|
list.HorizontalScroll = true;
|
|
scrollbar.Horizontal = true;
|
|
scrollbar.Model = list.Scroll;
|
|
scrollbar.SpriteResolve ??= list.SpriteResolve;
|
|
}
|
|
|
|
for (int i = 0; i < tabs.Length; i++)
|
|
{
|
|
int index = i;
|
|
SetClick(tabs[i], () => SelectTab(index));
|
|
}
|
|
_cast.OnClick = CastSelected;
|
|
ConfigureSpellName();
|
|
_spellbook.SpellbookChanged += OnSpellbookChanged;
|
|
_selection.Changed += OnSelectionChanged;
|
|
_objects.ObjectAdded += OnObjectChanged;
|
|
_objects.ObjectUpdated += OnObjectChanged;
|
|
_objects.ObjectRemoved += OnObjectChanged;
|
|
_objects.Cleared += OnObjectsCleared;
|
|
UpdateEndowment();
|
|
SelectTab(0);
|
|
Rebuild();
|
|
}
|
|
|
|
public int ActiveTab => _activeTab;
|
|
|
|
public static SpellcastingUiController? Bind(
|
|
ImportedLayout layout,
|
|
Spellbook spellbook,
|
|
RuntimeSpellCastState casting,
|
|
ClientObjectTable objects,
|
|
Func<uint> playerGuid,
|
|
Func<uint, GpuTextureSlot> resolveSpellIcon,
|
|
Func<ClientObject, GpuTextureSlot> resolveItemDragIcon,
|
|
Action<uint> useItem,
|
|
SelectionState selection,
|
|
Action<int, int, uint>? addFavorite,
|
|
Action<int, uint>? removeFavorite,
|
|
UiShortcutDigitGraphics? shortcutDigits = null,
|
|
uint emptySlotSprite = 0u,
|
|
Action<uint>? examineSpell = null)
|
|
{
|
|
if (layout.FindElement(CastButtonId) is not UiButton cast
|
|
|| layout.FindElement(EndowmentId) is not { } endowmentHost)
|
|
return null;
|
|
|
|
var tabs = new UiElement[8];
|
|
var groups = new UiElement[8];
|
|
var lists = new UiItemList?[8];
|
|
var scrollbars = new UiScrollbar?[8];
|
|
for (int i = 0; i < 8; i++)
|
|
{
|
|
if (layout.FindElement(TabIds[i]) is not { } tab
|
|
|| layout.FindElement(GroupIds[i]) is not { } group)
|
|
return null;
|
|
tabs[i] = tab;
|
|
groups[i] = group;
|
|
lists[i] = Descendants(group).OfType<UiItemList>().FirstOrDefault();
|
|
scrollbars[i] = Descendants(group).OfType<UiScrollbar>().FirstOrDefault(
|
|
scrollbar => scrollbar.DatElementId == FavoriteScrollbarId);
|
|
}
|
|
|
|
return new SpellcastingUiController(
|
|
layout, spellbook, casting, objects, playerGuid, resolveSpellIcon,
|
|
resolveItemDragIcon, useItem, examineSpell, selection,
|
|
addFavorite, removeFavorite,
|
|
tabs, groups, lists, scrollbars, cast, endowmentHost,
|
|
shortcutDigits, emptySlotSprite);
|
|
}
|
|
|
|
public void AddFavorite(uint spellId)
|
|
{
|
|
IReadOnlyList<uint> spells = _spellbook.GetFavorites(_activeTab);
|
|
if (spells.Contains(spellId))
|
|
{
|
|
SelectSpell(spellId);
|
|
return;
|
|
}
|
|
int position = spells.Count;
|
|
_addFavorite?.Invoke(_activeTab, position, spellId);
|
|
SelectSpell(spellId);
|
|
}
|
|
|
|
public bool Handle(InputAction action)
|
|
{
|
|
if (action is >= InputAction.UseSpellSlot_1 and <= InputAction.UseSpellSlot_9)
|
|
{
|
|
int index = (int)action - (int)InputAction.UseSpellSlot_1;
|
|
IReadOnlyList<uint> spells = _spellbook.GetFavorites(_activeTab);
|
|
if (index < spells.Count)
|
|
{
|
|
SelectSpell(spells[index]);
|
|
CastSelected();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
switch (action)
|
|
{
|
|
case InputAction.CombatPrevSpellTab: SelectTab((_activeTab + 7) % 8); return true;
|
|
case InputAction.CombatNextSpellTab: SelectTab((_activeTab + 1) % 8); return true;
|
|
case InputAction.CombatFirstSpellTab: SelectTab(0); return true;
|
|
case InputAction.CombatLastSpellTab: SelectTab(7); return true;
|
|
case InputAction.CombatPrevSpell: MoveSelection(-1, false); return true;
|
|
case InputAction.CombatNextSpell: MoveSelection(1, false); return true;
|
|
case InputAction.CombatFirstSpell: MoveSelection(0, true); return true;
|
|
case InputAction.CombatLastSpell: MoveSelection(-1, true); return true;
|
|
case InputAction.CombatCastCurrentSpell: CastSelected(); return true;
|
|
default: return false;
|
|
}
|
|
}
|
|
|
|
private void SelectTab(int tab)
|
|
{
|
|
_activeTab = Math.Clamp(tab, 0, 7);
|
|
for (int i = 0; i < 8; i++)
|
|
{
|
|
SetSelected(_tabs[i], i == _activeTab);
|
|
_groups[i].Visible = i == _activeTab;
|
|
}
|
|
IReadOnlyList<uint> favorites = _spellbook.GetFavorites(_activeTab);
|
|
if (_endowmentSelected[_activeTab] && _endowmentItemId != 0u)
|
|
_selected[_activeTab] = null;
|
|
else if (_selected[_activeTab] is not uint selected || !favorites.Contains(selected))
|
|
{
|
|
_selected[_activeTab] = favorites.Count == 0 ? null : favorites[0];
|
|
_endowmentSelected[_activeTab] = favorites.Count == 0 && _endowmentItemId != 0u;
|
|
}
|
|
SyncSelection();
|
|
UpdateCastAvailability();
|
|
}
|
|
|
|
private void MoveSelection(int delta, bool edge)
|
|
{
|
|
IReadOnlyList<uint> spells = _spellbook.GetFavorites(_activeTab);
|
|
int count = spells.Count + (_endowmentItemId != 0u ? 1 : 0);
|
|
if (count == 0) return;
|
|
int current = _endowmentSelected[_activeTab]
|
|
? 0
|
|
: (_selected[_activeTab] is uint id ? spells.IndexOf(id) : -1)
|
|
+ (_endowmentItemId != 0u ? 1 : 0);
|
|
int next = edge ? (delta < 0 ? count - 1 : 0) : (current + delta + count) % count;
|
|
if (_endowmentItemId != 0u && next == 0) SelectEndowment();
|
|
else SelectSpell(spells[next - (_endowmentItemId != 0u ? 1 : 0)]);
|
|
}
|
|
|
|
private void SelectSpell(uint spellId)
|
|
{
|
|
_endowmentSelected[_activeTab] = false;
|
|
_selected[_activeTab] = spellId;
|
|
SyncSelection();
|
|
ScrollSelectedSpellIntoView(_activeTab, spellId);
|
|
UpdateCastAvailability();
|
|
}
|
|
|
|
private void ScrollSelectedSpellIntoView(int tab, uint spellId)
|
|
{
|
|
UiItemList? list = _lists[tab];
|
|
if (list is null) return;
|
|
for (int i = 0; i < list.GetNumUIItems(); i++)
|
|
{
|
|
if (list.GetItem(i) is UiCatalogSlot { EntryId: var entryId }
|
|
&& entryId == spellId)
|
|
{
|
|
// SpellCastSubMenu::SetSelected @ 0x004C5B00 exposes the
|
|
// selected UIItem through UIElement_ListBox::ScrollToView.
|
|
list.ScrollItemIntoView(i);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CastSelected()
|
|
{
|
|
if (_endowmentSelected[_activeTab] && _endowmentItemId != 0u)
|
|
_useItem(_endowmentItemId);
|
|
else if (_selected[_activeTab] is uint spellId)
|
|
_casting.Cast(spellId);
|
|
}
|
|
|
|
private void SelectEndowment()
|
|
{
|
|
if (_endowmentItemId == 0u) return;
|
|
_endowmentSelected[_activeTab] = true;
|
|
_selected[_activeTab] = null;
|
|
SyncSelection();
|
|
UpdateCastAvailability();
|
|
}
|
|
|
|
private void Rebuild()
|
|
{
|
|
for (int tab = 0; tab < 8; tab++)
|
|
{
|
|
UiItemList? list = _lists[tab];
|
|
if (list is null) continue;
|
|
IReadOnlyList<uint> favorites = _spellbook.GetFavorites(tab);
|
|
int targetTab = tab;
|
|
list.CatalogDropped = (payload, x, _) =>
|
|
{
|
|
if (payload is SpellbookShortcutDragPayload shortcut)
|
|
DropSpellbookShortcut(
|
|
shortcut, targetTab, DropPosition(list, x, favorites.Count));
|
|
};
|
|
using (list.DeferLayout())
|
|
{
|
|
list.Flush();
|
|
list.SingleRow = true;
|
|
list.HorizontalScroll = true;
|
|
list.CellWidth = 32f;
|
|
list.CellHeight = 32f;
|
|
list.CellEmptySprite = _emptySlotSprite;
|
|
list.EmptySlotFactory = () => CreateEmptyFavoriteSlot(list, targetTab);
|
|
list.FillVisibleEmptySlots = true;
|
|
foreach (uint spellId in favorites)
|
|
{
|
|
uint id = spellId;
|
|
int position = list.GetNumUIItems();
|
|
_spellbook.TryGetMetadata(id, out SpellMetadata? metadata);
|
|
var slot = new UiCatalogSlot
|
|
{
|
|
EntryId = id,
|
|
CatalogIconTexture = metadata is null ? GpuTextureSlot.Unassigned : _resolveSpellIcon(id),
|
|
Label = metadata?.Name ?? $"Spell {id}",
|
|
SpriteResolve = list.SpriteResolve,
|
|
CatalogDragPayload = new SpellFavoriteDragPayload(tab, position, id),
|
|
DragBegan = payload => BeginFavoriteDrag((SpellFavoriteDragPayload)payload),
|
|
DragEnded = payload => EndFavoriteDrag((SpellFavoriteDragPayload)payload),
|
|
Dropped = payload =>
|
|
{
|
|
if (payload is SpellFavoriteDragPayload favorite)
|
|
DropFavorite(favorite, targetTab, position);
|
|
else if (payload is SpellbookShortcutDragPayload shortcut)
|
|
DropSpellbookShortcut(shortcut, targetTab, position);
|
|
},
|
|
};
|
|
// gmSpellcastingUI::ListenToElementMessage @ 0x004C7AB0
|
|
// selects on left press (message parameter 7). A completed
|
|
// double click casts, but release is not required merely to
|
|
// change the current spell.
|
|
slot.DoubleClicked = CastSelected;
|
|
list.AddItem(slot);
|
|
}
|
|
}
|
|
StampShortcutOverlays(list);
|
|
}
|
|
SelectTab(_activeTab);
|
|
}
|
|
|
|
private UiCatalogSlot CreateEmptyFavoriteSlot(UiItemList list, int targetTab)
|
|
{
|
|
int shortcutIndex = list.GetNumUIItems();
|
|
UiCatalogSlot? slot = null;
|
|
slot = new UiCatalogSlot
|
|
{
|
|
SpriteResolve = list.SpriteResolve,
|
|
Dropped = payload =>
|
|
{
|
|
int position = Math.Max(0, list.IndexOf(slot!));
|
|
int favoriteCount = _spellbook.GetFavorites(targetTab).Count;
|
|
position = Math.Min(position, favoriteCount);
|
|
if (payload is SpellFavoriteDragPayload favorite)
|
|
DropFavorite(favorite, targetTab, position);
|
|
else if (payload is SpellbookShortcutDragPayload shortcut)
|
|
DropSpellbookShortcut(shortcut, targetTab, position);
|
|
},
|
|
};
|
|
ConfigureShortcutOverlay(slot, shortcutIndex);
|
|
return slot;
|
|
}
|
|
|
|
private void StampShortcutOverlays(UiItemList list)
|
|
{
|
|
for (int i = 0; i < list.GetNumUIItems(); i++)
|
|
{
|
|
UiItemSlot? slot = list.GetItem(i);
|
|
if (slot is null) continue;
|
|
ConfigureShortcutOverlay(slot, i);
|
|
}
|
|
}
|
|
|
|
private void ConfigureShortcutOverlay(UiItemSlot slot, int index)
|
|
{
|
|
slot.RegularDigits = _shortcutDigits?.RegularDigits;
|
|
slot.GhostedDigits = _shortcutDigits?.GhostedDigits;
|
|
slot.EmptyDigits = _shortcutDigits?.EmptyDigits;
|
|
if (index < 9)
|
|
slot.SetShortcutNum(index, ghosted: false);
|
|
else
|
|
slot.ClearShortcutNum();
|
|
}
|
|
|
|
private void BeginFavoriteDrag(SpellFavoriteDragPayload payload)
|
|
=> _removeFavorite?.Invoke(payload.SourceTab, payload.SpellId);
|
|
|
|
private static void EndFavoriteDrag(SpellFavoriteDragPayload payload)
|
|
{
|
|
// The press-time command already performed retail's PlayerModule
|
|
// removal on the one Runtime-owned Spellbook.
|
|
}
|
|
|
|
private void DropFavorite(SpellFavoriteDragPayload payload, int targetTab, int targetPosition)
|
|
{
|
|
_addFavorite?.Invoke(targetTab, targetPosition, payload.SpellId);
|
|
_selected[targetTab] = payload.SpellId;
|
|
}
|
|
|
|
private void DropSpellbookShortcut(
|
|
SpellbookShortcutDragPayload payload,
|
|
int targetTab,
|
|
int targetPosition)
|
|
{
|
|
// gmSpellbookUI::ListenToElementMessage @ 0x0048BFD0 publishes
|
|
// CM_Magic::SendNotice_AddSpellShortcut; the open spellcasting menu
|
|
// chooses the favorite tab/position and persists it.
|
|
_addFavorite?.Invoke(targetTab, targetPosition, payload.SpellId);
|
|
_selected[targetTab] = payload.SpellId;
|
|
}
|
|
|
|
private static int DropPosition(UiItemList list, int localX, int favoriteCount)
|
|
{
|
|
int position = list.CellWidth <= 0f
|
|
? favoriteCount
|
|
: (int)MathF.Floor(MathF.Max(0f, localX) / list.CellWidth);
|
|
return Math.Clamp(position, 0, favoriteCount);
|
|
}
|
|
|
|
private void OnSpellbookChanged() => _favoritesDirty = true;
|
|
|
|
private void OnObjectChanged(ClientObject _) => _endowmentDirty = true;
|
|
|
|
private void OnObjectsCleared() => _endowmentDirty = true;
|
|
|
|
public void Tick()
|
|
{
|
|
if (_endowmentDirty)
|
|
{
|
|
_endowmentDirty = false;
|
|
UpdateEndowment();
|
|
SelectTab(_activeTab);
|
|
}
|
|
if (_favoritesDirty)
|
|
{
|
|
_favoritesDirty = false;
|
|
Rebuild();
|
|
}
|
|
}
|
|
|
|
private void SyncSelection()
|
|
{
|
|
for (int tab = 0; tab < 8; tab++)
|
|
{
|
|
UiItemList? list = _lists[tab];
|
|
if (list is null) continue;
|
|
for (int i = 0; i < list.GetNumUIItems(); i++)
|
|
if (list.GetItem(i) is UiCatalogSlot slot)
|
|
slot.Selected = slot.EntryId == _selected[tab];
|
|
}
|
|
_endowmentSlot.Selected = _endowmentItemId != 0u
|
|
&& _endowmentSelected[_activeTab];
|
|
}
|
|
|
|
private void OnSelectionChanged(SelectionTransition _) => UpdateCastAvailability();
|
|
|
|
private void UpdateCastAvailability()
|
|
=> _cast.Enabled = _endowmentSelected[_activeTab]
|
|
? _endowmentItemId != 0u
|
|
: _selected[_activeTab] is uint spellId
|
|
&& _casting.IsTargetReady(spellId);
|
|
|
|
private void ConfigureSpellName()
|
|
{
|
|
if (_spellName is null) return;
|
|
_spellName.OneLine = true;
|
|
_spellName.Centered = true;
|
|
_spellName.Padding = 0;
|
|
_spellName.LinesProvider = () =>
|
|
{
|
|
uint? chosenSpell = _endowmentSelected[_activeTab]
|
|
? (_endowmentSpellId == 0u ? null : _endowmentSpellId)
|
|
: _selected[_activeTab];
|
|
string name = chosenSpell is uint spellId
|
|
&& _spellbook.TryGetMetadata(spellId, out SpellMetadata metadata)
|
|
? metadata.Name : string.Empty;
|
|
return [new UiText.Line(name, _spellName.DefaultColor)];
|
|
};
|
|
}
|
|
|
|
private void UpdateEndowment()
|
|
{
|
|
ClientObject? endowment = _objects.GetEquippedBy(_playerGuid())
|
|
.FirstOrDefault(item =>
|
|
(item.CurrentlyEquippedLocation & EquipMask.Held) != 0
|
|
&& (item.Type & ItemType.Caster) != 0
|
|
&& item.SpellId.GetValueOrDefault() != 0u);
|
|
|
|
_endowmentItemId = endowment?.ObjectId ?? 0u;
|
|
_endowmentSpellId = endowment?.SpellId ?? 0u;
|
|
_endowmentHost.Visible = endowment is not null;
|
|
_endowmentSlot.EntryId = _endowmentItemId;
|
|
_endowmentSlot.CatalogIconTexture = _endowmentSpellId == 0u
|
|
? GpuTextureSlot.Unassigned : _resolveSpellIcon(_endowmentSpellId);
|
|
_endowmentSlot.CatalogOverlayTexture = endowment is null
|
|
? GpuTextureSlot.Unassigned : _resolveItemDragIcon(endowment);
|
|
string spellName = _spellbook.TryGetMetadata(_endowmentSpellId, out SpellMetadata metadata)
|
|
? metadata.Name : $"Spell {_endowmentSpellId}";
|
|
_endowmentSlot.Label = endowment is null
|
|
? string.Empty : $"{endowment.GetAppropriateName()} ({spellName})";
|
|
|
|
for (int tab = 0; tab < 8; tab++)
|
|
{
|
|
if (_endowmentItemId != 0u && _selected[tab] is null)
|
|
_endowmentSelected[tab] = true;
|
|
else if (_endowmentItemId == 0u && _endowmentSelected[tab])
|
|
_endowmentSelected[tab] = false;
|
|
}
|
|
}
|
|
|
|
private static IEnumerable<UiElement> Descendants(UiElement root)
|
|
{
|
|
foreach (UiElement child in root.Children)
|
|
{
|
|
yield return child;
|
|
foreach (UiElement nested in Descendants(child)) yield return nested;
|
|
}
|
|
}
|
|
|
|
public void OnShown() => SyncSelection();
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_disposed) return;
|
|
_disposed = true;
|
|
_spellbook.SpellbookChanged -= OnSpellbookChanged;
|
|
_selection.Changed -= OnSelectionChanged;
|
|
_objects.ObjectAdded -= OnObjectChanged;
|
|
_objects.ObjectUpdated -= OnObjectChanged;
|
|
_objects.ObjectRemoved -= OnObjectChanged;
|
|
_objects.Cleared -= OnObjectsCleared;
|
|
foreach (UiElement tab in _tabs) SetClick(tab, null);
|
|
foreach (UiItemList? list in _lists)
|
|
{
|
|
if (list is null) continue;
|
|
list.PrimaryCatalogEntryPressed = null;
|
|
list.ExamineCatalogEntryRequested = null;
|
|
}
|
|
_cast.OnClick = null;
|
|
}
|
|
|
|
private static void SetClick(UiElement element, Action? action)
|
|
{
|
|
element.ClickThrough = action is null;
|
|
switch (element)
|
|
{
|
|
case UiButton button: button.OnClick = action; break;
|
|
case UiText text: text.OnClick = action; break;
|
|
case UiDatElement dat: dat.OnClick = action; break;
|
|
}
|
|
}
|
|
|
|
private static void SetSelected(UiElement element, bool selected)
|
|
{
|
|
if (element is IUiDatStateful stateful
|
|
&& stateful.TrySetRetailState(selected ? RetailUiStateIds.Open : RetailUiStateIds.Closed))
|
|
return;
|
|
if (element is UiButton button)
|
|
button.Selected = selected;
|
|
else if (element is UiText text)
|
|
text.DefaultColor = selected
|
|
? new System.Numerics.Vector4(1f, 1f, 1f, 1f)
|
|
: new System.Numerics.Vector4(0.65f, 0.65f, 0.65f, 1f);
|
|
}
|
|
}
|
|
|
|
internal sealed record SpellFavoriteDragPayload(int SourceTab, int SourcePosition, uint SpellId);
|
|
|
|
/// <summary>
|
|
/// A learned-spell shortcut carried from the spellbook. Unlike a favorite drag,
|
|
/// lifting this payload never removes anything from its source collection.
|
|
/// </summary>
|
|
internal sealed record SpellbookShortcutDragPayload(uint SpellId);
|
|
|
|
internal static class FavoriteListExtensions
|
|
{
|
|
public static int IndexOf(this IReadOnlyList<uint> values, uint value)
|
|
{
|
|
for (int i = 0; i < values.Count; i++) if (values[i] == value) return i;
|
|
return -1;
|
|
}
|
|
}
|