-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamewindow.cpp
468 lines (406 loc) · 14.3 KB
/
gamewindow.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/// GameWindow handles the UI side of gameplay. It registers users input, emits it
/// to the GameModel and then recieves an emit from the GameModel to update the users screen.
/// Written By: name'); DROP TABLE teams;-- ?
#include "gamewindow.h"
#include "gamemodel.h"
#include "ui_gamewindow.h"
#include <iostream>
using std::cout;
using std::endl;
/// \brief GameWindow::GameWindow
/// Constructor.
/// \param model
/// \param parent
GameWindow::GameWindow(GameModel &model, QWidget *parent) :
QWidget(parent),
ui(new Ui::GameWindow)
{
ui->setupUi(this);
ui->currentGuess->setFocus();
//Only visible at the end of a round
ui->winLabel->setVisible(false);
ui->winLabel->setEnabled(false);
ui->nextFlag->setVisible(false);
ui->nextFlag->setEnabled(false);
ui->flagAnimation->setVisible(false);
ui->guessButton->setStyleSheet("QPushButton {color: white; background-color: rgb(50,200,50);}" "QPushButton:pressed {background-color: rgb(150,255,150);}");
ui->nextFlag->setStyleSheet("QPushButton {color: white; background-color: rgb(50,50,200);}" "QPushButton:pressed {background-color: rgb(150,150,255);}");
//signal with string of guess connect to model
connect(this, &GameWindow::newGuess, &model, &GameModel::newGuessSlot);
connect(this, &GameWindow::newGame, &model, &GameModel::newGameStartedSlot);
connect(&model, &GameModel::sendUIGuessInfo, this, &GameWindow::receiveCurrentGuessInfo);
connect(&model, &GameModel::newCountryPicked, this, &GameWindow::setUIforNewCountry);
//When player guesses correct, change ui to reflect win
connect(&model, &GameModel::sendWin, this, &GameWindow::winScreen);
connect(&model, &GameModel::sendLoss, this, &GameWindow::lossScreen);
// Start new round after winning (Sam)
connect(this, &GameWindow::nextCountry, &model, &GameModel::playNextCountry);
//connect for typing and guesses
connect(&model, &GameModel::invalidGuess, this, &GameWindow::invalidGuessSlot);
connect(this, &GameWindow::userTypingAndNeedsSuggestions, &model, &GameModel::getSuggestionsForUserSlot);
connect(&model, &GameModel::newSuggestions, this, &GameWindow::addSuggestions);
//connect home button to main UI.
connect(ui->homeButton, &QPushButton::clicked, this, &GameWindow::backToHomeSlot);
connect(&model, &GameModel::sendFlagAnimation, this, &GameWindow::receiveFlagAnimation);
//shake timer when incorrect guess
connect(&shakeTimer, &QTimer::timeout, this, &GameWindow::shakeGuessBox);
//connect to help window
connect(ui->helpButton, &QPushButton::clicked, this, &GameWindow::openHelpWindow);
//connect from help back to game
connect(&gameWindowHelp, &GameWindowHelp::returnToGame, this, &GameWindow::openGameWindow);
//reset game after going home
connect(this, &GameWindow::resetGame, &model, &GameModel::resetRound);
connect(this, &GameWindow::resetGame, this, &GameWindow::clearHintsAndGuesses);
}
/// \brief GameWindow::~GameWindow
/// Destructor
GameWindow::~GameWindow()
{
delete ui;
}
/// \brief GameWindow::initNewGame
/// Rsets ui when new game is created. Send signal to reset model.
/// \param difficulty
void GameWindow::initNewGame(int difficulty)
{
ui->hintLabel1->setText("Hint 1:");
ui->hintLabel2->setText("Hint 2:");
ui->hintLabel3->setText("Hint 3:");
ui->hintLabel4->setText("Hint 4:");
ui->hintLabel5->setText("Hint 5:");
emit newGame(difficulty);
}
/// \brief GameWindow::makeWidgetsVisibleAndEnabled
/// Recursively make all widgets in the game window visible and enabled.
/// \param widget
void GameWindow::makeWidgetsVisibleAndEnabled(QWidget *widget)
{
// Set visibility and enable state for widget
widget->setVisible(true);
widget->setEnabled(true);
// Recursively iterate through child widgets and set visibility and enable state
const QObjectList &children = widget->children();
for (QObject *child : children) {
QWidget *childWidget = qobject_cast<QWidget *>(child);
if (childWidget) {
makeWidgetsVisibleAndEnabled(childWidget);
}
}
}
/// \brief GameWindow::on_currentGuess_returnPressed
void GameWindow::on_currentGuess_returnPressed()
{
userGuessed();
}
/// \brief GameWindow::on_guessButton_clicked
void GameWindow::on_guessButton_clicked()
{
userGuessed();
}
/// \brief GameWindow::userGuessed
/// Emits valid input to the model for handling.
/// Valid input is concidered to be text in currentGuess.
void GameWindow::userGuessed()
{
QString currentGuess ="";
if(ui->currentGuess->text()!="")
{
currentGuess = ui->currentGuess->text();
string guessStr = currentGuess.toStdString();
ui->currentGuess->setText("");
emit newGuess(guessStr);
}
}
/// \brief GameWindow::clearHintsAndGuesses
/// Empty the hints and guess history from the previous round.
void GameWindow::clearHintsAndGuesses()
{
// Clear guess history
ui->guessLine1->clear();
ui->guessLine2->clear();
ui->guessLine3->clear();
ui->guessLine4->clear();
ui->guessLine5->clear();
ui->distanceLine1->clear();
ui->distanceLine2->clear();
ui->distanceLine3->clear();
ui->distanceLine4->clear();
ui->distanceLine5->clear();
ui->arrowLabel1->setText("<-");
ui->arrowLabel2->setText("<-");
ui->arrowLabel3->setText("<-");
ui->arrowLabel4->setText("<-");
ui->arrowLabel5->setText("<-");
// Clear all hints
ui->hintLabel1->clear();
ui->hintLabel2->setText("Hint 2: ");
ui->hintLabel3->setText("Hint 3: ");
ui->hintLabel4->setText("Hint 4: ");
ui->hintLabel5->setText("Hint 5: ");
ui->currentGuess->setPlaceholderText("Guess a Country");
}
/// \brief GameWindow::setUIforNewCountry
/// Load the ui with the country flag and first hint
/// \param filepath
/// filepath to the country flag image being loaded to the ui
/// \param fact1
/// The first fact displayed as hint 1
void GameWindow::setUIforNewCountry(QString filepath, QString fact1)
{
//set flag
QPixmap flag(filepath);
//emit sendPixmapForAnimation(flag);
ui->flagImageLabel->setPixmap(flag.scaled(ui->flagImageLabel->size(), Qt::KeepAspectRatio,Qt::SmoothTransformation));
//set fact1
ui->hintLabel1->setText("Hint 1: " + fact1);
ui->scrollArea->setVisible(false);
//reset animation
ui->flagAnimation->timer.stop();
ui->flagAnimation->set();
}
/// \brief GameWindow::receiveCurrentGuessInfo
/// Slot triggered from a GameModel emit.
/// Parameters provide data needed to give feedback to the user.
/// \param guess
/// \param guessNum
/// \param distance
/// \param hints
/// \param arrowDirection
void GameWindow::receiveCurrentGuessInfo(std::string guess, int guessNum, double distance, std::vector<QString> hints, std::string arrowDirection)
{
ui->currentGuess->setPlaceholderText("Guess a Country");
//calculate roation of arrow and size of pixmap
int angle = getRotationAngle(arrowDirection);
int scale;
if(angle % 90 == 0 )
{
scale = 24;
}
else
{
scale = 32;
}
QTransform rotationAngle;
rotationAngle.rotate(angle);
QPixmap arrow(":/new/prefix1/arrowImage.png");
arrow = arrow.transformed(rotationAngle);
//text based updates for whichever guess they are on.
QString guessStr = QString::fromStdString(guess);
QString distanceStr = QString::number(distance);
if(guessNum == 0)
{
ui->hintLabel2->setText("Hint 2: " + hints[1]); //A hint is already displayed before first guess
ui->guessLine1->setText(guessStr);
ui->distanceLine1->setText(distanceStr + " Miles");
ui->arrowLabel1->setPixmap(arrow.scaled(scale,scale, Qt::KeepAspectRatio,Qt::SmoothTransformation)); ///ui->arrowLabel1->size()
}
else if(guessNum == 1)
{
ui->hintLabel3->setText("Hint 3: " + hints[2]);
ui->guessLine2->setText(guessStr);
ui->distanceLine2->setText(distanceStr + " Miles");
ui->arrowLabel2->setPixmap(arrow.scaled(scale,scale, Qt::KeepAspectRatio,Qt::SmoothTransformation));
}
else if(guessNum == 2)
{
ui->hintLabel4->setText("Hint 4: " + hints[3]);
ui->guessLine3->setText(guessStr);
ui->distanceLine3->setText(distanceStr + " Miles");
ui->arrowLabel3->setPixmap(arrow.scaled(scale,scale, Qt::KeepAspectRatio,Qt::SmoothTransformation));
}
else if(guessNum == 3)
{
ui->hintLabel5->setText("Hint 5: " + hints[4]); //last hint because its their last guess next
ui->guessLine4->setText(guessStr);
ui->distanceLine4->setText(distanceStr + " Miles");
ui->arrowLabel4->setPixmap(arrow.scaled(scale,scale, Qt::KeepAspectRatio,Qt::SmoothTransformation));
}
else if(guessNum == 4)
{
ui->guessLine5->setText(guessStr);
ui->distanceLine5->setText(distanceStr + " Miles");
ui->arrowLabel5->setPixmap(arrow.scaled(scale,scale, Qt::KeepAspectRatio,Qt::SmoothTransformation));
}
}
///\brief returns angle to rotate pixmap to point in the right direction of the country to guess
///\param string direction. "east" "southwest" etc.
int GameWindow::getRotationAngle(std::string dir){
int angle = 0;
if(dir =="north"){
angle = 0;
}
else if(dir == "northwest"){
angle = 45;
}
else if(dir == "west"){
angle = 90;
}
else if(dir == "southwest"){
angle = 135;
}
else if(dir == "south"){
angle = 180;
}
else if(dir == "southeast"){
angle = 225;
}
else if(dir == "east"){
angle = 270;
}
else{ //northeast
angle = 315;
}
return angle;
}
/// \brief GameWindow::winScreen
/// Display a `you win` screen when the correct country is guessed.
void GameWindow::winScreen(QString country)
{
ui->winLabel->setVisible(true);
ui->nextFlag->setVisible(true);
ui->nextFlag->setEnabled(true);
ui->topLabel->setText(country);
ui->guessButton->setEnabled(false);
ui->currentGuess->setEnabled(false);
}
/// \brief GameWindow::lossScreen
/// Display a `you lose` screen when the wrong country is guessed.
void GameWindow::lossScreen(QString country)
{
ui->winLabel->setText("You Lost!");
ui->winLabel->setVisible(true);
ui->nextFlag->setVisible(true);
ui->nextFlag->setEnabled(true);
ui->topLabel->setText("Country: " + country);
ui->guessButton->setEnabled(false);
ui->currentGuess->setEnabled(false);
}
/// \brief GameWindow::on_nextFlag_clicked
/// Start the game again with the next flag.
void GameWindow::on_nextFlag_clicked()
{
hideWinScreen();
clearHintsAndGuesses();
emit nextCountry();
}
///\brief when guess is not a country don't count guess and shake box?
void GameWindow::invalidGuessSlot()
{
////shake the text box because invalid country guess
ui->currentGuess->setPlaceholderText("Invalid Guess!");
left = false;
shakeCount = 0;
shakeTimer.start(50);
}
/// \brief GameWindow::shakeGuessBox
void GameWindow::shakeGuessBox(){
if(shakeCount>=5){
shakeTimer.stop();
ui->currentGuess->setGeometry(340,500,481,21);
shakeCount = 0;
left = false;
}
else{
if(left){ //if box on left
ui->currentGuess->setGeometry(343,500,481,21);
left = false;
}
else{
ui->currentGuess->setGeometry(337,500,481,21);
left = true;
}
shakeCount++;
}
}
/// \brief GameWindow::hideWinScreen
/// Hide visibility of the win screen and make the game window visible again.
void GameWindow::hideWinScreen()
{
ui->winLabel->setText("You Win!");
ui->winLabel->setVisible(false);
ui->nextFlag->setVisible(false);
ui->nextFlag->setEnabled(false);
ui->topLabel->setText("Guess the country!");
ui->guessButton->setEnabled(true);
ui->currentGuess->setEnabled(true);
ui->currentGuess->setFocus();
ui->flagImageLabel->setVisible(true);
ui->flagAnimation->setVisible(false);
}
///\brief When user is typing. look at current string and give user suggestions
void GameWindow::on_currentGuess_textChanged(const QString &arg1)
{
string input = arg1.QString::toStdString();
emit userTypingAndNeedsSuggestions(input);
}
/// \brief GameWindow::addSuggestions
/// Adds suggestions to the suggList.
/// \param suggestions
void GameWindow::addSuggestions(std::vector<string> suggestions)
{
ui->scrollArea->setVisible(true);
ui->suggList->clear();
//put new suggestions in
for(int i = 0; i<suggestions.size(); i++){
QString suggestionStr = QString::fromStdString(suggestions[i]);
QListWidgetItem *suggItem = new QListWidgetItem(suggestionStr);
ui->suggList->addItem(suggItem);
}
//set height for scroll area
int sugCount = 0;
if(ui->suggList->count() > 3){
sugCount = 3;
}
else{
sugCount = ui->suggList->count();
}
int scrollBoxHeight = sugCount * 19;
QRect sizeOfBox(340,518,481,scrollBoxHeight);
// ui->suggList->setGeometry(sizeOfBox);
ui->scrollArea->setGeometry(sizeOfBox);
}
/// \brief GameWindow::on_suggList_itemClicked
/// When the user clicks a suggested item then current guess becomes the selected country.
/// \param item
void GameWindow::on_suggList_itemClicked(QListWidgetItem *item)
{
QString countryName = item->text();
ui->currentGuess->setText(countryName);
ui->currentGuess->setFocus();
}
/// \brief GameWindow::backToHomeSlot
/// Translates a button click to an emit.
void GameWindow::backToHomeSlot()
{
emit resetGame();
emit backToHome();
}
/// \brief GameWindow::receiveFlagAnimation
///
/// \param filepath
void GameWindow::receiveFlagAnimation(QString filepath, int event){
QImage image(filepath);
QImage re = image.scaledToHeight(ui->flagImageLabel->height());
re = re.scaledToWidth(ui->flagImageLabel->width());
ui->flagAnimation->image = re;
ui->flagAnimation->count = 1;
ui->flagAnimation->size = 1;
ui->flagAnimation->event = event;
ui->flagImageLabel->setVisible(false);
ui->flagAnimation->setVisible(true);
ui->flagAnimation->timer.start(10);
}
/// \brief GameWindow::openHelpWindow
/// Open the game window help.
void GameWindow::openHelpWindow()
{
gameWindowHelp.show();
this->close();
}
/// \brief GameWindow::openGameWindow
/// shows game window hides help window
void GameWindow::openGameWindow()
{
this->show();
gameWindowHelp.hide();
}