/
Router.cpp
118 lines (110 loc) · 2.12 KB
/
Router.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
#include "Router.h"
Router::Router(std::string value)
{
this->title = value;
}
Router::Router(handler func, std::string title_value)
{
title = title_value;
view = func;
}
std::string Router::getTitle()
{
return title;
}
int Router::getLevel()
{
return level;
}
bool Router::is_exit(int position)
{
return position == items.size() + (parent != NULL) ? 1 : 0;
}
bool Router::is_back(int position)
{
return position == items.size() && parent != NULL;
}
void Router::setActive(int* position)
{
if (is_exit(*position))
exit(0);
if (is_back(*position)){
if (parent != NULL)
parent->navigate();
*position = 0;
return;
}
Router* router = &(items[*position]);
router->run();
*position = 0;
router->navigate();
}
void Router::run()
{
system("CLS");
if (view != NULL)
{
view();
system("pause");
}
if (items.size() == 0)
parent->navigate();
}
void Router::setLevel(int value)
{
level = value;
}
void Router::addRoute(Router* router){
std::cout << "Add new router: " << router->getTitle() << std::endl;
router->parent = this;
std::cout << "Set router parent: " << router->parent->getTitle() << std::endl;
items.push_back(*router);
};
void Router::displayMenu(int position)
{
std::cout << "----- " << this->getTitle() << "----- " << std::endl;
int size = items.size();
for (int i = 0; i < size; i++)
std::cout << ((position == i) ? "> " : " ") << items[i].getTitle() << std::endl;
if (parent != NULL)
std::cout << (is_back(position) ? "> " : " ") << "Back." << std::endl;
std::cout << (is_exit(position) ? "> " : " ") << "Exit program." << std::endl;
}
int Router::getMenuSize() {
return items.size();
}
void Router::navigate()
{
system("CLS");
displayMenu(step);
int param = _getch();
switch (param)
{
case 72:
system("CLS");
step--;
if (step < 0)
if (parent != NULL)
step = getMenuSize() + 1;
else
step = getMenuSize();
navigate();
break;
case 80:
system("CLS");
step++;
if (step > getMenuSize() + (parent != NULL) ? 1 : 0)
step = 0;
navigate();
break;
case 224:
//system("CLS");
navigate();
break;
case 13:
setActive(&step);
break;
default:
navigate();
}
}