Card.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2020, Till Mayer <till.mayer@web.de>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Array.h>
  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. #include <ctype.h>
  16. namespace Cards {
  17. class Card final : public Core::Object {
  18. C_OBJECT(Card)
  19. public:
  20. static constexpr int width = 80;
  21. static constexpr int height = 100;
  22. static constexpr int card_count = 13;
  23. static constexpr int card_radius = 5;
  24. static constexpr Array<StringView, card_count> labels = {
  25. "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
  26. };
  27. enum class Suit {
  28. Clubs,
  29. Diamonds,
  30. Spades,
  31. Hearts,
  32. __Count
  33. };
  34. virtual ~Card() override = default;
  35. Gfx::IntRect& rect() { return m_rect; }
  36. Gfx::IntPoint position() const { return m_rect.location(); }
  37. const Gfx::IntPoint& old_position() const { return m_old_position; }
  38. uint8_t value() const { return m_value; };
  39. Suit suit() const { return m_suit; }
  40. bool is_old_position_valid() const { return m_old_position_valid; }
  41. bool is_moving() const { return m_moving; }
  42. bool is_upside_down() const { return m_upside_down; }
  43. bool is_inverted() const { return m_inverted; }
  44. Gfx::Color color() const { return (m_suit == Suit::Diamonds || m_suit == Suit::Hearts) ? Color::Red : Color::Black; }
  45. void set_position(const Gfx::IntPoint p) { m_rect.set_location(p); }
  46. void set_moving(bool moving) { m_moving = moving; }
  47. void set_upside_down(bool upside_down) { m_upside_down = upside_down; }
  48. void set_inverted(bool inverted) { m_inverted = inverted; }
  49. void save_old_position();
  50. void draw(GUI::Painter&) const;
  51. void clear(GUI::Painter&, const Color& background_color) const;
  52. void clear_and_draw(GUI::Painter&, const Color& background_color);
  53. private:
  54. Card(Suit suit, uint8_t value);
  55. static NonnullRefPtr<Gfx::Bitmap> invert_bitmap(Gfx::Bitmap&);
  56. Gfx::IntRect m_rect;
  57. NonnullRefPtr<Gfx::Bitmap> m_front;
  58. RefPtr<Gfx::Bitmap> m_front_inverted;
  59. Gfx::IntPoint m_old_position;
  60. Suit m_suit;
  61. uint8_t m_value;
  62. bool m_old_position_valid { false };
  63. bool m_moving { false };
  64. bool m_upside_down { false };
  65. bool m_inverted { false };
  66. };
  67. }
  68. template<>
  69. struct AK::Formatter<Cards::Card> : Formatter<FormatString> {
  70. ErrorOr<void> format(FormatBuilder& builder, Cards::Card const& card)
  71. {
  72. StringView suit;
  73. switch (card.suit()) {
  74. case Cards::Card::Suit::Clubs:
  75. suit = "C"sv;
  76. break;
  77. case Cards::Card::Suit::Diamonds:
  78. suit = "D"sv;
  79. break;
  80. case Cards::Card::Suit::Hearts:
  81. suit = "H"sv;
  82. break;
  83. case Cards::Card::Suit::Spades:
  84. suit = "S"sv;
  85. break;
  86. default:
  87. VERIFY_NOT_REACHED();
  88. }
  89. return Formatter<FormatString>::format(builder, "{:>2}{}", Cards::Card::labels[card.value()], suit);
  90. }
  91. };