-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleWindow.cpp
335 lines (293 loc) · 9.65 KB
/
ConsoleWindow.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
#include "ConsoleWindow.h"
#include <QKeyEvent>
#include <QPainter>
#include <QHBoxLayout>
#include <QFontDatabase>
#include <QTimer>
#include <QFrame>
#include <QMenu>
#include <QMenuBar>
#include <iostream>
#include <QString>
// --- ConsoleWindow::ConsoleWidget
std::unordered_map<char, QString> icons = {
{'|', "v-wall.png"},
{'g', "stupid_ghost.png"},
{'G', "smart_ghost.png"},
{'t', "right-upper-corner.png"},
{'*', "pacman.png"},
{'.', "dot.png"},
{'p', "pill.png"},
{'J', "right-lower-corner.png"},
{'T', "left-upper-corner.png"},
{'L', "left-lower-corner.png"},
{'-', "h-wall.png"},
{'X', "brick.png"},
};
/// convertes qt keyboard event to ascii code (using special values of class ConsoleWindow)
char convert_2_ascii(QKeyEvent *event)
{
if (event->key() == Qt::Key_Up)
return ConsoleWindow::CURSOR_UP;
if (event->key() == Qt::Key_Down)
return ConsoleWindow::CURSOR_DOWN;
if (event->key() == Qt::Key_Left)
return ConsoleWindow::CURSOR_LEFT;
if (event->key() == Qt::Key_Right)
return ConsoleWindow::CURSOR_RIGHT;
const QString &str = event->text();
if (str.count() <= 0)
return 0;
char result = str[0].toLatin1();
if (result < 32)
return 0;
if (result > 126)
return 0;
return result;
}
void ConsoleWindow::ConsoleWidget::keyPressEvent(QKeyEvent *event)
{
if (event->isAutoRepeat())
return;
lastKey = convert_2_ascii(event);
event->accept();
onKeyPress();
}
void ConsoleWindow::ConsoleWidget::keyReleaseEvent(QKeyEvent *event)
{
if (event->isAutoRepeat())
return;
lastKey = 0;
std::ignore = event;
event->accept();
}
void ConsoleWindow::ConsoleWidget::paintEvent(QPaintEvent *event)
{
std::ignore = event;
QPainter painter(this);
QRect r = geometry();
r.setX(0);
r.setY(0);
painter.fillRect(r, QColor(0, 0, 0));
QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
fixedFont.setPixelSize(int(charSize));
painter.setFont(fixedFont); // QFont("Consolas", int(charSize))
painter.setPen(Qt::white);
QFontMetrics metrics(fixedFont);
int offset = metrics.ascent(); // vertical distance to baseline - for placement in y-direction
bool bottomCorner;
bool leftCorner;
for (unsigned y = 0; y < height; y++) {
for (unsigned x = 0; x < width; x++) {
//if (iconBuffer[x + width * y] == ' '){
painter.drawText(int(x * charSize),
int(y * charSize) + offset,
QString(textBuffer[x + width * y]));
//}
if (iconBuffer[x + width * y] != ' '){
if (iconBuffer[(x + width * y)] == '+'){
bottomCorner = (iconBuffer[(x + width * y)+width] != '|');
leftCorner = (iconBuffer[(x + width * y) -1] != '-');
if (bottomCorner && leftCorner){painter.drawPixmap(int(x * charSize),int(y * charSize) , QPixmap(icons['L']));}
else if (bottomCorner && !leftCorner){painter.drawPixmap(int(x * charSize),int(y * charSize) , QPixmap(icons['J']));}
else if (!bottomCorner && leftCorner){painter.drawPixmap(int(x * charSize),int(y * charSize) , QPixmap(icons['T']));}
else if (!bottomCorner && !leftCorner){painter.drawPixmap(int(x * charSize),int(y * charSize) , QPixmap(icons['t']));}
}
else {
painter.drawPixmap(int(x * charSize),
int(y * charSize ) , QPixmap(icons[iconBuffer[(x + width * y) ]]));}
}
}
}
}
ConsoleWindow::ConsoleWidget::ConsoleWidget(std::function<void()> onKeyPressFunc,
QWidget *parent,
unsigned width,
unsigned height,
unsigned charSize)
: QWidget(parent), // parent constructor
textBuffer(width * height,
' '),iconBuffer(width * height,
' '), // textBuffer constructor - std::vector<char>(initial_size, initial_value)
onKeyPress(onKeyPressFunc)
{
this->width = width;
this->height = height;
this->charSize = charSize;
setMinimumWidth(int(width * charSize));
setMaximumWidth(int(width * charSize));
setMinimumHeight(int(height * charSize));
setMaximumHeight(int(height * charSize));
lastKey = 0;
setFocusPolicy(Qt::StrongFocus);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
}
void ConsoleWindow::ConsoleWidget::setCharSize(unsigned charSize)
{
this->charSize = charSize;
setMinimumWidth(int(width * charSize));
setMaximumWidth(int(width * charSize));
setMinimumHeight(int(height * charSize));
setMaximumHeight(int(height * charSize));
update();
}
void ConsoleWindow::ConsoleWidget::setCharacter(int x, int y, char character)
{
if (x < 0 || x >= int(width) || y < 0 || y >= int(height)) {
return;
}
if (character < ' ' || character > 126) {
character = ' ';
}
textBuffer[static_cast<size_t>(x + int(width) * y)] = character;
}
void ConsoleWindow::ConsoleWidget::setIcon(int x, int y, char character)
{
if (x < 0 || x >= int(width) || y < 0 || y >= int(height)) {
return;
}
if (character < ' ' || character > 126) {
character = ' ';
}
iconBuffer[static_cast<size_t>(x + int(width) * y)] = character;
}
char ConsoleWindow::ConsoleWidget::getCharacter(int x, int y)
{
if (x < 0 || x >= int(width) || y < 0 || y >= int(height))
return 0;
return textBuffer[static_cast<size_t>(x + int(width) * y)];
}
char ConsoleWindow::ConsoleWidget::getIcon(int x, int y)
{
if (x < 0 || x >= int(width) || y < 0 || y >= int(height))
return 0;
return iconBuffer[static_cast<size_t>(x + int(width) * y)];
}
// --- ConsoleWindow
void ConsoleWindow::refreshTimerTimeout()
{
onRefresh();
update();
}
void ConsoleWindow::onExit()
{
close();
}
void ConsoleWindow::onSmallFont()
{
console->setCharSize(8);
centralWidget()->adjustSize();
adjustSize();
}
void ConsoleWindow::onMediumFont()
{
console->setCharSize(16);
centralWidget()->adjustSize();
adjustSize();
}
void ConsoleWindow::onLargeFont()
{
console->setCharSize(32);
centralWidget()->adjustSize();
adjustSize();
}
void ConsoleWindow::keyPressNotification()
{
onKeyPress();
}
ConsoleWindow::ConsoleWindow(QWidget *parent, unsigned width, unsigned height) : QMainWindow(parent)
{
QFrame *frame = new QFrame(this);
frame->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
setCentralWidget(frame);
console = new ConsoleWidget(std::bind(&ConsoleWindow::keyPressNotification, this), frame, width, height);
QHBoxLayout *layout = new QHBoxLayout(frame);
layout->setMargin(0);
layout->setSpacing(0);
layout->addWidget(console);
layout->setSizeConstraint(QLayout::SetFixedSize);
refreshTimer = new QTimer(this);
refreshTimer->setInterval(50);
refreshTimer->setSingleShot(false);
QObject::connect(refreshTimer, SIGNAL(timeout()), this, SLOT(refreshTimerTimeout()));
refreshTimer->start();
console->setFocus();
fileMenu = menuBar()->addMenu("&File");
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
exitAct->setStatusTip("close the window");
connect(exitAct, &QAction::triggered, this, &ConsoleWindow::onExit);
fileMenu->addAction(exitAct);
viewMenu = menuBar()->addMenu("&View");
smallAct = new QAction(tr("&Small Font"), this);
smallAct->setStatusTip("use 8px font");
connect(smallAct, &QAction::triggered, this, &ConsoleWindow::onSmallFont);
viewMenu->addAction(smallAct);
mediumAct = new QAction(tr("&Medium Font"), this);
mediumAct->setStatusTip("use 16px font");
connect(mediumAct, &QAction::triggered, this, &ConsoleWindow::onMediumFont);
viewMenu->addAction(mediumAct);
largeAct = new QAction(tr("&Large Font"), this);
largeAct->setStatusTip("use 32px font");
connect(largeAct, &QAction::triggered, this, &ConsoleWindow::onLargeFont);
viewMenu->addAction(largeAct);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
}
void ConsoleWindow::setCharacter(int x, int y, char character)
{
console->setCharacter(x, y, character);
}
void ConsoleWindow::setIcon(int x, int y, char character)
{
console->setIcon(x, y, character);
}
char ConsoleWindow::getCharacter(int x, int y)
{
return console->getCharacter(x, y);
}
char ConsoleWindow::getIcon(int x, int y)
{
return console->getIcon(x, y);
}
void ConsoleWindow::writeString(int x, int y, std::string text)
{
int l = int(text.size());
for (int i = 0; i < l; i++) {
console->setCharacter(x + i, y, text[size_t(i)]);
}
}
void ConsoleWindow::writeIcons(int x, int y, std::string text)
{
int l = int(text.size());
for (int i = 0; i < l; i++) {
console->setIcon(x + i, y, text[size_t(i)]);
}
}
void ConsoleWindow::clear(char character)
{
int width = int(getWidth());
int height = int(getHeight());
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
console->setCharacter(x, y, character);
}
}
}
void ConsoleWindow::clearIcons()
{
int width = int(getWidth());
int height = int(getHeight());
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
console->setIcon(x, y, ' ');
}
}
}
char ConsoleWindow::getPressedKey()
{
return console->getPressedKey();
}
ConsoleWindow::~ConsoleWindow()
{
delete console;
}