New theme colours: Card header (themes without it get a shade between the card face and Card shading) and Selected (default Morgan's eye glint). The card the inspector shows glows too, so one selected card is easy to see. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
343 lines
13 KiB
GDScript
343 lines
13 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 (TableCardGoButton), 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()
|
|
# A thicker band in its own colour: the place to grab the card.
|
|
var hs := Style.box(Style.CARD_HEAD, 8, [12, 13, 10, 12])
|
|
hs.corner_radius_bottom_left = 0
|
|
hs.corner_radius_bottom_right = 0
|
|
hs.border_width_bottom = 1
|
|
hs.border_color = Style.RULE
|
|
header.add_theme_stylebox_override("panel", hs)
|
|
header.custom_minimum_size.y = 56
|
|
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 build:
|
|
row.add_child(CardMoveHandle.new())
|
|
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_SMART
|
|
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 := TableCardGoButton.make(self)
|
|
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 ""
|
|
|
|
|
|
|
|
## 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.
|
|
## Greys out the roll button while the card can't roll: a range runs
|
|
## backwards, or it costs more than the value has (the tooltip says why).
|
|
func sync_roll_button() -> void:
|
|
if not is_instance_valid(roll_button):
|
|
return
|
|
var table := card.table
|
|
var reason := "a range runs backwards" if RollRange.backwards(table) != "" else ""
|
|
if reason == "" and RollCosts.has_cost(table):
|
|
reason = RollCosts.blocked(table, Roller.live_context(card.main.data, card.main.persistent_vars), card.main.data)
|
|
roll_button.disabled = reason != ""
|
|
roll_button.modulate.a = 0.4 if reason != "" else 1.0
|
|
if not roll_button.has_meta("base_tip"):
|
|
roll_button.set_meta("base_tip", roll_button.tooltip_text)
|
|
var tip := str(roll_button.get_meta("base_tip"))
|
|
if RollCosts.has_cost(table):
|
|
tip += "\n" + RollCosts.describe(table, card.main.data)
|
|
if reason != "":
|
|
tip += "\nCan't roll: " + reason
|
|
roll_button.tooltip_text = tip
|
|
|
|
|
|
func refresh_live() -> void:
|
|
sync_roll_button()
|
|
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
|
|
|
|
|
|
|
|
## 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)
|