2020-03-09 18:09:22 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-09 18:09:22 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Card.h"
|
2021-05-16 05:13:11 +03:00
|
|
|
#include <AK/Format.h>
|
2021-06-04 14:40:16 +03:00
|
|
|
#include <AK/RefCounted.h>
|
2020-03-09 18:09:22 +03:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
2021-05-20 14:01:14 +03:00
|
|
|
namespace Cards {
|
2021-05-05 16:42:59 +03:00
|
|
|
|
2021-06-04 14:40:16 +03:00
|
|
|
class CardStack final : public RefCounted<CardStack> {
|
2020-03-09 18:09:22 +03:00
|
|
|
public:
|
|
|
|
enum Type {
|
|
|
|
Invalid,
|
|
|
|
Stock,
|
|
|
|
Normal,
|
|
|
|
Waste,
|
2021-05-15 06:09:41 +03:00
|
|
|
Play,
|
2020-03-09 18:09:22 +03:00
|
|
|
Foundation
|
|
|
|
};
|
|
|
|
|
2021-06-16 03:29:08 +03:00
|
|
|
enum MovementRule {
|
|
|
|
Alternating,
|
|
|
|
Same,
|
|
|
|
Any,
|
|
|
|
};
|
|
|
|
|
2020-03-09 18:09:22 +03:00
|
|
|
CardStack();
|
2020-10-26 21:34:46 +03:00
|
|
|
CardStack(const Gfx::IntPoint& position, Type type);
|
2021-06-04 14:40:16 +03:00
|
|
|
CardStack(const Gfx::IntPoint& position, Type type, NonnullRefPtr<CardStack> associated_stack);
|
2020-03-09 18:09:22 +03:00
|
|
|
|
|
|
|
bool is_empty() const { return m_stack.is_empty(); }
|
|
|
|
bool is_focused() const { return m_focused; }
|
|
|
|
Type type() const { return m_type; }
|
2021-05-16 05:13:11 +03:00
|
|
|
const NonnullRefPtrVector<Card>& stack() const { return m_stack; }
|
2020-03-09 18:09:22 +03:00
|
|
|
size_t count() const { return m_stack.size(); }
|
|
|
|
const Card& peek() const { return m_stack.last(); }
|
|
|
|
Card& peek() { return m_stack.last(); }
|
2020-06-10 11:57:59 +03:00
|
|
|
const Gfx::IntRect& bounding_box() const { return m_bounding_box; }
|
2020-03-09 18:09:22 +03:00
|
|
|
|
|
|
|
void set_focused(bool focused) { m_focused = focused; }
|
|
|
|
|
|
|
|
void push(NonnullRefPtr<Card> card);
|
|
|
|
NonnullRefPtr<Card> pop();
|
2021-05-15 06:09:41 +03:00
|
|
|
void move_to_stack(CardStack&);
|
2020-03-09 18:09:22 +03:00
|
|
|
void rebound_cards();
|
|
|
|
|
2021-06-16 03:29:08 +03:00
|
|
|
bool is_allowed_to_push(const Card&, size_t stack_size = 1, MovementRule movement_rule = Alternating) const;
|
|
|
|
void add_all_grabbed_cards(const Gfx::IntPoint& click_location, NonnullRefPtrVector<Card>& grabbed, MovementRule movement_rule = Alternating);
|
2020-03-09 18:09:22 +03:00
|
|
|
void draw(GUI::Painter&, const Gfx::Color& background_color);
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
private:
|
2020-10-26 21:34:46 +03:00
|
|
|
struct StackRules {
|
|
|
|
uint8_t shift_x { 0 };
|
|
|
|
uint8_t shift_y { 0 };
|
|
|
|
uint8_t step { 1 };
|
|
|
|
uint8_t shift_y_upside_down { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr StackRules rules_for_type(Type stack_type)
|
|
|
|
{
|
|
|
|
switch (stack_type) {
|
|
|
|
case Foundation:
|
|
|
|
return { 2, 1, 4, 1 };
|
|
|
|
case Normal:
|
2021-05-04 20:02:54 +03:00
|
|
|
return { 0, 20, 1, 3 };
|
2020-10-26 21:34:46 +03:00
|
|
|
case Stock:
|
|
|
|
return { 2, 1, 8, 1 };
|
2021-05-15 06:09:41 +03:00
|
|
|
case Waste:
|
|
|
|
return { 0, 0, 1, 0 };
|
|
|
|
case Play:
|
|
|
|
return { 20, 0, 1, 0 };
|
2020-10-26 21:34:46 +03:00
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 18:09:22 +03:00
|
|
|
void calculate_bounding_box();
|
|
|
|
|
2021-06-04 14:40:16 +03:00
|
|
|
RefPtr<CardStack> m_associated_stack;
|
2020-03-09 18:09:22 +03:00
|
|
|
NonnullRefPtrVector<Card> m_stack;
|
2020-06-10 11:57:59 +03:00
|
|
|
Vector<Gfx::IntPoint> m_stack_positions;
|
|
|
|
Gfx::IntPoint m_position;
|
|
|
|
Gfx::IntRect m_bounding_box;
|
2020-03-09 18:09:22 +03:00
|
|
|
Type m_type { Invalid };
|
2020-10-26 21:34:46 +03:00
|
|
|
StackRules m_rules;
|
2020-03-09 18:09:22 +03:00
|
|
|
bool m_focused { false };
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect m_base;
|
2020-03-09 18:09:22 +03:00
|
|
|
};
|
2021-05-05 16:42:59 +03:00
|
|
|
|
|
|
|
}
|
2021-05-16 05:13:11 +03:00
|
|
|
|
|
|
|
template<>
|
2021-05-20 14:01:14 +03:00
|
|
|
struct AK::Formatter<Cards::CardStack> : Formatter<FormatString> {
|
|
|
|
void format(FormatBuilder& builder, const Cards::CardStack& stack)
|
2021-05-16 05:13:11 +03:00
|
|
|
{
|
|
|
|
StringView type;
|
|
|
|
|
|
|
|
switch (stack.type()) {
|
2021-05-20 14:01:14 +03:00
|
|
|
case Cards::CardStack::Type::Stock:
|
2021-05-16 05:13:11 +03:00
|
|
|
type = "Stock"sv;
|
|
|
|
break;
|
2021-05-20 14:01:14 +03:00
|
|
|
case Cards::CardStack::Type::Normal:
|
2021-05-16 05:13:11 +03:00
|
|
|
type = "Normal"sv;
|
|
|
|
break;
|
2021-05-20 14:01:14 +03:00
|
|
|
case Cards::CardStack::Type::Foundation:
|
2021-05-16 05:13:11 +03:00
|
|
|
type = "Foundation"sv;
|
|
|
|
break;
|
2021-05-20 14:01:14 +03:00
|
|
|
case Cards::CardStack::Type::Waste:
|
2021-05-16 05:13:11 +03:00
|
|
|
type = "Waste"sv;
|
|
|
|
break;
|
2021-05-20 14:01:14 +03:00
|
|
|
case Cards::CardStack::Type::Play:
|
2021-05-16 05:13:11 +03:00
|
|
|
type = "Play"sv;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder cards;
|
|
|
|
bool first_card = true;
|
|
|
|
|
|
|
|
for (const auto& card : stack.stack()) {
|
|
|
|
cards.appendff("{}{}", (first_card ? "" : " "), card);
|
|
|
|
first_card = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Formatter<FormatString>::format(builder, "{:<10} {:>16}: {}", type, stack.bounding_box(), cards.build());
|
|
|
|
}
|
|
|
|
};
|