TableTower/ui/table_card_go_button.gd
RobinFiredrake ff76e56daf Size audit 2: split table_card, its header and rows
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 13:30:11 -06:00

100 lines
4.8 KiB
GDScript

class_name TableCardGoButton
extends RefCounted
## The button at the right of a card's header that starts a roll. Its look
## depends on the card kind: a die with "2d6" on roll tables, "?" on choice
## cards, a loop arrow, a fork for routers, "Print" on sheets and a badge on
## checks. Variable cards get no button. TableCardHeader asks for it.
## Builds the go button (null for variable cards). It also fills in
## header.die_icon and header.roll_button.
static func make(header: TableCardHeader) -> Button:
var card := header.card
var table := card.table
header.die_icon = null
header.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
header.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))
header.roll_button = go_button
return go_button
## A fork: one stem splitting into two branches, each ending in a dot (the
## router card's go button). `radius` is about half the icon's height.
static func draw_fork(canvas: CanvasItem, center: Vector2, radius: float, color: Color, width: float = 2.0) -> void:
var base := center + Vector2(-radius, 0)
var split := center + Vector2(-radius * 0.15, 0)
canvas.draw_line(base, split, color, width, true)
var up := center + Vector2(radius * 0.85, -radius * 0.75)
var down := center + Vector2(radius * 0.85, radius * 0.75)
for tip: Vector2 in [up, down]:
var points := PackedVector2Array()
for step in 9:
var weight := float(step) / 8.0
points.append(split.bezier_interpolate(split + Vector2(radius * 0.5, 0), tip - Vector2(radius * 0.45, 0), tip, weight))
canvas.draw_polyline(points, color, width, true)
canvas.draw_circle(tip, width * 1.25, color)
canvas.draw_circle(base, width * 0.9, color)