ladybird/Userland/Libraries/LibCards/CardPainter.h
david072 1541942e10 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.
2023-11-24 11:45:32 +00:00

67 lines
2.1 KiB
C++

/*
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2023, David Ganz <david.g.ganz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Array.h>
#include <AK/String.h>
#include <LibCards/Card.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Color.h>
namespace Cards {
class CardPainter {
public:
static CardPainter& the();
NonnullRefPtr<Gfx::Bitmap> card_front(Suit, Rank);
NonnullRefPtr<Gfx::Bitmap> card_back();
NonnullRefPtr<Gfx::Bitmap> card_front_inverted(Suit, Rank);
NonnullRefPtr<Gfx::Bitmap> card_back_inverted();
NonnullRefPtr<Gfx::Bitmap> card_front_highlighted(Suit, Rank);
NonnullRefPtr<Gfx::Bitmap> card_front_disabled(Suit, Rank);
NonnullRefPtr<Gfx::Bitmap> card_back_disabled();
void set_back_image_path(StringView path);
void set_front_images_set_name(StringView path);
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);
void paint_card_back(Gfx::Bitmap&);
void paint_inverted_card(Gfx::Bitmap& bitmap, Gfx::Bitmap const& source_to_invert);
void paint_highlighted_card(Gfx::Bitmap& bitmap, Gfx::Bitmap const& source_to_highlight);
void paint_disabled_card(Gfx::Bitmap& bitmap, Gfx::Bitmap const& source_to_disable);
Array<RefPtr<Gfx::Bitmap>, to_underlying(Suit::__Count)> m_suit_pips;
Array<RefPtr<Gfx::Bitmap>, to_underlying(Suit::__Count)> m_suit_pips_flipped_vertically;
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;
String m_back_image_path;
String m_front_images_set_name;
Color m_background_color;
};
}