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