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