[918] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Reto Grieder |
---|
| 23 | * Co-authors: |
---|
[919] | 24 | * Some guy writing the example code from Ogre |
---|
[918] | 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | @file |
---|
| 30 | @brief Implementation of a little Input handler that distributes everything |
---|
| 31 | coming from OIS. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "OrxonoxStableHeaders.h" |
---|
| 35 | |
---|
[920] | 36 | #include "core/CoreIncludes.h" |
---|
[919] | 37 | #include "Orxonox.h" |
---|
[922] | 38 | #include "InputEventListener.h" |
---|
[918] | 39 | #include "InputHandler.h" |
---|
| 40 | |
---|
| 41 | namespace orxonox |
---|
| 42 | { |
---|
[919] | 43 | /** |
---|
| 44 | @brief Constructor only resets the pointer values to 0. |
---|
| 45 | */ |
---|
[918] | 46 | InputHandler::InputHandler() |
---|
| 47 | { |
---|
[919] | 48 | //RegisterObject(InputHandler); |
---|
| 49 | this->mouse_ = 0; |
---|
| 50 | this->keyboard_ = 0; |
---|
| 51 | this->inputSystem_ = 0; |
---|
[918] | 52 | } |
---|
| 53 | |
---|
[919] | 54 | /** |
---|
| 55 | @brief The one instance of the InputHandler is stored in this function. |
---|
| 56 | @return The pointer to the only instance of the InputHandler |
---|
| 57 | */ |
---|
[922] | 58 | InputHandler *InputHandler::getSingleton() |
---|
[919] | 59 | { |
---|
| 60 | static InputHandler theOnlyInstance; |
---|
| 61 | return &theOnlyInstance; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | /** |
---|
[922] | 65 | @brief Creates the OIS::InputMananger, the keyboard and the mouse and |
---|
| 66 | assigns the key bindings. |
---|
[919] | 67 | @param windowHnd The window handle of the render window |
---|
| 68 | @param windowWidth The width of the render window |
---|
| 69 | @param windowHeight The height of the render window |
---|
| 70 | */ |
---|
| 71 | void InputHandler::initialise(size_t windowHnd, int windowWidth, int windowHeight) |
---|
| 72 | { |
---|
[918] | 73 | if (!inputSystem_) |
---|
| 74 | { |
---|
| 75 | // Setup basic variables |
---|
| 76 | OIS::ParamList paramList; |
---|
| 77 | std::ostringstream windowHndStr; |
---|
| 78 | |
---|
| 79 | // Fill parameter list |
---|
| 80 | windowHndStr << (unsigned int)windowHnd; |
---|
| 81 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
| 82 | |
---|
[928] | 83 | #if defined OIS_LINUX_PLATFORM |
---|
| 84 | paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); |
---|
| 85 | #endif |
---|
| 86 | |
---|
[918] | 87 | // Create inputsystem |
---|
| 88 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
---|
| 89 | |
---|
| 90 | // If possible create a buffered keyboard |
---|
| 91 | if (inputSystem_->numKeyboards() > 0) |
---|
| 92 | { |
---|
| 93 | keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true)); |
---|
| 94 | keyboard_->setEventCallback(this); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | // If possible create a buffered mouse |
---|
| 98 | if (inputSystem_->numMice() > 0 ) |
---|
| 99 | { |
---|
| 100 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
---|
| 101 | mouse_->setEventCallback(this); |
---|
| 102 | |
---|
| 103 | // Set mouse region |
---|
[919] | 104 | this->setWindowExtents(windowWidth, windowHeight); |
---|
[918] | 105 | } |
---|
| 106 | } |
---|
[922] | 107 | |
---|
| 108 | // load the key bindings |
---|
| 109 | InputEvent empty = {0, false, 0, 0, 0}; |
---|
| 110 | for (int i = 0; i < this->numberOfKeys_; i++) |
---|
| 111 | this->bindingsKeyPressed_[i] = empty; |
---|
| 112 | |
---|
| 113 | //assign 'abort' to the escape key |
---|
| 114 | this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1; |
---|
[918] | 115 | } |
---|
| 116 | |
---|
[919] | 117 | /** |
---|
[928] | 118 | @brief Destroys all the created input devices. |
---|
| 119 | */ |
---|
| 120 | void InputHandler::destroy() |
---|
| 121 | { |
---|
| 122 | this->inputSystem_->destroyInputObject(this->mouse_); |
---|
| 123 | this->inputSystem_->destroyInputObject(this->keyboard_); |
---|
| 124 | OIS::InputManager::destroyInputSystem(this->inputSystem_); |
---|
| 125 | |
---|
| 126 | //TODO: If the InputHandler has been destroyed, how does it know? |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | /** |
---|
[919] | 130 | @brief Updates the InputHandler |
---|
| 131 | @param dt Delta time |
---|
| 132 | */ |
---|
[918] | 133 | void InputHandler::tick(float dt) |
---|
| 134 | { |
---|
| 135 | // capture all the input. That calls the event handlers. |
---|
| 136 | if (mouse_) |
---|
| 137 | mouse_->capture(); |
---|
| 138 | |
---|
| 139 | if (keyboard_) |
---|
| 140 | keyboard_->capture(); |
---|
| 141 | } |
---|
| 142 | |
---|
[919] | 143 | /** |
---|
| 144 | @brief Adjusts the mouse window metrics. |
---|
| 145 | This method has to be called every time the size of the window changes. |
---|
| 146 | @param width The new width of the render window |
---|
| 147 | @param height the new height of the render window |
---|
| 148 | */ |
---|
[918] | 149 | void InputHandler::setWindowExtents(int width, int height) |
---|
| 150 | { |
---|
| 151 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
---|
| 152 | const OIS::MouseState &mouseState = mouse_->getMouseState(); |
---|
| 153 | mouseState.width = width; |
---|
| 154 | mouseState.height = height; |
---|
| 155 | } |
---|
| 156 | |
---|
[919] | 157 | /** |
---|
[922] | 158 | @brief Calls all the objects from classes that derive from InputEventListener. |
---|
| 159 | @param evt The input event that occured. |
---|
| 160 | */ |
---|
| 161 | inline void InputHandler::callListeners(orxonox::InputEvent &evt) |
---|
| 162 | { |
---|
| 163 | for (Iterator<InputEventListener> it = ObjectList<InputEventListener>::start(); it; ) |
---|
| 164 | { |
---|
| 165 | if (it->bActive_) |
---|
| 166 | (it++)->eventOccured(evt); |
---|
| 167 | else |
---|
| 168 | it++; |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /** |
---|
[919] | 173 | @brief Event handler for the keyPressed Event. |
---|
| 174 | @param e Event information |
---|
| 175 | */ |
---|
[918] | 176 | bool InputHandler::keyPressed(const OIS::KeyEvent &e) |
---|
| 177 | { |
---|
[922] | 178 | callListeners(this->bindingsKeyPressed_[(int)e.key]); |
---|
[918] | 179 | return true; |
---|
| 180 | } |
---|
| 181 | |
---|
[919] | 182 | /** |
---|
| 183 | @brief Event handler for the keyReleased Event. |
---|
| 184 | @param e Event information |
---|
| 185 | */ |
---|
[918] | 186 | bool InputHandler::keyReleased(const OIS::KeyEvent &e) |
---|
| 187 | { |
---|
| 188 | return true; |
---|
| 189 | } |
---|
| 190 | |
---|
[919] | 191 | /** |
---|
| 192 | @brief Event handler for the mouseMoved Event. |
---|
| 193 | @param e Event information |
---|
| 194 | */ |
---|
[918] | 195 | bool InputHandler::mouseMoved(const OIS::MouseEvent &e) |
---|
| 196 | { |
---|
| 197 | return true; |
---|
| 198 | } |
---|
| 199 | |
---|
[919] | 200 | /** |
---|
| 201 | @brief Event handler for the mousePressed Event. |
---|
| 202 | @param e Event information |
---|
| 203 | @param id The ID of the mouse button |
---|
| 204 | */ |
---|
[918] | 205 | bool InputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 206 | { |
---|
| 207 | return true; |
---|
| 208 | } |
---|
| 209 | |
---|
[919] | 210 | /** |
---|
| 211 | @brief Event handler for the mouseReleased Event. |
---|
| 212 | @param e Event information |
---|
| 213 | @param id The ID of the mouse button |
---|
| 214 | */ |
---|
[918] | 215 | bool InputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 216 | { |
---|
| 217 | return true; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | } |
---|