Changeset 1755 for code/trunk/src/core/input/InputManager.h
- Timestamp:
- Sep 10, 2008, 1:37:36 AM (17 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/gui (added) merged: 1636,1638,1640-1647,1649-1654,1656,1659-1665,1670,1672-1674,1686,1688-1692,1694-1697,1704 /code/branches/input (added) merged: 1629-1630
- Property svn:mergeinfo changed
-
code/trunk/src/core/input/InputManager.h
r1555 r1755 28 28 29 29 /** 30 @file 31 @brief Implementation of a little Input handler that distributes everything 32 coming from OIS. 33 */ 30 @file 31 @brief 32 Implementation of a little Input handler that distributes everything 33 coming from OIS. 34 */ 34 35 35 36 #ifndef _InputManager_H__ … … 40 41 #include <map> 41 42 #include <vector> 42 43 #include <stack> 43 44 #include "util/Math.h" 44 45 #include "core/OrxonoxClass.h" … … 47 48 namespace orxonox 48 49 { 49 /** 50 * Helper class to realise a vector<int[4]> 51 */ 52 class POVStates 53 { 54 public: 55 int operator[](unsigned int index) { return povStates[index]; } 56 int povStates[4]; 57 }; 58 59 /** 60 * Helper class to realise a vector< {int[4], int[4]} > 61 */ 62 class SliderStates 63 { 64 public: 65 IntVector2 sliderStates[4]; 66 }; 67 68 /** 69 * Struct for storing a custom input state 70 */ 71 struct StoredState 72 { 73 std::vector<KeyHandler*> activeKeyHandlers_; 74 std::vector<MouseHandler*> activeMouseHandlers_; 75 std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_; 76 std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_; 77 }; 78 79 struct JoyStickCalibration 80 { 81 int zeroStates[24]; 82 float positiveCoeff[24]; 83 float negativeCoeff[24]; 84 }; 85 86 /** 87 @brief Captures and distributes mouse and keyboard input. 88 */ 89 class _CoreExport InputManager 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 }; 60 61 /** 62 @brief 63 Helper class to realise a vector< {int[4], int[4]} > 64 */ 65 class SliderStates 66 { 67 public: 68 IntVector2 sliderStates[4]; 69 }; 70 71 struct JoyStickCalibration 72 { 73 int zeroStates[24]; 74 float positiveCoeff[24]; 75 float negativeCoeff[24]; 76 }; 77 78 /** 79 @brief 80 Captures and distributes mouse and keyboard input. 81 */ 82 class _CoreExport InputManager 90 83 : public OrxonoxClass, 91 public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener 92 { 93 public: // enumerations 94 /** 95 @brief Designates the way input is handled and redirected. 96 */ 97 enum InputState 98 { 99 IS_UNINIT, //!< InputManager has not yet been initialised. 100 IS_NONE, //!< Input is discarded. 101 IS_NORMAL, //!< Normal play state. Key and button bindings are active. 102 IS_GUI, //!< All OIS input events are passed to CEGUI. 103 IS_CONSOLE, //!< Keyboard input is redirected to the InputBuffer. 104 IS_DETECT, //!< All the input additionally goes to the KeyDetector 105 IS_NODETECT, //!< remove KeyDetector 106 IS_NOCALIBRATE, 107 IS_CALIBRATE, 108 IS_CUSTOM //!< Any possible configuration. 109 }; 110 111 public: // member functions 112 void setConfigValues(); 113 114 public: // static functions 115 static bool initialise(const size_t windowHnd, int windowWidth, int windowHeight, 116 bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false); 117 static bool initialiseKeyboard(); 118 static bool initialiseMouse(); 119 static bool initialiseJoySticks(); 120 static int numberOfKeyboards(); 121 static int numberOfMice(); 122 static int numberOfJoySticks(); 123 124 static void destroy(); 125 static void destroyKeyboard(); 126 static void destroyMouse(); 127 static void destroyJoySticks(); 128 129 //static bool isModifierDown(KeyboardModifier::Enum modifier); 130 //static bool isKeyDown(KeyCode::Enum key); 131 //static const MouseState getMouseState(); 132 //static const JoyStickState getJoyStickState(unsigned int ID); 133 134 static void setWindowExtents(const int width, const int height); 135 136 static void setInputState(const InputState state); 137 static InputState getInputState(); 138 139 static void storeKeyStroke(const std::string& name); 140 static void keyBind(const std::string& command); 141 142 static void calibrate(); 143 144 static void tick(float dt); 145 146 static bool addKeyHandler (KeyHandler* handler, const std::string& name); 147 static bool removeKeyHandler (const std::string& name); 148 static KeyHandler* getKeyHandler (const std::string& name); 149 static bool enableKeyHandler (const std::string& name); 150 static bool disableKeyHandler (const std::string& name); 151 static bool isKeyHandlerActive (const std::string& name); 152 153 static bool addMouseHandler (MouseHandler* handler, const std::string& name); 154 static bool removeMouseHandler (const std::string& name); 155 static MouseHandler* getMouseHandler (const std::string& name); 156 static bool enableMouseHandler (const std::string& name); 157 static bool disableMouseHandler (const std::string& name); 158 static bool isMouseHandlerActive (const std::string& name); 159 160 static bool addJoyStickHandler (JoyStickHandler* handler, const std::string& name); 161 static bool removeJoyStickHandler (const std::string& name); 162 static JoyStickHandler* getJoyStickHandler(const std::string& name); 163 static bool enableJoyStickHandler (const std::string& name, unsigned int id); 164 static bool disableJoyStickHandler (const std::string& name, unsigned int id); 165 static bool isJoyStickHandlerActive (const std::string& name, unsigned int id); 166 167 private: // functions 168 // don't mess with a Singleton 169 InputManager (); 170 InputManager (const InputManager&); 171 ~InputManager(); 172 173 // Intenal methods 174 bool _initialise(const size_t, int, int, bool, bool, bool); 175 bool _initialiseKeyboard(); 176 bool _initialiseMouse(); 177 bool _initialiseJoySticks(); 178 179 void _destroy(); 180 void _destroyKeyboard(); 181 void _destroyMouse(); 182 void _destroyJoySticks(); 183 184 void _updateTickables(); 185 186 void _saveState(); 187 void _restoreState(); 188 189 void _completeCalibration(); 190 191 void _fireAxis(unsigned int iJoyStick, int axis, int value); 192 unsigned int _getJoystick(const OIS::JoyStickEvent& arg); 193 194 void _tick(float dt); 195 196 // input events 197 bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 198 bool mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 199 bool mouseMoved (const OIS::MouseEvent &arg); 200 bool keyPressed (const OIS::KeyEvent &arg); 201 bool keyReleased (const OIS::KeyEvent &arg); 202 bool buttonPressed (const OIS::JoyStickEvent &arg, int button); 203 bool buttonReleased(const OIS::JoyStickEvent &arg, int button); 204 bool axisMoved (const OIS::JoyStickEvent &arg, int axis); 205 bool sliderMoved (const OIS::JoyStickEvent &arg, int id); 206 bool povMoved (const OIS::JoyStickEvent &arg, int id); 207 //bool vector3Moved (const OIS::JoyStickEvent &arg, int id); 208 209 static InputManager& _getSingleton(); 210 static InputManager* _getSingletonPtr() { return &_getSingleton(); } 211 212 private: // variables 213 OIS::InputManager* inputSystem_; //!< OIS input manager 214 OIS::Keyboard* keyboard_; //!< OIS mouse 215 OIS::Mouse* mouse_; //!< OIS keyboard 216 std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks 217 unsigned int joySticksSize_; 218 219 KeyBinder* keyBinder_; //!< KeyBinder instance 220 KeyDetector* keyDetector_; //!< KeyDetector instance 221 InputBuffer* buffer_; //!< InputBuffer instance 222 CalibratorCallback* calibratorCallback_; 223 224 InputState state_; 225 InputState stateRequest_; 226 InputState savedState_; 227 unsigned int keyboardModifiers_; 228 StoredState savedHandlers_; 229 230 // joystick calibration 231 //std::vector<int> marginalsMaxConfig_; 232 //std::vector<int> marginalsMinConfig_; 233 int marginalsMax_[24]; 234 int marginalsMin_[24]; 235 bool bCalibrated_; 236 237 //! Keeps track of the joy stick POV states 238 std::vector<POVStates> povStates_; 239 //! Keeps track of the possibly two slider axes 240 std::vector<SliderStates> sliderStates_; 241 std::vector<JoyStickCalibration> joySticksCalibration_; 242 243 std::map<std::string, KeyHandler*> keyHandlers_; 244 std::map<std::string, MouseHandler*> mouseHandlers_; 245 std::map<std::string, JoyStickHandler*> joyStickHandlers_; 246 247 std::vector<KeyHandler*> activeKeyHandlers_; 248 std::vector<MouseHandler*> activeMouseHandlers_; 249 std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_; 250 std::vector<std::pair<InputTickable*, HandlerState> > activeHandlers_; 251 252 std::vector<Key> keysDown_; 253 std::vector<MouseButton::Enum> mouseButtonsDown_; 254 std::vector<std::vector<int> > joyStickButtonsDown_; 255 256 static std::string bindingCommmandString_s; 257 }; 258 84 public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener 85 { 86 // --> setConfigValues is private 87 friend class ClassIdentifier<InputManager>; 88 // let Core class use tick(.) 89 friend class Core; 90 91 public: 92 enum InputManagerState 93 { 94 Uninitialised = 0x00, 95 OISReady = 0x01, 96 InternalsReady = 0x02, 97 Ticking = 0x04, 98 Calibrating = 0x08, 99 ReloadRequest = 0x10, 100 JoyStickSupport = 0x20 // used with ReloadRequest to store a bool 101 }; 102 103 InputManager (); 104 ~InputManager(); 105 106 void initialise(size_t windowHnd, int windowWidth, int windowHeight, bool joyStickSupport = true); 107 108 void reloadInputSystem(bool joyStickSupport = true); 109 110 int numberOfKeyboards() { return keyboard_ ? 1 : 0; } 111 int numberOfMice() { return mouse_ ? 1 : 0; } 112 int numberOfJoySticks() { return joySticksSize_; } 113 114 void setWindowExtents(const int width, const int height); 115 116 template <class T> 117 T* createInputState(const std::string& name, int priority) 118 { 119 T* state = new T; 120 if (_configureInputState(state, name, priority)) 121 return state; 122 else 123 { 124 delete state; 125 return 0; 126 } 127 } 128 129 InputState* getState (const std::string& name); 130 InputState* getCurrentState(); 131 bool requestDestroyState (const std::string& name); 132 bool requestEnterState (const std::string& name); 133 bool requestLeaveState (const std::string& name); 134 135 void tick(float dt); 136 137 static InputManager& getInstance() { assert(singletonRef_s); return *singletonRef_s; } 138 static InputManager* getInstancePtr() { return singletonRef_s; } 139 140 public: // console commands 141 static void storeKeyStroke(const std::string& name); 142 static void keyBind(const std::string& command); 143 static void calibrate(); 144 static void reload(bool joyStickSupport = true); 145 146 private: // functions 147 // don't mess with a Singleton 148 InputManager (const InputManager&); 149 150 // Intenal methods 151 void _initialiseKeyboard(); 152 void _initialiseMouse(); 153 void _initialiseJoySticks(); 154 void _redimensionLists(); 155 156 void _destroyKeyboard(); 157 void _destroyMouse(); 158 void _destroyJoySticks(); 159 void _destroyState(InputState* state); 160 void _clearBuffers(); 161 162 void _reload(bool joyStickSupport); 163 164 void _completeCalibration(); 165 166 void _fireAxis(unsigned int iJoyStick, int axis, int value); 167 unsigned int _getJoystick(const OIS::JoyStickEvent& arg); 168 169 void _updateActiveStates(); 170 bool _configureInputState(InputState* state, const std::string& name, int priority); 171 172 // input events 173 bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 174 bool mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 175 bool mouseMoved (const OIS::MouseEvent &arg); 176 bool keyPressed (const OIS::KeyEvent &arg); 177 bool keyReleased (const OIS::KeyEvent &arg); 178 bool buttonPressed (const OIS::JoyStickEvent &arg, int button); 179 bool buttonReleased(const OIS::JoyStickEvent &arg, int button); 180 bool axisMoved (const OIS::JoyStickEvent &arg, int axis); 181 bool sliderMoved (const OIS::JoyStickEvent &arg, int id); 182 bool povMoved (const OIS::JoyStickEvent &arg, int id); 183 // don't remove that! Or else add OIS as dependency library to orxonox. 184 bool vector3Moved (const OIS::JoyStickEvent &arg, int id) { return true; } 185 186 void setConfigValues(); 187 188 private: // variables 189 OIS::InputManager* inputSystem_; //!< OIS input manager 190 OIS::Keyboard* keyboard_; //!< OIS mouse 191 OIS::Mouse* mouse_; //!< OIS keyboard 192 std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks 193 unsigned int joySticksSize_; 194 unsigned int devicesNum_; 195 size_t windowHnd_; //!< Render window handle 196 InputManagerState internalState_; //!< Current internal state 197 198 // some internally handled states 199 SimpleInputState* stateDetector_; //!< KeyDetector instance 200 SimpleInputState* stateCalibrator_; 201 SimpleInputState* stateEmpty_; 202 203 std::map<std::string, InputState*> inputStatesByName_; 204 std::map<int, InputState*> inputStatesByPriority_; 205 206 std::set<InputState*> stateEnterRequests_; //!< Request to enter a new state 207 std::set<InputState*> stateLeaveRequests_; //!< Request to leave a running state 208 std::set<InputState*> stateDestroyRequests_; //!< Request to destroy a state 209 210 std::map<int, InputState*> activeStates_; 211 std::vector<InputState*> activeStatesTop_; //!< Current input states for joy stick events. 212 std::vector<InputState*> activeStatesTicked_; //!< Current input states for joy stick events. 213 214 // joystick calibration 215 //std::vector<int> marginalsMaxConfig_; 216 //std::vector<int> marginalsMinConfig_; 217 int marginalsMax_[24]; 218 int marginalsMin_[24]; 219 bool bCalibrated_; 220 bool bCalibrating_; 221 222 unsigned int keyboardModifiers_; //!< Bit mask representing keyboard modifiers. 223 std::vector<POVStates> povStates_; //!< Keeps track of the joy stick POV states. 224 std::vector<SliderStates> sliderStates_; //!< Keeps track of the possibly two slider axes. 225 std::vector<JoyStickCalibration> joySticksCalibration_; 226 227 std::vector<Key> keysDown_; 228 std::vector<MouseButton::Enum> mouseButtonsDown_; 229 std::vector<std::vector<JoyStickButton::Enum> > joyStickButtonsDown_; 230 231 static std::string bindingCommmandString_s; 232 static InputManager* singletonRef_s; 233 }; 259 234 } 260 235
Note: See TracChangeset
for help on using the changeset viewer.