Просмотр исходного кода

Solitaire: Highlight cards beneath dragged cards that are valid targets

If the card beneath the card currently being dragged is a valid drop
target, we will highlight it to indicate this to the user.
Timothy Flynn 2 лет назад
Родитель
Сommit
a2277655c0
2 измененных файлов с 26 добавлено и 0 удалено
  1. 23 0
      Userland/Games/Solitaire/Game.cpp
  2. 3 0
      Userland/Games/Solitaire/Game.h

+ 23 - 0
Userland/Games/Solitaire/Game.cpp

@@ -272,6 +272,7 @@ void Game::mousedown_event(GUI::MouseEvent& event)
 void Game::mouseup_event(GUI::MouseEvent& event)
 {
     GUI::Frame::mouseup_event(event);
+    clear_hovered_card();
 
     if (!is_moving_cards() || m_game_over_animation || m_new_game_animation)
         return;
@@ -312,6 +313,18 @@ void Game::mousemove_event(GUI::MouseEvent& event)
     int dx = click_location.dx_relative_to(m_mouse_down_location);
     int dy = click_location.dy_relative_to(m_mouse_down_location);
 
+    if (auto target_stack = find_stack_to_drop_on(Cards::CardStack::MovementRule::Alternating); target_stack && !target_stack->is_empty()) {
+        if (auto& top_card = target_stack->peek(); top_card != m_hovered_card) {
+            clear_hovered_card();
+            m_hovered_card = top_card;
+
+            m_hovered_card->set_highlighted(true);
+            update(m_hovered_card->rect());
+        }
+    } else {
+        clear_hovered_card();
+    }
+
     for (auto& to_intersect : moving_cards()) {
         mark_intersecting_stacks_dirty(to_intersect);
         to_intersect.rect().translate_by(dx, dy);
@@ -619,4 +632,14 @@ void Game::perform_undo()
     invalidate_layout();
 }
 
+void Game::clear_hovered_card()
+{
+    if (!m_hovered_card)
+        return;
+
+    m_hovered_card->set_highlighted(false);
+    update(m_hovered_card->rect());
+    m_hovered_card = nullptr;
+}
+
 }

+ 3 - 0
Userland/Games/Solitaire/Game.h

@@ -174,6 +174,7 @@ private:
     void create_new_animation_card();
     void set_background_fill_enabled(bool);
     void check_for_game_over();
+    void clear_hovered_card();
 
     virtual void paint_event(GUI::PaintEvent&) override;
     virtual void mousedown_event(GUI::MouseEvent&) override;
@@ -203,6 +204,8 @@ private:
     uint8_t m_passes_left_before_punishment { 0 };
 
     bool m_auto_collect { false };
+
+    RefPtr<Card> m_hovered_card;
 };
 
 }