Card.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. static RefPtr<Gfx::Bitmap> s_background_inverted;
  57. Card::Card(Type type, uint8_t value)
  58. : m_rect(Gfx::IntRect({}, { width, height }))
  59. , m_front(*Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height }))
  60. , m_type(type)
  61. , m_value(value)
  62. {
  63. VERIFY(value < card_count);
  64. Gfx::IntRect paint_rect({ 0, 0 }, { width, height });
  65. if (s_background.is_null()) {
  66. s_background = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height });
  67. Gfx::Painter bg_painter(*s_background);
  68. auto image = Gfx::Bitmap::try_load_from_file("/res/icons/cards/buggie-deck.png").release_value_but_fixme_should_propagate_errors();
  69. float aspect_ratio = image->width() / static_cast<float>(image->height());
  70. auto target_size = Gfx::IntSize(static_cast<int>(aspect_ratio * (height - 5)), height - 5);
  71. bg_painter.fill_rect_with_rounded_corners(paint_rect, Color::Black, card_radius);
  72. auto inner_paint_rect = paint_rect.shrunken(2, 2);
  73. bg_painter.fill_rect_with_rounded_corners(inner_paint_rect, Color::White, card_radius - 1);
  74. bg_painter.draw_scaled_bitmap(
  75. { { (width - target_size.width()) / 2, (height - target_size.height()) / 2 }, target_size },
  76. *image, image->rect());
  77. s_background_inverted = invert_bitmap(*s_background);
  78. }
  79. Gfx::Painter painter(m_front);
  80. auto& font = Gfx::FontDatabase::default_font().bold_variant();
  81. auto label = labels[value];
  82. painter.fill_rect_with_rounded_corners(paint_rect, Color::Black, card_radius);
  83. paint_rect.shrink(2, 2);
  84. painter.fill_rect_with_rounded_corners(paint_rect, Color::White, card_radius - 1);
  85. paint_rect.set_height(paint_rect.height() / 2);
  86. paint_rect.shrink(10, 6);
  87. auto text_rect = Gfx::IntRect { 4, 6, font.width("10"), font.glyph_height() };
  88. painter.draw_text(text_rect, label, font, Gfx::TextAlignment::Center, color());
  89. NonnullRefPtr<Gfx::CharacterBitmap> symbol = s_diamond;
  90. switch (m_type) {
  91. case Diamonds:
  92. symbol = s_diamond;
  93. break;
  94. case Clubs:
  95. symbol = s_club;
  96. break;
  97. case Spades:
  98. symbol = s_spade;
  99. break;
  100. case Hearts:
  101. symbol = s_heart;
  102. break;
  103. default:
  104. VERIFY_NOT_REACHED();
  105. }
  106. painter.draw_bitmap(
  107. { text_rect.x() + (text_rect.width() - symbol->size().width()) / 2, text_rect.bottom() + 5 },
  108. symbol, color());
  109. for (int y = height / 2; y < height; ++y) {
  110. for (int x = 0; x < width; ++x) {
  111. m_front->set_pixel(x, y, m_front->get_pixel(width - x - 1, height - y - 1));
  112. }
  113. }
  114. m_front_inverted = invert_bitmap(*m_front);
  115. }
  116. Card::~Card()
  117. {
  118. }
  119. void Card::draw(GUI::Painter& painter) const
  120. {
  121. VERIFY(!s_background.is_null());
  122. if (m_inverted)
  123. painter.blit(position(), m_upside_down ? *s_background_inverted : *m_front_inverted, m_front_inverted->rect());
  124. else
  125. painter.blit(position(), m_upside_down ? *s_background : *m_front, m_front->rect());
  126. }
  127. void Card::clear(GUI::Painter& painter, const Color& background_color) const
  128. {
  129. painter.fill_rect({ old_position(), { width, height } }, background_color);
  130. }
  131. void Card::save_old_position()
  132. {
  133. m_old_position = m_rect.location();
  134. m_old_position_valid = true;
  135. }
  136. void Card::clear_and_draw(GUI::Painter& painter, const Color& background_color)
  137. {
  138. if (is_old_position_valid())
  139. clear(painter, background_color);
  140. draw(painter);
  141. save_old_position();
  142. }
  143. NonnullRefPtr<Gfx::Bitmap> Card::invert_bitmap(Gfx::Bitmap& bitmap)
  144. {
  145. auto inverted_bitmap = bitmap.clone().release_value_but_fixme_should_propagate_errors();
  146. for (int y = 0; y < inverted_bitmap->height(); y++) {
  147. for (int x = 0; x < inverted_bitmap->width(); x++) {
  148. inverted_bitmap->set_pixel(x, y, inverted_bitmap->get_pixel(x, y).inverted());
  149. }
  150. }
  151. return *inverted_bitmap;
  152. }
  153. }