TableTower/ui/table_card.gd

352 lines
10 KiB
GDScript3
Raw Normal View History

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 UI: choice pop-up, card kinds, Build creation and settings, tray - ChoiceDialog: card-styled pop-up for pick (option rows with details, Random, 1-9 / R keys), number (spin box with min/max/default, Enter) and text. Cancel, Esc, freeing the dialog, a new roll or a collection switch all answer null through a separate Reply object, so a roll never hangs. - Main: real prompt hook; the steps so far light on the board and in the tray before each question; a roll with questions shows the rest at once; a new roll cancels an open question cleanly; refresh_live() re-renders variable, sheet and question text only after rolls and edits. - TableCard: "?" badge and question for choice cards, loop icon, rule chip and Each time / Afterwards rows, compact live variable cards (reset in Build), live sheet cards with one outlet, expression-dice subtitles, effect chips in Play and a validated effects editor in Build. - Build: "+ Table" menu for every kind; kind-specific settings (choice type, question, save as, min/max; loop mode and rule; variable name, type, starting value; sheet template) checked with Expr.check; expression pipe counts in the pipe popover and on pipe labels. - Tray: effect lines under chips, last 12 steps behind "+N earlier steps", sheet headline, Cancelled / action-limit pill, capped indents and Earlier rows; Variables tab lists every board variable with Reset all. - RichText: error markers in red; previews show {@x_*} and {= expr}. - examples/logic_demo.json and tests/test_logic_ui.gd. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:07:57 -06:00
##
## 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
Logic cards UI: choice pop-up, card kinds, Build creation and settings, tray - ChoiceDialog: card-styled pop-up for pick (option rows with details, Random, 1-9 / R keys), number (spin box with min/max/default, Enter) and text. Cancel, Esc, freeing the dialog, a new roll or a collection switch all answer null through a separate Reply object, so a roll never hangs. - Main: real prompt hook; the steps so far light on the board and in the tray before each question; a roll with questions shows the rest at once; a new roll cancels an open question cleanly; refresh_live() re-renders variable, sheet and question text only after rolls and edits. - TableCard: "?" badge and question for choice cards, loop icon, rule chip and Each time / Afterwards rows, compact live variable cards (reset in Build), live sheet cards with one outlet, expression-dice subtitles, effect chips in Play and a validated effects editor in Build. - Build: "+ Table" menu for every kind; kind-specific settings (choice type, question, save as, min/max; loop mode and rule; variable name, type, starting value; sheet template) checked with Expr.check; expression pipe counts in the pipe popover and on pipe labels. - Tray: effect lines under chips, last 12 steps behind "+N earlier steps", sheet headline, Cancelled / action-limit pill, capped indents and Earlier rows; Variables tab lists every board variable with Reset all. - RichText: error markers in red; previews show {@x_*} and {= expr}. - examples/logic_demo.json and tests/test_logic_ui.gd. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:07:57 -06:00
## 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
var rebuilder: TableCardRebuild
## The ornamental border (null when the theme has none).
var frame: CardFrame
var group_mark: GroupMark
## 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
## True while the inspector shows this card (Build): it glows like a selected card.
var _inspected := 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 FrameSet.active:
frame = CardFrame.new()
add_child(frame)
group_mark = GroupMark.new()
add_child(group_mark)
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)
rebuilder = TableCardRebuild.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()
## Throws away the card's contents and builds them again (TableCardRebuild).
func rebuild() -> void:
rebuilder.run()
Logic cards UI: choice pop-up, card kinds, Build creation and settings, tray - ChoiceDialog: card-styled pop-up for pick (option rows with details, Random, 1-9 / R keys), number (spin box with min/max/default, Enter) and text. Cancel, Esc, freeing the dialog, a new roll or a collection switch all answer null through a separate Reply object, so a roll never hangs. - Main: real prompt hook; the steps so far light on the board and in the tray before each question; a roll with questions shows the rest at once; a new roll cancels an open question cleanly; refresh_live() re-renders variable, sheet and question text only after rolls and edits. - TableCard: "?" badge and question for choice cards, loop icon, rule chip and Each time / Afterwards rows, compact live variable cards (reset in Build), live sheet cards with one outlet, expression-dice subtitles, effect chips in Play and a validated effects editor in Build. - Build: "+ Table" menu for every kind; kind-specific settings (choice type, question, save as, min/max; loop mode and rule; variable name, type, starting value; sheet template) checked with Expr.check; expression pipe counts in the pipe popover and on pipe labels. - Tray: effect lines under chips, last 12 steps behind "+N earlier steps", sheet headline, Cancelled / action-limit pill, capped indents and Earlier rows; Variables tab lists every board variable with Reset all. - RichText: error markers in red; previews show {@x_*} and {= expr}. - examples/logic_demo.json and tests/test_logic_ui.gd. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:07:57 -06:00
## 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 100.0 if RangeWeights.uses_weights(table) else 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)
Logic cards UI: choice pop-up, card kinds, Build creation and settings, tray - ChoiceDialog: card-styled pop-up for pick (option rows with details, Random, 1-9 / R keys), number (spin box with min/max/default, Enter) and text. Cancel, Esc, freeing the dialog, a new roll or a collection switch all answer null through a separate Reply object, so a roll never hangs. - Main: real prompt hook; the steps so far light on the board and in the tray before each question; a roll with questions shows the rest at once; a new roll cancels an open question cleanly; refresh_live() re-renders variable, sheet and question text only after rolls and edits. - TableCard: "?" badge and question for choice cards, loop icon, rule chip and Each time / Afterwards rows, compact live variable cards (reset in Build), live sheet cards with one outlet, expression-dice subtitles, effect chips in Play and a validated effects editor in Build. - Build: "+ Table" menu for every kind; kind-specific settings (choice type, question, save as, min/max; loop mode and rule; variable name, type, starting value; sheet template) checked with Expr.check; expression pipe counts in the pipe popover and on pipe labels. - Tray: effect lines under chips, last 12 steps behind "+N earlier steps", sheet headline, Cancelled / action-limit pill, capped indents and Earlier rows; Variables tab lists every board variable with Reset all. - RichText: error markers in red; previews show {@x_*} and {= expr}. - examples/logic_demo.json and tests/test_logic_ui.gd. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:07:57 -06:00
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()
## The card the inspector shows: glows like a selected card in Build.
func set_inspected(on: bool) -> void:
if _inspected != on:
_inspected = on
skin.apply_style()
## Build-mode selection (Main.selection): a glowing 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;
## a card with columns is wider by its extra columns.
func extra_width() -> float:
var cols := rows_ui.columns_ui.extra_width() if rows_ui else 0.0
return (BUILD_WIDTH - WIDTH if main.mode == Main.Mode.BUILD else 0.0) + cols
## 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 mark_rerolled(eid: String) -> void:
list_ui.mark_rerolled(eid)
func hidden_has_pipes() -> bool:
return list_ui.hidden_has_pipes()