Card.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2020, Till Mayer <till.mayer@web.de>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Format.h>
  10. #include <LibCore/Object.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGfx/Bitmap.h>
  13. #include <LibGfx/CharacterBitmap.h>
  14. #include <LibGfx/Rect.h>
  15. namespace Cards {
  16. enum class Rank : u8 {
  17. Ace,
  18. Two,
  19. Three,
  20. Four,
  21. Five,
  22. Six,
  23. Seven,
  24. Eight,
  25. Nine,
  26. Ten,
  27. Jack,
  28. Queen,
  29. King,
  30. __Count
  31. };
  32. constexpr StringView card_rank_label(Rank rank)
  33. {
  34. switch (rank) {
  35. case Rank::Ace:
  36. return "A"sv;
  37. case Rank::Two:
  38. return "2"sv;
  39. case Rank::Three:
  40. return "3"sv;
  41. case Rank::Four:
  42. return "4"sv;
  43. case Rank::Five:
  44. return "5"sv;
  45. case Rank::Six:
  46. return "6"sv;
  47. case Rank::Seven:
  48. return "7"sv;
  49. case Rank::Eight:
  50. return "8"sv;
  51. case Rank::Nine:
  52. return "9"sv;
  53. case Rank::Ten:
  54. return "10"sv;
  55. case Rank::Jack:
  56. return "J"sv;
  57. case Rank::Queen:
  58. return "Q"sv;
  59. case Rank::King:
  60. return "K"sv;
  61. case Rank::__Count:
  62. VERIFY_NOT_REACHED();
  63. }
  64. VERIFY_NOT_REACHED();
  65. }
  66. enum class Suit : u8 {
  67. Clubs,
  68. Diamonds,
  69. Spades,
  70. Hearts,
  71. __Count
  72. };
  73. class Card final : public Core::Object {
  74. C_OBJECT(Card)
  75. public:
  76. static constexpr int width = 80;
  77. static constexpr int height = 100;
  78. static constexpr int card_count = to_underlying(Rank::__Count);
  79. static constexpr int card_radius = 5;
  80. virtual ~Card() override = default;
  81. Gfx::IntRect& rect() { return m_rect; }
  82. Gfx::IntRect const& rect() const { return m_rect; }
  83. Gfx::IntPoint position() const { return m_rect.location(); }
  84. Gfx::IntPoint old_position() const { return m_old_position; }
  85. Rank rank() const { return m_rank; };
  86. Suit suit() const { return m_suit; }
  87. bool is_old_position_valid() const { return m_old_position_valid; }
  88. bool is_moving() const { return m_moving; }
  89. bool is_upside_down() const { return m_upside_down; }
  90. bool is_inverted() const { return m_inverted; }
  91. bool is_previewed() const { return m_previewed; }
  92. Gfx::Color color() const { return (m_suit == Suit::Diamonds || m_suit == Suit::Hearts) ? Color::Red : Color::Black; }
  93. void set_position(const Gfx::IntPoint p) { m_rect.set_location(p); }
  94. void set_moving(bool moving) { m_moving = moving; }
  95. void set_upside_down(bool upside_down) { m_upside_down = upside_down; }
  96. void set_inverted(bool inverted) { m_inverted = inverted; }
  97. void set_previewed(bool previewed) { m_previewed = previewed; }
  98. void save_old_position();
  99. void paint(GUI::Painter&, bool highlighted = false) const;
  100. void clear(GUI::Painter&, Color background_color) const;
  101. void clear_and_paint(GUI::Painter& painter, Color background_color, bool highlighted);
  102. private:
  103. Card(Suit, Rank);
  104. Gfx::IntRect m_rect;
  105. Gfx::IntPoint m_old_position;
  106. Suit m_suit;
  107. Rank m_rank;
  108. bool m_old_position_valid { false };
  109. bool m_moving { false };
  110. bool m_upside_down { false };
  111. bool m_inverted { false };
  112. bool m_previewed { false };
  113. };
  114. enum class Shuffle {
  115. No,
  116. Yes,
  117. };
  118. ErrorOr<Vector<NonnullRefPtr<Card>>> create_standard_deck(Shuffle);
  119. ErrorOr<Vector<NonnullRefPtr<Card>>> create_deck(unsigned full_club_suit_count, unsigned full_diamond_suit_count, unsigned full_heart_suit_count, unsigned full_spade_suit_count, Shuffle);
  120. }
  121. template<>
  122. struct AK::Formatter<Cards::Card> : Formatter<FormatString> {
  123. ErrorOr<void> format(FormatBuilder& builder, Cards::Card const& card)
  124. {
  125. StringView suit;
  126. switch (card.suit()) {
  127. case Cards::Suit::Clubs:
  128. suit = "C"sv;
  129. break;
  130. case Cards::Suit::Diamonds:
  131. suit = "D"sv;
  132. break;
  133. case Cards::Suit::Hearts:
  134. suit = "H"sv;
  135. break;
  136. case Cards::Suit::Spades:
  137. suit = "S"sv;
  138. break;
  139. default:
  140. VERIFY_NOT_REACHED();
  141. }
  142. return Formatter<FormatString>::format(builder, "{:>2}{}"sv, Cards::card_rank_label(card.rank()), suit);
  143. }
  144. };