summaryrefslogtreecommitdiffstats
path: root/pacman-c++/client.cpp
blob: 7c428c58224f45b31a46bc06b93ec0be00d8d658 (plain)
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
#include "client.h"
#include "clicklabel.h"
#include "audio.h"
#include "util.h"
#include "pacman.pb.h"

Client::Client()
  : m_ambientMuted(false)
{
  m_settings = new QSettings(qApp->organizationName(), qApp->applicationName(), this);
  createMenu();
  m_mainWidget = new MainWidget(this);
  m_mainWidget->setAmbientMuted(m_ambientMuted);
  setCentralWidget(m_mainWidget);
}

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(!sound);
  fileMenu->addAction(toggleAmbientAction);
  if (sound)
  {
    connect(toggleAmbientAction, SIGNAL(triggered(bool)), this, SLOT(enableAmbientSound(bool)));
    connect(this, SIGNAL(setMuteActionsChecked(bool)), toggleAmbientAction, 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()
{
  QDialog *about = new QDialog(this);
  about->setWindowTitle("About Pacman");
  about->setWindowFlags(about->windowFlags() & ~Qt::WindowContextHelpButtonHint);

  QGridLayout *layout = new QGridLayout(about);
  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>"
      ", &copy; <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);
  label->setWordWrap(true);
  label->setOpenExternalLinks(true);
  label->setTextInteractionFlags(Qt::TextBrowserInteraction);

  QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
  QPushButton *closeButton = buttonBox->button(QDialogButtonBox::Close);
  buttonBox->addButton(closeButton, QDialogButtonBox::ButtonRole(QDialogButtonBox::RejectRole | QDialogButtonBox::AcceptRole));
  connect(buttonBox , SIGNAL(rejected()), about, SLOT(reject()));

  layout->addWidget(label, 0, 1, 4, 4);
  layout->addWidget(buttonBox, 4, 0, 1, 5);
  about->show();
}

bool Constants::server = false;

int main(int argc, char ** argv)
{
  /* 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;

  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()));

  Client client;
  client.show();
  client.setWindowTitle(app.applicationName());

  int ret = app.exec();

  /* Delete all global objects allocated by libprotobuf */
  google::protobuf::ShutdownProtobufLibrary();

  return ret;
}