-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.cpp
169 lines (145 loc) · 5.45 KB
/
menu.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
#include "libs/termkit/termkit.cpp"
#include <iostream>
#include <sys/types.h>
#include <vector>
const char *title =
R"(
██████╗███████╗██╗ ███████╗ █████╗ ████████╗ █████╗ ██████╗ █████╗ ███╗ ███╗███████╗
██╔════╝██╔════╝██║ ██╔════╝██╔══██╗╚══██╔══╝ ██╔══██╗ ██╔════╝ ██╔══██╗████╗ ████║██╔════╝
╚█████╗ █████╗ ██║ █████╗ ██║ ╚═╝ ██║ ███████║ ██║ ██╗ ███████║██╔████╔██║█████╗
╚═══██╗██╔══╝ ██║ ██╔══╝ ██║ ██╗ ██║ ██╔══██║ ██║ ╚██╗██╔══██║██║╚██╔╝██║██╔══╝
██████╔╝███████╗███████╗███████╗╚█████╔╝ ██║ ██║ ██║ ╚██████╔╝██║ ██║██║ ╚═╝ ██║███████╗
╚═════╝ ╚══════╝╚══════╝╚══════╝ ╚════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝
)";
enum Keys
{
CTRLC = 3,
TAB = 9,
ENTER = 13,
ESC = 27,
UP = 65,
DOWN = 66,
// Windows has different keycodes for up and down arrows
WINUP = 72,
WINDOWN = 80,
BAKTAB = 90,
BRACKET = 91
};
class MenuEntry
{
public:
void (*exec)();
std::string displayName;
MenuEntry(void (*callee)(), std::string name)
{
this->exec = callee;
this->displayName = name;
}
};
void displayTitle()
{
std::cout << termkit::center_text_block(termkit::rgb_fg(title, 255, 0, 0, true), 98)
<< termkit::DEFAULT_TERM_STYLE << std::endl;
std::cout << termkit::center_line(std::string(58, ' ') + termkit::bold_text("v0.0.1"))
<< std::endl;
std::cout << std::endl;
}
void renderSelectable(std::vector<MenuEntry> &games, unsigned short selected_option)
{
std::string selected_game_display_name = games[0].displayName;
for (unsigned long i = 0; i < games.size(); i++)
{
selected_game_display_name = games[i].displayName;
if (i == selected_option)
{
std::cout << termkit::center_line(termkit::bold_text(termkit::rgb_fg(selected_game_display_name, 255, 0, 0)), selected_game_display_name.length())
<< std::endl;
}
else
std::cout << termkit::center_line(selected_game_display_name, selected_game_display_name.length())
<< std::endl;
}
}
void incrementSelectedOption(unsigned short &selected_option, unsigned long number_of_games)
{
selected_option += 1;
selected_option %= number_of_games;
}
void decrementSelectedOption(unsigned short &selected_option, unsigned long number_of_games)
{
selected_option -= 1;
selected_option %= number_of_games;
}
extern void showMenu(std::vector<MenuEntry> games)
{
const unsigned long number_of_games = games.size();
const unsigned short terminal_height = termkit::get_term_size().height;
const unsigned short ui_vcenter_point = (12 + number_of_games) / 2;
// ^^
// Height of the logo + it's padding and version number
menuStart:
{
unsigned short selected_option = 0;
bool is_selecting = true;
termkit::clear();
termkit::hide_cursor();
termkit::set_term_title("SAE Games");
termkit::move_cursor_down(terminal_height / 2 - ui_vcenter_point);
displayTitle();
while (is_selecting)
{
// render
renderSelectable(games, selected_option);
termkit::move_cursor_up(games.size());
// keyboard input
switch (termkit::getch())
{
case CTRLC:
return;
case TAB:
incrementSelectedOption(selected_option, number_of_games);
continue;
case ENTER:
is_selecting = false;
break;
// handle multi-character keys '^[' e.i UP arrow is ^[A
// 27 91 A
#if defined(unix) || defined(__APPLE__) // linux and mac support
case ESC:
if (termkit::getch() != BRACKET)
continue;
/*
As said a couple of lines before, on UNIX-like systems arrow keys are composed of 3 characters, the first being the escape one (code 27 '^')
the second being an openning bracket (code 91 '[')
the last one being the character identifying the key
*/
char g = termkit::getch();
switch (g)
{
case UP:
decrementSelectedOption(selected_option, number_of_games);
continue;
case DOWN:
incrementSelectedOption(selected_option, number_of_games);
continue;
case BAKTAB:
decrementSelectedOption(selected_option, number_of_games);
continue;
}
// Windows support
#else
case WINUP:
decrementSelectedOption(selected_option, number_of_games);
continue;
case WINDOWN:
incrementSelectedOption(selected_option, number_of_games);
continue;
#endif
}
}
termkit::clear();
termkit::show_cursor();
games[selected_option].exec();
}
goto menuStart;
}