| 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: |
|---|
| 24 | * Some guy writing the example code from Ogre |
|---|
| 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 | |
|---|
| 36 | #include "core/CoreIncludes.h" |
|---|
| 37 | #include "Orxonox.h" |
|---|
| 38 | #include "InputEventListener.h" |
|---|
| 39 | #include "InputHandler.h" |
|---|
| 40 | |
|---|
| 41 | namespace orxonox |
|---|
| 42 | { |
|---|
| 43 | /** |
|---|
| 44 | @brief Constructor only resets the pointer values to 0. |
|---|
| 45 | */ |
|---|
| 46 | InputHandler::InputHandler() |
|---|
| 47 | { |
|---|
| 48 | //RegisterObject(InputHandler); |
|---|
| 49 | this->mouse_ = 0; |
|---|
| 50 | this->keyboard_ = 0; |
|---|
| 51 | this->inputSystem_ = 0; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 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 | */ |
|---|
| 58 | InputHandler *InputHandler::getSingleton() |
|---|
| 59 | { |
|---|
| 60 | static InputHandler theOnlyInstance; |
|---|
| 61 | return &theOnlyInstance; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | @brief Creates the OIS::InputMananger, the keyboard and the mouse and |
|---|
| 66 | assigns the key bindings. |
|---|
| 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 | { |
|---|
| 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 | |
|---|
| 83 | // Create inputsystem |
|---|
| 84 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
|---|
| 85 | |
|---|
| 86 | // If possible create a buffered keyboard |
|---|
| 87 | if (inputSystem_->numKeyboards() > 0) |
|---|
| 88 | { |
|---|
| 89 | keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true)); |
|---|
| 90 | keyboard_->setEventCallback(this); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | // If possible create a buffered mouse |
|---|
| 94 | if (inputSystem_->numMice() > 0 ) |
|---|
| 95 | { |
|---|
| 96 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
|---|
| 97 | mouse_->setEventCallback(this); |
|---|
| 98 | |
|---|
| 99 | // Set mouse region |
|---|
| 100 | this->setWindowExtents(windowWidth, windowHeight); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | // load the key bindings |
|---|
| 105 | InputEvent empty = {0, false, 0, 0, 0}; |
|---|
| 106 | for (int i = 0; i < this->numberOfKeys_; i++) |
|---|
| 107 | this->bindingsKeyPressed_[i] = empty; |
|---|
| 108 | |
|---|
| 109 | //assign 'abort' to the escape key |
|---|
| 110 | this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | @brief Updates the InputHandler |
|---|
| 115 | @param dt Delta time |
|---|
| 116 | */ |
|---|
| 117 | void InputHandler::tick(float dt) |
|---|
| 118 | { |
|---|
| 119 | // capture all the input. That calls the event handlers. |
|---|
| 120 | if (mouse_) |
|---|
| 121 | mouse_->capture(); |
|---|
| 122 | |
|---|
| 123 | if (keyboard_) |
|---|
| 124 | keyboard_->capture(); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | @brief Adjusts the mouse window metrics. |
|---|
| 129 | This method has to be called every time the size of the window changes. |
|---|
| 130 | @param width The new width of the render window |
|---|
| 131 | @param height the new height of the render window |
|---|
| 132 | */ |
|---|
| 133 | void InputHandler::setWindowExtents(int width, int height) |
|---|
| 134 | { |
|---|
| 135 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
|---|
| 136 | const OIS::MouseState &mouseState = mouse_->getMouseState(); |
|---|
| 137 | mouseState.width = width; |
|---|
| 138 | mouseState.height = height; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /** |
|---|
| 142 | @brief Calls all the objects from classes that derive from InputEventListener. |
|---|
| 143 | @param evt The input event that occured. |
|---|
| 144 | */ |
|---|
| 145 | inline void InputHandler::callListeners(orxonox::InputEvent &evt) |
|---|
| 146 | { |
|---|
| 147 | for (Iterator<InputEventListener> it = ObjectList<InputEventListener>::start(); it; ) |
|---|
| 148 | { |
|---|
| 149 | if (it->bActive_) |
|---|
| 150 | (it++)->eventOccured(evt); |
|---|
| 151 | else |
|---|
| 152 | it++; |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | /** |
|---|
| 157 | @brief Event handler for the keyPressed Event. |
|---|
| 158 | @param e Event information |
|---|
| 159 | */ |
|---|
| 160 | bool InputHandler::keyPressed(const OIS::KeyEvent &e) |
|---|
| 161 | { |
|---|
| 162 | callListeners(this->bindingsKeyPressed_[(int)e.key]); |
|---|
| 163 | return true; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | /** |
|---|
| 167 | @brief Event handler for the keyReleased Event. |
|---|
| 168 | @param e Event information |
|---|
| 169 | */ |
|---|
| 170 | bool InputHandler::keyReleased(const OIS::KeyEvent &e) |
|---|
| 171 | { |
|---|
| 172 | return true; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | /** |
|---|
| 176 | @brief Event handler for the mouseMoved Event. |
|---|
| 177 | @param e Event information |
|---|
| 178 | */ |
|---|
| 179 | bool InputHandler::mouseMoved(const OIS::MouseEvent &e) |
|---|
| 180 | { |
|---|
| 181 | return true; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | /** |
|---|
| 185 | @brief Event handler for the mousePressed Event. |
|---|
| 186 | @param e Event information |
|---|
| 187 | @param id The ID of the mouse button |
|---|
| 188 | */ |
|---|
| 189 | bool InputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
|---|
| 190 | { |
|---|
| 191 | return true; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | /** |
|---|
| 195 | @brief Event handler for the mouseReleased Event. |
|---|
| 196 | @param e Event information |
|---|
| 197 | @param id The ID of the mouse button |
|---|
| 198 | */ |
|---|
| 199 | bool InputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
|---|
| 200 | { |
|---|
| 201 | return true; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | } |
|---|