[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 | |
---|
[918] | 29 | /** |
---|
[1755] | 30 | @file |
---|
| 31 | @brief |
---|
| 32 | Implementation of a little Input handler that distributes everything |
---|
| 33 | coming from OIS. |
---|
| 34 | */ |
---|
[973] | 35 | |
---|
| 36 | #ifndef _InputManager_H__ |
---|
| 37 | #define _InputManager_H__ |
---|
| 38 | |
---|
[1519] | 39 | #include "core/CorePrereqs.h" |
---|
[1062] | 40 | |
---|
[1219] | 41 | #include <map> |
---|
[1293] | 42 | #include <vector> |
---|
[1755] | 43 | #include <stack> |
---|
[1349] | 44 | #include "util/Math.h" |
---|
[2896] | 45 | #include "util/OrxEnum.h" |
---|
[1524] | 46 | #include "core/OrxonoxClass.h" |
---|
[1293] | 47 | #include "InputInterfaces.h" |
---|
[973] | 48 | |
---|
| 49 | namespace orxonox |
---|
| 50 | { |
---|
[1755] | 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 | }; |
---|
[1349] | 61 | |
---|
[1219] | 62 | /** |
---|
[1755] | 63 | @brief |
---|
| 64 | Helper class to realise a vector< {int[4], int[4]} > |
---|
[1219] | 65 | */ |
---|
[1755] | 66 | class SliderStates |
---|
[1219] | 67 | { |
---|
[1755] | 68 | public: |
---|
| 69 | IntVector2 sliderStates[4]; |
---|
[1219] | 70 | }; |
---|
[973] | 71 | |
---|
[1755] | 72 | struct JoyStickCalibration |
---|
| 73 | { |
---|
[2662] | 74 | int middleValue[24]; |
---|
[1755] | 75 | float positiveCoeff[24]; |
---|
| 76 | float negativeCoeff[24]; |
---|
| 77 | }; |
---|
[1502] | 78 | |
---|
[2896] | 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 | |
---|
[1755] | 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>; |
---|
[1066] | 102 | |
---|
[1755] | 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 | }; |
---|
[1219] | 114 | |
---|
[1755] | 115 | InputManager (); |
---|
| 116 | ~InputManager(); |
---|
[1293] | 117 | |
---|
[1755] | 118 | void initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport = true); |
---|
[1219] | 119 | |
---|
[1755] | 120 | void reloadInputSystem(bool joyStickSupport = true); |
---|
[1219] | 121 | |
---|
[1878] | 122 | void clearBuffers(); |
---|
| 123 | |
---|
[1887] | 124 | unsigned int numberOfKeyboards() { return keyboard_ ? 1 : 0; } |
---|
| 125 | unsigned int numberOfMice() { return mouse_ ? 1 : 0; } |
---|
| 126 | unsigned int numberOfJoySticks() { return joySticksSize_; } |
---|
[1502] | 127 | |
---|
[1755] | 128 | void setWindowExtents(const int width, const int height); |
---|
[1887] | 129 | void setKeyDetectorCallback(const std::string& command); |
---|
[1502] | 130 | |
---|
[1755] | 131 | template <class T> |
---|
[2896] | 132 | T* createInputState(const std::string& name, bool bAlwaysGetsInput = false, bool bTransparent = false, InputStatePriority priority = InputStatePriority::Dynamic); |
---|
[1524] | 133 | |
---|
[1755] | 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); |
---|
[1219] | 139 | |
---|
[3084] | 140 | #ifdef ORXONOX_PLATFORM_LINUX |
---|
| 141 | // HACK! |
---|
| 142 | static void grabMouse(); |
---|
| 143 | static void ungrabMouse(); |
---|
| 144 | #endif |
---|
| 145 | |
---|
[2896] | 146 | void update(const Clock& time); |
---|
[1219] | 147 | |
---|
[1755] | 148 | static InputManager& getInstance() { assert(singletonRef_s); return *singletonRef_s; } |
---|
| 149 | static InputManager* getInstancePtr() { return singletonRef_s; } |
---|
[1219] | 150 | |
---|
[1878] | 151 | // console commands |
---|
[1755] | 152 | static void calibrate(); |
---|
| 153 | static void reload(bool joyStickSupport = true); |
---|
[973] | 154 | |
---|
[1878] | 155 | public: // variables |
---|
| 156 | static EmptyHandler EMPTY_HANDLER; |
---|
[2662] | 157 | static const unsigned int sliderAxes = 8; |
---|
[1878] | 158 | |
---|
[1755] | 159 | private: // functions |
---|
| 160 | // don't mess with a Singleton |
---|
| 161 | InputManager (const InputManager&); |
---|
[973] | 162 | |
---|
[1755] | 163 | // Intenal methods |
---|
| 164 | void _initialiseKeyboard(); |
---|
| 165 | void _initialiseMouse(); |
---|
| 166 | void _initialiseJoySticks(); |
---|
[2662] | 167 | void _configureJoySticks(); |
---|
[973] | 168 | |
---|
[2662] | 169 | void _loadCalibration(); |
---|
| 170 | void _startCalibration(); |
---|
| 171 | void _completeCalibration(); |
---|
| 172 | void _evaluateCalibration(); |
---|
| 173 | |
---|
[1755] | 174 | void _destroyKeyboard(); |
---|
| 175 | void _destroyMouse(); |
---|
| 176 | void _destroyJoySticks(); |
---|
| 177 | void _destroyState(InputState* state); |
---|
| 178 | void _clearBuffers(); |
---|
[1349] | 179 | |
---|
[1755] | 180 | void _reload(bool joyStickSupport); |
---|
[1502] | 181 | |
---|
[1755] | 182 | void _fireAxis(unsigned int iJoyStick, int axis, int value); |
---|
| 183 | unsigned int _getJoystick(const OIS::JoyStickEvent& arg); |
---|
[1502] | 184 | |
---|
[1755] | 185 | void _updateActiveStates(); |
---|
[2896] | 186 | bool _configureInputState(InputState* state, const std::string& name, bool bAlwaysGetsInput, bool bTransparent, int priority); |
---|
[1219] | 187 | |
---|
[1755] | 188 | // input events |
---|
| 189 | bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
| 190 | bool mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
| 191 | bool mouseMoved (const OIS::MouseEvent &arg); |
---|
| 192 | bool keyPressed (const OIS::KeyEvent &arg); |
---|
| 193 | bool keyReleased (const OIS::KeyEvent &arg); |
---|
| 194 | bool buttonPressed (const OIS::JoyStickEvent &arg, int button); |
---|
| 195 | bool buttonReleased(const OIS::JoyStickEvent &arg, int button); |
---|
| 196 | bool axisMoved (const OIS::JoyStickEvent &arg, int axis); |
---|
| 197 | bool sliderMoved (const OIS::JoyStickEvent &arg, int id); |
---|
| 198 | bool povMoved (const OIS::JoyStickEvent &arg, int id); |
---|
| 199 | // don't remove that! Or else add OIS as dependency library to orxonox. |
---|
| 200 | bool vector3Moved (const OIS::JoyStickEvent &arg, int id) { return true; } |
---|
[1219] | 201 | |
---|
[1755] | 202 | void setConfigValues(); |
---|
[2662] | 203 | void _calibrationFileCallback(); |
---|
[1219] | 204 | |
---|
[1755] | 205 | private: // variables |
---|
| 206 | OIS::InputManager* inputSystem_; //!< OIS input manager |
---|
| 207 | OIS::Keyboard* keyboard_; //!< OIS mouse |
---|
| 208 | OIS::Mouse* mouse_; //!< OIS keyboard |
---|
| 209 | std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks |
---|
| 210 | unsigned int joySticksSize_; |
---|
[2662] | 211 | std::vector<std::string> joyStickIDs_; //!< Execution unique identification strings for the joy sticks |
---|
[1755] | 212 | unsigned int devicesNum_; |
---|
| 213 | size_t windowHnd_; //!< Render window handle |
---|
| 214 | InputManagerState internalState_; //!< Current internal state |
---|
[1219] | 215 | |
---|
[1878] | 216 | // some internally handled states and handlers |
---|
[1755] | 217 | SimpleInputState* stateEmpty_; |
---|
[2662] | 218 | KeyDetector* keyDetector_; //!< KeyDetector instance |
---|
[1881] | 219 | InputBuffer* calibratorCallbackBuffer_; |
---|
[1502] | 220 | |
---|
[1755] | 221 | std::map<std::string, InputState*> inputStatesByName_; |
---|
[1219] | 222 | |
---|
[1755] | 223 | std::set<InputState*> stateEnterRequests_; //!< Request to enter a new state |
---|
| 224 | std::set<InputState*> stateLeaveRequests_; //!< Request to leave a running state |
---|
| 225 | std::set<InputState*> stateDestroyRequests_; //!< Request to destroy a state |
---|
[1502] | 226 | |
---|
[1755] | 227 | std::map<int, InputState*> activeStates_; |
---|
[2896] | 228 | std::vector<std::vector<InputState*> > activeStatesTriggered_; |
---|
| 229 | std::vector<InputState*> activeStatesTicked_; |
---|
[1349] | 230 | |
---|
[1755] | 231 | // joystick calibration |
---|
[2662] | 232 | std::vector<std::vector<int> > joyStickMinValues_; |
---|
| 233 | std::vector<std::vector<int> > joyStickMaxValues_; |
---|
| 234 | std::vector<std::vector<int> > joyStickMiddleValues_; |
---|
| 235 | std::vector<ConfigValueContainer*> calibrationConfigValueContainers_; |
---|
| 236 | std::vector<JoyStickCalibration> joyStickCalibrations_; |
---|
[1219] | 237 | |
---|
[1755] | 238 | unsigned int keyboardModifiers_; //!< Bit mask representing keyboard modifiers. |
---|
| 239 | std::vector<POVStates> povStates_; //!< Keeps track of the joy stick POV states. |
---|
| 240 | std::vector<SliderStates> sliderStates_; //!< Keeps track of the possibly two slider axes. |
---|
[1219] | 241 | |
---|
[1755] | 242 | std::vector<Key> keysDown_; |
---|
[1887] | 243 | std::vector<MouseButtonCode::ByEnum> mouseButtonsDown_; |
---|
| 244 | std::vector<std::vector<JoyStickButtonCode::ByEnum> > joyStickButtonsDown_; |
---|
[1219] | 245 | |
---|
[2662] | 246 | // ConfigValues |
---|
| 247 | std::string calibrationFilename_; //!< Joy stick calibration ini filename |
---|
| 248 | |
---|
[1755] | 249 | static InputManager* singletonRef_s; |
---|
| 250 | }; |
---|
[1878] | 251 | |
---|
| 252 | /** |
---|
| 253 | @brief |
---|
| 254 | Creates a new InputState by type, name and priority. |
---|
| 255 | |
---|
| 256 | You will have to use this method because the |
---|
| 257 | c'tors and d'tors are private. |
---|
| 258 | @remarks |
---|
| 259 | The InputManager will take care of the state completely. That also |
---|
| 260 | means it gets deleted when the InputManager is destroyed! |
---|
| 261 | @param name |
---|
| 262 | Name of the InputState when referenced as string |
---|
| 263 | @param priority |
---|
| 264 | Priority matters when multiple states are active. You can specify any |
---|
| 265 | number, but 1 - 99 is preferred (99 means high). |
---|
| 266 | */ |
---|
| 267 | template <class T> |
---|
[2896] | 268 | T* InputManager::createInputState(const std::string& name, bool bAlwaysGetsInput, bool bTransparent, InputStatePriority priority) |
---|
[1878] | 269 | { |
---|
| 270 | T* state = new T; |
---|
[2896] | 271 | if (_configureInputState(state, name, bAlwaysGetsInput, bTransparent, priority)) |
---|
[1878] | 272 | return state; |
---|
| 273 | else |
---|
| 274 | { |
---|
| 275 | delete state; |
---|
| 276 | return 0; |
---|
| 277 | } |
---|
| 278 | } |
---|
[973] | 279 | } |
---|
| 280 | |
---|
| 281 | #endif /* _InputManager_H__ */ |
---|