TableTower/ui/table_card_header.gd
RobinFiredrake c009021ac3 Descriptive names rule; first fix in TableCardHeader._make_go_button
DESIGN.md §5: no one- or two-letter names (loop counters and math
coordinates excepted); old names are fixed when the code is touched.
Rename b -> go_button and rn/rh/rp -> normal_style/hover_style/
pressed_style. Also note where Coderstanding is now installed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:18:54 -06:00

407 lines
17 KiB
GDScript

class_name TableCardHeader
extends RefCounted
## The top of a TableCard: collapse chevron, name (or its inline editor and
## ⋯ menu in Build), the go button, the card-mode deck row and the
## kind-specific lines under the title, plus the dashed inputs box. Owns the
## live labels refresh_live() updates (a choice's question, "3 available now").
var card: TableCard
var die_icon: DieIcon
## The die button (disabled while a range runs backwards).
var roll_button: Button
var deck_label: Label
## Play mode: "3 available now" on a dynamic pick card.
var options_label: Label
var prompt_label: RichTextLabel
func _init(p_card: TableCard) -> void:
card = p_card
## Drops the references to the previous build's labels.
func clear() -> void:
deck_label = null
prompt_label = null
options_label = null
func make(build: bool, ranged: bool) -> Control:
var table := card.table
var main := card.main
var header := PanelContainer.new()
var hs := Style.box(Color(0, 0, 0, 0), 0, [12, 10, 10, 9])
hs.border_width_bottom = 1
hs.border_color = Style.RULE
header.add_theme_stylebox_override("panel", hs)
header.mouse_filter = Control.MOUSE_FILTER_PASS
header.mouse_default_cursor_shape = Control.CURSOR_MOVE
header.gui_input.connect(card._on_header_input.bind(header))
var row := TableCard.hbox(8)
var head_box := TableCard.vbox(4)
header.add_child(head_box)
head_box.add_child(row)
if card.can_collapse():
var chevron := ChevronButton.new()
chevron.open = not table.collapsed
chevron.tooltip_text = ("Expand " if table.collapsed else "Collapse ") + table.name
chevron.size_flags_vertical = Control.SIZE_SHRINK_CENTER
chevron.pressed.connect(func() -> void: main.toggle_collapse(table.id))
row.add_child(chevron)
header.mouse_entered.connect(func() -> void:
if not card._dragging and main.mode == Main.Mode.PLAY:
main.details().show_table(header, table.id, true)
)
if build:
var name_edit := LineEdit.new()
name_edit.text = table.name
name_edit.size_flags_horizontal = Control.SIZE_EXPAND_FILL
name_edit.size_flags_vertical = Control.SIZE_SHRINK_CENTER
name_edit.custom_minimum_size = Vector2(40, 0)
name_edit.add_theme_font_override("font", Style.display(true))
name_edit.add_theme_font_size_override("font_size", 18)
name_edit.add_theme_stylebox_override("normal", Style.bordered(Style.box(Color.WHITE, 6, [7, 3, 7, 3]), 1, Style.RULE))
name_edit.text_changed.connect(func(s: String) -> void:
table.name = s
main.mark_dirty()
)
row.add_child(name_edit)
row.add_child(card.edit_ui.make_card_menu())
else:
var titles := TableCard.vbox(3)
titles.size_flags_horizontal = Control.SIZE_EXPAND_FILL
titles.size_flags_vertical = Control.SIZE_SHRINK_CENTER
titles.mouse_filter = Control.MOUSE_FILTER_IGNORE
var title := Style.make_label(Formatter.strip_tags(table.name), Style.display(true), 20, Style.INK)
title.autowrap_mode = TextServer.AUTOWRAP_WORD
if table.details.strip_edges() != "":
# A small ⓘ after the name says hovering shows the table's details.
var name_row := TableCard.hbox(4)
name_row.mouse_filter = Control.MOUSE_FILTER_IGNORE
title.size_flags_horizontal = Control.SIZE_EXPAND_FILL
name_row.add_child(title)
var mark := Style.make_label(RichText.INFO_MARK, Style.body(true), 12, Style.FAINT)
mark.size_flags_vertical = Control.SIZE_SHRINK_CENTER
name_row.add_child(mark)
titles.add_child(name_row)
else:
titles.add_child(title)
var sub := _subtitle()
if sub != "":
var cl := Style.make_label(sub, Style.body(true), 12, Style.MUTED)
cl.mouse_filter = Control.MOUSE_FILTER_IGNORE
cl.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
titles.add_child(cl)
row.add_child(titles)
var deck_row: Control = null
if table.card_mode:
deck_row = _make_deck_row()
var go := _make_go_button()
if go != null:
row.add_child(go)
if deck_row != null:
head_box.add_child(deck_row)
var extra := _make_kind_header(build)
if extra != null:
head_box.add_child(extra)
return header
## The small line under the title in Play mode ("2d6 + dm(@end) · 2 ranges").
func _subtitle() -> String:
var table := card.table
match table.kind:
RollTable.KIND_ROLL:
if table.is_ranged():
var n := "%d ranges" % table.entries.size()
if table.clamps():
return "%s · %s" % [RulesView.roll_line(table, card.main.data, card.plain), n]
return n
if table.collapsed:
return "%d results" % table.entries.size()
RollTable.KIND_CHOICE:
if table.collapsed:
return "%d options" % table.entries.size()
RollTable.KIND_ROUTER:
return "if / else if / otherwise · %d %s" % [table.entries.size(), "case" if table.entries.size() == 1 else "cases"]
RollTable.KIND_CHECK:
return "Check card"
return ""
## The header button that starts a roll: the die on roll tables, a "?" badge on
## choice cards, a loop arrow on loop cards and "Print" on sheets. Variable
## cards have none.
func _make_go_button() -> Button:
var table := card.table
die_icon = null
roll_button = null
if table.kind == RollTable.KIND_VARIABLE:
return null
var go_button := Button.new()
go_button.size_flags_vertical = Control.SIZE_SHRINK_CENTER
go_button.add_theme_font_override("font", Style.display(true))
go_button.add_theme_font_size_override("font_size", 17)
var left := 10
match table.kind:
RollTable.KIND_CHOICE:
go_button.text = "?"
go_button.add_theme_font_size_override("font_size", 20)
go_button.custom_minimum_size = Vector2(36, 0)
go_button.tooltip_text = "Ask: %s" % Formatter.strip_tags(table.prompt if table.prompt != "" else table.name)
RollTable.KIND_LOOP:
go_button.custom_minimum_size = Vector2(38, 32)
go_button.tooltip_text = "Run %s" % Formatter.strip_tags(table.name)
var icon := Control.new()
icon.mouse_filter = Control.MOUSE_FILTER_IGNORE
icon.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
icon.draw.connect(func() -> void: Icons.draw_loop(icon, icon.size / 2.0 + Vector2(0, -1), 7.0, Style.ACCENT, 2.2))
go_button.add_child(icon)
RollTable.KIND_ROUTER:
go_button.custom_minimum_size = Vector2(38, 32)
go_button.tooltip_text = "Route %s: the first case that's true is chosen" % Formatter.strip_tags(table.name)
var fork := Control.new()
fork.mouse_filter = Control.MOUSE_FILTER_IGNORE
fork.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
fork.draw.connect(func() -> void: draw_fork(fork, fork.size / 2.0 + Vector2(0, 0), 8.0, Style.ACCENT, 2.2))
go_button.add_child(fork)
RollTable.KIND_SHEET:
go_button.text = "Print"
go_button.add_theme_font_override("font", Style.body_bold(true))
go_button.add_theme_font_size_override("font_size", 14)
go_button.tooltip_text = "Print this sheet in the tray"
RollTable.KIND_CHECK:
go_button.text = TableCardText.check_badge(table)
go_button.custom_minimum_size = Vector2(38, 0)
go_button.tooltip_text = "Roll the check: %s\nShift-click to skip the animation" % RulesView.check_line(table, card.main.data, card.plain)
_:
left = 34
var label := "d%d" % table.die_size()
if table.has_mod() and not table.is_expr_dice():
# The dice, not the ranges' end (which a modifier stretches).
label = table.dice_label()
elif table.is_expr_dice():
label = table.dice_label() if table.dice_label().length() <= 5 else "Roll"
go_button.text = label
go_button.tooltip_text = "Roll %s on %s\nShift-click to skip the animation" % [table.roll_text() if table.is_ranged() else table.dice_label(), table.name]
var normal_style := Style.box(Style.BG, 8, [left, 6, 10, 6])
normal_style.border_width_bottom = 2
normal_style.border_color = Style.BG_DEEP
var hover_style := normal_style.duplicate() as StyleBoxFlat
hover_style.bg_color = Style.BG_HOVER
var pressed_style := normal_style.duplicate() as StyleBoxFlat
pressed_style.border_width_bottom = 1
pressed_style.content_margin_top = 7
pressed_style.content_margin_bottom = 5
Style.skin_button(go_button, normal_style, hover_style, pressed_style, Style.ACCENT)
if table.kind == RollTable.KIND_ROLL:
var die := DieIcon.new()
die.spinning = card._rolling
die_icon = die
go_button.add_child(die)
go_button.resized.connect(func() -> void: die.position = Vector2(8, (go_button.size.y - 20.0) / 2.0 - 1.0))
go_button.pressed.connect(func() -> void: card.main.start_roll(table.id))
roll_button = go_button
return go_button
## Kind-specific lines under the header: a choice card's question, a loop's
## rule, a variable's name.
func _make_kind_header(_build: bool) -> Control:
var table := card.table
var data := card.main.data
var plain := card.plain
match table.kind:
RollTable.KIND_CHOICE:
var v := TableCard.vbox(3)
prompt_label = RichText.label("", 15, Style.INK, true)
prompt_label.mouse_filter = Control.MOUSE_FILTER_IGNORE
v.add_child(prompt_label)
var info := Describe.choice_label(table.choice)
if table.choice == "number":
info += " · %s to %s" % [RulesView.dice(table.min_expr, data, plain), RulesView.dice(table.max_expr, data, plain)]
if table.save_as != "":
info += " · saves %s" % RulesView.value_name(table.save_as, data, plain)
var l := Style.make_label(info, Style.body(true), 12, Style.MUTED)
l.mouse_filter = Control.MOUSE_FILTER_IGNORE
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
v.add_child(l)
if table.is_dynamic():
var bad := not Roller.lint_options(table).is_empty()
var ol := Style.make_label(RulesView.options_line(table, data, plain), Style.body_bold(true), 12, Style.DANGER_INK if bad else Style.TRIM_DARK)
ol.mouse_filter = Control.MOUSE_FILTER_PASS
ol.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
ol.tooltip_text = "One option per %s; the Chosen result runs for whichever is picked (option, option_label and option_value hold it)" % table.options_description()
v.add_child(ol)
if not _build:
options_label = Style.make_label("", Style.body(true), 12, Style.MUTED)
options_label.mouse_filter = Control.MOUSE_FILTER_IGNORE
v.add_child(options_label)
return v
RollTable.KIND_LOOP:
var chip := PanelContainer.new()
chip.mouse_filter = Control.MOUSE_FILTER_PASS
chip.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
chip.add_theme_stylebox_override("panel", Style.bordered(Style.box(Color(Style.ACCENT, 0.16), 6, [8, 1, 8, 2]), 1, Color(Style.ACCENT_DARK, 0.35)))
var err := Expr.check(table.loop_expr) if table.loop_expr.strip_edges() != "" else "No rule yet"
var rule := RulesView.loop_rule(table, data, plain)
var l := Style.make_label(rule, Style.display(true), 16, Style.DANGER_INK if err != "" else Style.TRIM_DARK)
l.mouse_filter = Control.MOUSE_FILTER_IGNORE
if rule.length() > 26:
# A long plain rule wraps across the card instead of widening it.
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
chip.size_flags_horizontal = Control.SIZE_FILL
chip.add_child(l)
chip.tooltip_text = err if err != "" else (TableCardText.loop_rule(table) if plain else "")
return chip
RollTable.KIND_ROUTER:
if not _build:
return null
var rl := Style.make_label("Branch · the first true case is chosen", Style.body(true), 12, Style.MUTED)
rl.mouse_filter = Control.MOUSE_FILTER_IGNORE
rl.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
return rl
RollTable.KIND_VARIABLE:
var txt := "@%s · %s" % [table.var_name, table.var_type]
if table.is_collection():
txt = "Ledger · kept between rolls" if plain else "@%s_* (spread values)" % table.var_name
elif plain:
txt = "%s · kept between rolls" % table.var_type.capitalize()
var l := Style.make_label(txt, Style.body(true), 12, Style.MUTED)
l.mouse_filter = Control.MOUSE_FILTER_IGNORE
l.tooltip_text = "@" + table.var_name if plain and not table.is_collection() else ""
if l.tooltip_text != "":
l.mouse_filter = Control.MOUSE_FILTER_PASS
return l
RollTable.KIND_CHECK:
var cl := RichText.label(TableCardText.check_sentence(table, data, plain), 14, Style.INK, true)
cl.mouse_filter = Control.MOUSE_FILTER_IGNORE
return cl
return null
## Re-renders the header's live labels: a dynamic pick's available count and a
## choice's question.
func refresh_live() -> void:
var table := card.table
var main := card.main
if is_instance_valid(options_label):
var n := Roller.available_count(table, Roller.live_context(main.data, main.persistent_vars))
var txt := "No options available now" if n == 0 else ("1 option available now" if n == 1 else "%d options available now" % n)
if options_label.text != txt:
options_label.text = txt
if is_instance_valid(prompt_label):
var p: String
if main.mode == Main.Mode.BUILD:
p = RichText.preview(table.prompt, main.data)
else:
p = RichText.decorate(TextExpander.render(table.prompt, Roller.live_context(main.data, main.persistent_vars)))
if prompt_label.text != p:
prompt_label.text = p
prompt_label.visible = table.prompt.strip_edges() != ""
## A dashed box under the header naming the card's inputs: "Input Tier ·
## starts at 1" (plain) or "input tier · default 1" (text), one line each.
func make_inputs_box() -> Control:
var wrap := MarginContainer.new()
wrap.mouse_filter = Control.MOUSE_FILTER_PASS
wrap.add_theme_constant_override("margin_left", 10)
wrap.add_theme_constant_override("margin_right", 10)
wrap.add_theme_constant_override("margin_top", 8)
var p := PanelContainer.new()
p.mouse_filter = Control.MOUSE_FILTER_PASS
p.add_theme_stylebox_override("panel", DashedBox.new())
p.tooltip_text = "Values a thread into this card can pass (\"with\"); a roll that starts here uses the starting values"
wrap.add_child(p)
var v := TableCard.vbox(2)
p.add_child(v)
for inp: Dictionary in card.table.inputs:
var line := RichText.label(TableCardText.input_line(inp, card.main.data, card.plain), 13, Style.MUTED, true)
line.mouse_filter = Control.MOUSE_FILTER_IGNORE
line.set_meta("input_line", true)
v.add_child(line)
wrap.set_meta("inputs_box", true)
return wrap
## A dashed rounded outline (the inputs box), since StyleBoxFlat has no
## dashes.
class DashedBox extends StyleBox:
func _init() -> void:
content_margin_left = 9
content_margin_right = 9
content_margin_top = 6
content_margin_bottom = 6
func _draw(to_canvas_item: RID, rect: Rect2) -> void:
var r := rect.grow(-0.5)
var pts := PackedVector2Array([r.position, Vector2(r.end.x, r.position.y), r.end, Vector2(r.position.x, r.end.y), r.position])
var col := Style.DASH
for i in 4:
var a := pts[i]
var b := pts[i + 1]
var seg_len := a.distance_to(b)
var d := 0.0
while d < seg_len:
var e := minf(d + 5.0, seg_len)
RenderingServer.canvas_item_add_line(to_canvas_item, a.lerp(b, d / seg_len), a.lerp(b, e / seg_len), col, 1.2, true)
d += 9.0
## A fork: one stem splitting into two branches, each ending in a dot (the
## router card's go button). `r` is about half the icon's height.
static func draw_fork(ci: CanvasItem, c: Vector2, r: float, color: Color, width: float = 2.0) -> void:
var base := c + Vector2(-r, 0)
var split := c + Vector2(-r * 0.15, 0)
ci.draw_line(base, split, color, width, true)
var up := c + Vector2(r * 0.85, -r * 0.75)
var down := c + Vector2(r * 0.85, r * 0.75)
for tip: Vector2 in [up, down]:
var pts := PackedVector2Array()
for k in 9:
var u := float(k) / 8.0
pts.append(split.bezier_interpolate(split + Vector2(r * 0.5, 0), tip - Vector2(r * 0.45, 0), tip, u))
ci.draw_polyline(pts, color, width, true)
ci.draw_circle(tip, width * 1.25, color)
ci.draw_circle(base, width * 0.9, color)
## Deck mode: "4 / 6 left" and a small Shuffle link under the title.
func _make_deck_row() -> Control:
var h := TableCard.hbox(6)
var pad := Control.new()
pad.custom_minimum_size = Vector2(26, 0)
pad.mouse_filter = Control.MOUSE_FILTER_IGNORE
h.add_child(pad)
deck_label = Style.make_label("", Style.body(true), 12, Style.MUTED)
deck_label.size_flags_vertical = Control.SIZE_SHRINK_CENTER
deck_label.mouse_filter = Control.MOUSE_FILTER_IGNORE
h.add_child(deck_label)
var b := Button.new()
b.text = "↻ Shuffle"
b.tooltip_text = "Put every card back in the deck"
b.add_theme_font_override("font", Style.body(true))
b.add_theme_font_size_override("font_size", 12)
b.size_flags_vertical = Control.SIZE_SHRINK_CENTER
var n := Style.box(Color(0, 0, 0, 0), 5, [5, 0, 5, 1])
var hv := Style.box(Style.CARD_HOVER, 5, [5, 0, 5, 1])
Style.skin_button(b, n, hv, hv, Style.TRIM, Style.INK)
b.pressed.connect(func() -> void: card.main.reshuffle_table(card.table.id))
h.add_child(b)
update_deck()
return h
## Refreshes the cards-left count of a card-mode table.
func update_deck() -> void:
if deck_label == null or not is_instance_valid(deck_label):
return
var left := card.main.cards_left(card.table.id)
deck_label.text = "%d / %d left" % [left.x, left.y]
deck_label.add_theme_color_override("font_color", Style.DANGER_INK if left.x == 0 else Style.MUTED)