[918] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1502] | 3 | * > www.orxonox.net < |
---|
[918] | 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 | */ |
---|
[973] | 28 | |
---|
| 29 | #ifndef _InputManager_H__ |
---|
| 30 | #define _InputManager_H__ |
---|
| 31 | |
---|
[3327] | 32 | #include "InputPrereqs.h" |
---|
[1062] | 33 | |
---|
[1219] | 34 | #include <map> |
---|
[3196] | 35 | #include <set> |
---|
| 36 | #include <string> |
---|
[1293] | 37 | #include <vector> |
---|
[3196] | 38 | |
---|
[3370] | 39 | #include "util/Singleton.h" |
---|
[3327] | 40 | #include "core/WindowEventListener.h" |
---|
| 41 | #include "InputState.h" |
---|
[973] | 42 | |
---|
[6417] | 43 | // tolua_begin |
---|
[973] | 44 | namespace orxonox |
---|
| 45 | { |
---|
[1755] | 46 | /** |
---|
| 47 | @brief |
---|
[3327] | 48 | Manages the input devices (mouse, keyboard, joy sticks) and the input states. |
---|
[1349] | 49 | |
---|
[6084] | 50 | Every input device has its own wrapper class which does the actually input event |
---|
[3327] | 51 | distribution. The InputManager only creates reloads (on request) those devices. |
---|
[973] | 52 | |
---|
[3327] | 53 | The other functionality concerns handling InputStates. They act as a layer |
---|
| 54 | between InputHandlers (like the KeyBinder or the GUIManager) and InputDevices. |
---|
| 55 | InputStates are memory managed by the IputManager. You cannot create or destroy |
---|
| 56 | them on your own. Therefore all states get destroyed with the InputManager d'tor. |
---|
| 57 | @note |
---|
| 58 | - The actual lists containing all the InputStates for a specific device are stored |
---|
| 59 | in the InputDevices themselves. |
---|
| 60 | - The devices_ vector always has at least two elements: Keyboard (first) and mouse. |
---|
[6084] | 61 | You best access them internally with InputDeviceEnumerator::Keyboard/Mouse |
---|
[3327] | 62 | The first joy stick is accessed with InputDeviceEnumerator::FirstJoyStick. |
---|
| 63 | - Keyboard construction is mandatory , mouse and joy sticks are not. |
---|
| 64 | If the OIS::InputManager or the Keyboard fail, an exception is thrown. |
---|
[1755] | 65 | */ |
---|
[6417] | 66 | class _CoreExport InputManager |
---|
| 67 | // tolua_end |
---|
| 68 | : public Singleton<InputManager>, public WindowEventListener |
---|
| 69 | { // tolua_export |
---|
[3370] | 70 | friend class Singleton<InputManager>; |
---|
[1755] | 71 | public: |
---|
[3327] | 72 | //! Represents internal states of the InputManager. |
---|
| 73 | enum State |
---|
[1755] | 74 | { |
---|
[3327] | 75 | Nothing = 0x00, |
---|
| 76 | Bad = 0x02, |
---|
| 77 | Ticking = 0x04, |
---|
| 78 | Calibrating = 0x08, |
---|
| 79 | ReloadRequest = 0x10, |
---|
[1755] | 80 | }; |
---|
[1219] | 81 | |
---|
[3327] | 82 | /** |
---|
| 83 | @brief |
---|
| 84 | Loads the devices and initialises the KeyDetector and the Calibrator. |
---|
[6417] | 85 | |
---|
[3327] | 86 | If either the OIS input system and/or the keyboard could not be created, |
---|
| 87 | the constructor fails with an std::exception. |
---|
| 88 | */ |
---|
[5695] | 89 | InputManager(); |
---|
[3327] | 90 | //! Destroys all devices AND all input states! |
---|
[1755] | 91 | ~InputManager(); |
---|
[3327] | 92 | void setConfigValues(); |
---|
[1293] | 93 | |
---|
[3327] | 94 | /** |
---|
| 95 | @brief |
---|
| 96 | Updates the devices (which distribute the input events) and the input states. |
---|
[1219] | 97 | |
---|
[3327] | 98 | Any InpuStates changes (destroy, enter, leave) and happens here. If a reload request |
---|
[6084] | 99 | was submitted while updating, the request will be postponed until the next update call. |
---|
[3327] | 100 | */ |
---|
[6417] | 101 | void preUpdate(const Clock& time); |
---|
[3327] | 102 | //! Clears all input device buffers. This usually only includes the pressed button list. |
---|
[1878] | 103 | void clearBuffers(); |
---|
[3327] | 104 | //! Starts joy stick calibration. |
---|
| 105 | void calibrate(); |
---|
| 106 | /** |
---|
| 107 | @brief |
---|
| 108 | Reloads all the input devices. Use this method to initialise new joy sticks. |
---|
| 109 | @note |
---|
[6417] | 110 | Only reloads immediately if the call stack doesn't include the preUpdate() method. |
---|
[3327] | 111 | */ |
---|
| 112 | void reload(); |
---|
[1878] | 113 | |
---|
[3327] | 114 | //------------------------------- |
---|
| 115 | // Input States |
---|
| 116 | //------------------------------- |
---|
| 117 | /** |
---|
| 118 | @brief |
---|
| 119 | Creates a new InputState that gets managed by the InputManager. |
---|
| 120 | @remarks |
---|
| 121 | The InputManager will take care of the state completely. That also |
---|
| 122 | means it gets deleted when the InputManager is destroyed! |
---|
| 123 | @param name |
---|
| 124 | Unique name of the InputState when referenced as string |
---|
| 125 | @param priority |
---|
| 126 | Priority matters when multiple states are active. You can specify any |
---|
| 127 | number, but 1 - 99 is preferred (99 means high priority). |
---|
| 128 | */ |
---|
| 129 | InputState* createInputState(const std::string& name, bool bAlwaysGetsInput = false, bool bTransparent = false, InputStatePriority priority = InputStatePriority::Dynamic); |
---|
| 130 | /** |
---|
| 131 | @brief |
---|
| 132 | Returns a pointer to a InputState referenced by name. |
---|
| 133 | @return |
---|
| 134 | Returns NULL if state was not found. |
---|
| 135 | */ |
---|
| 136 | InputState* getState(const std::string& name); |
---|
| 137 | /** |
---|
| 138 | @brief |
---|
| 139 | Activates a specific input state. |
---|
[6084] | 140 | It might not actually be activated if the priority is too low! |
---|
[3327] | 141 | @return |
---|
| 142 | False if name was not found, true otherwise. |
---|
| 143 | */ |
---|
[6417] | 144 | bool enterState(const std::string& name); // tolua_export |
---|
[3327] | 145 | /** |
---|
| 146 | @brief |
---|
| 147 | Deactivates a specific input state. |
---|
| 148 | @return |
---|
| 149 | False if name was not found, true otherwise. |
---|
| 150 | */ |
---|
[6417] | 151 | bool leaveState(const std::string& name); // tolua_export |
---|
[3327] | 152 | /** |
---|
| 153 | @brief |
---|
| 154 | Removes and destroys an input state. |
---|
| 155 | @return |
---|
| 156 | True if removal was successful, false if name was not found. |
---|
| 157 | @remarks |
---|
| 158 | - You can't remove the internal states "empty", "calibrator" and "detector". |
---|
[6417] | 159 | - The removal process is being postponed if InputManager::preUpdate() is currently running. |
---|
[3327] | 160 | */ |
---|
| 161 | bool destroyState(const std::string& name); |
---|
[1502] | 162 | |
---|
[3327] | 163 | //------------------------------- |
---|
| 164 | // Various getters and setters |
---|
| 165 | //------------------------------- |
---|
| 166 | //! Returns the number of joy stick that have been created since the c'tor or last call to reload(). |
---|
| 167 | unsigned int getJoyStickQuantity() const |
---|
| 168 | { return devices_.size() - InputDeviceEnumerator::FirstJoyStick; } |
---|
| 169 | //! Returns a pointer to the OIS InputManager. Only you if you know what you're doing! |
---|
[5695] | 170 | OIS::InputManager* getOISInputManager() { return this->oisInputManager_; } |
---|
| 171 | std::pair<int, int> getMousePosition() const; |
---|
[1502] | 172 | |
---|
[6417] | 173 | static InputManager& getInstance() { return Singleton<InputManager>::getInstance(); } // tolua_export |
---|
| 174 | |
---|
[1755] | 175 | private: // functions |
---|
| 176 | // don't mess with a Singleton |
---|
[3327] | 177 | InputManager(const InputManager&); |
---|
[973] | 178 | |
---|
[6084] | 179 | // Internal methods |
---|
[5695] | 180 | void loadDevices(); |
---|
[3327] | 181 | void loadMouse(); |
---|
| 182 | void loadJoySticks(); |
---|
| 183 | void destroyDevices(); |
---|
[973] | 184 | |
---|
[3327] | 185 | void stopCalibration(); |
---|
| 186 | void reloadInternal(); |
---|
[2662] | 187 | |
---|
[3327] | 188 | void destroyStateInternal(InputState* state); |
---|
| 189 | void updateActiveStates(); |
---|
[1349] | 190 | |
---|
[3327] | 191 | // From WindowEventListener |
---|
| 192 | void windowFocusChanged(); |
---|
[1502] | 193 | |
---|
[1755] | 194 | private: // variables |
---|
[3327] | 195 | State internalState_; //!< Current internal state |
---|
| 196 | OIS::InputManager* oisInputManager_; //!< OIS input manager |
---|
| 197 | std::vector<InputDevice*> devices_; //!< List of all input devices (keyboard, mouse, joy sticks) |
---|
[5929] | 198 | MouseMode::Value mouseMode_; //!< Currently applied mouse mode |
---|
[1219] | 199 | |
---|
[1878] | 200 | // some internally handled states and handlers |
---|
[3327] | 201 | InputState* emptyState_; //!< Lowest priority states (makes handling easier) |
---|
| 202 | //! InputBuffer that reacts to the Enter key when calibrating the joy sticks |
---|
| 203 | InputBuffer* calibratorCallbackHandler_; |
---|
[1502] | 204 | |
---|
[3327] | 205 | std::map<std::string, InputState*> statesByName_; //!< Contains all the created input states by name |
---|
| 206 | std::map<int, InputState*> activeStates_; //!< Contains all active input states by priority (std::map is sorted!) |
---|
| 207 | std::vector<InputState*> activeStatesTicked_; //!< Like activeStates_, but only contains the ones that currently receive events |
---|
[1219] | 208 | |
---|
[3327] | 209 | std::set<InputState*> stateEnterRequests_; //!< Requests to enter a new state |
---|
| 210 | std::set<InputState*> stateLeaveRequests_; //!< Requests to leave a running state |
---|
| 211 | std::set<InputState*> stateDestroyRequests_; //!< Requests to destroy a state |
---|
[1502] | 212 | |
---|
[3370] | 213 | static InputManager* singletonPtr_s; //!< Pointer reference to the singleton |
---|
[6417] | 214 | }; // tolua_export |
---|
| 215 | } // tolua_export |
---|
[973] | 216 | |
---|
| 217 | #endif /* _InputManager_H__ */ |
---|