extends VBoxContainer ## Cards tab — filterable, sortable grid of the player's owned cards. ## ## Fetches the full owned map once (collection.get_owned), then all filtering, ## search, and sorting happen client-side using ContentCache definitions. ## No "load more" or pagination. const CARD_WIDGET := preload("res://scenes/collection/components/CardWidget.tscn") const CARD_WIDTH_ESTIMATE := 168 # ── Node references ─────────────────────────────────────────────────────────── @onready var search_input : LineEdit = %SearchInput @onready var type_option : OptionButton = %TypeOption @onready var affinity_option : OptionButton = %AffinityOption @onready var card_grid : GridContainer = %CardGrid @onready var loading_label : Label = %LoadingLabel @onready var empty_label : Label = %EmptyLabel # ── State ───────────────────────────────────────────────────────────────────── ## Full owned map: { definitionId(String) → ownedCount(int) } var _owned : Dictionary = {} var _search_timer : float = 0.0 const _SEARCH_DEBOUNCE := 0.15 # Emitted when the user taps a card — parent uses this to show the detail popup. signal card_selected(dto: Dictionary, def: Dictionary) # ── Lifecycle ───────────────────────────────────────────────────────────────── func _ready() -> void: _populate_type_options() _populate_affinity_options() _update_columns() search_input.text_changed.connect(_on_search_changed) type_option.item_selected.connect(func(_i): _rebuild_grid()) affinity_option.item_selected.connect(func(_i): _rebuild_grid()) %LoadMoreBtn.hide() get_viewport().size_changed.connect(_update_columns) _fetch_owned() func _exit_tree() -> void: if get_viewport() and get_viewport().size_changed.is_connected(_update_columns): get_viewport().size_changed.disconnect(_update_columns) func _process(delta: float) -> void: if _search_timer > 0.0: _search_timer -= delta if _search_timer <= 0.0: _search_timer = 0.0 _rebuild_grid() # ── Option population ───────────────────────────────────────────────────────── func _populate_type_options() -> void: type_option.clear() type_option.add_item("All Types") for t in ["Spirit", "Repentance", "Terramorpha", "ArchSpirit", "Ruin", "Instinct"]: type_option.add_item(t) func _populate_affinity_options() -> void: affinity_option.clear() affinity_option.add_item("All Affinities") for a in [ # Basic "Void", "Aether", "Lake", "Hill", "Forest", "Field", # Non-basic "Ocean", "Island", "Marsh", "Swamp", "Mountain", "Steppe", "Fort", "Plains", "Grove", "Jungle", "PerfectAether", "PureVoid", ]: affinity_option.add_item(a) # ── Responsive columns ──────────────────────────────────────────────────────── func _update_columns() -> void: var available_width := card_grid.size.x if available_width <= 0.0: available_width = get_viewport_rect().size.x - 16.0 card_grid.columns = max(1, int(available_width / CARD_WIDTH_ESTIMATE)) # ── Data fetching (one-time) ────────────────────────────────────────────────── func _fetch_owned() -> void: _set_status("loading") var cached := CollectionCache.get_owned_map() if not cached.is_empty(): _owned = cached _rebuild_grid() Network.request( "collection.get_owned", {}, func(_t: String, p: Variant): _on_owned_response(p), func(_code: String, msg: String): _on_owned_error(msg) ) func _on_owned_response(payload: Variant) -> void: var p := payload as Dictionary var raw : Variant = p.get("owned", {}) _owned = raw if typeof(raw) == TYPE_DICTIONARY else {} CollectionCache.store_owned_map(_owned) _rebuild_grid() func _on_owned_error(msg: String) -> void: _set_status("empty") push_warning("CardsView: %s" % msg) # ── Grid rendering (client-side filtered) ──────────────────────────────────── func _rebuild_grid() -> void: for child in card_grid.get_children(): child.queue_free() if _owned.is_empty(): _set_status("empty") return var search := search_input.text.strip_edges().to_lower() var type_idx := type_option.selected var aff_idx := affinity_option.selected var type_filter := type_option.get_item_text(type_idx) if type_idx > 0 else "" var aff_filter := affinity_option.get_item_text(aff_idx) if aff_idx > 0 else "" var entries : Array = [] for def_id_var in _owned: var def_id := str(def_id_var) var count := int(_owned[def_id_var]) var def := ContentCache.get_definition(def_id) if def.is_empty(): continue var name := str(def.get("name", "")) var c_type := str(def.get("cardType", "")) var affinity := str(def.get("affinity", "")) if not search.is_empty() and not name.to_lower().contains(search): continue if not type_filter.is_empty() and c_type != type_filter: continue if not aff_filter.is_empty() and affinity != aff_filter: continue entries.append({ "cardDefinitionId": def_id, "name": name, "cardType": c_type, "affinity": affinity, "rarity": str(def.get("rarity", "")), "tier": def.get("tier"), "copyCount": count, }) entries.sort_custom(func(a, b): return str(a["name"]) < str(b["name"])) if entries.is_empty(): _set_status("empty") return for dto in entries: var def := ContentCache.get_definition(str(dto["cardDefinitionId"])) _add_card_to_grid(dto, def) _set_status("done") func _add_card_to_grid(dto: Dictionary, def: Dictionary) -> void: var count := int(dto.get("copyCount", 1)) var wrapper := VBoxContainer.new() wrapper.add_theme_constant_override("separation", 4) wrapper.size_flags_horizontal = Control.SIZE_SHRINK_CENTER var widget := CARD_WIDGET.instantiate() as PanelContainer widget.card_pressed.connect(func(_pressed_id: String): card_selected.emit(dto.duplicate(true), def)) wrapper.add_child(widget) widget.setup(dto, def) if count > 1: var count_label := Label.new() count_label.text = "x%d" % count count_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER count_label.add_theme_font_size_override("font_size", 11) count_label.modulate = Color(0.72, 0.72, 0.80) wrapper.add_child(count_label) card_grid.add_child(wrapper) # ── Event handlers ──────────────────────────────────────────────────────────── func _on_search_changed(_text: String) -> void: _search_timer = _SEARCH_DEBOUNCE # ── Status helper ───────────────────────────────────────────────────────────── func _set_status(state: String) -> void: loading_label.visible = (state == "loading") empty_label.visible = (state == "empty")