Debian Codegraph @

src/game/eventhandler.cpp

raw symbols SCIP
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 #include "eventhandler.hpp"
17 #include "keycodes.hpp"
18 #include "settings.hpp"
19 #include "statemachine.hpp"
20
21 #include <Menu>
22 #include <MenuManager>
23
24 #include <cassert>
25
26 EventHandler::EventHandler(InputHandler & inputHandler)
27 : m_inputHandler { inputHandler }
28 , m_captureMode { false }
29 {
30 // Default key bindings
31 m_keyToActionMap[KeyCodes::LSHIFT] = ActionMapping(1, InputHandler::Action::Up);
32 m_keyToActionMap[KeyCodes::RSHIFT] = ActionMapping(0, InputHandler::Action::Up);
33 m_keyToActionMap[KeyCodes::LCTRL] = ActionMapping(1, InputHandler::Action::Down);
34 m_keyToActionMap[KeyCodes::RCTRL] = ActionMapping(0, InputHandler::Action::Down);
35 m_keyToActionMap[Qt::Key_Left] = ActionMapping(0, InputHandler::Action::Left);
36 m_keyToActionMap[Qt::Key_Right] = ActionMapping(0, InputHandler::Action::Right);
37 m_keyToActionMap[Qt::Key_A] = ActionMapping(1, InputHandler::Action::Left);
38 m_keyToActionMap[Qt::Key_D] = ActionMapping(1, InputHandler::Action::Right);
39 m_keyToActionMap[Qt::Key_Up] = ActionMapping(0, InputHandler::Action::Up);
40 m_keyToActionMap[Qt::Key_Down] = ActionMapping(0, InputHandler::Action::Down);
41 m_keyToActionMap[Qt::Key_W] = ActionMapping(1, InputHandler::Action::Up);
42 m_keyToActionMap[Qt::Key_S] = ActionMapping(1, InputHandler::Action::Down);
43
44 loadKeyMappings();
45
46 m_mouseCursorTimer.setSingleShot(true);
47 m_mouseCursorTimer.setInterval(3000);
48 connect(&m_mouseCursorTimer, &QTimer::timeout, this, &EventHandler::cursorHid);
49 }
50
51 void EventHandler::loadKeyMappings()
52 {
53 const std::vector<InputHandler::Action> actions = {
54 InputHandler::Action::Up,
55 InputHandler::Action::Down,
56 InputHandler::Action::Left,
57 InputHandler::Action::Right
58 };
59
60 for (int player = 0; player < 2; player++)
61 {
62 for (InputHandler::Action action : actions)
63 {
64 mapKeyToAction(
65 player,
66 action,
67 Settings::instance().loadKeyMapping(player, action));
68 }
69 }
70 }
71
72 void EventHandler::enableCaptureMode(InputHandler::Action action, int player)
73 {
74 assert(player == 0 || player == 1);
75
76 m_captureMode = true;
77 m_captureAction = action;
78 m_capturePlayer = player;
79 }
80
81 void EventHandler::disableCaptureMode()
82 {
83 m_captureMode = false;
84 }
85
86 bool EventHandler::handleKeyPressEvent(QKeyEvent * event)
87 {
88 return StateMachine::instance().state() != StateMachine::State::Menu ? handleGameKeyPressEvent(event) : handleMenuKeyPressEvent(event);
89 }
90
91 bool EventHandler::handleKeyReleaseEvent(QKeyEvent * event)
92 {
93 return StateMachine::instance().state() != StateMachine::State::Menu ? handleGameKeyReleaseEvent(event) : false;
94 }
95
96 bool EventHandler::handleMousePressEvent(QMouseEvent * event, int screenWidth, int screenHeight, bool mirrorY)
97 {
98 if (StateMachine::instance().state() == StateMachine::State::Menu)
99 {
100 const int y = mirrorY ? screenHeight - static_cast<int>(event->position().y()) : static_cast<int>(event->position().y());
101 if (MTFH::MenuManager::instance().mousePress(
102 static_cast<int>(event->position().x()), y, screenWidth, screenHeight))
103 {
104 emit soundRequested("menuClick");
105 }
106 }
107
108 return true;
109 }
110
111 bool EventHandler::handleMouseReleaseEvent(QMouseEvent * event, int screenWidth, int screenHeight, bool mirrorY)
112 {
113 if (StateMachine::instance().state() == StateMachine::State::Menu)
114 {
115 const int y = mirrorY ? screenHeight - static_cast<int>(event->position().y()) : static_cast<int>(event->position().y());
116 MTFH::MenuManager::instance().mouseRelease(
117 static_cast<int>(event->position().x()), y, screenWidth, screenHeight);
118
119 if (MTFH::MenuManager::instance().isDone())
120 {
121 emit soundRequested("menuBoom");
122 }
123 }
124
125 return true;
126 }
127
128 bool EventHandler::handleMouseMoveEvent(QMouseEvent * event)
129 {
130 Q_UNUSED(event)
131 m_mouseCursorTimer.start();
132 emit cursorRevealed();
133 return true;
134 }
135
136 bool EventHandler::handleMenuKeyPressEvent(QKeyEvent * event)
137 {
138 if (m_captureMode)
139 {
140 if (mapKeyToAction(m_capturePlayer, m_captureAction, static_cast<int>(event->nativeScanCode())))
141 {
142 disableCaptureMode();
143 MTFH::MenuManager::instance().popMenu();
144 }
145 }
146 else
147 {
148 switch (event->key())
149 {
150 case Qt::Key_Left:
151 emit soundRequested("menuClick");
152 MTFH::MenuManager::instance().left();
153 break;
154 case Qt::Key_Right:
155 emit soundRequested("menuClick");
156 MTFH::MenuManager::instance().right();
157 break;
158 case Qt::Key_Up:
159 emit soundRequested("menuClick");
160 MTFH::MenuManager::instance().up();
161 break;
162 case Qt::Key_Down:
163 emit soundRequested("menuClick");
164 MTFH::MenuManager::instance().down();
165 break;
166 case Qt::Key_Return:
167 case Qt::Key_Enter:
168 MTFH::MenuManager::instance().selectCurrentItem();
169 if (MTFH::MenuManager::instance().isDone())
170 {
171 emit soundRequested("menuBoom");
172 }
173 else
174 {
175 emit soundRequested("menuClick");
176 }
177 break;
178 case Qt::Key_Escape:
179 case Qt::Key_Q:
180 emit soundRequested("menuClick");
181 MTFH::MenuManager::instance().activeMenu()->exit();
182 if (MTFH::MenuManager::instance().isDone())
183 {
184 emit gameExited();
185 }
186 break;
187 default:
188 return false;
189 }
190 }
191
192 return true;
193 }
194
195 bool EventHandler::handleGameKeyPressEvent(QKeyEvent * event)
196 {
197 return applyMatchingAction(event, true);
198 }
199
200 bool EventHandler::handleGameKeyReleaseEvent(QKeyEvent * event)
201 {
202 return applyMatchingAction(event, false);
203 }
204
205 bool EventHandler::applyMatchingAction(QKeyEvent * event, bool press)
206 {
207 if (!event->isAutoRepeat())
208 {
209 if (m_keyToActionMap.count(static_cast<int>(event->nativeScanCode())))
210 {
211 const unsigned int player = m_keyToActionMap.at(event->nativeScanCode()).player();
212 const InputHandler::Action action = m_keyToActionMap.at(event->nativeScanCode()).action();
213 m_inputHandler.setActionState(player, action, press);
214 return true;
215 }
216
217 if (m_keyToActionMap.count(event->key()))
218 {
219 const unsigned int player = m_keyToActionMap.at(event->key()).player();
220 const InputHandler::Action action = m_keyToActionMap.at(event->key()).action();
221 m_inputHandler.setActionState(player, action, press);
222 return true;
223 }
224
225 if (press)
226 {
227 switch (event->key())
228 {
229 case Qt::Key_Escape:
230 case Qt::Key_Q:
231 StateMachine::instance().quit();
232 return true;
233 case Qt::Key_P:
234 emit pauseToggled();
235 return true;
236 default:
237 break;
238 }
239 }
240 }
241
242 return false;
243 }
244
245 bool EventHandler::mapKeyToAction(int player, InputHandler::Action action, int key)
246 {
247 if (key && key != Qt::Key_Escape && key != Qt::Key_Q && key != Qt::Key_P)
248 {
249 // Find the matching action and change the key
250 auto iter = m_keyToActionMap.begin();
251 while (iter != m_keyToActionMap.end())
252 {
253 if (iter->second.action() == action && iter->second.player() == player)
254 {
255 iter = m_keyToActionMap.erase(iter);
256 }
257 else
258 {
259 iter++;
260 }
261 }
262
263 m_keyToActionMap[key] = { player, action };
264
265 Settings::instance().saveKeyMapping(player, action, key);
266
267 return true;
268 }
269
270 return false;
271 }