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

197 lines
7.5 KiB
GDScript

class_name PaletteList
extends VBoxContainer
## A palette: a search box over grouped items in two columns. Click an item to
## add it; drag it to drop it somewhere. Shared by the card palette
## (CardPalette, spreads) and the field palette (FieldPalette, sheets).
## The owner fills it through callables:
## - sections_for(query) -> [[group title, [[item, on_add(at)], …]], …]
## (item: {label, tip, group, words}; at = Vector2.INF for "no place given")
## - can_use() -> bool: false greys the items (Play mode); a click then calls on_blocked()
## - drop_at(on_add, screen_point) -> bool: places a dragged item, true if taken
## - after_use(): runs after an item was added (phones go back to the canvas)
var search: LineEdit
var sections_for := Callable()
var can_use := Callable()
var on_blocked := Callable()
var drop_at := Callable()
var after_use := Callable()
var _items: VBoxContainer
var _dragging := Callable()
var _greyed := false
var _try_words := ""
func setup(placeholder: String, hint_text: String, title_text: String = "", try_words: String = "") -> void:
add_theme_constant_override("separation", 12)
_try_words = try_words
if title_text != "":
var head := VBoxContainer.new()
head.add_theme_constant_override("separation", 0)
head.add_child(Style.make_label(title_text, Style.display(), 19, Style.ON_BG))
var hint := Style.make_label(hint_text, Style.body(), 13, Style.ON_BG_MUTED)
hint.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
head.add_child(hint)
add_child(head)
search = LineEdit.new()
search.placeholder_text = placeholder
search.clear_button_enabled = true
style_search(search)
search.text_changed.connect(func(_s: String) -> void: rebuild())
search.text_submitted.connect(func(_s: String) -> void: use_first())
add_child(search)
var scroll := ScrollContainer.new()
scroll.size_flags_vertical = SIZE_EXPAND_FILL
scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
add_child(scroll)
_items = VBoxContainer.new()
_items.size_flags_horizontal = SIZE_EXPAND_FILL
_items.add_theme_constant_override("separation", 12)
scroll.add_child(_items)
if title_text == "":
var hint := Style.make_label(hint_text, Style.body(), 12, Style.ON_BG_MUTED)
hint.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
add_child(hint)
## A search box for a dark panel: translucent, with a magnifier on the left.
static func style_search(edit: LineEdit) -> void:
var normal := Style.bordered(Style.box(Color(1, 1, 1, 0.07), 8, [32, 8, 8, 8]), 1, Color(1, 1, 1, 0.22))
var focus := Style.bordered(Style.box(Color(1, 1, 1, 0.1), 8, [32, 8, 8, 8]), 1, Style.ACCENT)
edit.add_theme_stylebox_override("normal", normal)
edit.add_theme_stylebox_override("focus", focus)
edit.add_theme_color_override("font_color", Style.ON_BG)
edit.add_theme_color_override("font_placeholder_color", Style.ON_BG_MUTED)
edit.add_theme_color_override("caret_color", Style.ON_BG)
edit.add_theme_color_override("clear_button_color", Style.ON_BG_MUTED)
edit.add_theme_font_size_override("font_size", 14)
var icon := MenuIcons.make(MenuIcons.SEARCH, Style.ON_BG_MUTED, 16)
icon.position = Vector2(10, 10)
edit.add_child(icon)
## True when `query` (lower case, may be "") matches every word against the
## item's label, tip, group or search words.
static func matches(item: Dictionary, query: String) -> bool:
if query == "":
return true
var hay := " ".join([item.get("label", ""), item.get("tip", ""), item.get("group", ""), item.get("words", "")]).to_lower()
for word in query.split(" ", false):
if not hay.contains(word):
return false
return true
func query() -> String:
return search.text.strip_edges().to_lower()
## Greys the items (true) or not; checked every frame against can_use.
func _process(_delta: float) -> void:
var grey: bool = can_use.is_valid() and not can_use.call()
if grey != _greyed:
_greyed = grey
_items.modulate.a = 0.45 if grey else 1.0
func rebuild() -> void:
for c in _items.get_children():
_items.remove_child(c)
c.queue_free()
var any := false
for section: Array in _sections():
if (section[1] as Array).is_empty():
continue
any = true
var group := VBoxContainer.new()
group.add_theme_constant_override("separation", 6)
_items.add_child(group)
group.add_child(Style.make_label(str(section[0]).to_upper(), Style.caps(), 11, Style.ACCENT))
var grid := GridContainer.new()
grid.columns = 2
grid.add_theme_constant_override("h_separation", 5)
grid.add_theme_constant_override("v_separation", 5)
group.add_child(grid)
for entry: Array in section[1]:
grid.add_child(_item_button(entry[0], entry[1]))
if not any:
var box := PanelContainer.new()
var dashed := StyleBoxFlat.new()
dashed.bg_color = Color(0, 0, 0, 0)
dashed.border_color = Color(1, 1, 1, 0.25)
dashed.set_border_width_all(1)
dashed.set_corner_radius_all(8)
dashed.set_content_margin_all(10)
box.add_theme_stylebox_override("panel", dashed)
var text := "Nothing matches “%s”." % search.text.strip_edges()
if _try_words != "":
text += " Try a word like %s." % _try_words
var none := Style.make_label(text, Style.body(), 13, Style.ON_BG_MUTED)
none.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
box.add_child(none)
_items.add_child(box)
func _sections() -> Array:
return sections_for.call(query()) if sections_for.is_valid() else []
## Enter in the search box adds the first item shown.
func use_first() -> void:
for section: Array in _sections():
if not (section[1] as Array).is_empty():
use(section[1][0][1], Vector2.INF)
return
## Adds through `on_add`, or calls on_blocked when items are greyed.
func use(on_add: Callable, at: Vector2) -> void:
if can_use.is_valid() and not can_use.call():
if on_blocked.is_valid():
on_blocked.call()
return
on_add.call(at)
if after_use.is_valid():
after_use.call()
func _item_button(item: Dictionary, on_add: Callable) -> Button:
var b := Button.new()
b.text = str(item["label"])
b.name = str(item["label"]).validate_node_name()
var normal := Style.bordered(Style.box(Color(1, 1, 1, 0.05), 6, [8, 6, 8, 6]), 1, Color(1, 1, 1, 0.14))
var hover := Style.bordered(Style.box(Color(1, 1, 1, 0.12), 6, [8, 6, 8, 6]), 1, Color(1, 1, 1, 0.3))
Style.skin_button(b, normal, hover, hover, Style.ON_BG)
b.add_theme_font_size_override("font_size", 13)
b.mouse_default_cursor_shape = Control.CURSOR_DRAG
b.size_flags_horizontal = SIZE_EXPAND_FILL
b.alignment = HORIZONTAL_ALIGNMENT_LEFT
b.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
b.clip_text = true
b.custom_minimum_size.x = 60
b.tooltip_text = str(item.get("tip", ""))
b.focus_mode = Control.FOCUS_NONE
b.pressed.connect(func() -> void: use(on_add, Vector2.INF))
b.set_drag_forwarding(func(_at: Vector2) -> Variant: return _start_drag(item, on_add), Callable(), Callable())
return b
func _start_drag(item: Dictionary, on_add: Callable) -> Variant:
if can_use.is_valid() and not can_use.call():
return null
_dragging = on_add
var label := Style.make_label(str(item["label"]), Style.body_bold(), 15, Style.INK)
var chip := PanelContainer.new()
chip.add_theme_stylebox_override("panel", Style.box(Style.LIT, 6, [8, 4, 8, 4]))
chip.add_child(label)
set_drag_preview(chip)
return {"palette_item": item["label"]}
## Nothing took the drop, so the owner places the item where it was let go.
func _notification(what: int) -> void:
if what == NOTIFICATION_DRAG_END and _dragging.is_valid():
var on_add := _dragging
_dragging = Callable()
if not get_viewport().gui_is_drag_successful() and drop_at.is_valid():
drop_at.call(on_add, get_global_mouse_position())