TableTower/ui/inspector_roll.gd
RobinFiredrake 33913889da Roll modifiers and resizable tray/inspector
- Ranged roll tables take a modifier (dice_mod, e.g. @luck). Ranges may run
  past the dice (-3 to 24 on a d20); totals past the ends use the end result.
- Range and chance code moved from RollTable into RollRange; chances follow
  the modifier's current value; plain wording and Why lines name it.
- ModifierEditor builds modifiers in plain words for roll and check cards;
  the inspector's roll section is now InspectorRoll.
- The Earlier tab scrolls; the tray and inspector resize with drag handles
  (PanelSplitter), sizes kept in local settings.
- Demo Room check uses 2d6 + Luck with a luck-only vault; new test_modifier
  suite; docs updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 10:01:10 -06:00

83 lines
3.4 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name InspectorRoll
extends RefCounted
## The inspector's section for a roll table: Plain or Ranged, the dice, the
## modifier (ModifierEditor) and a live sentence ("Roll d20 + Luck") with
## what the modifier does now. Writes go through Inspector.apply_settings, so
## the settings dialog's rules apply.
static func build(insp: Inspector, box: VBoxContainer, t: RollTable, plain: bool) -> void:
var v := InspectorKit.vbox(6)
var ranged := t.is_ranged()
var dice := InspectorKit.line(t.dice, "2d6", 90.0)
InspectorKit.tag(dice, "card.dice")
insp.fields["dice"] = dice
var err := InspectorKit.error_label()
var seg := ModalCard.segmented(["Plain", "Ranged"], 1 if ranged else 0, func(i: int) -> void:
var msg := insp.apply_settings({"ranged": i == 1, "dice": dice.text.strip_edges() if dice.text.strip_edges() != "" else "1d6"}, true)
InspectorKit.show_error(err, msg)
)
v.add_child(seg)
if not ranged:
v.add_child(InspectorKit.note("Each result is equally likely, unless you give it a weight on the card. Switch to Ranged to add a modifier to the roll.", Style.MUTED, 13))
v.add_child(err)
box.add_child(InspectorKit.field("Rolls", v))
return
var row := InspectorKit.hbox(6)
row.add_child(InspectorKit.word("Roll"))
row.add_child(dice)
v.add_child(row)
dice.text_changed.connect(func(s: String) -> void:
if not TableSettingsDialog.dice_ok_text(s):
InspectorKit.show_error(err, "“%s” isn't dice such as 2d6 or 1d100." % s.strip_edges())
InspectorKit.mark(dice, false)
return
InspectorKit.mark(dice, true)
InspectorKit.show_error(err, insp.apply_settings({"ranged": true, "dice": s.strip_edges()}))
)
v.add_child(err)
box.add_child(InspectorKit.field("Rolls", v))
var mod := ModifierEditor.new(insp.main, t.dice_mod, plain, "card.dice_mod")
insp.fields["dice_mod"] = mod
var merr := InspectorKit.error_label()
mod.changed.connect(func(s: String) -> void: InspectorKit.show_error(merr, insp.apply_settings({"dice_mod": s})))
var mbox := InspectorKit.vbox(5)
mbox.add_child(mod)
mbox.add_child(merr)
box.add_child(InspectorKit.field("Modifier", mbox))
# A live sentence, what the modifier does, and any range problems.
var sentence := InspectorKit.note("", Style.INK, 14)
sentence.add_theme_font_override("font", Style.body_bold())
var how := InspectorKit.note("", Style.MUTED, 13)
var probs := InspectorKit.note("", Style.WARN_INK, 13)
insp.fields["roll_sentence"] = sentence
insp.fields["roll_help"] = how
box.add_child(sentence)
box.add_child(how)
box.add_child(probs)
var refresh := func() -> void:
sentence.text = RulesView.roll_line(t, insp.main.data, plain)
how.text = help(t, insp.main)
how.visible = how.text != ""
probs.text = t.range_problems()
probs.visible = probs.text != ""
insp._live.append(refresh)
refresh.call()
## What a ranged table's modifier means, in words: the dice's own range, the
## modifier's value now, and how the ends work. "" without a modifier.
static func help(t: RollTable, main: Main) -> String:
if not t.has_mod():
return ""
var parts := PackedStringArray()
if not t.is_expr_dice():
var nat := RollRange.natural(t)
parts.append("The dice alone roll %s–%s." % [t.fmt(nat.x), t.fmt(nat.y)])
var lm := RollRange.live_mod(t, Roller.live_context(main.data, main.persistent_vars))
if lm["known"]:
parts.append("The modifier is %s right now." % Describe.signed_number(int(lm["value"])))
parts.append("A total past the lowest or highest result counts as that result.")
return " ".join(parts)