TableTower/core/sheet_text.gd
RobinFiredrake 5941733af1 Sheets phase D: sheets on spreads as linked Sheet cards
- A Sheet card can show a sheet file (sheet, prefix): first-tab preview,
  Open sheet with a way back, the spread's Play/Build in the editor.
- Rolls read and write "Use in tables" boxes as @name or @prefix_name;
  locked boxes and formulas/lists keep their values; changed boxes glow.
- A roll reaching the card shows the sheet's text (SheetText) in the
  tray; text boxes also show spread values when on the spread.
- Old template Sheet cards migrate to a sheet with one text box (not in
  Help demos). Card palette lists your sheets.
- Sheet canvas: tiled grid (smooth panning); boxes no longer overlap
  (nearest free spot, drags stop short; Alt places freely).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 16:52:41 -06:00

73 lines
3.2 KiB
GDScript

class_name SheetText
extends RefCounted
## A sheet as one block of BBCode, for when a roll reaches its card: the tray
## headline, "Copy" and "Save as a file" show it (docs/LOGIC.md §6). Boxes
## used in tables become {@name} (with the card's prefix), so the roll fills
## in the values it just set; other boxes show their current value. Text
## boxes keep their own BBCode and {@…}, so they can show spread values too.
static func summary(doc: SheetDoc, prefix: String = "") -> String:
var lines: Array[String] = []
for tab in doc.tabs:
var boxes: Array = (tab["boxes"] as Array).duplicate()
boxes.sort_custom(func(a: SheetBox, b: SheetBox) -> bool: return a.y < b.y or (a.y == b.y and a.x < b.x))
for box: SheetBox in boxes:
var line := _line(doc, box, prefix)
if line != "":
lines.append(line)
return "\n".join(lines)
static func _line(doc: SheetDoc, box: SheetBox, prefix: String) -> String:
match box.kind:
SheetKinds.HEADING:
return "[b]%s[/b]" % box.title
SheetKinds.DIVIDER, SheetKinds.PICTURE, SheetKinds.DICE:
return ""
SheetKinds.TEXT:
var text := str(box.value if box.value != null else "").strip_edges()
if text == "":
return ""
return text if box.title == "" else "[b]%s:[/b] %s" % [box.title, text]
var shown := ""
if box.var_name != "" and SheetLinks.WRITABLE.has(box.kind):
shown = "{@%s}" % (box.var_name if prefix == "" else "%s_%s" % [prefix, box.var_name])
else:
shown = _value(doc, box)
if shown == "":
return ""
return "[b]%s:[/b] %s" % [box.title, shown] if box.title != "" else shown
## A box's value in words: lists by their names, currency with its total…
static func _value(doc: SheetDoc, box: SheetBox) -> String:
var v: Variant = box.value
match box.kind:
SheetKinds.RESOURCE:
var r: Dictionary = v if v is Dictionary else {}
return "%s / %s" % [SheetValues.show(r.get("cur", 0)), SheetValues.show(r.get("max", 0))]
SheetKinds.LIST:
var names: Array = []
var cols: Array = box.config.get("columns", [])
var first := str((cols[0] as Dictionary).get("name", "Name")) if not cols.is_empty() else "Name"
for entry: Variant in (v if v is Array else []):
if entry is Dictionary:
names.append(str((entry as Dictionary).get(first, "")))
return ", ".join(names)
SheetKinds.SLOTS:
return ", ".join((v if v is Array else []).map(func(it: Variant) -> String: return str((it as Dictionary).get("name", "")) if it is Dictionary else ""))
SheetKinds.CONDITIONS:
return ", ".join((v if v is Array else []).map(func(x: Variant) -> String: return str(x)))
SheetKinds.CURRENCY:
var coins: Array = box.config.get("coins", [])
return "%s %s" % [SheetValues.show(SheetValues.currency_total(box)), str((coins[0] as Dictionary).get("name", "")) if not coins.is_empty() else ""]
SheetKinds.STEP_DIE, SheetKinds.USAGE_DIE, SheetKinds.SHORT_TEXT, SheetKinds.DROPDOWN:
return str(v if v != null else "")
SheetKinds.CHECKBOX:
return "Yes" if bool(v) else "No"
SheetKinds.LADDER:
var steps: Array = box.config.get("steps", SheetKinds.FATE_LADDER)
var i := int(SheetValues.number(v))
return str(steps[i]) if i >= 0 and i < steps.size() else ""
return SheetValues.show(SheetValues.value_of(doc, box))