Card.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2020, Till Mayer <till.mayer@web.de>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "Card.h"
  9. #include <AK/Random.h>
  10. #include <LibCards/CardPainter.h>
  11. namespace Cards {
  12. Card::Card(Suit suit, Rank rank)
  13. : m_rect(Gfx::IntRect({}, { width, height }))
  14. , m_suit(suit)
  15. , m_rank(rank)
  16. {
  17. VERIFY(to_underlying(rank) < card_count);
  18. }
  19. void Card::paint(GUI::Painter& painter, bool highlighted) const
  20. {
  21. auto& card_painter = CardPainter::the();
  22. auto bitmap = [&]() {
  23. if (m_inverted)
  24. return m_upside_down ? card_painter.card_back_inverted() : card_painter.card_front_inverted(m_suit, m_rank);
  25. if (highlighted) {
  26. VERIFY(!m_upside_down);
  27. return card_painter.card_front_highlighted(m_suit, m_rank);
  28. }
  29. return m_upside_down ? card_painter.card_back() : card_painter.card_front(m_suit, m_rank);
  30. }();
  31. painter.blit(position(), bitmap, bitmap->rect());
  32. }
  33. void Card::clear(GUI::Painter& painter, Color background_color) const
  34. {
  35. painter.fill_rect({ old_position(), { width, height } }, background_color);
  36. }
  37. void Card::save_old_position()
  38. {
  39. m_old_position = m_rect.location();
  40. m_old_position_valid = true;
  41. }
  42. void Card::clear_and_paint(GUI::Painter& painter, Color background_color, bool highlighted)
  43. {
  44. if (is_old_position_valid())
  45. clear(painter, background_color);
  46. paint(painter, highlighted);
  47. save_old_position();
  48. }
  49. ErrorOr<Vector<NonnullRefPtr<Card>>> create_standard_deck(Shuffle shuffle)
  50. {
  51. return create_deck(1, 1, 1, 1, shuffle);
  52. }
  53. ErrorOr<Vector<NonnullRefPtr<Card>>> create_deck(unsigned full_club_suit_count, unsigned full_diamond_suit_count, unsigned full_heart_suit_count, unsigned full_spade_suit_count, Shuffle shuffle)
  54. {
  55. Vector<NonnullRefPtr<Card>> deck;
  56. TRY(deck.try_ensure_capacity(Card::card_count * (full_club_suit_count + full_diamond_suit_count + full_heart_suit_count + full_spade_suit_count)));
  57. auto add_cards_for_suit = [&deck](Cards::Suit suit, unsigned number_of_suits) -> ErrorOr<void> {
  58. for (auto i = 0u; i < number_of_suits; ++i) {
  59. for (auto rank = 0; rank < Card::card_count; ++rank) {
  60. deck.unchecked_append(TRY(Card::try_create(suit, static_cast<Cards::Rank>(rank))));
  61. }
  62. }
  63. return {};
  64. };
  65. TRY(add_cards_for_suit(Cards::Suit::Clubs, full_club_suit_count));
  66. TRY(add_cards_for_suit(Cards::Suit::Diamonds, full_diamond_suit_count));
  67. TRY(add_cards_for_suit(Cards::Suit::Hearts, full_heart_suit_count));
  68. TRY(add_cards_for_suit(Cards::Suit::Spades, full_spade_suit_count));
  69. if (shuffle == Shuffle::Yes)
  70. AK::shuffle(deck);
  71. return deck;
  72. }
  73. }