diff options
| author | manuel <manuel@mausz.at> | 2011-05-05 00:57:07 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2011-05-05 00:57:07 +0200 |
| commit | ce48af53646cd9e7ec762fc1ac176b3aa620b11d (patch) | |
| tree | f8fbf2cae8c7d0cbac2696a8f4cf94410bfb4928 /pacman-c++/client/client.cpp | |
| parent | e54ccad07e256ba877bd41d70bd358bd0085bd1e (diff) | |
| download | foop-ce48af53646cd9e7ec762fc1ac176b3aa620b11d.tar.gz foop-ce48af53646cd9e7ec762fc1ac176b3aa620b11d.tar.bz2 foop-ce48af53646cd9e7ec762fc1ac176b3aa620b11d.zip | |
- refactorized the whole project and made a few subprojects
- replaced tcp with enet
- added connect dialog
- some smaller bugfixes
Diffstat (limited to 'pacman-c++/client/client.cpp')
| -rw-r--r-- | pacman-c++/client/client.cpp | 265 |
1 files changed, 265 insertions, 0 deletions
diff --git a/pacman-c++/client/client.cpp b/pacman-c++/client/client.cpp new file mode 100644 index 0000000..d064d9e --- /dev/null +++ b/pacman-c++/client/client.cpp | |||
| @@ -0,0 +1,265 @@ | |||
| 1 | #include "client.h" | ||
| 2 | #include "clicklabel.h" | ||
| 3 | #include "audio.h" | ||
| 4 | #include "util.h" | ||
| 5 | #include "pacman.pb.h" | ||
| 6 | |||
| 7 | extern "C" { | ||
| 8 | #include "enet/enet.h" | ||
| 9 | } | ||
| 10 | |||
| 11 | Client::Client() | ||
| 12 | : m_ambientMuted(false) | ||
| 13 | { | ||
| 14 | m_settings = new QSettings(qApp->organizationName(), qApp->applicationName(), this); | ||
| 15 | m_dialog = new QDialog(this); | ||
| 16 | m_mainWidget = new MainWidget(this); | ||
| 17 | createMenu(); | ||
| 18 | m_mainWidget->setAmbientMuted(m_ambientMuted); | ||
| 19 | setCentralWidget(m_mainWidget); | ||
| 20 | showConnectDialog(); | ||
| 21 | m_dialog->setFocus(); | ||
| 22 | } | ||
| 23 | |||
| 24 | void Client::createMenu() | ||
| 25 | { | ||
| 26 | QMenu *fileMenu = menuBar()->addMenu("&File"); | ||
| 27 | |||
| 28 | bool sound = AudioManager::self()->isWorking(); | ||
| 29 | bool muted = !sound || m_settings->value("muted", false).toBool(); | ||
| 30 | AudioManager::self()->setMuted(muted); | ||
| 31 | |||
| 32 | /* toggle sound: corner icon */ | ||
| 33 | ClickLabel *toggleSound = new ClickLabel("Toggle Sound", this); | ||
| 34 | toggleSound->setToolTip("Toggle Sound"); | ||
| 35 | toggleSound->setFixedWidth(20); | ||
| 36 | toggleSound->setFixedHeight(16); | ||
| 37 | toggleSound->setAlignment(Qt::AlignBottom); | ||
| 38 | toggleSound->setPixmap(soundIcon(!muted)); | ||
| 39 | if (sound) | ||
| 40 | { | ||
| 41 | connect(toggleSound, SIGNAL(clicked()), this, SLOT(toggleSound())); | ||
| 42 | connect(AudioManager::self(), SIGNAL(mutedChanged(bool)), this, SLOT(mutedChanged(bool))); | ||
| 43 | } | ||
| 44 | menuBar()->setCornerWidget(toggleSound); | ||
| 45 | |||
| 46 | /* toggle sound: menu */ | ||
| 47 | QAction *toggleSoundAction = new QAction("&Sound", this); | ||
| 48 | toggleSoundAction->setToolTip("Toggle Sound"); | ||
| 49 | toggleSoundAction->setCheckable(true); | ||
| 50 | toggleSoundAction->setChecked(!muted); | ||
| 51 | toggleSoundAction->setDisabled(!sound); | ||
| 52 | fileMenu->addAction(toggleSoundAction); | ||
| 53 | if (sound) | ||
| 54 | { | ||
| 55 | connect(toggleSoundAction, SIGNAL(triggered()), this, SLOT(toggleSound())); | ||
| 56 | connect(this, SIGNAL(setMuteActionsChecked(bool)), toggleSoundAction, SLOT(setChecked(bool))); | ||
| 57 | } | ||
| 58 | |||
| 59 | /* toggle ambient sound: menu */ | ||
| 60 | m_ambientMuted = muted || m_settings->value("ambientMuted", false).toBool(); | ||
| 61 | QAction *toggleAmbientAction = new QAction("&Ambient Sound", this); | ||
| 62 | toggleAmbientAction->setToolTip("Toggle Ambient Sound"); | ||
| 63 | toggleAmbientAction->setCheckable(true); | ||
| 64 | toggleAmbientAction->setChecked(!m_ambientMuted); | ||
| 65 | toggleAmbientAction->setDisabled(muted); | ||
| 66 | fileMenu->addAction(toggleAmbientAction); | ||
| 67 | if (sound) | ||
| 68 | { | ||
| 69 | connect(toggleAmbientAction, SIGNAL(triggered(bool)), this, SLOT(enableAmbientSound(bool))); | ||
| 70 | connect(this, SIGNAL(setMuteActionsChecked(bool)), toggleAmbientAction, SLOT(setEnabled(bool))); | ||
| 71 | } | ||
| 72 | |||
| 73 | /* connect/disconnect entry */ | ||
| 74 | fileMenu->addSeparator(); | ||
| 75 | QAction *connectAction = new QAction("C&onnect", this); | ||
| 76 | fileMenu->addAction(connectAction); | ||
| 77 | connect(connectAction, SIGNAL(triggered()), this, SLOT(showConnectDialog())); | ||
| 78 | connect(m_mainWidget, SIGNAL(connected(bool)), connectAction, SLOT(setDisabled(bool))); | ||
| 79 | |||
| 80 | QAction *disconnectAction = new QAction("Di&sconnect", this); | ||
| 81 | disconnectAction->setDisabled(true); | ||
| 82 | fileMenu->addAction(disconnectAction); | ||
| 83 | connect(disconnectAction, SIGNAL(triggered()), m_mainWidget, SLOT(doDisconnect())); | ||
| 84 | connect(m_mainWidget, SIGNAL(connected(bool)), disconnectAction, SLOT(setEnabled(bool))); | ||
| 85 | |||
| 86 | /* exit entry */ | ||
| 87 | fileMenu->addSeparator(); | ||
| 88 | QAction *quitAction = new QAction("E&xit", this); | ||
| 89 | quitAction->setIcon(QIcon::fromTheme(QLatin1String("application-exit"))); | ||
| 90 | fileMenu->addAction(quitAction); | ||
| 91 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); | ||
| 92 | |||
| 93 | QAction *aboutAction= menuBar()->addAction("Ab&out"); | ||
| 94 | connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout())); | ||
| 95 | } | ||
| 96 | |||
| 97 | void Client::toggleSound() | ||
| 98 | { | ||
| 99 | if (!AudioManager::self()->isWorking()) | ||
| 100 | return; | ||
| 101 | bool muted = !AudioManager::self()->isMuted(); | ||
| 102 | AudioManager::self()->setMuted(muted); | ||
| 103 | /* mute ambient sound again if explicitly muted */ | ||
| 104 | if (!muted && m_ambientMuted) | ||
| 105 | m_mainWidget->setAmbientMuted(true); | ||
| 106 | } | ||
| 107 | |||
| 108 | void Client::mutedChanged(bool muted) | ||
| 109 | { | ||
| 110 | ClickLabel *tmp = qobject_cast<ClickLabel *>(menuBar()->cornerWidget()); | ||
| 111 | tmp->setPixmap(soundIcon(!muted)); | ||
| 112 | m_settings->setValue("muted", muted); | ||
| 113 | emit setMuteActionsChecked(!muted); | ||
| 114 | } | ||
| 115 | |||
| 116 | void Client::enableAmbientSound(bool enabled) | ||
| 117 | { | ||
| 118 | if (!AudioManager::self()->isWorking()) | ||
| 119 | return; | ||
| 120 | m_ambientMuted = !enabled; | ||
| 121 | m_mainWidget->setAmbientMuted(m_ambientMuted); | ||
| 122 | m_settings->setValue("ambientMuted", m_ambientMuted); | ||
| 123 | } | ||
| 124 | |||
| 125 | QPixmap Client::soundIcon(bool enabled) const | ||
| 126 | { | ||
| 127 | QImage img(enabled ? ":/soundon" : ":/soundoff"); | ||
| 128 | img.setColor(1, menuBar()->palette().color( | ||
| 129 | enabled ? QPalette::Active : QPalette::Disabled, | ||
| 130 | QPalette::ButtonText).rgba()); | ||
| 131 | return QPixmap::fromImage(img); | ||
| 132 | } | ||
| 133 | |||
| 134 | void Client::showAbout() | ||
| 135 | { | ||
| 136 | if (m_dialog != NULL) | ||
| 137 | { | ||
| 138 | delete m_dialog; | ||
| 139 | m_dialog = new QDialog(this); | ||
| 140 | } | ||
| 141 | m_dialog->setWindowTitle("About Pacman"); | ||
| 142 | m_dialog->setWindowFlags(m_dialog->windowFlags() & ~Qt::WindowContextHelpButtonHint); | ||
| 143 | |||
| 144 | QGridLayout *layout = new QGridLayout(m_dialog); | ||
| 145 | layout->setSizeConstraint(QLayout::SetFixedSize); | ||
| 146 | |||
| 147 | QString actoricons; | ||
| 148 | for(int i = 0; Color::order[i] != Color::none; ++i) | ||
| 149 | actoricons += QString("<img src=\":/actor%1icon\"/>").arg(i + 1); | ||
| 150 | |||
| 151 | const QString text = QString( | ||
| 152 | "<h3>Multiplayer Pacman %1</h3>" | ||
| 153 | "Authors: H. Demel, B. Mallinger, M. Mausz, M. Racz<br/>" | ||
| 154 | "<br/>" | ||
| 155 | "Gameplay based on <a href=\"http://en.wikipedia.org/wiki/Pac-Man\">Pacman</a>" | ||
| 156 | ", © <a href=\"http://www.namco.co.jp/\">Namco</a> 1980<br/>" | ||
| 157 | "<br/>" | ||
| 158 | "Developed using Qt %2 (%3 bit)<br/>") | ||
| 159 | .arg(actoricons, QLatin1String(QT_VERSION_STR), QString::number(QSysInfo::WordSize)); | ||
| 160 | |||
| 161 | QLabel *label = new QLabel(text, m_dialog); | ||
| 162 | label->setWordWrap(true); | ||
| 163 | label->setOpenExternalLinks(true); | ||
| 164 | label->setTextInteractionFlags(Qt::TextBrowserInteraction); | ||
| 165 | |||
| 166 | QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, m_dialog); | ||
| 167 | QPushButton *closeButton = buttonBox->button(QDialogButtonBox::Close); | ||
| 168 | buttonBox->addButton(closeButton, QDialogButtonBox::ButtonRole(QDialogButtonBox::RejectRole | QDialogButtonBox::AcceptRole)); | ||
| 169 | connect(buttonBox , SIGNAL(rejected()), m_dialog, SLOT(reject())); | ||
| 170 | |||
| 171 | layout->addWidget(label, 0, 1, 4, 4); | ||
| 172 | layout->addWidget(buttonBox, 4, 0, 1, 5); | ||
| 173 | m_dialog->show(); | ||
| 174 | } | ||
| 175 | |||
| 176 | void Client::showConnectDialog() | ||
| 177 | { | ||
| 178 | if (m_dialog != NULL) | ||
| 179 | { | ||
| 180 | delete m_dialog; | ||
| 181 | m_dialog = new QDialog(this); | ||
| 182 | } | ||
| 183 | m_dialog->setWindowTitle("Connect"); | ||
| 184 | |||
| 185 | QGridLayout *layout = new QGridLayout(m_dialog); | ||
| 186 | layout->setSizeConstraint(QLayout::SetFixedSize); | ||
| 187 | |||
| 188 | QLabel *srvLabel = new QLabel("Address:", m_dialog); | ||
| 189 | QLineEdit *srv = new QLineEdit(m_settings->value("address", "127.0.0.1").toString(), m_dialog); | ||
| 190 | QLabel *portLabel = new QLabel("Port:", m_dialog); | ||
| 191 | QDoubleSpinBox *port = new QDoubleSpinBox(m_dialog); | ||
| 192 | port->setDecimals(0); | ||
| 193 | port->setMinimum(1); | ||
| 194 | port->setMaximum(65535); | ||
| 195 | port->setValue(m_settings->value("port", Constants::Networking::port).toUInt()); | ||
| 196 | |||
| 197 | QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, | ||
| 198 | Qt::Horizontal, m_dialog); | ||
| 199 | QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); | ||
| 200 | QPushButton *cancelButton = buttonBox->button(QDialogButtonBox::Cancel); | ||
| 201 | buttonBox->addButton(okButton, QDialogButtonBox::ButtonRole(QDialogButtonBox::AcceptRole)); | ||
| 202 | buttonBox->addButton(cancelButton, QDialogButtonBox::ButtonRole(QDialogButtonBox::RejectRole)); | ||
| 203 | connect(buttonBox, SIGNAL(rejected()), m_dialog, SLOT(reject())); | ||
| 204 | connect(buttonBox, SIGNAL(accepted()), m_dialog, SLOT(accept())); | ||
| 205 | |||
| 206 | layout->addWidget(srvLabel, 0, 0); | ||
| 207 | layout->addWidget(srv, 0, 1); | ||
| 208 | layout->addWidget(portLabel, 1, 0); | ||
| 209 | layout->addWidget(port, 1, 1); | ||
| 210 | layout->addWidget(buttonBox, 4, 0, 1, 5); | ||
| 211 | |||
| 212 | connect(m_dialog, SIGNAL(accepted()), this, SLOT(onAcceptConnectDialog())); | ||
| 213 | m_dialog->show(); | ||
| 214 | } | ||
| 215 | |||
| 216 | void Client::onAcceptConnectDialog() | ||
| 217 | { | ||
| 218 | if (m_dialog == NULL) | ||
| 219 | return; | ||
| 220 | QGridLayout *layout = static_cast<QGridLayout *>(m_dialog->layout()); | ||
| 221 | QLineEdit *srv = static_cast<QLineEdit *>(layout->itemAtPosition(0, 1)->widget()); | ||
| 222 | QDoubleSpinBox *port = static_cast<QDoubleSpinBox *>(layout->itemAtPosition(1, 1)->widget()); | ||
| 223 | m_settings->setValue("address", srv->text()); | ||
| 224 | m_settings->setValue("port", int(port->value())); | ||
| 225 | m_mainWidget->doConnect(srv->text(), int(port->value())); | ||
| 226 | } | ||
| 227 | |||
| 228 | bool Constants::server = false; | ||
| 229 | |||
| 230 | int main(int argc, char **argv) | ||
| 231 | { | ||
| 232 | /* Verify that the version of the library that we linked against is | ||
| 233 | * compatible with the version of the headers we compiled against. | ||
| 234 | */ | ||
| 235 | GOOGLE_PROTOBUF_VERIFY_VERSION; | ||
| 236 | |||
| 237 | if (enet_initialize () != 0) | ||
| 238 | { | ||
| 239 | qCritical() << "An error occurred while initializing ENet"; | ||
| 240 | return EXIT_FAILURE; | ||
| 241 | } | ||
| 242 | |||
| 243 | Q_INIT_RESOURCE(pacman); | ||
| 244 | QApplication app(argc, argv, true); | ||
| 245 | app.setOrganizationName("TU Wien FOOP"); | ||
| 246 | app.setApplicationName("Pacman Client"); | ||
| 247 | app.setWindowIcon(QIcon(":/appicon")); | ||
| 248 | |||
| 249 | qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); | ||
| 250 | |||
| 251 | AudioManager::self(); | ||
| 252 | |||
| 253 | Client client; | ||
| 254 | client.show(); | ||
| 255 | client.setWindowTitle(app.applicationName()); | ||
| 256 | |||
| 257 | int ret = app.exec(); | ||
| 258 | |||
| 259 | enet_deinitialize(); | ||
| 260 | |||
| 261 | /* Delete all global objects allocated by libprotobuf */ | ||
| 262 | google::protobuf::ShutdownProtobufLibrary(); | ||
| 263 | |||
| 264 | return ret; | ||
| 265 | } | ||
