Card.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2020, Till Mayer <till.mayer@web.de>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Card.h"
  7. #include <LibGUI/Widget.h>
  8. #include <LibGfx/Font.h>
  9. #include <LibGfx/FontDatabase.h>
  10. namespace Cards {
  11. static const NonnullRefPtr<Gfx::CharacterBitmap> s_diamond = Gfx::CharacterBitmap::create_from_ascii(
  12. " # "
  13. " ### "
  14. " ##### "
  15. " ####### "
  16. "#########"
  17. " ####### "
  18. " ##### "
  19. " ### "
  20. " # ",
  21. 9, 9);
  22. static const NonnullRefPtr<Gfx::CharacterBitmap> s_heart = Gfx::CharacterBitmap::create_from_ascii(
  23. " # # "
  24. " ### ### "
  25. "#########"
  26. "#########"
  27. "#########"
  28. " ####### "
  29. " ##### "
  30. " ### "
  31. " # ",
  32. 9, 9);
  33. static const NonnullRefPtr<Gfx::CharacterBitmap> s_spade = Gfx::CharacterBitmap::create_from_ascii(
  34. " # "
  35. " ### "
  36. " ##### "
  37. " ####### "
  38. "#########"
  39. "#########"
  40. " ## # ## "
  41. " ### "
  42. " ### ",
  43. 9, 9);
  44. static const NonnullRefPtr<Gfx::CharacterBitmap> s_club = Gfx::CharacterBitmap::create_from_ascii(
  45. " ### "
  46. " ##### "
  47. " ##### "
  48. " ## ### ## "
  49. "###########"
  50. "###########"
  51. "#### # ####"
  52. " ## ### ## "
  53. " ### ",
  54. 11, 9);
  55. static RefPtr<Gfx::Bitmap> s_background;
  56. Card::Card(Type type, uint8_t value)
  57. : m_rect(Gfx::IntRect({}, { width, height }))
  58. , m_front(*Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { width, height }))
  59. , m_type(type)
  60. , m_value(value)
  61. {
  62. VERIFY(value < card_count);
  63. Gfx::IntRect paint_rect({ 0, 0 }, { width, height });
  64. if (s_background.is_null()) {
  65. s_background = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { width, height });
  66. Gfx::Painter bg_painter(*s_background);
  67. s_background->fill(Color::White);
  68. auto image = Gfx::Bitmap::load_from_file("/res/icons/cards/buggie-deck.png");
  69. VERIFY(!image.is_null());
  70. float aspect_ratio = image->width() / static_cast<float>(image->height());
  71. auto target_size = Gfx::IntSize(static_cast<int>(aspect_ratio * (height - 5)), height - 5);
  72. bg_painter.draw_scaled_bitmap(
  73. { { (width - target_size.width()) / 2, (height - target_size.height()) / 2 }, target_size },
  74. *image, image->rect());
  75. bg_painter.draw_rect(paint_rect, Color::Black);
  76. }
  77. Gfx::Painter painter(m_front);
  78. auto& font = Gfx::FontDatabase::default_font().bold_variant();
  79. auto label = labels[value];
  80. m_front->fill(Color::White);
  81. painter.draw_rect(paint_rect, Color::Black);
  82. paint_rect.set_height(paint_rect.height() / 2);
  83. paint_rect.shrink(10, 6);
  84. painter.draw_text(paint_rect, label, font, Gfx::TextAlignment::TopLeft, color());
  85. NonnullRefPtr<Gfx::CharacterBitmap> symbol = s_diamond;
  86. switch (m_type) {
  87. case Diamonds:
  88. symbol = s_diamond;
  89. break;
  90. case Clubs:
  91. symbol = s_club;
  92. break;
  93. case Spades:
  94. symbol = s_spade;
  95. break;
  96. case Hearts:
  97. symbol = s_heart;
  98. break;
  99. default:
  100. VERIFY_NOT_REACHED();
  101. }
  102. painter.draw_bitmap(
  103. { paint_rect.x() + (font.width(label) - symbol->size().width()) / 2, font.glyph_height() + paint_rect.y() + 3 },
  104. symbol, color());
  105. for (int y = height / 2; y < height; ++y) {
  106. for (int x = 0; x < width; ++x) {
  107. m_front->set_pixel(x, y, m_front->get_pixel(width - x - 1, height - y - 1));
  108. }
  109. }
  110. }
  111. Card::~Card()
  112. {
  113. }
  114. void Card::draw(GUI::Painter& painter) const
  115. {
  116. VERIFY(!s_background.is_null());
  117. painter.blit(position(), m_upside_down ? *s_background : *m_front, m_front->rect());
  118. }
  119. void Card::clear(GUI::Painter& painter, const Color& background_color) const
  120. {
  121. painter.fill_rect({ old_position(), { width, height } }, background_color);
  122. }
  123. void Card::save_old_position()
  124. {
  125. m_old_position = m_rect.location();
  126. m_old_position_valid = true;
  127. }
  128. void Card::clear_and_draw(GUI::Painter& painter, const Color& background_color)
  129. {
  130. if (is_old_position_valid())
  131. clear(painter, background_color);
  132. draw(painter);
  133. save_old_position();
  134. }
  135. }