1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:07:45 +00:00

Libraries: Change enums to enum classes in LibCards

This commit is contained in:
Lenny Maiorani 2022-03-17 15:01:03 -06:00 committed by Andreas Kling
parent d3893a73fb
commit 56046d3f9b
6 changed files with 43 additions and 43 deletions

View file

@ -90,7 +90,7 @@ void Game::timer_event(Core::TimerEvent&)
void Game::create_new_animation_card() void Game::create_new_animation_card()
{ {
auto card = Card::construct(static_cast<Card::Type>(get_random_uniform(Card::Type::__Count)), get_random_uniform(Card::card_count)); auto card = Card::construct(static_cast<Card::Type>(get_random_uniform(to_underlying(Card::Type::__Count))), get_random_uniform(Card::card_count));
card->set_position({ get_random_uniform(Game::width - Card::width), get_random_uniform(Game::height / 8) }); card->set_position({ get_random_uniform(Game::width - Card::width), get_random_uniform(Game::height / 8) });
int x_sgn = card->position().x() > (Game::width / 2) ? -1 : 1; int x_sgn = card->position().x() > (Game::width / 2) ? -1 : 1;

View file

@ -260,7 +260,7 @@ void Game::mousedown_event(GUI::MouseEvent& event)
update(top_card.rect()); update(top_card.rect());
} }
} else if (m_focused_cards.is_empty()) { } else if (m_focused_cards.is_empty()) {
to_check.add_all_grabbed_cards(click_location, m_focused_cards, Cards::CardStack::Same); to_check.add_all_grabbed_cards(click_location, m_focused_cards, Cards::CardStack::MovementRule::Same);
m_mouse_down_location = click_location; m_mouse_down_location = click_location;
if (m_focused_stack) if (m_focused_stack)
m_focused_stack->set_focused(false); m_focused_stack->set_focused(false);
@ -310,7 +310,7 @@ void Game::mouseup_event(GUI::MouseEvent& event)
if (stack.is_focused()) if (stack.is_focused())
continue; continue;
if (stack.is_allowed_to_push(m_focused_cards.at(0), m_focused_cards.size(), Cards::CardStack::Any) && !stack.is_empty()) { if (stack.is_allowed_to_push(m_focused_cards.at(0), m_focused_cards.size(), Cards::CardStack::MovementRule::Any) && !stack.is_empty()) {
move_focused_cards(stack); move_focused_cards(stack);
rebound = false; rebound = false;
@ -324,7 +324,7 @@ void Game::mouseup_event(GUI::MouseEvent& event)
for (auto& focused_card : m_focused_cards) { for (auto& focused_card : m_focused_cards) {
if (stack.bounding_box().intersects(focused_card.rect())) { if (stack.bounding_box().intersects(focused_card.rect())) {
if (stack.is_allowed_to_push(m_focused_cards.at(0), m_focused_cards.size(), Cards::CardStack::Any)) { if (stack.is_allowed_to_push(m_focused_cards.at(0), m_focused_cards.size(), Cards::CardStack::MovementRule::Any)) {
move_focused_cards(stack); move_focused_cards(stack);
rebound = false; rebound = false;

View file

@ -112,14 +112,14 @@ Card::Card(Type type, uint8_t value)
auto const& symbol = [&]() -> Gfx::CharacterBitmap const& { auto const& symbol = [&]() -> Gfx::CharacterBitmap const& {
switch (m_type) { switch (m_type) {
case Diamonds: case Type::Diamonds:
return s_diamond; return s_diamond;
case Clubs: case Type::Clubs:
return s_club; return s_club;
break; break;
case Spades: case Type::Spades:
return s_spade; return s_spade;
case Hearts: case Type::Hearts:
return s_heart; return s_heart;
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();

View file

@ -29,7 +29,7 @@ public:
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
}; };
enum Type { enum class Type {
Clubs, Clubs,
Diamonds, Diamonds,
Spades, Spades,
@ -49,7 +49,7 @@ public:
bool is_moving() const { return m_moving; } bool is_moving() const { return m_moving; }
bool is_upside_down() const { return m_upside_down; } bool is_upside_down() const { return m_upside_down; }
bool is_inverted() const { return m_inverted; } bool is_inverted() const { return m_inverted; }
Gfx::Color color() const { return (m_type == Diamonds || m_type == Hearts) ? Color::Red : Color::Black; } Gfx::Color color() const { return (m_type == Type::Diamonds || m_type == Type::Hearts) ? Color::Red : Color::Black; }
void set_position(const Gfx::IntPoint p) { m_rect.set_location(p); } void set_position(const Gfx::IntPoint p) { m_rect.set_location(p); }
void set_moving(bool moving) { m_moving = moving; } void set_moving(bool moving) { m_moving = moving; }

View file

@ -10,7 +10,7 @@ namespace Cards {
CardStack::CardStack() CardStack::CardStack()
: m_position({ 0, 0 }) : m_position({ 0, 0 })
, m_type(Invalid) , m_type(Type::Invalid)
, m_base(m_position, { Card::width, Card::height }) , m_base(m_position, { Card::width, Card::height })
{ {
} }
@ -21,7 +21,7 @@ CardStack::CardStack(const Gfx::IntPoint& position, Type type)
, m_rules(rules_for_type(type)) , m_rules(rules_for_type(type))
, m_base(m_position, { Card::width, Card::height }) , m_base(m_position, { Card::width, Card::height })
{ {
VERIFY(type != Invalid); VERIFY(type != Type::Invalid);
calculate_bounding_box(); calculate_bounding_box();
} }
@ -32,7 +32,7 @@ CardStack::CardStack(const Gfx::IntPoint& position, Type type, NonnullRefPtr<Car
, m_rules(rules_for_type(type)) , m_rules(rules_for_type(type))
, m_base(m_position, { Card::width, Card::height }) , m_base(m_position, { Card::width, Card::height })
{ {
VERIFY(type != Invalid); VERIFY(type != Type::Invalid);
calculate_bounding_box(); calculate_bounding_box();
} }
@ -59,13 +59,13 @@ void CardStack::draw(GUI::Painter& painter, const Gfx::Color& background_color)
}; };
switch (m_type) { switch (m_type) {
case Stock: case Type::Stock:
if (draw_background_if_empty()) { if (draw_background_if_empty()) {
painter.fill_rect(m_base.shrunken(Card::width / 4, Card::height / 4), background_color.lightened(1.5)); painter.fill_rect(m_base.shrunken(Card::width / 4, Card::height / 4), background_color.lightened(1.5));
painter.fill_rect(m_base.shrunken(Card::width / 2, Card::height / 2), background_color); painter.fill_rect(m_base.shrunken(Card::width / 2, Card::height / 2), background_color);
} }
break; break;
case Foundation: case Type::Foundation:
if (draw_background_if_empty()) { if (draw_background_if_empty()) {
for (int y = 0; y < (m_base.height() - 4) / 8; ++y) { for (int y = 0; y < (m_base.height() - 4) / 8; ++y) {
for (int x = 0; x < (m_base.width() - 4) / 5; ++x) { for (int x = 0; x < (m_base.width() - 4) / 5; ++x) {
@ -74,11 +74,11 @@ void CardStack::draw(GUI::Painter& painter, const Gfx::Color& background_color)
} }
} }
break; break;
case Play: case Type::Play:
case Normal: case Type::Normal:
draw_background_if_empty(); draw_background_if_empty();
break; break;
case Waste: case Type::Waste:
break; break;
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
@ -112,7 +112,7 @@ void CardStack::add_all_grabbed_cards(const Gfx::IntPoint& click_location, Nonnu
{ {
VERIFY(grabbed.is_empty()); VERIFY(grabbed.is_empty());
if (m_type != Normal) { if (m_type != Type::Normal) {
auto& top_card = peek(); auto& top_card = peek();
if (top_card.rect().contains(click_location)) { if (top_card.rect().contains(click_location)) {
top_card.set_moving(true); top_card.set_moving(true);
@ -159,13 +159,13 @@ void CardStack::add_all_grabbed_cards(const Gfx::IntPoint& click_location, Nonnu
if (i != 0) { if (i != 0) {
bool color_match; bool color_match;
switch (movement_rule) { switch (movement_rule) {
case Alternating: case MovementRule::Alternating:
color_match = card.color() != last_color; color_match = card.color() != last_color;
break; break;
case Same: case MovementRule::Same:
color_match = card.color() == last_color; color_match = card.color() == last_color;
break; break;
case Any: case MovementRule::Any:
color_match = true; color_match = true;
break; break;
} }
@ -189,18 +189,18 @@ void CardStack::add_all_grabbed_cards(const Gfx::IntPoint& click_location, Nonnu
bool CardStack::is_allowed_to_push(const Card& card, size_t stack_size, MovementRule movement_rule) const bool CardStack::is_allowed_to_push(const Card& card, size_t stack_size, MovementRule movement_rule) const
{ {
if (m_type == Stock || m_type == Waste || m_type == Play) if (m_type == Type::Stock || m_type == Type::Waste || m_type == Type::Play)
return false; return false;
if (m_type == Normal && is_empty()) { if (m_type == Type::Normal && is_empty()) {
// FIXME: proper solution for this // FIXME: proper solution for this
if (movement_rule == Alternating) { if (movement_rule == MovementRule::Alternating) {
return card.value() == 12; return card.value() == 12;
} }
return true; return true;
} }
if (m_type == Foundation && is_empty()) if (m_type == Type::Foundation && is_empty())
return card.value() == 0; return card.value() == 0;
if (!is_empty()) { if (!is_empty()) {
@ -208,21 +208,21 @@ bool CardStack::is_allowed_to_push(const Card& card, size_t stack_size, Movement
if (top_card.is_upside_down()) if (top_card.is_upside_down())
return false; return false;
if (m_type == Foundation) { if (m_type == Type::Foundation) {
// Prevent player from dragging an entire stack of cards to the foundation stack // Prevent player from dragging an entire stack of cards to the foundation stack
if (stack_size > 1) if (stack_size > 1)
return false; return false;
return top_card.type() == card.type() && m_stack.size() == card.value(); return top_card.type() == card.type() && m_stack.size() == card.value();
} else if (m_type == Normal) { } else if (m_type == Type::Normal) {
bool color_match; bool color_match;
switch (movement_rule) { switch (movement_rule) {
case Alternating: case MovementRule::Alternating:
color_match = card.color() != top_card.color(); color_match = card.color() != top_card.color();
break; break;
case Same: case MovementRule::Same:
color_match = card.color() == top_card.color(); color_match = card.color() == top_card.color();
break; break;
case Any: case MovementRule::Any:
color_match = true; color_match = true;
break; break;
} }
@ -248,7 +248,7 @@ void CardStack::push(NonnullRefPtr<Card> card)
top_most_position.translate_by(m_rules.shift_x, m_rules.shift_y); top_most_position.translate_by(m_rules.shift_x, m_rules.shift_y);
} }
if (m_type == Stock) if (m_type == Type::Stock)
card->set_upside_down(true); card->set_upside_down(true);
card->set_position(top_most_position); card->set_position(top_most_position);
@ -263,7 +263,7 @@ NonnullRefPtr<Card> CardStack::pop()
auto card = m_stack.take_last(); auto card = m_stack.take_last();
calculate_bounding_box(); calculate_bounding_box();
if (m_type == Stock) if (m_type == Type::Stock)
card->set_upside_down(false); card->set_upside_down(false);
m_stack_positions.take_last(); m_stack_positions.take_last();

View file

@ -15,7 +15,7 @@ namespace Cards {
class CardStack final : public RefCounted<CardStack> { class CardStack final : public RefCounted<CardStack> {
public: public:
enum Type { enum class Type {
Invalid, Invalid,
Stock, Stock,
Normal, Normal,
@ -24,7 +24,7 @@ public:
Foundation Foundation
}; };
enum MovementRule { enum class MovementRule {
Alternating, Alternating,
Same, Same,
Any, Any,
@ -50,8 +50,8 @@ public:
void move_to_stack(CardStack&); void move_to_stack(CardStack&);
void rebound_cards(); void rebound_cards();
bool is_allowed_to_push(const Card&, size_t stack_size = 1, MovementRule movement_rule = Alternating) const; bool is_allowed_to_push(const Card&, size_t stack_size = 1, MovementRule movement_rule = MovementRule::Alternating) const;
void add_all_grabbed_cards(const Gfx::IntPoint& click_location, NonnullRefPtrVector<Card>& grabbed, MovementRule movement_rule = Alternating); void add_all_grabbed_cards(const Gfx::IntPoint& click_location, NonnullRefPtrVector<Card>& grabbed, MovementRule movement_rule = MovementRule::Alternating);
void draw(GUI::Painter&, const Gfx::Color& background_color); void draw(GUI::Painter&, const Gfx::Color& background_color);
void clear(); void clear();
@ -66,15 +66,15 @@ private:
constexpr StackRules rules_for_type(Type stack_type) constexpr StackRules rules_for_type(Type stack_type)
{ {
switch (stack_type) { switch (stack_type) {
case Foundation: case Type::Foundation:
return { 2, 1, 4, 1 }; return { 2, 1, 4, 1 };
case Normal: case Type::Normal:
return { 0, 20, 1, 3 }; return { 0, 20, 1, 3 };
case Stock: case Type::Stock:
return { 2, 1, 8, 1 }; return { 2, 1, 8, 1 };
case Waste: case Type::Waste:
return { 0, 0, 1, 0 }; return { 0, 0, 1, 0 };
case Play: case Type::Play:
return { 20, 0, 1, 0 }; return { 20, 0, 1, 0 };
default: default:
return {}; return {};
@ -88,7 +88,7 @@ private:
Vector<Gfx::IntPoint> m_stack_positions; Vector<Gfx::IntPoint> m_stack_positions;
Gfx::IntPoint m_position; Gfx::IntPoint m_position;
Gfx::IntRect m_bounding_box; Gfx::IntRect m_bounding_box;
Type m_type { Invalid }; Type m_type { Type::Invalid };
StackRules m_rules; StackRules m_rules;
bool m_focused { false }; bool m_focused { false };
Gfx::IntRect m_base; Gfx::IntRect m_base;