-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoor.h
966 lines (849 loc) · 22 KB
/
door.h
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
#ifndef DOOR_H
#define DOOR_H
#include <cstdint>
#include <ctime>
#include <fstream>
#include <functional>
#include <future>
#include <iostream>
#include <list>
#include <memory>
#include <ostream>
#include <vector>
#include "anyoption.h"
// raw mode
#include <termios.h>
#include <unistd.h>
#define CSI "\x1b["
// getkey definitions
#define XKEY_START 0x1000
#define XKEY_UP_ARROW 0x1001
#define XKEY_DOWN_ARROW 0x1002
#define XKEY_RIGHT_ARROW 0x1003
#define XKEY_LEFT_ARROW 0x1004
#define XKEY_HOME 0x1010
#define XKEY_END 0x1011
#define XKEY_PGUP 0x1012
#define XKEY_PGDN 0x1023
#define XKEY_INSERT 0x1024
#define XKEY_DELETE 0x7f
#define XKEY_F1 0x1021
#define XKEY_F2 0x1022
#define XKEY_F3 0x1023
#define XKEY_F4 0x1024
#define XKEY_F5 0x1025
#define XKEY_F6 0x1026
#define XKEY_F7 0x1027
#define XKEY_F8 0x1028
#define XKEY_F9 0x1029
#define XKEY_F10 0x102a
#define XKEY_F11 0x102b
#define XKEY_F12 0x102c
#define XKEY_UNKNOWN 0x1111
#define TIMEOUT -1
#define HANGUP -2
#define OUTOFTIME -3
/**
* @brief The BBS door project.
* This is an attempt at writing a C++ BBS door toolkit.
*/
namespace door {
extern bool unicode;
extern bool full_cp437;
extern bool debug_capture;
extern std::list<char> pushback;
/*
Translate CP437 strings to unicode for output.
if (door::unicode) {
// perform translation
}
*/
void cp437toUnicode(std::string input, std::string &out);
void cp437toUnicode(const char *input, std::string &out);
/*
door 2.0
*/
/**
* ANSI Color codes
*/
/**
* @brief The colors available under ANSI-BBS
*/
enum class COLOR : std::int8_t {
/// BLACK (0)
BLACK,
/// RED (1)
RED,
/// GREEN (2)
GREEN,
/// BROWN (3)
BROWN,
/// YELLOW (3)
YELLOW = 3,
/// BLUE (4)
BLUE,
/// MAGENTA (5)
MAGENTA,
/// CYAN (6)
CYAN,
/// WHITE (7)
WHITE
};
/**
* @brief ANSI-BBS text attributes
*/
enum class ATTR : std::int8_t {
/// RESET forces all attributes (and Colors) to be sent.
RESET,
/// BOLD is the same as BRIGHT.
BOLD,
/// BRIGHT is the same as BOLD.
BRIGHT = 1,
/// SLOW BLINK
BLINK = 5,
/// INVERSE is Background on Foreground.
INVERSE = 7
};
constexpr long strhash(const char *txt) {
long result = 0;
for (int x = 0; x < 3; ++x) {
if (txt[x] == 0) break;
result = (result << 8) | txt[x];
}
return result;
};
enum Attribute {
ATTR_NONE = 0x00,
ATTR_BOLD = 0x01,
ATTR_BRIGHT = 0x01,
ATTR_INVERSE = 0x02,
ATTR_BLINK = 0x04,
ATTR_RESET = 0x08,
};
/**
* @class ANSIColor
* This holds foreground, background and ANSI-BBS attribute
* information.
* The special attribute RESET forces attribute and color
* output always.
*
* @brief Foreground, Background and Attributes
*
*/
class ANSIColor {
public:
/** Foreground color */
COLOR fg;
/** Background color */
COLOR bg;
// Track attributes (ATTR)
unsigned char attr;
/** reset flag / always send color and attributes */
// unsigned int reset : 1;
/** bold / bright flag */
// unsigned int bold : 1;
/** blink slow blinking text */
// unsigned int blink : 1;
/** inverse */
// unsigned int inverse : 1;
public:
ANSIColor();
constexpr ANSIColor(const char *text)
: fg{COLOR::WHITE}, bg{COLOR::BLACK}, attr{0} {
const char *cp = text;
bool use_on = false;
while (*cp != 0) {
long key = strhash(cp);
switch (key) {
case strhash("BRI"):
attr |= ATTR_BOLD;
break;
case strhash("BOL"):
attr |= ATTR_BOLD;
break;
case strhash("BLI"):
attr |= ATTR_BLINK;
break;
case strhash("INV"):
attr |= ATTR_INVERSE;
break;
case strhash("RES"):
attr |= ATTR_RESET;
break;
case strhash("ON "):
use_on = true;
break;
case strhash("BLU"):
if (use_on)
bg = COLOR::BLUE;
else
fg = COLOR::BLUE;
break;
case strhash("RED"):
if (use_on)
bg = COLOR::RED;
else
fg = COLOR::RED;
break;
case strhash("GRE"):
if (use_on)
bg = COLOR::GREEN;
else
fg = COLOR::GREEN;
break;
case strhash("YEL"):
if (use_on)
bg = COLOR::YELLOW;
else {
fg = COLOR::YELLOW;
attr |= ATTR_BOLD;
}
// if (use_on) throw error!
break;
case strhash("BRO"):
if (use_on)
bg = COLOR::BROWN;
else
fg = COLOR::BROWN;
break;
case strhash("CYA"):
if (use_on)
bg = COLOR::CYAN;
else
fg = COLOR::CYAN;
break;
case strhash("MAG"):
if (use_on)
bg = COLOR::MAGENTA;
else
fg = COLOR::MAGENTA;
break;
case strhash("BLA"):
if (use_on)
bg = COLOR::BLACK;
else
fg = COLOR::BLACK;
break;
case strhash("WHI"):
if (use_on)
bg = COLOR::WHITE;
else
fg = COLOR::WHITE;
break;
}
// skip to the space character
while ((*cp != ' ') && (*cp != 0)) ++cp;
// skip past the space character
while (*cp == ' ') ++cp;
}
};
ANSIColor(ATTR a);
ANSIColor(COLOR f);
ANSIColor(COLOR f, ATTR a);
ANSIColor(COLOR f, ATTR a1, ATTR a2);
ANSIColor(COLOR f, COLOR b);
ANSIColor(COLOR f, COLOR b, ATTR a);
ANSIColor(COLOR f, COLOR b, ATTR a1, ATTR a2);
ANSIColor &Attr(ATTR a);
bool operator==(const ANSIColor &c) const;
bool operator!=(const ANSIColor &c) const;
void setFg(COLOR f);
void setFg(COLOR f, ATTR a);
void setBg(COLOR b);
/**
* Get the foreground color
* @return COLOR
*/
COLOR getFg() { return fg; };
/**
* Get the background color
* @return COLOR
*/
COLOR getBg() { return bg; };
void setAttr(ATTR a);
std::string output(void) const;
std::string debug(void);
std::string output(ANSIColor &previous) const;
friend std::ostream &operator<<(std::ostream &os, const ANSIColor &c);
};
/**
* @class Door
*
* This handles output to the caller, via ostream.
*/
class Door : public std::ostream, private std::streambuf {
private:
std::streamsize xsputn(const char *s, std::streamsize n) override;
int overflow(int c) override;
/** The name used for logfile */
std::string doorname;
void parse_dropfile(const char *filepath);
void init(void);
std::time_t startup;
/** Initial terminal defaults. */
struct termios tio_default;
signed int getch(void);
signed int getkey_or_pushback(void);
/** Did we read a dropfile? */
bool has_dropfile;
bool debugging;
/** Name of the dropfile. */
std::string dropfilename;
/** Contents of the dropfile. */
vector<std::string> dropfilelines;
/** Logfile */
ofstream logf;
void detect_unicode_and_screen(void);
/** Allow us to stop the time_thread. */
std::promise<void> stop_thread;
/** Used by time_thread to know when a minute has passed. */
int seconds_elapsed;
void time_thread_run(std::future<void> future);
/** Thread used to update time_left and time_used. */
std::thread time_thread;
public:
Door(std::string dname, int argc, char *argv[]);
Door(Door &) = delete;
virtual ~Door();
ofstream &log(void);
/** Commandline options parser. */
AnyOption opt;
/** Buffer that holds the output for testing. */
std::string debug_buffer;
/**
* Previous ANSI-BBS colors and attributes sent.
* This is used to optimize ANSI Color output.
* \see ANSIColor::output()
*/
ANSIColor previous;
/** \todo Enable tracking cursor position. */
bool track;
/** \todo Current cursor X position. */
int cx;
/** \todo Current cursor Y position. */
int cy;
/** Detected screen width. \ref Door::detect_unicode_and_screen */
int width;
/** Detected screen height. */
int height;
/**
* @brief Number of seconds before timing out.
*
* When prompting for user input, this is the number of seconds they have to
* respond before we give up and timeout on them. (Default 120/2 minutes)
*/
int inactivity;
/** BBS Dropfile username */
std::string username;
/** BBS Dropfile handle */
std::string handle;
/** BBS Dropfile location */
std::string location;
/** BBS Dropfile sysop name */
std::string sysop;
// std::string bbsname;
/** BBS Dropfile node number */
int node;
/** time left in minutes */
atomic<int> time_left;
/** time used in minutes */
atomic<int> time_used;
signed int getkey(void);
bool haskey(void);
signed int sleep_key(int secs);
signed int sleep_ms_key(int msecs);
std::string input_string(int max);
int get_one_of(const char *keys);
};
// Use this to define the deprecated colorizer [POC]
// typedef std::function<void(Door &, std::string &)> colorFunction;
/**
* @class ColorOutput
* This works with \ref Render to create the output. This consists
* of ANSIColor and text position + length.
*
* @brief This holds an ANSIColor and text position + length
*
*/
class ColorOutput {
public:
ColorOutput();
void reset(void);
/// Color to use for this fragment
ANSIColor c;
/// Starting position of Render.text
int pos;
/// Length
int len;
};
/*
No, don't do this.
Instead, return an iterator/generator.
*/
/**
* @class Render
* This holds the string, and a vector that contains ColorOutput parts.
*
* @see Render::output()
*
* @brief Rendering a string with ANSIColor
*
*/
class Render {
/// Complete text to be rendered.
std::string text;
public:
Render(const std::string txt);
/// Vector of ColorOutput object.
std::vector<ColorOutput> outputs;
void append(ANSIColor color, int len = 1);
void output(std::ostream &os);
};
/**
* This defines the render output function. Given the line text, we output the
* color codes needs to display the line.
*
* ~~~{.cpp}
* door::ANSIColor upperColor, lowerColor;
*
* door::RenderFunction render = [upperColor, lowerColor]
* (const std::string &text) -> door::Render {
* door::Render r(text);
* for (char const &c : text) {
* if (std::isupper(c))
* r.append(upperColor);
* else
* r.append(lowerColor);
* }
* return r;
* };
* ~~~
*
* @brief Render output function
*
*/
typedef std::function<Render(const std::string &)> renderFunction;
/**
* This defines the update function.
*
* This updates the text.
*
* ~~~{.cpp}
* int score = 0;
*
* door::updateFunction updater = [](void) -> std::string {
* std::string text = "Score: ";
* text += std::to_string(score);
* return text;
* };
*
* fancyLine.setUpdater(updater);
* ~~~
*/
typedef std::function<std::string(void)> updateFunction;
/**
* @class Clrscr
* Clear the screen
* @brief Clear the screen
*/
class Clrscr {
public:
Clrscr(void);
friend std::ostream &operator<<(std::ostream &os, const Clrscr &clr);
};
/**
* Clear the BBS terminal.
*
*/
extern Clrscr cls;
/**
* @class NewLine
* Carriage return + Newline
* @brief CR+LF
*/
class NewLine {
public:
NewLine(void);
friend std::ostream &operator<<(std::ostream &os, const NewLine &nl);
};
/**
* CRLF
*/
extern NewLine nl;
/**
* This resets the colors to normal state.
*
* @brief reset colors to normal
*/
extern ANSIColor reset;
/// @deprecated Not used
enum class Justify { NONE, LEFT, RIGHT, CENTER };
/**
* @class Goto
* This handles outputting ANSI codes to position the cursor on the screen.
*
* @brief ANSI Goto X, Y position
*/
class Goto {
/// X-Position
int x;
/// Y-Position
int y;
public:
Goto(int xpos, int ypos);
/**
* Default Goto constructor copier
*/
Goto(const Goto &) = default;
void set(int xpos, int ypos);
friend std::ostream &operator<<(std::ostream &os, const Goto &g);
};
extern const char SaveCursor[];
extern const char RestoreCursor[];
#ifdef EXPERIMENTAL
/* should we try to derive a base class, so you can have multilines of
* multilines? */
class LineBase {
public:
virtual ~LineBase() = default;
virtual bool update(void) = 0;
// friend std::ostream &operator<<(std::ostream &os, const LineBase &lb) = 0;
};
class BasicLine {
protected:
std::string text;
bool hasColor;
ANSIColor color;
/// renderFunction to use when rendering Line.
renderFunction render;
/// updateFunction to use when updating.
updateFunction updater;
public:
BasicLine(std::string txt);
BasicLine(std::string txt, ANSIColor c);
BasicLine(const BasicLine &rhs) = default;
virtual ~BasicLine() = default;
bool hasRender(void);
void setText(std::string txt);
void setColor(ANSIColor c);
void setRender(renderFunction rf);
void setUpdater(updateFunction uf);
bool update(void);
friend std::ostream &operator<<(std::ostream &os, const BasicLine &l);
};
class MultiLine {
protected:
std::vector<std::shared_ptr<BasicLine>> lines;
public:
MultiLine();
void append(std::shared_ptr<BasicLine> bl);
bool update(void);
friend std::ostream &operator<<(std::ostream &os, const MultiLine &l);
};
#endif
/**
* @class Line
* This holds text and ANSIColor information, and knows how to
* send them out to the Door.
* @brief Text and ANSIColor
*/
class Line {
protected:
/// Text of the line
std::string text;
/// Do we have color?
bool hasColor;
/// Line color
ANSIColor color;
/// Padding characters
std::string padding;
/// Padding color
ANSIColor paddingColor;
/// renderFunction to use when rendering Line.
renderFunction render;
/// updateFunction to use when updating.
updateFunction updater;
int width;
/**
* @param width int
*/
// void makeWidth(int width);
public:
Line(const std::string &txt, int width = 0);
Line(const char *txt, int width = 0);
Line(const std::string &txt, int width, ANSIColor c);
Line(const char *txt, int width, ANSIColor c);
Line(const std::string &txt, int width, renderFunction rf);
Line(const char *txt, int width, renderFunction rf);
Line(const Line &rhs);
Line(Line &&rhs);
// ~Line();
bool hasRender(void);
int length(void); // const;
void fit(void);
/**
* @param padstring std::string &
* @param padColor ANSIColor
*/
void setPadding(std::string &padstring, ANSIColor padColor);
/**
* @param padstring const char *
* @param padColor ANSIColor
*/
void setPadding(const char *padstring, ANSIColor padcolor);
void setText(std::string &txt);
void setText(const char *txt);
const char *getText(void) { return text.c_str(); };
void setColor(ANSIColor c);
void setRender(renderFunction rf);
void setUpdater(updateFunction uf);
bool update(void);
std::string debug(void);
/**
* @todo This might be a problem, because const Line wouldn't
* allow me to track "updates". I.E. I send the line, I'd
* need to change the line's State to "nothing changed".
* Then, if something did change, the next update request would
* be able to know that yes, this does indeed need to be sent.
*
* @bug This also might cause problems if I display a shared
* BasicLine (in multiple places), and then update it. It
* would only update in the first place (the others wouldn't
* show it needs an update).
*/
friend std::ostream &operator<<(std::ostream &os, const Line &l);
};
/// Example BlueYellow renderFunction
extern renderFunction rBlueYellow;
/*
Progress Bar Styles:
solid chars.
half step chars.
gradient (1/4 %25, %50, %75) chars.
percentage (solid chars, percentage is XX% or 100)
percent_space (solid chars, percentage is " XX% " or " 100 ")
*/
enum class BarStyle { SOLID, HALF_STEP, GRADIENT, PERCENTAGE, PERCENT_SPACE };
/**
* BarColorRange
*
* vector<door::BarColorRange> colorRange = {
* {2500, door::ANSIColor(door::COLOR::RED)},
* {5000, door::ANSIColor(door::COLOR::BROWN)},
* {7500, door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD)},
* {9500, door::ANSIColor(door::COLOR::GREEN)},
* {10100, door::ANSIColor(door::COLOR::GREEN, door::ATTR::BOLD)}};
* BarLine.setColorRange(colorRange);
*
*/
struct BarColorRange {
unsigned long percent;
ANSIColor c;
};
class BarLine : public Line {
protected:
BarStyle barstyle;
unsigned long current_percent;
void init(void);
std::string update_bar(void);
int length;
vector<BarColorRange> colorRange;
public:
BarLine(int width);
BarLine(int width, BarStyle style);
BarLine(int width, BarStyle style, ANSIColor c);
void watch(float &percent);
void watch(int &value, int &max);
void setStyle(BarStyle s);
void set(int value, int max);
void set(float percent);
void set(unsigned long percent);
void setColorRange(vector<BarColorRange> bcr);
// friend std::ostream &operator<<(std::ostream &os, const BarLine &b);
};
/**
* The different Borders supported by Panel.
*
*/
enum class BorderStyle {
/// NONE (0)
NONE,
/// SINGLE (1)
SINGLE,
/// DOUBLE (2)
DOUBLE,
/// SINGLE top DOUBLE side (3)
SINGLE_DOUBLE,
/// DOUBLE top SINGLE side (4)
DOUBLE_SINGLE,
/// BLANK (5)
BLANK
};
class Panel {
protected:
int x;
int y;
int width; // or padding ?
BorderStyle border_style;
ANSIColor border_color;
/**
* @todo Fix this to use shared_ptr.
* I don't think unique_ptr is the right way to go with this. I want to reuse
* things, and that means shared_ptr!
*
*/
std::vector<std::unique_ptr<Line>> lines;
bool hidden;
// when you show panel, should it mark it as
// redisplay everything?? maybe??
bool shown_once; // ?? maybe shown_once_already ?
std::unique_ptr<Line> title;
int offset;
public:
Panel(int x, int y, int width);
Panel(int width);
// Panel(const Panel &);
Panel(Panel &) = delete; // default;
Panel(Panel &&ref);
void set(int x, int y);
void get(int &x, int &y) {
x = this->x;
y = this->y;
};
void setTitle(std::unique_ptr<Line> T, int off = 1);
void setStyle(BorderStyle bs);
void setColor(ANSIColor c);
int getWidth(void) { return width; };
int getHeight(void) {
if (border_style == BorderStyle::NONE)
return lines.size();
else
return lines.size() + 2;
};
void hide(void);
void show(void);
void addLine(std::unique_ptr<Line> l);
// bool delLine(std::shared_ptr<Line> l); // ?
/*
void display(void);
void update(void);
*/
/**
* @brief Updates a panel.
*
* returns True if something was changed (and cursor has moved)
* False, nothing to do, cursor is ok.
*
* @param d
* @return true
* @return false
*/
bool update(Door &d);
void update(Door &d, int line);
void update(void);
door::Goto gotoEnd(void);
std::unique_ptr<Line> spacer_line(bool single);
void lineSetBack(ANSIColor back);
friend std::ostream &operator<<(std::ostream &os, const Panel &p);
};
/*
Menu - defaults to double lines.
Has colorize for selected item / non-selected.
Arrow keys + ENTER, or keypress to select an item.
[O] Option Displayed Here
[ + ] = c1
O = c2
Remaining UC TEXT = c3
Remaining LC text = c4
// Colors for CS and CU (color selected, color unselected)
*/
class Menu : public Panel {
protected:
unsigned int chosen;
std::vector<char> options;
renderFunction selectedRender;
renderFunction unselectedRender;
/*
std::function<void(Door &d, std::string &)> selectedColorizer;
std::function<void(Door &d, std::string &)> unselectedColorizer;
*/
public:
static renderFunction defaultSelectedRender;
static renderFunction defaultUnselectedRender;
/*
static std::function<void(Door &d, std::string &)> defaultSelectedColorizer;
static std::function<void(Door &d, std::string &)> defaultUnselectedColorizer;
*/
Menu(int x, int y, int width);
Menu(int width);
// Menu(const Menu &);
Menu(const Menu &) = delete;
Menu(Menu &&);
void addSelection(char c, const char *line);
void addSelection(char c, const char *line, updateFunction update);
void defaultSelection(int d);
void setRender(bool selected, renderFunction render);
int choose(Door &door);
char which(int d);
static renderFunction makeRender(ANSIColor c1, ANSIColor c2, ANSIColor c3,
ANSIColor c4);
};
class Screen {
protected:
// bool hidden;
/**
* @brief vector of panels.
*/
std::vector<std::unique_ptr<Panel>> panels;
public:
Screen(void);
Screen(Screen &) = default;
void addPanel(std::unique_ptr<Panel> p);
/*
bool delPanel(std::shared_ptr<Panel> p);
void hide(void);
void show(void);
*/
bool update(Door &d);
void update(void);
friend std::ostream &operator<<(std::ostream &os, const Screen &s);
};
/*
screen - contains panels.
- default to 1,1 X 80,24
- refresh(style) could redraw panels by order they were added,
or could redraw panels from top to bottom, left to right.
crazy ideas:
hide panels / z-order
how to handle panel on top of other panels?
Can I have you win + show animated final score calculations?
panel - has X,Y and width, optional length. contains lines.
length could be simply number of "lines".
- has optional border. double/single/Ds/Sd TOPbottom
- has optional title.
- has optional footer.
addLine()
append() - Appends another line to current line.
set(X,Y) - set a "line" at a given X,Y position.
menu - another type of panel, contains menu options/lines.
lightmenu - like above, but allows arrow keys to select menu options.
line - contains text.
(Maybe a "dirty" flag is needed here?)
- has optional (width)
- has optional (justify - L, R, Center)
- has optional padding (# of blank chars)
- has color (of text)
- has formatter/coloring function (to colorize the text)
Example would be one that sets capital letters to one color, lower to another.
Another example would be one that displays Score: XXX, where Score is one
color, : is another, and XXX is yet another. Properly padded, of course.
- has "lambda" function to update the value? (Maybe?)
Idea would be that I could update the score, and panel.update(). It would
call all the line.update() functions and only update anything that has
changed.
Crazy ideas:
Can I delete a line, and have it automatically removed from a panel?
lightline - text, changes format/coloring if focus/nofocus is set?
*/
} // namespace door
#endif