[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 SimpleInputState class. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #include "SimpleInputState.h" |
---|
| 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
| 39 | SimpleInputState::SimpleInputState() |
---|
| 40 | : keyHandler_(0) |
---|
| 41 | , mouseHandler_(0) |
---|
| 42 | , joyStickHandlerAll_(0) |
---|
| 43 | { |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | void SimpleInputState::numberOfJoySticksChanged(unsigned int n) |
---|
| 47 | { |
---|
| 48 | unsigned int oldSize = joyStickHandler_.size(); |
---|
| 49 | joyStickHandler_.resize(n); |
---|
| 50 | |
---|
[1659] | 51 | if (n > oldSize) |
---|
[1637] | 52 | { |
---|
| 53 | // we have to add the handler in joyStickHandlerAll_ to the joyStickHandler_[>n] |
---|
| 54 | for (unsigned int i = oldSize; i < n; ++i) |
---|
| 55 | { |
---|
| 56 | joyStickHandler_[i] = joyStickHandlerAll_; |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | update(); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | void SimpleInputState::keyPressed(const KeyEvent& evt) |
---|
| 63 | { |
---|
| 64 | if (keyHandler_) |
---|
| 65 | keyHandler_->keyPressed(evt); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | @brief |
---|
| 70 | Adds a joy stick handler. |
---|
| 71 | @param handler |
---|
| 72 | Pointer to the handler object. |
---|
| 73 | @param joyStickID |
---|
| 74 | ID of the joy stick |
---|
| 75 | @return |
---|
| 76 | True if added, false otherwise. |
---|
| 77 | */ |
---|
| 78 | bool SimpleInputState::setJoyStickHandler(JoyStickHandler* handler, unsigned int joyStickID) |
---|
| 79 | { |
---|
| 80 | if (joyStickID >= joyStickHandler_.size()) |
---|
| 81 | return false; |
---|
| 82 | |
---|
| 83 | joyStickHandler_[joyStickID] = handler; |
---|
| 84 | update(); |
---|
| 85 | return true; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | @brief |
---|
| 90 | Adds a joy stick handler. |
---|
| 91 | @param handler |
---|
| 92 | Pointer to the handler object. |
---|
| 93 | @return |
---|
| 94 | True if added, false if handler already existed. |
---|
| 95 | */ |
---|
| 96 | bool SimpleInputState::setJoyStickHandler(JoyStickHandler* handler) |
---|
| 97 | { |
---|
| 98 | if (handler == joyStickHandlerAll_) |
---|
| 99 | return false; |
---|
| 100 | |
---|
| 101 | joyStickHandlerAll_ = handler; |
---|
| 102 | for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandler_.size(); ++iJoyStick) |
---|
| 103 | setJoyStickHandler(handler, iJoyStick); |
---|
| 104 | update(); |
---|
| 105 | return true; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | @brief |
---|
| 110 | Adds a handler of any kind. dynamic_cast determines to which list it is added. |
---|
| 111 | @param handler |
---|
| 112 | Pointer to the handler object. |
---|
| 113 | @return |
---|
| 114 | True if added, false if handler already existed. |
---|
| 115 | */ |
---|
[1878] | 116 | bool SimpleInputState::setHandler(InputHandler* handler) |
---|
[1637] | 117 | { |
---|
| 118 | setKeyHandler(dynamic_cast<KeyHandler*>(handler)); |
---|
| 119 | setMouseHandler(dynamic_cast<MouseHandler*>(handler)); |
---|
| 120 | return setJoyStickHandler(dynamic_cast<JoyStickHandler*>(handler)); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | void SimpleInputState::update() |
---|
| 124 | { |
---|
| 125 | // we can use a set to have a list of unique pointers (an object can implement all 3 handlers) |
---|
[1878] | 126 | std::set<InputHandler*> tempSet; |
---|
[1637] | 127 | if (keyHandler_) |
---|
| 128 | tempSet.insert(keyHandler_); |
---|
| 129 | if (mouseHandler_) |
---|
| 130 | tempSet.insert(mouseHandler_); |
---|
| 131 | for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandler_.size(); iJoyStick++) |
---|
| 132 | if (joyStickHandler_[iJoyStick]) |
---|
| 133 | tempSet.insert(joyStickHandler_[iJoyStick]); |
---|
| 134 | |
---|
| 135 | // copy the content of the map back to the actual vector |
---|
| 136 | allHandlers_.clear(); |
---|
[1878] | 137 | for (std::set<InputHandler*>::const_iterator itHandler = tempSet.begin(); |
---|
[1637] | 138 | itHandler != tempSet.end(); itHandler++) |
---|
| 139 | allHandlers_.push_back(*itHandler); |
---|
| 140 | |
---|
| 141 | // update the deviceEnabled options |
---|
[1887] | 142 | setInputDeviceEnabled(InputDevice::Keyboard, (keyHandler_ != 0)); |
---|
| 143 | setInputDeviceEnabled(InputDevice::Mouse, (mouseHandler_ != 0)); |
---|
[1637] | 144 | for (unsigned int i = 0; i < joyStickHandler_.size(); ++i) |
---|
| 145 | setInputDeviceEnabled(2 + i, (joyStickHandler_[i] != 0)); |
---|
[1878] | 146 | |
---|
| 147 | // inform InputManager that there might be changes in EMPTY_HANDLER situation |
---|
| 148 | bHandlersChanged_ = true; |
---|
[1637] | 149 | } |
---|
| 150 | } |
---|