| 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 | * ... |
|---|
| 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 "core/Debug.h" |
|---|
| 38 | #include "Orxonox.h" |
|---|
| 39 | #include "InputEventListener.h" |
|---|
| 40 | #include "InputHandler.h" |
|---|
| 41 | |
|---|
| 42 | namespace orxonox |
|---|
| 43 | { |
|---|
| 44 | /** |
|---|
| 45 | @brief The reference to the singleton |
|---|
| 46 | */ |
|---|
| 47 | InputHandler* InputHandler::singletonRef_s = 0; |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | @brief Constructor only resets the pointer values to 0. |
|---|
| 51 | */ |
|---|
| 52 | InputHandler::InputHandler() : |
|---|
| 53 | mouse_(0), keyboard_(0), inputSystem_(0) |
|---|
| 54 | { |
|---|
| 55 | //RegisterObject(InputHandler); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | @brief Destructor only called at the end of the program |
|---|
| 60 | */ |
|---|
| 61 | InputHandler::~InputHandler() |
|---|
| 62 | { |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | @brief The one instance of the InputHandler is stored in this function. |
|---|
| 67 | @return The pointer to the only instance of the InputHandler |
|---|
| 68 | */ |
|---|
| 69 | InputHandler *InputHandler::getSingleton() |
|---|
| 70 | { |
|---|
| 71 | if (!singletonRef_s) |
|---|
| 72 | singletonRef_s = new InputHandler(); |
|---|
| 73 | return singletonRef_s; |
|---|
| 74 | //static InputHandler theOnlyInstance; |
|---|
| 75 | //return &theOnlyInstance; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | @brief Creates the OIS::InputMananger, the keyboard and the mouse and |
|---|
| 80 | assigns the key bindings. |
|---|
| 81 | @param windowHnd The window handle of the render window |
|---|
| 82 | @param windowWidth The width of the render window |
|---|
| 83 | @param windowHeight The height of the render window |
|---|
| 84 | */ |
|---|
| 85 | bool InputHandler::initialise(size_t windowHnd, int windowWidth, int windowHeight) |
|---|
| 86 | { |
|---|
| 87 | if (!this->inputSystem_) |
|---|
| 88 | { |
|---|
| 89 | // Setup basic variables |
|---|
| 90 | OIS::ParamList paramList; |
|---|
| 91 | std::ostringstream windowHndStr; |
|---|
| 92 | |
|---|
| 93 | // Fill parameter list |
|---|
| 94 | windowHndStr << (unsigned int)windowHnd; |
|---|
| 95 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
|---|
| 96 | |
|---|
| 97 | #if defined OIS_LINUX_PLATFORM |
|---|
| 98 | paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); |
|---|
| 99 | #endif |
|---|
| 100 | |
|---|
| 101 | try |
|---|
| 102 | { |
|---|
| 103 | // Create inputsystem |
|---|
| 104 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
|---|
| 105 | //if (getSoftDebugLevel() >= ORX_DEBUG) |
|---|
| 106 | // orxonox::OutputHandler::getOutStream().setOutputLevel(4) << "asdfblah" << std::endl; |
|---|
| 107 | COUT(ORX_DEBUG) << "*** InputHandler: Created OIS input system" << std::endl; |
|---|
| 108 | |
|---|
| 109 | // create a keyboard. If none are available the exception is caught. |
|---|
| 110 | keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true)); |
|---|
| 111 | keyboard_->setEventCallback(this); |
|---|
| 112 | COUT(ORX_DEBUG) << "*** InputHandler: Created OIS mouse" << std::endl; |
|---|
| 113 | |
|---|
| 114 | // create a mouse. If none are available the exception is caught. |
|---|
| 115 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
|---|
| 116 | mouse_->setEventCallback(this); |
|---|
| 117 | COUT(ORX_DEBUG) << "*** InputHandler: Created OIS keyboard" << std::endl; |
|---|
| 118 | |
|---|
| 119 | // Set mouse region |
|---|
| 120 | this->setWindowExtents(windowWidth, windowHeight); |
|---|
| 121 | } |
|---|
| 122 | catch (OIS::Exception ex) |
|---|
| 123 | { |
|---|
| 124 | // something went wrong with the initialisation |
|---|
| 125 | COUT(ORX_ERROR) << "Error: Failed creating an input system. Message: \"" << ex.eText << "\"" << std::endl; |
|---|
| 126 | this->inputSystem_ = 0; |
|---|
| 127 | return false; |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | COUT(ORX_DEBUG) << "*** InputHandler: Loading key bindings..." << std::endl; |
|---|
| 132 | // temporary solution: create event list |
|---|
| 133 | //InputEvent[] list = this->createEventList(); |
|---|
| 134 | // load the key bindings |
|---|
| 135 | InputEvent empty = {0, false, 0, 0, 0}; |
|---|
| 136 | for (int i = 0; i < this->numberOfKeys_; i++) |
|---|
| 137 | this->bindingsKeyPressed_[i] = empty; |
|---|
| 138 | |
|---|
| 139 | //assign 'abort' to the escape key |
|---|
| 140 | this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1; |
|---|
| 141 | COUT(ORX_DEBUG) << "*** InputHandler: Loading done." << std::endl; |
|---|
| 142 | |
|---|
| 143 | return true; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | @brief Destroys all the created input devices. |
|---|
| 148 | */ |
|---|
| 149 | void InputHandler::destroyDevices() |
|---|
| 150 | { |
|---|
| 151 | COUT(ORX_DEBUG) << "*** InputHandler: Destroying InputHandler..." << std::endl; |
|---|
| 152 | if (this->mouse_) |
|---|
| 153 | this->inputSystem_->destroyInputObject(mouse_); |
|---|
| 154 | if (this->keyboard_) |
|---|
| 155 | this->inputSystem_->destroyInputObject(keyboard_); |
|---|
| 156 | if (this->inputSystem_) |
|---|
| 157 | OIS::InputManager::destroyInputSystem(this->inputSystem_); |
|---|
| 158 | |
|---|
| 159 | this->mouse_ = 0; |
|---|
| 160 | this->keyboard_ = 0; |
|---|
| 161 | this->inputSystem_ = 0; |
|---|
| 162 | COUT(ORX_DEBUG) << "*** InputHandler: Destroying done." << std::endl; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | /** |
|---|
| 166 | @brief Destroys the singleton. |
|---|
| 167 | */ |
|---|
| 168 | void InputHandler::destroy() |
|---|
| 169 | { |
|---|
| 170 | if (singletonRef_s) |
|---|
| 171 | delete singletonRef_s; |
|---|
| 172 | singletonRef_s = 0; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | /** |
|---|
| 176 | @brief Updates the InputHandler |
|---|
| 177 | @param dt Delta time |
|---|
| 178 | */ |
|---|
| 179 | void InputHandler::tick(float dt) |
|---|
| 180 | { |
|---|
| 181 | // capture all the input. That calls the event handlers. |
|---|
| 182 | if (mouse_) |
|---|
| 183 | mouse_->capture(); |
|---|
| 184 | |
|---|
| 185 | if (keyboard_) |
|---|
| 186 | keyboard_->capture(); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /** |
|---|
| 190 | @brief Adjusts the mouse window metrics. |
|---|
| 191 | This method has to be called every time the size of the window changes. |
|---|
| 192 | @param width The new width of the render window |
|---|
| 193 | @param height the new height of the render window |
|---|
| 194 | */ |
|---|
| 195 | void InputHandler::setWindowExtents(int width, int height) |
|---|
| 196 | { |
|---|
| 197 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
|---|
| 198 | const OIS::MouseState &mouseState = mouse_->getMouseState(); |
|---|
| 199 | mouseState.width = width; |
|---|
| 200 | mouseState.height = height; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | /** |
|---|
| 204 | @brief Calls all the objects from classes that derive from InputEventListener. |
|---|
| 205 | @param evt The input event that occured. |
|---|
| 206 | */ |
|---|
| 207 | inline void InputHandler::callListeners(orxonox::InputEvent &evt) |
|---|
| 208 | { |
|---|
| 209 | for (Iterator<InputEventListener> it = ObjectList<InputEventListener>::start(); it; ) |
|---|
| 210 | { |
|---|
| 211 | if (it->bActive_) |
|---|
| 212 | (it++)->eventOccured(evt); |
|---|
| 213 | else |
|---|
| 214 | it++; |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | /** |
|---|
| 219 | @brief Event handler for the keyPressed Event. |
|---|
| 220 | @param e Event information |
|---|
| 221 | */ |
|---|
| 222 | bool InputHandler::keyPressed(const OIS::KeyEvent &e) |
|---|
| 223 | { |
|---|
| 224 | callListeners(this->bindingsKeyPressed_[(int)e.key]); |
|---|
| 225 | return true; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | /** |
|---|
| 229 | @brief Event handler for the keyReleased Event. |
|---|
| 230 | @param e Event information |
|---|
| 231 | */ |
|---|
| 232 | bool InputHandler::keyReleased(const OIS::KeyEvent &e) |
|---|
| 233 | { |
|---|
| 234 | return true; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | /** |
|---|
| 238 | @brief Event handler for the mouseMoved Event. |
|---|
| 239 | @param e Event information |
|---|
| 240 | */ |
|---|
| 241 | bool InputHandler::mouseMoved(const OIS::MouseEvent &e) |
|---|
| 242 | { |
|---|
| 243 | return true; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | /** |
|---|
| 247 | @brief Event handler for the mousePressed Event. |
|---|
| 248 | @param e Event information |
|---|
| 249 | @param id The ID of the mouse button |
|---|
| 250 | */ |
|---|
| 251 | bool InputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
|---|
| 252 | { |
|---|
| 253 | return true; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | /** |
|---|
| 257 | @brief Event handler for the mouseReleased Event. |
|---|
| 258 | @param e Event information |
|---|
| 259 | @param id The ID of the mouse button |
|---|
| 260 | */ |
|---|
| 261 | bool InputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
|---|
| 262 | { |
|---|
| 263 | return true; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | } |
|---|