TableTower/ui/card_palette.gd

204 lines
7.6 KiB
GDScript3
Raw Normal View History

class_name CardPalette
extends VBoxContainer
## The Cards tab of the left dock: every kind of card (CardCatalog) and your
## templates, in groups, with a search box at the top. 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 search: LineEdit
var notice: PanelContainer
var _items: VBoxContainer
var _templates: Array[Dictionary] = []
var _dragging: Dictionary = {}
var _mode := -1
func setup(p_main: Main) -> void:
m = p_main
add_theme_constant_override("separation", 8)
notice = _make_notice()
add_child(notice)
search = LineEdit.new()
search.placeholder_text = "Search cards and templates"
search.clear_button_enabled = true
ModalCard.style_line_edit(search)
search.text_changed.connect(func(_s: String) -> void: rebuild())
search.text_submitted.connect(func(_s: String) -> void: _add_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", 4)
scroll.add_child(_items)
var hint := Style.make_label("Click to add in the middle of the view, or drag onto the spread.", Style.body(), 12, Style.ON_BG_MUTED)
hint.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
add_child(hint)
rebuild()
func _process(_delta: float) -> void:
if int(m.mode) != _mode:
_mode = int(m.mode)
var play := m.mode != Main.Mode.BUILD
notice.visible = play
_items.modulate.a = 0.45 if play else 1.0
func _make_notice() -> PanelContainer:
var box := PanelContainer.new()
box.add_theme_stylebox_override("panel", Style.bordered(Style.box(Style.CARD, 8, [10, 8, 10, 8]), 2, Style.TRIM))
var col := VBoxContainer.new()
col.add_theme_constant_override("separation", 6)
box.add_child(col)
var text := Style.make_label(BuildModeHint.PALETTE_MESSAGE, Style.body_bold(), 14, Style.INK)
text.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
col.add_child(text)
var go := ModalCard.make_button("Switch to Build", "primary")
go.name = "SwitchToBuild"
go.pressed.connect(func() -> void: m.set_mode(Main.Mode.BUILD))
col.add_child(go)
return box
## 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 []
rebuild()
## Rebuilds the item list from the catalog, filtered by the search text.
func rebuild() -> void:
for c in _items.get_children():
_items.remove_child(c)
c.queue_free()
var query := search.text.strip_edges().to_lower()
var any := false
for group in CardCatalog.groups():
var shown: Array = CardCatalog.ITEMS.filter(func(item: Dictionary) -> bool:
return item["group"] == group and CardCatalog.matches(item, query))
any = _add_group(group, shown.map(func(item: Dictionary) -> Array: return [item, _add_card.bind(item)])) or any
var templates: Array = []
for row: Dictionary in _templates:
var item := {"label": row["name"], "group": CardCatalog.GROUP_TEMPLATES, "tip": row.get("description", ""), "words": "template"}
if CardCatalog.matches(item, query):
templates.append([item, _add_template.bind(str(row["id"]))])
if CardCatalog.matches(ALL_TEMPLATES, query):
templates.append([ALL_TEMPLATES, func(_at: Vector2) -> void: m.templating.open_template_picker()])
any = _add_group(CardCatalog.GROUP_TEMPLATES, templates) or any
if CardCatalog.matches(GROUP_ITEM, query):
any = _add_group(CardCatalog.GROUP_OTHER, [[GROUP_ITEM, _add_group_card]]) or any
if not any:
var none := Style.make_label("Nothing matches “%s”." % search.text.strip_edges(), Style.body(), 14, Style.ON_BG_MUTED)
none.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
_items.add_child(none)
## A group heading and its items in two columns. `entries` holds
## [item, on_add(at)] pairs; at = Vector2.INF means the middle of the view.
func _add_group(title: String, entries: Array) -> bool:
if entries.is_empty():
return false
_items.add_child(Style.make_label(title.to_upper(), Style.caps(), 11, Style.ON_BG_MUTED))
var grid := GridContainer.new()
grid.columns = 2
grid.add_theme_constant_override("h_separation", 4)
grid.add_theme_constant_override("v_separation", 4)
_items.add_child(grid)
for entry: Array in entries:
grid.add_child(_item_button(entry[0], entry[1]))
return true
func _item_button(item: Dictionary, on_add: Callable) -> Button:
var b := Style.bar_button(str(item["label"]))
b.name = str(item["label"]).validate_node_name()
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
## Adds the item, or explains Build mode in Play.
func _use(on_add: Callable, at: Vector2) -> void:
if m.mode != Main.Mode.BUILD:
BuildModeHint.explain(m)
return
on_add.call(at)
if m.mobile:
m.mobile.show_list(false)
## Enter in the search box adds the first card that matches.
func _add_first() -> void:
var query := search.text.strip_edges().to_lower()
for item: Dictionary in CardCatalog.ITEMS:
if CardCatalog.matches(item, query):
_use(_add_card.bind(item), Vector2.INF)
return
func _start_drag(item: Dictionary, on_add: Callable) -> Variant:
if m.mode != Main.Mode.BUILD:
return null
_dragging = {"on_add": 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 palette places the card where it was let go
## (only on the spread; let go anywhere else and nothing is added).
func _notification(what: int) -> void:
if what == NOTIFICATION_DRAG_END and not _dragging.is_empty():
var on_add: Callable = _dragging["on_add"]
_dragging = {}
if get_viewport().gui_is_drag_successful():
return
drop_at(on_add, get_global_mouse_position())
## 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)
_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)