TableTower/ui/table_card.gd
RobinFiredrake 82d67eb64f Size audit: split ChoiceDialog and TableCard into helpers
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:56:20 -06:00

391 lines
11 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
var skin: TableCardSkin
var input: TableCardInput
var geometry: TableCardGeometry
var list_ui: TableCardList
## The ornamental border (null when the theme has none).
var frame: CardFrame
## The resize handle (CardGrip), kept as the last child.
var grip: CardGrip
## Takes the mouse while zoomed out (CardShield).
var shield: CardShield
## 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
skin = TableCardSkin.new(self)
input = TableCardInput.new(self)
geometry = TableCardGeometry.new(self)
list_ui = TableCardList.new(self)
skin.build_styles()
if Style.CARD_FRAME == "ornate":
frame = CardFrame.new()
add_child(frame)
grip = CardGrip.new(self)
add_child(grip)
shield = CardShield.new(main, _on_header_input, open_menu_on, func() -> void: main.rolling.start_roll(table.id))
add_child(shield)
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()
func _on_rules_view_changed(_v: Variant = null) -> void:
rebuild()
func rebuild() -> void:
for c in get_children():
if c == frame or c == grip or c == shield:
continue
remove_child(c)
c.queue_free()
skin.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 = skin.make_warning()
col.add_child(_warn_panel)
update_warning()
if build:
col.add_child(edit_ui.make_footer(ranged))
skin.apply_style()
refresh_live()
input.raise_covers()
return
list_ui.fill(col, build, ranged, collapsed)
_warn_panel = skin.make_warning()
col.add_child(_warn_panel)
update_warning()
if build:
col.add_child(edit_ui.make_footer(ranged))
skin.apply_style()
refresh_live()
input.raise_covers()
skin.dim_drawn()
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)
# ---------- 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()
skin.dim_drawn()
## 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()
## Build-mode selection (Main.selection): a copper ring around the card.
func set_selected(on: bool) -> void:
if _selected != on:
_selected = on
skin.apply_style()
func is_selected() -> bool:
return _selected
func _shrink() -> void:
size = Vector2(card_width(), 0)
func card_width() -> float:
return (table.width if table.width > 0.0 else WIDTH) + extra_width()
## Build mode has more controls per row, so cards are a little wider there.
func extra_width() -> float:
return BUILD_WIDTH - WIDTH if main.mode == Main.Mode.BUILD else 0.0
## The height the results take now (the scrolling list's, if there is one).
func list_area_height() -> float:
if is_instance_valid(list_scroll):
return list_scroll.size.y
if is_instance_valid(_list_box) and _list_box.get_parent() is Control:
return (_list_box.get_parent() as Control).size.y
return 0.0
func set_rolling(on: bool) -> void:
_rolling = on
if is_instance_valid(header_ui.die_icon):
header_ui.die_icon.spinning = on
skin.apply_style()
func set_drop_target(on: bool) -> void:
_drop_target = on
skin.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
# ---------- Geometry (world coordinates) ----------
# ---------- Input ----------
# ---------- Delegates (the helpers hold the code; these keep the public API) ----------
func update_warning() -> void:
skin.update_warning()
func set_user_size(w: float, list_h: float) -> void:
geometry.set_user_size(w, list_h)
func inlet_point() -> Vector2:
return geometry.inlet_point()
func anchor_for(eid: String) -> Dictionary:
return geometry.anchor_for(eid)
func _gui_input(ev: InputEvent) -> void:
input.gui_input(ev)
func open_menu_on(mb: InputEventMouseButton) -> bool:
return input.open_menu_on(mb)
func _on_header_input(ev: InputEvent, header: Control) -> void:
input.on_header_input(ev, header)
func refresh_lit() -> void:
list_ui.refresh_lit()
func reveal(eid: String) -> void:
list_ui.reveal(eid)
func sparkle(eid: String) -> void:
list_ui.sparkle(eid)
func hidden_has_pipes() -> bool:
return list_ui.hidden_has_pipes()