-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeck.cpp
1600 lines (1430 loc) · 40.6 KB
/
deck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "deck.h"
#include "space.h"
#include "utils.h"
#include "version.h"
#include <algorithm>
#include <iomanip> // setw
#include <map>
#include <sstream>
Deck::Deck(door::ANSIColor backcolor) : card_back_color{backcolor} {
for (int i = 0; i < 52; ++i) {
cards.push_back(cardOf(i));
}
// 0 = BLANK, 1-4 levels
for (int i = 0; i < 5; ++i) {
backs.push_back(backOf(i));
}
mark.push_back(markOf(0));
mark.push_back(markOf(1));
}
Deck::~Deck() {}
Deck::Deck(Deck &&ref) {
card_back_color = ref.card_back_color;
/*
for (auto c : cards)
delete c;
cards.clear();
*/
cards = ref.cards;
ref.cards.clear();
/*
for (auto b : backs)
delete b;
backs.clear();
*/
backs = ref.backs;
ref.backs.clear();
/*
for (auto m : mark)
delete m;
mark.clear();
*/
mark = ref.mark;
ref.mark.clear();
// card_height = ref.card_height;
};
Deck &Deck::operator=(Deck &&ref) {
card_back_color = ref.card_back_color;
/*
for (auto c : cards)
delete c;
cards.clear();
*/
cards = ref.cards;
ref.cards.clear();
/*
for (auto b : backs)
delete b;
backs.clear();
*/
backs = ref.backs;
ref.backs.clear();
/*
for (auto m : mark)
delete m;
mark.clear();
*/
mark = ref.mark;
ref.mark.clear();
// card_height = ref.card_height;
return *this;
}
int Deck::getDeck(int c) { return c / 52; }
int Deck::getSuit(int c) { return (c % 52) / 13; }
int Deck::getRank(int c) { return (c % 52) % 13; }
char Deck::rankSymbol(int c) {
const char symbols[] = "A23456789TJQK";
return symbols[c];
}
std::string Deck::suitSymbol(int c) {
// unicode
if (door::unicode) {
switch (c) {
case 0:
return std::string("\u2665");
case 1:
return std::string("\u2666");
case 2:
return std::string("\u2663");
case 3:
return std::string("\u2660");
}
} else {
if (door::full_cp437) {
switch (c) {
case 0:
return std::string(1, '\x03');
case 1:
return std::string(1, '\x04');
case 2:
return std::string(1, '\x05');
case 3:
return std::string(1, '\x06');
}
} else {
// These look horrible!
switch (c) {
case 0:
return std::string(1, '*'); // H
case 1:
return std::string(1, '^'); // D
case 2:
return std::string(1, '%'); // C
case 3:
return std::string(1, '$'); // S
}
}
}
return std::string("!", 1);
}
shared_panel Deck::cardOf(int c) {
int suit = getSuit(c);
int rank = getRank(c);
bool is_red = (suit < 2);
door::ANSIColor color;
if (is_red) {
color = door::ANSIColor(door::COLOR::RED, door::COLOR::WHITE);
} else {
color = door::ANSIColor(door::COLOR::BLACK, door::COLOR::WHITE);
}
shared_panel p = std::make_shared<door::Panel>(0, 0, 5);
// setColor sets border_color. NOT WHAT I WANT.
// p->setColor(color);
char r = rankSymbol(rank);
std::string s = suitSymbol(suit);
// build lines
std::ostringstream oss;
oss << r << s << " ";
std::string str = oss.str();
p->addLine(std::make_unique<door::Line>(str, 5, color));
oss.str(std::string());
oss.clear();
if (card_height == 5)
p->addLine(std::make_unique<door::Line>(" ", 5, color));
oss << " " << s << " ";
str = oss.str();
p->addLine(std::make_unique<door::Line>(str, 5, color));
oss.str(std::string());
oss.clear();
if (card_height == 5)
p->addLine(std::make_unique<door::Line>(" ", 5, color));
oss << " " << s << r;
str = oss.str();
p->addLine(std::make_unique<door::Line>(str, 5, color));
oss.str(std::string());
oss.clear();
return p;
}
std::string Deck::backSymbol(int level) {
std::string c;
if (level == 0) {
c = ' ';
return c;
}
if (door::unicode) {
switch (level) {
case 1:
c = "\u2591";
break;
case 2:
c = "\u2592";
break;
case 3:
c = "\u2593";
break;
case 4:
c = "\u2588";
break;
}
} else {
switch (level) {
case 1:
c = "\xb0";
break;
case 2:
c = "\xb1";
break;
case 3:
c = "\xb2";
break;
case 4:
c = "\xdb";
break;
}
}
return c;
}
void Deck::setBack(door::ANSIColor backColor) {
for (auto &back : backs) {
back->lineSetBack(backColor);
}
card_back_color = backColor;
}
shared_panel Deck::backOf(int level) {
// using: \xb0, 0xb1, 0xb2, 0xdb
// OR: \u2591, \u2592, \u2593, \u2588
// door::ANSIColor color(door::COLOR::RED, door::COLOR::BLACK);
shared_panel p = std::make_shared<door::Panel>(0, 0, 5);
std::string c = backSymbol(level);
std::string l = c + c + c + c + c;
for (int x = 0; x < card_height; ++x) {
p->addLine(std::make_unique<door::Line>(l, 5, card_back_color));
};
// p->addLine(std::make_unique<door::Line>(l, 5, card_back_color));
// p->addLine(std::make_unique<door::Line>(l, 5, card_back_color));
return p;
}
shared_panel Deck::markOf(int c) {
shared_panel p = std::make_shared<door::Panel>(1);
door::ANSIColor color = door::ANSIColor(
door::COLOR::BLUE, door::COLOR::WHITE); // , door::ATTR::BOLD);
std::string m;
if (c == 0)
m = " ";
else {
if (door::unicode) {
m = "\u25a0";
} else {
m = "\xfe";
}
}
p->addLine(std::make_unique<door::Line>(m, 1, color));
return p;
}
shared_panel Deck::card(int c) { return cards[c]; }
/**
* @brief Return panel for back of card.
*
* - 0 = Blank
* - 1 = level 1 (furthest/darkest)
* - 2 = level 2
* - 3 = level 3
* - 4 = level 4 (closest/lightest)
*
* @param level
* @return door::Panel*
*/
shared_panel Deck::back(int level) { return backs[level]; }
const std::array<std::pair<int, int>, 18> Deck::blocks = {
make_pair(3, 4), make_pair(5, 6), make_pair(7, 8), // end row 1
make_pair(9, 10), make_pair(10, 11), make_pair(12, 13),
make_pair(13, 14), make_pair(15, 16), make_pair(16, 17),
make_pair(18, 19), // end row 2
make_pair(19, 20), make_pair(20, 21), make_pair(21, 22),
make_pair(22, 23), make_pair(23, 24), make_pair(24, 25),
make_pair(25, 26), make_pair(26, 27) // 27
};
/**
* @brief Which card(s) are unblocked by this card?
*
* @param card
* @return * int
*/
std::vector<int> Deck::unblocks(int card) {
std::vector<int> result;
for (size_t i = 0; i < blocks.size(); ++i) {
if ((blocks.at(i).first == card) || (blocks.at(i).second == card)) {
result.push_back(i);
}
}
return result;
}
/**
* @brief Can this card play on this other card?
*
* @param card1
* @param card2
* @return true
* @return false
*/
bool Deck::canPlay(int card1, int card2) {
int rank1, rank2;
rank1 = getRank(card1);
rank2 = getRank(card2);
// this works %13 handles wrap-around for us.
if ((rank1 + 1) % 13 == rank2)
return true;
if (rank1 == 0) {
rank1 += 13;
}
if (rank1 - 1 == rank2)
return true;
return false;
}
/**
* @brief Returns marker
*
* 0 = blank
* 1 = [] symbol thing \xfe ■
*
* @param c
* @return door::Panel*
*/
shared_panel Deck::marker(int c) { return mark[c]; }
/**
* @brief removeCard
*
* This removes a card at a given position (c).
* It needs to know if there are cards underneath
* to the left or right. (If so, we restore those missing parts.)
*
* @param door
* @param c
* @param off_x
* @param off_y
* @param left
* @param right
*/
void Deck::removeCard(door::Door &door, int c, int off_x, int off_y, bool left,
bool right) {
int cx, cy, level;
cardPosLevel(c, cx, cy, level);
if (level > 1)
--level;
std::string cstr = backSymbol(level);
door::Goto g(cx + off_x, cy + off_y);
door << g << card_back_color;
if (left)
door << cstr;
else
door << " ";
door << " ";
if (right)
door << cstr;
else
door << " ";
g.set(cx + off_x, cy + off_y + 1);
door << g << " ";
g.set(cx + off_x, cy + off_y + 2);
door << g << " ";
}
/*
Layout spacing 1:
1 2 3 4 5 6
123456789012345678901234567890123456789012345678901234567890
░░░░░ ░░░░░ ░░░░░
░░░░░ ░░░░░ ░░░░░
▒▒▒▒▒░▒▒▒▒▒ #####░##### #####░#####
▒▒▒▒▒ ▒▒▒▒▒ ##### ##### ##### #####
▓▓▓▓▓▒▓▓▓▓▓▒▓▓▓▓▓ #####=#####=##### #####=#####=#####
▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓ ##### ##### ##### ##### ##### #####
█████▓█████▓█████▓#####=#####=#####=#####=#####=#####=#####
█████ █████ █████ ##### ##### ##### ##### ##### ##### #####
█████ █████ █████ ##### ##### ##### ##### ##### ##### #####
width = 5 * 10 + (1*9) = 59 OK!
Layout with spacing = 2:
EEEEE
ZZZZZ
yyyyyZZZyyyyy
yyyyy yyyyy
XXXXXyyyXXXXXyyyXXXXX
XXXXX XXXXX XXXXX
width = 5 * 10 + (2 * 9) = 50+18 = 68 ! I could do that!
*/
#ifdef NO
/**
* @brief Where does this card go in relation to everything else?
*
* This function is deprecated, see the other cardgo.
*
* @param pos
* @param space
* @param h
* @param x
* @param y
* @param level
*/
void cardgo(int pos, int space, int h, int &x, int &y, int &level) {
// special cases here
if (pos == 28) {
// cardgo(23, space, h, x, y, level);
cardgo(23, x, y, level);
y += h + 1;
--level;
return;
} else {
if (pos == 29) {
// cardgo(22, space, h, x, y, level);
cardgo(22, x, y, level);
y += h + 1;
--level;
return;
}
}
const int CARD_WIDTH = 5;
int HALF_WIDTH = 3;
// space = 1 or 3
// int space = 1;
// space = 3;
HALF_WIDTH += space / 2;
/*
int levels[4] = {3, 6, 9, 10};
for (level = 0; level < 4; ++level) {
if (pos < levels[level]) {
level++;
// we're here
y = (level -1) * 2 + 1;
} else {
pos -= levels[level];
}
}
*/
int between = CARD_WIDTH + space;
if (pos < 3) {
// top
level = 1;
y = (level - 1) * (h - 1) + 1;
x = pos * (between * 3) + between + HALF_WIDTH + space; // 10
return;
} else {
pos -= 3;
}
if (pos < 6) {
level = 2;
y = (level - 1) * (h - 1) + 1;
int group = (pos) / 2;
x = pos * between + (group * between) + CARD_WIDTH + space * 2;
return;
} else {
pos -= 6;
}
if (pos < 9) {
level = 3;
y = (level - 1) * (h - 1) + 1;
x = pos * between + HALF_WIDTH + space;
return;
} else {
pos -= 9;
}
if (pos < 10) {
level = 4;
y = (level - 1) * (h - 1) + 1;
x = (pos)*between + space;
return;
} else {
// something is wrong.
y = -1;
x = -1;
level = -1;
}
}
#endif
void cardPos(int pos, int &x, int &y) {
const int space = 3;
const int height = 3;
// special cases here
if (pos == 28) {
cardPos(23, x, y);
y += height + 1;
return;
} else {
if (pos == 29) {
cardPos(22, x, y);
y += height + 1;
return;
}
}
const int CARD_WIDTH = 5;
int HALF_WIDTH = 3;
HALF_WIDTH += space / 2;
int between = CARD_WIDTH + space;
int level; // I still need level in my calculations
if (pos < 3) {
// top
level = 1;
y = (level - 1) * (height - 1) + 1;
x = pos * (between * 3) + between + HALF_WIDTH + space; // 10
return;
} else {
pos -= 3;
}
if (pos < 6) {
level = 2;
y = (level - 1) * (height - 1) + 1;
int group = (pos) / 2;
x = pos * between + (group * between) + CARD_WIDTH + space * 2;
return;
} else {
pos -= 6;
}
if (pos < 9) {
level = 3;
y = (level - 1) * (height - 1) + 1;
x = pos * between + HALF_WIDTH + space;
return;
} else {
pos -= 9;
}
if (pos < 10) {
level = 4;
y = (level - 1) * (height - 1) + 1;
x = (pos)*between + space;
return;
} else {
// something is wrong.
y = -1;
x = -1;
level = -1;
}
}
void cardLevel(int pos, int &level) {
/*
const int space = 3;
const int height = 3;
*/
// special cases here
if (pos == 28) {
cardLevel(23, level);
--level;
return;
} else {
if (pos == 29) {
cardLevel(22, level);
--level;
return;
}
}
/*
const int CARD_WIDTH = 5;
int HALF_WIDTH = 3;
HALF_WIDTH += space / 2;
int between = CARD_WIDTH + space;
*/
if (pos < 3) {
// top
level = 1;
return;
} else {
pos -= 3;
}
if (pos < 6) {
level = 2;
return;
} else {
pos -= 6;
}
if (pos < 9) {
level = 3;
return;
} else {
pos -= 9;
}
if (pos < 10) {
level = 4;
return;
} else {
// something is wrong.
level = -1;
}
}
/**
* @brief Given card pos, calculate x, y, and level values.
*
* level is used to determine the card background gradient.
*
* @param pos
* @param x
* @param y
* @param level
*/
void cardPosLevel(int pos, int &x, int &y, int &level) {
const int space = 3;
const int height = 3;
// special cases here
if (pos == 28) {
cardPosLevel(23, x, y, level);
y += height + 1;
--level;
return;
} else {
if (pos == 29) {
cardPosLevel(22, x, y, level);
y += height + 1;
--level;
return;
}
}
const int CARD_WIDTH = 5;
int HALF_WIDTH = 3;
HALF_WIDTH += space / 2;
int between = CARD_WIDTH + space;
if (pos < 3) {
// top
level = 1;
y = (level - 1) * (height - 1) + 1;
x = pos * (between * 3) + between + HALF_WIDTH + space; // 10
return;
} else {
pos -= 3;
}
if (pos < 6) {
level = 2;
y = (level - 1) * (height - 1) + 1;
int group = (pos) / 2;
x = pos * between + (group * between) + CARD_WIDTH + space * 2;
return;
} else {
pos -= 6;
}
if (pos < 9) {
level = 3;
y = (level - 1) * (height - 1) + 1;
x = pos * between + HALF_WIDTH + space;
return;
} else {
pos -= 9;
}
if (pos < 10) {
level = 4;
y = (level - 1) * (height - 1) + 1;
x = (pos)*between + space;
return;
} else {
// something is wrong.
y = -1;
x = -1;
level = -1;
}
}
/**
* @brief shuffle deck of cards
*
* example of seeding the deck for a given date 2/27/2021 game 1
* std::seed_seq s1{2021, 2, 27, 1};
* vector<int> deck1 = shuffleCards(s1, 1);
* @param seed
* @param decks
* @return vector<int>
*/
cards shuffleCards(std::seed_seq &seed, int decks) {
std::mt19937 gen;
// build deck of cards
int size = decks * 52;
std::vector<int> deck;
deck.reserve(size);
for (int x = 0; x < size; ++x) {
deck.push_back(x);
}
// repeatable, but random
gen.seed(seed);
std::shuffle(deck.begin(), deck.end(), gen);
return deck;
}
/**
* @brief generate a vector of ints to track card states.
*
* This initializes everything to 0.
*
* @param decks
* @return cards
*/
cards makeCardStates(int decks) {
// auto states = std::unique_ptr<std::vector<int>>(); // (decks * 52, 0)>;
std::vector<int> states;
states.assign(decks * 52, 0);
return states;
}
/**
* @brief Find the next card we can move the marker to.
*
* if left, look in the left - direction, otherwise the right + direction.
* current is the current active card.
* states is the card states (0 = down, 1 = in play, 2 = removed)
*
* updated: If we can't go any further left (or right), then
* roll around to the other side.
*
* @param left
* @param states
* @param current
* @return int
*/
int findNextActiveCard(bool left, const cards &states, int current) {
int cx, cy;
int current_x;
cardPos(current, cx, cy);
current_x = cx;
int x;
int pos = -1;
int pos_x;
int max_pos = -1;
int max_x = -1;
int min_pos = -1;
int min_x = 100;
if (left)
pos_x = 0;
else
pos_x = 100;
for (x = 0; x < 28; x++) {
if (states.at(x) == 1) {
// possible location
if (x == current)
continue;
cardPos(x, cx, cy);
// find max and min while we're iterating here
if (cx < min_x) {
min_pos = x;
min_x = cx;
}
if (cx > max_x) {
max_pos = x;
max_x = cx;
}
if (left) {
if ((cx < current_x) and (cx > pos_x)) {
pos_x = cx;
pos = x;
}
} else {
if ((cx > current_x) and (cx < pos_x)) {
pos_x = cx;
pos = x;
}
}
}
}
if (pos == -1) {
// we couldn't find one
if (left) {
// use max -- roll around to the right
pos = max_pos;
} else {
// use min -- roll around to the left
pos = min_pos;
}
}
return pos;
}
/**
* @brief Find the next closest card to move to.
*
* Given the card states, this finds the next closest card.
* Uses current.
*
* return -1 there's no options to go to. (END OF GAME)
* @param states
* @param current
* @return int
*/
int findClosestActiveCard(const cards &states, int current) {
int cx, cy;
int current_x;
cardPos(current, cx, cy);
current_x = cx;
int x;
int pos = -1;
int pos_x = -1;
for (x = 0; x < 28; x++) {
if (states.at(x) == 1) {
// possible location
if (x == current)
continue;
cardPos(x, cx, cy);
if (pos == -1) {
pos = x;
pos_x = cx;
} else {
if (abs(current_x - cx) < abs(current_x - pos_x)) {
pos = x;
pos_x = cx;
}
}
}
}
return pos;
}
vector<std::string> deck_colors = {std::string("All"), std::string("Blue"),
std::string("Brown"), std::string("Cyan"),
std::string("Green"), std::string("Magenta"),
std::string("Red"), std::string("White")};
/**
* @brief menu render that sets the text color based on the color found in the
* text itself. This only finds one color.
*
* @param c1 [] brackets
* @param c2 text within brackets
* @param c3 base color give (we set the FG, we use the BG)
* @return door::renderFunction
*/
door::renderFunction makeColorRender(door::ANSIColor c1, door::ANSIColor c2,
door::ANSIColor c3) {
door::renderFunction render = [c1, c2,
c3](const std::string &txt) -> door::Render {
door::Render r(txt);
bool option = true;
door::ColorOutput co;
// I need this mutable
door::ANSIColor textColor = c3;
// Color update:
{
std::string found;
for (auto &dc : deck_colors) {
if (txt.find(dc) != string::npos) {
found = dc;
break;
}
}
if (!found.empty()) {
if (found == "All") {
// handle this some other way.
textColor.setFg(door::COLOR::WHITE);
} else {
door::ANSIColor c = stringToANSIColor(found);
textColor.setFg(c.getFg());
}
}
}
co.pos = 0;
co.len = 0;
co.c = c1;
int tpos = 0;
for (char const &c : txt) {
if (option) {
if (c == '[' or c == ']') {
if (co.c != c1)
if (co.len != 0) {
r.outputs.push_back(co);
co.reset();
co.pos = tpos;
}
co.c = c1;
if (c == ']')
option = false;
} else {
if (co.c != c2)
if (co.len != 0) {
r.outputs.push_back(co);
co.reset();
co.pos = tpos;
}
co.c = c2;
}
} else {
if (co.c != textColor)
if (co.len != 0) {
r.outputs.push_back(co);
co.reset();
co.pos = tpos;
}
co.c = textColor;
}
co.len++;
tpos++;
}
if (co.len != 0) {
r.outputs.push_back(co);
}
return r;
};
return render;
}
/**
* @brief menu render that sets the text color based on the color found in the
* text itself. This finds all colors.
*
* @param c1 [] brackets
* @param c2 text within brackets
* @param c3 base color give (we set the FG, we use the BG)
* @return door::renderFunction
*/
door::renderFunction makeColorsRender(door::ANSIColor c1, door::ANSIColor c2,
door::ANSIColor c3) {
door::renderFunction render = [c1, c2,
c3](const std::string &txt) -> door::Render {
door::Render r(txt);
// find all of the words here
std::vector<std::pair<int, int>> words = find_words(txt);
auto words_it = words.begin();
// option is "[?]" part of the string
bool option = true;
// I need this mutable
door::ANSIColor textColor = c3;
door::ANSIColor normal = c3;
normal.setFg(door::COLOR::WHITE);
bool color_word = false;
std::pair<int, int> word_pair;
#ifdef DEBUG_OUTPUT
if (get_logger) {
get_logger() << "makeColorsRender() " << txt << std::endl;
for (auto word : words) {
get_logger() << word.first << "," << word.second << std::endl;
}
}
#endif
int tpos = 0;
for (char const &c : txt) {
if (option) {
if (c == '[' or c == ']') {
r.append(c1);
if (c == ']')
option = false;
} else {
r.append(c2);
}
} else {
// Ok, we are out of the options now.
if (color_word) {
// we're in a COLOR word.
if (tpos < word_pair.first + word_pair.second)
r.append(textColor);
else {
color_word = false;
r.append(normal);
}
} else {
// look for COLOR word.
while ((words_it != words.end()) and (words_it->first < tpos)) {