TableTower/ui/template_picker.gd
RobinFiredrake 8f16d337cf UI pass: striped rows, View/Help menus, zoom control, card ⋯ menu, fuller sample
- Striped result rows (parchment tint, on by default, `striped_rows` setting).
- Top bar: View ▾ (Plain words / Rule text, Collapse/Expand all, Striped rows,
  Minimap) and a "− 90% ▾ +" zoom control replace the Plain|Text switch,
  Collapse all, −/Fit/+ and Map. Arrange ▾ folds into Board ▾; Search is an icon.
- Help ▾ and F1 open a Help window; the 17 topics live in help/topics.txt.
- Build cards get a ⋯ menu (settings, template, collapse, delete) instead of
  the gear and footer Delete, sharing Style.overflow_button with group cards.
  Row × buttons show only on hover, focus or selection.
- Sample: Luck value card, Make Camp (pick card) and Search the Ruins
  (1d6 + Luck, with results only a modifier reaches).
- New test suite test_top_bar; Canvas/GraphML round-trip tests compare plain
  tables only, since logic keys are JSON-only.
- DESIGN.md: open items, including the Card Mode UI redesign.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 10:36:38 -06:00

196 lines
6.7 KiB
GDScript

class_name TemplatePicker
extends ModalCard
## "+ Card ▾ → From template…": 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 Color("#f7f3e8"), 10, [12, 10, 12, 10]), 1, Style.COPPER 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.FELT, Style.BRASS))
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 [["Rename", func() -> void: _rename(id, str(r["name"]))],
["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)
func _rename(id: String, old: String) -> void:
main._add_modal(ModalCard.ask_text("Rename template", "Name", old, "Rename", func(n: String) -> void:
if lib.rename(id, n):
refresh()
))
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 boards." % tname,
"Delete", func() -> void:
if lib.delete(id):
refresh()
main.toast("Deleted template “%s”" % tname)
, true))