[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 | #ifndef _InputState_H__ |
---|
| 30 | #define _InputState_H__ |
---|
| 31 | |
---|
[3327] | 32 | #include "InputPrereqs.h" |
---|
[1637] | 33 | |
---|
[3327] | 34 | #include <cassert> |
---|
[1637] | 35 | #include <string> |
---|
| 36 | #include <vector> |
---|
[11071] | 37 | #include <functional> |
---|
[1637] | 38 | |
---|
[8729] | 39 | #include "util/tribool.h" |
---|
[3327] | 40 | #include "InputHandler.h" |
---|
[6746] | 41 | #include "InputManager.h" |
---|
[3327] | 42 | #include "JoyStickQuantityListener.h" |
---|
| 43 | |
---|
[6746] | 44 | #define INPUT_STATE_PUSH_CALL(deviceIndex, functionName, ...) \ |
---|
[11071] | 45 | InputManager::getInstance().pushCall(std::function<void ()>(std::bind(&InputHandler::functionName, handlers_[deviceIndex], __VA_ARGS__))) |
---|
[6746] | 46 | |
---|
[1637] | 47 | namespace orxonox |
---|
| 48 | { |
---|
[3327] | 49 | /** |
---|
| 50 | @brief |
---|
| 51 | InputStates allow you to customise the input event targets at runtime. |
---|
| 52 | |
---|
| 53 | The general idea is a stack: Every activated InputState will be pushed on |
---|
| 54 | that stack and only the top one gets the input events. This is done for |
---|
| 55 | every device (keyboard, mouse, all joy sticks) separately to allow |
---|
[6746] | 56 | for instance keyboard input capturing for the console while you can still |
---|
[3327] | 57 | steer a ship with the mouse. |
---|
| 58 | There are two exceptions to this behaviour though: |
---|
| 59 | - If an InputState is created with the 'Transparent' parameter on, the |
---|
| 60 | state will not prevent input from getting to the state below it on the stack. |
---|
| 61 | This can be useful for instance if you need to deploy input to multiple |
---|
| 62 | handlers: Simply create two InputStates and make the high priority one transparent. |
---|
| 63 | - If an InputState is created with the 'AlwaysGetsInput' parameter on, then |
---|
| 64 | the state will always receive input as long as it is activated. |
---|
| 65 | - Note: If you mark an InputState with both parameters on, then it will |
---|
[6746] | 66 | not influence only other InputState at all. |
---|
[3327] | 67 | |
---|
[5695] | 68 | @par Priorities |
---|
[3327] | 69 | Every InputState has a priority when on the stack, but mostly this |
---|
| 70 | priority is dynamic (InputStatePriority::Dynamic) which means that a state |
---|
| 71 | pushed onto the stack will simply have a higher priority than the top one. |
---|
| 72 | This behaviour really only applies to normal states that don't have |
---|
| 73 | a high priority (InputStatePriority::HighPriority). These 'special' ones |
---|
| 74 | are used for features like the KeyDetector or the console. Use with care! |
---|
[5695] | 75 | |
---|
| 76 | @par Exclusive/Non-Exclusive mouse Mode |
---|
| 77 | You can select a specific mouse mode that tells whether the application |
---|
[6746] | 78 | should have exclusive access to it or not. |
---|
[5695] | 79 | When in non-exclusive mode, you can move the mouse out of the window |
---|
| 80 | like with any other normal window (only for windowed mode!). |
---|
| 81 | The setting is dictated by the topmost InputState that gets mouse events. |
---|
[3327] | 82 | */ |
---|
| 83 | class _CoreExport InputState : public JoyStickQuantityListener |
---|
| 84 | { |
---|
[1637] | 85 | friend class InputManager; |
---|
| 86 | |
---|
[3327] | 87 | //! Marks the index in the handler vector for the keyboard handler |
---|
| 88 | static const InputDeviceEnumerator::Value keyboardIndex_s = InputDeviceEnumerator::Keyboard; |
---|
| 89 | //! Marks the index in the handler vector for the mouse handler |
---|
| 90 | static const InputDeviceEnumerator::Value mouseIndex_s = InputDeviceEnumerator::Mouse; |
---|
| 91 | //! Marks the index in the handler vector for the first joy stick handler |
---|
| 92 | static const InputDeviceEnumerator::Value firstJoyStickIndex_s = InputDeviceEnumerator::FirstJoyStick; |
---|
| 93 | |
---|
[1637] | 94 | public: |
---|
[3327] | 95 | //! Sets the keyboard event handler (overwrites if there already was one!) |
---|
| 96 | void setKeyHandler (InputHandler* handler) |
---|
| 97 | { handlers_[keyboardIndex_s] = handler; bExpired_ = true; } |
---|
| 98 | //! Sets the mouse event handler (overwrites if there already was one!) |
---|
| 99 | void setMouseHandler (InputHandler* handler) |
---|
| 100 | { handlers_[mouseIndex_s] = handler; bExpired_ = true; } |
---|
| 101 | /** |
---|
| 102 | @brief |
---|
| 103 | Sets the joy stick event handler for one specific joy stick (overwrites if there already was one!) |
---|
| 104 | @return |
---|
| 105 | Returns false if the specified device was not found |
---|
| 106 | */ |
---|
| 107 | bool setJoyStickHandler(InputHandler* handler, unsigned int joyStick); |
---|
| 108 | //! Sets the joy stick event handler for all joy sticks (overwrites if there already was one!) |
---|
| 109 | void setJoyStickHandler(InputHandler* handler); |
---|
| 110 | //! Sets an InputHandler to be used for all devices |
---|
| 111 | void setHandler (InputHandler* handler); |
---|
| 112 | |
---|
[8729] | 113 | void setMouseExclusive(tribool value) { exclusiveMouse_ = value; this->bExpired_ = true; } |
---|
| 114 | tribool getMouseExclusive() const { return exclusiveMouse_; } |
---|
[5695] | 115 | |
---|
[3327] | 116 | //! Returns the name of the state (which is unique!) |
---|
[1637] | 117 | const std::string& getName() const { return name_; } |
---|
[3327] | 118 | //! Returns the priority of the state (which is unique if != 0) |
---|
[1637] | 119 | int getPriority() const { return priority_; } |
---|
| 120 | |
---|
[3327] | 121 | //! Tells whether there a handler installed for a specific device |
---|
| 122 | bool isInputDeviceEnabled(unsigned int device); |
---|
[1637] | 123 | |
---|
[3327] | 124 | //! Returns true if the handler situation has changed |
---|
| 125 | bool hasExpired() { return this->bExpired_; } |
---|
| 126 | //! Call this if you have applied the changes resulting from changed handlers |
---|
| 127 | void resetExpiration() { bExpired_ = false; } |
---|
[1878] | 128 | |
---|
[7401] | 129 | //! Updates one specific device handler with deviceUpdated |
---|
[3327] | 130 | void update(float dt, unsigned int device); |
---|
| 131 | //! Updates all handlers with allDevicesUpdated |
---|
| 132 | void update(float dt); |
---|
[1637] | 133 | |
---|
[3327] | 134 | //! Generic function that distributes all 9 button events |
---|
[6746] | 135 | template <typename EventType, class ButtonTypeParam> |
---|
| 136 | void buttonEvent(unsigned int device, ButtonTypeParam button); |
---|
[1637] | 137 | |
---|
[3327] | 138 | //! Event handler |
---|
| 139 | void mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize); |
---|
| 140 | //! Event handler |
---|
| 141 | void mouseScrolled(int abs, int rel); |
---|
| 142 | //! Event handler |
---|
| 143 | void joyStickAxisMoved(unsigned int device, unsigned int axis, float value); |
---|
[1637] | 144 | |
---|
[3327] | 145 | // Functors |
---|
| 146 | //! Called when the state is being activated (even if it doesn't get any events afterwards!) |
---|
| 147 | void entered(); |
---|
| 148 | //! Called upon deactivation of the state |
---|
| 149 | void left(); |
---|
| 150 | //! Sets a functor to be called upon activation of the state |
---|
[7284] | 151 | void setEnterFunctor(const FunctorPtr& functor) { this->enterFunctor_ = functor; } |
---|
[3327] | 152 | //! Sets a functor to be called upon deactivation of the state |
---|
[7284] | 153 | void setLeaveFunctor(const FunctorPtr& functor) { this->leaveFunctor_ = functor; } |
---|
[1637] | 154 | |
---|
[3327] | 155 | private: |
---|
| 156 | InputState(const std::string& name, bool bAlwaysGetsInput, bool bTransparent, InputStatePriority priority); |
---|
[11071] | 157 | ~InputState() = default; |
---|
[1637] | 158 | |
---|
[11071] | 159 | virtual void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList) override; |
---|
[1637] | 160 | |
---|
[3327] | 161 | //! Sets the priority (only to be used by the InputManager!) |
---|
| 162 | void setPriority(int priority) { priority_ = priority; } |
---|
[1646] | 163 | |
---|
[3327] | 164 | const std::string name_; //!< Name of the state |
---|
| 165 | const bool bAlwaysGetsInput_; //!< See class declaration for explanation |
---|
| 166 | const bool bTransparent_; //!< See class declaration for explanation |
---|
[8729] | 167 | tribool exclusiveMouse_; //!< See class declaration for explanation |
---|
[3327] | 168 | int priority_; //!< Current priority (might change) |
---|
| 169 | bool bExpired_; //!< See hasExpired() |
---|
| 170 | std::vector<InputHandler*> handlers_; //!< Vector with all handlers where the index is the device ID |
---|
| 171 | //! Handler to be used for all joy sticks (needs to be saved in case another joy stick gets attached) |
---|
| 172 | InputHandler* joyStickHandlerAll_; |
---|
[7284] | 173 | FunctorPtr enterFunctor_; //!< Functor to be executed on enter |
---|
| 174 | FunctorPtr leaveFunctor_; //!< Functor to be executed on leave |
---|
[3327] | 175 | }; |
---|
| 176 | |
---|
[8351] | 177 | ORX_FORCEINLINE void InputState::update(float dt) |
---|
[3327] | 178 | { |
---|
| 179 | for (unsigned int i = 0; i < handlers_.size(); ++i) |
---|
[11071] | 180 | if (handlers_[i] != nullptr) |
---|
[6746] | 181 | INPUT_STATE_PUSH_CALL(i, allDevicesUpdated, dt); |
---|
[3327] | 182 | } |
---|
| 183 | |
---|
[8351] | 184 | ORX_FORCEINLINE void InputState::update(float dt, unsigned int device) |
---|
[3327] | 185 | { |
---|
| 186 | switch (device) |
---|
[1637] | 187 | { |
---|
[3327] | 188 | case InputDeviceEnumerator::Keyboard: |
---|
[11071] | 189 | if (handlers_[keyboardIndex_s] != nullptr) |
---|
[6746] | 190 | INPUT_STATE_PUSH_CALL(keyboardIndex_s, keyboardUpdated, dt); |
---|
[3327] | 191 | break; |
---|
[1637] | 192 | |
---|
[3327] | 193 | case InputDeviceEnumerator::Mouse: |
---|
[11071] | 194 | if (handlers_[mouseIndex_s] != nullptr) |
---|
[6746] | 195 | INPUT_STATE_PUSH_CALL(mouseIndex_s, mouseUpdated, dt); |
---|
[3327] | 196 | break; |
---|
[1878] | 197 | |
---|
[3327] | 198 | default: // joy sticks |
---|
[11071] | 199 | if (handlers_[device] != nullptr) |
---|
[6746] | 200 | INPUT_STATE_PUSH_CALL(device, joyStickUpdated, device - firstJoyStickIndex_s, dt); |
---|
[3327] | 201 | break; |
---|
[1637] | 202 | } |
---|
[3327] | 203 | } |
---|
[1637] | 204 | |
---|
[6746] | 205 | template <typename EventType, class ButtonTypeParam> |
---|
[8351] | 206 | ORX_FORCEINLINE void InputState::buttonEvent(unsigned int device, ButtonTypeParam button) |
---|
[3327] | 207 | { |
---|
| 208 | assert(device < handlers_.size()); |
---|
[11071] | 209 | if (handlers_[device] != nullptr) |
---|
[6746] | 210 | { |
---|
| 211 | // We have to store the function pointer to tell the compiler about its actual type because of overloading |
---|
| 212 | void (InputHandler::*function)(unsigned int, ButtonTypeParam, EventType) = &InputHandler::buttonEvent<ButtonTypeParam>; |
---|
[11071] | 213 | InputManager::getInstance().pushCall(std::function<void ()>(std::bind(function, handlers_[device], device, button, EventType()))); |
---|
[6746] | 214 | } |
---|
[3327] | 215 | } |
---|
| 216 | |
---|
[8351] | 217 | ORX_FORCEINLINE void InputState::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
[3327] | 218 | { |
---|
[11071] | 219 | if (handlers_[mouseIndex_s] != nullptr) |
---|
[6746] | 220 | INPUT_STATE_PUSH_CALL(mouseIndex_s, mouseMoved, abs, rel, clippingSize); |
---|
[3327] | 221 | } |
---|
| 222 | |
---|
[8351] | 223 | ORX_FORCEINLINE void InputState::mouseScrolled(int abs, int rel) |
---|
[3327] | 224 | { |
---|
[11071] | 225 | if (handlers_[mouseIndex_s] != nullptr) |
---|
[6746] | 226 | INPUT_STATE_PUSH_CALL(mouseIndex_s, mouseScrolled, abs, rel); |
---|
[3327] | 227 | } |
---|
| 228 | |
---|
[8351] | 229 | ORX_FORCEINLINE void InputState::joyStickAxisMoved(unsigned int device, unsigned int axis, float value) |
---|
[3327] | 230 | { |
---|
| 231 | assert(device < handlers_.size()); |
---|
[11071] | 232 | if (handlers_[device] != nullptr) |
---|
[6746] | 233 | INPUT_STATE_PUSH_CALL(device, axisMoved, device - firstJoyStickIndex_s, axis, value); |
---|
[3327] | 234 | } |
---|
[1637] | 235 | } |
---|
| 236 | |
---|
| 237 | #endif /* _InputState_H__ */ |
---|