TableTower/ui/table_card.gd
RobinFiredrake f9aa66d74d Beaded card frames and per-card / per-thread colours
CardFrame draws the user's frame_corner/frame_border art around each card,
tiling the bead strip so it fits any size; a small shader recolours the art
to the theme's trim (or the card's own colour). Themes choose the border
(Tarot: beaded, Felt: plain). Cards (RollTable.color) and threads
(TableEntry.pipe_color, JSON "color") can have their own colour from the
inspector's Colour row (presets, Theme, or any colour).

tools/run_tests.sh imports first and prints at most 8 lines per failing suite.

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

532 lines
17 KiB
GDScript

class_name TableCard
extends PanelContainer
## One table on the board: a header with the collapse toggle, name and die
## button, then its numbered results. Rebuilt from the RollTable whenever the
## mode, the table's shape, or (when collapsed) the lit result changes.
##
## Logic cards (docs/LOGIC.md) share the layout: a choice card has a "?"
## badge, its question and its options (or one Answer row); a loop card shows
## its rule and the Each time / Afterwards rows; a variable card shows its
## live value and has no rows; a sheet card's one row is its rendered
## template; a router card has a fork icon in place of the die and one row
## per case, showing its condition ("if @str >= 8", "Otherwise") with its
## outlet. Live text (values, sheets, prompts) is only re-rendered by
## refresh_live(), which Main calls after rolls and edits, never per frame.
## A pick card whose options come from variables (docs/LOGIC.md §2a) says
## "Options from @skill_* · where …" (in Play with how many are available
## now) and has a Chosen row first; any pick option can have a Show only if
## condition, shown under its row ("only if …").
##
## Rules read in plain words (docs/PLAIN.md §6, Describe) or as their stored
## text, following RulesView.plain(main); cards rebuild on Main's
## rules_view_changed. Build mode only edits result text and the card's name
## inline; everything else (effects, conditions, details, card fields) is
## edited in Main's inspector, so rows show plain chips instead. Clicking a
## result row in Build calls main.select_result(table id, entry id) when Main
## has it; the selected row gets a copper bar on its left, from
## main.inspector_selection() ({"table_id", "entry_id"}) at rebuild time or
## set_selected_entry(entry id), which the inspector can call on any card.
##
## Check cards have a "2D" badge, their rule as a sentence and two outlet rows,
## Success and Failure. Collection cards list their members with live values
## (and "+ Add" in Build, which selects the card for the inspector). A card
## with inputs shows them in a dashed box under the header.
const WIDTH := 256.0
## Build mode has more controls per row, so cards are a little wider there.
const BUILD_WIDTH := 300.0
const LONG_LIST := 12
const LIST_HEIGHT := 432.0
const INLET_Y := 30.0
var main: Main
var table: RollTable
## entry id -> row PanelContainer, for the rows currently shown.
var rows: Dictionary = {}
var more_row: PanelContainer
var list_scroll: ScrollContainer
var header_ui: TableCardHeader
var rows_ui: TableCardRows
var edit_ui: TableCardEditing
var value_ui: TableCardValue
## The ornamental border (null when the theme has none).
var frame: CardFrame
## Rules in plain words (read from RulesView at each rebuild).
var plain := true
var _list_box: VBoxContainer
var _warn_panel: Control
var _warn_label: Label
var _rolling := false
var _dragging := false
var _drop_target := false
var _drag_offset := Vector2.ZERO
var _style_normal: StyleBoxFlat
var _style_rolling: StyleBoxFlat
var _style_dragging: StyleBoxFlat
var _style_drop: StyleBoxFlat
var _style_selected: StyleBoxFlat
var _selected := false
## The result picked in the inspector (copper bar), or "".
var _selected_eid := ""
func setup(p_main: Main, p_table: RollTable) -> void:
main = p_main
table = p_table
custom_minimum_size = Vector2(WIDTH, 0)
mouse_filter = MOUSE_FILTER_PASS
theme = Style.card_theme()
position = table.position
_style_normal = Style.box(Style.CARD, 10)
if Style.CARD_FRAME == "ornate":
_style_normal.set_content_margin_all(CardFrame.PAD)
_style_normal.shadow_color = Color(0, 0, 0, 0.32)
_style_normal.shadow_size = 16
_style_normal.shadow_offset = Vector2(0, 8)
_style_rolling = _style_normal.duplicate() as StyleBoxFlat
Style.bordered(_style_rolling, 3, Style.ACCENT)
_style_rolling.set_expand_margin_all(3)
_style_rolling.shadow_color = Color(Style.ACCENT, 0.45)
_style_rolling.shadow_size = 26
_style_rolling.shadow_offset = Vector2.ZERO
_style_drop = _style_rolling.duplicate() as StyleBoxFlat
_style_drop.shadow_color = Color(0, 0, 0, 0.32)
_style_drop.shadow_size = 16
_style_drop.shadow_offset = Vector2(0, 8)
_style_selected = _style_normal.duplicate() as StyleBoxFlat
Style.bordered(_style_selected, 3, Style.TRIM)
_style_selected.set_expand_margin_all(4)
_style_selected.shadow_color = Color(Style.TRIM, 0.5)
_style_selected.shadow_size = 14
_style_selected.shadow_offset = Vector2.ZERO
_style_dragging = _style_normal.duplicate() as StyleBoxFlat
_style_dragging.shadow_color = Color(0, 0, 0, 0.4)
_style_dragging.shadow_size = 26
_style_dragging.shadow_offset = Vector2(0, 16)
if Style.CARD_FRAME == "ornate":
frame = CardFrame.new()
add_child(frame)
header_ui = TableCardHeader.new(self)
rows_ui = TableCardRows.new(self)
edit_ui = TableCardEditing.new(self)
value_ui = TableCardValue.new(self)
minimum_size_changed.connect(func() -> void: _shrink.call_deferred())
RulesView.watch(main, _on_rules_view_changed)
rebuild()
## The frame follows the card's own colour (RollTable.color), else the theme's.
## Without a frame, the card's colour is a border instead.
func _sync_frame() -> void:
var own := Color.from_string(table.color, Color(0, 0, 0, 0)) if table.color != "" else Color(0, 0, 0, 0)
if frame:
frame.set_trim(own)
else:
for sb: StyleBoxFlat in [_style_normal, _style_dragging]:
Style.bordered(sb, 3 if own.a > 0.0 else 0, own)
func _on_rules_view_changed(_v: Variant = null) -> void:
rebuild()
func rebuild() -> void:
for c in get_children():
if c == frame:
continue
remove_child(c)
c.queue_free()
_sync_frame()
rows.clear()
rows_ui.clear()
more_row = null
list_scroll = null
header_ui.clear()
value_ui.clear()
plain = RulesView.plain(main)
if main.has_method("inspector_selection"):
var sel: Variant = main.call("inspector_selection")
_selected_eid = ""
# The inspector reports {kind, tid, eid}; older callers used {table_id, entry_id}.
if sel is Dictionary:
var d := sel as Dictionary
var tid := str(d.get("tid", d.get("table_id", "")))
if tid == table.id and str(d.get("kind", "result")) == "result":
_selected_eid = str(d.get("eid", d.get("entry_id", "")))
var build := main.mode == Main.Mode.BUILD
custom_minimum_size = Vector2(card_width(), 0)
_shrink.call_deferred()
var ranged := table.is_ranged()
var collapsed := is_collapsed()
var col := vbox(0)
add_child(col)
col.add_child(header_ui.make(build, ranged))
if not table.inputs.is_empty() and table.kind != RollTable.KIND_VARIABLE:
col.add_child(header_ui.make_inputs_box())
if table.kind == RollTable.KIND_VARIABLE:
col.add_child(value_ui.make_collection_body(build) if table.is_collection() else value_ui.make_variable_body(build))
_warn_panel = _make_warning()
col.add_child(_warn_panel)
update_warning()
if build:
col.add_child(edit_ui.make_footer(ranged))
_apply_style()
refresh_live()
return
var pad := MarginContainer.new()
pad.mouse_filter = MOUSE_FILTER_PASS
pad.add_theme_constant_override("margin_top", 6)
pad.add_theme_constant_override("margin_bottom", 8)
_list_box = vbox(0)
pad.add_child(_list_box)
if not collapsed and table.entries.size() > LONG_LIST:
list_scroll = ScrollContainer.new()
list_scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
list_scroll.custom_minimum_size = Vector2(0, LIST_HEIGHT)
list_scroll.mouse_filter = MOUSE_FILTER_PASS
_skin_scrollbar(list_scroll.get_v_scroll_bar())
pad.size_flags_horizontal = SIZE_EXPAND_FILL
list_scroll.add_child(pad)
col.add_child(list_scroll)
else:
col.add_child(pad)
var numw := _num_width(build, ranged)
for i in table.entries.size():
var e := table.entries[i]
var lit := main.is_entry_lit(table.id, e.id)
if collapsed and not lit:
continue
if table.kind == RollTable.KIND_SHEET:
_list_box.add_child(rows_ui.make_sheet_row(e, build))
elif table.kind == RollTable.KIND_ROUTER and build:
_list_box.add_child(edit_ui.make_case_row(e, i, numw))
elif table.kind == RollTable.KIND_CHECK:
_list_box.add_child(rows_ui.make_check_row(e, i, build, numw))
else:
_list_box.add_child(rows_ui.make_row(e, i, build, ranged, numw))
rows_ui.set_row_state(e.id, TableCardRows.RowState.LIT if lit else TableCardRows.RowState.NORMAL)
if collapsed:
more_row = rows_ui.make_more_row(numw)
_list_box.add_child(more_row)
_warn_panel = _make_warning()
col.add_child(_warn_panel)
update_warning()
if build:
col.add_child(edit_ui.make_footer(ranged))
_apply_style()
refresh_live()
if list_scroll != null:
for eid in rows:
if main.is_entry_lit(table.id, eid):
_reveal_when_laid_out.call_deferred(eid)
break
## Variable and sheet cards always show everything; other cards collapse.
func is_collapsed() -> bool:
return table.collapsed and can_collapse()
func can_collapse() -> bool:
return table.kind != RollTable.KIND_VARIABLE and table.kind != RollTable.KIND_SHEET
func _reveal_when_laid_out(eid: String) -> void:
await get_tree().process_frame
if is_instance_valid(list_scroll):
reveal(eid)
# ---------- Building blocks ----------
static func vbox(sep: int) -> VBoxContainer:
var v := VBoxContainer.new()
v.add_theme_constant_override("separation", sep)
v.mouse_filter = MOUSE_FILTER_PASS
return v
static func hbox(sep: int) -> HBoxContainer:
var h := HBoxContainer.new()
h.add_theme_constant_override("separation", sep)
h.mouse_filter = MOUSE_FILTER_PASS
return h
func _num_width(build: bool, ranged: bool) -> float:
var wide := table.die_size() >= 100
if build and ranged:
return 90.0
if ranged:
return 52.0 if wide else 40.0
return 24.0 if wide else 22.0
# Card texts now live in TableCardText; other files still call these here.
static func case_label(e: TableEntry) -> String:
return TableCardText.case_label(e)
static func loop_rule(t: RollTable) -> String:
return TableCardText.loop_rule(t)
static func options_line(t: RollTable) -> String:
return TableCardText.options_line(t)
static func check_outlet_name(index: int) -> String:
return TableCardText.check_outlet_name(index)
func _make_warning() -> Control:
var wrap := MarginContainer.new()
wrap.mouse_filter = MOUSE_FILTER_PASS
wrap.add_theme_constant_override("margin_left", 12)
wrap.add_theme_constant_override("margin_right", 12)
wrap.add_theme_constant_override("margin_bottom", 10)
var panel := PanelContainer.new()
panel.add_theme_stylebox_override("panel", Style.box(Style.WARN_BG, 6, [9, 6, 9, 6]))
panel.mouse_filter = MOUSE_FILTER_PASS
_warn_label = Style.make_label("", Style.body(true), 13, Style.WARN_INK)
_warn_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
panel.add_child(_warn_label)
wrap.add_child(panel)
return wrap
func update_warning() -> void:
var problems := table.range_problems()
var lines := PackedStringArray()
for item: Dictionary in Roller.lint_router(table):
lines.append(str(item["message"]))
for item: Dictionary in Roller.lint_options(table):
lines.append(str(item["message"]))
if not lines.is_empty():
problems = ("%s\n" % problems if problems != "" else "") + "\n".join(lines)
_warn_label.text = problems
_warn_panel.visible = problems != ""
func _skin_scrollbar(bar: VScrollBar) -> void:
var track := Style.box(Color(0, 0, 0, 0), 3, [3, 0, 3, 0])
var grab := Style.box(Color(0, 0, 0, 0.22), 3, [3, 0, 3, 0])
var grab_hi := Style.box(Color(0, 0, 0, 0.35), 3, [3, 0, 3, 0])
bar.add_theme_stylebox_override("scroll", track)
bar.add_theme_stylebox_override("scroll_focus", track)
bar.add_theme_stylebox_override("grabber", grab)
bar.add_theme_stylebox_override("grabber_highlight", grab_hi)
bar.add_theme_stylebox_override("grabber_pressed", grab_hi)
# ---------- State ----------
## Selects a result for the inspector: main.select_result(table id, entry id)
## when Main has it, and the copper bar on this row (cleared on other cards).
func select_entry(eid: String) -> void:
if main.mode != Main.Mode.BUILD:
return
if main.has_method("select_result"):
main.call("select_result", table.id, eid)
for c: Variant in main.cards.values():
if c is TableCard and c != self:
(c as TableCard).set_selected_entry("")
set_selected_entry(eid)
## Shows the copper selection bar on one result row ("" for none). The
## inspector may call this on every card when its selection changes.
func set_selected_entry(eid: String) -> void:
if _selected_eid == eid:
return
var old := _selected_eid
_selected_eid = eid
for k: String in [old, eid]:
if k != "" and rows.has(k):
rows_ui.restate(k)
func selected_entry() -> String:
return _selected_eid
## Refreshes the cards-left count of a card-mode table.
func update_deck() -> void:
header_ui.update_deck()
## Re-renders the live parts (a variable's value, a sheet, a choice's
## question) from the current variables. Only touches labels whose text
## changed. Main calls it after rolls and edits, never per frame.
func refresh_live() -> void:
value_ui.refresh()
rows_ui.refresh_sheet()
rows_ui.refresh_odds()
header_ui.refresh_live()
func _apply_style() -> void:
var s := _style_normal
if _drop_target:
s = _style_drop
elif _rolling:
s = _style_rolling
elif _selected:
s = _style_selected
elif _dragging:
s = _style_dragging
add_theme_stylebox_override("panel", s)
## Build-mode selection (Main.selection): a copper ring around the card.
func set_selected(on: bool) -> void:
if _selected != on:
_selected = on
_apply_style()
func is_selected() -> bool:
return _selected
func _shrink() -> void:
size = Vector2(card_width(), 0)
func card_width() -> float:
return BUILD_WIDTH if main.mode == Main.Mode.BUILD else WIDTH
func set_rolling(on: bool) -> void:
_rolling = on
if is_instance_valid(header_ui.die_icon):
header_ui.die_icon.spinning = on
_apply_style()
func set_drop_target(on: bool) -> void:
_drop_target = on
_apply_style()
func flick_random() -> void:
rows_ui.flick_random()
func clear_flick() -> void:
rows_ui.clear_flick()
func set_more_text(text: String) -> void:
if rows_ui.more_label:
rows_ui.more_label.text = text
## Updates highlights after a roll. Collapsed cards rebuild so the rolled result shows.
func refresh_lit() -> void:
update_deck()
if is_collapsed():
var want: Array = []
for e in table.entries:
if main.is_entry_lit(table.id, e.id):
want.append(e.id)
if want != rows.keys():
rebuild()
elif rows_ui.more_label:
rows_ui.more_label.text = rows_ui.more_text()
return
for eid in rows:
rows_ui.update_row_text(eid)
rows_ui.restate(eid)
## Scrolls a long list so the given result sits in the middle.
func reveal(eid: String) -> void:
if list_scroll == null or not rows.has(eid):
return
var row: Control = rows[eid]
list_scroll.scroll_vertical = int(row.position.y + 6.0 - list_scroll.size.y / 2.0 + row.size.y / 2.0)
func hidden_has_pipes() -> bool:
for e in table.entries:
if not rows.has(e.id) and (not e.pipes.is_empty() or not main.ref_targets_for(e).is_empty()):
return true
return false
# ---------- Geometry (world coordinates) ----------
func inlet_point() -> Vector2:
return Vector2(position.x, position.y + INLET_Y)
## Where a pipe leaves this card for an entry: its row, or the "more" row when the
## entry is hidden in a collapsed card. Rows scrolled out of a long list clamp to
## its edge. Returns {} if there's nowhere to anchor.
func anchor_for(eid: String) -> Dictionary:
var row: Control = rows.get(eid, more_row)
if row == null or not row.is_inside_tree():
return {}
var to_world := main.board.world.get_global_transform().affine_inverse()
var r := row.get_global_rect()
var y := (to_world * (r.position + Vector2(0, r.size.y / 2.0))).y
var shown := true
if list_scroll != null and rows.has(eid):
var sr := list_scroll.get_global_rect()
var top := (to_world * sr.position).y + 14.0
var bottom := (to_world * sr.end).y - 14.0
shown = y >= top - 4.0 and y <= bottom + 4.0
y = clampf(y, top, bottom)
return {"pos": Vector2(position.x + size.x, y), "visible": shown}
# ---------- Input ----------
func _gui_input(ev: InputEvent) -> void:
# Clicks on the card body shouldn't pan the board; the wheel still passes through.
var mb := ev as InputEventMouseButton
if mb and mb.button_index == MOUSE_BUTTON_LEFT:
accept_event()
## Dragging the header moves the card (and, in Build, every selected card).
## In Build, Ctrl- or Shift-click adds the card to the selection or takes it
## out; dropping a card into a group frame adds it to that group (Main).
func _on_header_input(ev: InputEvent, header: Control) -> void:
var mb := ev as InputEventMouseButton
if mb and mb.button_index == MOUSE_BUTTON_LEFT:
if mb.pressed:
main.details().hide_for()
var build := main.mode == Main.Mode.BUILD
if build and (mb.ctrl_pressed or mb.shift_pressed or mb.meta_pressed):
main.toggle_selected(table.id)
header.accept_event()
return
if build and not main.is_selected(table.id):
main.clear_selection()
_dragging = true
_drag_offset = main.board.world_mouse() - position
get_parent().move_child(self, -1)
main.begin_card_drag(self)
elif _dragging:
_dragging = false
main.end_card_drag(self)
else:
main.mark_dirty()
_apply_style()
header.accept_event()
return
var mm := ev as InputEventMouseMotion
if mm and _dragging:
var p := main.board.world_mouse() - _drag_offset
main.drag_card(self, Vector2(maxf(10.0, roundf(p.x)), maxf(10.0, roundf(p.y))))
header.accept_event()