LibCards+Games: Return ErrorOr from CardStack::add_all_grabbed_cards()

...and CardGame::pick_up_cards_from_stack() which is its only caller.
This commit is contained in:
Sam Atkins 2023-01-20 13:33:30 +00:00 committed by Linus Groh
parent c7c4d70f6e
commit ccabc8e930
6 changed files with 16 additions and 13 deletions

View file

@ -266,7 +266,7 @@ void Game::mousedown_event(GUI::MouseEvent& event)
if (event.button() == GUI::MouseButton::Secondary) { if (event.button() == GUI::MouseButton::Secondary) {
preview_card(to_check, click_location); preview_card(to_check, click_location);
} else { } 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_location = click_location;
m_mouse_down = true; m_mouse_down = true;
} }

View file

@ -272,7 +272,7 @@ void Game::mousedown_event(GUI::MouseEvent& event)
update(top_card.rect()); update(top_card.rect());
} }
} else if (!is_moving_cards()) { } 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; m_mouse_down_location = click_location;
// When the user wants to automatically move cards, do not go into the drag mode. // When the user wants to automatically move cards, do not go into the drag mode.
if (event.button() != GUI::MouseButton::Secondary) if (event.button() != GUI::MouseButton::Secondary)

View file

@ -52,10 +52,11 @@ Gfx::IntRect CardGame::moving_cards_bounds() const
return m_moving_cards.first().rect().united(m_moving_cards.last().rect()); 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<void> 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; m_moving_cards_source_stack = stack;
return {};
} }
RefPtr<CardStack> CardGame::find_stack_to_drop_on(CardStack::MovementRule movement_rule) const RefPtr<CardStack> CardGame::find_stack_to_drop_on(CardStack::MovementRule movement_rule) const

View file

@ -42,7 +42,7 @@ public:
NonnullRefPtrVector<Card> const& moving_cards() const { return m_moving_cards; } NonnullRefPtrVector<Card> const& moving_cards() const { return m_moving_cards; }
Gfx::IntRect moving_cards_bounds() const; Gfx::IntRect moving_cards_bounds() const;
RefPtr<CardStack> moving_cards_source_stack() const { return m_moving_cards_source_stack; } RefPtr<CardStack> 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<void> pick_up_cards_from_stack(CardStack&, Gfx::IntPoint click_location, CardStack::MovementRule);
RefPtr<CardStack> find_stack_to_drop_on(CardStack::MovementRule) const; RefPtr<CardStack> find_stack_to_drop_on(CardStack::MovementRule) const;
ErrorOr<void> drop_cards_on_stack(CardStack&, CardStack::MovementRule); ErrorOr<void> drop_cards_on_stack(CardStack&, CardStack::MovementRule);
void clear_moving_cards(); void clear_moving_cards();

View file

@ -121,7 +121,7 @@ void CardStack::rebound_cards()
card.set_position(m_stack_positions.at(card_index++)); card.set_position(m_stack_positions.at(card_index++));
} }
void CardStack::add_all_grabbed_cards(Gfx::IntPoint click_location, NonnullRefPtrVector<Card>& grabbed, MovementRule movement_rule) ErrorOr<void> CardStack::add_all_grabbed_cards(Gfx::IntPoint click_location, NonnullRefPtrVector<Card>& grabbed, MovementRule movement_rule)
{ {
VERIFY(grabbed.is_empty()); VERIFY(grabbed.is_empty());
@ -129,9 +129,9 @@ void CardStack::add_all_grabbed_cards(Gfx::IntPoint click_location, NonnullRefPt
auto& top_card = peek(); auto& top_card = peek();
if (top_card.rect().contains(click_location)) { if (top_card.rect().contains(click_location)) {
top_card.set_moving(true); top_card.set_moving(true);
grabbed.append(top_card); TRY(grabbed.try_append(top_card));
} }
return; return {};
} }
RefPtr<Card> last_intersect; RefPtr<Card> last_intersect;
@ -144,22 +144,22 @@ void CardStack::add_all_grabbed_cards(Gfx::IntPoint click_location, NonnullRefPt
last_intersect = card; last_intersect = card;
} else if (!last_intersect.is_null()) { } else if (!last_intersect.is_null()) {
if (grabbed.is_empty()) { if (grabbed.is_empty()) {
grabbed.append(*last_intersect); TRY(grabbed.try_append(*last_intersect));
last_intersect->set_moving(true); last_intersect->set_moving(true);
} }
if (card.is_upside_down()) { if (card.is_upside_down()) {
grabbed.clear(); grabbed.clear();
return; return {};
} }
card.set_moving(true); card.set_moving(true);
grabbed.append(card); TRY(grabbed.try_append(card));
} }
} }
if (grabbed.is_empty() && !last_intersect.is_null()) { if (grabbed.is_empty() && !last_intersect.is_null()) {
grabbed.append(*last_intersect); TRY(grabbed.try_append(*last_intersect));
last_intersect->set_moving(true); last_intersect->set_moving(true);
} }
@ -198,6 +198,8 @@ void CardStack::add_all_grabbed_cards(Gfx::IntPoint click_location, NonnullRefPt
} }
grabbed.clear(); grabbed.clear();
} }
return {};
} }
bool CardStack::is_allowed_to_push(Card const& card, size_t stack_size, MovementRule movement_rule) const bool CardStack::is_allowed_to_push(Card const& card, size_t stack_size, MovementRule movement_rule) const

View file

@ -49,7 +49,7 @@ public:
void rebound_cards(); void rebound_cards();
bool is_allowed_to_push(Card const&, size_t stack_size = 1, MovementRule movement_rule = MovementRule::Alternating) const; 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<Card>& grabbed, MovementRule movement_rule = MovementRule::Alternating); ErrorOr<void> add_all_grabbed_cards(Gfx::IntPoint click_location, NonnullRefPtrVector<Card>& grabbed, MovementRule movement_rule = MovementRule::Alternating);
bool preview_card(Gfx::IntPoint click_location); bool preview_card(Gfx::IntPoint click_location);
void clear_card_preview(); void clear_card_preview();