TableTower/ui/inspector_thread_rolls.gd
2026-11-14 14:40:03 -06:00

137 lines
5.4 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 InspectorThreadRolls
extends RefCounted
## The thread inspector's "how the card is rolled" options: roll it with other
## dice instead (a card with numbered ranges only), and roll again on some of
## its results. InspectorThread.box() adds rows() to the thread box.
## Results the "Roll again" list shows before it scrolls, and its height then.
const LIST_ROWS := 7
const LIST_MAX_H := 196.0
## Inspector meta: the thread targets whose "Roll again" list is open.
const OPEN_META := "reroll_open"
## "" when `s` is dice the thread can roll with (empty = the card's own).
static func dice_error(s: String) -> String:
var d := s.strip_edges()
if Dice.roll_problem(d) == "":
return ""
return "That isn't something the app can roll. Try dice such as 3d10+70."
static func set_dice(insp: Inspector, to: String, s: String) -> void:
var e := insp.selected_entry()
if e == null or dice_error(s) != "" or s.strip_edges() == e.dice_for(to):
return
e.set_pipe_dice(to, s)
insp.wrote(insp.selected_table())
## Ticks (on) or unticks the target's result `rid` in the thread's "roll again" list.
static func set_reroll(insp: Inspector, to: String, rid: String, on: bool) -> void:
var e := insp.selected_entry()
if e == null:
return
var ids := e.reroll_for(to)
if ids.has(rid) == on:
return
if on:
ids.append(rid)
else:
ids.erase(rid)
e.set_pipe_reroll(to, ids)
insp.wrote(insp.selected_table())
## Adds the rows for the thread to `target` to `v`; fills `parts` with
## "dice" (LineEdit), "reroll_toggle" (Button) and "reroll" (rid -> CheckBox).
static func rows(insp: Inspector, e: TableEntry, to: String, target: RollTable, v: VBoxContainer, parts: Dictionary) -> void:
if target == null or target.kind != RollTable.KIND_ROLL or target.entries.is_empty():
return
if target.is_ranged():
_dice_row(insp, e, to, target, v, parts)
else:
# Say why "Roll it with … instead" isn't here, so it can be found.
var hint := InspectorKit.note("Rolling it with other dice needs numbered ranges: make %s Ranged in its own settings." % Inspector._title_of(target.name, "the card"), Style.MUTED, 12)
hint.set_meta("dice_hint", true)
v.add_child(hint)
_reroll_rows(insp, e, to, target, v, parts)
static func _dice_row(insp: Inspector, e: TableEntry, to: String, target: RollTable, v: VBoxContainer, parts: Dictionary) -> void:
var row := InspectorKit.flow()
row.add_child(InspectorKit.word("Roll it with"))
var le := InspectorKit.amount(e.dice_for(to), target.dice)
le.tooltip_text = "Other dice to roll %s with this time, such as 3d10+70. Leave it empty to use the card's own dice (%s)." % [target.name, target.dice]
InspectorKit.tag(le, "pipe.%s.dice" % to)
row.add_child(le)
row.add_child(InspectorKit.word("instead"))
var err := InspectorKit.error_label()
le.text_changed.connect(func(s: String) -> void:
var msg := dice_error(s)
InspectorKit.show_error(err, msg)
InspectorKit.mark(le, msg == "")
set_dice(insp, to, s)
)
parts["dice"] = le
v.add_child(row)
v.add_child(err)
static func _reroll_rows(insp: Inspector, e: TableEntry, to: String, target: RollTable, v: VBoxContainer, parts: Dictionary) -> void:
var open: Dictionary = insp.get_meta(OPEN_META, {})
var toggle := InspectorKit.link("", "Pick results that are rolled again when they come up")
toggle.alignment = HORIZONTAL_ALIGNMENT_LEFT
toggle.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
toggle.size_flags_horizontal = Control.SIZE_EXPAND_FILL
v.add_child(toggle)
var scroll := ScrollContainer.new()
scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
# Short lists show whole; longer ones scroll inside a capped box.
if target.entries.size() <= LIST_ROWS:
scroll.vertical_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
else:
scroll.custom_minimum_size.y = LIST_MAX_H
var list := InspectorKit.vbox(0)
scroll.add_child(list)
var boxes := {}
for i in target.entries.size():
var te := target.entries[i]
var text := Formatter.strip_tags(te.text).strip_edges()
var num := target.num_label(te, i)
var cb := InspectorKit.check_box(("%s %s" % [num, text]).strip_edges() if text != "" else num, e.reroll_for(to).has(te.id))
cb.add_theme_font_override("font", Style.body())
cb.add_theme_font_size_override("font_size", 14)
cb.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
cb.clip_text = true
cb.size_flags_horizontal = Control.SIZE_EXPAND_FILL
cb.tooltip_text = cb.text
var rid := te.id
cb.toggled.connect(func(on: bool) -> void:
set_reroll(insp, to, rid, on)
_label_toggle(toggle, insp, to, target, scroll.visible)
)
boxes[te.id] = cb
list.add_child(cb)
v.add_child(scroll)
var show := func(on: bool) -> void:
scroll.visible = on
var o: Dictionary = insp.get_meta(OPEN_META, {})
if on:
o[to] = true
else:
o.erase(to)
insp.set_meta(OPEN_META, o)
_label_toggle(toggle, insp, to, target, on)
toggle.pressed.connect(func() -> void: show.call(not scroll.visible))
show.call(open.has(to))
parts["reroll_toggle"] = toggle
parts["reroll"] = boxes
## "Roll again if it comes up: 96–00 ▸" (or "never").
static func _label_toggle(toggle: Button, insp: Inspector, to: String, target: RollTable, open: bool) -> void:
var e := insp.selected_entry()
var ids: Array = e.reroll_for(to) if e else []
var picked := DescribeCards.reroll_names(ids, target) if not ids.is_empty() else "never"
toggle.text = "Roll again if it comes up: %s %s" % [picked, "▾" if open else "▸"]