summaryrefslogtreecommitdiffstats
path: root/pacman-c++/mainwidget.cpp
blob: 02ece0fb0ceb14b95265fd5bfa65017e90b04397 (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
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
#include "mainwidget.h"
#include "actor.h"
#include "block.h"
#include "bonuspoint.h"
#include "point.h"
#include "constants.h"
#include "audioplayer.h"
#include "clicklabel.h"
#include "util.h"

MainWidget::MainWidget(QMainWindow *window, QWidget *parent)
  : SceneHolder(parent), m_mainwindow(window), m_currentKey(0), m_running(false)
{
  createGui();
  updateMap(Util::createDummyMap());

  //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame()));
  //AudioPlayer::self()->play(AudioPlayer::Intro);

  QTimer *timer = new QTimer(this);
  connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
  timer->start(Constants::tick);

  startGame();
}

void MainWidget::createGui()
{
  createMenu();
  setFocusPolicy(Qt::StrongFocus);

  QVBoxLayout *layout = new QVBoxLayout(this);
  QHBoxLayout *scoreLayout = new QHBoxLayout();

  for (unsigned int i = 1; i < 4; ++i)
  {
    QGroupBox *scoreBox = new QGroupBox(QString("Spieler %1").arg(i), this);
    scoreBox->setObjectName(QString("actor%1").arg(i));
    scoreBox->setCheckable(true);
    connect(scoreBox, SIGNAL(clicked()), this, SLOT(playerScoreClicked()));
    scoreLayout->addWidget(scoreBox);

    QGridLayout *playerLayout = new QGridLayout();
    scoreBox->setLayout(playerLayout);

    playerLayout->addWidget(new QLabel("Rundenpunkte:", this), 0, 0);
    playerLayout->addWidget(new QLabel("Gesamtpunkte:", this), 1, 0);

    playerLayout->addWidget(new QLabel("", this), 0, 1);
    playerLayout->addWidget(new QLabel("", this), 1, 1);

    m_playerScoreLayouts.append(playerLayout);
  }

  QGraphicsView *window = new QGraphicsView(m_scene, this);
  window->setFrameStyle(0);
  window->setAlignment(Qt::AlignLeft | Qt::AlignTop);
  window->setFixedSize(Constants::map_size_pixel.width, Constants::map_size_pixel.height);
  window->setWindowFlags(window->windowFlags() & ~Qt::WindowMaximizeButtonHint);

  layout->addLayout(scoreLayout);
  layout->addWidget(window);

  QFile css(":/stylesheet");
  css.open(QFile::ReadOnly);
  qApp->setStyleSheet(QLatin1String(css.readAll()));

  setLayout(layout);
}

void MainWidget::createMenu()
{
  QAction *quitAction = new QAction("E&xit", this);
  connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

  QMenu *fileMenu = m_mainwindow->menuBar()->addMenu("File");
  fileMenu->addAction(quitAction);

  ClickLabel *toggleSound = new ClickLabel("Toggle Sound", this);
  toggleSound->setFixedWidth(20);
  toggleSound->setFixedHeight(16);
  toggleSound->setAlignment(Qt::AlignBottom);
  bool sound = AudioPlayer::self()->isWorking();
  QImage img(sound ? ":/soundon" : ":/soundoff");
  img.setColor(1, m_mainwindow->menuBar()->palette().color(sound ? QPalette::Active : QPalette::Disabled,
                                             QPalette::ButtonText).rgba());
  toggleSound->setPixmap(QPixmap::fromImage(img));
  if (sound)
    connect(toggleSound, SIGNAL(clicked()), this, SLOT(toggleSound()));

  m_mainwindow->menuBar()->setCornerWidget(toggleSound);
}

void MainWidget::updateScore()
{
  QMapIterator<Color::Color, Actor*> i(m_actors);
  while (i.hasNext())
  {
    i.next();
    if (i.key() > Color::max)
    {
      /* player #4 isn't supported in score */
      Q_ASSERT(false);
      continue;
    }
    int id = (i.key() >> 1);
    QLabel *turnPointsLbl =
      dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(0,1)->widget());
    QLabel *allPointsLbl =
      dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(1,1)->widget());
    turnPointsLbl->setText(QString::number(id));
    allPointsLbl->setText(QString::number(id));
  }
}

void MainWidget::updateMap(const Transmission::map_t& map)
{
  SceneHolder::updateMap(map);
  updateScore();
}


Transmission::field_t MainWidget::translateKey(int key)
{
  switch(key)
  {
  case Qt::Key_W:
  case Qt::Key_Up:
    return Transmission::direction_up;
    break;
  case Qt::Key_S:
  case Qt::Key_Down:
    return Transmission::direction_down;
    break;
  case Qt::Key_A:
  case Qt::Key_Left:
    return Transmission::direction_left;
    break;
  case Qt::Key_D:
  case Qt::Key_Right:
    return Transmission::direction_right;
    break;
  default:
    return 0;
  }
}

void MainWidget::tick()
{
  Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None);

  QMapIterator<Color::Color, Actor*> i(m_actors);
  while (i.hasNext())
  {
    i.next();
    i.value()->move(mov);
    QList<QGraphicsItem *> list(i.value()->collidingItems());
    for(int j = 0; j < list.count(); ++j)
    {
      if (list.at(j)->parentItem() == i.value())
        continue;
      list.at(j)->setOpacity(0.6);
    }
  }
}


void MainWidget::keyPressEvent(QKeyEvent* event)
{
  if (!m_running)
    return;

  QWidget::keyPressEvent(event);
  m_currentKey = translateKey(event->key());

  return;

  // test stuff
  Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None);

  QMapIterator<Color::Color, Actor*> i(m_actors);
  while (i.hasNext())
  {
    i.next();
    i.value()->move(mov);
  }
}

void MainWidget::keyReleaseEvent(QKeyEvent* event)
{
  if (!m_running)
    return;

  QWidget::keyReleaseEvent(event);
  Transmission::field_t releasedKey = translateKey(event->key());
  if (releasedKey == m_currentKey)
  {
    // current key got released
    // if this is false, a key got released which has already
    // been replaced by a different key, so this case is disregarded
    m_currentKey = Transmission::direction_none;
  }
}

void MainWidget::startGame()
{
  m_running = true;
}

void MainWidget::playerScoreClicked()
{
  QGroupBox *tmp = qobject_cast<QGroupBox *>(sender());
  tmp->setChecked(true);
  return;
}

void MainWidget::toggleSound() const
{
  if (!AudioPlayer::self()->isWorking())
    return;

  bool muted = AudioPlayer::self()->isMuted();
  QImage img(muted ? ":/soundon" : ":/soundoff");
  img.setColor(1, m_mainwindow->menuBar()->palette().color(
      muted ? QPalette::Active : QPalette::Disabled,
      QPalette::ButtonText).rgba());
  ClickLabel *tmp = qobject_cast<ClickLabel *>(m_mainwindow->menuBar()->cornerWidget());
  tmp->setPixmap(QPixmap::fromImage(img));
  AudioPlayer::self()->setMuted(!muted);
}