-
Notifications
You must be signed in to change notification settings - Fork 0
/
UI.cpp
303 lines (254 loc) · 6.65 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "UI.h"
// Initializes all of the necessary windows
bool UserInterface::initialize(unsigned int width) {
// Sets up the UI. Returns false if the screen is the wrong size.
int scrY, scrX;
int xp;
int y, x;
int yl, xl;
getmaxyx(stdscr, scrY, scrX);
if (scrY < MINY || scrX < MINX)
return false;
// xp = scrX * 3 / 10;
xp = width;
y = scrY;
x = xp;
yl = 0;
xl = scrX - xp;
// Create the windows.
// b windows are borders only.
bmain = newwin(y, x, yl, xl);
// These are the side windows.
y -= 2;
x -= 4;
yl += 1;
xl += 2;
info = newwin(y, x, yl, xl);
inven = newwin(y, x, yl, xl);
// Popup window border.
float factor = 1.5;
y = scrY / factor;
x = (scrX - xp) / factor;
yl = (scrY - y) / 2;
xl = (scrX - xp - x) / 2;
bpop = newwin(y, x, yl, xl);
// Popup window.
y -= 2;
x -= 4;
yl += 1;
xl += 2;
pop = newwin(y, x, yl, xl);
// Add them to the panels.
pbmain = new_panel(bmain);
pinfo = new_panel(info);
pinven = new_panel(inven);
pbpop = new_panel(bpop);
ppop = new_panel(pop);
// Set borders.
wborder(bmain, BVER, BVER, BHOR, BHOR, BVER, BVER, BVER, BVER);
wborder(bpop, BVER, BVER, BHOR, BHOR, BVER, BVER, BVER, BVER);
// Hide inventory and popup.
hide_panel(pinven);
hide_panel(pbpop);
hide_panel(ppop);
// Refresh the screen.
update_panels();
doupdate();
return true;
}
///// Information window functions.
// Actions
void UserInterface::actions(std::vector<std::string> values) {
// Prints the actions a player can take at the current time.
int ypos = 0;
int xoffset = -3;
printTitle(info, ypos, xoffset, LABEL_Actions);
printDualCol(info, ++ypos, xoffset, values);
return;
}
// Tile inspect
void UserInterface::tileInspect(std::vector<std::string> values) {
// Prints the information of a grovenik and its occupant.
int ypos = getmaxy(info) / 2 - 2;
int xoffset = -1;
clearInspect();
printTitle(info, ypos, xoffset, LABEL_Inspect);
printDualCol(info, ++ypos, xoffset, values);
return;
}
// Clears the inspect tile portion of the info window
void UserInterface::clearInspect() {
int maxY;
int ypos;
maxY = getmaxy(info) - 3;
ypos = getmaxy(info) / 2 - 2;
while (ypos < maxY) {
wmove(info, ypos, 0);
wclrtoeol(info);
++ypos;
}
return;
}
// Whiffles & energy
void UserInterface::whifflesEnergy(int whiffles, int energy) {
// Prints the number of whiffles and energy of the hero.
std::vector<std::string> values;
int ypos, xoffset;
values = {std::to_string(whiffles), std::to_string(energy), LABEL_Whiffles,
LABEL_Energy};
ypos = getmaxy(info) - 3;
xoffset = 0;
printDualCol(info, ypos, xoffset, values);
return;
}
// Display inventory
char UserInterface::displayInventory(
std::vector<std::vector<std::string>> tools, bool getInput) {
char ch;
int ist;
int y, cy, cx;
int ts;
show_panel(pinven);
werase(inven);
ist = 0;
y = getmaxy(inven);
cy = 0;
ts = tools.size();
do {
// Print all the tools.
werase(inven);
for (int i = ist; i < ts; i++) {
printDualCol(inven, cy, 0, tools[i]);
getyx(inven, cy, cx);
cy += 2;
if (cy > y)
break;
}
cy = 0;
ch = getch();
// Scroll upwards.
if (ch == CTRL_UP) {
ist -= 1;
if (ist < 0)
ist = 0;
}
// Scroll downwards.
else if (ch == CTRL_DOWN) {
if (ist < ts)
ist++;
} else if (getInput && ch != CTRL_INV) {
break;
}
} while (ch != CTRL_INV);
hide_panel(pinven);
update_panels();
doupdate();
return ch;
}
//////// Popups
// Popup with single string
char UserInterface::popup(std::string message) {
char ch;
openPop();
printTitle(pop, 0, 0, message);
ch = getch();
closePop();
return ch;
}
// Popup with a string and an occupant
char UserInterface::popup(std::string message,
std::vector<std::string> values) {
char ch;
int ypos;
openPop();
// Print message
printTitle(pop, 0, 0, message);
// Print occupant details
ypos = getmaxy(pop) - values.size() / 2 - 1;
printDualCol(pop, ypos, 0, values);
// Wait for input
ch = getch();
closePop();
return ch;
}
// Popup with a string, obstacle, and list of tools
char UserInterface::popup(std::string message,
std::vector<std::string> obstacle,
std::vector<std::vector<std::string>> tools) {
char ch;
int ypos;
openPop();
// Print message
printTitle(pop, 0, 0, message);
// Print occupant
ypos = getmaxy(pop) - obstacle.size() / 2 - 1;
printDualCol(pop, ypos, 0, obstacle);
// Display inventory and get input.
ch = displayInventory(tools, true);
closePop();
return ch;
}
//////// Private functions
// Opens the popup and erases all text.
void UserInterface::openPop() {
show_panel(pbpop);
show_panel(ppop);
werase(pop);
return;
}
// Hides the popup and updates panels.
void UserInterface::closePop() {
hide_panel(pbpop);
hide_panel(ppop);
update_panels();
doupdate();
return;
}
// Prints a string in the width-center of the window
void UserInterface::printTitle(WINDOW *win, int ypos, int hor_offset,
std::string title) {
// Prints the title in the center.
int start = (getmaxx(win) - title.length()) / 2;
if (start < 0)
start = 0;
mvwaddstr(win, ypos, start, title.data());
return;
}
// Print dual columns of information
void UserInterface::printDualCol(
// The main method of printing information to the UI elements.
WINDOW *win, int ypos, int hor_offset, std::vector<std::string> values) {
int maxY, maxX;
int center, start;
int vcen;
std::string str;
// Find center of window and vector.
getmaxyx(win, maxY, maxX);
center = maxX / 2;
vcen = values.size() / 2;
// Prints through the vector.
for (int i = 0; i < vcen; i++) {
if (ypos >= maxY)
break;
// Clear the line of old text.
wmove(win, ypos, 0);
wclrtoeol(win);
// Create the string and center it correctly.
str = values[i] + " : " + values[vcen + i];
start = center - str.find_first_of(':') + hor_offset;
int s = str.length();
if (start < 0)
start = 0;
// Print the string.
mvwaddstr(win, ypos, start, str.data());
// If string is too wide, update ypos.
while (s > maxX) {
++ypos;
s -= maxX;
}
++ypos;
}
update_panels();
doupdate();
return;
}