| 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 "InputEventListener.h" |
|---|
| 39 | #include "InputHandler.h" |
|---|
| 40 | #include "InputBuffer.h" |
|---|
| 41 | #include "ConsoleCommand.h" |
|---|
| 42 | |
|---|
| 43 | namespace orxonox |
|---|
| 44 | { |
|---|
| 45 | SetConsoleCommand(InputManager, setInputMode, true).setDefaultValue(0, IM_INGAME); |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | @brief Constructor only resets the pointer values to 0. |
|---|
| 49 | */ |
|---|
| 50 | InputManager::InputManager() : |
|---|
| 51 | inputSystem_(0), keyboard_(0), mouse_(0), |
|---|
| 52 | currentMode_(IM_UNINIT), setMode_(IM_UNINIT), |
|---|
| 53 | handlerGUI_(0), handlerBuffer_(0), handlerGame_(0) |
|---|
| 54 | { |
|---|
| 55 | RegisterObject(InputManager); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | @brief Destructor only called at the end of the program |
|---|
| 60 | */ |
|---|
| 61 | InputManager::~InputManager() |
|---|
| 62 | { |
|---|
| 63 | this->destroy(); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | @brief The one instance of the InputManager is stored in this function. |
|---|
| 68 | @return A reference to the only instance of the InputManager |
|---|
| 69 | */ |
|---|
| 70 | InputManager& InputManager::getSingleton() |
|---|
| 71 | { |
|---|
| 72 | static InputManager theOnlyInstance; |
|---|
| 73 | return theOnlyInstance; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | @brief Creates the OIS::InputMananger, the keyboard and the mouse and |
|---|
| 78 | assigns the key bindings. |
|---|
| 79 | @param windowHnd The window handle of the render window |
|---|
| 80 | @param windowWidth The width of the render window |
|---|
| 81 | @param windowHeight The height of the render window |
|---|
| 82 | */ |
|---|
| 83 | bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight) |
|---|
| 84 | { |
|---|
| 85 | if (!this->inputSystem_) |
|---|
| 86 | { |
|---|
| 87 | // Setup basic variables |
|---|
| 88 | OIS::ParamList paramList; |
|---|
| 89 | std::ostringstream windowHndStr; |
|---|
| 90 | |
|---|
| 91 | // Fill parameter list |
|---|
| 92 | windowHndStr << (unsigned int)windowHnd; |
|---|
| 93 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
|---|
| 94 | |
|---|
| 95 | #if defined OIS_LINUX_PLATFORM |
|---|
| 96 | paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); |
|---|
| 97 | #endif |
|---|
| 98 | |
|---|
| 99 | try |
|---|
| 100 | { |
|---|
| 101 | // Create inputsystem |
|---|
| 102 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
|---|
| 103 | COUT(ORX_DEBUG) << "*** InputManager: Created OIS input system" << std::endl; |
|---|
| 104 | |
|---|
| 105 | // create a keyboard. If none are available the exception is caught. |
|---|
| 106 | keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true)); |
|---|
| 107 | COUT(ORX_DEBUG) << "*** InputManager: Created OIS keyboard" << std::endl; |
|---|
| 108 | |
|---|
| 109 | if (this->handlerBuffer_) |
|---|
| 110 | this->handlerBuffer_->setKeyboard(this->keyboard_); |
|---|
| 111 | |
|---|
| 112 | // create a mouse. If none are available the exception is caught. |
|---|
| 113 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
|---|
| 114 | COUT(ORX_DEBUG) << "*** InputManager: Created OIS mouse" << std::endl; |
|---|
| 115 | |
|---|
| 116 | // Set mouse region |
|---|
| 117 | this->setWindowExtents(windowWidth, windowHeight); |
|---|
| 118 | } |
|---|
| 119 | catch (OIS::Exception ex) |
|---|
| 120 | { |
|---|
| 121 | // something went wrong with the initialisation |
|---|
| 122 | COUT(ORX_ERROR) << "Error: Failed creating an input system/keyboard/mouse. Message: \"" << ex.eText << "\"" << std::endl; |
|---|
| 123 | this->inputSystem_ = 0; |
|---|
| 124 | return false; |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | // create the handlers |
|---|
| 129 | this->handlerGUI_ = new InputHandlerGUI(); |
|---|
| 130 | this->handlerGame_ = new InputHandlerGame(); |
|---|
| 131 | //this->handlerBuffer_ = new InputBuffer(); |
|---|
| 132 | this->handlerGame_->loadBindings(); |
|---|
| 133 | |
|---|
| 134 | /*COUT(ORX_DEBUG) << "*** InputManager: Loading key bindings..." << std::endl; |
|---|
| 135 | // load the key bindings |
|---|
| 136 | InputEvent empty = {0, false, 0, 0, 0}; |
|---|
| 137 | for (int i = 0; i < this->numberOfKeys_; i++) |
|---|
| 138 | this->bindingsKeyPressed_[i] = empty; |
|---|
| 139 | |
|---|
| 140 | //assign 'abort' to the escape key |
|---|
| 141 | this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1; |
|---|
| 142 | COUT(ORX_DEBUG) << "*** InputManager: Loading done." << std::endl;*/ |
|---|
| 143 | |
|---|
| 144 | return true; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | /** |
|---|
| 148 | @brief Destroys all the created input devices and handlers. |
|---|
| 149 | */ |
|---|
| 150 | void InputManager::destroy() |
|---|
| 151 | { |
|---|
| 152 | COUT(ORX_DEBUG) << "*** InputManager: Destroying ..." << std::endl; |
|---|
| 153 | if (this->mouse_) |
|---|
| 154 | this->inputSystem_->destroyInputObject(mouse_); |
|---|
| 155 | if (this->keyboard_) |
|---|
| 156 | this->inputSystem_->destroyInputObject(keyboard_); |
|---|
| 157 | if (this->inputSystem_) |
|---|
| 158 | OIS::InputManager::destroyInputSystem(this->inputSystem_); |
|---|
| 159 | |
|---|
| 160 | this->mouse_ = 0; |
|---|
| 161 | this->keyboard_ = 0; |
|---|
| 162 | this->inputSystem_ = 0; |
|---|
| 163 | |
|---|
| 164 | if (this->handlerBuffer_) |
|---|
| 165 | delete this->handlerBuffer_; |
|---|
| 166 | if (this->handlerGame_) |
|---|
| 167 | delete this->handlerGame_; |
|---|
| 168 | if (this->handlerGUI_) |
|---|
| 169 | delete this->handlerGUI_; |
|---|
| 170 | |
|---|
| 171 | this->handlerBuffer_ = 0; |
|---|
| 172 | this->handlerGame_ = 0; |
|---|
| 173 | this->handlerGUI_ = 0; |
|---|
| 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 | // reset the game if it has changed |
|---|
| 185 | if (this->currentMode_ != this->setMode_) |
|---|
| 186 | { |
|---|
| 187 | switch (this->setMode_) |
|---|
| 188 | { |
|---|
| 189 | case IM_GUI: |
|---|
| 190 | this->mouse_->setEventCallback(this->handlerGUI_); |
|---|
| 191 | this->keyboard_->setEventCallback(this->handlerGUI_); |
|---|
| 192 | break; |
|---|
| 193 | case IM_INGAME: |
|---|
| 194 | this->mouse_->setEventCallback(this->handlerGame_); |
|---|
| 195 | this->keyboard_->setEventCallback(this->handlerGame_); |
|---|
| 196 | break; |
|---|
| 197 | case IM_KEYBOARD: |
|---|
| 198 | this->mouse_->setEventCallback(this->handlerGame_); |
|---|
| 199 | this->keyboard_->setEventCallback(this->handlerBuffer_); |
|---|
| 200 | break; |
|---|
| 201 | case IM_UNINIT: |
|---|
| 202 | this->mouse_->setEventCallback(0); |
|---|
| 203 | this->keyboard_->setEventCallback(0); |
|---|
| 204 | break; |
|---|
| 205 | } |
|---|
| 206 | this->currentMode_ = this->setMode_; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | // capture all the input. That calls the event handlers. |
|---|
| 210 | if (mouse_) |
|---|
| 211 | mouse_->capture(); |
|---|
| 212 | |
|---|
| 213 | if (keyboard_) |
|---|
| 214 | keyboard_->capture(); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | /** |
|---|
| 218 | @brief Adjusts the mouse window metrics. |
|---|
| 219 | This method has to be called every time the size of the window changes. |
|---|
| 220 | @param width The new width of the render window |
|---|
| 221 | @param height the new height of the render window |
|---|
| 222 | */ |
|---|
| 223 | void InputManager::setWindowExtents(int width, int height) |
|---|
| 224 | { |
|---|
| 225 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
|---|
| 226 | const OIS::MouseState &mouseState = mouse_->getMouseState(); |
|---|
| 227 | mouseState.width = width; |
|---|
| 228 | mouseState.height = height; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | /** |
|---|
| 232 | @brief Sets the input mode to either GUI, inGame or Buffer |
|---|
| 233 | @param mode The new input mode |
|---|
| 234 | @remark Only has an affect if the mode actually changes |
|---|
| 235 | */ |
|---|
| 236 | void InputManager::setInputMode(int mode) |
|---|
| 237 | { |
|---|
| 238 | if (mode > 0 && mode < 4) |
|---|
| 239 | getSingleton().setMode_ = (InputMode)mode; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | /** |
|---|
| 243 | @brief Returns the current input handling method |
|---|
| 244 | @return The current input mode. |
|---|
| 245 | */ |
|---|
| 246 | InputMode InputManager::getInputMode() |
|---|
| 247 | { |
|---|
| 248 | return this->currentMode_; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | void InputManager::feedInputBuffer(InputBuffer* buffer) |
|---|
| 252 | { |
|---|
| 253 | this->handlerBuffer_ = buffer; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | } |
|---|