| 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 | /** | 
|---|
| 30 | @file | 
|---|
| 31 | @brief | 
|---|
| 32 |     Implementation of a little Input handler that distributes everything | 
|---|
| 33 |     coming from OIS. | 
|---|
| 34 | */ | 
|---|
| 35 |  | 
|---|
| 36 | #ifndef _InputManager_H__ | 
|---|
| 37 | #define _InputManager_H__ | 
|---|
| 38 |  | 
|---|
| 39 | #include "core/CorePrereqs.h" | 
|---|
| 40 |  | 
|---|
| 41 | #include <map> | 
|---|
| 42 | #include <vector> | 
|---|
| 43 | #include <stack> | 
|---|
| 44 | #include "util/Math.h" | 
|---|
| 45 | #include "core/OrxonoxClass.h" | 
|---|
| 46 | #include "InputInterfaces.h" | 
|---|
| 47 |  | 
|---|
| 48 | namespace orxonox | 
|---|
| 49 | { | 
|---|
| 50 |     /** | 
|---|
| 51 |     @brief | 
|---|
| 52 |         Helper class to realise a vector<int[4]> | 
|---|
| 53 |     */ | 
|---|
| 54 |     class POVStates | 
|---|
| 55 |     { | 
|---|
| 56 |     public: | 
|---|
| 57 |         int& operator[](unsigned int index) { return povStates[index]; } | 
|---|
| 58 |         int povStates[4]; | 
|---|
| 59 |     }; | 
|---|
| 60 |  | 
|---|
| 61 |     /** | 
|---|
| 62 |     @brief | 
|---|
| 63 |         Helper class to realise a vector< {int[4], int[4]} > | 
|---|
| 64 |     */ | 
|---|
| 65 |     class SliderStates | 
|---|
| 66 |     { | 
|---|
| 67 |     public: | 
|---|
| 68 |         IntVector2 sliderStates[4]; | 
|---|
| 69 |     }; | 
|---|
| 70 |  | 
|---|
| 71 |     struct JoyStickCalibration | 
|---|
| 72 |     { | 
|---|
| 73 |         int middleValue[24]; | 
|---|
| 74 |         float positiveCoeff[24]; | 
|---|
| 75 |         float negativeCoeff[24]; | 
|---|
| 76 |     }; | 
|---|
| 77 |  | 
|---|
| 78 |     /** | 
|---|
| 79 |     @brief | 
|---|
| 80 |         Captures and distributes mouse and keyboard input. | 
|---|
| 81 |     */ | 
|---|
| 82 |     class _CoreExport InputManager | 
|---|
| 83 |         : public OrxonoxClass, | 
|---|
| 84 |         public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener | 
|---|
| 85 |     { | 
|---|
| 86 |         // --> setConfigValues is private | 
|---|
| 87 |         friend class ClassIdentifier<InputManager>; | 
|---|
| 88 |  | 
|---|
| 89 |     public: | 
|---|
| 90 |         enum InputManagerState | 
|---|
| 91 |         { | 
|---|
| 92 |             Uninitialised    = 0x00, | 
|---|
| 93 |             OISReady         = 0x01, | 
|---|
| 94 |             InternalsReady   = 0x02, | 
|---|
| 95 |             Ticking          = 0x04, | 
|---|
| 96 |             Calibrating      = 0x08, | 
|---|
| 97 |             ReloadRequest    = 0x10, | 
|---|
| 98 |             JoyStickSupport  = 0x20 // used with ReloadRequest to store a bool | 
|---|
| 99 |         }; | 
|---|
| 100 |  | 
|---|
| 101 |         InputManager (); | 
|---|
| 102 |         ~InputManager(); | 
|---|
| 103 |  | 
|---|
| 104 |         void initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport = true); | 
|---|
| 105 |  | 
|---|
| 106 |         void reloadInputSystem(bool joyStickSupport = true); | 
|---|
| 107 |  | 
|---|
| 108 |         void clearBuffers(); | 
|---|
| 109 |  | 
|---|
| 110 |         unsigned int  numberOfKeyboards() { return keyboard_ ? 1 : 0; } | 
|---|
| 111 |         unsigned int  numberOfMice()      { return mouse_    ? 1 : 0; } | 
|---|
| 112 |         unsigned int  numberOfJoySticks() { return joySticksSize_; } | 
|---|
| 113 |  | 
|---|
| 114 |         void setWindowExtents(const int width, const int height); | 
|---|
| 115 |         void setKeyDetectorCallback(const std::string& command); | 
|---|
| 116 |  | 
|---|
| 117 |         template <class T> | 
|---|
| 118 |         T* createInputState(const std::string& name, int priority); | 
|---|
| 119 |  | 
|---|
| 120 |         InputState* getState       (const std::string& name); | 
|---|
| 121 |         InputState* getCurrentState(); | 
|---|
| 122 |         ExtendedInputState* getMasterInputState() { return this->stateMaster_; } | 
|---|
| 123 |         bool requestDestroyState   (const std::string& name); | 
|---|
| 124 |         bool requestEnterState     (const std::string& name); | 
|---|
| 125 |         bool requestLeaveState     (const std::string& name); | 
|---|
| 126 |  | 
|---|
| 127 |         void tick(float dt); | 
|---|
| 128 |  | 
|---|
| 129 |         static InputManager& getInstance()    { assert(singletonRef_s); return *singletonRef_s; } | 
|---|
| 130 |         static InputManager* getInstancePtr() { return singletonRef_s; } | 
|---|
| 131 |  | 
|---|
| 132 |         // console commands | 
|---|
| 133 |         static void calibrate(); | 
|---|
| 134 |         static void reload(bool joyStickSupport = true); | 
|---|
| 135 |  | 
|---|
| 136 |     public: // variables | 
|---|
| 137 |         static EmptyHandler                 EMPTY_HANDLER; | 
|---|
| 138 |         static const unsigned int           sliderAxes = 8; | 
|---|
| 139 |  | 
|---|
| 140 |     private: // functions | 
|---|
| 141 |         // don't mess with a Singleton | 
|---|
| 142 |         InputManager (const InputManager&); | 
|---|
| 143 |  | 
|---|
| 144 |         // Intenal methods | 
|---|
| 145 |         void _initialiseKeyboard(); | 
|---|
| 146 |         void _initialiseMouse(); | 
|---|
| 147 |         void _initialiseJoySticks(); | 
|---|
| 148 |         void _configureJoySticks(); | 
|---|
| 149 |  | 
|---|
| 150 |         void _loadCalibration(); | 
|---|
| 151 |         void _startCalibration(); | 
|---|
| 152 |         void _completeCalibration(); | 
|---|
| 153 |         void _evaluateCalibration(); | 
|---|
| 154 |  | 
|---|
| 155 |         void _destroyKeyboard(); | 
|---|
| 156 |         void _destroyMouse(); | 
|---|
| 157 |         void _destroyJoySticks(); | 
|---|
| 158 |         void _destroyState(InputState* state); | 
|---|
| 159 |         void _clearBuffers(); | 
|---|
| 160 |  | 
|---|
| 161 |         void _reload(bool joyStickSupport); | 
|---|
| 162 |  | 
|---|
| 163 |         void _fireAxis(unsigned int iJoyStick, int axis, int value); | 
|---|
| 164 |         unsigned int _getJoystick(const OIS::JoyStickEvent& arg); | 
|---|
| 165 |  | 
|---|
| 166 |         void _updateActiveStates(); | 
|---|
| 167 |         bool _configureInputState(InputState* state, const std::string& name, int priority); | 
|---|
| 168 |  | 
|---|
| 169 |         // input events | 
|---|
| 170 |         bool mousePressed  (const OIS::MouseEvent    &arg, OIS::MouseButtonID id); | 
|---|
| 171 |         bool mouseReleased (const OIS::MouseEvent    &arg, OIS::MouseButtonID id); | 
|---|
| 172 |         bool mouseMoved    (const OIS::MouseEvent    &arg); | 
|---|
| 173 |         bool keyPressed    (const OIS::KeyEvent      &arg); | 
|---|
| 174 |         bool keyReleased   (const OIS::KeyEvent      &arg); | 
|---|
| 175 |         bool buttonPressed (const OIS::JoyStickEvent &arg, int button); | 
|---|
| 176 |         bool buttonReleased(const OIS::JoyStickEvent &arg, int button); | 
|---|
| 177 |         bool axisMoved     (const OIS::JoyStickEvent &arg, int axis); | 
|---|
| 178 |         bool sliderMoved   (const OIS::JoyStickEvent &arg, int id); | 
|---|
| 179 |         bool povMoved      (const OIS::JoyStickEvent &arg, int id); | 
|---|
| 180 |         // don't remove that! Or else add OIS as dependency library to orxonox. | 
|---|
| 181 |         bool vector3Moved  (const OIS::JoyStickEvent &arg, int id) { return true; } | 
|---|
| 182 |  | 
|---|
| 183 |         void setConfigValues(); | 
|---|
| 184 |         void _calibrationFileCallback(); | 
|---|
| 185 |  | 
|---|
| 186 |     private: // variables | 
|---|
| 187 |         OIS::InputManager*                  inputSystem_;          //!< OIS input manager | 
|---|
| 188 |         OIS::Keyboard*                      keyboard_;             //!< OIS mouse | 
|---|
| 189 |         OIS::Mouse*                         mouse_;                //!< OIS keyboard | 
|---|
| 190 |         std::vector<OIS::JoyStick*>         joySticks_;            //!< OIS joy sticks | 
|---|
| 191 |         unsigned int                        joySticksSize_; | 
|---|
| 192 |         std::vector<std::string>            joyStickIDs_;          //!< Execution unique identification strings for the joy sticks | 
|---|
| 193 |         unsigned int                        devicesNum_; | 
|---|
| 194 |         size_t                              windowHnd_;            //!< Render window handle | 
|---|
| 195 |         InputManagerState                   internalState_;        //!< Current internal state | 
|---|
| 196 |  | 
|---|
| 197 |         // some internally handled states and handlers | 
|---|
| 198 |         SimpleInputState*                   stateEmpty_; | 
|---|
| 199 |         ExtendedInputState*                 stateMaster_;          //!< Always active master input state | 
|---|
| 200 |         KeyDetector*                        keyDetector_;          //!< KeyDetector instance | 
|---|
| 201 |         InputBuffer*                        calibratorCallbackBuffer_; | 
|---|
| 202 |  | 
|---|
| 203 |         std::map<std::string, InputState*>  inputStatesByName_; | 
|---|
| 204 |         std::map<int, InputState*>          inputStatesByPriority_; | 
|---|
| 205 |  | 
|---|
| 206 |         std::set<InputState*>               stateEnterRequests_;   //!< Request to enter a new state | 
|---|
| 207 |         std::set<InputState*>               stateLeaveRequests_;   //!< Request to leave a running state | 
|---|
| 208 |         std::set<InputState*>               stateDestroyRequests_; //!< Request to destroy a state | 
|---|
| 209 |  | 
|---|
| 210 |         std::map<int, InputState*>          activeStates_; | 
|---|
| 211 |         std::vector<InputState*>            activeStatesTop_;      //!< Current input states for joy stick events. | 
|---|
| 212 |         std::vector<InputState*>            activeStatesTicked_;   //!< Current input states for joy stick events. | 
|---|
| 213 |  | 
|---|
| 214 |         // joystick calibration | 
|---|
| 215 |         std::vector<std::vector<int> >      joyStickMinValues_; | 
|---|
| 216 |         std::vector<std::vector<int> >      joyStickMaxValues_; | 
|---|
| 217 |         std::vector<std::vector<int> >      joyStickMiddleValues_; | 
|---|
| 218 |         std::vector<ConfigValueContainer*>  calibrationConfigValueContainers_; | 
|---|
| 219 |         std::vector<JoyStickCalibration>    joyStickCalibrations_;  | 
|---|
| 220 |  | 
|---|
| 221 |         unsigned int                        keyboardModifiers_;    //!< Bit mask representing keyboard modifiers. | 
|---|
| 222 |         std::vector<POVStates>              povStates_;            //!< Keeps track of the joy stick POV states. | 
|---|
| 223 |         std::vector<SliderStates>           sliderStates_;         //!< Keeps track of the possibly two slider axes. | 
|---|
| 224 |  | 
|---|
| 225 |         std::vector<Key>                    keysDown_; | 
|---|
| 226 |         std::vector<MouseButtonCode::ByEnum>      mouseButtonsDown_; | 
|---|
| 227 |         std::vector<std::vector<JoyStickButtonCode::ByEnum> >  joyStickButtonsDown_; | 
|---|
| 228 |  | 
|---|
| 229 |         // ConfigValues | 
|---|
| 230 |         std::string                         calibrationFilename_;  //!< Joy stick calibration ini filename | 
|---|
| 231 |  | 
|---|
| 232 |         static InputManager*                singletonRef_s; | 
|---|
| 233 |     }; | 
|---|
| 234 |  | 
|---|
| 235 |     /** | 
|---|
| 236 |     @brief | 
|---|
| 237 |         Creates a new InputState by type, name and priority. | 
|---|
| 238 |          | 
|---|
| 239 |         You will have to use this method because the | 
|---|
| 240 |         c'tors and d'tors are private. | 
|---|
| 241 |     @remarks | 
|---|
| 242 |         The InputManager will take care of the state completely. That also | 
|---|
| 243 |         means it gets deleted when the InputManager is destroyed! | 
|---|
| 244 |     @param name | 
|---|
| 245 |         Name of the InputState when referenced as string | 
|---|
| 246 |     @param priority | 
|---|
| 247 |         Priority matters when multiple states are active. You can specify any | 
|---|
| 248 |         number, but 1 - 99 is preferred (99 means high). | 
|---|
| 249 |     */ | 
|---|
| 250 |     template <class T> | 
|---|
| 251 |     T* InputManager::createInputState(const std::string& name, int priority) | 
|---|
| 252 |     { | 
|---|
| 253 |         T* state = new T; | 
|---|
| 254 |         if (_configureInputState(state, name, priority)) | 
|---|
| 255 |             return state; | 
|---|
| 256 |         else | 
|---|
| 257 |         { | 
|---|
| 258 |             delete state; | 
|---|
| 259 |             return 0; | 
|---|
| 260 |         } | 
|---|
| 261 |     } | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | #endif /* _InputManager_H__ */ | 
|---|