-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
115 lines (92 loc) · 3.02 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QTime>
#include <QKeyEvent>
#include <QtDebug>
#include <QPainter>
#include "numberdisplay.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
dialog = new StartGameDialog(this);
connect(dialog, SIGNAL(setDifficulty(char)), this, SLOT(setDifficulty(char)));
smile = setSmile("smile");
deadSmile = setSmile("dead_smile");
shockedSmile = setSmile("shocked_smile");
coolSmile = setSmile("cool_smile");
this->ui->smileButton->setIcon(this->smile);
this->ui->smileButton->setIconSize(this->ui->smileButton->size());
flagDisplay = new NumberDisplay(this);
timerDisplay = new NumberDisplay(this);
connect(this, SIGNAL(updateFlagCount(int)), flagDisplay, SLOT(setNum(int)));
connect(this, SIGNAL(updateTimer(int)), timerDisplay, SLOT(setNum(int)));
ui->numberDisplayLayout->addWidget(flagDisplay);
ui->numberDisplayLayout->addWidget(timerDisplay);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerTick()));
}
QIcon MainWindow::setSmile(QString smileName) {
QPixmap smile = QPixmap(":/smiles/" + smileName + ".png");
return QIcon(smile);
}
MainWindow::~MainWindow()
{
delete ui;
delete timer;
}
void MainWindow::setDifficulty(char diff) {
if (field) {
field->close();
timerCount = 0;
tickTimer = false;
emit updateTimer(0);
}
field = new Field(this, diff);
connect(field, SIGNAL(sendFlagCount(int)), this, SLOT(setFlagCount(int)));
connect(field, SIGNAL(startGameTimer(bool)), this, SLOT(timerTick(bool)));
connect(field, SIGNAL(resizeInLayout(int,int)), this, SLOT(updateFieldHeight(int,int)));
connect(field, SIGNAL(updateGameStatus(char)), this, SLOT(gameOver(char)));
connect(field, SIGNAL(toggleSmile(bool)), this, SLOT(toggleSmile(bool)));
ui->fieldLayout->addWidget(field);
setFlagCount(field->getFlagCount());
}
void MainWindow::updateFieldHeight(int width, int height) {
if (field) {
this->ui->fieldLayout->geometry().setX(width);
this->ui->fieldLayout->geometry().setY(height);
}
}
void MainWindow::timerTick(bool fromField) {
if(fromField || tickTimer) {
emit updateTimer(timerCount++);
timer->start(1000);
tickTimer = true;
}
}
void MainWindow::on_smileButton_clicked(){
this->dialog->show();
this->ui->smileButton->setIcon(this->smile);
}
void MainWindow::gameOver(char gameStatus) {
if(gameStatus == 'w') {
this->ui->smileButton->setIcon(this->coolSmile);
}
else if(gameStatus == 'l') {
this->ui->smileButton->setIcon(this->deadSmile);
}
timer->stop();
}
void MainWindow::setFlagCount(int flagCount) {
emit updateFlagCount(flagCount);
}
void MainWindow::toggleSmile(bool shocked){
if(shocked) {
this->ui->smileButton->setIcon(this->shockedSmile);
}
else {
this->ui->smileButton->setIcon(this->smile);
}
}