-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
175 lines (149 loc) · 5.48 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
const char buttonStyleSheet[] = "background: #8f7a66; color: white; border: none; border-radius: 3px; padding: 9px 12px;font-size: 14px;";
const char buttonStyleSheetDisabled[] = "background: #bdbdbd; color: white; border: none; border-radius: 3px; padding: 9px 12px;font-size: 14px;";
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
gameContainer(new GameContainer())
{
ui->setupUi(this);
gameContainer->setParent(ui->centralWidget);
gameContainer->setFixedSize(CONTAINER_WIDTH, CONTAINER_WIDTH);
ui->horizontalLayout->insertWidget(2, gameContainer);
ui->gameInfo->setFixedHeight(CONTAINER_WIDTH);
// 事件
connect(ui->exitButton, SIGNAL(clicked()), this, SLOT(handleClosed()));
connect(ui->newGameButton, SIGNAL(clicked()), this, SLOT(handleNewGameClicked()));
connect(ui->retractButton, SIGNAL(clicked()), this, SLOT(handleRetractClicked()));
connect(ui->winTile128Button, SIGNAL(clicked()), this, SLOT(handleWinTile128Clicked()));
connect(ui->winTile2048Button, SIGNAL(clicked()), this, SLOT(handleWinTile2048Clicked()));
connect(ui->eliminateRowButton, SIGNAL(clicked()), this, SLOT(handleEliminateRowClicked()));
connect(ui->eliminateColButton, SIGNAL(clicked()), this, SLOT(handleEliminateColClicked()));
connect(ui->soundEffectsVolumeSlider, SIGNAL(valueChanged(int)), this, SLOT(handleSoundEffectsVolumeChanged(int)));
connect(gameContainer, SIGNAL(soundEffectsVolumeChanged(int)), this, SLOT(handleSoundEffectsVolumeChanged(int)));
connect(gameContainer, SIGNAL(propRetractEnabled(bool)), this, SLOT(handlePropRetractEnabled(bool)));
connect(gameContainer, SIGNAL(propEliminateRowEnabled(int)), this, SLOT(handlePropEliminateRowEnabled(int)));
connect(gameContainer, SIGNAL(propEliminateColEnabled(int)), this, SLOT(handlePropEliminateColEnabled(int)));
connect(gameContainer, SIGNAL(bestScoreUpdated(int)), this, SLOT(handleBestScoreUpdated(int)));
connect(gameContainer, SIGNAL(scoreUpdated(int)), this, SLOT(handleScoreUpdated(int)));
connect(gameContainer, SIGNAL(rankingListUpdated(const std::string &)), this, SLOT(handleRankingListUpdated(const std::string &)));
connect(gameContainer, SIGNAL(currentStatusUpdated(const std::string &)), this, SLOT(handleCurrentStatusUpdated(const std::string &)));
handleSoundEffectsVolumeChanged(QMessageBox::question(this, "欢迎", "要打开游戏声音吗?") == QMessageBox::Yes ? 80 : 0);
gameContainer->startGame();
}
void MainWindow::handleClosed()
{
close();
}
void MainWindow::handleNewGameClicked()
{
gameContainer->newGame();
}
void MainWindow::handleRetractClicked()
{
gameContainer->retract();
}
void MainWindow::handleEliminateRowClicked()
{
gameContainer->eliminateRow();
}
void MainWindow::handleEliminateColClicked()
{
gameContainer->eliminateCol();
}
void MainWindow::handleScoreUpdated(int score)
{
std::ostringstream text;
text << score;
ui->score->setText(text.str().c_str());
}
void MainWindow::handleBestScoreUpdated(int score)
{
std::ostringstream text;
text << score;
ui->bestScore->setText(text.str().c_str());
}
void MainWindow::handleRankingListUpdated(const std::string &content)
{
ui->rankingList->setText(content.c_str());
}
void MainWindow::handleCurrentStatusUpdated(const std::string &content)
{
ui->currentStatus->setText(content.c_str());
}
void MainWindow::handleSoundEffectsVolumeChanged(int value)
{
std::ostringstream text;
text << value << '%';
ui->soundEffectsVolume->setText(text.str().c_str());
if (ui->soundEffectsVolumeSlider->value() != value)
ui->soundEffectsVolumeSlider->setValue(value);
gameContainer->setSoundEffectsVolume(value / 100.0);
}
void MainWindow::handlePropRetractEnabled(bool value)
{
ui->retractButton->setEnabled(value);
ui->retractButton->setStyleSheet(value ? buttonStyleSheet : buttonStyleSheetDisabled);
}
void MainWindow::handlePropEliminateRowEnabled(int value)
{
std::ostringstream text;
text << "消除最大行 × ";
if (value >= 0)
text << value;
else
text << "??";
ui->eliminateRowButton->setText(text.str().c_str());
ui->eliminateRowButton->setEnabled(value > 0);
ui->eliminateRowButton->setStyleSheet(value > 0 ? buttonStyleSheet : buttonStyleSheetDisabled);
}
void MainWindow::handlePropEliminateColEnabled(int value)
{
std::ostringstream text;
text << "消除最大列 × ";
if (value >= 0)
text << value;
else
text << "??";
ui->eliminateColButton->setText(text.str().c_str());
ui->eliminateColButton->setEnabled(value > 0);
ui->eliminateColButton->setStyleSheet(value > 0 ? buttonStyleSheet : buttonStyleSheetDisabled);
}
void MainWindow::handleWinTile128Clicked()
{
gameContainer->setWinTile(128);
}
void MainWindow::handleWinTile2048Clicked()
{
gameContainer->setWinTile(2048);
}
MainWindow::~MainWindow()
{
delete ui;
delete gameContainer;
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_W:
case Qt::Key_Up:
gameContainer->move(MOVE_UP);
break;
case Qt::Key_A:
case Qt::Key_Left:
gameContainer->move(MOVE_LEFT);
break;
case Qt::Key_S:
case Qt::Key_Down:
gameContainer->move(MOVE_DOWN);
break;
case Qt::Key_D:
case Qt::Key_Right:
gameContainer->move(MOVE_RIGHT);
break;
default:
return;
}
}