diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-01-04 18:25:23 -0500 |
---|---|---|
committer | Sam Atkins <atkinssj@gmail.com> | 2023-01-05 13:05:13 +0000 |
commit | 2a09807f2e26f402d929925622ed27d9967c475b (patch) | |
tree | f88d9363800e9b06e7fa1b550e55be866437698b /Userland/Libraries/LibCards/CardPainter.cpp | |
parent | eeb6072f155470b4b47d2eb2ff64a7919a707cf1 (diff) | |
download | serenity-2a09807f2e26f402d929925622ed27d9967c475b.zip |
LibCards: Support highlighting cards of interest
For example, in Solitaire, when dragging a card around, it's common for
other implementations to highlight the card underneath the dragged card
if that other card is a valid drop target. This implementation will draw
a rounded rectangle within the edges of the highlighted card, using a
rudimentary complementary color of the board background color.
Diffstat (limited to 'Userland/Libraries/LibCards/CardPainter.cpp')
-rw-r--r-- | Userland/Libraries/LibCards/CardPainter.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCards/CardPainter.cpp b/Userland/Libraries/LibCards/CardPainter.cpp index c2c869617a..e073f9feae 100644 --- a/Userland/Libraries/LibCards/CardPainter.cpp +++ b/Userland/Libraries/LibCards/CardPainter.cpp @@ -102,6 +102,21 @@ NonnullRefPtr<Gfx::Bitmap> CardPainter::card_back() return *m_card_back; } +NonnullRefPtr<Gfx::Bitmap> CardPainter::card_front_highlighted(Suit suit, Rank rank) +{ + auto suit_id = to_underlying(suit); + auto rank_id = to_underlying(rank); + + auto& existing_bitmap = m_cards_highlighted[suit_id][rank_id]; + if (!existing_bitmap.is_null()) + return *existing_bitmap; + + m_cards_highlighted[suit_id][rank_id] = create_card_bitmap(); + paint_highlighted_card(*m_cards_highlighted[suit_id][rank_id], card_front(suit, rank)); + + return *m_cards_highlighted[suit_id][rank_id]; +} + NonnullRefPtr<Gfx::Bitmap> CardPainter::card_front_inverted(Suit suit, Rank rank) { auto suit_id = to_underlying(suit); @@ -140,6 +155,17 @@ void CardPainter::set_background_image_path(DeprecatedString path) paint_inverted_card(*m_card_back_inverted, *m_card_back); } +void CardPainter::set_background_color(Color background_color) +{ + m_background_color = background_color; + + // Clear any cached card bitmaps that depend on the background color. + for (auto& suit_array : m_cards_highlighted) { + for (auto& rank_array : suit_array) + rank_array = nullptr; + } +} + NonnullRefPtr<Gfx::Bitmap> CardPainter::create_card_bitmap() { return Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { Card::width, Card::height }).release_value_but_fixme_should_propagate_errors(); @@ -212,4 +238,17 @@ void CardPainter::paint_inverted_card(Gfx::Bitmap& bitmap, Gfx::Bitmap const& so }); } +void CardPainter::paint_highlighted_card(Gfx::Bitmap& bitmap, Gfx::Bitmap const& source_to_highlight) +{ + Gfx::Painter painter { bitmap }; + auto paint_rect = source_to_highlight.rect(); + auto background_complement = m_background_color.xored(Color::White); + + painter.fill_rect_with_rounded_corners(paint_rect, Color::Black, Card::card_radius); + paint_rect.shrink(2, 2); + painter.fill_rect_with_rounded_corners(paint_rect, background_complement, Card::card_radius - 1); + paint_rect.shrink(2, 2); + painter.blit({ 4, 4 }, source_to_highlight, source_to_highlight.rect().shrunken(8, 8)); +} + } |