| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2012 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #ifndef EVENTHANDLER_HPP |
| 17 | #define EVENTHANDLER_HPP |
| 18 | |
| 19 | #include "inputhandler.hpp" |
| 20 | |
| 21 | #include <QKeyEvent> |
| 22 | #include <QObject> |
| 23 | #include <QTimer> |
| 24 | |
| 25 | #include <string> |
| 26 | |
| 27 | //! Handles key and mouse events. |
| 28 | class EventHandler : public QObject |
| 29 | { |
| 30 | Q_OBJECT |
| 31 | |
| 32 | public: |
| 33 | //! Constructor. |
| 34 | EventHandler(InputHandler & inputHandler); |
| 35 | |
| 36 | void enableCaptureMode(InputHandler::Action action, int player); |
| 37 | |
| 38 | bool handleKeyPressEvent(QKeyEvent * event); |
| 39 | bool handleKeyReleaseEvent(QKeyEvent * event); |
| 40 | |
| 41 | bool handleMouseMoveEvent(QMouseEvent * event); |
| 42 | bool handleMousePressEvent(QMouseEvent * event, int screenWidth, int screenHeight, bool mirrorY); |
| 43 | bool handleMouseReleaseEvent(QMouseEvent * event, int screenWidth, int screenHeight, bool mirrorY); |
| 44 | |
| 45 | signals: |
| 46 | |
| 47 | void pauseToggled(); |
| 48 | |
| 49 | void gameExited(); |
| 50 | |
| 51 | void soundRequested(QString handle, bool loop = false); |
| 52 | |
| 53 | void cursorRevealed(); |
| 54 | |
| 55 | void cursorHid(); |
| 56 | |
| 57 | private: |
| 58 | class ActionMapping |
| 59 | { |
| 60 | public: |
| 61 | ActionMapping(int player, InputHandler::Action action) |
| 62 | : m_player { player } |
| 63 | , m_action { action } |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | ActionMapping() = default; |
| 68 | |
| 69 | int player() const |
| 70 | { |
| 71 | return m_player; |
| 72 | } |
| 73 | |
| 74 | InputHandler::Action action() const |
| 75 | { |
| 76 | return m_action; |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | int m_player; |
| 81 | InputHandler::Action m_action; |
| 82 | }; |
| 83 | |
| 84 | bool applyMatchingAction(QKeyEvent * event, bool press); |
| 85 | |
| 86 | void disableCaptureMode(); |
| 87 | |
| 88 | bool handleMenuKeyPressEvent(QKeyEvent * event); |
| 89 | bool handleGameKeyPressEvent(QKeyEvent * event); |
| 90 | bool handleGameKeyReleaseEvent(QKeyEvent * event); |
| 91 | |
| 92 | void loadKeyMappings(); |
| 93 | |
| 94 | bool mapKeyToAction(int player, InputHandler::Action action, int key); |
| 95 | |
| 96 | typedef std::map<int, ActionMapping> KeyToActionMap; |
| 97 | |
| 98 | KeyToActionMap m_keyToActionMap; |
| 99 | |
| 100 | InputHandler & m_inputHandler; |
| 101 | |
| 102 | bool m_captureMode; |
| 103 | |
| 104 | InputHandler::Action m_captureAction; |
| 105 | |
| 106 | int m_capturePlayer; |
| 107 | |
| 108 | QTimer m_mouseCursorTimer; |
| 109 | }; |
| 110 | |
| 111 | #endif // EVENTHANDLER_HPP |