CardStack.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2020, Till Mayer <till.mayer@web.de>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CardStack.h"
  7. namespace Cards {
  8. CardStack::CardStack()
  9. : m_position({ 0, 0 })
  10. , m_type(Invalid)
  11. , m_base(m_position, { Card::width, Card::height })
  12. {
  13. }
  14. CardStack::CardStack(const Gfx::IntPoint& position, Type type)
  15. : m_position(position)
  16. , m_type(type)
  17. , m_rules(rules_for_type(type))
  18. , m_base(m_position, { Card::width, Card::height })
  19. {
  20. VERIFY(type != Invalid);
  21. calculate_bounding_box();
  22. }
  23. CardStack::CardStack(const Gfx::IntPoint& position, Type type, NonnullRefPtr<CardStack> associated_stack)
  24. : m_associated_stack(move(associated_stack))
  25. , m_position(position)
  26. , m_type(type)
  27. , m_rules(rules_for_type(type))
  28. , m_base(m_position, { Card::width, Card::height })
  29. {
  30. VERIFY(type != Invalid);
  31. calculate_bounding_box();
  32. }
  33. void CardStack::clear()
  34. {
  35. m_stack.clear();
  36. m_stack_positions.clear();
  37. }
  38. void CardStack::draw(GUI::Painter& painter, const Gfx::Color& background_color)
  39. {
  40. auto draw_background_if_empty = [&]() {
  41. size_t number_of_moving_cards = 0;
  42. for (const auto& card : m_stack)
  43. number_of_moving_cards += card.is_moving();
  44. if (m_associated_stack && !m_associated_stack->is_empty())
  45. return false;
  46. if (!is_empty() && (m_stack.size() != number_of_moving_cards))
  47. return false;
  48. painter.fill_rect_with_rounded_corners(m_base, background_color.darkened(0.5), Card::card_radius);
  49. painter.fill_rect_with_rounded_corners(m_base.shrunken(2, 2), background_color, Card::card_radius - 1);
  50. return true;
  51. };
  52. switch (m_type) {
  53. case Stock:
  54. if (draw_background_if_empty()) {
  55. painter.fill_rect(m_base.shrunken(Card::width / 4, Card::height / 4), background_color.lightened(1.5));
  56. painter.fill_rect(m_base.shrunken(Card::width / 2, Card::height / 2), background_color);
  57. }
  58. break;
  59. case Foundation:
  60. if (draw_background_if_empty()) {
  61. for (int y = 0; y < (m_base.height() - 4) / 8; ++y) {
  62. for (int x = 0; x < (m_base.width() - 4) / 5; ++x) {
  63. painter.draw_rect({ 4 + m_base.x() + x * 5, 4 + m_base.y() + y * 8, 1, 1 }, background_color.darkened(0.5));
  64. }
  65. }
  66. }
  67. break;
  68. case Play:
  69. case Normal:
  70. draw_background_if_empty();
  71. break;
  72. case Waste:
  73. break;
  74. default:
  75. VERIFY_NOT_REACHED();
  76. }
  77. if (is_empty())
  78. return;
  79. if (m_rules.shift_x == 0 && m_rules.shift_y == 0) {
  80. auto& card = peek();
  81. card.draw(painter);
  82. return;
  83. }
  84. for (auto& card : m_stack) {
  85. if (!card.is_moving())
  86. card.clear_and_draw(painter, Gfx::Color::Transparent);
  87. }
  88. }
  89. void CardStack::rebound_cards()
  90. {
  91. VERIFY(m_stack_positions.size() == m_stack.size());
  92. size_t card_index = 0;
  93. for (auto& card : m_stack)
  94. card.set_position(m_stack_positions.at(card_index++));
  95. }
  96. void CardStack::add_all_grabbed_cards(const Gfx::IntPoint& click_location, NonnullRefPtrVector<Card>& grabbed)
  97. {
  98. VERIFY(grabbed.is_empty());
  99. if (m_type != Normal) {
  100. auto& top_card = peek();
  101. if (top_card.rect().contains(click_location)) {
  102. top_card.set_moving(true);
  103. grabbed.append(top_card);
  104. }
  105. return;
  106. }
  107. RefPtr<Card> last_intersect;
  108. for (auto& card : m_stack) {
  109. if (card.rect().contains(click_location)) {
  110. if (card.is_upside_down())
  111. continue;
  112. last_intersect = card;
  113. } else if (!last_intersect.is_null()) {
  114. if (grabbed.is_empty()) {
  115. grabbed.append(*last_intersect);
  116. last_intersect->set_moving(true);
  117. }
  118. if (card.is_upside_down()) {
  119. grabbed.clear();
  120. return;
  121. }
  122. card.set_moving(true);
  123. grabbed.append(card);
  124. }
  125. }
  126. if (grabbed.is_empty() && !last_intersect.is_null()) {
  127. grabbed.append(*last_intersect);
  128. last_intersect->set_moving(true);
  129. }
  130. }
  131. bool CardStack::is_allowed_to_push(const Card& card, size_t stack_size) const
  132. {
  133. if (m_type == Stock || m_type == Waste || m_type == Play)
  134. return false;
  135. if (m_type == Normal && is_empty())
  136. return card.value() == 12;
  137. if (m_type == Foundation && is_empty())
  138. return card.value() == 0;
  139. if (!is_empty()) {
  140. auto& top_card = peek();
  141. if (top_card.is_upside_down())
  142. return false;
  143. if (m_type == Foundation) {
  144. // Prevent player from dragging an entire stack of cards to the foundation stack
  145. if (stack_size > 1)
  146. return false;
  147. return top_card.type() == card.type() && m_stack.size() == card.value();
  148. } else if (m_type == Normal) {
  149. return top_card.color() != card.color() && top_card.value() == card.value() + 1;
  150. }
  151. VERIFY_NOT_REACHED();
  152. }
  153. return true;
  154. }
  155. void CardStack::push(NonnullRefPtr<Card> card)
  156. {
  157. auto size = m_stack.size();
  158. auto top_most_position = m_stack_positions.is_empty() ? m_position : m_stack_positions.last();
  159. if (size && size % m_rules.step == 0) {
  160. if (peek().is_upside_down())
  161. top_most_position.translate_by(m_rules.shift_x, m_rules.shift_y_upside_down);
  162. else
  163. top_most_position.translate_by(m_rules.shift_x, m_rules.shift_y);
  164. }
  165. if (m_type == Stock)
  166. card->set_upside_down(true);
  167. card->set_position(top_most_position);
  168. m_stack.append(card);
  169. m_stack_positions.append(top_most_position);
  170. calculate_bounding_box();
  171. }
  172. NonnullRefPtr<Card> CardStack::pop()
  173. {
  174. auto card = m_stack.take_last();
  175. calculate_bounding_box();
  176. if (m_type == Stock)
  177. card->set_upside_down(false);
  178. m_stack_positions.take_last();
  179. return card;
  180. }
  181. void CardStack::move_to_stack(CardStack& stack)
  182. {
  183. while (!m_stack.is_empty()) {
  184. auto card = m_stack.take_first();
  185. m_stack_positions.take_first();
  186. stack.push(move(card));
  187. }
  188. calculate_bounding_box();
  189. }
  190. void CardStack::calculate_bounding_box()
  191. {
  192. m_bounding_box = Gfx::IntRect(m_position, { Card::width, Card::height });
  193. if (m_stack.is_empty())
  194. return;
  195. uint16_t width = 0;
  196. uint16_t height = 0;
  197. size_t card_position = 0;
  198. for (auto& card : m_stack) {
  199. if (card_position % m_rules.step == 0 && card_position) {
  200. if (card.is_upside_down()) {
  201. width += m_rules.shift_x;
  202. height += m_rules.shift_y_upside_down;
  203. } else {
  204. width += m_rules.shift_x;
  205. height += m_rules.shift_y;
  206. }
  207. }
  208. ++card_position;
  209. }
  210. m_bounding_box.set_size(Card::width + width, Card::height + height);
  211. }
  212. }