M6.2: Step V Abilities — drag-drop assignment + roll/auto-assign

Per GODOT_PORTING_GUIDE.md §7, the highest-risk piece in the wizard.
Three reusable widgets + the orchestrating step.

Scenes/Widgets/AbilityToken.cs:
  Draggable Control with Value + Origin metadata. _GetDragData returns
  a Dictionary payload {kind, value, from, ability, idx} per guide
  §7.1. MouseFilter = Pass so clicks propagate to the parent slot
  for the click-to-return affordance (later removed; see commit body).
  Drag preview is a dimmed duplicate.

Scenes/Widgets/AbilitySlot.cs:
  PanelContainer drop target per guide §7.2. Accepts any
  ability_value payload via _CanDropData / _DropData and emits
  Dropped(payload). Each slot owns one ability id (STR/DEX/CON/INT/
  WIS/CHA).

Scenes/Widgets/AbilityPool.cs:
  HBoxContainer drop target per guide §7.3. Accepts only slot→pool
  drops (returning an assigned value to the pool); pool→pool drops
  are no-ops.

Scenes/Steps/StepStats.cs:
  Direct port of StepStats in steps.jsx per guide §7.4. Standard
  array (default) and roll-4d6-drop-lowest method tabs; Reroll
  button visible in roll mode; Auto Assign sorts the remaining pool
  descending and places the largest values into empty slots ordered
  by class.PrimaryAbility. Three drag-drop cases (pool→slot,
  slot→slot swap, slot→pool) all delegate to a single Patch call,
  then the entire token tree rebuilds from the new draft state on
  the Changed signal — handlers don't reparent anything manually.

Issues hit during development and resolved before commit:
  - Initial click-to-return on slot pre-empted drag-from-slot every
    time (the GuiInput fired on mouse-down, before Godot detected
    the drag). Removed click-to-return — drag is the canonical
    interaction; that matches the React prototype anyway.
  - Token MouseFilter = Stop blocked clicks from reaching the slot
    layer; switched to Pass which still works as a drag source.
  - Refresh() teardown + rebuild reset the parent ScrollContainer's
    scroll to 0 every drop. CallDeferred / SetDeferred / CreateTimer
    all raced because layout settles over multiple frames; the fix
    that worked was capturing scroll position pre-rebuild and
    restoring in _Process the next frame.

Wizard.cs:
  StepTypes[5] = typeof(StepStats); the Abilities step is now
  reachable. (StepTypes[1..4, 6..7] still null — coming in M6.3+.)

Verified: all three drag scenarios + click handling + auto-assign
+ method switch + reroll work; scroll position holds across drops.

