- TableCardColumns: the column-name header, each row's cells after its text (Play: wrapping text; Build: a field per cell, edited inline), cards widen per column, and in "each" mode the cell each column landed on is lit (Main.lit_columns, from the rolled node's columns). - InspectorColumns: Add columns, add/rename/move/remove (cells kept in step), one roll vs each column, Save as (value picker) and Only if (condition builder); the result inspector gets a field per extra column. - Help: a Columns topic after Ranged tables and dice. - tests/test_ui_columns.gd. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
206 lines
6.9 KiB
GDScript
206 lines
6.9 KiB
GDScript
class_name TableCardColumns
|
||
extends RefCounted
|
||
## A TableCard's columns (a roll card with RollTable.columns): the small
|
||
## header row of column names over the results, each row's cells after its
|
||
## text (Play: the text, wrapping; Build: a field per cell, edited inline like
|
||
## the result text) and, after a roll, the cell each column landed on lit
|
||
## (from the rolled node's "columns", kept in Main.lit_columns). Column 0 is
|
||
## the row's own text, so TableCardRows draws it as usual.
|
||
|
||
## Width of one extra column's cell.
|
||
const CELL_W := 112.0
|
||
## Room the Build row keeps after the cells: the weight box and the ×.
|
||
const BUILD_TAIL := 34.0 + 22.0 + 8.0
|
||
const BUILD_TAIL_RANGED := 22.0 + 4.0
|
||
## Room a ranged Play row keeps for its chance ("17%").
|
||
const ODDS_TAIL := 36.0
|
||
|
||
var card: TableCard
|
||
## entry id -> Array of {"box", "label" (Play) or "edit" (Build), "k"}.
|
||
var cells: Dictionary = {}
|
||
|
||
|
||
func _init(p_card: TableCard) -> void:
|
||
card = p_card
|
||
|
||
|
||
func clear() -> void:
|
||
cells.clear()
|
||
|
||
|
||
func active() -> bool:
|
||
return card.table != null and card.table.has_columns()
|
||
|
||
|
||
## The extra card width the columns after the first need.
|
||
func extra_width() -> float:
|
||
if not active():
|
||
return 0.0
|
||
return (card.table.columns.size() - 1) * (CELL_W + _sep())
|
||
|
||
|
||
func _sep() -> float:
|
||
return 4.0 if card.main.mode == Main.Mode.BUILD else 8.0
|
||
|
||
|
||
## The row of column names over the results, lined up with the cells.
|
||
func make_header(build: bool, ranged: bool, numw: float) -> Control:
|
||
var t := card.table
|
||
var pad := MarginContainer.new()
|
||
pad.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
pad.add_theme_constant_override("margin_left", 12)
|
||
pad.add_theme_constant_override("margin_right", 22)
|
||
pad.add_theme_constant_override("margin_bottom", 2)
|
||
var h := TableCard.hbox(int(_sep()))
|
||
pad.add_child(h)
|
||
var lead := Control.new()
|
||
lead.custom_minimum_size = Vector2(numw, 0)
|
||
lead.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
||
h.add_child(lead)
|
||
for k in t.columns.size():
|
||
var l := Style.make_label(str(t.columns[k].get("name", "")), Style.body_bold(true), 12, Style.MUTED)
|
||
l.clip_text = true
|
||
l.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
|
||
l.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
l.tooltip_text = _column_tip(t.columns[k], k)
|
||
if k == 0:
|
||
l.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
else:
|
||
l.custom_minimum_size = Vector2(CELL_W, 0)
|
||
h.add_child(l)
|
||
var tail := 0.0
|
||
if build:
|
||
tail = BUILD_TAIL_RANGED if ranged else BUILD_TAIL
|
||
elif ranged and t.has_chances():
|
||
tail = ODDS_TAIL
|
||
if tail > 0.0:
|
||
var sp := Control.new()
|
||
sp.custom_minimum_size = Vector2(tail, 0)
|
||
sp.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
||
h.add_child(sp)
|
||
pad.set_meta("columns_header", true)
|
||
return pad
|
||
|
||
|
||
func _column_tip(col: Dictionary, k: int) -> String:
|
||
var bits := PackedStringArray()
|
||
if k > 0 and str(col.get("when", "")).strip_edges() != "":
|
||
bits.append("Only rolled if " + str(col["when"]).strip_edges())
|
||
if str(col.get("save_as", "")).strip_edges() != "":
|
||
bits.append("Saved as " + str(col["save_as"]).strip_edges())
|
||
return "\n".join(bits)
|
||
|
||
|
||
## Adds row `e`'s cells (columns 1..) to its row box `h`, right after the text.
|
||
func add_cells(h: HBoxContainer, e: TableEntry, index: int, build: bool) -> void:
|
||
if not active():
|
||
return
|
||
var list: Array = []
|
||
for k in range(1, card.table.columns.size()):
|
||
var c := _build_cell(e, k) if build else _play_cell(e, index, k)
|
||
h.add_child(c["box"])
|
||
list.append(c)
|
||
cells[e.id] = list
|
||
|
||
|
||
func _build_cell(e: TableEntry, k: int) -> Dictionary:
|
||
var edit := LineEdit.new()
|
||
edit.text = RollTable.cell_text(e, k)
|
||
edit.placeholder_text = str(card.table.columns[k].get("name", ""))
|
||
edit.custom_minimum_size = Vector2(CELL_W, 0)
|
||
edit.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
edit.add_theme_font_size_override("font_size", 15)
|
||
edit.tooltip_text = edit.placeholder_text
|
||
edit.text_changed.connect(func(s: String) -> void:
|
||
set_cell(e, k, s)
|
||
card.main.mark_dirty()
|
||
)
|
||
edit.focus_entered.connect(func() -> void: card.select_entry(e.id))
|
||
edit.set_meta("cell", k)
|
||
return {"box": edit, "edit": edit, "k": k}
|
||
|
||
|
||
func _play_cell(e: TableEntry, index: int, k: int) -> Dictionary:
|
||
var box := PanelContainer.new()
|
||
box.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
box.custom_minimum_size = Vector2(CELL_W, 0)
|
||
box.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
var l := RichText.label("", 15, Style.INK, true)
|
||
l.custom_minimum_size = Vector2(CELL_W - 8.0, 0)
|
||
RichText.hook_hover(l, card.main)
|
||
box.add_child(l)
|
||
var c := {"box": box, "label": l, "k": k, "index": index, "entry": e}
|
||
_show_cell(c)
|
||
return c
|
||
|
||
|
||
## Re-renders a Play row's cells from the last roll (TableCardRows calls it
|
||
## with the row's text).
|
||
func refresh(eid: String) -> void:
|
||
for c: Dictionary in cells.get(eid, []):
|
||
if c.has("label"):
|
||
_show_cell(c)
|
||
|
||
|
||
func _show_cell(c: Dictionary) -> void:
|
||
var e: TableEntry = c["entry"]
|
||
var k: int = c["k"]
|
||
var rolled := rolled_cell(int(c["index"]), k)
|
||
var text := RichText.decorate(str(rolled["text"])) if not rolled.is_empty() \
|
||
else RichText.preview(RollTable.cell_text(e, k), card.main.data)
|
||
var l: RichTextLabel = c["label"]
|
||
if l.text != text:
|
||
l.text = text
|
||
l.tooltip_text = Formatter.strip_tags(text).strip_edges()
|
||
var lit := not rolled.is_empty()
|
||
# "Roll each column separately": a lit row's other cells weren't rolled.
|
||
var passed := not lit and card.table.column_rolls == RollTable.COLUMNS_EACH \
|
||
and card.main.is_entry_lit(card.table.id, e.id) and not _lit_columns().is_empty()
|
||
RichText.set_fonts(l, 15, true, lit)
|
||
l.add_theme_color_override("default_color", Style.MUTED if passed else Style.INK)
|
||
var box: PanelContainer = c["box"]
|
||
box.add_theme_stylebox_override("panel", Style.box(Style.ACCENT if lit and card.table.column_rolls == RollTable.COLUMNS_EACH else Color(0, 0, 0, 0), 4, [4, 2, 4, 2]))
|
||
box.set_meta("lit", lit)
|
||
|
||
|
||
# ---------- The last roll ----------
|
||
|
||
## The "columns" of every lit roll of this card in the last roll.
|
||
func _lit_columns() -> Array:
|
||
var out: Array = []
|
||
var pre := card.table.id + ":"
|
||
for key: String in card.main.lit_columns:
|
||
if key.begins_with(pre) and card.main.lit_entries.has(key):
|
||
out.append(card.main.lit_columns[key])
|
||
return out
|
||
|
||
|
||
## What column `k` rolled on result number `index` in the last roll
|
||
## ({name, text, index, value?}), or {} when it didn't land there.
|
||
func rolled_cell(index: int, k: int) -> Dictionary:
|
||
for cols: Array in _lit_columns():
|
||
if k < cols.size():
|
||
var c: Dictionary = cols[k]
|
||
if not c.get("skipped", false) and int(c.get("index", -1)) == index:
|
||
return c
|
||
return {}
|
||
|
||
|
||
## Main.lit_columns from a rolled node or step: its "columns", if it has them.
|
||
static func note_lit(m: Main, key: String, d: Dictionary) -> void:
|
||
if d.get("columns") is Array:
|
||
m.lit_columns[key] = d["columns"]
|
||
else:
|
||
m.lit_columns.erase(key)
|
||
|
||
|
||
# ---------- Editing ----------
|
||
|
||
## Sets column `k`'s text on result `e` (k 0 is its own text).
|
||
static func set_cell(e: TableEntry, k: int, s: String) -> void:
|
||
if k == 0:
|
||
e.text = s
|
||
return
|
||
while e.cells.size() < k:
|
||
e.cells.append("")
|
||
e.cells[k - 1] = s
|