1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-12 05:52:08 +00:00

Spider: Use CardGame dragging functionality

This commit is contained in:
Sam Atkins 2022-09-28 17:17:01 +01:00 committed by Sam Atkins
parent 7d37aebc07
commit a4c1ee905f
2 changed files with 25 additions and 60 deletions

View file

@ -67,8 +67,7 @@ void Game::setup(Mode mode)
m_new_deck = Cards::create_deck(0, 0, heart_suits, spade_suits, Cards::Shuffle::Yes); m_new_deck = Cards::create_deck(0, 0, heart_suits, spade_suits, Cards::Shuffle::Yes);
m_focused_stack = nullptr; clear_moving_cards();
m_focused_cards.clear();
m_new_game_animation = true; m_new_game_animation = true;
start_timer(s_timer_interval_ms); start_timer(s_timer_interval_ms);
@ -174,33 +173,28 @@ void Game::paint_event(GUI::PaintEvent& event)
painter.add_clip_rect(frame_inner_rect()); painter.add_clip_rect(frame_inner_rect());
painter.add_clip_rect(event.rect()); painter.add_clip_rect(event.rect());
if (!m_focused_cards.is_empty()) { if (is_moving_cards()) {
for (auto& focused_card : m_focused_cards) for (auto& card : moving_cards())
focused_card.clear(painter, background_color); card.clear(painter, background_color);
} }
for (auto& stack : stacks()) { for (auto& stack : stacks()) {
stack.paint(painter, background_color); stack.paint(painter, background_color);
} }
if (!m_focused_cards.is_empty()) { if (is_moving_cards()) {
for (auto& focused_card : m_focused_cards) { for (auto& card : moving_cards()) {
focused_card.paint(painter); card.paint(painter);
focused_card.save_old_position(); card.save_old_position();
} }
} }
if (!m_mouse_down) { if (!m_mouse_down) {
if (!m_focused_cards.is_empty()) { if (is_moving_cards()) {
for (auto& card : m_focused_cards) for (auto& card : moving_cards())
card.set_moving(false); card.set_moving(false);
m_focused_cards.clear();
}
if (m_focused_stack) {
m_focused_stack->set_focused(false);
m_focused_stack = nullptr;
} }
clear_moving_cards();
} }
} }
@ -229,13 +223,9 @@ void Game::mousedown_event(GUI::MouseEvent& event)
start_timer_if_necessary(); start_timer_if_necessary();
update(top_card.rect()); update(top_card.rect());
} }
} else if (m_focused_cards.is_empty()) { } else if (!is_moving_cards()) {
to_check.add_all_grabbed_cards(click_location, m_focused_cards, Cards::CardStack::MovementRule::Same); pick_up_cards_from_stack(to_check, click_location, Cards::CardStack::MovementRule::Same);
m_mouse_down_location = click_location; m_mouse_down_location = click_location;
if (m_focused_stack)
m_focused_stack->set_focused(false);
to_check.set_focused(true);
m_focused_stack = &to_check;
// 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)
m_mouse_down = true; m_mouse_down = true;
@ -249,28 +239,17 @@ void Game::mousedown_event(GUI::MouseEvent& event)
void Game::move_focused_cards(CardStack& stack) void Game::move_focused_cards(CardStack& stack)
{ {
for (auto& to_intersect : m_focused_cards) { drop_cards_on_stack(stack, Cards::CardStack::MovementRule::Any);
mark_intersecting_stacks_dirty(to_intersect);
stack.push(to_intersect);
(void)m_focused_stack->pop();
}
update_score(-1); update_score(-1);
moving_cards_source_stack()->make_top_card_visible();
update(m_focused_stack->bounding_box());
update(stack.bounding_box());
detect_full_stacks(); detect_full_stacks();
if (m_focused_stack->make_top_card_visible())
update(m_focused_stack->peek().rect());
} }
void Game::mouseup_event(GUI::MouseEvent& event) void Game::mouseup_event(GUI::MouseEvent& event)
{ {
GUI::Frame::mouseup_event(event); GUI::Frame::mouseup_event(event);
if (!m_focused_stack || m_focused_cards.is_empty() || m_new_game_animation || m_draw_animation) if (!is_moving_cards() || m_new_game_animation || m_draw_animation)
return; return;
bool rebound = true; bool rebound = true;
@ -281,37 +260,25 @@ void Game::mouseup_event(GUI::MouseEvent& event)
if (stack.is_focused()) if (stack.is_focused())
continue; continue;
if (stack.is_allowed_to_push(m_focused_cards.at(0), m_focused_cards.size(), Cards::CardStack::MovementRule::Any) && !stack.is_empty()) { if (stack.is_allowed_to_push(moving_cards().at(0), moving_cards().size(), Cards::CardStack::MovementRule::Any) && !stack.is_empty()) {
move_focused_cards(stack); move_focused_cards(stack);
rebound = false; rebound = false;
break; break;
} }
} }
} else { } else if (auto target_stack = find_stack_to_drop_on(Cards::CardStack::MovementRule::Any); !target_stack.is_null()) {
for (auto& stack : stacks()) { auto& stack = *target_stack;
if (stack.is_focused())
continue;
for (auto& focused_card : m_focused_cards) {
if (stack.bounding_box().intersects(focused_card.rect())) {
if (stack.is_allowed_to_push(m_focused_cards.at(0), m_focused_cards.size(), Cards::CardStack::MovementRule::Any)) {
move_focused_cards(stack); move_focused_cards(stack);
rebound = false; rebound = false;
break;
}
}
}
}
} }
if (rebound) { if (rebound) {
for (auto& to_intersect : m_focused_cards) for (auto& to_intersect : moving_cards())
mark_intersecting_stacks_dirty(to_intersect); mark_intersecting_stacks_dirty(to_intersect);
m_focused_stack->rebound_cards(); moving_cards_source_stack()->rebound_cards();
update(m_focused_stack->bounding_box()); update(moving_cards_source_stack()->bounding_box());
} }
m_mouse_down = false; m_mouse_down = false;
@ -328,7 +295,7 @@ void Game::mousemove_event(GUI::MouseEvent& event)
int dx = click_location.dx_relative_to(m_mouse_down_location); int dx = click_location.dx_relative_to(m_mouse_down_location);
int dy = click_location.dy_relative_to(m_mouse_down_location); int dy = click_location.dy_relative_to(m_mouse_down_location);
for (auto& to_intersect : m_focused_cards) { for (auto& to_intersect : moving_cards()) {
mark_intersecting_stacks_dirty(to_intersect); mark_intersecting_stacks_dirty(to_intersect);
to_intersect.rect().translate_by(dx, dy); to_intersect.rect().translate_by(dx, dy);
update(to_intersect.rect()); update(to_intersect.rect());

View file

@ -82,9 +82,7 @@ private:
Mode m_mode { Mode::SingleSuit }; Mode m_mode { Mode::SingleSuit };
NonnullRefPtrVector<Card> m_focused_cards;
NonnullRefPtrVector<Card> m_new_deck; NonnullRefPtrVector<Card> m_new_deck;
CardStack* m_focused_stack { nullptr };
Gfx::IntPoint m_mouse_down_location; Gfx::IntPoint m_mouse_down_location;
bool m_mouse_down { false }; bool m_mouse_down { false };