-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseMenu.cpp
82 lines (81 loc) · 3.09 KB
/
baseMenu.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
#pragma once
#include "terminal.cpp"
#include <math.h>
using namespace std;
class baseMenu{
protected:
int titleHeight = 1,
state = 0,
cardWidth = 2, cardHeight = 2,
gridWidth = 1,
hMargin = 1, vMargin = 1,
hPad = 1, vPad = 1;
wstring title;
vector<vector<wstring>> cardsContent;
vector<wstring> emptyMenu;
void init(){
createCards();
int menuWidth = (cardWidth + hMargin) * gridWidth + hMargin;
emptyMenu.emplace_back(pad(title, menuWidth));
for (int i = 1; i < vMargin; i++) emptyMenu.emplace_back(repeat(L" ", menuWidth));
for (int i = 0, len = ceil(1.0 * cards.size() / gridWidth); i < len; i++){
for (auto j: row(i)) emptyMenu.emplace_back(j);
for (int i = 0; i < vMargin; i++) emptyMenu.emplace_back(repeat(L" ", menuWidth));
}
}
void setFinalState(){
while (1){
state = max(0, min(state, int(cardsContent.size() - 1)));
draw();
switch (terminal::getKey()){
case 'A': state -= gridWidth; break;
case 'B': state += gridWidth; break;
case 'C': state++; break;
case 'D': state--; break;
case ' ':
case '\r': return;
case 'x':
case 'c': terminal::exit();
}
}
}
private:
vector<vector<wstring>> cards;
wstring repeat(wstring s, int times){
wstring r;
while (times--) r += s;
return r;
}
wstring pad(wstring s, int width){
int padding = width - s.size();
return repeat(L" ", padding / 2) + s + repeat(L" ", ceil(padding / 2.0));
}
void draw(){
terminal::printBuffer = emptyMenu;
int left = hMargin + (state % gridWidth) * (cardWidth + hMargin), top = titleHeight + (state / gridWidth) * (cardHeight + vMargin);
terminal::highlightArea(left, left + cardWidth - 1, top, top + cardHeight - 1);
terminal::print();
}
void createCards(){
for (int j = 0, len = cardsContent.size(); j < len; j++){
vector<wstring> card;
card.emplace_back(L"╭" + repeat(L"─", cardWidth - 2) + L"╮");
for (int i = 0; i < vPad; i++) card.emplace_back(L"│" + repeat(L" ", cardWidth - 2) + L"│");
for (auto i: cardsContent[j])card.emplace_back(L"│" + pad(i, cardWidth - 2) + L"│");
for (int i = 0; i < vPad; i++) card.emplace_back(L"│" + repeat(L" ", cardWidth - 2) + L"│");
card.emplace_back(L"╰" + repeat(L"─", cardWidth - 2) + L"╯");
cards.emplace_back(card);
}
}
vector<wstring> row(int i){
vector<wstring> cardRow;
int firstCard = i * gridWidth, lastCard = min(firstCard + gridWidth, int(cardsContent.size()));
for (int i = 0; i < cardHeight; i++){
cardRow.emplace_back(repeat(L" ", hMargin));
for (int j = firstCard; j < lastCard; j++){
cardRow.back() += cards[j][i] + repeat(L" ", hMargin);
}
}
return cardRow;
}
};