From 0a1796cd778799e31ee4edeaa3646412bc486f27 Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 2 Mar 2026 07:45:10 -0500 Subject: [PATCH] fix: setup() after add_child(); defer scene change from _ready() card_widget.gd uses @onready vars which are null until _ready() fires. _ready() only fires after add_child(), so both cards_view.gd and decks_view.gd must call add_child(widget) before widget.setup(). login_screen.gd called change_scene_to_file() synchronously from _ready(), causing "parent busy" crash. Deferred with call_deferred(). Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .../scenes/collection/views/cards_view.gd | 179 ++++++++++++++++++ .../scenes/collection/views/decks_view.gd | 4 +- .../scenes/login/login_screen.gd | 3 +- 3 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 Client/origin-client/scenes/collection/views/cards_view.gd diff --git a/Client/origin-client/scenes/collection/views/cards_view.gd b/Client/origin-client/scenes/collection/views/cards_view.gd new file mode 100644 index 0000000..d8b3cab --- /dev/null +++ b/Client/origin-client/scenes/collection/views/cards_view.gd @@ -0,0 +1,179 @@ +extends VBoxContainer +## Cards tab — paginated, filterable grid of the player's owned cards. +## +## Fetches from collection.get_cards (page, pageSize, nameFilter?, +## cardTypeFilter?, affinityFilter?). Displays results as CardWidget +## instances in a responsive GridContainer. +## +## Typing in the search box starts a 0.4 s debounce before reloading. +## "Load More" appends the next page without clearing the existing cards. + +const CARD_WIDGET := preload("res://scenes/collection/components/CardWidget.tscn") +const PAGE_SIZE := 20 + +# ── 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 +@onready var load_more_btn : Button = %LoadMoreBtn + +# ── State ───────────────────────────────────────────────────────────────────── + +var _current_page : int = 1 +var _total_count : int = 0 +var _has_more : bool = false +var _loading : bool = false +var _search_timer : float = 0.0 +const _SEARCH_DEBOUNCE := 0.4 + +# 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): _reload()) + affinity_option.item_selected.connect(func(_i): _reload()) + load_more_btn.pressed.connect(_on_load_more) + get_viewport().size_changed.connect(_update_columns) + _reload() + + +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 + _reload() + +# ── Option population ───────────────────────────────────────────────────────── + +func _populate_type_options() -> void: + type_option.clear() + type_option.add_item("All Types") + for t in ["Creature", "Spell", "Item", "ArchSpirit"]: + 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 vp_w := get_viewport_rect().size.x + card_grid.columns = max(2, int(vp_w / 160)) + +# ── Data fetching ───────────────────────────────────────────────────────────── + +func _reload() -> void: + _current_page = 1 + _total_count = 0 + _has_more = false + for child in card_grid.get_children(): + child.queue_free() + _fetch_page(1) + + +func _fetch_page(page: int) -> void: + if _loading: + return + _loading = true + _set_status("loading") + + var payload := { + "page": page, + "pageSize": PAGE_SIZE, + } + var search := search_input.text.strip_edges() + if not search.is_empty(): + payload["nameFilter"] = search + + # Type filter — index 0 = "All Types" (skip) + var type_idx := type_option.selected + if type_idx > 0: + payload["cardTypeFilter"] = type_option.get_item_text(type_idx) + + # Affinity filter — index 0 = "All Affinities" (skip) + var aff_idx := affinity_option.selected + if aff_idx > 0: + payload["affinityFilter"] = affinity_option.get_item_text(aff_idx) + + Network.request( + "collection.get_cards", + payload, + func(_t: String, p: Variant): _on_cards_response(p, page), + func(_code: String, msg: String): _on_error(msg) + ) + + +func _on_cards_response(payload: Variant, page: int) -> void: + _loading = false + var p := payload as Dictionary + var cards : Array = p.get("cards", []) + _total_count = int(p.get("totalCount", 0)) + var page_size : int = int(p.get("pageSize", PAGE_SIZE)) + _current_page = page + _has_more = (page * page_size) < _total_count + + for dto in cards: + var def := ContentCache.get_definition(str(dto.get("cardDefinitionId", ""))) + var widget = CARD_WIDGET.instantiate() as PanelContainer + widget.card_pressed.connect(func(_def_id: String): + card_selected.emit(dto, def)) + card_grid.add_child(widget) # add_child triggers _ready / @onready before setup + widget.setup(dto, def) + + if cards.is_empty() and page == 1: + _set_status("empty") + else: + _set_status("done") + + load_more_btn.visible = _has_more + + +func _on_error(msg: String) -> void: + _loading = false + _set_status("empty") + push_warning("CardsView: %s" % msg) + +# ── Button handlers ─────────────────────────────────────────────────────────── + +func _on_search_changed(_text: String) -> void: + _search_timer = _SEARCH_DEBOUNCE + + +func _on_load_more() -> void: + _fetch_page(_current_page + 1) + +# ── Status helper ───────────────────────────────────────────────────────────── + +func _set_status(state: String) -> void: + loading_label.visible = (state == "loading") + empty_label.visible = (state == "empty") + if state != "loading": + load_more_btn.visible = _has_more diff --git a/Client/origin-client/scenes/collection/views/decks_view.gd b/Client/origin-client/scenes/collection/views/decks_view.gd index 33e803e..fcdf3e5 100644 --- a/Client/origin-client/scenes/collection/views/decks_view.gd +++ b/Client/origin-client/scenes/collection/views/decks_view.gd @@ -115,11 +115,11 @@ func _on_arch_spirits(payload: Variant) -> void: for dto in cards: var def := ContentCache.get_definition(str(dto.get("cardDefinitionId", ""))) var widget = CARD_WIDGET.instantiate() - widget.setup(dto, def) widget.custom_minimum_size = Vector2(110, 154) var def_id := str(dto.get("cardDefinitionId", "")) widget.card_pressed.connect(func(_d: String): _on_arch_spirit_selected(widget, def_id)) - arch_spirit_grid.add_child(widget) + arch_spirit_grid.add_child(widget) # add_child triggers _ready / @onready before setup + widget.setup(dto, def) func _on_arch_spirit_selected(tapped_widget, def_id: String) -> void: diff --git a/Client/origin-client/scenes/login/login_screen.gd b/Client/origin-client/scenes/login/login_screen.gd index 2b99173..b905d35 100644 --- a/Client/origin-client/scenes/login/login_screen.gd +++ b/Client/origin-client/scenes/login/login_screen.gd @@ -43,8 +43,9 @@ var _pending_pass := "" func _ready() -> void: # If already authenticated (e.g. scene reloaded), skip straight to main. + # Use call_deferred so the scene tree is not modified while _ready() is running. if not Auth.user_id.is_empty(): - _go_to_main() + _go_to_main.call_deferred() return Auth.auth_success.connect(_on_auth_success)