mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47: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:
parent
eeb6072f15
commit
2a09807f2e
5 changed files with 54 additions and 1 deletions
|
@ -26,7 +26,10 @@ void Card::paint(GUI::Painter& painter) const
|
||||||
auto bitmap = [&]() {
|
auto bitmap = [&]() {
|
||||||
if (m_inverted)
|
if (m_inverted)
|
||||||
return m_upside_down ? card_painter.card_back_inverted() : card_painter.card_front_inverted(m_suit, m_rank);
|
return m_upside_down ? card_painter.card_back_inverted() : card_painter.card_front_inverted(m_suit, m_rank);
|
||||||
|
if (m_highlighted) {
|
||||||
|
VERIFY(!m_upside_down);
|
||||||
|
return card_painter.card_front_highlighted(m_suit, m_rank);
|
||||||
|
}
|
||||||
return m_upside_down ? card_painter.card_back() : card_painter.card_front(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());
|
painter.blit(position(), bitmap, bitmap->rect());
|
||||||
|
|
|
@ -104,6 +104,7 @@ public:
|
||||||
void set_moving(bool moving) { m_moving = moving; }
|
void set_moving(bool moving) { m_moving = moving; }
|
||||||
void set_upside_down(bool upside_down) { m_upside_down = upside_down; }
|
void set_upside_down(bool upside_down) { m_upside_down = upside_down; }
|
||||||
void set_inverted(bool inverted) { m_inverted = inverted; }
|
void set_inverted(bool inverted) { m_inverted = inverted; }
|
||||||
|
void set_highlighted(bool highlighted) { m_highlighted = highlighted; }
|
||||||
|
|
||||||
void save_old_position();
|
void save_old_position();
|
||||||
|
|
||||||
|
@ -122,6 +123,7 @@ private:
|
||||||
bool m_moving { false };
|
bool m_moving { false };
|
||||||
bool m_upside_down { false };
|
bool m_upside_down { false };
|
||||||
bool m_inverted { false };
|
bool m_inverted { false };
|
||||||
|
bool m_highlighted { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Shuffle {
|
enum class Shuffle {
|
||||||
|
|
|
@ -126,5 +126,8 @@ void CardGame::set_background_color(Gfx::Color color)
|
||||||
auto new_palette = palette();
|
auto new_palette = palette();
|
||||||
new_palette.set_color(Gfx::ColorRole::Background, color);
|
new_palette.set_color(Gfx::ColorRole::Background, color);
|
||||||
set_palette(new_palette);
|
set_palette(new_palette);
|
||||||
|
|
||||||
|
CardPainter::the().set_background_color(color);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,6 +102,21 @@ NonnullRefPtr<Gfx::Bitmap> CardPainter::card_back()
|
||||||
return *m_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)
|
NonnullRefPtr<Gfx::Bitmap> CardPainter::card_front_inverted(Suit suit, Rank rank)
|
||||||
{
|
{
|
||||||
auto suit_id = to_underlying(suit);
|
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);
|
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()
|
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();
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <AK/Array.h>
|
#include <AK/Array.h>
|
||||||
#include <LibCards/Card.h>
|
#include <LibCards/Card.h>
|
||||||
#include <LibGfx/Bitmap.h>
|
#include <LibGfx/Bitmap.h>
|
||||||
|
#include <LibGfx/Color.h>
|
||||||
|
|
||||||
namespace Cards {
|
namespace Cards {
|
||||||
|
|
||||||
|
@ -20,8 +21,10 @@ public:
|
||||||
NonnullRefPtr<Gfx::Bitmap> card_back();
|
NonnullRefPtr<Gfx::Bitmap> card_back();
|
||||||
NonnullRefPtr<Gfx::Bitmap> card_front_inverted(Suit, Rank);
|
NonnullRefPtr<Gfx::Bitmap> card_front_inverted(Suit, Rank);
|
||||||
NonnullRefPtr<Gfx::Bitmap> card_back_inverted();
|
NonnullRefPtr<Gfx::Bitmap> card_back_inverted();
|
||||||
|
NonnullRefPtr<Gfx::Bitmap> card_front_highlighted(Suit, Rank);
|
||||||
|
|
||||||
void set_background_image_path(DeprecatedString path);
|
void set_background_image_path(DeprecatedString path);
|
||||||
|
void set_background_color(Color);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CardPainter();
|
CardPainter();
|
||||||
|
@ -29,13 +32,16 @@ private:
|
||||||
void paint_card_front(Gfx::Bitmap&, Suit, Rank);
|
void paint_card_front(Gfx::Bitmap&, Suit, Rank);
|
||||||
void paint_card_back(Gfx::Bitmap&);
|
void paint_card_back(Gfx::Bitmap&);
|
||||||
void paint_inverted_card(Gfx::Bitmap& bitmap, Gfx::Bitmap const& source_to_invert);
|
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);
|
||||||
|
|
||||||
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;
|
||||||
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_inverted;
|
||||||
|
Array<Array<RefPtr<Gfx::Bitmap>, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)> m_cards_highlighted;
|
||||||
RefPtr<Gfx::Bitmap> m_card_back;
|
RefPtr<Gfx::Bitmap> m_card_back;
|
||||||
RefPtr<Gfx::Bitmap> m_card_back_inverted;
|
RefPtr<Gfx::Bitmap> m_card_back_inverted;
|
||||||
|
|
||||||
DeprecatedString m_background_image_path;
|
DeprecatedString m_background_image_path;
|
||||||
|
Color m_background_color;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue