CardStack.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 position, Type type, RefPtr<CardStack> covered_stack = nullptr);
  29. bool is_empty() const { return m_stack.is_empty(); }
  30. Type type() const { return m_type; }
  31. Vector<NonnullRefPtr<Card>> const& stack() const { return m_stack; }
  32. size_t count() const { return m_stack.size(); }
  33. Card const& peek() const { return m_stack.last(); }
  34. Card& peek() { return m_stack.last(); }
  35. Gfx::IntRect const& bounding_box() const { return m_bounding_box; }
  36. bool make_top_card_visible(); // Returns true if the card was flipped.
  37. ErrorOr<void> push(NonnullRefPtr<Card>);
  38. NonnullRefPtr<Card> pop();
  39. ErrorOr<void> take_all(CardStack&);
  40. void rebound_cards();
  41. bool is_allowed_to_push(Card const&, size_t stack_size = 1, MovementRule movement_rule = MovementRule::Alternating) const;
  42. ErrorOr<void> add_all_grabbed_cards(Gfx::IntPoint click_location, Vector<NonnullRefPtr<Card>>& grabbed, MovementRule movement_rule = MovementRule::Alternating);
  43. bool preview_card(Gfx::IntPoint click_location);
  44. void clear_card_preview();
  45. void paint(GUI::Painter&, Gfx::Color background_color);
  46. void clear();
  47. void set_highlighted(bool highlighted) { m_highlighted = highlighted; }
  48. private:
  49. struct StackRules {
  50. uint8_t shift_x { 0 };
  51. uint8_t shift_y { 0 };
  52. uint8_t step { 1 };
  53. uint8_t shift_y_upside_down { 0 };
  54. };
  55. static constexpr StackRules rules_for_type(Type stack_type)
  56. {
  57. switch (stack_type) {
  58. case Type::Foundation:
  59. return { 2, 1, 4, 1 };
  60. case Type::Normal:
  61. return { 0, 20, 1, 3 };
  62. case Type::Stock:
  63. return { 2, 1, 8, 1 };
  64. case Type::Waste:
  65. return { 0, 0, 1, 0 };
  66. case Type::Play:
  67. return { 20, 0, 1, 0 };
  68. default:
  69. return {};
  70. }
  71. }
  72. void calculate_bounding_box();
  73. // An optional stack that this stack is painted on top of.
  74. // eg, in Solitaire the Play stack is positioned over the Waste stack.
  75. RefPtr<CardStack> m_covered_stack;
  76. Vector<NonnullRefPtr<Card>> m_stack;
  77. Vector<Gfx::IntPoint> m_stack_positions;
  78. Gfx::IntPoint m_position;
  79. Gfx::IntRect m_bounding_box;
  80. Type m_type { Type::Invalid };
  81. StackRules m_rules;
  82. Gfx::IntRect m_base;
  83. bool m_highlighted { false };
  84. };
  85. }
  86. template<>
  87. struct AK::Formatter<Cards::CardStack> : Formatter<FormatString> {
  88. ErrorOr<void> format(FormatBuilder& builder, Cards::CardStack const& stack)
  89. {
  90. StringView type;
  91. switch (stack.type()) {
  92. case Cards::CardStack::Type::Stock:
  93. type = "Stock"sv;
  94. break;
  95. case Cards::CardStack::Type::Normal:
  96. type = "Normal"sv;
  97. break;
  98. case Cards::CardStack::Type::Foundation:
  99. type = "Foundation"sv;
  100. break;
  101. case Cards::CardStack::Type::Waste:
  102. type = "Waste"sv;
  103. break;
  104. case Cards::CardStack::Type::Play:
  105. type = "Play"sv;
  106. break;
  107. default:
  108. VERIFY_NOT_REACHED();
  109. }
  110. StringBuilder cards;
  111. bool first_card = true;
  112. for (auto const& card : stack.stack()) {
  113. cards.appendff("{}{}", (first_card ? "" : " "), card);
  114. first_card = false;
  115. }
  116. return Formatter<FormatString>::format(builder, "{:<10} {:>16}: {}"sv, type, stack.bounding_box(), cards.to_deprecated_string());
  117. }
  118. };