TableTower/ui/main_templates.gd
RobinFiredrake ea1e7a6492 Rename to Table Tarot in the UI: spreads, threads, deck mode, branch, repeat
User-facing text only; code names, JSON keys and the project's internal
name (and so the user:// folder) are unchanged. The Collection card kind
keeps its name until a new one is chosen. The window title says Table
Tarot; the top bar shows the theme's logo (the fortune teller, drawn from a
copy of the art without its background square); the app icon is the
mascot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:24:23 -06:00

141 lines
5.4 KiB
GDScript

class_name MainTemplates
extends RefCounted
## Main's templates: saving cards as templates, inserting, import and export.
## Saved in user://templates (TemplateLibrary), shared by every collection.
## --sample runs keep them in memory only.
const TEMPLATE_FILTER := "*.json ; Table Tarot template"
var m: Main
var _templates: TemplateLibrary
func _init(p_main: Main) -> void:
m = p_main
func templates() -> TemplateLibrary:
if _templates == null:
_templates = TemplateLibrary.new("user://" if m._saving_enabled else "")
return _templates
## Opens the save dialog for a template of these tables (and groups).
func save_template_from(table_ids: Array, kind: String, group_ids: Array = []) -> void:
var tname := "Template"
if kind == "card" and table_ids.size() == 1 and m.data.get_table(str(table_ids[0])):
tname = Formatter.strip_tags(m.data.get_table(str(table_ids[0])).name)
elif kind == "group" and group_ids.size() == 1 and m.data.get_group(str(group_ids[0])):
tname = Formatter.strip_tags(m.data.get_group(str(group_ids[0])).name)
var tpl := TemplateKit.make(m.data, table_ids, kind, tname, "", group_ids)
if (tpl["tables"] as Array).is_empty():
m.toast("Nothing to save: the template would have no cards")
return
if kind == "selection":
tpl["name"] = "%d cards" % (tpl["tables"] as Array).size()
m._add_modal(TemplateForms.save_dialog(tpl, func(out: Dictionary) -> void:
var id := templates().add(out)
if id == "":
m.toast("Couldn't save the template")
else:
m.toast("Saved template “%s”. Insert it from + Card ▾ → From template…" % out["name"])
))
## The selection as a template; groups whose cards are all selected come along.
func save_selection_as_template() -> void:
if m.selection.is_empty():
m.toast("Select some cards first")
return
var gids: Array = []
for g in m.data.groups:
if g.members.is_empty():
continue
var all := true
for member in g.members:
if not m.selection.has(member):
all = false
break
if all:
gids.append(g.id)
save_template_from(m.selection.keys(), "selection", gids)
func open_template_picker() -> TemplatePicker:
var p := TemplatePicker.new(m, templates())
m._add_modal(p)
return p
## Asks for the template's parameters (if it has any), then inserts it.
func begin_template_insert(tpl: Dictionary) -> void:
if (tpl.get("params", []) as Array).is_empty():
insert_template(tpl, {})
return
m._add_modal(TemplateForms.params_dialog(tpl, func(values: Dictionary) -> void: insert_template(tpl, values)))
## Inserts a template at the view centre: parameters substituted, fresh ids,
## outside pipes relinked by name (or dropped and reported). Returns
## TemplateKit.instantiate's result.
func insert_template(tpl: Dictionary, values: Dictionary) -> Dictionary:
m.undo_redo._begin_edit()
var ext := TemplateKit.extent(tpl)
var center := m.board.screen_to_world(m.board.size / 2.0)
var origin := (center - ext / 2.0).round()
origin = Vector2(maxf(20.0, origin.x), maxf(20.0 + TableGroup.TITLE_H, origin.y))
var r := TemplateKit.insert(tpl, values, m.data, origin)
for t: RollTable in r["tables"]:
if t.kind == RollTable.KIND_VARIABLE and t.var_name == "":
t.var_name = m.editing._free_var_name("var")
var card := TableCard.new()
m.board.cards.add_child(card)
card.setup(m, t)
m.cards[t.id] = card
m.grouping._groups_changed()
m.undo_redo._end_edit("Insert template %s" % Formatter.strip_tags(str(tpl.get("name", "template"))))
var n := (r["tables"] as Array).size()
var msg := "Inserted “%s” (%d %s)" % [str(tpl.get("name", "template")), n, "card" if n == 1 else "cards"]
var relinked: Array = r["relinked"]
if not relinked.is_empty():
msg += " · %d %s relinked by name" % [relinked.size(), "pipe" if relinked.size() == 1 else "pipes"]
var dropped: Array = r["dropped"]
if dropped.is_empty():
m.toast(msg, true)
else:
m.toast(msg + " · %d left out" % dropped.size(), true)
var d := ModalCard.new("Inserted “%s”" % str(tpl.get("name", "template")), 540.0)
d.body.add_child(ModalCard.note("These threads led to cards outside the template, and this spread has no card with that name, so they were left out. Lay them again by hand, or rename a card and insert again.", Style.INK))
d.body.add_child(ModalCard.section("Left out (%d)" % dropped.size()))
for what: String in dropped:
d.body.add_child(ModalCard.note("• " + what, Style.INK))
if not relinked.is_empty():
d.body.add_child(ModalCard.section("Relinked (%d)" % relinked.size()))
for what: String in relinked:
d.body.add_child(ModalCard.note("• " + what))
d.add_button("Done", "primary", d.close)
m._add_modal(d)
return r
func export_template(id: String) -> void:
var text := templates().export_text(id)
if text == "":
m.toast("Couldn't read that template")
return
m.export_dialog("Export template", TEMPLATE_FILTER,
id.trim_prefix(TemplateLibrary.BUILTIN_PREFIX) + ".template.json", func() -> String: return text)
func import_template_file(picker: TemplatePicker = null) -> void:
m.file_dialog("Import a template", [TEMPLATE_FILTER], FileDialog.FILE_MODE_OPEN_FILE, func(path: String) -> void:
var r := templates().import_text(FileAccess.get_file_as_string(path))
if r["ok"]:
m.toast("Imported template “%s”" % r["name"])
if is_instance_valid(picker):
picker.refresh()
picker.select(r["id"])
else:
m._add_modal(IoMenu.report_dialog("Couldn't import %s" % path.get_file(), {"errors": r["errors"]}))
)