[1637] | 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 |
---|
| 32 | Implementation of the ExtendedInputState class. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #include "ExtendedInputState.h" |
---|
| 36 | |
---|
| 37 | #include <assert.h> |
---|
[1755] | 38 | #include "util/Debug.h" |
---|
[1637] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | using namespace InputDevice; |
---|
| 43 | |
---|
| 44 | void ExtendedInputState::numberOfJoySticksChanged(unsigned int n) |
---|
| 45 | { |
---|
| 46 | unsigned int oldSize = joyStickHandlers_.size(); |
---|
| 47 | joyStickHandlers_.resize(n); |
---|
| 48 | |
---|
| 49 | if (oldSize > n) |
---|
| 50 | { |
---|
| 51 | // we have to add all the handlers in joyStickHandlersAll_ to the joyStickHandlers_[>n] |
---|
| 52 | for (unsigned int j = 0; j < joyStickHandlersAll_.size(); ++j) |
---|
| 53 | { |
---|
| 54 | for (unsigned int i = oldSize; i < n; ++i) |
---|
| 55 | { |
---|
| 56 | joyStickHandlers_[i].push_back(joyStickHandlersAll_[j]); |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | update(); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | void ExtendedInputState::keyPressed(const KeyEvent& evt) |
---|
| 64 | { |
---|
| 65 | for (unsigned int i = 0; i < keyHandlers_.size(); i++) |
---|
| 66 | keyHandlers_[i]->keyPressed(evt); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | void ExtendedInputState::keyReleased(const KeyEvent& evt) |
---|
| 70 | { |
---|
| 71 | for (unsigned int i = 0; i < keyHandlers_.size(); i++) |
---|
| 72 | keyHandlers_[i]->keyReleased(evt); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | void ExtendedInputState::keyHeld(const KeyEvent& evt) |
---|
| 76 | { |
---|
| 77 | for (unsigned int i = 0; i < keyHandlers_.size(); i++) |
---|
| 78 | keyHandlers_[i]->keyHeld(evt); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | void ExtendedInputState::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
| 83 | { |
---|
| 84 | for (unsigned int i = 0; i < mouseHandlers_.size(); i++) |
---|
| 85 | mouseHandlers_[i]->mouseMoved(abs, rel, clippingSize); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | void ExtendedInputState::mouseScrolled(int abs, int rel) |
---|
| 89 | { |
---|
| 90 | for (unsigned int i = 0; i < mouseHandlers_.size(); i++) |
---|
| 91 | mouseHandlers_[i]->mouseScrolled(abs, rel); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void ExtendedInputState::mouseButtonPressed(MouseButton::Enum id) |
---|
| 95 | { |
---|
| 96 | for (unsigned int i = 0; i < mouseHandlers_.size(); i++) |
---|
| 97 | mouseHandlers_[i]->mouseButtonPressed(id); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void ExtendedInputState::mouseButtonReleased(MouseButton::Enum id) |
---|
| 101 | { |
---|
| 102 | for (unsigned int i = 0; i < mouseHandlers_.size(); i++) |
---|
| 103 | mouseHandlers_[i]->mouseButtonReleased(id); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void ExtendedInputState::mouseButtonHeld(MouseButton::Enum id) |
---|
| 107 | { |
---|
| 108 | for (unsigned int i = 0; i < mouseHandlers_.size(); i++) |
---|
| 109 | mouseHandlers_[i]->mouseButtonHeld(id); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | void ExtendedInputState::joyStickAxisMoved(unsigned int joyStickID, unsigned int axis, float value) |
---|
| 114 | { |
---|
| 115 | assert(joyStickID < joyStickHandlers_.size()); |
---|
| 116 | for (unsigned int i = 0; i < joyStickHandlers_[joyStickID].size(); i++) |
---|
| 117 | joyStickHandlers_[joyStickID][i]->joyStickAxisMoved(joyStickID, axis, value); |
---|
| 118 | } |
---|
| 119 | |
---|
[1641] | 120 | void ExtendedInputState::joyStickButtonPressed(unsigned int joyStickID, JoyStickButton::Enum id) |
---|
[1637] | 121 | { |
---|
| 122 | assert(joyStickID < joyStickHandlers_.size()); |
---|
| 123 | for (unsigned int i = 0; i < joyStickHandlers_[joyStickID].size(); i++) |
---|
[1641] | 124 | joyStickHandlers_[joyStickID][i]->joyStickButtonPressed(joyStickID, id); |
---|
[1637] | 125 | } |
---|
| 126 | |
---|
[1641] | 127 | void ExtendedInputState::joyStickButtonReleased(unsigned int joyStickID, JoyStickButton::Enum id) |
---|
[1637] | 128 | { |
---|
| 129 | assert(joyStickID < joyStickHandlers_.size()); |
---|
| 130 | for (unsigned int i = 0; i < joyStickHandlers_[joyStickID].size(); i++) |
---|
[1641] | 131 | joyStickHandlers_[joyStickID][i]->joyStickButtonReleased(joyStickID, id); |
---|
[1637] | 132 | } |
---|
| 133 | |
---|
[1641] | 134 | void ExtendedInputState::joyStickButtonHeld(unsigned int joyStickID, JoyStickButton::Enum id) |
---|
[1637] | 135 | { |
---|
| 136 | assert(joyStickID < joyStickHandlers_.size()); |
---|
| 137 | for (unsigned int i = 0; i < joyStickHandlers_[joyStickID].size(); i++) |
---|
[1641] | 138 | joyStickHandlers_[joyStickID][i]->joyStickButtonHeld(joyStickID, id); |
---|
[1637] | 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | @brief |
---|
| 144 | Adds a key handler. |
---|
| 145 | @param handler |
---|
| 146 | Pointer to the handler object. |
---|
| 147 | @return |
---|
| 148 | True if added, false if handler already existed. |
---|
| 149 | */ |
---|
| 150 | bool ExtendedInputState::addKeyHandler(KeyHandler* handler) |
---|
| 151 | { |
---|
| 152 | if (!handler) |
---|
| 153 | return false; |
---|
| 154 | |
---|
| 155 | // see whether the handler already is in the list |
---|
| 156 | for (std::vector<KeyHandler*>::iterator it = keyHandlers_.begin(); it != keyHandlers_.end(); it++) |
---|
| 157 | { |
---|
| 158 | if ((*it) == handler) |
---|
| 159 | { |
---|
| 160 | return false; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | keyHandlers_.push_back(handler); |
---|
| 164 | update(); |
---|
| 165 | return true; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | @brief |
---|
| 170 | Removes a Key handler from the state. |
---|
| 171 | @param handler |
---|
| 172 | Pointer to the handler. |
---|
| 173 | @return |
---|
| 174 | True if removal was successful, false if handler was not found. |
---|
| 175 | */ |
---|
| 176 | bool ExtendedInputState::removeKeyHandler(KeyHandler* handler) |
---|
| 177 | { |
---|
| 178 | if (!handler) |
---|
| 179 | return false; |
---|
| 180 | |
---|
| 181 | for (std::vector<KeyHandler*>::iterator it = keyHandlers_.begin(); it != keyHandlers_.end(); it++) |
---|
| 182 | { |
---|
| 183 | if ((*it) == handler) |
---|
| 184 | { |
---|
| 185 | keyHandlers_.erase(it); |
---|
| 186 | update(); |
---|
| 187 | return true; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | return false; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | @brief |
---|
| 196 | Adds a mouse handler. |
---|
| 197 | @param handler |
---|
| 198 | Pointer to the handler object. |
---|
| 199 | @return |
---|
| 200 | True if added, false if handler already existed. |
---|
| 201 | */ |
---|
| 202 | bool ExtendedInputState::addMouseHandler(MouseHandler* handler) |
---|
| 203 | { |
---|
| 204 | if (!handler) |
---|
| 205 | return false; |
---|
| 206 | |
---|
| 207 | // see whether the handler already is in the list |
---|
| 208 | for (std::vector<MouseHandler*>::iterator it = mouseHandlers_.begin(); it != mouseHandlers_.end(); it++) |
---|
| 209 | { |
---|
| 210 | if ((*it) == handler) |
---|
| 211 | { |
---|
| 212 | return false; |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | mouseHandlers_.push_back(handler); |
---|
| 216 | update(); |
---|
| 217 | return true; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | /** |
---|
| 221 | @brief |
---|
| 222 | Removes a mouse handler from the state. |
---|
| 223 | @param handler |
---|
| 224 | Pointer to the handler. |
---|
| 225 | @return |
---|
| 226 | True if removal was successful, false if handler was not found. |
---|
| 227 | */ |
---|
| 228 | bool ExtendedInputState::removeMouseHandler(MouseHandler* handler) |
---|
| 229 | { |
---|
| 230 | if (!handler) |
---|
| 231 | return false; |
---|
| 232 | |
---|
| 233 | for (std::vector<MouseHandler*>::iterator it = mouseHandlers_.begin(); it != mouseHandlers_.end(); it++) |
---|
| 234 | { |
---|
| 235 | if ((*it) == handler) |
---|
| 236 | { |
---|
| 237 | mouseHandlers_.erase(it); |
---|
| 238 | update(); |
---|
| 239 | return true; |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | return false; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | /** |
---|
| 247 | @brief |
---|
| 248 | Adds a joy stick handler. |
---|
| 249 | @param handler |
---|
| 250 | Pointer to the handler object. |
---|
| 251 | @param joyStickID |
---|
| 252 | ID of the joy stick |
---|
| 253 | @return |
---|
| 254 | True if added, false if handler already existed. |
---|
| 255 | */ |
---|
| 256 | bool ExtendedInputState::addJoyStickHandler(JoyStickHandler* handler, unsigned int joyStickID) |
---|
| 257 | { |
---|
| 258 | if (!handler || joyStickID >= joyStickHandlers_.size()) |
---|
| 259 | return false; |
---|
| 260 | |
---|
| 261 | // see whether the handler already is in the list |
---|
| 262 | for (std::vector<JoyStickHandler*>::iterator it = joyStickHandlers_[joyStickID].begin(); |
---|
| 263 | it != joyStickHandlers_[joyStickID].end(); it++) |
---|
| 264 | { |
---|
| 265 | if ((*it) == handler) |
---|
| 266 | { |
---|
| 267 | return false; |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | joyStickHandlers_[joyStickID].push_back(handler); |
---|
| 271 | update(); |
---|
| 272 | return true; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /** |
---|
| 276 | @brief |
---|
| 277 | Removes a joy stick handler from the state. |
---|
| 278 | @param handler |
---|
| 279 | Pointer to the handler. |
---|
| 280 | @param joyStickID |
---|
| 281 | ID of the joy stick |
---|
| 282 | @return |
---|
| 283 | True if removal was successful, false if handler was not found. |
---|
| 284 | */ |
---|
| 285 | bool ExtendedInputState::removeJoyStickHandler(JoyStickHandler* handler, unsigned int joyStickID) |
---|
| 286 | { |
---|
| 287 | if (!handler || joyStickID >= joyStickHandlers_.size()) |
---|
| 288 | return false; |
---|
| 289 | |
---|
| 290 | // remove it from the list of all-joystick handlers if present |
---|
| 291 | for (std::vector<JoyStickHandler*>::iterator it = joyStickHandlersAll_.begin(); |
---|
| 292 | it != joyStickHandlersAll_.end(); it++) |
---|
| 293 | { |
---|
| 294 | if ((*it) == handler) |
---|
| 295 | { |
---|
| 296 | joyStickHandlersAll_.erase(it); |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | // remove handler from the list of seperate lists of handlers |
---|
| 301 | for (std::vector<JoyStickHandler*>::iterator it = joyStickHandlers_[joyStickID].begin(); |
---|
| 302 | it != joyStickHandlers_[joyStickID].end(); it++) |
---|
| 303 | { |
---|
| 304 | if ((*it) == handler) |
---|
| 305 | { |
---|
| 306 | joyStickHandlers_[joyStickID].erase(it); |
---|
| 307 | update(); |
---|
| 308 | return true; |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | return false; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | /** |
---|
| 315 | @brief |
---|
| 316 | Adds a joy stick handler. |
---|
| 317 | @param handler |
---|
| 318 | Pointer to the handler object. |
---|
| 319 | @return |
---|
| 320 | True if added, false if handler already existed. |
---|
| 321 | */ |
---|
| 322 | bool ExtendedInputState::addJoyStickHandler(JoyStickHandler* handler) |
---|
| 323 | { |
---|
| 324 | if (!handler) |
---|
| 325 | return false; |
---|
| 326 | |
---|
| 327 | // see whether the handler already is in the big list |
---|
| 328 | for (std::vector<JoyStickHandler*>::iterator it = joyStickHandlersAll_.begin(); |
---|
| 329 | it != joyStickHandlersAll_.end(); it++) |
---|
| 330 | { |
---|
| 331 | if ((*it) == handler) |
---|
| 332 | { |
---|
| 333 | return false; |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | joyStickHandlersAll_.push_back(handler); |
---|
| 337 | update(); |
---|
| 338 | |
---|
| 339 | // now, we have to add it to all the separate lists |
---|
| 340 | for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandlers_.size(); ++iJoyStick) |
---|
| 341 | addJoyStickHandler(handler, iJoyStick); |
---|
| 342 | return true; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | /** |
---|
| 346 | @brief |
---|
| 347 | Removes a joy stick handler from the state. |
---|
| 348 | @param handler |
---|
| 349 | Pointer to the handler. |
---|
| 350 | @return |
---|
| 351 | True if removal was successful, false if handler was not found. |
---|
| 352 | */ |
---|
| 353 | bool ExtendedInputState::removeJoyStickHandler(JoyStickHandler* handler) |
---|
| 354 | { |
---|
| 355 | if (!handler) |
---|
| 356 | return false; |
---|
| 357 | |
---|
| 358 | bool success = false; |
---|
| 359 | // remove from all lists in a loop (also removes it from the big list) |
---|
| 360 | for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandlers_.size(); ++iJoyStick) |
---|
| 361 | success |= removeJoyStickHandler(handler, iJoyStick); |
---|
| 362 | |
---|
| 363 | return success; |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | void ExtendedInputState::removeAndDestroyAllHandlers() |
---|
| 367 | { |
---|
| 368 | for (std::vector<InputTickable*>::iterator it = allHandlers_.begin(); |
---|
| 369 | it != allHandlers_.end(); ++it) |
---|
| 370 | delete *it; |
---|
| 371 | |
---|
| 372 | allHandlers_.clear(); |
---|
| 373 | keyHandlers_.clear(); |
---|
| 374 | mouseHandlers_.clear(); |
---|
| 375 | joyStickHandlersAll_.clear(); |
---|
| 376 | for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandlers_.size(); ++iJoyStick) |
---|
| 377 | joyStickHandlers_[iJoyStick].clear(); |
---|
| 378 | update(); |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | /** |
---|
| 382 | @brief |
---|
| 383 | Adds a handler of any kind. dynamic_cast determines to which list it is added. |
---|
| 384 | @param handler |
---|
| 385 | Pointer to the handler object. |
---|
| 386 | @return |
---|
| 387 | True if added, false if handler already existed. |
---|
| 388 | */ |
---|
| 389 | bool ExtendedInputState::addHandler(InputTickable* handler) |
---|
| 390 | { |
---|
| 391 | bool success = false; |
---|
| 392 | |
---|
| 393 | success |= addKeyHandler(dynamic_cast<KeyHandler*>(handler)); |
---|
| 394 | success |= addMouseHandler(dynamic_cast<MouseHandler*>(handler)); |
---|
| 395 | success |= addJoyStickHandler(dynamic_cast<JoyStickHandler*>(handler)); |
---|
| 396 | |
---|
| 397 | return success; |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | /** |
---|
| 401 | @brief |
---|
| 402 | Removes a handler from all lists. |
---|
| 403 | @param handler |
---|
| 404 | Pointer to the handler. |
---|
| 405 | @return |
---|
| 406 | True if removal was successful, false if handler was not found. |
---|
| 407 | */ |
---|
| 408 | bool ExtendedInputState::removeHandler(InputTickable* handler) |
---|
| 409 | { |
---|
| 410 | bool success = false; |
---|
| 411 | |
---|
| 412 | success |= removeKeyHandler(dynamic_cast<KeyHandler*>(handler)); |
---|
| 413 | success |= removeMouseHandler(dynamic_cast<MouseHandler*>(handler)); |
---|
| 414 | success |= removeJoyStickHandler(dynamic_cast<JoyStickHandler*>(handler)); |
---|
| 415 | |
---|
| 416 | return success; |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | void ExtendedInputState::tickInput(float dt) |
---|
| 420 | { |
---|
| 421 | for (unsigned int i = 0; i < allHandlers_.size(); ++i) |
---|
| 422 | { |
---|
| 423 | allHandlers_[i]->tickInput(dt); |
---|
| 424 | } |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | void ExtendedInputState::tickInput(float dt, unsigned int device) |
---|
| 428 | { |
---|
| 429 | switch (device) |
---|
| 430 | { |
---|
| 431 | case Keyboard: |
---|
| 432 | for (unsigned int i = 0; i < keyHandlers_.size(); ++i) |
---|
| 433 | keyHandlers_[i]->tickKey(dt); |
---|
| 434 | break; |
---|
| 435 | |
---|
| 436 | case Mouse: |
---|
| 437 | for (unsigned int i = 0; i < mouseHandlers_.size(); ++i) |
---|
| 438 | mouseHandlers_[i]->tickMouse(dt); |
---|
| 439 | break; |
---|
| 440 | |
---|
| 441 | default: // joy sticks |
---|
| 442 | for (unsigned int i = 0; i < joyStickHandlers_[device - 2].size(); ++i) |
---|
| 443 | joyStickHandlers_[device - 2][i]->tickJoyStick(dt, device - 2); |
---|
| 444 | break; |
---|
| 445 | } |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | void ExtendedInputState::update() |
---|
| 449 | { |
---|
| 450 | // we can use a set to have a list of unique pointers (an object can implement all 3 handlers) |
---|
| 451 | std::set<InputTickable*> tempSet; |
---|
| 452 | for (unsigned int iHandler = 0; iHandler < keyHandlers_.size(); iHandler++) |
---|
| 453 | tempSet.insert(keyHandlers_[iHandler]); |
---|
| 454 | for (unsigned int iHandler = 0; iHandler < mouseHandlers_.size(); iHandler++) |
---|
| 455 | tempSet.insert(mouseHandlers_[iHandler]); |
---|
| 456 | for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandlers_.size(); iJoyStick++) |
---|
| 457 | for (unsigned int iHandler = 0; iHandler < joyStickHandlers_[iJoyStick].size(); iHandler++) |
---|
| 458 | tempSet.insert(joyStickHandlers_[iJoyStick][iHandler]); |
---|
| 459 | |
---|
| 460 | // copy the content of the map back to the actual vector |
---|
| 461 | allHandlers_.clear(); |
---|
| 462 | for (std::set<InputTickable*>::const_iterator itHandler = tempSet.begin(); |
---|
| 463 | itHandler != tempSet.end(); itHandler++) |
---|
| 464 | allHandlers_.push_back(*itHandler); |
---|
| 465 | |
---|
| 466 | // update the deviceEnabled options |
---|
| 467 | setInputDeviceEnabled(Keyboard, (keyHandlers_.size() != 0)); |
---|
| 468 | setInputDeviceEnabled(Mouse, (mouseHandlers_.size() != 0)); |
---|
| 469 | for (unsigned int i = 0; i < joyStickHandlers_.size(); ++i) |
---|
| 470 | setInputDeviceEnabled(2 + i, (joyStickHandlers_[i].size() != 0)); |
---|
| 471 | } |
---|
| 472 | } |
---|