extends VBoxContainer ## Collection screen — pill-tab container for Cards and Decks views. ## ## Checks for a background content sync on load; if the cache is already ## syncing (started by InitScreen) the SyncLabel shows "Syncing…" until done. ## If not syncing, triggers a silent background sync to pick up any new cards ## added since last session. const CARD_DETAIL_POPUP := preload("res://scenes/collection/components/CardDetailPopup.tscn") # ── Node references ─────────────────────────────────────────────────────────── @onready var back_btn : Button = %BackBtn @onready var sync_label : Label = %SyncLabel @onready var cards_tab_btn : Button = %CardsTabBtn @onready var decks_tab_btn : Button = %DecksTabBtn @onready var cards_view : VBoxContainer = %CardsView @onready var decks_view : VBoxContainer = %DecksView var _card_popup: Control = null # ── Lifecycle ───────────────────────────────────────────────────────────────── func _ready() -> void: ContentCache.apply_scene_background(self) back_btn.pressed.connect(_on_back) cards_tab_btn.pressed.connect(func(): _show_tab(0)) decks_tab_btn.pressed.connect(func(): _show_tab(1)) # Add popup to the viewport root so it overlays above the VBoxContainer. _card_popup = CARD_DETAIL_POPUP.instantiate() get_tree().get_root().call_deferred("add_child", _card_popup) cards_view.card_selected.connect(_on_card_selected) _show_tab(0) _setup_sync() func _exit_tree() -> void: if ContentCache.sync_complete.is_connected(_on_sync_done): ContentCache.sync_complete.disconnect(_on_sync_done) if ContentCache.sync_failed.is_connected(_on_sync_done_or_failed): ContentCache.sync_failed.disconnect(_on_sync_done_or_failed) if _card_popup != null and is_instance_valid(_card_popup): _card_popup.queue_free() _card_popup = null # ── Card detail ─────────────────────────────────────────────────────────────── func _on_card_selected(dto: Dictionary, def: Dictionary) -> void: if _card_popup != null and is_instance_valid(_card_popup): _card_popup.show_card(dto, def) # ── Tab switching ───────────────────────────────────────────────────────────── func _show_tab(index: int) -> void: cards_view.visible = (index == 0) decks_view.visible = (index == 1) _apply_tab_style(cards_tab_btn, index == 0) _apply_tab_style(decks_tab_btn, index == 1) func _apply_tab_style(btn: Button, active: bool) -> void: var style := StyleBoxFlat.new() style.corner_radius_top_left = 6 style.corner_radius_top_right = 6 style.corner_radius_bottom_left = 6 style.corner_radius_bottom_right = 6 if active: style.bg_color = Color(0.27, 0.47, 0.80, 1.0) else: style.bg_color = Color(0.18, 0.18, 0.26, 1.0) btn.add_theme_stylebox_override("normal", style) btn.add_theme_stylebox_override("hover", style) btn.add_theme_stylebox_override("pressed", style) # ── Background sync ─────────────────────────────────────────────────────────── func _setup_sync() -> void: if ContentCache.is_syncing(): sync_label.text = "Syncing…" sync_label.visible = true ContentCache.sync_complete.connect(_on_sync_done, CONNECT_ONE_SHOT) ContentCache.sync_failed.connect(_on_sync_done_or_failed, CONNECT_ONE_SHOT) else: # Trigger a silent background incremental sync ContentCache.sync_complete.connect(_on_sync_done, CONNECT_ONE_SHOT) ContentCache.sync_failed.connect(_on_sync_done_or_failed, CONNECT_ONE_SHOT) sync_label.text = "Syncing…" sync_label.visible = true ContentCache.start_sync() func _on_sync_done(_total: int) -> void: sync_label.visible = false func _on_sync_done_or_failed(_detail = null) -> void: sync_label.visible = false # ── Navigation ──────────────────────────────────────────────────────────────── func _on_back() -> void: get_tree().change_scene_to_file("res://scenes/main/MainScreen.tscn")