Card.h 3.0 KB

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