From 12612703f9a80d75a72603982c47ba5966ca9efd Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 28 Sep 2022 13:08:07 +0100 Subject: [PATCH] LibCards+Spider: Move `ensure_top_card_is_visible()` logic to CardStack --- Userland/Games/Spider/Game.cpp | 18 ++++-------------- Userland/Games/Spider/Game.h | 1 - Userland/Libraries/LibCards/CardStack.cpp | 14 ++++++++++++++ Userland/Libraries/LibCards/CardStack.h | 1 + 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Userland/Games/Spider/Game.cpp b/Userland/Games/Spider/Game.cpp index cd8f5a1582..b525802bac 100644 --- a/Userland/Games/Spider/Game.cpp +++ b/Userland/Games/Spider/Game.cpp @@ -115,18 +115,6 @@ void Game::mark_intersecting_stacks_dirty(Card& intersecting_card) update(intersecting_card.rect()); } -void Game::ensure_top_card_is_visible(NonnullRefPtr stack) -{ - if (stack->is_empty()) - return; - - auto& top_card = stack->peek(); - if (top_card.is_upside_down()) { - top_card.set_upside_down(false); - update(top_card.rect()); - } -} - void Game::detect_full_stacks() { auto& completed_stack = stack_at_location(Completed); @@ -160,7 +148,8 @@ void Game::detect_full_stacks() update(original_current_rect); update(completed_stack.bounding_box()); - ensure_top_card_is_visible(current_pile); + if (current_pile.make_top_card_visible()) + update(current_pile.peek().rect()); update_score(101); } @@ -283,7 +272,8 @@ void Game::move_focused_cards(CardStack& stack) detect_full_stacks(); - ensure_top_card_is_visible(*m_focused_stack); + if (m_focused_stack->make_top_card_visible()) + update(m_focused_stack->peek().rect()); } void Game::mouseup_event(GUI::MouseEvent& event) diff --git a/Userland/Games/Spider/Game.h b/Userland/Games/Spider/Game.h index 9addb2ff90..ed7f26766e 100644 --- a/Userland/Games/Spider/Game.h +++ b/Userland/Games/Spider/Game.h @@ -71,7 +71,6 @@ private: void update_score(int delta); void draw_cards(); void mark_intersecting_stacks_dirty(Card& intersecting_card); - void ensure_top_card_is_visible(NonnullRefPtr stack); void detect_full_stacks(); void detect_victory(); void move_focused_cards(CardStack& stack); diff --git a/Userland/Libraries/LibCards/CardStack.cpp b/Userland/Libraries/LibCards/CardStack.cpp index e5a0550013..32139826cd 100644 --- a/Userland/Libraries/LibCards/CardStack.cpp +++ b/Userland/Libraries/LibCards/CardStack.cpp @@ -227,6 +227,20 @@ bool CardStack::is_allowed_to_push(Card const& card, size_t stack_size, Movement return true; } +bool CardStack::make_top_card_visible() +{ + if (is_empty()) + return false; + + auto& top_card = peek(); + if (top_card.is_upside_down()) { + top_card.set_upside_down(false); + return true; + } + + return false; +} + void CardStack::push(NonnullRefPtr card) { auto top_most_position = m_stack_positions.is_empty() ? m_position : m_stack_positions.last(); diff --git a/Userland/Libraries/LibCards/CardStack.h b/Userland/Libraries/LibCards/CardStack.h index 6ccca07fbd..bcde339029 100644 --- a/Userland/Libraries/LibCards/CardStack.h +++ b/Userland/Libraries/LibCards/CardStack.h @@ -43,6 +43,7 @@ public: Gfx::IntRect const& bounding_box() const { return m_bounding_box; } void set_focused(bool focused) { m_focused = focused; } + bool make_top_card_visible(); // Returns true if the card was flipped. void push(NonnullRefPtr card); NonnullRefPtr pop();