forked from voodooattack/ADWIF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurses.cpp
349 lines (313 loc) · 9.19 KB
/
curses.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* Copyright (c) 2013, Abdullah A. Hassan <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "curses.hpp"
#include "adwif.hpp"
#include "util.hpp"
#include "map.hpp"
#include "game.hpp"
#include <ncursesw/curses.h>
#include <signal.h>
#include <memory.h>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <boost/algorithm/string.hpp>
namespace ADWIF
{
void reset(int code)
{
endwin();
initscr();
nonl();
cbreak();
noecho();
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
curs_set(FALSE);
set_escdelay(10);
start_color();
use_default_colors();
assume_default_colors(COLOR_WHITE, COLOR_BLACK);
idlok(stdscr, TRUE);
clear();
refresh();
}
bool CursesRenderer::init()
{
signal(SIGWINCH, reset);
reset(0);
if (has_colors() == FALSE)
{
endwin();
std::cout << "Your terminal does not support colour output." << std::endl;
return false;
}
else
{
if (height() < 24 || width() < 80)
{
endwin();
std::cout << "A minimum terminal size of 80x24 is required to play this game." << std::endl;
return false;
}
else
{
myColourMap.resize(COLOR_PAIRS);
return true;
}
}
myWindow = nullptr;
}
void CursesRenderer::shutdown()
{
endWindow();
endwin();
}
int CursesRenderer::width() const
{
return COLS;
}
int CursesRenderer::height() const
{
return LINES;
}
WINDOW * CursesRenderer::win() { return doClip ? myWindow : stdscr; }
int CursesRenderer::getPair(Colour fg, Colour bg)
{
int pair = -1;
for (int i = 0; i < COLOR_PAIRS; i++)
{
short c1, c2;
pair_content(i, &c1, &c2);
if (c1 == fg && c2 == bg)
{
pair = i;
break;
}
}
if (pair == -1)
{
for (unsigned int i = 1; i < myColourMap.size(); i++)
if (!myColourMap[i])
{
pair = i;
init_pair(pair, fg, bg);
myColourMap[i] = true;
break;
}
}
if (pair == -1)
pair = 0;
return pair;
}
void CursesRenderer::style(Colour fg, Colour bg, int styleMask)
{
int pair = getPair(fg, bg);
styleMask |= A_COLOR;
wattr_set(win(), styleMask, pair, NULL);
}
void CursesRenderer::style(int x, int y, int len, Colour fg, Colour bg, int styleMask)
{
int pair = getPair(fg, bg);
styleMask |= A_COLOR;
mvwchgat(win(), y, x, len, styleMask, pair, NULL);
}
void CursesRenderer::startWindow(int x, int y, int w, int h)
{
if (myWindow)
endWindow();
myWindow = newwin(h, w, y, x);
idlok(myWindow, TRUE);
doClip = true;
}
void CursesRenderer::endWindow()
{
if (myWindow)
{
wnoutrefresh(myWindow);
delwin(myWindow);
}
myWindow = nullptr;
doClip = false;
}
void CursesRenderer::drawChar(int x, int y, int c)
{
mvwaddnwstr(win(), y, x, (const wchar_t*) &c, 1);
}
void CursesRenderer::drawText(int x, int y, const std::string & text)
{
mvwaddnstr(win(), y, x, text.c_str(), text.size());
}
void CursesRenderer::drawMessage(const std::string & message)
{
std::string text = wrapText(message, this->width() / 2);
std::vector<std::string> msgBuf;
boost::split(msgBuf, text, boost::is_any_of("\n"));
unsigned int width = 0;
for (auto & s : msgBuf)
width = (s.size() > width ? s.size() : width);
width += 2;
int msgHeight = msgBuf.size();
int height = 2 + msgHeight;
startWindow(this->width() / 2 - width / 2,
this->height() / 2 - height / 2, width, height);
style(Colour::White, Colour::Black, Style::Normal);
wborder(win(), 0,0,0,0,0,0,0,0);
mvwaddstr(win(), 1, 1, text.c_str());
endWindow();
}
void CursesRenderer::clear()
{
werase(win());
clearok(win(), TRUE);
myColourMap.assign(COLOR_PAIRS, false);
}
void CursesRenderer::refresh() { doupdate(); }
bool CursesRenderer::resized() const
{
bool r = myResizedFlag;
if (r) myResizedFlag = false;
return r;
}
int CursesInput::key()
{
int ch = getch();
myRenderer->myResizedFlag = ch == KEY_RESIZE;
return ch == KEY_MOUSE || ch == KEY_RESIZE ? -1 : ch;
}
std::string CursesInput::prompt(const std::string & text, unsigned int maxLen, const std::string & suffix)
{
std::vector<std::string> msgBuf = split(text, '\n');
unsigned int width = maxLen + (suffix.empty() ? 0 : suffix.size() + 2);
for(auto & s : msgBuf)
width = (s.size() > width ? s.size() : width);
width = (width > maxLen ? width : maxLen) + 4;
int msgHeight = msgBuf.size();
int height = 3 + msgHeight;
char result[maxLen+1];
std::memset(result, 0, maxLen + 1);
myRenderer->startWindow(myRenderer->width() / 2 - width / 2,
myRenderer->height() / 2 - height / 2, width, height);
echo();
cbreak();
curs_set(1);
timeout(-1);
wborder(myRenderer->win(), 0,0,0,0,0,0,0,0);
int yoff = 1;
for(auto & s : msgBuf)
myRenderer->drawText(2, yoff++, s);
if (!suffix.empty())
myRenderer->drawText(maxLen + 4, msgHeight+1, suffix);
myRenderer->refresh();
wmove(myRenderer->win(), msgHeight+1, 2);
wgetnstr(myRenderer->win(), result, maxLen);
reset(0);
timeout(0);
myRenderer->endWindow();
return result;
}
bool CursesInput::promptYn(const std::string & text, bool caseSensetive)
{
int width = text.size() + 4;
int height = 3;
bool result = false;
myRenderer->startWindow(myRenderer->width() / 2 - width / 2,
myRenderer->height() / 2 - height / 2, width, height);
wborder(myRenderer->win(), 0,0,0,0,0,0,0,0);
myRenderer->drawText(2, 1, text);
timeout(-1);
myRenderer->refresh();
while (1)
{
int c = wgetch(myRenderer->win());
if (c == 'Y' || (!caseSensetive && c == 'y'))
{
result = true;
break;
}
else if (c == 'N' || c == Key::Escape || (!caseSensetive && c == 'n'))
{
result = false;
break;
}
}
timeout(0);
myRenderer->endWindow();
return result;
}
int CursesInput::getTimeout() const
{
return myTimeout;
}
void CursesInput::setTimeout(int timeout)
{
::timeout(timeout);
myTimeout = timeout;
}
namespace Style
{
extern const int Normal = A_COLOR;
extern const int Bold = A_BOLD;
extern const int Underline = A_UNDERLINE;
extern const int Dim = A_DIM;
extern const int Dark = A_DIM;
extern const int StandOut = A_STANDOUT;
extern const int AltCharSet = A_ALTCHARSET;
};
namespace Key
{
extern const int Escape = 27;
extern const int Break = KEY_BREAK;
extern const int Backspace = KEY_BACKSPACE;
extern const int Enter = KEY_ENTER;
extern const int Up = KEY_UP;
extern const int Down = KEY_DOWN;
extern const int Left = KEY_LEFT;
extern const int Right = KEY_RIGHT;
extern const int Num7 = KEY_A1;
extern const int Num8 = '8';
extern const int Num9 = KEY_A3;
extern const int Num4 = '4';
extern const int Num5 = KEY_B2;
extern const int Num6 = '6';
extern const int Num1 = KEY_C1;
extern const int Num2 = '2';
extern const int Num3 = KEY_C3;
extern const int Num0 = KEY_LL;
extern const int Insert = KEY_IL;
extern const int Home = KEY_HOME;
extern const int PgUp = KEY_PPAGE;
extern const int Del = KEY_DL;
extern const int End = KEY_END;
extern const int PgDn = KEY_NPAGE;
extern const int F1 = KEY_F(1);
extern const int F2 = KEY_F(2);
extern const int F3 = KEY_F(3);
extern const int F4 = KEY_F(4);
extern const int F5 = KEY_F(5);
extern const int F6 = KEY_F(6);
extern const int F7 = KEY_F(7);
extern const int F8 = KEY_F(8);
extern const int F9 = KEY_F(9);
extern const int F10 = KEY_F(10);
extern const int F11 = KEY_F(11);
extern const int F12 = KEY_F(12);
}
}