| 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 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #ifndef _SimpleInputState_H__ | 
|---|
| 35 | #define _SimpleInputState_H__ | 
|---|
| 36 |  | 
|---|
| 37 | #include "core/CorePrereqs.h" | 
|---|
| 38 |  | 
|---|
| 39 | #include <vector> | 
|---|
| 40 | #include <cassert> | 
|---|
| 41 | #include "InputInterfaces.h" | 
|---|
| 42 | #include "InputState.h" | 
|---|
| 43 |  | 
|---|
| 44 | namespace orxonox | 
|---|
| 45 | { | 
|---|
| 46 | class _CoreExport SimpleInputState : public InputState | 
|---|
| 47 | { | 
|---|
| 48 | friend class InputManager; | 
|---|
| 49 |  | 
|---|
| 50 | public: | 
|---|
| 51 | void setKeyHandler        (KeyHandler* handler) { keyHandler_ = handler; update(); } | 
|---|
| 52 | void setMouseHandler      (MouseHandler* handler) { mouseHandler_ = handler; update(); } | 
|---|
| 53 | bool setJoyStickHandler   (JoyStickHandler* handler, unsigned int joyStickID); | 
|---|
| 54 | bool setJoyStickHandler   (JoyStickHandler* handler); | 
|---|
| 55 | bool setHandler(InputHandler* handler); | 
|---|
| 56 |  | 
|---|
| 57 | private: | 
|---|
| 58 | SimpleInputState(); | 
|---|
| 59 | ~SimpleInputState() { } | 
|---|
| 60 |  | 
|---|
| 61 | void updateInput(float dt); | 
|---|
| 62 | void updateInput(float dt, unsigned int device); | 
|---|
| 63 |  | 
|---|
| 64 | void keyPressed (const KeyEvent& evt); | 
|---|
| 65 | void keyReleased(const KeyEvent& evt); | 
|---|
| 66 | void keyHeld    (const KeyEvent& evt); | 
|---|
| 67 |  | 
|---|
| 68 | void mouseButtonPressed (MouseButtonCode::ByEnum id); | 
|---|
| 69 | void mouseButtonReleased(MouseButtonCode::ByEnum id); | 
|---|
| 70 | void mouseButtonHeld    (MouseButtonCode::ByEnum id); | 
|---|
| 71 | void mouseMoved         (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize); | 
|---|
| 72 | void mouseScrolled      (int abs, int rel); | 
|---|
| 73 |  | 
|---|
| 74 | void joyStickButtonPressed (unsigned int joyStickID, JoyStickButtonCode::ByEnum id); | 
|---|
| 75 | void joyStickButtonReleased(unsigned int joyStickID, JoyStickButtonCode::ByEnum id); | 
|---|
| 76 | void joyStickButtonHeld    (unsigned int joyStickID, JoyStickButtonCode::ByEnum id); | 
|---|
| 77 | void joyStickAxisMoved     (unsigned int joyStickID, unsigned int axis, float value); | 
|---|
| 78 |  | 
|---|
| 79 | void update(); | 
|---|
| 80 | void numberOfJoySticksChanged(unsigned int n); | 
|---|
| 81 |  | 
|---|
| 82 | void onEnter(); | 
|---|
| 83 | void onLeave(); | 
|---|
| 84 |  | 
|---|
| 85 | KeyHandler*                   keyHandler_; | 
|---|
| 86 | MouseHandler*                 mouseHandler_; | 
|---|
| 87 | std::vector<JoyStickHandler*> joyStickHandler_; | 
|---|
| 88 | JoyStickHandler*              joyStickHandlerAll_; | 
|---|
| 89 | std::vector<InputHandler*>    allHandlers_; | 
|---|
| 90 | }; | 
|---|
| 91 |  | 
|---|
| 92 | inline void SimpleInputState::updateInput(float dt) | 
|---|
| 93 | { | 
|---|
| 94 | for (unsigned int i = 0; i < allHandlers_.size(); ++i) | 
|---|
| 95 | { | 
|---|
| 96 | allHandlers_[i]->updateInput(dt); | 
|---|
| 97 | } | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | inline void SimpleInputState::updateInput(float dt, unsigned int device) | 
|---|
| 101 | { | 
|---|
| 102 | switch (device) | 
|---|
| 103 | { | 
|---|
| 104 | case InputDevice::Keyboard: | 
|---|
| 105 | if (keyHandler_) | 
|---|
| 106 | keyHandler_->updateKey(dt); | 
|---|
| 107 | break; | 
|---|
| 108 |  | 
|---|
| 109 | case InputDevice::Mouse: | 
|---|
| 110 | if (mouseHandler_) | 
|---|
| 111 | mouseHandler_->updateMouse(dt); | 
|---|
| 112 | break; | 
|---|
| 113 |  | 
|---|
| 114 | default: // joy sticks | 
|---|
| 115 | if (joyStickHandler_[device - 2]) | 
|---|
| 116 | joyStickHandler_[device - 2]->updateJoyStick(dt, device - 2); | 
|---|
| 117 | break; | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | inline void SimpleInputState::keyReleased(const KeyEvent& evt) | 
|---|
| 122 | { | 
|---|
| 123 | if (keyHandler_) | 
|---|
| 124 | keyHandler_->keyReleased(evt); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | inline void SimpleInputState::keyHeld(const KeyEvent& evt) | 
|---|
| 128 | { | 
|---|
| 129 | if (keyHandler_) | 
|---|
| 130 | keyHandler_->keyHeld(evt); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | inline void SimpleInputState::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) | 
|---|
| 134 | { | 
|---|
| 135 | if (mouseHandler_) | 
|---|
| 136 | mouseHandler_->mouseMoved(abs, rel, clippingSize); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | inline void SimpleInputState::mouseScrolled(int abs, int rel) | 
|---|
| 140 | { | 
|---|
| 141 | if (mouseHandler_) | 
|---|
| 142 | mouseHandler_->mouseScrolled(abs, rel); | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | inline void SimpleInputState::mouseButtonPressed(MouseButtonCode::ByEnum id) | 
|---|
| 146 | { | 
|---|
| 147 | if (mouseHandler_) | 
|---|
| 148 | mouseHandler_->mouseButtonPressed(id); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | inline void SimpleInputState::mouseButtonReleased(MouseButtonCode::ByEnum id) | 
|---|
| 152 | { | 
|---|
| 153 | if (mouseHandler_) | 
|---|
| 154 | mouseHandler_->mouseButtonReleased(id); | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | inline void SimpleInputState::mouseButtonHeld(MouseButtonCode::ByEnum id) | 
|---|
| 158 | { | 
|---|
| 159 | if (mouseHandler_) | 
|---|
| 160 | mouseHandler_->mouseButtonHeld(id); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | inline void SimpleInputState::joyStickAxisMoved(unsigned int joyStickID, unsigned int axis, float value) | 
|---|
| 164 | { | 
|---|
| 165 | assert(joyStickID < joyStickHandler_.size()); | 
|---|
| 166 | if (joyStickHandler_[joyStickID]) | 
|---|
| 167 | joyStickHandler_[joyStickID]->joyStickAxisMoved(joyStickID, axis, value); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | inline void SimpleInputState::joyStickButtonPressed(unsigned int joyStickID, JoyStickButtonCode::ByEnum id) | 
|---|
| 171 | { | 
|---|
| 172 | assert(joyStickID < joyStickHandler_.size()); | 
|---|
| 173 | if (joyStickHandler_[joyStickID]) | 
|---|
| 174 | joyStickHandler_[joyStickID]->joyStickButtonPressed(joyStickID, id); | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | inline void SimpleInputState::joyStickButtonReleased(unsigned int joyStickID, JoyStickButtonCode::ByEnum id) | 
|---|
| 178 | { | 
|---|
| 179 | assert(joyStickID < joyStickHandler_.size()); | 
|---|
| 180 | if (joyStickHandler_[joyStickID]) | 
|---|
| 181 | joyStickHandler_[joyStickID]->joyStickButtonReleased(joyStickID, id); | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | inline void SimpleInputState::joyStickButtonHeld(unsigned int joyStickID, JoyStickButtonCode::ByEnum id) | 
|---|
| 185 | { | 
|---|
| 186 | assert(joyStickID < joyStickHandler_.size()); | 
|---|
| 187 | if (joyStickHandler_[joyStickID]) | 
|---|
| 188 | joyStickHandler_[joyStickID]->joyStickButtonHeld(joyStickID, id); | 
|---|
| 189 | } | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | #endif /* _SimpleInputState_H__ */ | 
|---|