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
|
#include "client.h"
#include "clicklabel.h"
#include "audio.h"
#include "util.h"
#include "pacman.pb.h"
extern "C" {
#include "enet/enet.h"
}
Client::Client()
: m_dialog(NULL), m_ambientMuted(false)
{
m_settings = new QSettings(qApp->organizationName(), qApp->applicationName(), this);
m_mainWidget = new MainWidget(this);
createMenu();
m_mainWidget->setAmbientMuted(m_ambientMuted);
setCentralWidget(m_mainWidget);
showConnectDialog();
}
void Client::createMenu()
{
QMenu *fileMenu = menuBar()->addMenu("&File");
bool sound = AudioManager::self()->isWorking();
bool muted = !sound || m_settings->value("muted", false).toBool();
AudioManager::self()->setMuted(muted);
/* toggle sound: corner icon */
ClickLabel *toggleSound = new ClickLabel("Toggle Sound", this);
toggleSound->setToolTip("Toggle Sound");
toggleSound->setFixedWidth(20);
toggleSound->setFixedHeight(16);
toggleSound->setAlignment(Qt::AlignBottom);
toggleSound->setPixmap(soundIcon(!muted));
if (sound)
{
connect(toggleSound, SIGNAL(clicked()), this, SLOT(toggleSound()));
connect(AudioManager::self(), SIGNAL(mutedChanged(bool)), this, SLOT(mutedChanged(bool)));
}
menuBar()->setCornerWidget(toggleSound);
/* toggle sound: menu */
QAction *toggleSoundAction = new QAction("&Sound", this);
toggleSoundAction->setToolTip("Toggle Sound");
toggleSoundAction->setCheckable(true);
toggleSoundAction->setChecked(!muted);
toggleSoundAction->setDisabled(!sound);
fileMenu->addAction(toggleSoundAction);
if (sound)
{
connect(toggleSoundAction, SIGNAL(triggered()), this, SLOT(toggleSound()));
connect(this, SIGNAL(setMuteActionsChecked(bool)), toggleSoundAction, SLOT(setChecked(bool)));
}
/* toggle ambient sound: menu */
m_ambientMuted = muted || m_settings->value("ambientMuted", false).toBool();
QAction *toggleAmbientAction = new QAction("&Ambient Sound", this);
toggleAmbientAction->setToolTip("Toggle Ambient Sound");
toggleAmbientAction->setCheckable(true);
toggleAmbientAction->setChecked(!m_ambientMuted);
toggleAmbientAction->setDisabled(muted);
fileMenu->addAction(toggleAmbientAction);
if (sound)
{
connect(toggleAmbientAction, SIGNAL(triggered(bool)), this, SLOT(enableAmbientSound(bool)));
connect(this, SIGNAL(setMuteActionsChecked(bool)), toggleAmbientAction, SLOT(setEnabled(bool)));
}
/* connect/disconnect entry */
fileMenu->addSeparator();
QAction *connectAction = new QAction("C&onnect", this);
fileMenu->addAction(connectAction);
connect(connectAction, SIGNAL(triggered()), this, SLOT(showConnectDialog()));
connect(m_mainWidget, SIGNAL(connected(bool)), connectAction, SLOT(setDisabled(bool)));
QAction *disconnectAction = new QAction("Di&sconnect", this);
disconnectAction->setDisabled(true);
fileMenu->addAction(disconnectAction);
connect(disconnectAction, SIGNAL(triggered()), m_mainWidget, SLOT(doDisconnect()));
connect(disconnectAction, SIGNAL(triggered()), m_mainWidget, SLOT(deleteGUI()));
connect(m_mainWidget, SIGNAL(connected(bool)), disconnectAction, SLOT(setEnabled(bool)));
/* exit entry */
fileMenu->addSeparator();
QAction *quitAction = new QAction("E&xit", this);
quitAction->setIcon(QIcon::fromTheme(QLatin1String("application-exit")));
fileMenu->addAction(quitAction);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
QAction *aboutAction= menuBar()->addAction("Ab&out");
connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
}
void Client::toggleSound()
{
if (!AudioManager::self()->isWorking())
return;
bool muted = !AudioManager::self()->isMuted();
AudioManager::self()->setMuted(muted);
/* mute ambient sound again if explicitly muted */
if (!muted && m_ambientMuted)
m_mainWidget->setAmbientMuted(true);
}
void Client::mutedChanged(bool muted)
{
ClickLabel *tmp = qobject_cast<ClickLabel *>(menuBar()->cornerWidget());
tmp->setPixmap(soundIcon(!muted));
m_settings->setValue("muted", muted);
emit setMuteActionsChecked(!muted);
}
void Client::enableAmbientSound(bool enabled)
{
if (!AudioManager::self()->isWorking())
return;
m_ambientMuted = !enabled;
m_mainWidget->setAmbientMuted(m_ambientMuted);
m_settings->setValue("ambientMuted", m_ambientMuted);
}
QPixmap Client::soundIcon(bool enabled) const
{
QImage img(enabled ? ":/soundon" : ":/soundoff");
img.setColor(1, menuBar()->palette().color(
enabled ? QPalette::Active : QPalette::Disabled,
QPalette::ButtonText).rgba());
return QPixmap::fromImage(img);
}
void Client::showAbout()
{
if (m_dialog != NULL)
delete m_dialog;
m_dialog = new QDialog(this);
m_dialog->setWindowTitle("About Pacman");
m_dialog->setWindowFlags(m_dialog->windowFlags() & ~Qt::WindowContextHelpButtonHint);
QGridLayout *layout = new QGridLayout(m_dialog);
layout->setSizeConstraint(QLayout::SetFixedSize);
QString actoricons;
for(int i = 0; Color::order[i] != Color::none; ++i)
actoricons += QString("<img src=\":/actor%1icon\"/>").arg(i + 1);
const QString text = QString(
"<h3>Multiplayer Pacman %1</h3>"
"Authors: H. Demel, B. Mallinger, M. Mausz, M. Racz<br/>"
"<br/>"
"Gameplay based on <a href=\"http://en.wikipedia.org/wiki/Pac-Man\">Pacman</a>"
", © <a href=\"http://www.namco.co.jp/\">Namco</a> 1980<br/>"
"<br/>"
"Developed using Qt %2 (%3 bit)<br/>")
.arg(actoricons, QLatin1String(QT_VERSION_STR), QString::number(QSysInfo::WordSize));
QLabel *label = new QLabel(text, m_dialog);
label->setWordWrap(true);
label->setOpenExternalLinks(true);
label->setTextInteractionFlags(Qt::TextBrowserInteraction);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, m_dialog);
QPushButton *closeButton = buttonBox->button(QDialogButtonBox::Close);
buttonBox->addButton(closeButton, QDialogButtonBox::ButtonRole(QDialogButtonBox::RejectRole | QDialogButtonBox::AcceptRole));
connect(buttonBox , SIGNAL(rejected()), m_dialog, SLOT(reject()));
layout->addWidget(label, 0, 1, 4, 4);
layout->addWidget(buttonBox, 4, 0, 1, 5);
m_dialog->show();
}
void Client::showConnectDialog()
{
if (m_dialog != NULL)
delete m_dialog;
m_dialog = new QDialog(this);
m_dialog->setModal(true);
m_dialog->setWindowTitle("Connect");
QGridLayout *layout = new QGridLayout(m_dialog);
layout->setSizeConstraint(QLayout::SetFixedSize);
QLabel *srvLabel = new QLabel("Server address:", m_dialog);
QComboBox *srv = new QComboBox(m_dialog);
srv->setEditable(true);
srv->setInsertPolicy(QComboBox::InsertAtTop);
QString defentry = QString("127.0.0.1:%1").arg(Constants::Networking::port);
QStringList srvlist = m_settings->value("srvlist", QStringList(defentry)).toStringList();
srv->addItems(srvlist);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
Qt::Horizontal, m_dialog);
QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
QPushButton *cancelButton = buttonBox->button(QDialogButtonBox::Cancel);
buttonBox->addButton(okButton, QDialogButtonBox::ButtonRole(QDialogButtonBox::AcceptRole));
buttonBox->addButton(cancelButton, QDialogButtonBox::ButtonRole(QDialogButtonBox::RejectRole));
connect(buttonBox, SIGNAL(rejected()), m_dialog, SLOT(reject()));
connect(buttonBox, SIGNAL(accepted()), this, SLOT(onAcceptConnectDialog()));
layout->addWidget(srvLabel, 0, 0);
layout->addWidget(srv, 0, 1);
layout->addWidget(buttonBox, 4, 0, 1, 5);
m_dialog->show();
}
void Client::onAcceptConnectDialog()
{
if (m_dialog == NULL)
return;
QGridLayout *layout = static_cast<QGridLayout *>(m_dialog->layout());
QComboBox *srv = static_cast<QComboBox *>(layout->itemAtPosition(0, 1)->widget());
QStringList selected = srv->currentText().split(':');
QString errormsg;
if (errormsg.isNull() && selected.count() < 2)
errormsg = "Invalid Syntax.\nUse: <address>:<port>";
/* port */
QString portstr = selected.takeLast();
bool ok;
unsigned int port = portstr.toUInt(&ok);
if (!ok || port < 1 || port > 65535)
errormsg = "Invalid port number. Must be between 1 and 65535";
/* address */
QString address = selected.join(":");
if (!errormsg.isNull())
{
QMessageBox::critical(this, "Error", errormsg);
return;
}
emit m_dialog->accept();
QStringList srvlist = m_settings->value("srvlist").toStringList();
srvlist.insert(0, srv->currentText());
srvlist.removeDuplicates();
while (srvlist.count() > 20)
srvlist.removeAt(20 - 1);
m_settings->setValue("srvlist", srvlist);
m_mainWidget->doConnect(address, port);
}
int main(int argc, char **argv)
{
Constants::server = false;
/* Verify that the version of the library that we linked against is
* compatible with the version of the headers we compiled against.
*/
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (enet_initialize () != 0)
{
qCritical() << "An error occurred while initializing ENet";
return EXIT_FAILURE;
}
Q_INIT_RESOURCE(pacman);
QApplication app(argc, argv, true);
app.setOrganizationName("TU Wien FOOP");
app.setApplicationName("Pacman Client");
app.setWindowIcon(QIcon(":/appicon"));
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
/* load sound */
AudioManager::self();
Client *client = new Client;
client->show();
client->setWindowTitle(app.applicationName());
int ret = app.exec();
/* delete client so that we'll disconnect from server before enet deinitialize */
delete client;
enet_deinitialize();
/* Delete all global objects allocated by libprotobuf */
google::protobuf::ShutdownProtobufLibrary();
return ret;
}
|