diff options
| author | totycro <totycro@unknown-horizons.org> | 2011-04-05 18:19:03 +0200 |
|---|---|---|
| committer | totycro <totycro@unknown-horizons.org> | 2011-04-05 18:19:03 +0200 |
| commit | 3cdc77bc7d1da312a0c7ae9c734f6a33f1c49ecf (patch) | |
| tree | ef6406c25ebb04d47c8c39c32121c5a37d444e56 /pacman-c++/mainwidget.cpp | |
| parent | de6884b116323b5baa620a4bcdf7b7eb44a833aa (diff) | |
| download | foop-3cdc77bc7d1da312a0c7ae9c734f6a33f1c49ecf.tar.gz foop-3cdc77bc7d1da312a0c7ae9c734f6a33f1c49ecf.tar.bz2 foop-3cdc77bc7d1da312a0c7ae9c734f6a33f1c49ecf.zip | |
added key handling
Diffstat (limited to 'pacman-c++/mainwidget.cpp')
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 9d7fb14..743b57e 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -6,7 +6,10 @@ | |||
| 6 | #include "constants.h" | 6 | #include "constants.h" |
| 7 | 7 | ||
| 8 | MainWidget::MainWidget() | 8 | MainWidget::MainWidget() |
| 9 | : currentKey(0) | ||
| 9 | { | 10 | { |
| 11 | setFocusPolicy(Qt::StrongFocus); | ||
| 12 | |||
| 10 | QVBoxLayout *layout = new QVBoxLayout(this); | 13 | QVBoxLayout *layout = new QVBoxLayout(this); |
| 11 | 14 | ||
| 12 | QHBoxLayout *m_scoreLayout = new QHBoxLayout(); | 15 | QHBoxLayout *m_scoreLayout = new QHBoxLayout(); |
| @@ -172,3 +175,40 @@ QPoint MainWidget::mapPositionToCoord(unsigned int x, unsigned int y) | |||
| 172 | return QPoint(x * field_size[0], y * field_size[1]); | 175 | return QPoint(x * field_size[0], y * field_size[1]); |
| 173 | } | 176 | } |
| 174 | 177 | ||
| 178 | transmission::field_t MainWidget::translateKey(int key) | ||
| 179 | { | ||
| 180 | switch(key) { | ||
| 181 | case Qt::Key_Up: | ||
| 182 | return transmission::direction_up; | ||
| 183 | break; | ||
| 184 | case Qt::Key_Down: | ||
| 185 | return transmission::direction_down; | ||
| 186 | break; | ||
| 187 | case Qt::Key_Left: | ||
| 188 | return transmission::direction_left; | ||
| 189 | break; | ||
| 190 | case Qt::Key_Right: | ||
| 191 | return transmission::direction_right; | ||
| 192 | break; | ||
| 193 | default: | ||
| 194 | return 0; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | void MainWidget::keyPressEvent(QKeyEvent* event) | ||
| 199 | { | ||
| 200 | QWidget::keyPressEvent(event); | ||
| 201 | currentKey = translateKey( event->key() ); | ||
| 202 | } | ||
| 203 | |||
| 204 | void MainWidget::keyReleaseEvent(QKeyEvent* event) | ||
| 205 | { | ||
| 206 | QWidget::keyReleaseEvent(event); | ||
| 207 | transmission::field_t releasedKey = translateKey(event->key()); | ||
| 208 | if (releasedKey == currentKey) { | ||
| 209 | // current key got released | ||
| 210 | // if this is false, a key got released which has already | ||
| 211 | // been replaced by a different key, so this case is disregarded | ||
| 212 | currentKey = 0; | ||
| 213 | } | ||
| 214 | } | ||
