mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 18:57:36 +00:00
LibCards+Solitaire: Elevate card highlight management to the card stack
Instead of indicating which individual cards should be highlighted, card games now indicate which stack is highlighted. This lets the stack draw empty stacks with a highlight (e.g. the Foundation stack in Solitaire). If the stack is non-empty, the stack can delegate highlighting to the top-most card.
This commit is contained in:
parent
8d8fcd0d64
commit
4cbdc747ab
6 changed files with 41 additions and 28 deletions
|
@ -20,13 +20,13 @@ Card::Card(Suit suit, Rank rank)
|
|||
VERIFY(to_underlying(rank) < card_count);
|
||||
}
|
||||
|
||||
void Card::paint(GUI::Painter& painter) const
|
||||
void Card::paint(GUI::Painter& painter, bool highlighted) const
|
||||
{
|
||||
auto& card_painter = CardPainter::the();
|
||||
auto bitmap = [&]() {
|
||||
if (m_inverted)
|
||||
return m_upside_down ? card_painter.card_back_inverted() : card_painter.card_front_inverted(m_suit, m_rank);
|
||||
if (m_highlighted) {
|
||||
if (highlighted) {
|
||||
VERIFY(!m_upside_down);
|
||||
return card_painter.card_front_highlighted(m_suit, m_rank);
|
||||
}
|
||||
|
@ -46,12 +46,12 @@ void Card::save_old_position()
|
|||
m_old_position_valid = true;
|
||||
}
|
||||
|
||||
void Card::clear_and_paint(GUI::Painter& painter, Color background_color)
|
||||
void Card::clear_and_paint(GUI::Painter& painter, Color background_color, bool highlighted)
|
||||
{
|
||||
if (is_old_position_valid())
|
||||
clear(painter, background_color);
|
||||
|
||||
paint(painter);
|
||||
paint(painter, highlighted);
|
||||
save_old_position();
|
||||
}
|
||||
|
||||
|
|
|
@ -104,13 +104,12 @@ public:
|
|||
void set_moving(bool moving) { m_moving = moving; }
|
||||
void set_upside_down(bool upside_down) { m_upside_down = upside_down; }
|
||||
void set_inverted(bool inverted) { m_inverted = inverted; }
|
||||
void set_highlighted(bool highlighted) { m_highlighted = highlighted; }
|
||||
|
||||
void save_old_position();
|
||||
|
||||
void paint(GUI::Painter&) const;
|
||||
void paint(GUI::Painter&, bool highlighted = false) const;
|
||||
void clear(GUI::Painter&, Color background_color) const;
|
||||
void clear_and_paint(GUI::Painter& painter, Color background_color);
|
||||
void clear_and_paint(GUI::Painter& painter, Color background_color, bool highlighted);
|
||||
|
||||
private:
|
||||
Card(Suit, Rank);
|
||||
|
@ -123,7 +122,6 @@ private:
|
|||
bool m_moving { false };
|
||||
bool m_upside_down { false };
|
||||
bool m_inverted { false };
|
||||
bool m_highlighted { false };
|
||||
};
|
||||
|
||||
enum class Shuffle {
|
||||
|
|
|
@ -43,8 +43,18 @@ void CardStack::paint(GUI::Painter& painter, Gfx::Color background_color)
|
|||
return false;
|
||||
if (!is_empty() && (m_stack.size() != number_of_moving_cards))
|
||||
return false;
|
||||
painter.fill_rect_with_rounded_corners(m_base, background_color.darkened(0.5), Card::card_radius);
|
||||
painter.fill_rect_with_rounded_corners(m_base.shrunken(2, 2), background_color, Card::card_radius - 1);
|
||||
|
||||
auto paint_rect = m_base;
|
||||
painter.fill_rect_with_rounded_corners(paint_rect, background_color.darkened(0.5), Card::card_radius);
|
||||
paint_rect.shrink(2, 2);
|
||||
|
||||
if (m_highlighted) {
|
||||
auto background_complement = background_color.xored(Color::White);
|
||||
painter.fill_rect_with_rounded_corners(paint_rect, background_complement, Card::card_radius - 1);
|
||||
paint_rect.shrink(4, 4);
|
||||
}
|
||||
|
||||
painter.fill_rect_with_rounded_corners(paint_rect, background_color, Card::card_radius - 1);
|
||||
return true;
|
||||
};
|
||||
|
||||
|
@ -83,9 +93,11 @@ void CardStack::paint(GUI::Painter& painter, Gfx::Color background_color)
|
|||
return;
|
||||
}
|
||||
|
||||
for (auto& card : m_stack) {
|
||||
if (!card.is_moving())
|
||||
card.clear_and_paint(painter, Gfx::Color::Transparent);
|
||||
for (size_t i = 0; i < m_stack.size(); ++i) {
|
||||
if (auto& card = m_stack[i]; !card.is_moving()) {
|
||||
auto highlighted = m_highlighted && (i == m_stack.size() - 1);
|
||||
card.clear_and_paint(painter, Gfx::Color::Transparent, highlighted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -53,6 +53,8 @@ public:
|
|||
void paint(GUI::Painter&, Gfx::Color background_color);
|
||||
void clear();
|
||||
|
||||
void set_highlighted(bool highlighted) { m_highlighted = highlighted; }
|
||||
|
||||
private:
|
||||
struct StackRules {
|
||||
uint8_t shift_x { 0 };
|
||||
|
@ -92,6 +94,7 @@ private:
|
|||
Type m_type { Type::Invalid };
|
||||
StackRules m_rules;
|
||||
Gfx::IntRect m_base;
|
||||
bool m_highlighted { false };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue