TableTower/ui/template_picker.gd
RobinFiredrake 16b34c40f6 Card palette: left dock Cards | List, search, click or drag to add
- CardCatalog: one list of card kinds for the palette, right-click New card
  and new-card names; Dice table and Deck are their own items.
- LeftDock holds the palette and List view in the shared dock tab style.
- Play greys the palette under a Switch to Build notice (replaces the
  greyed + Card button); + Card menu removed; Card palette button in Build.
- Templates dropped from the palette land where they are let go.
- Phones: + opens the palette full screen.
- docs/SHEETS.md: the Sheets and unified editor plan.

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

201 lines
6.9 KiB
GDScript

class_name TemplatePicker
extends ModalCard
## "All templates…" in the card palette: the template list with search, each row
## showing its name, description and a small preview. The user's own
## templates can be renamed, exported and deleted here; built-in ones are
## read-only. Import… adds a template file. Insert asks for parameters (if
## any) and drops the template at the view centre (Main.insert_template).
var main: Main
var lib: TemplateLibrary
var _search: LineEdit
var _list: VBoxContainer
var _insert: Button
var _selected := ""
var _rows: Dictionary = {} # id -> row PanelContainer
func _init(p_main: Main, p_lib: TemplateLibrary) -> void:
super._init("Insert a template", 640.0)
main = p_main
lib = p_lib
_search = LineEdit.new()
_search.placeholder_text = "Search templates"
_search.clear_button_enabled = true
ModalCard.style_line_edit(_search)
_search.text_changed.connect(func(_s: String) -> void: refresh())
_search.text_submitted.connect(func(_s: String) -> void:
if _selected != "":
_choose(_selected)
)
body.add_child(_search)
_list = VBoxContainer.new()
_list.add_theme_constant_override("separation", 8)
body.add_child(_list)
var imp := add_button("Import…", "plain", func() -> void: main.import_template_file(self))
imp.tooltip_text = "Add a template file (.json) to your templates"
var gap := Control.new()
gap.size_flags_horizontal = SIZE_EXPAND_FILL
footer.add_child(gap)
add_button("Cancel", "plain", close)
_insert = add_button("Insert", "primary", func() -> void:
if _selected != "":
_choose(_selected)
)
_insert.disabled = true
refresh()
ready.connect(func() -> void: _search.grab_focus())
## Rebuilds the list from the library, filtered by the search text.
func refresh() -> void:
for c in _list.get_children():
_list.remove_child(c)
c.queue_free()
_rows.clear()
var q := _search.text.strip_edges().to_lower()
var any := false
for r: Dictionary in lib.list():
var tpl: Dictionary = r["template"]
var hay := ("%s %s %s" % [r["name"], r["description"], TemplateKit.summary(tpl)]).to_lower()
if q != "" and not hay.contains(q):
continue
any = true
_list.add_child(_row(r))
if not any:
_list.add_child(ModalCard.note("No templates match." if q != "" else "No templates yet. Save one from a card's or a group's ⋯ menu, or from a selection."))
if not _rows.has(_selected):
_selected = ""
_insert.disabled = _selected == ""
_paint()
if is_inside_tree():
_fit()
## Ids of the rows shown, in order (tests use it).
func shown_ids() -> Array:
return _rows.keys()
func select(id: String) -> void:
if _rows.has(id):
_selected = id
_insert.disabled = false
_paint()
func _paint() -> void:
for id: String in _rows:
var on := id == _selected
var sb := Style.bordered(Style.box(Style.LIT if on else Style.CARD_SOFT, 10, [12, 10, 12, 10]), 1, Style.TRIM if on else Style.DASH)
(_rows[id] as PanelContainer).add_theme_stylebox_override("panel", sb)
func _row(r: Dictionary) -> Control:
var id: String = r["id"]
var tpl: Dictionary = r["template"]
var row := PanelContainer.new()
row.mouse_filter = MOUSE_FILTER_STOP
row.mouse_default_cursor_shape = CURSOR_POINTING_HAND
row.gui_input.connect(func(ev: InputEvent) -> void:
var mb := ev as InputEventMouseButton
if mb and mb.button_index == MOUSE_BUTTON_LEFT and mb.pressed:
select(id)
if mb.double_click:
_choose(id)
)
var h := HBoxContainer.new()
h.add_theme_constant_override("separation", 14)
h.mouse_filter = MOUSE_FILTER_IGNORE
row.add_child(h)
var pv := TemplateForms.preview(tpl)
pv.size_flags_vertical = SIZE_SHRINK_BEGIN
h.add_child(pv)
var v := VBoxContainer.new()
v.add_theme_constant_override("separation", 3)
v.size_flags_horizontal = SIZE_EXPAND_FILL
v.mouse_filter = MOUSE_FILTER_IGNORE
h.add_child(v)
var title_row := HBoxContainer.new()
title_row.add_theme_constant_override("separation", 8)
title_row.mouse_filter = MOUSE_FILTER_IGNORE
var title := Style.make_label(str(r["name"]), Style.display(), 20, Style.INK)
title.mouse_filter = MOUSE_FILTER_IGNORE
title_row.add_child(title)
title_row.add_child(_badge(str({"card": "Card", "group": "Group", "selection": "Cards"}.get(r["kind"], "Cards")), Style.BG, Style.ACCENT))
if r["builtin"]:
title_row.add_child(_badge("Built in", Color(0, 0, 0, 0.07), Style.MUTED))
v.add_child(title_row)
if str(r["description"]) != "":
var desc := ModalCard.note(str(r["description"]), Style.INK)
desc.mouse_filter = MOUSE_FILTER_IGNORE
v.add_child(desc)
var info := TemplateKit.summary(tpl)
var params: Array = tpl.get("params", [])
if not params.is_empty():
var names := PackedStringArray()
for p: Dictionary in params:
names.append(str(p.get("label", "")) if str(p.get("label", "")) != "" else str(p["name"]))
info += " · asks for " + ", ".join(names)
var sub := Style.make_label(info, Style.body(), 13, Style.MUTED)
sub.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
sub.mouse_filter = MOUSE_FILTER_IGNORE
v.add_child(sub)
if not r["builtin"]:
var tools := VBoxContainer.new()
tools.add_theme_constant_override("separation", 2)
tools.size_flags_vertical = SIZE_SHRINK_BEGIN
for spec in [["Edit…", func() -> void: _edit(id)],
["Export", func() -> void: main.export_template(id)],
["Delete", func() -> void: _delete(id, str(r["name"]))]]:
var b := Button.new()
b.text = spec[0]
b.add_theme_font_override("font", Style.body())
b.add_theme_font_size_override("font_size", 13)
var n := Style.box(Color(0, 0, 0, 0), 5, [8, 2, 8, 2])
var hv := Style.box(Style.HOVER_ROW, 5, [8, 2, 8, 2])
Style.skin_button(b, n, hv, hv, Style.MUTED, Style.DANGER_INK if spec[0] == "Delete" else Style.INK)
b.pressed.connect(spec[1])
tools.add_child(b)
h.add_child(tools)
_rows[id] = row
return row
func _badge(text: String, bg: Color, fg: Color) -> Control:
var p := PanelContainer.new()
p.add_theme_stylebox_override("panel", Style.box(bg, 5, [6, 1, 6, 2]))
p.size_flags_vertical = SIZE_SHRINK_CENTER
p.mouse_filter = MOUSE_FILTER_IGNORE
p.add_child(Style.make_label(text, Style.body_bold(), 11, fg))
return p
func _choose(id: String) -> void:
var tpl := lib.get_template(id)
if tpl.is_empty():
main.toast("Couldn't read that template")
return
close()
main.begin_template_insert(tpl)
## Edits a template's name, description and fill-in labels in place.
func _edit(id: String) -> void:
var tpl := lib.get_template(id)
if tpl.is_empty():
main.toast("Couldn't read that template")
return
main._add_modal(TemplateForms.save_dialog(tpl, func(out: Dictionary) -> void:
out["replace_id"] = id
main.templating.store_template(out)
refresh(), [], id))
func _delete(id: String, tname: String) -> void:
main._add_modal(ModalCard.confirm("Delete template", "Delete the template “%s”? Cards already inserted from it stay on their spreads." % tname,
"Delete", func() -> void:
if lib.delete(id):
refresh()
main.toast("Deleted template “%s”" % tname)
, true))