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