1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Reto Grieder |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file |
---|
31 | @brief Implementation of a little Input handler that distributes everything |
---|
32 | coming from OIS. |
---|
33 | */ |
---|
34 | |
---|
35 | #include "InputManager.h" |
---|
36 | #include "CoreIncludes.h" |
---|
37 | #include "Debug.h" |
---|
38 | #include "InputBuffer.h" |
---|
39 | #include "ConsoleCommand.h" |
---|
40 | #include "util/Convert.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | /** |
---|
45 | @brief Constructor only resets the pointer values to 0. |
---|
46 | */ |
---|
47 | InputManager::InputManager() : |
---|
48 | inputSystem_(0), keyboard_(0), mouse_(0), |
---|
49 | state_(IS_UNINIT), stateRequest_(IS_UNINIT) |
---|
50 | { |
---|
51 | RegisterObject(InputManager); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | @brief The one instance of the InputManager is stored in this function. |
---|
56 | @return A reference to the only instance of the InputManager |
---|
57 | */ |
---|
58 | InputManager& InputManager::_getSingleton() |
---|
59 | { |
---|
60 | static InputManager theOnlyInstance; |
---|
61 | return theOnlyInstance; |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | @brief Destructor only called at the end of the program |
---|
66 | */ |
---|
67 | InputManager::~InputManager() |
---|
68 | { |
---|
69 | this->_destroy(); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | @brief Creates the OIS::InputMananger, the keyboard and the mouse and |
---|
74 | assigns the key bindings. |
---|
75 | @param windowHnd The window handle of the render window |
---|
76 | @param windowWidth The width of the render window |
---|
77 | @param windowHeight The height of the render window |
---|
78 | */ |
---|
79 | bool InputManager::_initialise(size_t windowHnd, int windowWidth, int windowHeight) |
---|
80 | { |
---|
81 | if (!inputSystem_) |
---|
82 | { |
---|
83 | // Setup basic variables |
---|
84 | OIS::ParamList paramList; |
---|
85 | std::ostringstream windowHndStr; |
---|
86 | |
---|
87 | // Fill parameter list |
---|
88 | windowHndStr << (unsigned int)windowHnd; |
---|
89 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
90 | |
---|
91 | //#if defined OIS_LINUX_PLATFORM |
---|
92 | // paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); |
---|
93 | //#endif |
---|
94 | |
---|
95 | try |
---|
96 | { |
---|
97 | // Create inputsystem |
---|
98 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
---|
99 | COUT(ORX_DEBUG) << "*** InputManager: Created OIS input system" << std::endl; |
---|
100 | |
---|
101 | // create a keyboard. If none are available the exception is caught. |
---|
102 | keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true)); |
---|
103 | COUT(ORX_DEBUG) << "*** InputManager: Created OIS mouse" << std::endl; |
---|
104 | |
---|
105 | //TODO: check for already pressed keys |
---|
106 | |
---|
107 | // create a mouse. If none are available the exception is caught. |
---|
108 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
---|
109 | COUT(ORX_DEBUG) << "*** InputManager: Created OIS keyboard" << std::endl; |
---|
110 | |
---|
111 | // Set mouse region |
---|
112 | setWindowExtents(windowWidth, windowHeight); |
---|
113 | |
---|
114 | this->state_ = IS_NONE; |
---|
115 | } |
---|
116 | catch (OIS::Exception ex) |
---|
117 | { |
---|
118 | // something went wrong with the initialisation |
---|
119 | COUT(ORX_ERROR) << "Error: Failed creating an input system/keyboard/mouse. Message: \"" << ex.eText << "\"" << std::endl; |
---|
120 | inputSystem_ = 0; |
---|
121 | return false; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | keyboard_->setEventCallback(this); |
---|
126 | mouse_->setEventCallback(this); |
---|
127 | |
---|
128 | addKeyListener(new InputBuffer(), "buffer"); |
---|
129 | |
---|
130 | _loadBindings(); |
---|
131 | |
---|
132 | COUT(ORX_DEBUG) << "*** InputManager: Loading done." << std::endl; |
---|
133 | |
---|
134 | return true; |
---|
135 | } |
---|
136 | |
---|
137 | void InputManager::_loadBindings() |
---|
138 | { |
---|
139 | for (int i = 0; i < numberOfKeys_s; i++) |
---|
140 | { |
---|
141 | // simply write the key number (i) in the string |
---|
142 | this->bindingsKeyPress_[i] = ""; |
---|
143 | this->bindingsKeyRelease_[i] = ""; |
---|
144 | } |
---|
145 | this->bindingsKeyPress_[OIS::KC_NUMPADENTER] = "activateConsole"; |
---|
146 | this->bindingsKeyPress_[OIS::KC_ESCAPE] = "exit"; |
---|
147 | this->bindingsKeyHold_[OIS::KC_U] = "exec disco.txt"; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | @brief Destroys all the created input devices and handlers. |
---|
152 | */ |
---|
153 | void InputManager::_destroy() |
---|
154 | { |
---|
155 | COUT(ORX_DEBUG) << "*** InputManager: Destroying ..." << std::endl; |
---|
156 | if (mouse_) |
---|
157 | inputSystem_->destroyInputObject(mouse_); |
---|
158 | if (keyboard_) |
---|
159 | inputSystem_->destroyInputObject(keyboard_); |
---|
160 | if (inputSystem_) |
---|
161 | OIS::InputManager::destroyInputSystem(inputSystem_); |
---|
162 | |
---|
163 | mouse_ = 0; |
---|
164 | keyboard_ = 0; |
---|
165 | inputSystem_ = 0; |
---|
166 | |
---|
167 | OIS::KeyListener* buffer = keyListeners_["buffer"]; |
---|
168 | if (buffer) |
---|
169 | { |
---|
170 | this->removeKeyListener("buffer"); |
---|
171 | delete buffer; |
---|
172 | buffer = 0; |
---|
173 | } |
---|
174 | |
---|
175 | COUT(ORX_DEBUG) << "*** InputManager: Destroying done." << std::endl; |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | @brief Updates the InputManager |
---|
180 | @param dt Delta time |
---|
181 | */ |
---|
182 | void InputManager::tick(float dt) |
---|
183 | { |
---|
184 | if (state_ == IS_UNINIT) |
---|
185 | return; |
---|
186 | |
---|
187 | // reset the game if it has changed |
---|
188 | if (state_ != stateRequest_) |
---|
189 | { |
---|
190 | if (stateRequest_ != IS_CUSTOM) |
---|
191 | _setDefaultState(); |
---|
192 | |
---|
193 | switch (stateRequest_) |
---|
194 | { |
---|
195 | case IS_NORMAL: |
---|
196 | break; |
---|
197 | case IS_GUI: |
---|
198 | //FIXME: do stuff |
---|
199 | break; |
---|
200 | case IS_CONSOLE: |
---|
201 | if (this->keyListeners_.find("buffer") != this->keyListeners_.end()) |
---|
202 | this->activeKeyListeners_.push_back(this->keyListeners_["buffer"]); |
---|
203 | this->bDefaultKeyInput = false; |
---|
204 | break; |
---|
205 | case IS_CUSTOM: |
---|
206 | // don't do anything |
---|
207 | break; |
---|
208 | } |
---|
209 | state_ = stateRequest_; |
---|
210 | } |
---|
211 | |
---|
212 | // capture all the input. That calls the event handlers. |
---|
213 | if (mouse_) |
---|
214 | mouse_->capture(); |
---|
215 | if (keyboard_) |
---|
216 | keyboard_->capture(); |
---|
217 | } |
---|
218 | |
---|
219 | void InputManager::_setDefaultState() |
---|
220 | { |
---|
221 | this->activeJoyStickListeners_.clear(); |
---|
222 | this->activeKeyListeners_.clear(); |
---|
223 | this->activeMouseListeners_.clear(); |
---|
224 | this->bDefaultKeyInput = true; |
---|
225 | this->bDefaultMouseInput = true; |
---|
226 | this->bDefaultJoyStickInput = true; |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | /** |
---|
231 | @brief Event handler for the keyPressed Event. |
---|
232 | @param e Event information |
---|
233 | */ |
---|
234 | bool InputManager::keyPressed(const OIS::KeyEvent &e) |
---|
235 | { |
---|
236 | this->keysDown_.push_back(e.key); |
---|
237 | |
---|
238 | if (this->bDefaultKeyInput) |
---|
239 | { |
---|
240 | // find the appropriate key binding |
---|
241 | std::string cmdStr = bindingsKeyPress_[int(e.key)]; |
---|
242 | if (cmdStr != "") |
---|
243 | { |
---|
244 | CommandExecutor::execute(cmdStr); |
---|
245 | COUT(3) << "Executing command: " << cmdStr << std::endl; |
---|
246 | } |
---|
247 | } |
---|
248 | else |
---|
249 | { |
---|
250 | for (std::list<OIS::KeyListener*>::const_iterator it = activeKeyListeners_.begin(); it != activeKeyListeners_.end(); it++) |
---|
251 | (*it)->keyPressed(e); |
---|
252 | } |
---|
253 | return true; |
---|
254 | } |
---|
255 | |
---|
256 | /** |
---|
257 | @brief Event handler for the keyReleased Event. |
---|
258 | @param e Event information |
---|
259 | */ |
---|
260 | bool InputManager::keyReleased(const OIS::KeyEvent &e) |
---|
261 | { |
---|
262 | // remove the key from the keysDown_ list |
---|
263 | for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++) |
---|
264 | { |
---|
265 | if (*it == e.key) |
---|
266 | { |
---|
267 | keysDown_.erase(it); |
---|
268 | break; |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | if (this->bDefaultKeyInput) |
---|
273 | { |
---|
274 | // find the appropriate key binding |
---|
275 | std::string cmdStr = bindingsKeyRelease_[int(e.key)]; |
---|
276 | if (cmdStr != "") |
---|
277 | { |
---|
278 | CommandExecutor::execute(cmdStr); |
---|
279 | COUT(3) << "Executing command: " << cmdStr << std::endl; |
---|
280 | } |
---|
281 | } |
---|
282 | else |
---|
283 | { |
---|
284 | for (std::list<OIS::KeyListener*>::const_iterator it = activeKeyListeners_.begin(); it != activeKeyListeners_.end(); it++) |
---|
285 | (*it)->keyReleased(e); |
---|
286 | } |
---|
287 | return true; |
---|
288 | } |
---|
289 | |
---|
290 | /** |
---|
291 | @brief Event handler for the mouseMoved Event. |
---|
292 | @param e Event information |
---|
293 | */ |
---|
294 | bool InputManager::mouseMoved(const OIS::MouseEvent &e) |
---|
295 | { |
---|
296 | return true; |
---|
297 | } |
---|
298 | |
---|
299 | /** |
---|
300 | @brief Event handler for the mousePressed Event. |
---|
301 | @param e Event information |
---|
302 | @param id The ID of the mouse button |
---|
303 | */ |
---|
304 | bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
305 | { |
---|
306 | return true; |
---|
307 | } |
---|
308 | |
---|
309 | /** |
---|
310 | @brief Event handler for the mouseReleased Event. |
---|
311 | @param e Event information |
---|
312 | @param id The ID of the mouse button |
---|
313 | */ |
---|
314 | bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
315 | { |
---|
316 | return true; |
---|
317 | } |
---|
318 | |
---|
319 | bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button) |
---|
320 | { |
---|
321 | return true; |
---|
322 | } |
---|
323 | |
---|
324 | bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button) |
---|
325 | { |
---|
326 | return true; |
---|
327 | } |
---|
328 | |
---|
329 | bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis) |
---|
330 | { |
---|
331 | return true; |
---|
332 | } |
---|
333 | |
---|
334 | bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id) |
---|
335 | { |
---|
336 | return true; |
---|
337 | } |
---|
338 | |
---|
339 | bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id) |
---|
340 | { |
---|
341 | return true; |
---|
342 | } |
---|
343 | |
---|
344 | |
---|
345 | |
---|
346 | /** |
---|
347 | @brief Adjusts the mouse window metrics. |
---|
348 | This method has to be called every time the size of the window changes. |
---|
349 | @param width The new width of the render window |
---|
350 | @param height the new height of the render window |
---|
351 | */ |
---|
352 | void InputManager::setWindowExtents(int width, int height) |
---|
353 | { |
---|
354 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
---|
355 | const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState(); |
---|
356 | mouseState.width = width; |
---|
357 | mouseState.height = height; |
---|
358 | } |
---|
359 | |
---|
360 | /** |
---|
361 | @brief Sets the input mode to either GUI, inGame or Buffer |
---|
362 | @param mode The new input mode |
---|
363 | @remark Only has an affect if the mode actually changes |
---|
364 | */ |
---|
365 | void InputManager::setInputState(const InputState state) |
---|
366 | { |
---|
367 | _getSingleton().stateRequest_ = state; |
---|
368 | } |
---|
369 | |
---|
370 | /** |
---|
371 | @brief Returns the current input handling method |
---|
372 | @return The current input mode. |
---|
373 | */ |
---|
374 | InputManager::InputState InputManager::getInputState() |
---|
375 | { |
---|
376 | return _getSingleton().state_; |
---|
377 | } |
---|
378 | |
---|
379 | void InputManager::destroy() |
---|
380 | { |
---|
381 | _getSingleton()._destroy(); |
---|
382 | } |
---|
383 | |
---|
384 | bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight) |
---|
385 | { |
---|
386 | return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight); |
---|
387 | } |
---|
388 | |
---|
389 | bool InputManager::addKeyListener(OIS::KeyListener *listener, const std::string& name) |
---|
390 | { |
---|
391 | if (_getSingleton().keyListeners_.find(name) == _getSingleton().keyListeners_.end()) |
---|
392 | { |
---|
393 | _getSingleton().keyListeners_[name] = listener; |
---|
394 | return true; |
---|
395 | } |
---|
396 | else |
---|
397 | return false; |
---|
398 | } |
---|
399 | |
---|
400 | bool InputManager::removeKeyListener(const std::string &name) |
---|
401 | { |
---|
402 | std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().keyListeners_.find(name); |
---|
403 | if (it != _getSingleton().keyListeners_.end()) |
---|
404 | { |
---|
405 | _getSingleton().keyListeners_.erase(it); |
---|
406 | return true; |
---|
407 | } |
---|
408 | else |
---|
409 | return false; |
---|
410 | } |
---|
411 | |
---|
412 | OIS::KeyListener* InputManager::getKeyListener(const std::string& name) |
---|
413 | { |
---|
414 | std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().keyListeners_.find(name); |
---|
415 | if (it != _getSingleton().keyListeners_.end()) |
---|
416 | { |
---|
417 | return (*it).second; |
---|
418 | } |
---|
419 | else |
---|
420 | return 0; |
---|
421 | } |
---|
422 | |
---|
423 | } |
---|