mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibCards/CardPainter: Add a helper for accessing the caches
The CardPainter in LibCards caches already painted bitmaps. This adds a helper that gets the bitmap from a cache or creates a new one, if the it doesn't exist yet. It does this by calling a creator function with the new bitmap, which can then paint into the bitmap. This makes the code a bit simpler and shorter.
This commit is contained in:
parent
bac7c057e9
commit
1541942e10
Notes:
sideshowbarker
2024-07-17 10:16:43 +09:00
Author: https://github.com/david072 Commit: https://github.com/SerenityOS/serenity/commit/1541942e10 Pull-request: https://github.com/SerenityOS/serenity/pull/21907 Reviewed-by: https://github.com/AtkinsSJ ✅
2 changed files with 32 additions and 42 deletions
|
@ -82,19 +82,26 @@ static constexpr Gfx::CharacterBitmap s_club {
|
|||
|
||||
static constexpr u8 s_disabled_alpha = 90;
|
||||
|
||||
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_front(Suit suit, Rank rank)
|
||||
NonnullRefPtr<Gfx::Bitmap> CardPainter::get_bitmap_or_create(Suit suit, Rank rank, CardPainter::PaintCache& cache, Function<void(Gfx::Bitmap&)> creator)
|
||||
{
|
||||
auto suit_id = to_underlying(suit);
|
||||
auto rank_id = to_underlying(rank);
|
||||
|
||||
auto& existing_bitmap = m_cards[suit_id][rank_id];
|
||||
auto& existing_bitmap = cache[suit_id][rank_id];
|
||||
if (!existing_bitmap.is_null())
|
||||
return *existing_bitmap;
|
||||
|
||||
m_cards[suit_id][rank_id] = create_card_bitmap();
|
||||
paint_card_front(*m_cards[suit_id][rank_id], suit, rank);
|
||||
auto bitmap = create_card_bitmap();
|
||||
creator(bitmap);
|
||||
cache[suit_id][rank_id] = move(bitmap);
|
||||
return *cache[suit_id][rank_id];
|
||||
}
|
||||
|
||||
return *m_cards[suit_id][rank_id];
|
||||
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_front(Suit suit, Rank rank)
|
||||
{
|
||||
return get_bitmap_or_create(suit, rank, m_cards, [this, suit, rank](auto& bitmap) {
|
||||
paint_card_front(bitmap, suit, rank);
|
||||
});
|
||||
}
|
||||
|
||||
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_back()
|
||||
|
@ -110,47 +117,23 @@ NonnullRefPtr<Gfx::Bitmap> CardPainter::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];
|
||||
return get_bitmap_or_create(suit, rank, m_cards_highlighted, [this, suit, rank](auto& bitmap) {
|
||||
paint_highlighted_card(bitmap, card_front(suit, rank));
|
||||
});
|
||||
}
|
||||
|
||||
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_front_disabled(Suit suit, Rank rank)
|
||||
{
|
||||
auto suit_id = to_underlying(suit);
|
||||
auto rank_id = to_underlying(rank);
|
||||
|
||||
auto& existing_bitmap = m_cards_disabled[suit_id][rank_id];
|
||||
if (!existing_bitmap.is_null())
|
||||
return *existing_bitmap;
|
||||
|
||||
m_cards_disabled[suit_id][rank_id] = create_card_bitmap();
|
||||
paint_disabled_card(*m_cards_disabled[suit_id][rank_id], card_front(suit, rank));
|
||||
|
||||
return *m_cards_disabled[suit_id][rank_id];
|
||||
return get_bitmap_or_create(suit, rank, m_cards_disabled, [this, suit, rank](auto& bitmap) {
|
||||
paint_disabled_card(bitmap, card_front(suit, rank));
|
||||
});
|
||||
}
|
||||
|
||||
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_front_inverted(Suit suit, Rank rank)
|
||||
{
|
||||
auto suit_id = to_underlying(suit);
|
||||
auto rank_id = to_underlying(rank);
|
||||
|
||||
auto& existing_bitmap = m_cards_inverted[suit_id][rank_id];
|
||||
if (!existing_bitmap.is_null())
|
||||
return *existing_bitmap;
|
||||
|
||||
m_cards_inverted[suit_id][rank_id] = create_card_bitmap();
|
||||
paint_inverted_card(*m_cards_inverted[suit_id][rank_id], card_front(suit, rank));
|
||||
|
||||
return *m_cards_inverted[suit_id][rank_id];
|
||||
return get_bitmap_or_create(suit, rank, m_cards_inverted, [this, suit, rank](auto& bitmap) {
|
||||
paint_inverted_card(bitmap, card_front(suit, rank));
|
||||
});
|
||||
}
|
||||
|
||||
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_back_inverted()
|
||||
|
|
|
@ -32,7 +32,12 @@ public:
|
|||
void set_background_color(Color);
|
||||
|
||||
private:
|
||||
using PaintCache = Array<Array<RefPtr<Gfx::Bitmap>, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)>;
|
||||
|
||||
CardPainter();
|
||||
|
||||
NonnullRefPtr<Gfx::Bitmap> get_bitmap_or_create(Suit, Rank, PaintCache&, Function<void(Gfx::Bitmap&)>);
|
||||
|
||||
NonnullRefPtr<Gfx::Bitmap> create_card_bitmap();
|
||||
void paint_card_front(Gfx::Bitmap&, Suit, Rank);
|
||||
void paint_card_front_pips(Gfx::Bitmap&, Suit, Rank);
|
||||
|
@ -43,10 +48,12 @@ private:
|
|||
|
||||
Array<RefPtr<Gfx::Bitmap>, to_underlying(Suit::__Count)> m_suit_pips;
|
||||
Array<RefPtr<Gfx::Bitmap>, to_underlying(Suit::__Count)> m_suit_pips_flipped_vertically;
|
||||
Array<Array<RefPtr<Gfx::Bitmap>, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)> m_cards;
|
||||
Array<Array<RefPtr<Gfx::Bitmap>, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)> m_cards_inverted;
|
||||
Array<Array<RefPtr<Gfx::Bitmap>, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)> m_cards_highlighted;
|
||||
Array<Array<RefPtr<Gfx::Bitmap>, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)> m_cards_disabled;
|
||||
|
||||
PaintCache m_cards;
|
||||
PaintCache m_cards_inverted;
|
||||
PaintCache m_cards_highlighted;
|
||||
PaintCache m_cards_disabled;
|
||||
|
||||
RefPtr<Gfx::Bitmap> m_card_back;
|
||||
RefPtr<Gfx::Bitmap> m_card_back_inverted;
|
||||
RefPtr<Gfx::Bitmap> m_card_back_disabled;
|
||||
|
|
Loading…
Reference in a new issue