TableTower/ui/inspector_roll.gd
RobinFiredrake 1e944fa27d Inspector: one list with the card and every result, each collapsible
Selecting a card or result on the spread opens its section and closes the
rest; sections opened by hand stay open, and a click or focus inside one
makes it the section edits go to. Only open sections build their fields.
Result sections edit the weight or range and the position; the card
section picks Weights or Ranges and Auto-sort.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:35:27 -06:00

171 lines
7.6 KiB
GDScript
Raw Permalink 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.
## The Modifier field (ModifierEditor), with an optional note under it.
static func _modifier_field(insp: Inspector, box: VBoxContainer, t: RollTable, plain: bool, note: String = "") -> void:
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)
if note != "":
mbox.add_child(InspectorKit.note(note, Style.MUTED, 12))
mbox.add_child(merr)
box.add_child(InspectorKit.field("Modifier", mbox))
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.", Style.MUTED, 13))
v.add_child(err)
box.add_child(InspectorKit.field("Rolls", v))
_modifier_field(insp, box, t, plain, "Moves the result down the list (+) or up (−) by that many rows. The first and last results catch anything past them.")
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))
box.add_child(InspectorKit.field("Edit ranges as", edit_mode(insp, t)))
_modifier_field(insp, box, t, plain)
box.add_child(InspectorKit.field("Limits", limits(insp, t)))
# 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()
## Weights or Ranges (RangeWeights), with Auto-sort for Ranges.
static func edit_mode(insp: Inspector, t: RollTable) -> Control:
var v := InspectorKit.vbox(6)
var weights := t.range_edit == RangeWeights.WEIGHTS
var seg := ModalCard.segmented(["Weights", "Ranges"], 0 if weights else 1, func(i: int) -> void:
insp.apply_settings({"range_edit": RangeWeights.WEIGHTS if i == 0 else RangeWeights.RANGES}, true)
)
InspectorKit.tag(seg, "card.range_edit")
v.add_child(seg)
if weights:
v.add_child(InspectorKit.note("Type how many die faces each result covers; the ranges follow the row order.", Style.MUTED, 13))
else:
v.add_child(InspectorKit.note("Type each range by hand, as a printed table does.", Style.MUTED, 13))
var sort := InspectorKit.check_box("Auto-sort rows by range", t.auto_sort)
sort.tooltip_text = "After an edit or a drag, rows put themselves back in roll order"
InspectorKit.tag(sort, "card.auto_sort")
sort.toggled.connect(func(on: bool) -> void: insp.apply_settings({"auto_sort": on}, true))
v.add_child(sort)
return v
## "Never below [ ]" and "Never above [ ]": a number, or a value an earlier
## result sets (so one table can adjust a later one), via RollTable.roll_floor
## and roll_ceiling.
static func limits(insp: Inspector, t: RollTable) -> Control:
var v := InspectorKit.vbox(4)
for floor_side in [true, false]:
var row := InspectorKit.flow()
row.add_child(InspectorKit.word("Never below" if floor_side else "Never above"))
var le := InspectorKit.line(t.roll_floor if floor_side else t.roll_ceiling, "no limit", 110.0)
le.tooltip_text = "A number, or a value such as @atmo_min that an earlier result sets"
var err := InspectorKit.error_label()
var apply := func(s: String) -> void:
var msg := Expr.check(s) if s.strip_edges() != "" else ""
InspectorKit.show_error(err, msg)
InspectorKit.mark(le, msg == "")
if msg != "":
return
if floor_side:
t.roll_floor = s.strip_edges()
else:
t.roll_ceiling = s.strip_edges()
insp.wrote(t)
le.text_changed.connect(apply)
row.add_child(le)
var pick := ValuePicker.new(insp.main, "")
pick.tooltip_text = "Use a value"
pick.value_chosen.connect(func(n: String) -> void:
le.text = "@" + n.trim_prefix("@")
apply.call(le.text))
row.add_child(pick)
v.add_child(row)
v.add_child(err)
v.add_child(InspectorKit.note("To have one table adjust a later one, give the earlier result a change such as “Set Atmo min to 7”, then use that value here.", Style.MUTED, 12))
return v
## Deck mode: draw results without repeats, and what an empty deck does.
static func deck(insp: Inspector, box: VBoxContainer, t: RollTable) -> void:
var v := InspectorKit.vbox(6)
var on := InspectorKit.check_box("Draw without repeats", t.card_mode)
on.tooltip_text = "Each result is dealt like a card and stays out until the deck is shuffled"
on.toggled.connect(func(b: bool) -> void: insp.apply_settings({"card_mode": b}, true))
v.add_child(on)
if t.card_mode:
v.add_child(InspectorKit.note("When the deck is empty:", Style.MUTED, 13))
v.add_child(ModalCard.segmented(["Shuffle by itself", "Wait for Shuffle"], 1 if t.deck_empty == "stop" else 0, func(i: int) -> void:
insp.apply_settings({"deck_empty": "stop" if i == 1 else "reshuffle"}, true)))
if t.deck_empty == "stop":
var empty := InspectorKit.line(t.deck_empty_text, "Empty")
empty.tooltip_text = "What the card gives while its deck is empty"
empty.text_changed.connect(func(s: String) -> void:
t.deck_empty_text = s.strip_edges()
insp.wrote(t))
v.add_child(InspectorKit.field("Empty result", empty))
box.add_child(InspectorKit.field("Deck mode", v))
## 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)