Card.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 Array<StringView, card_count> labels = {
  23. "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
  24. };
  25. enum Type {
  26. Clubs,
  27. Diamonds,
  28. Spades,
  29. Hearts,
  30. __Count
  31. };
  32. virtual ~Card() override;
  33. Gfx::IntRect& rect() { return m_rect; }
  34. Gfx::IntPoint position() const { return m_rect.location(); }
  35. const Gfx::IntPoint& old_position() const { return m_old_position; }
  36. uint8_t value() const { return m_value; };
  37. Type type() const { return m_type; }
  38. bool is_old_position_valid() const { return m_old_position_valid; }
  39. bool is_moving() const { return m_moving; }
  40. bool is_upside_down() const { return m_upside_down; }
  41. Gfx::Color color() const { return (m_type == Diamonds || m_type == Hearts) ? Color::Red : Color::Black; }
  42. void set_position(const Gfx::IntPoint p) { m_rect.set_location(p); }
  43. void set_moving(bool moving) { m_moving = moving; }
  44. void set_upside_down(bool upside_down) { m_upside_down = upside_down; }
  45. void save_old_position();
  46. void draw(GUI::Painter&) const;
  47. void clear(GUI::Painter&, const Color& background_color) const;
  48. void clear_and_draw(GUI::Painter&, const Color& background_color);
  49. private:
  50. Card(Type type, uint8_t value);
  51. Gfx::IntRect m_rect;
  52. NonnullRefPtr<Gfx::Bitmap> m_front;
  53. Gfx::IntPoint m_old_position;
  54. Type m_type;
  55. uint8_t m_value;
  56. bool m_old_position_valid { false };
  57. bool m_moving { false };
  58. bool m_upside_down { false };
  59. };
  60. }
  61. template<>
  62. struct AK::Formatter<Cards::Card> : Formatter<FormatString> {
  63. void format(FormatBuilder& builder, const Cards::Card& card)
  64. {
  65. StringView type;
  66. switch (card.type()) {
  67. case Cards::Card::Type::Clubs:
  68. type = "C"sv;
  69. break;
  70. case Cards::Card::Type::Diamonds:
  71. type = "D"sv;
  72. break;
  73. case Cards::Card::Type::Hearts:
  74. type = "H"sv;
  75. break;
  76. case Cards::Card::Type::Spades:
  77. type = "S"sv;
  78. break;
  79. default:
  80. VERIFY_NOT_REACHED();
  81. }
  82. Formatter<FormatString>::format(builder, "{:>2}{}", Cards::Card::labels[card.value()], type);
  83. }
  84. };