TableTower/core/sheet_doc.gd
RobinFiredrake 2661744b06 Sheets phase B: main menu, Sheets list, sheet editor
- Main menu (Spreads, Sheets, Settings, Quit) on the theme background,
  with recent spreads and sheets; logo or ☰ returns to it.
- Sheets: data model, store, templates, snapshot undo; New sheet dialog
  (blank or a template); editor with folder tabs, snap-grid canvas on the
  spread background, field palette, inspector, Play/Build, status strip.
- Field kinds: Number, Text box (BBCode, live {@name}), Heading.
- PaletteList shared by the card and field palettes; shared top bar brand.
- Keys go to the screen that shows; theme reloads keep the screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 15:53:56 -06:00

115 lines
3.2 KiB
GDScript

class_name SheetDoc
extends RefCounted
## A sheet: a character, a vehicle, anything you keep track of. It has tabs
## (Main, Spells, Notes…), each a free canvas of boxes (SheetBox). Saved as
## JSON by SheetStore; a template is a sheet saved in the templates folder.
## Spec: docs/SHEETS.md.
const FORMAT_VERSION := 1
var id: String = ""
var name: String = "New sheet"
## Sheet tint as "#rrggbb" ("" = the theme's), e.g. to tell vehicles apart.
var tint: String = ""
## The template this sheet was made from ("" = blank).
var from_template: String = ""
## Each tab: {"id", "name", "boxes": Array[SheetBox]}.
var tabs: Array[Dictionary] = []
func _init(p_id: String = "", p_name: String = "New sheet") -> void:
id = p_id
name = p_name
if tabs.is_empty():
add_tab("Main")
func add_tab(tab_name: String) -> Dictionary:
var boxes: Array[SheetBox] = []
var tab := {"id": new_tab_id(), "name": tab_name, "boxes": boxes}
tabs.append(tab)
return tab
func new_tab_id() -> String:
var n := tabs.size() + 1
while tabs.any(func(t: Dictionary) -> bool: return t["id"] == "tab%d" % n):
n += 1
return "tab%d" % n
func get_tab(tab_id: String) -> Dictionary:
for tab in tabs:
if tab["id"] == tab_id:
return tab
return {}
## The box with `box_id` on any tab, or null.
func find_box(box_id: String) -> SheetBox:
for tab in tabs:
for box: SheetBox in tab["boxes"]:
if box.id == box_id:
return box
return null
## The tab that holds `box_id` ({} if none).
func tab_of(box_id: String) -> Dictionary:
for tab in tabs:
for box: SheetBox in tab["boxes"]:
if box.id == box_id:
return tab
return {}
## A box id not used on this sheet.
func new_box_id() -> String:
var n := 1
while find_box("b%d" % n) != null:
n += 1
return "b%d" % n
func remove_box(box_id: String) -> void:
var tab := tab_of(box_id)
if not tab.is_empty():
(tab["boxes"] as Array).assign((tab["boxes"] as Array).filter(func(b: SheetBox) -> bool: return b.id != box_id))
func to_dict() -> Dictionary:
var tab_list: Array = []
for tab in tabs:
var boxes: Array = []
for box: SheetBox in tab["boxes"]:
boxes.append(box.to_dict())
tab_list.append({"id": tab["id"], "name": tab["name"], "boxes": boxes})
var d := {"format_version": FORMAT_VERSION, "id": id, "name": name, "tabs": tab_list}
if tint != "":
d["tint"] = tint
if from_template != "":
d["from_template"] = from_template
return d
static func from_dict(d: Dictionary) -> SheetDoc:
var doc := SheetDoc.new(str(d.get("id", "")), str(d.get("name", "New sheet")))
doc.tint = str(d.get("tint", ""))
doc.from_template = str(d.get("from_template", ""))
var tab_list: Variant = d.get("tabs", [])
if tab_list is Array and not (tab_list as Array).is_empty():
doc.tabs.clear()
for td: Variant in tab_list:
if not td is Dictionary:
continue
var boxes: Array[SheetBox] = []
for bd: Variant in (td as Dictionary).get("boxes", []):
if bd is Dictionary:
boxes.append(SheetBox.from_dict(bd))
doc.tabs.append({"id": str(td.get("id", doc.new_tab_id())), "name": str(td.get("name", "Tab")), "boxes": boxes})
return doc
## A deep copy (through the saved form), e.g. a new sheet from a template.
func copy() -> SheetDoc:
return SheetDoc.from_dict(to_dict())