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