TableauColumn.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. FURI_LOG_D("Tableau", "%i %i, count %li", x, y, cards->count);
  18. if (cards->count == 0) {
  19. Card::RenderEmptyCard(x, y, buffer);
  20. if (selected)
  21. buffer->draw_rbox(x, y, x + 16, y + 22, Flip);
  22. return;
  23. }
  24. uint8_t selection = cards->count - selection_from_end;
  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);
  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);
  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);
  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);
  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);
  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. /* ListItem<Card> *c = cards->start;
  67. while (c){
  68. index++;
  69. if (c->data->exposed) {
  70. break;
  71. }
  72. c=c->next;
  73. }*/
  74. for (auto *card: *cards) {
  75. index++;
  76. if (card && card->exposed) {
  77. break;
  78. }
  79. }
  80. }
  81. return index;
  82. }
  83. TableauColumn::TableauColumn() {
  84. cards = new List<Card>();
  85. }
  86. TableauColumn::~TableauColumn() {
  87. delete cards;
  88. }