50 lines
1.6 KiB
GDScript
50 lines
1.6 KiB
GDScript
class_name CardGrid
|
|
extends ScrollContainer
|
|
## Scrollbares Kartenraster mit Beschriftung unter jeder Karte.
|
|
|
|
signal card_clicked(card: Cards.Card)
|
|
|
|
const COLS := 7
|
|
const CW := 100
|
|
const CH := 140
|
|
const GAP := 12
|
|
|
|
var grid: GridContainer
|
|
|
|
|
|
func _init(rect: Rect2) -> void:
|
|
position = rect.position
|
|
size = rect.size
|
|
horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
|
|
follow_focus = true # Gamepad: der fokussierten Karte hinterherscrollen
|
|
grid = GridContainer.new()
|
|
grid.columns = COLS
|
|
grid.add_theme_constant_override("h_separation", GAP)
|
|
grid.add_theme_constant_override("v_separation", GAP)
|
|
add_child(grid)
|
|
|
|
|
|
## cards: Karten; label_fn(card) -> String; dim_fn(card) -> bool; rarity_fn(card) -> String (Optik)
|
|
func fill(cards: Array, label_fn: Callable, dim_fn: Callable, rarity_fn: Callable = Callable()) -> void:
|
|
Style.clear(grid)
|
|
for c in cards:
|
|
var cell := Control.new()
|
|
cell.custom_minimum_size = Vector2(CW, CH + 16)
|
|
cell.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
var v := CardView.new(c, Vector2(CW, CH), true, rarity_fn.call(c) if rarity_fn.is_valid() else "gewöhnlich")
|
|
v.dim = dim_fn.call(c)
|
|
v.clicked.connect(func(_view): card_clicked.emit(c))
|
|
cell.add_child(v)
|
|
var text: String = label_fn.call(c)
|
|
Style.label(cell, text, 12, Style.MUTED if v.dim else Style.TEXT, Rect2(0, CH + 1, CW, 16), "center")
|
|
grid.add_child(cell)
|
|
|
|
|
|
func views() -> Array:
|
|
var out := []
|
|
for cell in grid.get_children():
|
|
for v in cell.get_children():
|
|
if v is CardView:
|
|
out.append(v)
|
|
return out
|