-
Notifications
You must be signed in to change notification settings - Fork 1
/
UI.cpp
138 lines (120 loc) · 3.5 KB
/
UI.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
#include "UI.hpp"
#include <iostream>
#include <string.h>
#include <math.h>
#include "State.hpp"
UI::UI(std::vector<Car *> &cars, Workshop &workshop) : cars(cars), workshop(workshop)
{
initscr();
keypad(stdscr, TRUE);
curs_set(0);
cbreak();
noecho();
start_color();
use_default_colors();
init_pair(2, COLOR_GREEN, -1);
init_pair(3, COLOR_RED, -1);
initializeWindow();
refreshThread = std::make_unique<std::thread>(&UI::refreshView, this);
keyboardThread = std::make_unique<std::thread>(&UI::endVisualisation, this);
}
UI::~UI()
{
keyboardThread->join();
refreshThread->join();
destroyWindow(window);
endwin();
}
void UI::destroyWindow(WINDOW *window)
{
wborder(window, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wrefresh(window);
delwin(window);
}
void UI::initializeWindow()
{
window = newwin(LINES, COLS, 0, 0);
const std::string centerHeader = "Workshop with threads";
const std::string rightHeader = "Jakub Pawleniak 248897";
mvprintw(0, COLS - centerHeader.length(), centerHeader.c_str());
mvprintw(LINES - 1, COLS - rightHeader.length(), rightHeader.c_str());
const std::string warningLine1 = "CLICK [ENTER] TO START";
const int rowIndexw1 = LINES / 2;
const int colIndexw1 = (COLS - warningLine1.length()) / 2 - 1;
mvprintw(rowIndexw1, colIndexw1, warningLine1.c_str());
while (getch() != 10)
;
workshop.open();
}
void UI::refreshView()
{
clear();
while (workshop.getIsOpen())
{
clear();
refreshSpaces();
std::this_thread::sleep_for(std::chrono::milliseconds(150));
}
}
void UI::refreshSpaces()
{
for (int i = 0; i < workshop.getSpaces().size(); i++)
{
int sizeX = 17;
int sizeY = 10;
auto &space = workshop.getSpaces().at(i);
int x = space->getCarId();
WINDOW *subwindow = subwin(stdscr, sizeY, sizeX, 0, 5 + ((sizeX + 2) * i));
if (x < 0)
{
wattron(subwindow, COLOR_PAIR(2));
}
box(subwindow, 0, 0);
const std::string spaceName = "STANOWISKO " + std::to_string(i);
mvwprintw(subwindow, 0, 1, spaceName.c_str());
int m = space->getMechanicAmount();
for (int j = 0; j < m; j++)
{
mvwprintw(subwindow, 0, sizeX - 2 - j, "I");
}
if (x < 0)
{
wattroff(subwindow, COLOR_PAIR(2));
}
if (x >= 0)
{
mvwprintw(subwindow, sizeY - 3, 1, cars.at(x)->getName().c_str());
mvwprintw(subwindow, sizeY - 2, 1, cars.at(x)->getStateString().c_str());
int barLength = sizeX - 2;
float progress = cars.at(x)->getProgress();
std::string finString;
std::string progressInPercent = std::to_string((int)std::round(progress * 100));
int lpad = std::round(progress * barLength);
int rpad = barLength - lpad;
for (int k = 0; k < lpad; k++)
{
finString = finString + "#";
}
for (int k = 0; k < rpad; k++)
{
finString = finString + " ";
}
mvwprintw(subwindow, sizeY - 1, 1, finString.c_str());
}
wrefresh(subwindow);
}
// refresh();
}
void UI::endVisualisation()
{
while (workshop.getIsOpen())
{
int keyPressed = wgetch(window);
switch (keyPressed)
{
case 27: // ESCAPE KEY
workshop.close();
break;
}
}
}