A roll table's result can now have "Can come up only if" (the same show_if options use): while it's false the result is rolled again, and the card shows the condition on the row. Rolling from List view skips the animation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
332 lines
13 KiB
GDScript
332 lines
13 KiB
GDScript
class_name TableCardRows
|
||
extends RefCounted
|
||
## A TableCard's result rows: building them (plain, check outlet, sheet and the
|
||
## collapsed card's "more" row), their Play-mode text and rule chips, and their
|
||
## highlight state (normal, flickering during a roll, lit, with the copper bar
|
||
## of the inspector's selected result). Build-only controls come from
|
||
## TableCardEditing; the Play text comes from TableCardRowText and the chance
|
||
## labels from TableCardOdds.
|
||
|
||
enum RowState { NORMAL, FLICK, LIT }
|
||
|
||
const LOOP_ROWS := ["Each time", "Afterwards"]
|
||
## Longest effects summary shown on a chip.
|
||
const CHIP_CHARS := 30
|
||
const PLAIN_CHIP_CHARS := 38
|
||
|
||
var card: TableCard
|
||
## entry id -> {num, text, entry, index}, for the rows currently shown.
|
||
var parts: Dictionary = {}
|
||
var more_label: Label
|
||
var sheet_label: RichTextLabel
|
||
var kind_rows: TableCardKindRows
|
||
var columns_ui: TableCardColumns
|
||
|
||
## Row styles by state, then +3 for the inspector's selected result, then +6
|
||
## for a striped (every second) row.
|
||
var _row_styles: Array[StyleBoxFlat] = []
|
||
var _flick_id := ""
|
||
## Chance labels of ranged rows in Play mode (TableCardOdds).
|
||
var odds: TableCardOdds
|
||
|
||
|
||
func _init(p_card: TableCard) -> void:
|
||
card = p_card
|
||
kind_rows = TableCardKindRows.new(self)
|
||
columns_ui = TableCardColumns.new(p_card)
|
||
odds = TableCardOdds.new(p_card)
|
||
# Normal, flicker and lit; then the same with the copper bar of the
|
||
# inspector's selected result. Striped rows tint only the normal state, so
|
||
# the flicker and the lit result always read over the stripe.
|
||
for striped in [false, true]:
|
||
for selected in [false, true]:
|
||
for c in [Style.STRIPE if striped else Color(0, 0, 0, 0), Style.FLICK, Style.LIT]:
|
||
var sb := Style.box(c, 0, [12, 6, 22, 6])
|
||
if selected:
|
||
sb.border_width_left = 3
|
||
sb.border_color = Style.TRIM
|
||
sb.anti_aliasing = false
|
||
_row_styles.append(sb)
|
||
|
||
|
||
## Forgets the previous build's rows.
|
||
func clear() -> void:
|
||
parts.clear()
|
||
columns_ui.clear()
|
||
odds.clear()
|
||
more_label = null
|
||
_flick_id = ""
|
||
sheet_label = null
|
||
|
||
|
||
func register(e: TableEntry, row: PanelContainer, num: Label, text: RichTextLabel, index: int) -> void:
|
||
card.rows[e.id] = row
|
||
parts[e.id] = {"num": num, "text": text, "entry": e, "index": index}
|
||
|
||
|
||
func _is_pick() -> bool:
|
||
return card.table.kind == RollTable.KIND_CHOICE and card.table.choice == "pick"
|
||
|
||
|
||
## True for the Chosen row of a dynamic pick card.
|
||
func _is_chosen_row(index: int) -> bool:
|
||
return index == 0 and card.table.is_dynamic()
|
||
|
||
|
||
func make_row(e: TableEntry, index: int, build: bool, ranged: bool, numw: float) -> Control:
|
||
var table := card.table
|
||
var main := card.main
|
||
var edit := card.edit_ui
|
||
var row := PanelContainer.new()
|
||
row.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
row.custom_minimum_size = Vector2(0, 36)
|
||
var h := TableCard.hbox(8)
|
||
row.add_child(h)
|
||
# Loop rows and the Answer row of number/text choices are fixed: no number,
|
||
# weight or delete.
|
||
var fixed := table.kind == RollTable.KIND_LOOP or (table.kind == RollTable.KIND_CHOICE and table.choice != "pick")
|
||
var chosen := _is_chosen_row(index)
|
||
|
||
if build and not fixed and not chosen and table.entries.size() > 1:
|
||
h.add_child(EntryHandle.new(card, e.id))
|
||
var num: Label = null
|
||
if build and ranged:
|
||
var r := TableCard.hbox(2)
|
||
r.custom_minimum_size = Vector2(numw, 0)
|
||
r.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
r.add_child(edit.range_edit(e, true))
|
||
r.add_child(Style.make_label("–", Style.display(true), 15, Style.MUTED))
|
||
r.add_child(edit.range_edit(e, false))
|
||
h.add_child(r)
|
||
else:
|
||
var nl := "" if fixed else (str(index + 1) if table.kind == RollTable.KIND_ROUTER else table.num_label(e, index))
|
||
if table.is_dynamic() and not ranged:
|
||
nl = "★" if chosen else str(index)
|
||
num = Style.make_label(nl, Style.display(true), 16, Style.MUTED)
|
||
num.custom_minimum_size = Vector2(numw, 0)
|
||
num.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
|
||
num.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
if fixed:
|
||
num.text = "↻" if table.kind == RollTable.KIND_LOOP and index == 0 else "→"
|
||
num.add_theme_font_override("font", Style.body_bold(true))
|
||
num.add_theme_font_size_override("font_size", 14)
|
||
h.add_child(num)
|
||
|
||
var text: RichTextLabel = null
|
||
var delete_button: Button = null
|
||
if build:
|
||
h.add_theme_constant_override("separation", 4)
|
||
var col := TableCard.vbox(2)
|
||
col.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
col.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
h.add_child(col)
|
||
if table.kind == RollTable.KIND_CHOICE and table.choice != "pick":
|
||
var al := Style.make_label("Answer", Style.body_italic(true), 15, Style.MUTED)
|
||
al.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
al.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
al.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
al.tooltip_text = "Its effects and threads run after the player answers ({answer} holds the answer)"
|
||
col.add_child(al)
|
||
else:
|
||
if chosen:
|
||
# A caption over the text field, so the field keeps its width.
|
||
var cl := Style.make_label("Chosen · runs for the picked option", Style.body_italic(true), 11, Style.TRIM_DARK)
|
||
cl.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
cl.tooltip_text = "Runs for whichever option from %s is picked: option, option_label and option_value hold it" % table.options_from.strip_edges()
|
||
col.add_child(cl)
|
||
col.add_child(edit.text_edit(e, index, chosen))
|
||
add_rule_chips(col, e)
|
||
columns_ui.add_cells(h, e, index, true)
|
||
if not ranged and not fixed:
|
||
h.add_child(edit.weight_edit(e))
|
||
if not fixed and not chosen:
|
||
delete_button = TableCardEditing.mini_button("×", "Delete this result", true, func() -> void: main.delete_entry(table.id, e.id))
|
||
h.add_child(delete_button)
|
||
edit.hook_select(row, e.id)
|
||
else:
|
||
var col := TableCard.vbox(3)
|
||
col.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
col.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
text = RichText.label(row_text(e), 16, Style.INK, true)
|
||
text.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
RichText.hook_hover(text, main)
|
||
col.add_child(text)
|
||
add_rule_chips(col, e)
|
||
h.add_child(col)
|
||
columns_ui.add_cells(h, e, index, false)
|
||
row.mouse_entered.connect(func() -> void: main.details().show_entry(row, table.id, e.id))
|
||
if ranged and table.has_chances():
|
||
var pct := Style.make_label("", Style.body(true), 12, Style.MUTED)
|
||
pct.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
pct.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
h.add_child(pct)
|
||
odds.track(pct, e)
|
||
if ranged and table.has_mod() and num != null and not RollRange.natural_reach(table, e):
|
||
var nat := RollRange.natural(table)
|
||
num.tooltip_text = "Only a modifier reaches this: the dice alone roll %s–%s" % [table.fmt(nat.x), table.fmt(nat.y)]
|
||
num.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
|
||
if e.image != "":
|
||
var pic := TextureRect.new()
|
||
pic.texture = main.data.picture(e.image)
|
||
pic.custom_minimum_size = Vector2(30, 30)
|
||
pic.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
|
||
pic.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
|
||
pic.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
pic.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
h.add_child(pic)
|
||
if e.page != "":
|
||
# A small § says the result follows a Book page.
|
||
var pg := main.data.get_page(e.page)
|
||
var mark := Style.make_label("§", Style.body_bold(true), 13, Style.TRIM)
|
||
mark.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
mark.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
mark.tooltip_text = "From the Book: %s" % (pg.label() if pg else "a deleted page")
|
||
h.add_child(mark)
|
||
h.move_child(mark, mini(1, h.get_child_count() - 1))
|
||
register(e, row, num, text, index)
|
||
if delete_button:
|
||
edit.reveal_on_hover(e.id, row, delete_button)
|
||
return row
|
||
|
||
|
||
## The plain chips under a row's text: a pick option's "only if …" and the
|
||
## entry's effects ("Luck −1"). Used in Play and Build alike.
|
||
func add_rule_chips(col: VBoxContainer, e: TableEntry) -> void:
|
||
var plain := card.plain
|
||
if (_is_pick() or card.table.kind == RollTable.KIND_ROLL) and e.show_if.strip_edges() != "":
|
||
var bad := Expr.check(TemplateKit.probe(e.show_if.strip_edges())) != ""
|
||
var si := Style.make_label(RulesView.show_if_line(e, card.main.data, plain), Style.body_bold(true), 12, Style.DANGER_INK if bad else Style.TRIM_DARK)
|
||
si.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
||
si.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
si.tooltip_text = ("only if " + e.show_if.strip_edges()) if plain else ""
|
||
si.set_meta("show_if_line", true)
|
||
col.add_child(si)
|
||
if e.effects.strip_edges() != "":
|
||
col.add_child(kind_rows.effects_chip(e))
|
||
|
||
|
||
## Play-mode text of a row (TableCardRowText): the rolled text when lit,
|
||
## otherwise the entry as written, with a lead-in for some card kinds.
|
||
func row_text(e: TableEntry) -> String:
|
||
return TableCardRowText.text_for(self, e)
|
||
|
||
|
||
## The roll modifier's value now (RollRange.live_mod): {"known", "value"}.
|
||
func live_mod() -> Dictionary:
|
||
return odds.live_mod()
|
||
|
||
|
||
## Updates the chance labels for the modifier's current value.
|
||
func refresh_odds() -> void:
|
||
odds.refresh()
|
||
|
||
|
||
## Re-renders a sheet card's template from the current variables.
|
||
func refresh_sheet() -> void:
|
||
if is_instance_valid(sheet_label):
|
||
var main := card.main
|
||
var s := RichText.decorate(Roller.render_sheet(main.data, main.persistent_vars, card.table))
|
||
if sheet_label.text != s:
|
||
sheet_label.text = s
|
||
|
||
|
||
func make_more_row(numw: float) -> PanelContainer:
|
||
var more_row := PanelContainer.new()
|
||
var normal := Style.box(Color(0, 0, 0, 0), 0, [12, 6, 22, 6])
|
||
var hover := Style.box(Style.HOVER_ROW, 0, [12, 6, 22, 6])
|
||
more_row.add_theme_stylebox_override("panel", normal)
|
||
more_row.custom_minimum_size = Vector2(0, 36)
|
||
more_row.mouse_filter = Control.MOUSE_FILTER_STOP
|
||
more_row.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
|
||
more_row.tooltip_text = "Show every result"
|
||
more_row.mouse_entered.connect(func() -> void: more_row.add_theme_stylebox_override("panel", hover))
|
||
more_row.mouse_exited.connect(func() -> void: more_row.add_theme_stylebox_override("panel", normal))
|
||
more_row.gui_input.connect(func(ev: InputEvent) -> void:
|
||
var mb := ev as InputEventMouseButton
|
||
if mb and mb.button_index == MOUSE_BUTTON_LEFT and not mb.pressed:
|
||
card.main.toggle_collapse.call_deferred(card.table.id)
|
||
)
|
||
var h := TableCard.hbox(8)
|
||
more_row.add_child(h)
|
||
var dots := Style.make_label("…", Style.display(true), 16, Style.MUTED)
|
||
dots.custom_minimum_size = Vector2(numw, 0)
|
||
dots.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
|
||
h.add_child(dots)
|
||
more_label = Style.make_label(more_text(), Style.body_italic(true), 14, Style.MUTED)
|
||
more_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
more_label.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
h.add_child(more_label)
|
||
return more_row
|
||
|
||
|
||
func more_text() -> String:
|
||
var table := card.table
|
||
var hidden := table.entries.size() - card.rows.size()
|
||
if card.rows.size() > 0:
|
||
return "%d more · show all" % hidden
|
||
return "%d %s · show all" % [table.entries.size(), "ranges" if table.is_ranged() else "results"]
|
||
|
||
|
||
# ---------- State ----------
|
||
|
||
func set_row_state(eid: String, state: RowState) -> void:
|
||
if not card.rows.has(eid):
|
||
return
|
||
var p: Dictionary = parts[eid]
|
||
var k := int(state) + (3 if eid == card.selected_entry() else 0)
|
||
if int(p["index"]) % 2 == 1 and card.main.local_state.striped_rows():
|
||
k += 6
|
||
# Restyling a row makes its text be measured again, which is slow on big
|
||
# spreads, so a row that already looks this way is left alone.
|
||
card.edit_ui.sync_delete(eid)
|
||
if int(p.get("applied_style", -1)) == k:
|
||
return
|
||
p["applied_style"] = k
|
||
(card.rows[eid] as PanelContainer).add_theme_stylebox_override("panel", _row_styles[k])
|
||
var num: Label = p["num"]
|
||
if num:
|
||
num.add_theme_color_override("font_color", Style.INK if state == RowState.LIT else Style.MUTED)
|
||
var text: RichTextLabel = p["text"]
|
||
if text:
|
||
RichText.set_fonts(text, 16, true, state == RowState.LIT)
|
||
|
||
|
||
## Sets a row lit or normal, from whether its result is lit now.
|
||
func restate(eid: String) -> void:
|
||
set_row_state(eid, RowState.LIT if card.main.is_entry_lit(card.table.id, eid) else RowState.NORMAL)
|
||
|
||
|
||
## Re-renders a Play-mode row's text (it shows the rolled text while lit).
|
||
func update_row_text(eid: String) -> void:
|
||
var p: Dictionary = parts.get(eid, {})
|
||
var text: RichTextLabel = p.get("text")
|
||
if text:
|
||
var want := row_text(p["entry"])
|
||
if text.text != want:
|
||
text.text = want
|
||
columns_ui.refresh(eid)
|
||
|
||
|
||
func flick_random() -> void:
|
||
clear_flick()
|
||
if card.rows.is_empty():
|
||
return
|
||
var keys := card.rows.keys()
|
||
_flick_id = keys[card.main.rng.randi_range(0, keys.size() - 1)]
|
||
set_row_state(_flick_id, RowState.FLICK)
|
||
|
||
|
||
func clear_flick() -> void:
|
||
if _flick_id != "":
|
||
restate(_flick_id)
|
||
_flick_id = ""
|
||
|
||
|
||
## Check outlets and sheet rows live in TableCardKindRows.
|
||
func make_check_row(e: TableEntry, index: int, build: bool, numw: float) -> Control:
|
||
return kind_rows.make_check_row(e, index, build, numw)
|
||
|
||
|
||
func make_sheet_row(e: TableEntry, build: bool) -> Control:
|
||
return kind_rows.make_sheet_row(e, build)
|