TableauColumn.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "TableauColumn.h"
  2. #include "utils/RenderBuffer.h"
  3. #define max(a, b) a>b?a:b
  4. void TableauColumn::Reset() {
  5. cards->empty();
  6. }
  7. void TableauColumn::AddCard(Card *c) {
  8. cards->add(c);
  9. }
  10. void TableauColumn::AddRange(List<Card> *hand) {
  11. for (auto *item: *hand) {
  12. cards->add(item);
  13. }
  14. }
  15. void TableauColumn::Render(uint8_t x, uint8_t y, bool selected, uint8_t selection_from_end, RenderBuffer *buffer) {
  16. // If the hand is empty
  17. if (cards->count == 0) {
  18. Card::RenderEmptyCard(x, y, buffer);
  19. if (selected)
  20. buffer->draw_rbox(x, y, x + 16, y + 22, Flip);
  21. return;
  22. }
  23. uint8_t selection = cards->count - selection_from_end;
  24. if (selected) selection--;
  25. uint8_t loop_end = cards->count;
  26. uint8_t loop_start = max(loop_end - 4, 0);
  27. uint8_t position = 0;
  28. uint8_t first_non_flipped = FirstNonFlipped();
  29. bool had_top = false;
  30. // Draw the first flipped and non-flipped card with adjusted visibility
  31. if (first_non_flipped <= loop_start && selection != first_non_flipped) {
  32. // Draw a card back if it is not the first card
  33. if (first_non_flipped > 0) {
  34. Card::RenderBack(x, y + position, selection == first_non_flipped, buffer, 4);
  35. // Increment loop start index and position
  36. position += 4;
  37. loop_start++;
  38. had_top = true;
  39. }
  40. // Draw the front side of the first non-flipped card
  41. (*cards)[first_non_flipped]->Render(x, y + position, selection == first_non_flipped, buffer, cards->count == 1 ? 22 : 8);
  42. position += 8;
  43. loop_start++; // Increment loop start index
  44. }
  45. // Draw the selected card with adjusted visibility
  46. if (loop_start > selection) {
  47. if (!had_top && first_non_flipped > 0) {
  48. Card::RenderBack(x, y + position, selection == first_non_flipped, buffer,4);
  49. position += 2;
  50. loop_start++;
  51. }
  52. // Draw the front side of the selected card
  53. (*cards)[selection]->Render(x, y + position, true, buffer, 8);
  54. position += 8;
  55. loop_start++; // Increment loop start index
  56. }
  57. //Draw the rest
  58. for (uint8_t i = loop_start; i < loop_end; i++, position += 4) {
  59. (*cards)[i]->Render(x, y + position, i == selection, buffer, (i+1)==loop_end ? 22 : 4);
  60. if (i == selection || i == first_non_flipped) position += 4;
  61. }
  62. }
  63. int8_t TableauColumn::FirstNonFlipped() {
  64. int8_t index = -1;
  65. if (cards->count > 0) {
  66. for (auto *card: *cards) {
  67. index++;
  68. if (card && card->exposed) {
  69. break;
  70. }
  71. }
  72. }
  73. return index;
  74. }
  75. TableauColumn::TableauColumn() {
  76. cards = new List<Card>();
  77. }
  78. TableauColumn::~TableauColumn() {
  79. delete cards;
  80. }
  81. void TableauColumn::AddTo(TableauColumn *other) {
  82. for (auto *item: *(other->cards)) {
  83. cards->add(item);
  84. }
  85. }
  86. Card *TableauColumn::TopCard() {
  87. return (*cards)[0];
  88. }
  89. uint8_t TableauColumn::Count() {
  90. return cards->count;
  91. }
  92. List<Card>* TableauColumn::ExtractEnd(uint8_t count) {
  93. return cards->splice(cards->count - count, count);
  94. }