diff options
Diffstat (limited to 'pacman-c++')
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 40 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.h | 14 |
2 files changed, 54 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 | } | ||
diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h index dc20d75..f7113a5 100644 --- a/pacman-c++/mainwidget.h +++ b/pacman-c++/mainwidget.h | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | #include "constants.h" | 4 | #include "constants.h" |
| 5 | #include <QtGui> | 5 | #include <QtGui> |
| 6 | #include <QtCore> | ||
| 6 | 7 | ||
| 7 | class Actor; | 8 | class Actor; |
| 8 | 9 | ||
| @@ -14,6 +15,11 @@ class MainWidget | |||
| 14 | public: | 15 | public: |
| 15 | MainWidget(); | 16 | MainWidget(); |
| 16 | 17 | ||
| 18 | protected: | ||
| 19 | // handling of current key | ||
| 20 | virtual void keyPressEvent(QKeyEvent* ); | ||
| 21 | virtual void keyReleaseEvent(QKeyEvent* ); | ||
| 22 | |||
| 17 | private: | 23 | private: |
| 18 | void loadDummyMap(); | 24 | void loadDummyMap(); |
| 19 | 25 | ||
| @@ -22,10 +28,18 @@ private: | |||
| 22 | // data conversion | 28 | // data conversion |
| 23 | QPoint mapPositionToCoord(unsigned int x, unsigned int y); | 29 | QPoint mapPositionToCoord(unsigned int x, unsigned int y); |
| 24 | 30 | ||
| 31 | // GUI elements needed in the progress of the game | ||
| 25 | QList<QGridLayout*> m_playerScoreLayouts; | 32 | QList<QGridLayout*> m_playerScoreLayouts; |
| 26 | QGraphicsScene *m_scene; | 33 | QGraphicsScene *m_scene; |
| 27 | 34 | ||
| 35 | // map of actors in order to keep track of those instances | ||
| 28 | QMap<Color, Actor*> m_actors; | 36 | QMap<Color, Actor*> m_actors; |
| 37 | |||
| 38 | // key currently pressed by user | ||
| 39 | transmission::field_t currentKey; | ||
| 40 | |||
| 41 | // translate Qt::Key to our key format | ||
| 42 | transmission::field_t translateKey(int); | ||
| 29 | }; | 43 | }; |
| 30 | 44 | ||
| 31 | #endif // MAINWIDGET_H | 45 | #endif // MAINWIDGET_H |
