| 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 | #ifndef _InputState_H__ |
|---|
| 30 | #define _InputState_H__ |
|---|
| 31 | |
|---|
| 32 | #include "InputPrereqs.h" |
|---|
| 33 | |
|---|
| 34 | #include <cassert> |
|---|
| 35 | #include <string> |
|---|
| 36 | #include <vector> |
|---|
| 37 | |
|---|
| 38 | #include "InputHandler.h" |
|---|
| 39 | #include "JoyStickQuantityListener.h" |
|---|
| 40 | |
|---|
| 41 | namespace orxonox |
|---|
| 42 | { |
|---|
| 43 | class _CoreExport InputState : public JoyStickQuantityListener |
|---|
| 44 | { |
|---|
| 45 | friend class InputManager; |
|---|
| 46 | |
|---|
| 47 | static const InputDeviceEnumerator::Value keyboardIndex_s = InputDeviceEnumerator::Keyboard; |
|---|
| 48 | static const InputDeviceEnumerator::Value mouseIndex_s = InputDeviceEnumerator::Mouse; |
|---|
| 49 | static const InputDeviceEnumerator::Value firstJoyStickIndex_s = InputDeviceEnumerator::FirstJoyStick; |
|---|
| 50 | |
|---|
| 51 | public: |
|---|
| 52 | void setKeyHandler (InputHandler* handler) |
|---|
| 53 | { handlers_[keyboardIndex_s] = handler; bExpired_ = true; } |
|---|
| 54 | void setMouseHandler (InputHandler* handler) |
|---|
| 55 | { handlers_[mouseIndex_s] = handler; bExpired_ = true; } |
|---|
| 56 | bool setJoyStickHandler(InputHandler* handler, unsigned int joyStick); |
|---|
| 57 | bool setJoyStickHandler(InputHandler* handler); |
|---|
| 58 | bool setHandler (InputHandler* handler); |
|---|
| 59 | |
|---|
| 60 | const std::string& getName() const { return name_; } |
|---|
| 61 | int getPriority() const { return priority_; } |
|---|
| 62 | |
|---|
| 63 | bool isInputDeviceEnabled(unsigned int device); |
|---|
| 64 | |
|---|
| 65 | bool hasExpired() { return this->bExpired_; } |
|---|
| 66 | void resetExpiration() { bExpired_ = false; } |
|---|
| 67 | |
|---|
| 68 | void update(float dt, unsigned int device); |
|---|
| 69 | void update(float dt); |
|---|
| 70 | |
|---|
| 71 | template <typename EventType, class Traits> |
|---|
| 72 | void buttonEvent(unsigned int device, const typename Traits::ButtonTypeParam button); |
|---|
| 73 | |
|---|
| 74 | void mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize); |
|---|
| 75 | void mouseScrolled(int abs, int rel); |
|---|
| 76 | void joyStickAxisMoved(unsigned int device, unsigned int axis, float value); |
|---|
| 77 | |
|---|
| 78 | // Functors |
|---|
| 79 | void entered(); |
|---|
| 80 | void left(); |
|---|
| 81 | void setEnterFunctor(Functor* functor) { this->enterFunctor_ = functor; } |
|---|
| 82 | void setLeaveFunctor(Functor* functor) { this->leaveFunctor_ = functor; } |
|---|
| 83 | |
|---|
| 84 | private: |
|---|
| 85 | InputState(); |
|---|
| 86 | ~InputState() { } |
|---|
| 87 | |
|---|
| 88 | void JoyStickQuantityChanged(unsigned int n); |
|---|
| 89 | |
|---|
| 90 | void setName(const std::string& name) { name_ = name; } |
|---|
| 91 | void setPriority(int priority) { priority_ = priority; } |
|---|
| 92 | |
|---|
| 93 | std::string name_; |
|---|
| 94 | int priority_; |
|---|
| 95 | bool bAlwaysGetsInput_; |
|---|
| 96 | bool bTransparent_; |
|---|
| 97 | bool bExpired_; |
|---|
| 98 | std::vector<InputHandler*> handlers_; |
|---|
| 99 | InputHandler* joyStickHandlerAll_; |
|---|
| 100 | Functor* enterFunctor_; |
|---|
| 101 | Functor* leaveFunctor_; |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | inline void InputState::update(float dt) |
|---|
| 105 | { |
|---|
| 106 | for (unsigned int i = 0; i < handlers_.size(); ++i) |
|---|
| 107 | if (handlers_[i] != NULL) |
|---|
| 108 | handlers_[i]->allDevicesUpdated(dt); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | inline void InputState::update(float dt, unsigned int device) |
|---|
| 112 | { |
|---|
| 113 | switch (device) |
|---|
| 114 | { |
|---|
| 115 | case InputDeviceEnumerator::Keyboard: |
|---|
| 116 | if (handlers_[keyboardIndex_s] != NULL) |
|---|
| 117 | handlers_[keyboardIndex_s]->keyboardUpdated(dt); |
|---|
| 118 | break; |
|---|
| 119 | |
|---|
| 120 | case InputDeviceEnumerator::Mouse: |
|---|
| 121 | if (handlers_[mouseIndex_s] != NULL) |
|---|
| 122 | handlers_[mouseIndex_s]->mouseUpdated(dt); |
|---|
| 123 | break; |
|---|
| 124 | |
|---|
| 125 | default: // joy sticks |
|---|
| 126 | if (handlers_[device] != NULL) |
|---|
| 127 | handlers_[device]->joyStickUpdated(device - firstJoyStickIndex_s, dt); |
|---|
| 128 | break; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | template <typename EventType, class Traits> |
|---|
| 133 | inline void InputState::buttonEvent(unsigned int device, const typename Traits::ButtonTypeParam button) |
|---|
| 134 | { |
|---|
| 135 | assert(device < handlers_.size()); |
|---|
| 136 | if (handlers_[device] != NULL) |
|---|
| 137 | handlers_[device]->buttonEvent(device, button, EventType()); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | inline void InputState::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
|---|
| 141 | { |
|---|
| 142 | if (handlers_[mouseIndex_s] != NULL) |
|---|
| 143 | handlers_[mouseIndex_s]->mouseMoved(abs, rel, clippingSize); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | inline void InputState::mouseScrolled(int abs, int rel) |
|---|
| 147 | { |
|---|
| 148 | if (handlers_[mouseIndex_s] != NULL) |
|---|
| 149 | handlers_[mouseIndex_s]->mouseScrolled(abs, rel); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | inline void InputState::joyStickAxisMoved(unsigned int device, unsigned int axis, float value) |
|---|
| 153 | { |
|---|
| 154 | assert(device < handlers_.size()); |
|---|
| 155 | if (handlers_[device] != NULL) |
|---|
| 156 | handlers_[device]->axisMoved(device - firstJoyStickIndex_s, axis, value); |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | #endif /* _InputState_H__ */ |
|---|