[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 | */ |
---|
[929] | 46 | InputHandler::InputHandler() : |
---|
| 47 | mouse_(0), keyboard_(0), inputSystem_(0), |
---|
| 48 | uninitialized_(true) |
---|
[918] | 49 | { |
---|
[919] | 50 | //RegisterObject(InputHandler); |
---|
[918] | 51 | } |
---|
| 52 | |
---|
[919] | 53 | /** |
---|
[929] | 54 | @brief Destructor only called at the end of the program |
---|
| 55 | */ |
---|
| 56 | InputHandler::~InputHandler() |
---|
| 57 | { |
---|
| 58 | this->destroy(); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
[919] | 62 | @brief The one instance of the InputHandler is stored in this function. |
---|
| 63 | @return The pointer to the only instance of the InputHandler |
---|
| 64 | */ |
---|
[922] | 65 | InputHandler *InputHandler::getSingleton() |
---|
[919] | 66 | { |
---|
| 67 | static InputHandler theOnlyInstance; |
---|
| 68 | return &theOnlyInstance; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
[922] | 72 | @brief Creates the OIS::InputMananger, the keyboard and the mouse and |
---|
| 73 | assigns the key bindings. |
---|
[919] | 74 | @param windowHnd The window handle of the render window |
---|
| 75 | @param windowWidth The width of the render window |
---|
| 76 | @param windowHeight The height of the render window |
---|
| 77 | */ |
---|
| 78 | void InputHandler::initialise(size_t windowHnd, int windowWidth, int windowHeight) |
---|
| 79 | { |
---|
[929] | 80 | if (this->uninitialized_ || !this->inputSystem_) |
---|
[918] | 81 | { |
---|
| 82 | // Setup basic variables |
---|
| 83 | OIS::ParamList paramList; |
---|
| 84 | std::ostringstream windowHndStr; |
---|
| 85 | |
---|
| 86 | // Fill parameter list |
---|
| 87 | windowHndStr << (unsigned int)windowHnd; |
---|
| 88 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
| 89 | |
---|
[928] | 90 | #if defined OIS_LINUX_PLATFORM |
---|
| 91 | paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); |
---|
| 92 | #endif |
---|
| 93 | |
---|
[918] | 94 | // Create inputsystem |
---|
| 95 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
---|
| 96 | |
---|
| 97 | // If possible create a buffered keyboard |
---|
| 98 | if (inputSystem_->numKeyboards() > 0) |
---|
| 99 | { |
---|
| 100 | keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true)); |
---|
| 101 | keyboard_->setEventCallback(this); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | // If possible create a buffered mouse |
---|
| 105 | if (inputSystem_->numMice() > 0 ) |
---|
| 106 | { |
---|
| 107 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
---|
| 108 | mouse_->setEventCallback(this); |
---|
| 109 | |
---|
| 110 | // Set mouse region |
---|
[919] | 111 | this->setWindowExtents(windowWidth, windowHeight); |
---|
[918] | 112 | } |
---|
[929] | 113 | |
---|
| 114 | uninitialized_ = false; |
---|
[918] | 115 | } |
---|
[922] | 116 | |
---|
| 117 | // load the key bindings |
---|
| 118 | InputEvent empty = {0, false, 0, 0, 0}; |
---|
| 119 | for (int i = 0; i < this->numberOfKeys_; i++) |
---|
| 120 | this->bindingsKeyPressed_[i] = empty; |
---|
| 121 | |
---|
| 122 | //assign 'abort' to the escape key |
---|
| 123 | this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1; |
---|
[918] | 124 | } |
---|
| 125 | |
---|
[919] | 126 | /** |
---|
[928] | 127 | @brief Destroys all the created input devices. |
---|
| 128 | */ |
---|
| 129 | void InputHandler::destroy() |
---|
| 130 | { |
---|
[930] | 131 | if (this->mouse_) |
---|
| 132 | this->inputSystem_->destroyInputObject(mouse_); |
---|
| 133 | if (this->keyboard_) |
---|
| 134 | this->inputSystem_->destroyInputObject(keyboard_); |
---|
| 135 | if (this->inputSystem_) |
---|
[929] | 136 | OIS::InputManager::destroyInputSystem(this->inputSystem_); |
---|
[928] | 137 | |
---|
[930] | 138 | this->mouse_ = 0; |
---|
| 139 | this->keyboard_ = 0; |
---|
| 140 | this->inputSystem_ = 0; |
---|
[929] | 141 | this->uninitialized_ = true; |
---|
[928] | 142 | } |
---|
| 143 | |
---|
| 144 | /** |
---|
[919] | 145 | @brief Updates the InputHandler |
---|
| 146 | @param dt Delta time |
---|
| 147 | */ |
---|
[918] | 148 | void InputHandler::tick(float dt) |
---|
| 149 | { |
---|
| 150 | // capture all the input. That calls the event handlers. |
---|
| 151 | if (mouse_) |
---|
| 152 | mouse_->capture(); |
---|
| 153 | |
---|
| 154 | if (keyboard_) |
---|
| 155 | keyboard_->capture(); |
---|
| 156 | } |
---|
| 157 | |
---|
[919] | 158 | /** |
---|
| 159 | @brief Adjusts the mouse window metrics. |
---|
| 160 | This method has to be called every time the size of the window changes. |
---|
| 161 | @param width The new width of the render window |
---|
| 162 | @param height the new height of the render window |
---|
| 163 | */ |
---|
[918] | 164 | void InputHandler::setWindowExtents(int width, int height) |
---|
| 165 | { |
---|
| 166 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
---|
| 167 | const OIS::MouseState &mouseState = mouse_->getMouseState(); |
---|
| 168 | mouseState.width = width; |
---|
| 169 | mouseState.height = height; |
---|
| 170 | } |
---|
| 171 | |
---|
[919] | 172 | /** |
---|
[922] | 173 | @brief Calls all the objects from classes that derive from InputEventListener. |
---|
| 174 | @param evt The input event that occured. |
---|
| 175 | */ |
---|
| 176 | inline void InputHandler::callListeners(orxonox::InputEvent &evt) |
---|
| 177 | { |
---|
| 178 | for (Iterator<InputEventListener> it = ObjectList<InputEventListener>::start(); it; ) |
---|
| 179 | { |
---|
| 180 | if (it->bActive_) |
---|
| 181 | (it++)->eventOccured(evt); |
---|
| 182 | else |
---|
| 183 | it++; |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /** |
---|
[919] | 188 | @brief Event handler for the keyPressed Event. |
---|
| 189 | @param e Event information |
---|
| 190 | */ |
---|
[918] | 191 | bool InputHandler::keyPressed(const OIS::KeyEvent &e) |
---|
| 192 | { |
---|
[922] | 193 | callListeners(this->bindingsKeyPressed_[(int)e.key]); |
---|
[918] | 194 | return true; |
---|
| 195 | } |
---|
| 196 | |
---|
[919] | 197 | /** |
---|
| 198 | @brief Event handler for the keyReleased Event. |
---|
| 199 | @param e Event information |
---|
| 200 | */ |
---|
[918] | 201 | bool InputHandler::keyReleased(const OIS::KeyEvent &e) |
---|
| 202 | { |
---|
| 203 | return true; |
---|
| 204 | } |
---|
| 205 | |
---|
[919] | 206 | /** |
---|
| 207 | @brief Event handler for the mouseMoved Event. |
---|
| 208 | @param e Event information |
---|
| 209 | */ |
---|
[918] | 210 | bool InputHandler::mouseMoved(const OIS::MouseEvent &e) |
---|
| 211 | { |
---|
| 212 | return true; |
---|
| 213 | } |
---|
| 214 | |
---|
[919] | 215 | /** |
---|
| 216 | @brief Event handler for the mousePressed Event. |
---|
| 217 | @param e Event information |
---|
| 218 | @param id The ID of the mouse button |
---|
| 219 | */ |
---|
[918] | 220 | bool InputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 221 | { |
---|
| 222 | return true; |
---|
| 223 | } |
---|
| 224 | |
---|
[919] | 225 | /** |
---|
| 226 | @brief Event handler for the mouseReleased Event. |
---|
| 227 | @param e Event information |
---|
| 228 | @param id The ID of the mouse button |
---|
| 229 | */ |
---|
[918] | 230 | bool InputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
| 231 | { |
---|
| 232 | return true; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | } |
---|