[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" |
---|
[1524] | 45 | #include "core/OrxonoxClass.h" |
---|
[1293] | 46 | #include "InputInterfaces.h" |
---|
[973] | 47 | |
---|
| 48 | namespace orxonox |
---|
| 49 | { |
---|
[1755] | 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 | }; |
---|
[1349] | 60 | |
---|
[1219] | 61 | /** |
---|
[1755] | 62 | @brief |
---|
| 63 | Helper class to realise a vector< {int[4], int[4]} > |
---|
[1219] | 64 | */ |
---|
[1755] | 65 | class SliderStates |
---|
[1219] | 66 | { |
---|
[1755] | 67 | public: |
---|
| 68 | IntVector2 sliderStates[4]; |
---|
[1219] | 69 | }; |
---|
[973] | 70 | |
---|
[1755] | 71 | struct JoyStickCalibration |
---|
| 72 | { |
---|
| 73 | int zeroStates[24]; |
---|
| 74 | float positiveCoeff[24]; |
---|
| 75 | float negativeCoeff[24]; |
---|
| 76 | }; |
---|
[1502] | 77 | |
---|
[1755] | 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>; |
---|
[1066] | 88 | |
---|
[1755] | 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 | }; |
---|
[1219] | 100 | |
---|
[1755] | 101 | InputManager (); |
---|
| 102 | ~InputManager(); |
---|
[1293] | 103 | |
---|
[1755] | 104 | void initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport = true); |
---|
[1219] | 105 | |
---|
[1755] | 106 | void reloadInputSystem(bool joyStickSupport = true); |
---|
[1219] | 107 | |
---|
[1878] | 108 | void clearBuffers(); |
---|
| 109 | |
---|
[1887] | 110 | unsigned int numberOfKeyboards() { return keyboard_ ? 1 : 0; } |
---|
| 111 | unsigned int numberOfMice() { return mouse_ ? 1 : 0; } |
---|
| 112 | unsigned int numberOfJoySticks() { return joySticksSize_; } |
---|
[1502] | 113 | |
---|
[1755] | 114 | void setWindowExtents(const int width, const int height); |
---|
[1887] | 115 | void setKeyDetectorCallback(const std::string& command); |
---|
[1502] | 116 | |
---|
[1755] | 117 | template <class T> |
---|
[1878] | 118 | T* createInputState(const std::string& name, int priority); |
---|
[1524] | 119 | |
---|
[1755] | 120 | InputState* getState (const std::string& name); |
---|
| 121 | InputState* getCurrentState(); |
---|
[1788] | 122 | ExtendedInputState* getMasterInputState() { return this->stateMaster_; } |
---|
[1755] | 123 | bool requestDestroyState (const std::string& name); |
---|
| 124 | bool requestEnterState (const std::string& name); |
---|
| 125 | bool requestLeaveState (const std::string& name); |
---|
[1219] | 126 | |
---|
[1755] | 127 | void tick(float dt); |
---|
[1219] | 128 | |
---|
[1755] | 129 | static InputManager& getInstance() { assert(singletonRef_s); return *singletonRef_s; } |
---|
| 130 | static InputManager* getInstancePtr() { return singletonRef_s; } |
---|
[1219] | 131 | |
---|
[1878] | 132 | // console commands |
---|
[1755] | 133 | static void calibrate(); |
---|
| 134 | static void reload(bool joyStickSupport = true); |
---|
[973] | 135 | |
---|
[1878] | 136 | public: // variables |
---|
| 137 | static EmptyHandler EMPTY_HANDLER; |
---|
| 138 | |
---|
[1755] | 139 | private: // functions |
---|
| 140 | // don't mess with a Singleton |
---|
| 141 | InputManager (const InputManager&); |
---|
[973] | 142 | |
---|
[1755] | 143 | // Intenal methods |
---|
| 144 | void _initialiseKeyboard(); |
---|
| 145 | void _initialiseMouse(); |
---|
| 146 | void _initialiseJoySticks(); |
---|
[1887] | 147 | void _configureNumberOfJoySticks(); |
---|
[973] | 148 | |
---|
[1755] | 149 | void _destroyKeyboard(); |
---|
| 150 | void _destroyMouse(); |
---|
| 151 | void _destroyJoySticks(); |
---|
| 152 | void _destroyState(InputState* state); |
---|
| 153 | void _clearBuffers(); |
---|
[1349] | 154 | |
---|
[1755] | 155 | void _reload(bool joyStickSupport); |
---|
[1502] | 156 | |
---|
[1755] | 157 | void _completeCalibration(); |
---|
[1502] | 158 | |
---|
[1755] | 159 | void _fireAxis(unsigned int iJoyStick, int axis, int value); |
---|
| 160 | unsigned int _getJoystick(const OIS::JoyStickEvent& arg); |
---|
[1502] | 161 | |
---|
[1755] | 162 | void _updateActiveStates(); |
---|
| 163 | bool _configureInputState(InputState* state, const std::string& name, int priority); |
---|
[1219] | 164 | |
---|
[1755] | 165 | // input events |
---|
| 166 | bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
| 167 | bool mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
| 168 | bool mouseMoved (const OIS::MouseEvent &arg); |
---|
| 169 | bool keyPressed (const OIS::KeyEvent &arg); |
---|
| 170 | bool keyReleased (const OIS::KeyEvent &arg); |
---|
| 171 | bool buttonPressed (const OIS::JoyStickEvent &arg, int button); |
---|
| 172 | bool buttonReleased(const OIS::JoyStickEvent &arg, int button); |
---|
| 173 | bool axisMoved (const OIS::JoyStickEvent &arg, int axis); |
---|
| 174 | bool sliderMoved (const OIS::JoyStickEvent &arg, int id); |
---|
| 175 | bool povMoved (const OIS::JoyStickEvent &arg, int id); |
---|
| 176 | // don't remove that! Or else add OIS as dependency library to orxonox. |
---|
| 177 | bool vector3Moved (const OIS::JoyStickEvent &arg, int id) { return true; } |
---|
[1219] | 178 | |
---|
[1755] | 179 | void setConfigValues(); |
---|
[1219] | 180 | |
---|
[1755] | 181 | private: // variables |
---|
| 182 | OIS::InputManager* inputSystem_; //!< OIS input manager |
---|
| 183 | OIS::Keyboard* keyboard_; //!< OIS mouse |
---|
| 184 | OIS::Mouse* mouse_; //!< OIS keyboard |
---|
| 185 | std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks |
---|
| 186 | unsigned int joySticksSize_; |
---|
| 187 | unsigned int devicesNum_; |
---|
| 188 | size_t windowHnd_; //!< Render window handle |
---|
| 189 | InputManagerState internalState_; //!< Current internal state |
---|
[1219] | 190 | |
---|
[1878] | 191 | // some internally handled states and handlers |
---|
[1755] | 192 | SimpleInputState* stateEmpty_; |
---|
[1788] | 193 | ExtendedInputState* stateMaster_; //!< Always active master input state |
---|
[1881] | 194 | KeyDetector* keyDetector_; //!< KeyDetector instance |
---|
| 195 | InputBuffer* calibratorCallbackBuffer_; |
---|
[1502] | 196 | |
---|
[1755] | 197 | std::map<std::string, InputState*> inputStatesByName_; |
---|
| 198 | std::map<int, InputState*> inputStatesByPriority_; |
---|
[1219] | 199 | |
---|
[1755] | 200 | std::set<InputState*> stateEnterRequests_; //!< Request to enter a new state |
---|
| 201 | std::set<InputState*> stateLeaveRequests_; //!< Request to leave a running state |
---|
| 202 | std::set<InputState*> stateDestroyRequests_; //!< Request to destroy a state |
---|
[1502] | 203 | |
---|
[1755] | 204 | std::map<int, InputState*> activeStates_; |
---|
| 205 | std::vector<InputState*> activeStatesTop_; //!< Current input states for joy stick events. |
---|
| 206 | std::vector<InputState*> activeStatesTicked_; //!< Current input states for joy stick events. |
---|
[1349] | 207 | |
---|
[1755] | 208 | // joystick calibration |
---|
| 209 | //std::vector<int> marginalsMaxConfig_; |
---|
| 210 | //std::vector<int> marginalsMinConfig_; |
---|
| 211 | int marginalsMax_[24]; |
---|
| 212 | int marginalsMin_[24]; |
---|
| 213 | bool bCalibrated_; |
---|
| 214 | bool bCalibrating_; |
---|
[1219] | 215 | |
---|
[1755] | 216 | unsigned int keyboardModifiers_; //!< Bit mask representing keyboard modifiers. |
---|
| 217 | std::vector<POVStates> povStates_; //!< Keeps track of the joy stick POV states. |
---|
| 218 | std::vector<SliderStates> sliderStates_; //!< Keeps track of the possibly two slider axes. |
---|
| 219 | std::vector<JoyStickCalibration> joySticksCalibration_; |
---|
[1219] | 220 | |
---|
[1755] | 221 | std::vector<Key> keysDown_; |
---|
[1887] | 222 | std::vector<MouseButtonCode::ByEnum> mouseButtonsDown_; |
---|
| 223 | std::vector<std::vector<JoyStickButtonCode::ByEnum> > joyStickButtonsDown_; |
---|
[1219] | 224 | |
---|
[1755] | 225 | static std::string bindingCommmandString_s; |
---|
| 226 | static InputManager* singletonRef_s; |
---|
| 227 | }; |
---|
[1878] | 228 | |
---|
| 229 | /** |
---|
| 230 | @brief |
---|
| 231 | Creates a new InputState by type, name and priority. |
---|
| 232 | |
---|
| 233 | You will have to use this method because the |
---|
| 234 | c'tors and d'tors are private. |
---|
| 235 | @remarks |
---|
| 236 | The InputManager will take care of the state completely. That also |
---|
| 237 | means it gets deleted when the InputManager is destroyed! |
---|
| 238 | @param name |
---|
| 239 | Name of the InputState when referenced as string |
---|
| 240 | @param priority |
---|
| 241 | Priority matters when multiple states are active. You can specify any |
---|
| 242 | number, but 1 - 99 is preferred (99 means high). |
---|
| 243 | */ |
---|
| 244 | template <class T> |
---|
| 245 | T* InputManager::createInputState(const std::string& name, int priority) |
---|
| 246 | { |
---|
| 247 | T* state = new T; |
---|
| 248 | if (_configureInputState(state, name, priority)) |
---|
| 249 | return state; |
---|
| 250 | else |
---|
| 251 | { |
---|
| 252 | delete state; |
---|
| 253 | return 0; |
---|
| 254 | } |
---|
| 255 | } |
---|
[973] | 256 | } |
---|
| 257 | |
---|
| 258 | #endif /* _InputManager_H__ */ |
---|