-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.cpp
More file actions
244 lines (191 loc) · 7.76 KB
/
display.cpp
File metadata and controls
244 lines (191 loc) · 7.76 KB
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
#include "display.h"
#include "games.h"
#include "settings.h"
#include "utils.h"
#include "logging.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <wincon/pdcwin.h>
#include <panel.h>
#include <QJsonArray>
#include <QJsonDocument>
#include <QDebug>
#include <QJsonObject>
namespace Pico{
namespace Display{
Pico::Logging::Funcs logging;
Funcs::Funcs(QObject* parent) :
QObject(parent)
{
initscr(); /// Initialize curses - mandatory
noecho(); /// Disables displaying user input on getch()
cbreak(); /// Enables instant input
nodelay(stdscr, true); /// Enables instant input
keypad(stdscr, TRUE); /// Detect function keys
start_color(); /// ENABLES COLORS
curs_set(0); /// HIDE CURSORS
PDC_set_title((Pico::Settings::HC_CONFIG.CLIENT_NAME).c_str()); /// SETS WINDOWS TITLE
init_pair(1, COLOR_BLACK, COLOR_WHITE); /// 1: STATUS window
for (int i =0; i < 4; i++){
windows[i] = newwin(0,0,0,0);
}
ResizeWindows();
///
/// PANELS
///
for (int i =0; i < 3; i++){
panels[i] = new_panel(windows[i]);
}
}
void Funcs::ResizeWindows(){
resize_term(0,0);
///
/// MAIN WINDOW
/// (displays game list, etc...)
///
windows[0] = resize_window(windows[0],LINES-2, COLS); /// As big as the terminal
mvwin(windows[0], 0,0);
///
/// COMMAND WINDOW
/// (the scanF place where you can ask instructions)
///
windows[1] = resize_window(windows[1], 3, COLS); /// Online 1 text line, but 3 real lines : so it can be boxed.
mvwin(windows[1], LINES - 4, 0);
///
/// STATUS WINDOW
/// (the scanF place where you can ask instructions)
///
windows[2] = resize_window(windows[2], 1, COLS);
wbkgdset(windows[2], COLOR_PAIR(1));
mvwin(windows[2], LINES -1, 0);
///
/// DOWNLOAD WINDOW
/// (Big thing; centered)
///
int dwWidth = 40;
int dwHeight = 30;
windows[3] = resize_window(windows[3], dwHeight, dwWidth);
mvwin(windows[3], LINES/2-dwHeight/2, COLS/2-dwWidth/2);
}
void Funcs::AddLine(std::string text){
lines.insert(lines.end(), text);
}
std::string Funcs::BoolToCheckbox(bool value){
if (value){
return "X";
}
return " ";
}
void Funcs::Display(){
ResizeWindows();
clear();
/// MAIN WINDOW
wclear(windows[0]);
box(windows[0], 0, 0);
int i = 1;
for (std::list<std::string>::iterator it =lines.begin(); it!=lines.end(); ++it){
mvwprintw(windows[0], i, 1, (*it).c_str());
i++;
}
lines.clear();
/// COMMAND ZONE
wclear(windows[1]);
box(windows[1], 0, 0);
mvwprintw(windows[1], 1, 1, std::string(command).c_str());
/// STATUS
wclear(windows[2]);
mvwprintw(windows[2], 0, 0, std::string("[status] "+status).c_str());
/// DOWNLOAD
wclear(windows[3]);
box(windows[3], 0, 0);
/* Show it on the screen */
update_panels();
doupdate();
}
void Funcs::RefreshCommand(std::string command){
/// COMMAND ZONE
wclear(windows[1]);
box(windows[1], 0, 0);
mvwprintw(windows[1], 1, 1, command.c_str());
wrefresh(windows[1]);
}
void Funcs::DisplayGameList(std::map<int, QJsonObject> *gamesMap){
int i = 0;
std::map<int, QJsonObject> fakeMap(*gamesMap);
int nameLength = 16;
int playersLength = 7;
int mapLength = 24;
int authorLength = 16;
int modLength = 16;
int passLength = 5;
std::string separator = " | ";
int totalLength =
nameLength
+playersLength
+mapLength
+authorLength
+modLength
+passLength
+ 6*separator.length(); //// Multiplying by the number of infos to display
std::string horizontalLine;
for (int j = 0; j < totalLength; j++){
horizontalLine += "=";
}
std::ostringstream headerStream;
headerStream
<< std::internal
<< Pico::Utils::makeLong("ID", 3)
<< separator << Pico::Utils::makeLong("NAME", nameLength)
<< separator << Pico::Utils::makeLong("PLAYERS", playersLength)
<< separator << Pico::Utils::makeLong("MAP", mapLength)
<< separator << Pico::Utils::makeLong("HOST", authorLength)
<< separator << Pico::Utils::makeLong("MOD", modLength)
<< separator << Pico::Utils::makeLong("PASS", passLength);
AddLine(headerStream.str());
AddLine(horizontalLine);
for (const auto& kv : fakeMap) {
i++;
QJsonObject data;
data = kv.second;
std::ostringstream gameLineStream;
std::string gameId = QString::number(i).toStdString();
std::string gameName = data.value("title").toString().toStdString();
std::string gamePlayers = QString::number(data.value("num_players").toInt()).toStdString() + "/" + QString::number(data.value("max_players").toInt()).toStdString();
std::string gameMap = data.value("mapname").toString().toStdString();
std::string gameAuthor = data.value("host").toString().toStdString();
std::string gameMod = data.value("featured_mod").toString().toStdString();
std::string gamePass = BoolToCheckbox(data.value("password_protected").toBool());
//std::string gameMod = std::to_string(LINES);
gameLineStream
<< std::internal
<< Pico::Utils::makeLong(gameId, 3)
<< separator << Pico::Utils::makeLong(gameName, nameLength)
<< separator << Pico::Utils::makeLong(gamePlayers, playersLength)
<< separator << Pico::Utils::makeLong(gameMap, mapLength)
<< separator << Pico::Utils::makeLong(gameAuthor, authorLength)
<< separator << Pico::Utils::makeLong(gameMod, modLength)
<< separator << Pico::Utils::makeLong(gamePass, passLength)
;
std::string gameLine(gameLineStream.str());
AddLine(gameLine);
}
if (i==0){
AddLine("Fetching games...");
}
Display();
}
void Funcs::DisplayDownloads(QVector<QNetworkReply *> currentDownloads){
}
////////////////
/// SLOTS
////////////////
void Funcs::OnStatusChange(QString newStatus){
status = newStatus.toStdString();
Display();
}
void Funcs::OnGameUpdate(std::map<int, QJsonObject> *gamesMap){
DisplayGameList(gamesMap);
}
}
}