LibCards: Painting disabled cards

This commit is contained in:
david072 2023-11-12 11:35:53 +01:00 committed by Sam Atkins
parent 65b9cb63ee
commit 55168b50dc
Notes: sideshowbarker 2024-07-17 11:33:34 +09:00
4 changed files with 56 additions and 0 deletions

View file

@ -2,6 +2,7 @@
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2023, David Ganz <david.g.ganz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -22,6 +23,8 @@ Card::Card(Suit suit, Rank rank)
void Card::paint(GUI::Painter& painter, bool highlighted) const
{
VERIFY(!(highlighted && m_disabled));
auto& card_painter = CardPainter::the();
auto bitmap = [&]() {
if (m_inverted)
@ -30,6 +33,9 @@ void Card::paint(GUI::Painter& painter, bool highlighted) const
VERIFY(!m_upside_down);
return card_painter.card_front_highlighted(m_suit, m_rank);
}
if (m_disabled) {
return m_upside_down ? card_painter.card_back_disabled() : card_painter.card_front_disabled(m_suit, m_rank);
}
return m_upside_down ? card_painter.card_back() : card_painter.card_front(m_suit, m_rank);
}();
painter.blit(position(), bitmap, bitmap->rect());

View file

@ -2,6 +2,7 @@
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2023, David Ganz <david.g.ganz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -99,6 +100,7 @@ public:
bool is_upside_down() const { return m_upside_down; }
bool is_inverted() const { return m_inverted; }
bool is_previewed() const { return m_previewed; }
bool is_disabled() const { return m_disabled; }
Gfx::Color color() const { return (m_suit == Suit::Diamonds || m_suit == Suit::Hearts) ? Color::Red : Color::Black; }
void set_position(const Gfx::IntPoint p) { m_rect.set_location(p); }
@ -106,6 +108,7 @@ public:
void set_upside_down(bool upside_down) { m_upside_down = upside_down; }
void set_inverted(bool inverted) { m_inverted = inverted; }
void set_previewed(bool previewed) { m_previewed = previewed; }
void set_disabled(bool disabled) { m_disabled = disabled; }
void save_old_position();
@ -125,6 +128,7 @@ private:
bool m_upside_down { false };
bool m_inverted { false };
bool m_previewed { false };
bool m_disabled { false };
};
enum class Shuffle {

View file

@ -2,6 +2,7 @@
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
* Copyright (c) 2022, the SerenityOS developers.
* 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
*/
@ -79,6 +80,8 @@ static constexpr Gfx::CharacterBitmap s_club {
9, 9
};
static constexpr u8 s_disabled_alpha = 90;
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_front(Suit suit, Rank rank)
{
auto suit_id = to_underlying(suit);
@ -120,6 +123,21 @@ NonnullRefPtr<Gfx::Bitmap> CardPainter::card_front_highlighted(Suit suit, Rank r
return *m_cards_highlighted[suit_id][rank_id];
}
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];
}
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_front_inverted(Suit suit, Rank rank)
{
auto suit_id = to_underlying(suit);
@ -146,6 +164,17 @@ NonnullRefPtr<Gfx::Bitmap> CardPainter::card_back_inverted()
return *m_card_back_inverted;
}
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_back_disabled()
{
if (!m_card_back_disabled.is_null())
return *m_card_back_disabled;
m_card_back_disabled = create_card_bitmap();
paint_disabled_card(*m_card_back_disabled, card_back());
return *m_card_back_disabled;
}
void CardPainter::set_back_image_path(StringView path)
{
if (m_back_image_path == path)
@ -467,4 +496,15 @@ void CardPainter::paint_highlighted_card(Gfx::Bitmap& bitmap, Gfx::Bitmap const&
painter.blit({ 4, 4 }, source_to_highlight, source_to_highlight.rect().shrunken(8, 8));
}
void CardPainter::paint_disabled_card(Gfx::Bitmap& bitmap, Gfx::Bitmap const& source_to_disable)
{
Gfx::Painter painter { bitmap };
auto disabled_color = Color(Color::Black);
disabled_color.set_alpha(s_disabled_alpha);
painter.blit_filtered(Gfx::IntPoint {}, source_to_disable, source_to_disable.rect(), [&](Color color) {
return color.blend(disabled_color);
});
}
}

View file

@ -1,5 +1,6 @@
/*
* 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
*/
@ -23,6 +24,8 @@ public:
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);
@ -36,14 +39,17 @@ private:
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;
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;
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;