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