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