CardStack.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2020, Till Mayer <till.mayer@web.de>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "Card.h"
  8. #include <AK/Format.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/Vector.h>
  11. namespace Cards {
  12. class CardStack final : public RefCounted<CardStack> {
  13. public:
  14. enum class Type {
  15. Invalid,
  16. Stock,
  17. Normal,
  18. Waste,
  19. Play,
  20. Foundation
  21. };
  22. enum class MovementRule {
  23. Alternating,
  24. Same,
  25. Any,
  26. };
  27. CardStack();
  28. CardStack(Gfx::IntPoint const& position, Type type, RefPtr<CardStack> covered_stack = nullptr);
  29. bool is_empty() const { return m_stack.is_empty(); }
  30. bool is_focused() const { return m_focused; }
  31. Type type() const { return m_type; }
  32. NonnullRefPtrVector<Card> const& stack() const { return m_stack; }
  33. size_t count() const { return m_stack.size(); }
  34. Card const& peek() const { return m_stack.last(); }
  35. Card& peek() { return m_stack.last(); }
  36. Gfx::IntRect const& bounding_box() const { return m_bounding_box; }
  37. void set_focused(bool focused) { m_focused = focused; }
  38. bool make_top_card_visible(); // Returns true if the card was flipped.
  39. void push(NonnullRefPtr<Card> card);
  40. NonnullRefPtr<Card> pop();
  41. void move_to_stack(CardStack&);
  42. void rebound_cards();
  43. bool is_allowed_to_push(Card const&, size_t stack_size = 1, MovementRule movement_rule = MovementRule::Alternating) const;
  44. void add_all_grabbed_cards(Gfx::IntPoint const& click_location, NonnullRefPtrVector<Card>& grabbed, MovementRule movement_rule = MovementRule::Alternating);
  45. void paint(GUI::Painter&, Gfx::Color const& background_color);
  46. void clear();
  47. private:
  48. struct StackRules {
  49. uint8_t shift_x { 0 };
  50. uint8_t shift_y { 0 };
  51. uint8_t step { 1 };
  52. uint8_t shift_y_upside_down { 0 };
  53. };
  54. static constexpr StackRules rules_for_type(Type stack_type)
  55. {
  56. switch (stack_type) {
  57. case Type::Foundation:
  58. return { 2, 1, 4, 1 };
  59. case Type::Normal:
  60. return { 0, 20, 1, 3 };
  61. case Type::Stock:
  62. return { 2, 1, 8, 1 };
  63. case Type::Waste:
  64. return { 0, 0, 1, 0 };
  65. case Type::Play:
  66. return { 20, 0, 1, 0 };
  67. default:
  68. return {};
  69. }
  70. }
  71. void calculate_bounding_box();
  72. // An optional stack that this stack is painted on top of.
  73. // eg, in Solitaire the Play stack is positioned over the Waste stack.
  74. RefPtr<CardStack> m_covered_stack;
  75. NonnullRefPtrVector<Card> m_stack;
  76. Vector<Gfx::IntPoint> m_stack_positions;
  77. Gfx::IntPoint m_position;
  78. Gfx::IntRect m_bounding_box;
  79. Type m_type { Type::Invalid };
  80. StackRules m_rules;
  81. bool m_focused { false };
  82. Gfx::IntRect m_base;
  83. };
  84. }
  85. template<>
  86. struct AK::Formatter<Cards::CardStack> : Formatter<FormatString> {
  87. ErrorOr<void> format(FormatBuilder& builder, Cards::CardStack const& stack)
  88. {
  89. StringView type;
  90. switch (stack.type()) {
  91. case Cards::CardStack::Type::Stock:
  92. type = "Stock"sv;
  93. break;
  94. case Cards::CardStack::Type::Normal:
  95. type = "Normal"sv;
  96. break;
  97. case Cards::CardStack::Type::Foundation:
  98. type = "Foundation"sv;
  99. break;
  100. case Cards::CardStack::Type::Waste:
  101. type = "Waste"sv;
  102. break;
  103. case Cards::CardStack::Type::Play:
  104. type = "Play"sv;
  105. break;
  106. default:
  107. VERIFY_NOT_REACHED();
  108. }
  109. StringBuilder cards;
  110. bool first_card = true;
  111. for (auto const& card : stack.stack()) {
  112. cards.appendff("{}{}", (first_card ? "" : " "), card);
  113. first_card = false;
  114. }
  115. return Formatter<FormatString>::format(builder, "{:<10} {:>16}: {}"sv, type, stack.bounding_box(), cards.build());
  116. }
  117. };