[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 | |
---|
[2896] | 140 | void update(const Clock& time); |
---|
[1219] | 141 | |
---|
[1755] | 142 | static InputManager& getInstance() { assert(singletonRef_s); return *singletonRef_s; } |
---|
| 143 | static InputManager* getInstancePtr() { return singletonRef_s; } |
---|
[1219] | 144 | |
---|
[1878] | 145 | // console commands |
---|
[1755] | 146 | static void calibrate(); |
---|
| 147 | static void reload(bool joyStickSupport = true); |
---|
[973] | 148 | |
---|
[1878] | 149 | public: // variables |
---|
| 150 | static EmptyHandler EMPTY_HANDLER; |
---|
[2662] | 151 | static const unsigned int sliderAxes = 8; |
---|
[1878] | 152 | |
---|
[1755] | 153 | private: // functions |
---|
| 154 | // don't mess with a Singleton |
---|
| 155 | InputManager (const InputManager&); |
---|
[973] | 156 | |
---|
[1755] | 157 | // Intenal methods |
---|
| 158 | void _initialiseKeyboard(); |
---|
| 159 | void _initialiseMouse(); |
---|
| 160 | void _initialiseJoySticks(); |
---|
[2662] | 161 | void _configureJoySticks(); |
---|
[973] | 162 | |
---|
[2662] | 163 | void _loadCalibration(); |
---|
| 164 | void _startCalibration(); |
---|
| 165 | void _completeCalibration(); |
---|
| 166 | void _evaluateCalibration(); |
---|
| 167 | |
---|
[1755] | 168 | void _destroyKeyboard(); |
---|
| 169 | void _destroyMouse(); |
---|
| 170 | void _destroyJoySticks(); |
---|
| 171 | void _destroyState(InputState* state); |
---|
| 172 | void _clearBuffers(); |
---|
[1349] | 173 | |
---|
[1755] | 174 | void _reload(bool joyStickSupport); |
---|
[1502] | 175 | |
---|
[1755] | 176 | void _fireAxis(unsigned int iJoyStick, int axis, int value); |
---|
| 177 | unsigned int _getJoystick(const OIS::JoyStickEvent& arg); |
---|
[1502] | 178 | |
---|
[1755] | 179 | void _updateActiveStates(); |
---|
[2896] | 180 | bool _configureInputState(InputState* state, const std::string& name, bool bAlwaysGetsInput, bool bTransparent, int priority); |
---|
[1219] | 181 | |
---|
[1755] | 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; } |
---|
[1219] | 195 | |
---|
[1755] | 196 | void setConfigValues(); |
---|
[2662] | 197 | void _calibrationFileCallback(); |
---|
[1219] | 198 | |
---|
[1755] | 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_; |
---|
[2662] | 205 | std::vector<std::string> joyStickIDs_; //!< Execution unique identification strings for the joy sticks |
---|
[1755] | 206 | unsigned int devicesNum_; |
---|
| 207 | size_t windowHnd_; //!< Render window handle |
---|
| 208 | InputManagerState internalState_; //!< Current internal state |
---|
[1219] | 209 | |
---|
[1878] | 210 | // some internally handled states and handlers |
---|
[1755] | 211 | SimpleInputState* stateEmpty_; |
---|
[2662] | 212 | KeyDetector* keyDetector_; //!< KeyDetector instance |
---|
[1881] | 213 | InputBuffer* calibratorCallbackBuffer_; |
---|
[1502] | 214 | |
---|
[1755] | 215 | std::map<std::string, InputState*> inputStatesByName_; |
---|
[1219] | 216 | |
---|
[1755] | 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 |
---|
[1502] | 220 | |
---|
[1755] | 221 | std::map<int, InputState*> activeStates_; |
---|
[2896] | 222 | std::vector<std::vector<InputState*> > activeStatesTriggered_; |
---|
| 223 | std::vector<InputState*> activeStatesTicked_; |
---|
[1349] | 224 | |
---|
[1755] | 225 | // joystick calibration |
---|
[2662] | 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_; |
---|
[1219] | 231 | |
---|
[1755] | 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. |
---|
[1219] | 235 | |
---|
[1755] | 236 | std::vector<Key> keysDown_; |
---|
[1887] | 237 | std::vector<MouseButtonCode::ByEnum> mouseButtonsDown_; |
---|
| 238 | std::vector<std::vector<JoyStickButtonCode::ByEnum> > joyStickButtonsDown_; |
---|
[1219] | 239 | |
---|
[2662] | 240 | // ConfigValues |
---|
| 241 | std::string calibrationFilename_; //!< Joy stick calibration ini filename |
---|
| 242 | |
---|
[1755] | 243 | static InputManager* singletonRef_s; |
---|
| 244 | }; |
---|
[1878] | 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> |
---|
[2896] | 262 | T* InputManager::createInputState(const std::string& name, bool bAlwaysGetsInput, bool bTransparent, InputStatePriority priority) |
---|
[1878] | 263 | { |
---|
| 264 | T* state = new T; |
---|
[2896] | 265 | if (_configureInputState(state, name, bAlwaysGetsInput, bTransparent, priority)) |
---|
[1878] | 266 | return state; |
---|
| 267 | else |
---|
| 268 | { |
---|
| 269 | delete state; |
---|
| 270 | return 0; |
---|
| 271 | } |
---|
| 272 | } |
---|
[973] | 273 | } |
---|
| 274 | |
---|
| 275 | #endif /* _InputManager_H__ */ |
---|