[918] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1502] | 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 | /** |
---|
[1755] | 30 | @file |
---|
| 31 | @brief |
---|
[3327] | 32 | Implementation of the InputManager and a static variable from the InputHandler. |
---|
| 33 | */ |
---|
[918] | 34 | |
---|
[1062] | 35 | #include "InputManager.h" |
---|
[1519] | 36 | |
---|
[3327] | 37 | #include <cassert> |
---|
[1755] | 38 | #include <climits> |
---|
[3196] | 39 | #include <ois/OISException.h> |
---|
| 40 | #include <ois/OISInputManager.h> |
---|
[3327] | 41 | #include <boost/foreach.hpp> |
---|
[7266] | 42 | #include <loki/ScopeGuard.h> |
---|
[1555] | 43 | |
---|
[5929] | 44 | #include "util/Clock.h" |
---|
[5695] | 45 | #include "util/Convert.h" |
---|
[1764] | 46 | #include "util/Exception.h" |
---|
[1519] | 47 | #include "core/CoreIncludes.h" |
---|
[5695] | 48 | #include "core/GraphicsManager.h" |
---|
[9667] | 49 | #include "core/config/ConfigValueIncludes.h" |
---|
| 50 | #include "core/config/CommandLineParser.h" |
---|
[7284] | 51 | #include "core/command/ConsoleCommand.h" |
---|
| 52 | #include "core/command/Functor.h" |
---|
[1555] | 53 | |
---|
[1219] | 54 | #include "InputBuffer.h" |
---|
[3327] | 55 | #include "JoyStick.h" |
---|
| 56 | #include "JoyStickQuantityListener.h" |
---|
| 57 | #include "Mouse.h" |
---|
| 58 | #include "Keyboard.h" |
---|
[918] | 59 | |
---|
[9723] | 60 | #include "WiiMote.h" |
---|
| 61 | |
---|
[9790] | 62 | |
---|
[918] | 63 | namespace orxonox |
---|
| 64 | { |
---|
[3280] | 65 | SetCommandLineSwitch(keyboard_no_grab).information("Whether not to exclusively grab the keyboard"); |
---|
[1502] | 66 | |
---|
[7284] | 67 | static const std::string __CC_InputManager_name = "InputManager"; |
---|
| 68 | static const std::string __CC_calibrate_name = "calibrate"; |
---|
| 69 | static const std::string __CC_reload_name = "reload"; |
---|
| 70 | |
---|
| 71 | SetConsoleCommand(__CC_InputManager_name, __CC_calibrate_name, &InputManager::calibrate).addShortcut(); |
---|
| 72 | SetConsoleCommand(__CC_InputManager_name, __CC_reload_name, &InputManager::reload ); |
---|
| 73 | |
---|
[3327] | 74 | // Abuse of this source file for the InputHandler |
---|
| 75 | InputHandler InputHandler::EMPTY; |
---|
| 76 | |
---|
[3370] | 77 | InputManager* InputManager::singletonPtr_s = 0; |
---|
[1084] | 78 | |
---|
[3327] | 79 | //! Defines the |= operator for easier use. |
---|
| 80 | inline InputManager::State operator|=(InputManager::State& lval, InputManager::State rval) |
---|
[1755] | 81 | { |
---|
[3327] | 82 | return (lval = (InputManager::State)(lval | rval)); |
---|
[1755] | 83 | } |
---|
[919] | 84 | |
---|
[3327] | 85 | //! Defines the &= operator for easier use. |
---|
| 86 | inline InputManager::State operator&=(InputManager::State& lval, int rval) |
---|
[1755] | 87 | { |
---|
[3327] | 88 | return (lval = (InputManager::State)(lval & rval)); |
---|
[1755] | 89 | } |
---|
[1219] | 90 | |
---|
[1755] | 91 | // ############################################################ |
---|
| 92 | // ##### Initialisation ##### |
---|
| 93 | // ########## ########## |
---|
| 94 | // ############################################################ |
---|
[5695] | 95 | InputManager::InputManager() |
---|
[3327] | 96 | : internalState_(Bad) |
---|
| 97 | , oisInputManager_(0) |
---|
| 98 | , devices_(2) |
---|
[8729] | 99 | , exclusiveMouse_(false) |
---|
[3327] | 100 | , emptyState_(0) |
---|
| 101 | , calibratorCallbackHandler_(0) |
---|
[918] | 102 | { |
---|
[9667] | 103 | RegisterObject(InputManager); |
---|
[1219] | 104 | |
---|
[8858] | 105 | orxout(internal_status, context::input) << "InputManager: Constructing..." << endl; |
---|
[3327] | 106 | |
---|
[6746] | 107 | // Allocate space for the function call buffer |
---|
| 108 | this->callBuffer_.reserve(16); |
---|
| 109 | |
---|
[3327] | 110 | this->setConfigValues(); |
---|
| 111 | |
---|
[5929] | 112 | if (GraphicsManager::getInstance().isFullScreen()) |
---|
[8729] | 113 | exclusiveMouse_ = true; |
---|
[5695] | 114 | this->loadDevices(); |
---|
[3327] | 115 | |
---|
| 116 | // Lowest priority empty InputState |
---|
| 117 | emptyState_ = createInputState("empty", false, false, InputStatePriority::Empty); |
---|
| 118 | emptyState_->setHandler(&InputHandler::EMPTY); |
---|
| 119 | activeStates_[emptyState_->getPriority()] = emptyState_; |
---|
| 120 | |
---|
| 121 | // Joy stick calibration helper callback |
---|
| 122 | InputState* calibrator = createInputState("calibrator", false, false, InputStatePriority::Calibrator); |
---|
| 123 | calibrator->setHandler(&InputHandler::EMPTY); |
---|
| 124 | calibratorCallbackHandler_ = new InputBuffer(); |
---|
| 125 | calibratorCallbackHandler_->registerListener(this, &InputManager::stopCalibration, '\r', true); |
---|
| 126 | calibrator->setKeyHandler(calibratorCallbackHandler_); |
---|
| 127 | |
---|
| 128 | this->updateActiveStates(); |
---|
| 129 | |
---|
[7284] | 130 | ModifyConsoleCommand(__CC_InputManager_name, __CC_calibrate_name).setObject(this); |
---|
| 131 | ModifyConsoleCommand(__CC_InputManager_name, __CC_reload_name).setObject(this); |
---|
[3327] | 132 | |
---|
[8858] | 133 | orxout(internal_status, context::input) << "InputManager: Construction complete." << endl; |
---|
[3327] | 134 | internalState_ = Nothing; |
---|
[1755] | 135 | } |
---|
[918] | 136 | |
---|
[2662] | 137 | void InputManager::setConfigValues() |
---|
| 138 | { |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | @brief |
---|
[1755] | 143 | Creates the OIS::InputMananger, the keyboard, the mouse and |
---|
[3327] | 144 | the joys ticks. If either of the first two fail, this method throws an exception. |
---|
[1755] | 145 | */ |
---|
[5695] | 146 | void InputManager::loadDevices() |
---|
[1755] | 147 | { |
---|
[8858] | 148 | orxout(verbose, context::input) << "InputManager: Loading input devices..." << endl; |
---|
[1755] | 149 | |
---|
[3327] | 150 | // When loading the devices they should not already be loaded |
---|
| 151 | assert(internalState_ & Bad); |
---|
| 152 | assert(devices_[InputDeviceEnumerator::Mouse] == 0); |
---|
| 153 | assert(devices_[InputDeviceEnumerator::Keyboard] == 0); |
---|
| 154 | assert(devices_.size() == InputDeviceEnumerator::FirstJoyStick); |
---|
[1755] | 155 | |
---|
[8351] | 156 | typedef std::pair<std::string, std::string> StringPair; |
---|
| 157 | |
---|
[5695] | 158 | // Fill parameter list |
---|
[3327] | 159 | OIS::ParamList paramList; |
---|
[5695] | 160 | size_t windowHnd = GraphicsManager::getInstance().getRenderWindowHandle(); |
---|
[8351] | 161 | paramList.insert(StringPair("WINDOW", multi_cast<std::string>(windowHnd))); |
---|
[3280] | 162 | #if defined(ORXONOX_PLATFORM_WINDOWS) |
---|
[8351] | 163 | paramList.insert(StringPair("w32_keyboard", "DISCL_NONEXCLUSIVE")); |
---|
| 164 | paramList.insert(StringPair("w32_keyboard", "DISCL_FOREGROUND")); |
---|
| 165 | paramList.insert(StringPair("w32_mouse", "DISCL_FOREGROUND")); |
---|
[8729] | 166 | if (exclusiveMouse_ || GraphicsManager::getInstance().isFullScreen()) |
---|
[5695] | 167 | { |
---|
| 168 | // Disable Windows key plus special keys (like play, stop, next, etc.) |
---|
[8351] | 169 | paramList.insert(StringPair("w32_keyboard", "DISCL_NOWINKEY")); |
---|
| 170 | paramList.insert(StringPair("w32_mouse", "DISCL_EXCLUSIVE")); |
---|
[5695] | 171 | } |
---|
| 172 | else |
---|
[8351] | 173 | paramList.insert(StringPair("w32_mouse", "DISCL_NONEXCLUSIVE")); |
---|
[3280] | 174 | #elif defined(ORXONOX_PLATFORM_LINUX) |
---|
[5695] | 175 | // Enabling this is probably a bad idea, but whenever orxonox crashes, the setting stays on |
---|
| 176 | // Trouble might be that the Pressed event occurs a bit too often... |
---|
[8351] | 177 | paramList.insert(StringPair("XAutoRepeatOn", "true")); |
---|
[5695] | 178 | |
---|
[8729] | 179 | if (exclusiveMouse_ || GraphicsManager::getInstance().isFullScreen()) |
---|
[5695] | 180 | { |
---|
[9550] | 181 | if (CommandLineParser::getValue("keyboard_no_grab").get<bool>()) |
---|
[8351] | 182 | paramList.insert(StringPair("x11_keyboard_grab", "false")); |
---|
[5695] | 183 | else |
---|
[8351] | 184 | paramList.insert(StringPair("x11_keyboard_grab", "true")); |
---|
| 185 | paramList.insert(StringPair("x11_mouse_grab", "true")); |
---|
| 186 | paramList.insert(StringPair("x11_mouse_hide", "true")); |
---|
[5695] | 187 | } |
---|
[3327] | 188 | else |
---|
[5695] | 189 | { |
---|
[8351] | 190 | paramList.insert(StringPair("x11_keyboard_grab", "false")); |
---|
| 191 | paramList.insert(StringPair("x11_mouse_grab", "false")); |
---|
| 192 | paramList.insert(StringPair("x11_mouse_hide", "false")); |
---|
[5695] | 193 | } |
---|
[1735] | 194 | #endif |
---|
[928] | 195 | |
---|
[3327] | 196 | try |
---|
| 197 | { |
---|
| 198 | oisInputManager_ = OIS::InputManager::createInputSystem(paramList); |
---|
| 199 | // Exception-safety |
---|
| 200 | Loki::ScopeGuard guard = Loki::MakeGuard(OIS::InputManager::destroyInputSystem, oisInputManager_); |
---|
[8858] | 201 | orxout(verbose, context::input) << "Created OIS input manager." << endl; |
---|
[1219] | 202 | |
---|
[3327] | 203 | if (oisInputManager_->getNumberOfDevices(OIS::OISKeyboard) > 0) |
---|
| 204 | devices_[InputDeviceEnumerator::Keyboard] = new Keyboard(InputDeviceEnumerator::Keyboard, oisInputManager_); |
---|
| 205 | else |
---|
| 206 | ThrowException(InitialisationFailed, "InputManager: No keyboard found, cannot proceed!"); |
---|
[1219] | 207 | |
---|
[3327] | 208 | // Successful initialisation |
---|
| 209 | guard.Dismiss(); |
---|
[1755] | 210 | } |
---|
[3331] | 211 | catch (const std::exception& ex) |
---|
[1755] | 212 | { |
---|
[3327] | 213 | oisInputManager_ = NULL; |
---|
| 214 | internalState_ |= Bad; |
---|
| 215 | ThrowException(InitialisationFailed, "Could not initialise the input system: " << ex.what()); |
---|
[1755] | 216 | } |
---|
[1502] | 217 | |
---|
[3327] | 218 | this->loadMouse(); |
---|
| 219 | this->loadJoySticks(); |
---|
[9790] | 220 | this->loadWiiMote(); |
---|
[3327] | 221 | // Reorder states in case some joy sticks were added/removed |
---|
| 222 | this->updateActiveStates(); |
---|
[9790] | 223 | |
---|
[8858] | 224 | orxout(verbose, context::input) << "Input devices loaded." << endl; |
---|
[1219] | 225 | } |
---|
[9790] | 226 | |
---|
[9723] | 227 | void InputManager::loadWiiMote() |
---|
| 228 | { |
---|
[9790] | 229 | |
---|
| 230 | CWii wii; // Defaults to 4 remotes |
---|
| 231 | std::vector< ::CWiimote>::iterator i; |
---|
| 232 | int reloadWiimotes = 0; |
---|
| 233 | int index; |
---|
| 234 | |
---|
| 235 | // Find and connect to the wiimotes |
---|
[9817] | 236 | std::vector<CWiimote>& wiimotes = wii.FindAndConnect(10); |
---|
[9790] | 237 | if (!wiimotes.size()) |
---|
[9723] | 238 | { |
---|
[9790] | 239 | cout << "No wiimotes found." << endl; |
---|
[9813] | 240 | return; |
---|
[9790] | 241 | } |
---|
| 242 | |
---|
| 243 | // Setup the wiimotes |
---|
| 244 | for(index = 0, i = wiimotes.begin(); i != wiimotes.end(); ++i, ++index) |
---|
| 245 | { |
---|
| 246 | // Use a reference to make working with the iterator handy. |
---|
| 247 | CWiimote & wiimote = *i; |
---|
| 248 | |
---|
| 249 | //Set Leds |
---|
| 250 | int LED_MAP[4] = |
---|
| 251 | {CWiimote::LED_1, CWiimote::LED_2, |
---|
| 252 | CWiimote::LED_3, CWiimote::LED_4}; |
---|
| 253 | wiimote.SetLEDs(LED_MAP[index]); |
---|
[9816] | 254 | try |
---|
| 255 | { |
---|
| 256 | orxout()<< "Size of devices vector before wiimote insertion:" << devices_.size() << std::endl; |
---|
[9838] | 257 | devices_.push_back(new WiiMote((unsigned int)devices_.size(), *i, wii)); |
---|
[9816] | 258 | //devices_[2] = new WiiMote(devices_.size(), *(new CWiimote())); |
---|
| 259 | orxout()<< "Size of devices vector after wiimote insertion:" << devices_.size() << std::endl; |
---|
[9790] | 260 | |
---|
[9816] | 261 | } |
---|
| 262 | catch(std::exception& e) //gotta catch em all |
---|
| 263 | { |
---|
| 264 | orxout()<<"Exception loading WiiMote!!!1!11!"; |
---|
| 265 | } |
---|
[9790] | 266 | |
---|
[9816] | 267 | |
---|
[9819] | 268 | |
---|
[9723] | 269 | } |
---|
[9790] | 270 | |
---|
| 271 | |
---|
[9723] | 272 | } |
---|
[3327] | 273 | //! Creates a new orxonox::Mouse |
---|
| 274 | void InputManager::loadMouse() |
---|
[1219] | 275 | { |
---|
[3327] | 276 | if (oisInputManager_->getNumberOfDevices(OIS::OISMouse) > 0) |
---|
[1755] | 277 | { |
---|
[3327] | 278 | try |
---|
[1755] | 279 | { |
---|
[3327] | 280 | devices_[InputDeviceEnumerator::Mouse] = new Mouse(InputDeviceEnumerator::Mouse, oisInputManager_); |
---|
[1755] | 281 | } |
---|
[3331] | 282 | catch (const std::exception& ex) |
---|
[1755] | 283 | { |
---|
[8858] | 284 | orxout(user_warning, context::input) << "Failed to create Mouse:" << ex.what() << '\n' |
---|
| 285 | << "Proceeding without mouse support." << endl; |
---|
[1755] | 286 | } |
---|
[1219] | 287 | } |
---|
[3327] | 288 | else |
---|
[8858] | 289 | orxout(user_warning, context::input) << "No mouse found! Proceeding without mouse support." << endl; |
---|
[1219] | 290 | } |
---|
[1755] | 291 | |
---|
[3327] | 292 | //! Creates as many joy sticks as are available. |
---|
| 293 | void InputManager::loadJoySticks() |
---|
[1219] | 294 | { |
---|
[3327] | 295 | for (int i = 0; i < oisInputManager_->getNumberOfDevices(OIS::OISJoyStick); i++) |
---|
[1755] | 296 | { |
---|
[3327] | 297 | try |
---|
[1755] | 298 | { |
---|
[3327] | 299 | devices_.push_back(new JoyStick(InputDeviceEnumerator::FirstJoyStick + i, oisInputManager_)); |
---|
[1755] | 300 | } |
---|
[3331] | 301 | catch (const std::exception& ex) |
---|
[1755] | 302 | { |
---|
[8858] | 303 | orxout(user_warning, context::input) << "Failed to create joy stick: " << ex.what() << endl; |
---|
[1755] | 304 | } |
---|
| 305 | } |
---|
[1505] | 306 | |
---|
[1887] | 307 | // inform all JoyStick Device Number Listeners |
---|
[3327] | 308 | std::vector<JoyStick*> joyStickList; |
---|
| 309 | for (unsigned int i = InputDeviceEnumerator::FirstJoyStick; i < devices_.size(); ++i) |
---|
| 310 | joyStickList.push_back(static_cast<JoyStick*>(devices_[i])); |
---|
| 311 | JoyStickQuantityListener::changeJoyStickQuantity(joyStickList); |
---|
[1505] | 312 | } |
---|
[1502] | 313 | |
---|
[3327] | 314 | // ############################################################ |
---|
| 315 | // ##### Destruction ##### |
---|
| 316 | // ########## ########## |
---|
| 317 | // ############################################################ |
---|
[1293] | 318 | |
---|
[3327] | 319 | InputManager::~InputManager() |
---|
[2662] | 320 | { |
---|
[8858] | 321 | orxout(internal_status, context::input) << "InputManager: Destroying..." << endl; |
---|
[1219] | 322 | |
---|
[6746] | 323 | // Leave all active InputStates (except "empty") |
---|
| 324 | while (this->activeStates_.size() > 1) |
---|
| 325 | this->leaveState(this->activeStates_.rbegin()->second->getName()); |
---|
| 326 | this->activeStates_.clear(); |
---|
| 327 | |
---|
[3327] | 328 | // Destroy calibrator helper handler and state |
---|
| 329 | this->destroyState("calibrator"); |
---|
| 330 | // Destroy KeyDetector and state |
---|
[9667] | 331 | delete calibratorCallbackHandler_; |
---|
[6746] | 332 | // Destroy the empty InputState |
---|
[3327] | 333 | this->destroyStateInternal(this->emptyState_); |
---|
[1502] | 334 | |
---|
[6746] | 335 | // Destroy all user InputStates |
---|
[3327] | 336 | while (statesByName_.size() > 0) |
---|
[6417] | 337 | this->destroyStateInternal(statesByName_.rbegin()->second); |
---|
[2662] | 338 | |
---|
[3327] | 339 | if (!(internalState_ & Bad)) |
---|
| 340 | this->destroyDevices(); |
---|
[2662] | 341 | |
---|
[7284] | 342 | // Reset console commands |
---|
| 343 | ModifyConsoleCommand(__CC_InputManager_name, __CC_calibrate_name).setObject(0); |
---|
| 344 | ModifyConsoleCommand(__CC_InputManager_name, __CC_reload_name).setObject(0); |
---|
| 345 | |
---|
[8858] | 346 | orxout(internal_status, context::input) << "InputManager: Destruction complete." << endl; |
---|
[1219] | 347 | } |
---|
[928] | 348 | |
---|
[1755] | 349 | /** |
---|
| 350 | @brief |
---|
[3327] | 351 | Destoys all input devices (joy sticks, mouse, keyboard and OIS::InputManager) |
---|
| 352 | @throw |
---|
| 353 | Method does not throw |
---|
[1755] | 354 | */ |
---|
[3327] | 355 | void InputManager::destroyDevices() |
---|
[1219] | 356 | { |
---|
[8858] | 357 | orxout(verbose, context::input) << "InputManager: Destroying devices..." << endl; |
---|
[3327] | 358 | |
---|
| 359 | BOOST_FOREACH(InputDevice*& device, devices_) |
---|
[1755] | 360 | { |
---|
[3327] | 361 | if (device == NULL) |
---|
| 362 | continue; |
---|
[6417] | 363 | const std::string& className = device->getClassName(); |
---|
[7174] | 364 | delete device; |
---|
| 365 | device = 0; |
---|
[8858] | 366 | orxout(verbose, context::input) << className << " destroyed." << endl; |
---|
[1755] | 367 | } |
---|
[3327] | 368 | devices_.resize(InputDeviceEnumerator::FirstJoyStick); |
---|
[2662] | 369 | |
---|
[3327] | 370 | assert(oisInputManager_ != NULL); |
---|
[3280] | 371 | try |
---|
| 372 | { |
---|
[3327] | 373 | OIS::InputManager::destroyInputSystem(oisInputManager_); |
---|
[3280] | 374 | } |
---|
[7174] | 375 | catch (const OIS::Exception& ex) |
---|
[3280] | 376 | { |
---|
[8858] | 377 | orxout(internal_error, context::input) << "OIS::InputManager destruction failed" << ex.eText << '\n' |
---|
| 378 | << "Potential resource leak!" << endl; |
---|
[3280] | 379 | } |
---|
[3327] | 380 | oisInputManager_ = NULL; |
---|
[1219] | 381 | |
---|
[3327] | 382 | internalState_ |= Bad; |
---|
[8858] | 383 | orxout(verbose, context::input) << "Destroyed devices." << endl; |
---|
[1755] | 384 | } |
---|
[1219] | 385 | |
---|
[1755] | 386 | // ############################################################ |
---|
| 387 | // ##### Reloading ##### |
---|
| 388 | // ########## ########## |
---|
| 389 | // ############################################################ |
---|
[1219] | 390 | |
---|
[3327] | 391 | void InputManager::reload() |
---|
[1755] | 392 | { |
---|
[6746] | 393 | if (internalState_ & Calibrating) |
---|
[8858] | 394 | orxout(internal_warning, context::input) << "Cannot reload input system. Joy sticks are currently being calibrated." << endl; |
---|
[1502] | 395 | else |
---|
[3327] | 396 | reloadInternal(); |
---|
[1755] | 397 | } |
---|
[1502] | 398 | |
---|
[3327] | 399 | //! Internal reload method. Destroys the OIS devices and loads them again. |
---|
| 400 | void InputManager::reloadInternal() |
---|
[1755] | 401 | { |
---|
[8858] | 402 | orxout(verbose, context::input) << "InputManager: Reloading ..." << endl; |
---|
[1502] | 403 | |
---|
[3327] | 404 | this->destroyDevices(); |
---|
[5695] | 405 | this->loadDevices(); |
---|
[1502] | 406 | |
---|
[3327] | 407 | internalState_ &= ~Bad; |
---|
[8858] | 408 | orxout(verbose, context::input) << "InputManager: Reloading complete." << endl; |
---|
[1502] | 409 | } |
---|
[1219] | 410 | |
---|
[1755] | 411 | // ############################################################ |
---|
| 412 | // ##### Runtime Methods ##### |
---|
| 413 | // ########## ########## |
---|
| 414 | // ############################################################ |
---|
[1555] | 415 | |
---|
[6417] | 416 | void InputManager::preUpdate(const Clock& time) |
---|
[1502] | 417 | { |
---|
[3327] | 418 | if (internalState_ & Bad) |
---|
| 419 | ThrowException(General, "InputManager was not correctly reloaded."); |
---|
| 420 | |
---|
| 421 | // check whether a state has changed its EMPTY situation |
---|
[1878] | 422 | bool bUpdateRequired = false; |
---|
| 423 | for (std::map<int, InputState*>::iterator it = activeStates_.begin(); it != activeStates_.end(); ++it) |
---|
| 424 | { |
---|
[3327] | 425 | if (it->second->hasExpired()) |
---|
[1878] | 426 | { |
---|
[3327] | 427 | it->second->resetExpiration(); |
---|
[1878] | 428 | bUpdateRequired = true; |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | if (bUpdateRequired) |
---|
[3327] | 432 | updateActiveStates(); |
---|
[1878] | 433 | |
---|
[6746] | 434 | // Capture all the input and collect the function calls |
---|
| 435 | // No event gets triggered here yet! |
---|
[3327] | 436 | BOOST_FOREACH(InputDevice* device, devices_) |
---|
| 437 | if (device != NULL) |
---|
| 438 | device->update(time); |
---|
[1219] | 439 | |
---|
[6746] | 440 | // Collect function calls for the update |
---|
[3327] | 441 | for (unsigned int i = 0; i < activeStatesTicked_.size(); ++i) |
---|
| 442 | activeStatesTicked_[i]->update(time.getDeltaTime()); |
---|
[2896] | 443 | |
---|
[6746] | 444 | // Execute all cached function calls in order |
---|
| 445 | // Why so complicated? The problem is that an InputHandler could trigger |
---|
| 446 | // a reload that would destroy the OIS devices or it could even leave and |
---|
| 447 | // then destroy its own InputState. That would of course lead to access |
---|
| 448 | // violations. |
---|
| 449 | // If we delay the calls, then OIS and and the InputStates are not anymore |
---|
| 450 | // in the call stack and can therefore be edited. |
---|
| 451 | for (size_t i = 0; i < this->callBuffer_.size(); ++i) |
---|
| 452 | this->callBuffer_[i](); |
---|
| 453 | |
---|
| 454 | this->callBuffer_.clear(); |
---|
[1293] | 455 | } |
---|
[1219] | 456 | |
---|
[1755] | 457 | /** |
---|
| 458 | @brief |
---|
| 459 | Updates the currently active states (according to activeStates_) for each device. |
---|
[6417] | 460 | Also, a list of all active states (no duplicates!) is compiled for the general preUpdate(). |
---|
[1755] | 461 | */ |
---|
[3327] | 462 | void InputManager::updateActiveStates() |
---|
[1293] | 463 | { |
---|
[6746] | 464 | // Calculate the stack of input states |
---|
| 465 | // and assign it to the corresponding device |
---|
[3327] | 466 | for (unsigned int i = 0; i < devices_.size(); ++i) |
---|
[2896] | 467 | { |
---|
[3327] | 468 | if (devices_[i] == NULL) |
---|
| 469 | continue; |
---|
| 470 | std::vector<InputState*>& states = devices_[i]->getStateListRef(); |
---|
[2896] | 471 | bool occupied = false; |
---|
[3327] | 472 | states.clear(); |
---|
[2896] | 473 | for (std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin(); rit != activeStates_.rend(); ++rit) |
---|
| 474 | { |
---|
[9790] | 475 | orxout() << "Checking ID " << i <<std::endl; |
---|
| 476 | orxout() << "Checking condition 1: " << rit->second->isInputDeviceEnabled(i) <<std::endl; |
---|
| 477 | orxout() << "Checking condition 2: " << rit->second->bAlwaysGetsInput_ <<std::endl; |
---|
| 478 | orxout() << "Checking condition 3: " << !occupied <<std::endl; |
---|
| 479 | if (rit->second->isInputDeviceEnabled(i) && (!occupied || rit->second->bAlwaysGetsInput_)) |
---|
[2896] | 480 | { |
---|
[9805] | 481 | orxout() << "Success with ID " << i <<std::endl; |
---|
[3327] | 482 | states.push_back(rit->second); |
---|
[2896] | 483 | if (!rit->second->bTransparent_) |
---|
| 484 | occupied = true; |
---|
| 485 | } |
---|
| 486 | } |
---|
| 487 | } |
---|
[1293] | 488 | |
---|
[6746] | 489 | // See that we only update each InputState once for each device |
---|
| 490 | // Using an std::set to avoid duplicates |
---|
[1755] | 491 | std::set<InputState*> tempSet; |
---|
[3327] | 492 | for (unsigned int i = 0; i < devices_.size(); ++i) |
---|
| 493 | if (devices_[i] != NULL) |
---|
| 494 | for (unsigned int iState = 0; iState < devices_[i]->getStateListRef().size(); ++iState) |
---|
| 495 | tempSet.insert(devices_[i]->getStateListRef()[iState]); |
---|
[1219] | 496 | |
---|
[6746] | 497 | // Copy the content of the std::set back to the actual vector |
---|
[1755] | 498 | activeStatesTicked_.clear(); |
---|
| 499 | for (std::set<InputState*>::const_iterator it = tempSet.begin();it != tempSet.end(); ++it) |
---|
| 500 | activeStatesTicked_.push_back(*it); |
---|
[5695] | 501 | |
---|
| 502 | // Check whether we have to change the mouse mode |
---|
[8729] | 503 | tribool requestedMode = dontcare; |
---|
[5695] | 504 | std::vector<InputState*>& mouseStates = devices_[InputDeviceEnumerator::Mouse]->getStateListRef(); |
---|
[5929] | 505 | if (mouseStates.empty()) |
---|
[8729] | 506 | requestedMode = false; |
---|
[6417] | 507 | else |
---|
[6746] | 508 | requestedMode = mouseStates.front()->getMouseExclusive(); |
---|
[8729] | 509 | if (requestedMode != dontcare && exclusiveMouse_ != requestedMode) |
---|
[5695] | 510 | { |
---|
[8729] | 511 | assert(requestedMode != dontcare); |
---|
| 512 | exclusiveMouse_ = (requestedMode == true); |
---|
[5695] | 513 | if (!GraphicsManager::getInstance().isFullScreen()) |
---|
| 514 | this->reloadInternal(); |
---|
| 515 | } |
---|
[1755] | 516 | } |
---|
[1219] | 517 | |
---|
[1878] | 518 | void InputManager::clearBuffers() |
---|
| 519 | { |
---|
[3327] | 520 | BOOST_FOREACH(InputDevice* device, devices_) |
---|
| 521 | if (device != NULL) |
---|
| 522 | device->clearBuffers(); |
---|
[1878] | 523 | } |
---|
[1502] | 524 | |
---|
[3327] | 525 | void InputManager::calibrate() |
---|
[1219] | 526 | { |
---|
[8858] | 527 | orxout(message) << "Move all joy stick axes fully in all directions." << '\n' |
---|
| 528 | << "When done, put the axex in the middle position and press enter." << endl; |
---|
[1219] | 529 | |
---|
[3327] | 530 | BOOST_FOREACH(InputDevice* device, devices_) |
---|
| 531 | if (device != NULL) |
---|
| 532 | device->startCalibration(); |
---|
[1219] | 533 | |
---|
[3327] | 534 | internalState_ |= Calibrating; |
---|
| 535 | enterState("calibrator"); |
---|
[1349] | 536 | } |
---|
[1755] | 537 | |
---|
[3327] | 538 | //! Tells all devices to stop the calibration and evaluate it. Buffers are being cleared as well! |
---|
| 539 | void InputManager::stopCalibration() |
---|
[1349] | 540 | { |
---|
[3327] | 541 | BOOST_FOREACH(InputDevice* device, devices_) |
---|
| 542 | if (device != NULL) |
---|
| 543 | device->stopCalibration(); |
---|
[1502] | 544 | |
---|
[3327] | 545 | // restore old input state |
---|
| 546 | leaveState("calibrator"); |
---|
| 547 | internalState_ &= ~Calibrating; |
---|
| 548 | // Clear buffers to prevent button hold events |
---|
| 549 | this->clearBuffers(); |
---|
[1293] | 550 | |
---|
[8858] | 551 | orxout(message) << "Calibration has been stored." << endl; |
---|
[1755] | 552 | } |
---|
[1502] | 553 | |
---|
[6417] | 554 | //! Gets called by WindowEventListener upon focus change --> clear buffers |
---|
[7874] | 555 | void InputManager::windowFocusChanged(bool bFocus) |
---|
[1755] | 556 | { |
---|
[3327] | 557 | this->clearBuffers(); |
---|
[1755] | 558 | } |
---|
[1502] | 559 | |
---|
[5695] | 560 | std::pair<int, int> InputManager::getMousePosition() const |
---|
| 561 | { |
---|
| 562 | Mouse* mouse = static_cast<Mouse*>(devices_[InputDeviceEnumerator::Mouse]); |
---|
| 563 | if (mouse != NULL) |
---|
| 564 | { |
---|
| 565 | const OIS::MouseState state = mouse->getOISDevice()->getMouseState(); |
---|
| 566 | return std::make_pair(state.X.abs, state.Y.abs); |
---|
| 567 | } |
---|
| 568 | else |
---|
| 569 | return std::make_pair(0, 0); |
---|
| 570 | } |
---|
| 571 | |
---|
[1755] | 572 | // ############################################################ |
---|
[5695] | 573 | // ##### Input States ##### |
---|
[1755] | 574 | // ########## ########## |
---|
| 575 | // ############################################################ |
---|
[1219] | 576 | |
---|
[3327] | 577 | InputState* InputManager::createInputState(const std::string& name, bool bAlwaysGetsInput, bool bTransparent, InputStatePriority priority) |
---|
[1219] | 578 | { |
---|
[6417] | 579 | if (name.empty()) |
---|
[3327] | 580 | return 0; |
---|
| 581 | if (statesByName_.find(name) == statesByName_.end()) |
---|
[1755] | 582 | { |
---|
[2896] | 583 | if (priority >= InputStatePriority::HighPriority || priority == InputStatePriority::Empty) |
---|
[1755] | 584 | { |
---|
[2896] | 585 | // Make sure we don't add two high priority states with the same priority |
---|
[3327] | 586 | for (std::map<std::string, InputState*>::const_iterator it = this->statesByName_.begin(); |
---|
| 587 | it != this->statesByName_.end(); ++it) |
---|
[2896] | 588 | { |
---|
| 589 | if (it->second->getPriority() == priority) |
---|
| 590 | { |
---|
[8858] | 591 | orxout(internal_warning, context::input) << "Could not add an InputState with the same priority '" |
---|
| 592 | << static_cast<int>(priority) << "' != 0." << endl; |
---|
[3327] | 593 | return 0; |
---|
[2896] | 594 | } |
---|
| 595 | } |
---|
| 596 | } |
---|
[3327] | 597 | InputState* state = new InputState(name, bAlwaysGetsInput, bTransparent, priority); |
---|
| 598 | statesByName_[name] = state; |
---|
| 599 | |
---|
| 600 | return state; |
---|
[1755] | 601 | } |
---|
| 602 | else |
---|
| 603 | { |
---|
[8858] | 604 | orxout(internal_warning, context::input) << "Could not add an InputState with the same name '" << name << "'." << endl; |
---|
[3327] | 605 | return 0; |
---|
[1755] | 606 | } |
---|
[1219] | 607 | } |
---|
| 608 | |
---|
[1755] | 609 | InputState* InputManager::getState(const std::string& name) |
---|
[1219] | 610 | { |
---|
[3327] | 611 | std::map<std::string, InputState*>::iterator it = statesByName_.find(name); |
---|
| 612 | if (it != statesByName_.end()) |
---|
[1755] | 613 | return it->second; |
---|
| 614 | else |
---|
| 615 | return 0; |
---|
[1219] | 616 | } |
---|
| 617 | |
---|
[3327] | 618 | bool InputManager::enterState(const std::string& name) |
---|
[1219] | 619 | { |
---|
[1755] | 620 | // get pointer from the map with all stored handlers |
---|
[3327] | 621 | std::map<std::string, InputState*>::const_iterator it = statesByName_.find(name); |
---|
[6746] | 622 | if (it != statesByName_.end() && activeStates_.find(it->second->getPriority()) == activeStates_.end()) |
---|
[1755] | 623 | { |
---|
[6746] | 624 | // exists and not active |
---|
| 625 | if (it->second->getPriority() == 0) |
---|
[1755] | 626 | { |
---|
[6746] | 627 | // Get smallest possible priority between 1 and maxStateStackSize_s |
---|
| 628 | for (std::map<int, InputState*>::reverse_iterator rit = activeStates_.rbegin(); |
---|
| 629 | rit != activeStates_.rend(); ++rit) |
---|
[1755] | 630 | { |
---|
[6746] | 631 | if (rit->first < InputStatePriority::HighPriority) |
---|
| 632 | { |
---|
| 633 | it->second->setPriority(rit->first + 1); |
---|
| 634 | break; |
---|
| 635 | } |
---|
[1755] | 636 | } |
---|
[6746] | 637 | // In case no normal handler was on the stack |
---|
| 638 | if (it->second->getPriority() == 0) |
---|
| 639 | it->second->setPriority(1); |
---|
[1755] | 640 | } |
---|
[6746] | 641 | activeStates_[it->second->getPriority()] = it->second; |
---|
| 642 | updateActiveStates(); |
---|
| 643 | it->second->entered(); |
---|
| 644 | |
---|
| 645 | return true; |
---|
[1755] | 646 | } |
---|
| 647 | return false; |
---|
[1219] | 648 | } |
---|
| 649 | |
---|
[3327] | 650 | bool InputManager::leaveState(const std::string& name) |
---|
[1219] | 651 | { |
---|
[2896] | 652 | if (name == "empty") |
---|
| 653 | { |
---|
[8858] | 654 | orxout(internal_warning, context::input) << "InputManager: Leaving the empty state is not allowed!" << endl; |
---|
[2896] | 655 | return false; |
---|
| 656 | } |
---|
[1755] | 657 | // get pointer from the map with all stored handlers |
---|
[3327] | 658 | std::map<std::string, InputState*>::const_iterator it = statesByName_.find(name); |
---|
[6746] | 659 | if (it != statesByName_.end() && activeStates_.find(it->second->getPriority()) != activeStates_.end()) |
---|
[1755] | 660 | { |
---|
[6746] | 661 | // exists and active |
---|
| 662 | |
---|
| 663 | it->second->left(); |
---|
| 664 | |
---|
| 665 | activeStates_.erase(it->second->getPriority()); |
---|
| 666 | if (it->second->getPriority() < InputStatePriority::HighPriority) |
---|
| 667 | it->second->setPriority(0); |
---|
| 668 | updateActiveStates(); |
---|
| 669 | |
---|
| 670 | return true; |
---|
[1755] | 671 | } |
---|
| 672 | return false; |
---|
[1219] | 673 | } |
---|
| 674 | |
---|
[3327] | 675 | bool InputManager::destroyState(const std::string& name) |
---|
[1219] | 676 | { |
---|
[3327] | 677 | if (name == "empty") |
---|
| 678 | { |
---|
[8858] | 679 | orxout(internal_warning, context::input) << "InputManager: Removing the empty state is not allowed!" << endl; |
---|
[3327] | 680 | return false; |
---|
| 681 | } |
---|
| 682 | std::map<std::string, InputState*>::iterator it = statesByName_.find(name); |
---|
| 683 | if (it != statesByName_.end()) |
---|
| 684 | { |
---|
[6746] | 685 | this->leaveState(name); |
---|
| 686 | destroyStateInternal(it->second); |
---|
[2662] | 687 | |
---|
[3327] | 688 | return true; |
---|
| 689 | } |
---|
| 690 | return false; |
---|
[1219] | 691 | } |
---|
| 692 | |
---|
[3327] | 693 | //! Destroys an InputState internally. |
---|
| 694 | void InputManager::destroyStateInternal(InputState* state) |
---|
[1219] | 695 | { |
---|
[6746] | 696 | assert(state && this->activeStates_.find(state->getPriority()) == this->activeStates_.end()); |
---|
[3327] | 697 | statesByName_.erase(state->getName()); |
---|
[9667] | 698 | delete state; |
---|
[1219] | 699 | } |
---|
[8079] | 700 | |
---|
[8729] | 701 | bool InputManager::setMouseExclusive(const std::string& name, tribool value) |
---|
[8079] | 702 | { |
---|
| 703 | if (name == "empty") |
---|
| 704 | { |
---|
[8858] | 705 | orxout(internal_warning, context::input) << "InputManager: Changing the empty state is not allowed!" << endl; |
---|
[8079] | 706 | return false; |
---|
| 707 | } |
---|
| 708 | std::map<std::string, InputState*>::iterator it = statesByName_.find(name); |
---|
| 709 | if (it != statesByName_.end()) |
---|
| 710 | { |
---|
| 711 | it->second->setMouseExclusive(value); |
---|
| 712 | return true; |
---|
| 713 | } |
---|
| 714 | return false; |
---|
| 715 | } |
---|
[918] | 716 | } |
---|