[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 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Implementation of a little Input handler that distributes everything |
---|
| 32 | coming from OIS. |
---|
| 33 | */ |
---|
[973] | 34 | |
---|
| 35 | #ifndef _InputManager_H__ |
---|
| 36 | #define _InputManager_H__ |
---|
| 37 | |
---|
[1519] | 38 | #include "core/CorePrereqs.h" |
---|
[1062] | 39 | |
---|
[1219] | 40 | #include <map> |
---|
[1293] | 41 | #include <vector> |
---|
[973] | 42 | |
---|
[1219] | 43 | #include "ois/OIS.h" |
---|
[1349] | 44 | #include "util/Math.h" |
---|
[1524] | 45 | #include "core/OrxonoxClass.h" |
---|
[1293] | 46 | #include "InputInterfaces.h" |
---|
[973] | 47 | |
---|
| 48 | namespace orxonox |
---|
| 49 | { |
---|
| 50 | /** |
---|
[1349] | 51 | * Helper class to realise a vector<int[4]> |
---|
| 52 | */ |
---|
| 53 | class POVStates |
---|
| 54 | { |
---|
| 55 | public: |
---|
| 56 | int operator[](unsigned int index) { return povStates[index]; } |
---|
| 57 | int povStates[4]; |
---|
| 58 | }; |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Helper class to realise a vector< {int[4], int[4]} > |
---|
| 62 | */ |
---|
| 63 | class SliderStates |
---|
| 64 | { |
---|
| 65 | public: |
---|
| 66 | IntVector2 sliderStates[4]; |
---|
| 67 | }; |
---|
| 68 | |
---|
| 69 | /** |
---|
[1502] | 70 | * Struct for storing a custom input state |
---|
| 71 | */ |
---|
| 72 | struct StoredState |
---|
| 73 | { |
---|
| 74 | std::vector<KeyHandler*> activeKeyHandlers_; |
---|
| 75 | std::vector<MouseHandler*> activeMouseHandlers_; |
---|
| 76 | std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_; |
---|
| 77 | std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_; |
---|
| 78 | }; |
---|
| 79 | |
---|
| 80 | struct JoyStickCalibration |
---|
| 81 | { |
---|
| 82 | int zeroStates[24]; |
---|
| 83 | float positiveCoeff[24]; |
---|
| 84 | float negativeCoeff[24]; |
---|
| 85 | }; |
---|
| 86 | |
---|
| 87 | /** |
---|
[973] | 88 | @brief Captures and distributes mouse and keyboard input. |
---|
| 89 | */ |
---|
| 90 | class _CoreExport InputManager |
---|
[1524] | 91 | : public OrxonoxClass, |
---|
[1219] | 92 | public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener |
---|
[973] | 93 | { |
---|
[1219] | 94 | public: // enumerations |
---|
| 95 | /** |
---|
| 96 | @brief Designates the way input is handled and redirected. |
---|
| 97 | */ |
---|
| 98 | enum InputState |
---|
| 99 | { |
---|
[1502] | 100 | IS_UNINIT, //!< InputManager has not yet been initialised. |
---|
| 101 | IS_NONE, //!< Input is discarded. |
---|
| 102 | IS_NORMAL, //!< Normal play state. Key and button bindings are active. |
---|
| 103 | IS_GUI, //!< All OIS input events are passed to CEGUI. |
---|
| 104 | IS_CONSOLE, //!< Keyboard input is redirected to the InputBuffer. |
---|
| 105 | IS_DETECT, //!< All the input additionally goes to the KeyDetector |
---|
| 106 | IS_NODETECT, //!< remove KeyDetector |
---|
| 107 | IS_NOCALIBRATE, |
---|
| 108 | IS_CALIBRATE, |
---|
| 109 | IS_CUSTOM //!< Any possible configuration. |
---|
[1219] | 110 | }; |
---|
[973] | 111 | |
---|
[1502] | 112 | public: // member functions |
---|
| 113 | void setConfigValues(); |
---|
| 114 | |
---|
[1219] | 115 | public: // static functions |
---|
[1293] | 116 | static bool initialise(const size_t windowHnd, int windowWidth, int windowHeight, |
---|
| 117 | bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false); |
---|
[1219] | 118 | static bool initialiseKeyboard(); |
---|
| 119 | static bool initialiseMouse(); |
---|
| 120 | static bool initialiseJoySticks(); |
---|
| 121 | static int numberOfKeyboards(); |
---|
| 122 | static int numberOfMice(); |
---|
| 123 | static int numberOfJoySticks(); |
---|
[1066] | 124 | |
---|
[1219] | 125 | static void destroy(); |
---|
| 126 | static void destroyKeyboard(); |
---|
| 127 | static void destroyMouse(); |
---|
| 128 | static void destroyJoySticks(); |
---|
| 129 | |
---|
[1502] | 130 | //static bool isModifierDown(KeyboardModifier::Enum modifier); |
---|
| 131 | //static bool isKeyDown(KeyCode::Enum key); |
---|
[1349] | 132 | //static const MouseState getMouseState(); |
---|
| 133 | //static const JoyStickState getJoyStickState(unsigned int ID); |
---|
[1293] | 134 | |
---|
[1219] | 135 | static void setWindowExtents(const int width, const int height); |
---|
| 136 | |
---|
| 137 | static void setInputState(const InputState state); |
---|
| 138 | static InputState getInputState(); |
---|
| 139 | |
---|
[1502] | 140 | static void storeKeyStroke(const std::string& name); |
---|
| 141 | static void keyBind(const std::string& command); |
---|
| 142 | |
---|
| 143 | static void calibrate(); |
---|
| 144 | |
---|
[1524] | 145 | static void tick(float dt); |
---|
| 146 | |
---|
[1219] | 147 | static bool addKeyHandler (KeyHandler* handler, const std::string& name); |
---|
| 148 | static bool removeKeyHandler (const std::string& name); |
---|
| 149 | static KeyHandler* getKeyHandler (const std::string& name); |
---|
| 150 | static bool enableKeyHandler (const std::string& name); |
---|
| 151 | static bool disableKeyHandler (const std::string& name); |
---|
| 152 | static bool isKeyHandlerActive (const std::string& name); |
---|
| 153 | |
---|
| 154 | static bool addMouseHandler (MouseHandler* handler, const std::string& name); |
---|
| 155 | static bool removeMouseHandler (const std::string& name); |
---|
| 156 | static MouseHandler* getMouseHandler (const std::string& name); |
---|
| 157 | static bool enableMouseHandler (const std::string& name); |
---|
| 158 | static bool disableMouseHandler (const std::string& name); |
---|
| 159 | static bool isMouseHandlerActive (const std::string& name); |
---|
| 160 | |
---|
| 161 | static bool addJoyStickHandler (JoyStickHandler* handler, const std::string& name); |
---|
| 162 | static bool removeJoyStickHandler (const std::string& name); |
---|
| 163 | static JoyStickHandler* getJoyStickHandler(const std::string& name); |
---|
[1293] | 164 | static bool enableJoyStickHandler (const std::string& name, unsigned int id); |
---|
| 165 | static bool disableJoyStickHandler (const std::string& name, unsigned int id); |
---|
| 166 | static bool isJoyStickHandlerActive (const std::string& name, unsigned int id); |
---|
[1219] | 167 | |
---|
| 168 | private: // functions |
---|
[973] | 169 | // don't mess with a Singleton |
---|
| 170 | InputManager (); |
---|
| 171 | InputManager (const InputManager&); |
---|
| 172 | ~InputManager(); |
---|
| 173 | |
---|
[1219] | 174 | // Intenal methods |
---|
[1293] | 175 | bool _initialise(const size_t, int, int, bool, bool, bool); |
---|
[1219] | 176 | bool _initialiseKeyboard(); |
---|
| 177 | bool _initialiseMouse(); |
---|
| 178 | bool _initialiseJoySticks(); |
---|
[973] | 179 | |
---|
[1219] | 180 | void _destroy(); |
---|
| 181 | void _destroyKeyboard(); |
---|
| 182 | void _destroyMouse(); |
---|
| 183 | void _destroyJoySticks(); |
---|
[973] | 184 | |
---|
[1349] | 185 | void _updateTickables(); |
---|
| 186 | |
---|
[1502] | 187 | void _saveState(); |
---|
| 188 | void _restoreState(); |
---|
| 189 | |
---|
| 190 | void _completeCalibration(); |
---|
| 191 | |
---|
| 192 | void _fireAxis(unsigned int iJoyStick, int axis, int value); |
---|
| 193 | unsigned int _getJoystick(const OIS::JoyStickEvent& arg); |
---|
| 194 | |
---|
[1524] | 195 | void _tick(float dt); |
---|
[1219] | 196 | |
---|
| 197 | // input events |
---|
[1293] | 198 | bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
| 199 | bool mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
[1219] | 200 | bool mouseMoved (const OIS::MouseEvent &arg); |
---|
[1293] | 201 | bool keyPressed (const OIS::KeyEvent &arg); |
---|
| 202 | bool keyReleased (const OIS::KeyEvent &arg); |
---|
[1502] | 203 | bool buttonPressed (const OIS::JoyStickEvent &arg, int button); |
---|
| 204 | bool buttonReleased(const OIS::JoyStickEvent &arg, int button); |
---|
| 205 | bool axisMoved (const OIS::JoyStickEvent &arg, int axis); |
---|
| 206 | bool sliderMoved (const OIS::JoyStickEvent &arg, int id); |
---|
| 207 | bool povMoved (const OIS::JoyStickEvent &arg, int id); |
---|
| 208 | //bool vector3Moved (const OIS::JoyStickEvent &arg, int id); |
---|
[1219] | 209 | |
---|
| 210 | static InputManager& _getSingleton(); |
---|
| 211 | static InputManager* _getSingletonPtr() { return &_getSingleton(); } |
---|
| 212 | |
---|
| 213 | private: // variables |
---|
[1502] | 214 | OIS::InputManager* inputSystem_; //!< OIS input manager |
---|
| 215 | OIS::Keyboard* keyboard_; //!< OIS mouse |
---|
| 216 | OIS::Mouse* mouse_; //!< OIS keyboard |
---|
| 217 | std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks |
---|
[1293] | 218 | unsigned int joySticksSize_; |
---|
[1219] | 219 | |
---|
[1502] | 220 | KeyBinder* keyBinder_; //!< KeyBinder instance |
---|
| 221 | KeyDetector* keyDetector_; //!< KeyDetector instance |
---|
| 222 | InputBuffer* buffer_; //!< InputBuffer instance |
---|
| 223 | CalibratorCallback* calibratorCallback_; |
---|
| 224 | |
---|
[1219] | 225 | InputState state_; |
---|
| 226 | InputState stateRequest_; |
---|
[1502] | 227 | InputState savedState_; |
---|
[1293] | 228 | unsigned int keyboardModifiers_; |
---|
[1502] | 229 | StoredState savedHandlers_; |
---|
[1219] | 230 | |
---|
[1502] | 231 | // joystick calibration |
---|
| 232 | //std::vector<int> marginalsMaxConfig_; |
---|
| 233 | //std::vector<int> marginalsMinConfig_; |
---|
| 234 | int marginalsMax_[24]; |
---|
| 235 | int marginalsMin_[24]; |
---|
| 236 | bool bCalibrated_; |
---|
| 237 | |
---|
[1349] | 238 | //! Keeps track of the joy stick POV states |
---|
| 239 | std::vector<POVStates> povStates_; |
---|
| 240 | //! Keeps track of the possibly two slider axes |
---|
| 241 | std::vector<SliderStates> sliderStates_; |
---|
[1502] | 242 | std::vector<JoyStickCalibration> joySticksCalibration_; |
---|
[1349] | 243 | |
---|
[1219] | 244 | std::map<std::string, KeyHandler*> keyHandlers_; |
---|
| 245 | std::map<std::string, MouseHandler*> mouseHandlers_; |
---|
| 246 | std::map<std::string, JoyStickHandler*> joyStickHandlers_; |
---|
| 247 | |
---|
| 248 | std::vector<KeyHandler*> activeKeyHandlers_; |
---|
| 249 | std::vector<MouseHandler*> activeMouseHandlers_; |
---|
[1293] | 250 | std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_; |
---|
[1502] | 251 | std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_; |
---|
[1219] | 252 | |
---|
[1293] | 253 | std::vector<Key> keysDown_; |
---|
| 254 | std::vector<MouseButton::Enum> mouseButtonsDown_; |
---|
| 255 | std::vector<std::vector<int> > joyStickButtonsDown_; |
---|
[1219] | 256 | |
---|
[1502] | 257 | static std::string bindingCommmandString_s; |
---|
[973] | 258 | }; |
---|
[1293] | 259 | |
---|
[973] | 260 | } |
---|
| 261 | |
---|
| 262 | #endif /* _InputManager_H__ */ |
---|