TableTower/core/sheet_store.gd
RobinFiredrake dd0323c53f Sheets phase C: every field kind, details panel, canvas panning
- 25 palette items over 23 kinds: basics, trackers (bars, ticks, clocks,
  dots, step/usage dice, ladders), formulas and dice buttons (rule
  language with sheet @names), lists (rows, mini table, chips, point pool
  that may go negative, bonds), slots, conditions, spell slots, XP,
  currency, heading, divider, picture.
- Per-kind settings in the inspector; hover excerpts and an in-app
  details panel for list entries; live redraw of dependent boxes.
- Canvas: the page stays put after edits (boxes can go anywhere), a
  screen of room past the boxes, drag-to-pan and edge scrolling.

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

127 lines
3.8 KiB
GDScript

class_name SheetStore
extends RefCounted
## Sheets and sheet templates as JSON files: user://sheets/<id>.json and
## user://sheet_templates/<id>.json. `root` can point elsewhere for tests.
const SHEETS := "sheets"
const TEMPLATES := "sheet_templates"
var root := "user://"
func _init(p_root: String = "user://") -> void:
root = p_root
func dir_for(templates: bool) -> String:
return root.path_join(TEMPLATES if templates else SHEETS)
func path_for(id: String, templates: bool) -> String:
return dir_for(templates).path_join(id + ".json")
## Rows for the Sheets list, newest first: {id, name, modified, from_template}.
func list(templates: bool = false) -> Array[Dictionary]:
var out: Array[Dictionary] = []
var dir := dir_for(templates)
if not DirAccess.dir_exists_absolute(dir):
return out
for file in DirAccess.get_files_at(dir):
if not file.ends_with(".json"):
continue
var path := dir.path_join(file)
var d := _read(path)
if d.is_empty():
continue
out.append({"id": file.get_basename(), "name": str(d.get("name", file.get_basename())),
"modified": FileAccess.get_modified_time(path), "from_template": str(d.get("from_template", ""))})
out.sort_custom(func(a: Dictionary, b: Dictionary) -> bool: return int(a["modified"]) > int(b["modified"]))
return out
## The sheet with `id`, or null.
func load_sheet(id: String, templates: bool = false) -> SheetDoc:
if not LibraryStore.is_valid_id(id):
return null
var d := _read(path_for(id, templates))
if d.is_empty():
return null
var doc := SheetDoc.from_dict(d)
doc.id = id
return doc
func save(doc: SheetDoc, templates: bool = false) -> Error:
if not LibraryStore.is_valid_id(doc.id):
return ERR_INVALID_PARAMETER
DirAccess.make_dir_recursive_absolute(dir_for(templates))
return BoardFileIO.write_text_atomic(path_for(doc.id, templates), JSON.stringify(doc.to_dict(), "\t"))
func delete(id: String, templates: bool = false) -> Error:
if not LibraryStore.is_valid_id(id):
return ERR_INVALID_PARAMETER
return DirAccess.remove_absolute(path_for(id, templates))
## A file id from `name` not used yet in that folder ("brin", "brin-2"…).
func free_id(name: String, templates: bool = false) -> String:
var base := LibraryStore.slugify(name)
if base == "":
base = "sheet"
var id := base
var n := 2
while FileAccess.file_exists(path_for(id, templates)):
id = "%s-%d" % [base, n]
n += 1
return id
## Copies a picture into the sheets' pictures folder (so the sheet keeps it
## if the original moves) and returns the copy's path ("" if unreadable).
func keep_picture(path: String) -> String:
var bytes := FileAccess.get_file_as_bytes(path)
if bytes.is_empty():
return ""
var dir := dir_for(false).path_join("pictures")
DirAccess.make_dir_recursive_absolute(dir)
var kept := dir.path_join("%s.%s" % [str(hash(bytes)).replace("-", "n"), path.get_extension().to_lower()])
var f := FileAccess.open(kept, FileAccess.WRITE)
if f == null:
return ""
f.store_buffer(bytes)
return kept
## A new sheet, blank or copied from a template, saved and returned.
func create(name: String, template_id: String = "") -> SheetDoc:
var doc: SheetDoc = null
if template_id != "":
var tpl := load_sheet(template_id, true)
if tpl:
doc = tpl.copy()
doc.from_template = template_id
if doc == null:
doc = SheetDoc.new()
doc.name = name
doc.id = free_id(name)
save(doc)
return doc
## Saves a copy of `doc` as a template named `name`; returns its id.
func save_as_template(doc: SheetDoc, name: String) -> String:
var tpl := doc.copy()
tpl.name = name
tpl.from_template = ""
tpl.id = free_id(name, true)
save(tpl, true)
return tpl.id
func _read(path: String) -> Dictionary:
if not FileAccess.file_exists(path):
return {}
var parsed: Variant = JSON.parse_string(FileAccess.get_file_as_string(path))
return parsed if parsed is Dictionary else {}