TableTower/ui/inspector_roll.gd

142 lines
6.1 KiB
GDScript3
Raw Normal View History

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))
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()
## "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)