From ccabc8e9309bef79320ec61a98951c7003e24584 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 20 Jan 2023 13:33:30 +0000 Subject: [PATCH] LibCards+Games: Return ErrorOr from CardStack::add_all_grabbed_cards() ...and CardGame::pick_up_cards_from_stack() which is its only caller. --- Userland/Games/Solitaire/Game.cpp | 2 +- Userland/Games/Spider/Game.cpp | 2 +- Userland/Libraries/LibCards/CardGame.cpp | 5 +++-- Userland/Libraries/LibCards/CardGame.h | 2 +- Userland/Libraries/LibCards/CardStack.cpp | 16 +++++++++------- Userland/Libraries/LibCards/CardStack.h | 2 +- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Userland/Games/Solitaire/Game.cpp b/Userland/Games/Solitaire/Game.cpp index 8fbb91e3af..adc2f0430c 100644 --- a/Userland/Games/Solitaire/Game.cpp +++ b/Userland/Games/Solitaire/Game.cpp @@ -266,7 +266,7 @@ void Game::mousedown_event(GUI::MouseEvent& event) if (event.button() == GUI::MouseButton::Secondary) { preview_card(to_check, click_location); } else { - pick_up_cards_from_stack(to_check, click_location, Cards::CardStack::MovementRule::Alternating); + pick_up_cards_from_stack(to_check, click_location, Cards::CardStack::MovementRule::Alternating).release_value_but_fixme_should_propagate_errors(); m_mouse_down_location = click_location; m_mouse_down = true; } diff --git a/Userland/Games/Spider/Game.cpp b/Userland/Games/Spider/Game.cpp index 7229e65ea9..a79b929890 100644 --- a/Userland/Games/Spider/Game.cpp +++ b/Userland/Games/Spider/Game.cpp @@ -272,7 +272,7 @@ void Game::mousedown_event(GUI::MouseEvent& event) update(top_card.rect()); } } else if (!is_moving_cards()) { - pick_up_cards_from_stack(to_check, click_location, Cards::CardStack::MovementRule::Same); + pick_up_cards_from_stack(to_check, click_location, Cards::CardStack::MovementRule::Same).release_value_but_fixme_should_propagate_errors(); m_mouse_down_location = click_location; // When the user wants to automatically move cards, do not go into the drag mode. if (event.button() != GUI::MouseButton::Secondary) diff --git a/Userland/Libraries/LibCards/CardGame.cpp b/Userland/Libraries/LibCards/CardGame.cpp index a863128709..0808bd6a91 100644 --- a/Userland/Libraries/LibCards/CardGame.cpp +++ b/Userland/Libraries/LibCards/CardGame.cpp @@ -52,10 +52,11 @@ Gfx::IntRect CardGame::moving_cards_bounds() const return m_moving_cards.first().rect().united(m_moving_cards.last().rect()); } -void CardGame::pick_up_cards_from_stack(Cards::CardStack& stack, Gfx::IntPoint click_location, CardStack::MovementRule movement_rule) +ErrorOr CardGame::pick_up_cards_from_stack(Cards::CardStack& stack, Gfx::IntPoint click_location, CardStack::MovementRule movement_rule) { - stack.add_all_grabbed_cards(click_location, m_moving_cards, movement_rule); + TRY(stack.add_all_grabbed_cards(click_location, m_moving_cards, movement_rule)); m_moving_cards_source_stack = stack; + return {}; } RefPtr CardGame::find_stack_to_drop_on(CardStack::MovementRule movement_rule) const diff --git a/Userland/Libraries/LibCards/CardGame.h b/Userland/Libraries/LibCards/CardGame.h index d6d60e04c2..15ece5eaf4 100644 --- a/Userland/Libraries/LibCards/CardGame.h +++ b/Userland/Libraries/LibCards/CardGame.h @@ -42,7 +42,7 @@ public: NonnullRefPtrVector const& moving_cards() const { return m_moving_cards; } Gfx::IntRect moving_cards_bounds() const; RefPtr moving_cards_source_stack() const { return m_moving_cards_source_stack; } - void pick_up_cards_from_stack(CardStack&, Gfx::IntPoint click_location, CardStack::MovementRule); + ErrorOr pick_up_cards_from_stack(CardStack&, Gfx::IntPoint click_location, CardStack::MovementRule); RefPtr find_stack_to_drop_on(CardStack::MovementRule) const; ErrorOr drop_cards_on_stack(CardStack&, CardStack::MovementRule); void clear_moving_cards(); diff --git a/Userland/Libraries/LibCards/CardStack.cpp b/Userland/Libraries/LibCards/CardStack.cpp index 8d03708f4c..75e6a4b58a 100644 --- a/Userland/Libraries/LibCards/CardStack.cpp +++ b/Userland/Libraries/LibCards/CardStack.cpp @@ -121,7 +121,7 @@ void CardStack::rebound_cards() card.set_position(m_stack_positions.at(card_index++)); } -void CardStack::add_all_grabbed_cards(Gfx::IntPoint click_location, NonnullRefPtrVector& grabbed, MovementRule movement_rule) +ErrorOr CardStack::add_all_grabbed_cards(Gfx::IntPoint click_location, NonnullRefPtrVector& grabbed, MovementRule movement_rule) { VERIFY(grabbed.is_empty()); @@ -129,9 +129,9 @@ void CardStack::add_all_grabbed_cards(Gfx::IntPoint click_location, NonnullRefPt auto& top_card = peek(); if (top_card.rect().contains(click_location)) { top_card.set_moving(true); - grabbed.append(top_card); + TRY(grabbed.try_append(top_card)); } - return; + return {}; } RefPtr last_intersect; @@ -144,22 +144,22 @@ void CardStack::add_all_grabbed_cards(Gfx::IntPoint click_location, NonnullRefPt last_intersect = card; } else if (!last_intersect.is_null()) { if (grabbed.is_empty()) { - grabbed.append(*last_intersect); + TRY(grabbed.try_append(*last_intersect)); last_intersect->set_moving(true); } if (card.is_upside_down()) { grabbed.clear(); - return; + return {}; } card.set_moving(true); - grabbed.append(card); + TRY(grabbed.try_append(card)); } } if (grabbed.is_empty() && !last_intersect.is_null()) { - grabbed.append(*last_intersect); + TRY(grabbed.try_append(*last_intersect)); last_intersect->set_moving(true); } @@ -198,6 +198,8 @@ void CardStack::add_all_grabbed_cards(Gfx::IntPoint click_location, NonnullRefPt } grabbed.clear(); } + + return {}; } bool CardStack::is_allowed_to_push(Card const& card, size_t stack_size, MovementRule movement_rule) const diff --git a/Userland/Libraries/LibCards/CardStack.h b/Userland/Libraries/LibCards/CardStack.h index 7fb164371b..8645a031cc 100644 --- a/Userland/Libraries/LibCards/CardStack.h +++ b/Userland/Libraries/LibCards/CardStack.h @@ -49,7 +49,7 @@ public: void rebound_cards(); bool is_allowed_to_push(Card const&, size_t stack_size = 1, MovementRule movement_rule = MovementRule::Alternating) const; - void add_all_grabbed_cards(Gfx::IntPoint click_location, NonnullRefPtrVector& grabbed, MovementRule movement_rule = MovementRule::Alternating); + ErrorOr add_all_grabbed_cards(Gfx::IntPoint click_location, NonnullRefPtrVector& grabbed, MovementRule movement_rule = MovementRule::Alternating); bool preview_card(Gfx::IntPoint click_location); void clear_card_preview();