Closes M6.2. Next per guide §12: M6.3 — popover system (TraitChip +
shared PopoverLayer) before adding more easy card-grid steps.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Christopher Wiebe
2026-05-02 20:36:19 -07:00
parent ee5439285c
commit 4d3db17a89
5 changed files with 520 additions and 1 deletions
@@ -0,0 +1,37 @@
using Godot;
namespace Theriapolis.GodotHost.Scenes.Widgets;
/// <summary>
/// Container for unassigned AbilityToken children. Per
/// GODOT_PORTING_GUIDE.md §7.3, accepts only slot→pool drops (returning
/// an assigned value to the pool); pool→pool drops are no-ops. The step
/// consumes <see cref="Dropped"/> and mutates CharacterDraft.StatPool /
/// StatAssign in one call so the inevitable refresh re-creates tokens
/// in the right places.
/// </summary>
public partial class AbilityPool : HBoxContainer
{
[Signal] public delegate void DroppedEventHandler(Godot.Collections.Dictionary payload);
public override void _Ready()
{
AddThemeConstantOverride("separation", 8);
MouseFilter = MouseFilterEnum.Stop;
CustomMinimumSize = new Vector2(0, 64);
}
public override bool _CanDropData(Vector2 atPosition, Variant data)
{
if (data.VariantType != Variant.Type.Dictionary) return false;
var d = data.AsGodotDictionary();
if (!d.ContainsKey("kind") || d["kind"].AsString() != "ability_value") return false;
// Only slot→pool drops are meaningful; pool→pool is a no-op.
return d.ContainsKey("from") && d["from"].AsString() == "slot";
}
public override void _DropData(Vector2 atPosition, Variant data)
{
EmitSignal(SignalName.Dropped, data);
}
}
@@ -0,0 +1,40 @@
using Godot;
namespace Theriapolis.GodotHost.Scenes.Widgets;
/// <summary>
/// Drop target for ability assignment. Per GODOT_PORTING_GUIDE.md §7.2:
/// accepts any drag payload tagged "ability_value" and emits
/// <see cref="Dropped"/> with the payload so the step orchestrates the
/// state change centrally. Visual content (the AbilityToken when filled,
/// or a placeholder dash when empty) is set by the step on every refresh.
///
/// Each slot owns exactly one ability id (STR/DEX/CON/INT/WIS/CHA). On
/// click of a filled slot, returns the value to the pool (synthesised
/// slot→pool payload) — matches the React prototype's "click to unbind"
/// affordance from <c>steps.jsx</c>.
/// </summary>
public partial class AbilitySlot : PanelContainer
{
[Signal] public delegate void DroppedEventHandler(Godot.Collections.Dictionary payload);
[Export] public string Ability { get; set; } = "STR";
public override void _Ready()
{
CustomMinimumSize = new Vector2(56, 56);
MouseFilter = MouseFilterEnum.Stop;
}
public override bool _CanDropData(Vector2 atPosition, Variant data)
{
if (data.VariantType != Variant.Type.Dictionary) return false;
var d = data.AsGodotDictionary();
return d.ContainsKey("kind") && d["kind"].AsString() == "ability_value";
}
public override void _DropData(Vector2 atPosition, Variant data)
{
EmitSignal(SignalName.Dropped, data);
}
}
@@ -0,0 +1,71 @@
using Godot;
namespace Theriapolis.GodotHost.Scenes.Widgets;
/// <summary>
/// Draggable ability-score token. Replaces the React prototype's
/// <c>.die</c> + filled <c>.slot</c> in <c>steps.jsx</c> per
/// GODOT_PORTING_GUIDE.md §7.1. Shows the int value as a centered
/// label; <c>_GetDragData</c> returns a Dictionary payload describing
/// where the token came from so the step can mutate CharacterDraft
/// accordingly.
///
/// Tokens are owned by either an AbilityPool (Origin == "pool",
/// OriginPoolIdx set) or an AbilitySlot (Origin == "slot",
/// OriginAbility set). Dropping a token rebuilds the StepStats UI from
/// the new draft state — token instances are short-lived.
/// </summary>
public partial class AbilityToken : PanelContainer
{
[Export] public int Value { get; set; }
[Export] public string Origin { get; set; } = "pool";
[Export] public string OriginAbility { get; set; } = "";
[Export] public int OriginPoolIdx { get; set; } = -1;
public override void _Ready()
{
CustomMinimumSize = new Vector2(56, 56);
// PASS so clicks propagate up to the parent AbilitySlot's GuiInput
// handler (click-to-return). Drag detection still triggers on the
// deepest non-IGNORE Control under the cursor, so PASS works for
// _GetDragData too.
MouseFilter = MouseFilterEnum.Pass;
var label = new Label
{
Text = Value.ToString(),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
MouseFilter = MouseFilterEnum.Ignore,
};
label.AddThemeFontSizeOverride("font_size", 22);
AddChild(label);
}
public override Variant _GetDragData(Vector2 atPosition)
{
// Drag preview — a dimmed duplicate of the token.
var preview = new Panel { CustomMinimumSize = new Vector2(56, 56) };
var lbl = new Label
{
Text = Value.ToString(),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
lbl.AddThemeFontSizeOverride("font_size", 22);
lbl.AnchorRight = 1f;
lbl.AnchorBottom = 1f;
preview.AddChild(lbl);
preview.Modulate = new Color(1, 1, 1, 0.85f);
SetDragPreview(preview);
return new Godot.Collections.Dictionary
{
{ "kind", "ability_value" },
{ "value", Value },
{ "from", Origin },
{ "ability", OriginAbility },
{ "idx", OriginPoolIdx },
};
}
}