TableTower/ui/card_palette.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

95 lines
3.6 KiB
GDScript

class_name CardPalette
extends VBoxContainer
## The Cards tab of the left dock: every kind of card (CardCatalog) and your
## templates in a PaletteList. Click an item to add it in the middle of the
## view; drag it to place it on the spread.
## In Play the items are greyed under a notice with "Switch to Build"
## (BuildModeHint), so players learn where cards are added.
## Palette-only items that aren't card kinds.
const GROUP_ITEM := {"label": "Empty group", "group": CardCatalog.GROUP_OTHER,
"tip": "A new, empty group. Drag cards onto it.", "words": "group frame folder"}
const ALL_TEMPLATES := {"label": "All templates…", "group": CardCatalog.GROUP_TEMPLATES,
"tip": "Search, import, rename and delete templates", "words": "import manage"}
var m: Main
var list: PaletteList
var search: LineEdit
var notice: PanelContainer
var _templates: Array[Dictionary] = []
func setup(p_main: Main) -> void:
m = p_main
add_theme_constant_override("separation", 8)
notice = BuildModeHint.make_notice(m, func() -> void: m.set_mode(Main.Mode.BUILD))
add_child(notice)
list = PaletteList.new()
list.setup("Search cards and templates", "Drag onto the spread, or click to add", "Add a card", "“dice”, “choice” or “if”")
list.size_flags_vertical = SIZE_EXPAND_FILL
list.sections_for = _sections
list.can_use = func() -> bool: return m.mode == Main.Mode.BUILD
list.on_blocked = BuildModeHint.explain.bind(m)
list.drop_at = drop_at
list.after_use = func() -> void:
if m.mobile:
m.mobile.show_list(false)
add_child(list)
search = list.search
list.rebuild()
func _process(_delta: float) -> void:
notice.visible = m.mode != Main.Mode.BUILD
## Reads the template list again (it lives in files, so not on every keystroke).
func refresh_templates() -> void:
_templates = m.templating.templates().list() if m.templating else []
list.rebuild()
func _sections(query: String) -> Array:
var out: Array = []
for group in CardCatalog.groups():
var entries: Array = []
for item: Dictionary in CardCatalog.ITEMS:
if item["group"] == group and PaletteList.matches(item, query):
entries.append([item, _add_card.bind(item)])
out.append([group, entries])
var templates: Array = []
for row: Dictionary in _templates:
var item := {"label": row["name"], "group": CardCatalog.GROUP_TEMPLATES, "tip": row.get("description", ""), "words": "template"}
if PaletteList.matches(item, query):
templates.append([item, _add_template.bind(str(row["id"]))])
if PaletteList.matches(ALL_TEMPLATES, query):
templates.append([ALL_TEMPLATES, func(_at: Vector2) -> void: m.templating.open_template_picker()])
out.append([CardCatalog.GROUP_TEMPLATES, templates])
out.append([CardCatalog.GROUP_OTHER, [[GROUP_ITEM, _add_group_card]] if PaletteList.matches(GROUP_ITEM, query) else []])
return out
## Adds through `on_add` with its top-left corner under a screen point on the spread.
func drop_at(on_add: Callable, screen_point: Vector2) -> bool:
var board := m.board
if not board.is_visible_in_tree() or not board.get_global_rect().has_point(screen_point):
return false
var world := board.screen_to_world(screen_point - board.global_position)
list.use(on_add, (world - Vector2(128, 20)).round())
return true
func _add_card(at: Vector2, item: Dictionary) -> void:
m.editing.add_table(str(item["kind"]), str(item["sub"]), at, str(item["variant"]))
func _add_template(at: Vector2, id: String) -> void:
var tpl := m.templating.templates().get_template(id)
if tpl.is_empty():
m.toast("Couldn't read that template")
return
m.templating.begin_template_insert(tpl, at)
func _add_group_card(at: Vector2) -> void:
m.list_view.new_group(at)