Sfoglia il codice sorgente

LibCards+Spider: Move `ensure_top_card_is_visible()` logic to CardStack

Sam Atkins 2 anni fa
parent
commit
12612703f9

+ 4 - 14
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<CardStack> 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)

+ 0 - 1
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<CardStack> stack);
     void detect_full_stacks();
     void detect_victory();
     void move_focused_cards(CardStack& stack);

+ 14 - 0
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> card)
 {
     auto top_most_position = m_stack_positions.is_empty() ? m_position : m_stack_positions.last();

+ 1 - 0
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> card);
     NonnullRefPtr<Card> pop();