class_name MainSheetLinks extends RefCounted ## Sheets on the open spread. A Sheet card shows a sheet file ## (RollTable.sheet_id) as a small picture of its first tab (SheetPreview); ## threads can lead into it like any card, and a roll that reaches it shows ## the sheet's text (SheetText via Roller.sheet_text). Old template-only ## Sheet cards are migrated: their text becomes a text box in a new sheet. ## Around each roll it runs the variable bridge (SheetLinks): linked boxes ## become @variables, changed values go back into the boxes. var m: Main ## sheet id -> {box id: "from "}: boxes the last roll wrote. var recent: Dictionary = {} var _given: Dictionary = {} var _roll_label := "" func _init(p_main: Main) -> void: m = p_main Roller.sheet_text = func(t: RollTable) -> String: if not is_instance_valid(m) or not m.is_inside_tree(): return t.entries[0].text var doc := load_doc(t.sheet_id) return SheetText.summary(doc, t.sheet_prefix) if doc else t.entries[0].text func store() -> SheetStore: return m.screens.sheets.store if m.screens else null ## The sheet with `id`: the editor's copy when it is open there. func load_doc(id: String) -> SheetDoc: if m.screens == null or id == "": return null var ed := m.screens.sheets.editor if ed.doc and ed.doc.id == id: return ed.doc return store().load_sheet(id) ## Old Sheet cards (a BBCode template, no sheet file) get a sheet whose one ## text box holds the template; it keeps showing spread values like {@name}. ## The file id comes from the spread and card, so doing it again (after an ## undo) links the same file. True when any card changed. func migrate() -> bool: if m.data == null or store() == null: return false # Help demos save nothing: their old Sheet cards stay as they are there # ("Keep a copy" makes a real spread, which migrates when it opens). if m.demo and m.demo.active(): return false var changed := false for t in m.data.tables: if t.kind != RollTable.KIND_SHEET or t.sheet_id != "": continue var id := LibraryStore.slugify("%s-%s" % [m.data.id, t.id]) if store().load_sheet(id) == null: var doc := SheetDoc.new(id, Formatter.strip_tags(t.name).strip_edges() if Formatter.strip_tags(t.name).strip_edges() != "" else "Sheet") var box := SheetKinds.make_box(SheetKinds.TEXT, doc.new_box_id(), Vector2(16, 16)) box.title = "" box.w = 24 * SheetKinds.GRID box.h = 14 * SheetKinds.GRID box.value = t.entries[0].text if not t.entries.is_empty() else "" doc.tabs[0]["boxes"].append(box) store().save(doc) t.sheet_id = id changed = true return changed ## True when a Sheet card on the open spread shows sheet `id`. func on_spread(id: String) -> bool: return m.data != null and SheetLinks.sheet_cards(m.data).any(func(t: RollTable) -> bool: return t.sheet_id == id) ## Fills in {@name} in a sheet's text: the sheet's own boxes first, then (when ## the sheet is on the open spread) the spread's values, such as an answer a ## choice card saved. func expand(doc: SheetDoc, text: String) -> String: if not text.contains("{@"): return text var values := SheetValues.vars(doc) if on_spread(doc.id): for name: String in m.persistent_vars: if not values.has(name): var entry: Variant = m.persistent_vars[name] values[name] = (entry as Dictionary).get("value", "") if entry is Dictionary else entry for name: String in values: text = text.replace("{@%s}" % name, "[b]%s[/b]" % SheetValues.show(values[name])) return text ## Redraws the sheet pictures on the cards (after a roll or a sheet edit). func refresh_previews() -> void: for t in SheetLinks.sheet_cards(m.data) if m.data else []: var card: TableCard = m.card_for(t.id) var preview: SheetPreview = card.find_child("SheetPreview", true, false) if card else null if preview: preview.reload() ## Places sheet `sheet_id` on the spread as a Sheet card, top-left at `at` ## (INF = view centre). A second sheet gets a prefix from its name so their ## @names don't clash. func add_card(sheet_id: String, at: Vector2 = Vector2.INF) -> RollTable: var doc := load_doc(sheet_id) if doc == null: m.toast("Couldn't read that sheet") return null var prefix := "" if not SheetLinks.sheet_cards(m.data).is_empty(): prefix = LibraryStore.slugify(doc.name.get_slice(" ", 0)).replace("-", "_") return m.editing.add_table(RollTable.KIND_SHEET, "", at, "sheet:%s:%s" % [sheet_id, prefix], doc.name) func set_prefix(card_id: String, prefix: String) -> void: var t := m.data.get_table(card_id) if t == null: return m.undo_redo._begin_edit() t.sheet_prefix = LibraryStore.slugify(prefix).replace("-", "_") m.mark_dirty() m.undo_redo._end_edit("Sheet prefix") m.inspector_edited(card_id) refresh_previews() ## Opens the card's sheet in the sheet editor, in the spread's mode, with a ## way back to the spread. func open(card_id: String) -> void: var t := m.data.get_table(card_id) open_sheet(t.sheet_id if t else "", "That sheet is gone. Delete the card, or link another sheet.") ## Opens sheet `id` (a card's, or a Lore Entry's item sheet) with a way back to the spread. func open_sheet(id: String, gone: String = "That sheet is gone.") -> void: var doc := load_doc(id) if doc == null: m.toast(gone) return m.screens.show(MainScreens.SHEETS) m.screens.sheets.open_doc(doc) m.screens.sheets.editor.open_from_spread(m.data.name) ## Right-click items for a linked Sheet card (Actions.card). func menu_items(t: RollTable) -> Array: return [ {"text": "Open sheet", "do": open.bind(t.id)}, {"text": "Prefix for tables…", "do": ask_prefix.bind(t.id), "disabled": m.mode != Main.Mode.BUILD, "tip": "With a prefix like brin, tables use @brin_str. Blank = plain @str."}, ] func ask_prefix(card_id: String) -> void: var t := m.data.get_table(card_id) m._add_modal(ModalCard.ask_text("Prefix for tables", "Tables read this sheet's boxes as @prefix_name. Blank = plain @name.", t.sheet_prefix, "Save", func(text: String) -> void: set_prefix(card_id, text), true)) # ---------- The variable bridge around a roll ---------- ## Before a roll: every linked box becomes a spread variable. func before_roll(table_name: String) -> void: _given = {} _roll_label = table_name if m.data == null or m.screens == null: return _given = SheetLinks.inject(m.data, m.persistent_vars, load_doc) ## After a roll: values it changed go into their boxes (one undo step when ## the sheet is open in the editor); locked boxes keep theirs. func after_roll() -> void: if _given.is_empty(): return var found := SheetLinks.changes(_given, m.persistent_vars) var by_sheet := {} for c: Dictionary in found: if not by_sheet.has(c["sheet"]): by_sheet[c["sheet"]] = [] (by_sheet[c["sheet"]] as Array).append(c) var said: Array[String] = [] for sheet_id: String in by_sheet: var doc := load_doc(sheet_id) if doc == null: continue var marks := {} var write := func() -> void: for c: Dictionary in by_sheet[sheet_id]: var box := doc.find_box(c["box"]) if box and SheetLinks.write(box, c["value"]): marks[box.id] = "from %s" % _roll_label said.append("%s: %s → %s" % [doc.name, box.title, SheetText._value(doc, box)]) else: var g: Dictionary = _given[c["name"]] m.persistent_vars[c["name"]] = {"value": g["value"], "source": "locked on %s" % doc.name} var ed := m.screens.sheets.editor if ed.doc == doc: ed.edit("Roll: %s" % _roll_label, write) else: write.call() store().save(doc) recent[sheet_id] = marks _given = {} refresh_previews() if not said.is_empty(): m.toast(" · ".join(said))