class_name SheetLinks extends RefCounted ## The bridge between a spread and the sheets on it (Sheet cards with a ## RollTable.sheet_id). Before a roll, every "Use in tables" box of each linked sheet ## becomes a spread variable: @var, or @prefix_var when the node has a prefix ## (RollTable.sheet_prefix: two characters on one spread). After the roll, values the roll changed go ## back into their boxes, unless the box is locked or its kind can't take a ## value (formulas, lists…); a locked box puts the variable back. ## Kinds a roll can write into, and how the value is stored. const WRITABLE := [SheetKinds.NUMBER, SheetKinds.SHORT_TEXT, SheetKinds.TEXT, SheetKinds.CHECKBOX, SheetKinds.DROPDOWN, SheetKinds.RESOURCE, SheetKinds.TICKS, SheetKinds.CLOCK, SheetKinds.DOTS, SheetKinds.LADDER, SheetKinds.XP] ## The spread variable name for a box on a card: "str" or "brin_str". static func var_for(card: RollTable, box: SheetBox) -> String: var prefix := card.sheet_prefix.strip_edges() return box.var_name if prefix == "" else "%s_%s" % [prefix, box.var_name] ## The spread's Sheet cards that show a sheet. static func sheet_cards(board: TowerBoard) -> Array[RollTable]: var out: Array[RollTable] = [] for t in board.tables: if t.kind == RollTable.KIND_SHEET and t.sheet_id != "": out.append(t) return out ## Puts every linked box's value into `vars` (name -> {"value", "source"}). ## Returns what was put in: name -> {"sheet", "box", "value"}, for write_back. ## `load_doc` gives a SheetDoc for a sheet id (null when it is gone). static func inject(board: TowerBoard, vars: Dictionary, load_doc: Callable) -> Dictionary: var given := {} for card in sheet_cards(board): var doc: SheetDoc = load_doc.call(card.sheet_id) if doc == null: continue var values := SheetValues.vars(doc) for tab in doc.tabs: for box: SheetBox in tab["boxes"]: if box.var_name == "" or not values.has(box.var_name): continue var name := var_for(card, box) var v: Variant = _plain(values[box.var_name]) vars[name] = {"value": v, "source": "sheet %s" % doc.name} given[name] = {"sheet": doc.id, "box": box.id, "value": v} return given ## After a roll: each variable that changed goes into its box. Returns ## [{"sheet", "box", "name", "value", "locked"}] for every change seen; locked ## boxes keep their value and their variable is put back. static func changes(given: Dictionary, vars: Dictionary) -> Array: var out: Array = [] for name: String in given: var g: Dictionary = given[name] var entry: Variant = vars.get(name) var now: Variant = (entry as Dictionary).get("value") if entry is Dictionary else entry if now == null or str(now) == str(g["value"]): continue out.append({"sheet": g["sheet"], "box": g["box"], "name": name, "value": now}) return out ## Writes `value` into `box` as its kind stores it. False when the box is ## locked or its kind can't take a value (the caller puts the variable back). static func write(box: SheetBox, value: Variant) -> bool: if box.locked or not WRITABLE.has(box.kind): return false match box.kind: SheetKinds.NUMBER, SheetKinds.XP: box.value = Expr.normalize(SheetValues.number(value)) SheetKinds.CHECKBOX: box.value = Expr.truthy(value) SheetKinds.RESOURCE: var v: Dictionary = (box.value as Dictionary).duplicate() if box.value is Dictionary else {"cur": 0, "max": 10} v["cur"] = int(SheetValues.number(value)) box.value = v SheetKinds.TICKS, SheetKinds.CLOCK, SheetKinds.DOTS, SheetKinds.LADDER: box.value = int(SheetValues.number(value)) _: box.value = str(value) return true ## Spread variables hold numbers and text only. static func _plain(v: Variant) -> Variant: if v is int or v is float or v is String: return Expr.normalize(v) if v == null: return "" return str(v)