1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:57:45 +00:00

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.
This commit is contained in:
Timothy Flynn 2023-01-04 18:25:23 -05:00 committed by Sam Atkins
parent eeb6072f15
commit 2a09807f2e
5 changed files with 54 additions and 1 deletions

View file

@ -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));
}
}