summaryrefslogtreecommitdiffstats
path: root/pacman-c++/client.cpp
blob: fc0fdfc00a5a3cab1e6affb22de6148362ccbe79 (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
#include "client.h"
#include "clicklabel.h"
#include "audio.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);
  connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
  fileMenu->addAction(quitAction);
}

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

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;
}