Changeset 1203 for code/branches/input/src/core
- Timestamp:
- Apr 29, 2008, 11:13:11 PM (17 years ago)
- Location:
- code/branches/input/src/core
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/input/src/core/CMakeLists.txt
r1196 r1203 5 5 Identifier.cc 6 6 IdentifierDistributor.cc 7 #InputHandler.cc7 InputHandler.cc 8 8 InputManager.cc 9 9 # InputEventListener.cc -
code/branches/input/src/core/InputBuffer.h
r1195 r1203 36 36 37 37 #include "ois/OISKeyboard.h" 38 #include "InputHandler.h" 38 39 39 40 namespace orxonox … … 42 43 {}; 43 44 44 class _CoreExport InputBuffer : public OIS::KeyListener45 class _CoreExport InputBuffer : public KeyHandler 45 46 { 46 47 struct InputBufferListenerTuple … … 110 111 bool keyPressed(const OIS::KeyEvent &e); 111 112 bool keyReleased(const OIS::KeyEvent &e); 113 bool keyHeld(const OIS::KeyEvent &e) { return true; } 112 114 113 115 OIS::Keyboard* keyboard_; -
code/branches/input/src/core/InputHandler.cc
r1182 r1203 35 35 #include "Debug.h" 36 36 #include "util/Convert.h" 37 #include "InputEventListener.h"38 #include "InputEvent.h"39 #include "InputManager.h"40 37 #include "core/CommandExecutor.h" 41 38 … … 43 40 { 44 41 // ############################### 45 // ### InputHandlerGame###42 // ###### KeyBinder ###### 46 43 // ############################### 47 44 48 45 /** 49 @brief standard constructor 50 */ 51 InputHandlerGame::InputHandlerGame() 52 { 46 @brief Constructor that does as little as necessary. 47 */ 48 KeyBinder::KeyBinder() 49 { 50 clearBindings(); 53 51 } 54 52 … … 56 54 @brief Destructor 57 55 */ 58 InputHandlerGame::~InputHandlerGame() 59 { 60 } 61 62 /** 63 @brief Loads the key bindings from the ini file. 64 Currently, this is just a simple test routine that fills the list with numbers. 65 */ 66 bool InputHandlerGame::loadBindings() 56 KeyBinder::~KeyBinder() 57 { 58 } 59 60 /** 61 @brief Overwrites all bindings with "" 62 */ 63 void KeyBinder::clearBindings() 67 64 { 68 65 for (int i = 0; i < numberOfKeys_s; i++) 69 66 { 70 // simply write the key number (i) in the string 71 this->bindingsKeyPress_[i] = ""; 72 this->bindingsKeyRelease_[i] = ""; 73 } 74 this->bindingsKeyPress_[OIS::KC_NUMPADENTER] = "setInputMode " + getConvertedValue<int, std::string>(IM_KEYBOARD); 75 this->bindingsKeyPress_[OIS::KC_ESCAPE] = "exit"; 76 this->bindingsKeyHold_[OIS::KC_U] = "exec disco.txt"; 77 return true; 78 } 67 bindingsKeyPress_ [i] = ""; 68 bindingsKeyRelease_[i] = ""; 69 bindingsKeyHold_ [i] = ""; 70 } 71 for (int i = 0; i < numberOfMouseButtons_s; i++) 72 { 73 bindingsMouseButtonPress_ [i] = ""; 74 bindingsMouseButtonRelease_[i] = ""; 75 bindingsMouseButtonHold_ [i] = ""; 76 } 77 for (int i = 0; i < numberOfJoyStickButtons_s; i++) 78 { 79 bindingsJoyStickButtonPress_ [i] = ""; 80 bindingsJoyStickButtonRelease_[i] = ""; 81 bindingsJoyStickButtonHold_ [i] = ""; 82 } 83 } 84 85 /** 86 @brief Loads the key and button bindings. 87 @return True if loading succeeded. 88 */ 89 bool KeyBinder::loadBindings() 90 { 91 COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings..." << std::endl; 92 93 // clear all the bindings at first. 94 clearBindings(); 95 96 // TODO: Insert the code to load the bindings from file. 97 bindingsKeyPress_[OIS::KC_NUMPADENTER] = "activateConsole"; 98 bindingsKeyPress_[OIS::KC_ESCAPE] = "exit"; 99 bindingsKeyHold_[OIS::KC_U] = "exec disco.txt"; 100 101 COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings done." << std::endl; 102 return true; 103 } 104 79 105 80 106 /** … … 82 108 @param e Event information 83 109 */ 84 bool InputHandlerGame::keyPressed(const OIS::KeyEvent &e) 85 { 86 this->keysDown_.push_back(e.key); 110 bool KeyBinder::keyPressed(const OIS::KeyEvent &e) 111 { 87 112 // find the appropriate key binding 88 113 std::string cmdStr = bindingsKeyPress_[int(e.key)]; … … 92 117 COUT(3) << "Executing command: " << cmdStr << std::endl; 93 118 } 119 94 120 return true; 95 121 } … … 99 125 @param e Event information 100 126 */ 101 bool InputHandlerGame::keyReleased(const OIS::KeyEvent &e) 102 { 103 // remove the key from the keysDown_ list 104 for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++) 105 { 106 if (*it == e.key) 107 { 108 keysDown_.erase(it); 109 break; 110 } 111 } 112 127 bool KeyBinder::keyReleased(const OIS::KeyEvent &e) 128 { 113 129 // find the appropriate key binding 114 130 std::string cmdStr = bindingsKeyRelease_[int(e.key)]; … … 118 134 COUT(3) << "Executing command: " << cmdStr << std::endl; 119 135 } 136 137 return true; 138 } 139 140 /** 141 @brief Event handler for the keyHeld Event. 142 @param e Event information 143 */ 144 bool KeyBinder::keyHeld(const OIS::KeyEvent &e) 145 { 146 // find the appropriate key binding 147 std::string cmdStr = bindingsKeyHold_[int(e.key)]; 148 if (cmdStr != "") 149 { 150 CommandExecutor::execute(cmdStr); 151 COUT(3) << "Executing command: " << cmdStr << std::endl; 152 } 153 120 154 return true; 121 155 } … … 125 159 @param e Event information 126 160 */ 127 bool InputHandlerGame::mouseMoved(const OIS::MouseEvent &e)161 bool KeyBinder::mouseMoved(const OIS::MouseEvent &e) 128 162 { 129 163 return true; … … 135 169 @param id The ID of the mouse button 136 170 */ 137 bool InputHandlerGame::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) 138 { 171 bool KeyBinder::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) 172 { 173 // find the appropriate key binding 174 std::string cmdStr = bindingsMouseButtonPress_[int(id)]; 175 if (cmdStr != "") 176 { 177 CommandExecutor::execute(cmdStr); 178 COUT(3) << "Executing command: " << cmdStr << std::endl; 179 } 180 139 181 return true; 140 182 } … … 145 187 @param id The ID of the mouse button 146 188 */ 147 bool InputHandlerGame::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) 148 { 149 return true; 150 } 151 152 /** 153 @brief Tick method to do additional calculations. 154 @param dt Delta time. 155 */ 156 void InputHandlerGame::tick(float dt) 157 { 158 // iterate through all the pressed keys 159 for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++) 160 { 161 // find the appropriate key binding 162 std::string cmdStr = bindingsKeyHold_[*it]; 163 if (cmdStr != "") 164 { 165 CommandExecutor::execute(cmdStr); 166 COUT(3) << "Executing command: " << cmdStr << std::endl; 167 } 168 } 169 } 170 171 /** 172 @brief Calls all the objects from classes that derive from InputEventListener. 173 @param evt The input event that occured. 174 */ 175 inline void InputHandlerGame::callListeners(orxonox::InputEvent &evt) 176 { 177 for (Iterator<InputEventListener> it = ObjectList<InputEventListener>::start(); it; ) 178 { 179 if (it->bActive_) 180 (it++)->eventOccured(evt); 181 else 182 it++; 183 } 184 } 189 bool KeyBinder::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) 190 { 191 // find the appropriate key binding 192 std::string cmdStr = bindingsMouseButtonRelease_[int(id)]; 193 if (cmdStr != "") 194 { 195 CommandExecutor::execute(cmdStr); 196 COUT(3) << "Executing command: " << cmdStr << std::endl; 197 } 198 199 return true; 200 } 201 202 /** 203 @brief Event handler for the mouseHeld Event. 204 @param e Event information 205 @param id The ID of the mouse button 206 */ 207 bool KeyBinder::mouseHeld(const OIS::MouseEvent &e, OIS::MouseButtonID id) 208 { 209 // find the appropriate key binding 210 std::string cmdStr = bindingsMouseButtonHold_[int(id)]; 211 if (cmdStr != "") 212 { 213 CommandExecutor::execute(cmdStr); 214 COUT(3) << "Executing command: " << cmdStr << std::endl; 215 } 216 217 return true; 218 } 219 220 bool KeyBinder::buttonPressed(const OIS::JoyStickEvent &arg, int button) 221 { 222 // find the appropriate key binding 223 std::string cmdStr = bindingsJoyStickButtonPress_[button]; 224 if (cmdStr != "") 225 { 226 CommandExecutor::execute(cmdStr); 227 COUT(3) << "Executing command: " << cmdStr << std::endl; 228 } 229 230 return true; 231 } 232 233 bool KeyBinder::buttonReleased(const OIS::JoyStickEvent &arg, int button) 234 { 235 // find the appropriate key binding 236 std::string cmdStr = bindingsJoyStickButtonRelease_[button]; 237 if (cmdStr != "") 238 { 239 CommandExecutor::execute(cmdStr); 240 COUT(3) << "Executing command: " << cmdStr << std::endl; 241 } 242 243 return true; 244 } 245 246 bool KeyBinder::buttonHeld(const OIS::JoyStickEvent &arg, int button) 247 { 248 // find the appropriate key binding 249 std::string cmdStr = bindingsJoyStickButtonHold_[button]; 250 if (cmdStr != "") 251 { 252 CommandExecutor::execute(cmdStr); 253 COUT(3) << "Executing command: " << cmdStr << std::endl; 254 } 255 256 return true; 257 } 258 259 bool KeyBinder::axisMoved(const OIS::JoyStickEvent &arg, int axis) 260 { 261 return true; 262 } 263 264 bool KeyBinder::sliderMoved(const OIS::JoyStickEvent &arg, int id) 265 { 266 return true; 267 } 268 269 bool KeyBinder::povMoved(const OIS::JoyStickEvent &arg, int id) 270 { 271 return true; 272 } 273 185 274 186 275 187 276 // ############################### 188 // ### InputHandlerGUI###277 // ### GUIInputHandler ### 189 278 // ############################### 190 279 191 /** 192 @brief standard constructor 193 */ 194 InputHandlerGUI::InputHandlerGUI() 195 { 196 } 197 198 /** 199 @brief Destructor 200 */ 201 InputHandlerGUI::~InputHandlerGUI() 202 { 203 } 204 205 /** 206 @brief Event handler for the keyPressed Event. 207 @param e Event information 208 */ 209 bool InputHandlerGUI::keyPressed(const OIS::KeyEvent &e) 210 { 211 //CEGUI::System::getSingleton().injectKeyDown( arg.key ); 212 //CEGUI::System::getSingleton().injectChar( arg.text ); 213 return true; 214 } 215 216 /** 217 @brief Event handler for the keyReleased Event. 218 @param e Event information 219 */ 220 bool InputHandlerGUI::keyReleased(const OIS::KeyEvent &e) 221 { 222 //CEGUI::System::getSingleton().injectKeyUp( arg.key ); 223 return true; 224 } 225 226 /** 227 @brief Event handler for the mouseMoved Event. 228 @param e Event information 229 */ 230 bool InputHandlerGUI::mouseMoved(const OIS::MouseEvent &e) 231 { 232 //CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel ); 233 return true; 234 } 235 236 /** 237 @brief Event handler for the mousePressed Event. 238 @param e Event information 239 @param id The ID of the mouse button 240 */ 241 bool InputHandlerGUI::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) 242 { 243 //CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id)); 244 return true; 245 } 246 247 /** 248 @brief Event handler for the mouseReleased Event. 249 @param e Event information 250 @param id The ID of the mouse button 251 */ 252 bool InputHandlerGUI::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) 253 { 254 //CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id)); 255 return true; 256 } 257 258 /** 259 @brief Tick method to do additional calculations. 260 @param dt Delta time. 261 */ 262 void InputHandlerGUI::tick(float dt) 263 { 264 265 } 280 ///** 281 // @brief standard constructor 282 //*/ 283 //GUIInputHandler::GUIInputHandler() 284 //{ 285 //} 286 287 ///** 288 // @brief Destructor 289 //*/ 290 //GUIInputHandler::~GUIInputHandler() 291 //{ 292 //} 293 294 ///** 295 // @brief Event handler for the keyPressed Event. 296 // @param e Event information 297 //*/ 298 //bool GUIInputHandler::keyPressed(const OIS::KeyEvent &e) 299 //{ 300 ////CEGUI::System::getSingleton().injectKeyDown( arg.key ); 301 ////CEGUI::System::getSingleton().injectChar( arg.text ); 302 // return true; 303 //} 304 305 ///** 306 // @brief Event handler for the keyReleased Event. 307 // @param e Event information 308 //*/ 309 //bool GUIInputHandler::keyReleased(const OIS::KeyEvent &e) 310 //{ 311 ////CEGUI::System::getSingleton().injectKeyUp( arg.key ); 312 // return true; 313 //} 314 315 ///** 316 // @brief Event handler for the mouseMoved Event. 317 // @param e Event information 318 //*/ 319 //bool GUIInputHandler::mouseMoved(const OIS::MouseEvent &e) 320 //{ 321 ////CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel ); 322 // return true; 323 //} 324 325 ///** 326 // @brief Event handler for the mousePressed Event. 327 // @param e Event information 328 // @param id The ID of the mouse button 329 //*/ 330 //bool GUIInputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) 331 //{ 332 ////CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id)); 333 // return true; 334 //} 335 336 ///** 337 // @brief Event handler for the mouseReleased Event. 338 // @param e Event information 339 // @param id The ID of the mouse button 340 //*/ 341 //bool GUIInputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) 342 //{ 343 ////CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id)); 344 // return true; 345 //} 266 346 267 347 } -
code/branches/input/src/core/InputHandler.h
r1182 r1203 38 38 39 39 #include <string> 40 #include <list> 41 #include <OIS/OIS.h> 42 43 #include "InputEvent.h" 40 #include "ois/OIS.h" 44 41 45 42 namespace orxonox … … 56 53 } 57 54 58 class _CoreExport BaseInputHandler59 : public OIS::KeyListener, public OIS::MouseListener60 {61 virtual void tick(float dt) = 0;62 };63 64 55 /** 65 @brief Captures mouse and keyboard input while in the actual game mode. 66 Manages the key bindings. 56 @brief Interface class used for key input listeners. 67 57 */ 68 class _CoreExport InputHandlerGame : public BaseInputHandler58 class _CoreExport KeyHandler : public OIS::KeyListener 69 59 { 70 60 public: 71 InputHandlerGame (); 72 ~InputHandlerGame(); 61 virtual bool keyHeld(const OIS::KeyEvent &arg) = 0; 62 }; 63 64 /** 65 @brief Interface class used for mouse input listeners. 66 */ 67 class _CoreExport MouseHandler : public OIS::MouseListener 68 { 69 public: 70 virtual bool mouseHeld(const OIS::MouseEvent &arg, OIS::MouseButtonID id) = 0; 71 }; 72 73 /** 74 @brief Interface class used for joy stick input listeners. 75 */ 76 class _CoreExport JoyStickHandler : public OIS::JoyStickListener 77 { 78 public: 79 virtual bool buttonHeld(const OIS::JoyStickEvent &arg, int button) = 0; 80 }; 81 82 83 /** 84 @brief Captures mouse, keyboard and joy stick input while in the actual game mode. 85 Manages the key bindings. 86 */ 87 class _CoreExport KeyBinder : public KeyHandler, public MouseHandler, public JoyStickHandler 88 { 89 public: 90 KeyBinder (); 91 ~KeyBinder(); 73 92 74 93 bool loadBindings(); 94 void clearBindings(); 75 95 76 private: 77 // input events 78 bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 79 bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id); 80 bool mouseMoved (const OIS::MouseEvent &arg); 96 private: // functions 81 97 bool keyPressed (const OIS::KeyEvent &arg); 82 98 bool keyReleased (const OIS::KeyEvent &arg); 99 bool keyHeld (const OIS::KeyEvent &arg); 83 100 84 void tick(float dt); 101 bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 102 bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id); 103 bool mouseHeld (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 104 bool mouseMoved (const OIS::MouseEvent &arg); 85 105 86 // temporary hack 87 void callListeners(InputEvent &evt); 106 bool buttonPressed (const OIS::JoyStickEvent &arg, int button); 107 bool buttonReleased(const OIS::JoyStickEvent &arg, int button); 108 bool buttonHeld (const OIS::JoyStickEvent &arg, int button); 109 bool axisMoved (const OIS::JoyStickEvent &arg, int axis); 110 bool sliderMoved (const OIS::JoyStickEvent &arg, int id); 111 bool povMoved (const OIS::JoyStickEvent &arg, int id); 88 112 89 //! Stores all the keys that are down 90 std::list<OIS::KeyCode> keysDown_; 91 113 private: // variables 92 114 /** denotes the maximum number of different keys there are in OIS. 93 256 should be ok since the highest number in the enum is 237. */115 256 should be ok since the highest number in the OIS enum is 237. */ 94 116 static const int numberOfKeys_s = 256; 95 117 //! Array of input events for every pressed key 96 std::string bindingsKeyPress_ [numberOfKeys_s];118 std::string bindingsKeyPress_ [numberOfKeys_s]; 97 119 //! Array of input events for every released key 98 120 std::string bindingsKeyRelease_[numberOfKeys_s]; 99 //! Array of input events for every holdingkey100 std::string bindingsKeyHold_ [numberOfKeys_s];121 //!Array of input events for every held key 122 std::string bindingsKeyHold_ [numberOfKeys_s]; 101 123 102 124 /** denotes the maximum number of different buttons there are in OIS. 103 16 should be ok since the highest number in the enum is 7. */ 104 static const int numberOfButtons_s = 16; 105 //! Array of input events for every pressed key 106 std::string bindingsButtonPressed_[numberOfButtons_s]; 107 //! Array of input events for every released key 108 std::string bindingsButtonReleased_[numberOfButtons_s]; 125 16 should be ok since the highest number in the OIS enum is 7. */ 126 static const int numberOfMouseButtons_s = 16; 127 //! Array of input events for every pressed mouse button 128 std::string bindingsMouseButtonPress_ [numberOfMouseButtons_s]; 129 //! Array of input events for every released mouse button 130 std::string bindingsMouseButtonRelease_[numberOfMouseButtons_s]; 131 //! Array of input events for every held mouse button 132 std::string bindingsMouseButtonHold_ [numberOfMouseButtons_s]; 133 134 /** denotes the maximum number of different buttons there are in OIS. 135 32 should be ok. */ 136 static const int numberOfJoyStickButtons_s = 32; 137 //! Array of input events for every pressed joy stick button 138 std::string bindingsJoyStickButtonPress_ [numberOfJoyStickButtons_s]; 139 //! Array of input events for every released joy stick button 140 std::string bindingsJoyStickButtonRelease_[numberOfJoyStickButtons_s]; 141 //! Array of input events for every held joy stick button 142 std::string bindingsJoyStickButtonHold_ [numberOfJoyStickButtons_s]; 109 143 110 144 }; … … 115 149 GUI. 116 150 */ 117 class _CoreExport InputHandlerGUI : public BaseInputHandler118 {119 public:120 InputHandlerGUI();121 ~InputHandlerGUI();151 //class _CoreExport GUIInputHandler : public KeyHandler, public MouseHandler, public JoyStickHandler 152 //{ 153 //public: 154 // GUIInputHandler (); 155 // ~GUIInputHandler(); 122 156 123 void tick(float dt); 157 //private: 158 // // input events 159 //bool keyPressed (const OIS::KeyEvent &arg); 160 //bool keyReleased (const OIS::KeyEvent &arg); 161 //bool keyHeld (const OIS::KeyEvent &arg); 124 162 125 private: 126 // input events 127 bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 128 bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id); 129 bool mouseMoved (const OIS::MouseEvent &arg); 130 bool keyPressed (const OIS::KeyEvent &arg); 131 bool keyReleased (const OIS::KeyEvent &arg); 132 }; 163 // bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 164 //bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id); 165 //bool mouseHeld (const OIS::MouseEvent &arg, OIS::MouseButtonID id); 166 // bool mouseMoved (const OIS::MouseEvent &arg); 167 168 //bool buttonPressed (const OIS::JoyStickEvent &arg, int button); 169 //bool buttonReleased(const OIS::JoyStickEvent &arg, int button); 170 //bool buttonHeld (const OIS::JoyStickEvent &arg, int button); 171 //bool axisMoved (const OIS::JoyStickEvent &arg, int axis); 172 //bool sliderMoved (const OIS::JoyStickEvent &arg, int id); 173 //bool povMoved (const OIS::JoyStickEvent &arg, int id); 174 //}; 133 175 134 176 } -
code/branches/input/src/core/InputManager.cc
r1195 r1203 45 45 // ### Internal Methods ### 46 46 // ############################### 47 // ############################### 47 48 48 49 /** … … 54 55 state_(IS_UNINIT), stateRequest_(IS_UNINIT) 55 56 { 56 // overwrite every key binding with ""57 _clearBindings();58 _setNumberOfJoysticks(0);59 60 57 RegisterObject(InputManager); 61 58 } … … 76 73 InputManager::~InputManager() 77 74 { 78 this->_destroy();75 _destroy(); 79 76 } 80 77 … … 129 126 setWindowExtents(windowWidth, windowHeight); 130 127 131 this->state_ = IS_NONE;128 state_ = IS_NONE; 132 129 CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl; 133 130 } … … 140 137 addKeyListener(new InputBuffer(), "buffer"); 141 138 139 KeyBinder* binder = new KeyBinder(); 140 binder->loadBindings(); 141 addKeyListener(binder, "keybinder"); 142 addMouseListener(binder, "keybinder"); 143 142 144 // Read all the key bindings and assign them 143 if (!_loadBindings())144 return false;145 //if (!_loadBindings()) 146 // return false; 145 147 146 148 CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl; … … 150 152 /** 151 153 @brief Creates a keyboard and sets the event handler. 152 */ 153 void InputManager::_initialiseKeyboard() 154 { 154 @return False if keyboard stays uninitialised, true otherwise. 155 */ 156 bool InputManager::_initialiseKeyboard() 157 { 158 if (keyboard_ != 0) 159 { 160 CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl; 161 return true; 162 } 155 163 try 156 164 { … … 162 170 // note: OIS will not detect keys that have already been down when the keyboard was created. 163 171 CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; 172 return true; 164 173 } 165 174 else 175 { 166 176 CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl; 177 return false; 178 } 167 179 } 168 180 catch (OIS::Exception ex) 169 181 { 182 // TODO: Test this output regarding formatting 170 183 CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n" 171 184 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 172 185 keyboard_ = 0; 186 return false; 173 187 } 174 188 } … … 176 190 /** 177 191 @brief Creates a mouse and sets the event handler. 178 */ 179 void InputManager::_initialiseMouse() 180 { 192 @return False if mouse stays uninitialised, true otherwise. 193 */ 194 bool InputManager::_initialiseMouse() 195 { 196 if (mouse_ != 0) 197 { 198 CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl; 199 return true; 200 } 181 201 try 182 202 { … … 187 207 mouse_->setEventCallback(this); 188 208 CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl; 209 return true; 189 210 } 190 211 else 212 { 191 213 CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl; 214 return false; 215 } 192 216 } 193 217 catch (OIS::Exception ex) … … 196 220 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 197 221 mouse_ = 0; 222 return false; 198 223 } 199 224 } … … 201 226 /** 202 227 @brief Creates all joy sticks and sets the event handler. 203 */ 204 void InputManager::_initialiseJoySticks() 205 { 228 @return False joy stick stay uninitialised, true otherwise. 229 */ 230 bool InputManager::_initialiseJoySticks() 231 { 232 if (joySticks_.size() > 0) 233 { 234 CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl; 235 return true; 236 } 237 bool success = false; 206 238 if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0) 207 239 { 208 _setNumberOfJoysticks(inputSystem_->getNumberOfDevices(OIS::OISJoyStick)); 209 for (std::vector<OIS::JoyStick*>::iterator it = joySticks_.begin(); it != joySticks_.end(); it++) 240 for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++) 210 241 { 211 242 try 212 243 { 213 *it = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true)); 244 OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true)); 245 joySticks_[stig->getID()] = stig; 214 246 // register our listener in OIS. 215 (*it)->setEventCallback(this); 216 CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << (*it)->getID() << std::endl; 247 stig->setEventCallback(this); 248 CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl; 249 success = true; 217 250 } 218 251 catch (OIS::Exception ex) 219 252 { 220 CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy stick with ID" << (*it)->getID()<< "\n"253 CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n" 221 254 << "OIS error message: \"" << ex.eText << "\"" << std::endl; 222 (*it) = 0;223 255 } 224 256 } 225 257 } 226 258 else 259 { 227 260 CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl; 228 } 229 230 /** 231 @brief Resizes all lists related to joy sticks and sets joy stick bindings to "". 232 @param size Number of joy sticks available. 233 */ 234 void InputManager::_setNumberOfJoysticks(int size) 235 { 236 this->bindingsJoyStickButtonHold_ .resize(size); 237 this->bindingsJoyStickButtonPress_ .resize(size); 238 this->bindingsJoyStickButtonRelease_.resize(size); 239 this->bJoyStickButtonBindingsActive_.resize(size); 240 this->joyStickButtonsDown_ .resize(size); 241 this->joySticks_ .resize(size); 242 for (int j = 0; j < size; j++) 243 { 244 bindingsJoyStickButtonPress_ [j].resize(numberOfJoyStickButtons_s); 245 bindingsJoyStickButtonRelease_[j].resize(numberOfJoyStickButtons_s); 246 bindingsJoyStickButtonHold_ [j].resize(numberOfJoyStickButtons_s); 247 for (int i = 0; i < numberOfJoyStickButtons_s; i++) 248 { 249 this->bindingsJoyStickButtonPress_ [j][i] = ""; 250 this->bindingsJoyStickButtonRelease_[j][i] = ""; 251 this->bindingsJoyStickButtonHold_ [j][i] = ""; 252 } 253 } 254 } 255 256 /** 257 @brief Loads the key and button bindings. 258 */ 259 bool InputManager::_loadBindings() 260 { 261 CCOUT(ORX_DEBUG) << "Loading key bindings..." << std::endl; 262 263 // clear all the bindings at first. 264 _clearBindings(); 265 266 // TODO: Insert the code to load the bindings from file. 267 this->bindingsKeyPress_[OIS::KC_NUMPADENTER] = "activateConsole"; 268 this->bindingsKeyPress_[OIS::KC_ESCAPE] = "exit"; 269 this->bindingsKeyHold_[OIS::KC_U] = "exec disco.txt"; 270 271 CCOUT(ORX_DEBUG) << "Loading key bindings done." << std::endl; 272 return true; 273 } 274 275 /** 276 @brief Overwrites all bindings with "" 277 */ 278 void InputManager::_clearBindings() 279 { 280 for (int i = 0; i < numberOfKeys_s; i++) 281 { 282 this->bindingsKeyPress_ [i] = ""; 283 this->bindingsKeyRelease_[i] = ""; 284 this->bindingsKeyHold_ [i] = ""; 285 } 286 for (int i = 0; i < numberOfMouseButtons_s; i++) 287 { 288 this->bindingsMouseButtonPress_ [i] = ""; 289 this->bindingsMouseButtonRelease_[i] = ""; 290 this->bindingsMouseButtonHold_ [i] = ""; 291 } 292 for (unsigned int j = 0; j < joySticks_.size(); j++) 293 { 294 for (int i = 0; i < numberOfJoyStickButtons_s; i++) 295 { 296 this->bindingsJoyStickButtonPress_ [j][i] = ""; 297 this->bindingsJoyStickButtonRelease_[j][i] = ""; 298 this->bindingsJoyStickButtonHold_ [j][i] = ""; 299 } 300 } 261 return false; 262 } 263 return success; 301 264 } 302 265 … … 310 273 if (state_ != IS_UNINIT) 311 274 { 312 this->listenersKeyActive_.clear();313 this->listenersMouseActive_.clear();314 this->listenersJoySticksActive_.clear();315 this->listenersKey_.clear();316 this->listenersMouse_.clear();317 this->listenersJoySticks_.clear();318 319 this->keysDown_.clear();320 this->mouseButtonsDown_.clear();321 322 _clearBindings();323 324 if (keyboard_)325 inputSystem_->destroyInputObject(keyboard_);326 keyboard_ = 0;327 328 if (mouse_)329 inputSystem_->destroyInputObject(mouse_);330 mouse_ = 0;331 332 if (joySticks_.size() > 0)333 {334 for (unsigned int i = 0; i < joySticks_.size(); i++)335 {336 if (joySticks_[i] != 0)337 inputSystem_->destroyInputObject(joySticks_[i]);338 }339 _setNumberOfJoysticks(0);340 }341 342 if (inputSystem_)343 OIS::InputManager::destroyInputSystem(inputSystem_);344 inputSystem_ = 0;345 346 275 if (listenersKey_.find("buffer") != listenersKey_.end()) 347 276 delete listenersKey_["buffer"]; 348 277 349 this->state_ = IS_UNINIT; 278 if (listenersKey_.find("keybinder") != listenersKey_.end()) 279 delete listenersKey_["keybinder"]; 280 281 listenersKeyActive_.clear(); 282 listenersMouseActive_.clear(); 283 listenersJoySticksActive_.clear(); 284 listenersKey_.clear(); 285 listenersMouse_.clear(); 286 listenersJoySticks_.clear(); 287 288 keysDown_.clear(); 289 mouseButtonsDown_.clear(); 290 joySticksButtonsDown_.clear(); 291 292 _destroyKeyboard(); 293 _destroyMouse(); 294 _destroyJoySticks(); 295 296 // inputSystem_ can never be 0, or else the code is mistaken 297 OIS::InputManager::destroyInputSystem(inputSystem_); 298 inputSystem_ = 0; 299 300 state_ = IS_UNINIT; 350 301 } 351 302 else … … 355 306 } 356 307 357 358 // ############################### 359 // ### Interface Methods ### 360 // ############################### 308 /** 309 @brief Destroys the keyboard and sets it to 0. 310 */ 311 void InputManager::_destroyKeyboard() 312 { 313 if (keyboard_) 314 // inputSystem_ can never be 0, or else the code is mistaken 315 inputSystem_->destroyInputObject(keyboard_); 316 keyboard_ = 0; 317 CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl; 318 } 319 320 /** 321 @brief Destroys the mouse and sets it to 0. 322 */ 323 void InputManager::_destroyMouse() 324 { 325 if (mouse_) 326 // inputSystem_ can never be 0, or else the code is mistaken 327 inputSystem_->destroyInputObject(mouse_); 328 mouse_ = 0; 329 CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl; 330 } 331 332 /** 333 @brief Destroys all the joy sticks and resizes the lists to 0. 334 */ 335 void InputManager::_destroyJoySticks() 336 { 337 if (joySticks_.size() > 0) 338 { 339 // note: inputSystem_ can never be 0, or else the code is mistaken 340 for (std::map<int, OIS::JoyStick*>::iterator itstick = joySticks_.begin(); itstick != joySticks_.end(); itstick++) 341 { 342 if ((*itstick).second != 0) 343 inputSystem_->destroyInputObject((*itstick).second); 344 } 345 joySticks_.clear(); 346 } 347 CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl; 348 } 349 350 351 // ################################# 352 // ### Private Interface Methods ### 353 // ################################# 354 // ################################# 361 355 362 356 /** … … 372 366 if (state_ != stateRequest_) 373 367 { 374 switch (stateRequest_) 375 { 376 case IS_NORMAL: 377 this->listenersKeyActive_.clear(); 378 this->listenersMouseActive_.clear(); 379 this->listenersJoySticksActive_.clear(); 380 this->bKeyBindingsActive_ = true; 381 this->bMouseButtonBindingsActive_ = true; 382 for (unsigned int i = 0; i < joySticks_.size(); i++) 383 this->bJoyStickButtonBindingsActive_[i] = true; 384 break; 385 386 case IS_GUI: 387 // FIXME: do stuff 388 break; 389 390 case IS_CONSOLE: 391 this->listenersKeyActive_.clear(); 392 this->listenersMouseActive_.clear(); 393 this->listenersJoySticksActive_.clear(); 394 this->bKeyBindingsActive_ = false; 395 this->bMouseButtonBindingsActive_ = true; 396 for (unsigned int i = 0; i < joySticks_.size(); i++) 397 this->bJoyStickButtonBindingsActive_[i] = true; 398 if (listenersKey_.find("buffer") != listenersKey_.end()) 399 listenersKeyActive_.push_back(listenersKey_["buffer"]); 400 else 368 if (stateRequest_ != IS_CUSTOM) 369 { 370 listenersKeyActive_.clear(); 371 listenersMouseActive_.clear(); 372 listenersJoySticksActive_.clear(); 373 374 switch (stateRequest_) 401 375 { 402 // someone fiddled with the InputBuffer 403 CCOUT(2) << "Error: Cannot redirect input to console, InputBuffer instance missing." << std::endl; 404 this->bKeyBindingsActive_ = true; 376 case IS_NORMAL: 377 // normal play mode 378 if (listenersKey_.find("keybinder") != listenersKey_.end()) 379 listenersKeyActive_.push_back(listenersKey_["keybinder"]); 380 if (listenersMouse_.find("keybinder") != listenersMouse_.end()) 381 listenersMouseActive_.push_back(listenersMouse_["keybinder"]); 382 if (listenersJoySticks_.find("keybinder") != listenersJoySticks_.end()) 383 { 384 for (std::map<int, OIS::JoyStick*>::const_iterator it = joySticks_.begin(); 385 it != joySticks_.end(); it++) 386 listenersJoySticksActive_[(*it).first].push_back(listenersJoySticks_["keybinder"]); 387 } 388 break; 389 390 case IS_GUI: 391 // FIXME: do stuff 392 break; 393 394 case IS_CONSOLE: 395 if (listenersMouse_.find("keybinder") != listenersMouse_.end()) 396 listenersMouseActive_.push_back(listenersMouse_["keybinder"]); 397 if (listenersJoySticks_.find("keybinder") != listenersJoySticks_.end()) 398 { 399 for (std::map<int, OIS::JoyStick*>::const_iterator it = joySticks_.begin(); 400 it != joySticks_.end(); it++) 401 listenersJoySticksActive_[(*it).first].push_back(listenersJoySticks_["keybinder"]); 402 } 403 404 if (listenersKey_.find("buffer") != listenersKey_.end()) 405 listenersKeyActive_.push_back(listenersKey_["buffer"]); 406 else 407 { 408 // someone fiddled with the InputBuffer 409 CCOUT(2) << "Error: Cannot redirect input to console, InputBuffer instance missing." << std::endl; 410 if (listenersKey_.find("keybinder") != listenersKey_.end()) 411 listenersKeyActive_.push_back(listenersKey_["keybinder"]); 412 else 413 // this is bad 414 CCOUT(2) << "Error: Cannot reactivate key binder: not found!" << std::endl; 415 } 416 break; 417 418 default: 419 break; 405 420 } 406 break; 407 408 case IS_NONE: 409 this->listenersKeyActive_.clear(); 410 this->listenersMouseActive_.clear(); 411 this->listenersJoySticksActive_.clear(); 412 this->bKeyBindingsActive_ = false; 413 this->bMouseButtonBindingsActive_ = false; 414 for (unsigned int i = 0; i < joySticks_.size(); i++) 415 this->bJoyStickButtonBindingsActive_[i] = false; 416 break; 417 418 case IS_CUSTOM: 419 // don't do anything 420 break; 421 422 case IS_UNINIT: 423 // this can't happen 424 break; 425 } 426 state_ = stateRequest_; 421 state_ = stateRequest_; 422 } 427 423 } 428 424 … … 432 428 if (keyboard_) 433 429 keyboard_->capture(); 434 } 435 436 /*void InputManager::_setDefaultState() 437 { 438 this->listenersKeyActive_.clear(); 439 this->listenersMouseActive_.clear(); 440 this->listenersJoyStickActive_.clear(); 441 this->bKeyBindingsActive_ = true; 442 this->bMouseButtonBindingsActive_ = true; 443 this->bJoyStickButtonBindingsActive_ = true; 444 }*/ 430 431 432 // call all the listeners for the held key events 433 for (std::list<OIS::KeyCode>::const_iterator itKey = keysDown_.begin(); 434 itKey != keysDown_.end(); itKey++) 435 { 436 OIS::KeyEvent keyArg(keyboard_, *itKey, 0); 437 for (std::list<KeyHandler*>::const_iterator itKeyHandler = listenersKeyActive_.begin(); 438 itKeyHandler != listenersKeyActive_.end(); itKeyHandler++) 439 { 440 (*itKeyHandler)->keyHeld(keyArg); 441 } 442 } 443 444 // call all the listeners for the held mouse button events 445 for (std::list<OIS::MouseButtonID>::const_iterator itMouseButton = mouseButtonsDown_.begin(); 446 itMouseButton != mouseButtonsDown_.end(); itMouseButton++) 447 { 448 OIS::MouseEvent mouseButtonArg(mouse_, mouse_->getMouseState()); 449 for (std::list<MouseHandler*>::const_iterator itMouseHandler = listenersMouseActive_.begin(); 450 itMouseHandler != listenersMouseActive_.end(); itMouseHandler++) 451 { 452 (*itMouseHandler)->mouseHeld(mouseButtonArg, *itMouseButton); 453 } 454 } 455 456 // call all the listeners for the held joy stick button events 457 for (std::map<int, std::list <int> >::const_iterator itJoyStick = joySticksButtonsDown_.begin(); 458 itJoyStick != joySticksButtonsDown_.end(); itJoyStick++) 459 { 460 int id = (*itJoyStick).first; 461 for (std::list<int>::const_iterator itJoyStickButton = (*itJoyStick).second.begin(); 462 itJoyStickButton != (*itJoyStick).second.end(); itJoyStickButton++) 463 { 464 OIS::JoyStickEvent joyStickButtonArg(joySticks_[id], joySticks_[id]->getJoyStickState()); 465 for (std::list<JoyStickHandler*>::const_iterator itJoyStickHandler = listenersJoySticksActive_[id].begin(); 466 itJoyStickHandler != listenersJoySticksActive_[id].end(); itJoyStickHandler++) 467 { 468 (*itJoyStickHandler)->buttonHeld(joyStickButtonArg, *itJoyStickButton); 469 } 470 } 471 } 472 } 445 473 446 474 … … 451 479 bool InputManager::keyPressed(const OIS::KeyEvent &e) 452 480 { 453 this->keysDown_.push_back(e.key); 454 455 if (this->bKeyBindingsActive_) 456 { 457 // find the appropriate key binding 458 std::string cmdStr = bindingsKeyPress_[int(e.key)]; 459 if (cmdStr != "") 460 { 461 CommandExecutor::execute(cmdStr); 462 COUT(3) << "Executing command: " << cmdStr << std::endl; 463 } 464 } 465 else 466 { 467 for (std::list<OIS::KeyListener*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++) 468 (*it)->keyPressed(e); 469 } 481 // check whether the key is already in the list (can happen when focus was lost) 482 for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++) 483 { 484 if (*it == e.key) 485 { 486 keysDown_.erase(it); 487 break; 488 } 489 } 490 keysDown_.push_back(e.key); 491 492 for (std::list<KeyHandler*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++) 493 (*it)->keyPressed(e); 494 470 495 return true; 471 496 } … … 487 512 } 488 513 489 if (this->bKeyBindingsActive_) 490 { 491 // find the appropriate key binding 492 std::string cmdStr = bindingsKeyRelease_[int(e.key)]; 493 if (cmdStr != "") 494 { 495 CommandExecutor::execute(cmdStr); 496 COUT(3) << "Executing command: " << cmdStr << std::endl; 497 } 498 } 499 else 500 { 501 for (std::list<OIS::KeyListener*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++) 502 (*it)->keyReleased(e); 503 } 514 for (std::list<KeyHandler*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++) 515 (*it)->keyReleased(e); 516 504 517 return true; 505 518 } … … 511 524 bool InputManager::mouseMoved(const OIS::MouseEvent &e) 512 525 { 526 for (std::list<MouseHandler*>::const_iterator it = listenersMouseActive_.begin(); it != listenersMouseActive_.end(); it++) 527 (*it)->mouseMoved(e); 528 513 529 return true; 514 530 } … … 521 537 bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) 522 538 { 539 // check whether the button is already in the list (can happen when focus was lost) 540 for (std::list<OIS::MouseButtonID>::iterator it = mouseButtonsDown_.begin(); it != mouseButtonsDown_.end(); it++) 541 { 542 if (*it == id) 543 { 544 mouseButtonsDown_.erase(it); 545 break; 546 } 547 } 548 mouseButtonsDown_.push_back(id); 549 550 for (std::list<MouseHandler*>::const_iterator it = listenersMouseActive_.begin(); it != listenersMouseActive_.end(); it++) 551 (*it)->mousePressed(e, id); 552 523 553 return true; 524 554 } … … 531 561 bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) 532 562 { 563 // remove the button from the keysDown_ list 564 for (std::list<OIS::MouseButtonID>::iterator it = mouseButtonsDown_.begin(); it != mouseButtonsDown_.end(); it++) 565 { 566 if (*it == id) 567 { 568 mouseButtonsDown_.erase(it); 569 break; 570 } 571 } 572 573 for (std::list<MouseHandler*>::const_iterator it = listenersMouseActive_.begin(); it != listenersMouseActive_.end(); it++) 574 (*it)->mouseReleased(e, id); 575 533 576 return true; 534 577 } … … 536 579 bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button) 537 580 { 581 int devID = arg.device->getID(); 582 583 // check whether the button is already in the list (can happen when focus was lost) 584 std::list<int>& buttonsDownList = joySticksButtonsDown_[devID]; 585 for (std::list<int>::iterator it = buttonsDownList.begin(); it != buttonsDownList.end(); it++) 586 { 587 if (*it == button) 588 { 589 buttonsDownList.erase(it); 590 break; 591 } 592 } 593 joySticksButtonsDown_[devID].push_back(button); 594 595 std::list<JoyStickHandler*>::iterator end = listenersJoySticksActive_[devID].end(); 596 for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++) 597 (*it)->buttonPressed(arg, button); 598 538 599 return true; 539 600 } … … 541 602 bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button) 542 603 { 604 int devID = arg.device->getID(); 605 606 // remove the button from the joySticksButtonsDown_ list 607 std::list<int>& buttonsDownList = joySticksButtonsDown_[devID]; 608 for (std::list<int>::iterator it = buttonsDownList.begin(); it != buttonsDownList.end(); it++) 609 { 610 if (*it == button) 611 { 612 buttonsDownList.erase(it); 613 break; 614 } 615 } 616 617 std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end(); 618 for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++) 619 (*it)->buttonReleased(arg, button); 620 543 621 return true; 544 622 } … … 546 624 bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis) 547 625 { 626 int devID = arg.device->getID(); 627 std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end(); 628 for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++) 629 (*it)->axisMoved(arg, axis); 630 548 631 return true; 549 632 } … … 551 634 bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id) 552 635 { 636 int devID = arg.device->getID(); 637 std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end(); 638 for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++) 639 (*it)->sliderMoved(arg, id); 640 553 641 return true; 554 642 } … … 556 644 bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id) 557 645 { 646 int devID = arg.device->getID(); 647 std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end(); 648 for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++) 649 (*it)->povMoved(arg, id); 650 558 651 return true; 559 652 } … … 563 656 // ### Static Interface Methods ### 564 657 // ################################ 658 // ################################ 659 660 bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight, 661 bool createKeyboard, bool createMouse, bool createJoySticks) 662 { 663 return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight, 664 createKeyboard, createMouse, createJoySticks); 665 } 666 667 bool InputManager::initialiseKeyboard() 668 { 669 return _getSingleton()._initialiseKeyboard(); 670 } 671 672 bool InputManager::initialiseMouse() 673 { 674 return _getSingleton()._initialiseMouse(); 675 } 676 677 bool InputManager::initialiseJoySticks() 678 { 679 return _getSingleton()._initialiseJoySticks(); 680 } 681 682 683 void InputManager::destroy() 684 { 685 _getSingleton()._destroy(); 686 } 687 688 void InputManager::destroyKeyboard() 689 { 690 return _getSingleton()._destroyKeyboard(); 691 } 692 693 void InputManager::destroyMouse() 694 { 695 return _getSingleton()._destroyMouse(); 696 } 697 698 void InputManager::destroyJoySticks() 699 { 700 return _getSingleton()._destroyJoySticks(); 701 } 702 565 703 566 704 /** … … 600 738 } 601 739 602 void InputManager::destroy() 603 { 604 _getSingleton()._destroy(); 605 } 606 607 bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight, 608 bool createKeyboard, bool createMouse, bool createJoySticks) 609 { 610 return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight, 611 createKeyboard, createMouse, createJoySticks); 612 } 613 614 /*bool InputManager::initialiseKeyboard() 615 { 616 } 617 618 bool InputManager::initialiseMouse() 619 { 620 } 621 622 bool InputManager::initialiseJoySticks() 623 { 624 }*/ 625 626 bool InputManager::addKeyListener(OIS::KeyListener *listener, const std::string& name) 740 741 // ###### KeyHandler ###### 742 743 /** 744 @brief Adds a new key listener. 745 @param listener Pointer to the listener object. 746 @param name Unique name of the listener. 747 @return True if added, false if name already existed. 748 */ 749 bool InputManager::addKeyListener(KeyHandler* listener, const std::string& name) 627 750 { 628 751 if (_getSingleton().listenersKey_.find(name) == _getSingleton().listenersKey_.end()) … … 635 758 } 636 759 760 /** 761 @brief Removes a Key Listener from the list. 762 @param name Unique name of the listener. 763 @return True if removal was successful, false if name was not found. 764 */ 637 765 bool InputManager::removeKeyListener(const std::string &name) 638 766 { 639 std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().listenersKey_.find(name); 767 disableKeyListener(name); 768 std::map<std::string, KeyHandler*>::iterator it = _getSingleton().listenersKey_.find(name); 640 769 if (it != _getSingleton().listenersKey_.end()) 641 770 { … … 647 776 } 648 777 649 OIS::KeyListener* InputManager::getKeyListener(const std::string& name) 650 { 651 std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().listenersKey_.find(name); 778 /** 779 @brief Returns the pointer to a listener. 780 @param name Unique name of the listener. 781 @return Pointer to the instance, 0 if name was not found. 782 */ 783 KeyHandler* InputManager::getKeyListener(const std::string& name) 784 { 785 std::map<std::string, KeyHandler*>::iterator it = _getSingleton().listenersKey_.find(name); 652 786 if (it != _getSingleton().listenersKey_.end()) 653 787 { … … 658 792 } 659 793 794 /** 795 @brief Enables a specific key listener that has already been added. 796 @param name Unique name of the listener. 797 @return False if name was not found, true otherwise. 798 */ 799 bool InputManager::enableKeyListener(const std::string& name) 800 { 801 // get pointer from the map with all stored listeners 802 std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().listenersKey_.find(name); 803 if (mapIt == _getSingleton().listenersKey_.end()) 804 return false; 805 // see whether the listener is already in the list 806 for (std::list<KeyHandler*>::iterator it = _getSingleton().listenersKeyActive_.begin(); 807 it != _getSingleton().listenersKeyActive_.end(); it++) 808 { 809 if ((*it) == (*mapIt).second) 810 return true; 811 } 812 _getSingleton().listenersKeyActive_.push_back((*mapIt).second); 813 return true; 814 } 815 816 /** 817 @brief Disables a specific key listener. 818 @param name Unique name of the listener. 819 @return False if name was not found, true otherwise. 820 */ 821 bool InputManager::disableKeyListener(const std::string &name) 822 { 823 // get pointer from the map with all stored listeners 824 std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().listenersKey_.find(name); 825 if (mapIt == _getSingleton().listenersKey_.end()) 826 return false; 827 // look for the listener in the list 828 for (std::list<KeyHandler*>::iterator it = _getSingleton().listenersKeyActive_.begin(); 829 it != _getSingleton().listenersKeyActive_.end(); it++) 830 { 831 if ((*it) == (*mapIt).second) 832 { 833 _getSingleton().listenersKeyActive_.erase(it); 834 return true; 835 } 836 } 837 return true; 838 } 839 840 841 // ###### MouseHandler ###### 842 /** 843 @brief Adds a new mouse listener. 844 @param listener Pointer to the listener object. 845 @param name Unique name of the listener. 846 @return True if added, false if name already existed. 847 */ 848 bool InputManager::addMouseListener(MouseHandler* listener, const std::string& name) 849 { 850 if (_getSingleton().listenersMouse_.find(name) == _getSingleton().listenersMouse_.end()) 851 { 852 _getSingleton().listenersMouse_[name] = listener; 853 return true; 854 } 855 else 856 return false; 857 } 858 859 /** 860 @brief Removes a Mouse Listener from the list. 861 @param name Unique name of the listener. 862 @return True if removal was successful, false if name was not found. 863 */ 864 bool InputManager::removeMouseListener(const std::string &name) 865 { 866 disableMouseListener(name); 867 std::map<std::string, MouseHandler*>::iterator it = _getSingleton().listenersMouse_.find(name); 868 if (it != _getSingleton().listenersMouse_.end()) 869 { 870 _getSingleton().listenersMouse_.erase(it); 871 return true; 872 } 873 else 874 return false; 875 } 876 877 /** 878 @brief Returns the pointer to a listener. 879 @param name Unique name of the listener. 880 @return Pointer to the instance, 0 if name was not found. 881 */ 882 MouseHandler* InputManager::getMouseListener(const std::string& name) 883 { 884 std::map<std::string, MouseHandler*>::iterator it = _getSingleton().listenersMouse_.find(name); 885 if (it != _getSingleton().listenersMouse_.end()) 886 { 887 return (*it).second; 888 } 889 else 890 return 0; 891 } 892 893 /** 894 @brief Enables a specific mouse listener that has already been added. 895 @param name Unique name of the listener. 896 @return False if name was not found, true otherwise. 897 */ 898 bool InputManager::enableMouseListener(const std::string& name) 899 { 900 // get pointer from the map with all stored listeners 901 std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().listenersMouse_.find(name); 902 if (mapIt == _getSingleton().listenersMouse_.end()) 903 return false; 904 // see whether the listener is already in the list 905 for (std::list<MouseHandler*>::iterator it = _getSingleton().listenersMouseActive_.begin(); 906 it != _getSingleton().listenersMouseActive_.end(); it++) 907 { 908 if ((*it) == (*mapIt).second) 909 return true; 910 } 911 _getSingleton().listenersMouseActive_.push_back((*mapIt).second); 912 return true; 913 } 914 915 /** 916 @brief Disables a specific mouse listener. 917 @param name Unique name of the listener. 918 @return False if name was not found, true otherwise. 919 */ 920 bool InputManager::disableMouseListener(const std::string &name) 921 { 922 // get pointer from the map with all stored listeners 923 std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().listenersMouse_.find(name); 924 if (mapIt == _getSingleton().listenersMouse_.end()) 925 return false; 926 // look for the listener in the list 927 for (std::list<MouseHandler*>::iterator it = _getSingleton().listenersMouseActive_.begin(); 928 it != _getSingleton().listenersMouseActive_.end(); it++) 929 { 930 if ((*it) == (*mapIt).second) 931 { 932 _getSingleton().listenersMouseActive_.erase(it); 933 return true; 934 } 935 } 936 return true; 937 } 938 939 940 // ###### JoyStickHandler ###### 941 942 /** 943 @brief Adds a new joy stick listener. 944 @param listener Pointer to the listener object. 945 @param name Unique name of the listener. 946 @return True if added, false if name already existed. 947 */ 948 bool InputManager::addJoyStickListener(JoyStickHandler* listener, const std::string& name) 949 { 950 if (_getSingleton().listenersJoySticks_.find(name) == _getSingleton().listenersJoySticks_.end()) 951 { 952 _getSingleton().listenersJoySticks_[name] = listener; 953 return true; 954 } 955 else 956 return false; 957 } 958 959 /** 960 @brief Removes a JoyStick Listener from the list. 961 @param name Unique name of the listener. 962 @return True if removal was successful, false if name was not found. 963 */ 964 bool InputManager::removeJoyStickListener(const std::string &name) 965 { 966 for (std::map<int, OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin(); itstick != _getSingleton().joySticks_.end(); itstick++) 967 disableJoyStickListener(name, (*itstick).first); 968 969 std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticks_.find(name); 970 if (it != _getSingleton().listenersJoySticks_.end()) 971 { 972 _getSingleton().listenersJoySticks_.erase(it); 973 return true; 974 } 975 else 976 return false; 977 } 978 979 /** 980 @brief Returns the pointer to a listener. 981 @param name Unique name of the listener. 982 @return Pointer to the instance, 0 if name was not found. 983 */ 984 JoyStickHandler* InputManager::getJoyStickListener(const std::string& name) 985 { 986 std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticks_.find(name); 987 if (it != _getSingleton().listenersJoySticks_.end()) 988 { 989 return (*it).second; 990 } 991 else 992 return 0; 993 } 994 995 /** 996 @brief Enables a specific joy stick listener that has already been added. 997 @param name Unique name of the listener. 998 @return False if name or id was not found, true otherwise. 999 */ 1000 bool InputManager::enableJoyStickListener(const std::string& name, const int id) 1001 { 1002 // get pointer from the map with all stored listeners 1003 std::map<std::string, JoyStickHandler*>::const_iterator listenerIt = _getSingleton().listenersJoySticks_.find(name); 1004 if (listenerIt == _getSingleton().listenersJoySticks_.end()) 1005 return false; 1006 1007 // check for existance of the ID 1008 std::map<int, OIS::JoyStick*>::const_iterator joyStickIt = _getSingleton().joySticks_.find(id); 1009 if (joyStickIt == _getSingleton().joySticks_.end()) 1010 return false; 1011 1012 // see whether the listener is already in the list 1013 for (std::list<JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticksActive_[id].begin(); 1014 it != _getSingleton().listenersJoySticksActive_[id].end(); it++) 1015 { 1016 if ((*it) == (*listenerIt).second) 1017 return true; 1018 } 1019 _getSingleton().listenersJoySticksActive_[id].push_back((*listenerIt).second); 1020 return true; 1021 } 1022 1023 /** 1024 @brief Disables a specific joy stick listener. 1025 @param name Unique name of the listener. 1026 @return False if name or id was not found, true otherwise. 1027 */ 1028 bool InputManager::disableJoyStickListener(const std::string &name, int id) 1029 { 1030 // get pointer from the map with all stored listeners 1031 std::map<std::string, JoyStickHandler*>::const_iterator listenerIt = _getSingleton().listenersJoySticks_.find(name); 1032 if (listenerIt == _getSingleton().listenersJoySticks_.end()) 1033 return false; 1034 1035 // check for existance of the ID 1036 std::map<int, OIS::JoyStick*>::const_iterator joyStickIt = _getSingleton().joySticks_.find(id); 1037 if (joyStickIt == _getSingleton().joySticks_.end()) 1038 return false; 1039 1040 // look for the listener in the list 1041 for (std::list<JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticksActive_[id].begin(); 1042 it != _getSingleton().listenersJoySticksActive_[id].end(); it++) 1043 { 1044 if ((*it) == (*listenerIt).second) 1045 { 1046 _getSingleton().listenersJoySticksActive_[id].erase(it); 1047 return true; 1048 } 1049 } 1050 return true; 1051 } 1052 660 1053 } -
code/branches/input/src/core/InputManager.h
r1195 r1203 43 43 #include "ois/OIS.h" 44 44 #include "Tickable.h" 45 #include "InputEvent.h" 45 //#include "InputEvent.h" 46 #include "InputHandler.h" 46 47 47 48 namespace orxonox 48 49 { 50 class Mouse : public OIS::Mouse 51 { 52 }; 53 49 54 /** 50 55 @brief Captures and distributes mouse and keyboard input. … … 70 75 }; 71 76 72 public: // functions 73 // METHODS OF THE STATIC INTERFACE 74 77 public: // static functions 75 78 static bool initialise(size_t windowHnd, int windowWidth, int windowHeight, 76 79 bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false); 80 static bool initialiseKeyboard(); 81 static bool initialiseMouse(); 82 static bool initialiseJoySticks(); 83 77 84 static void destroy(); 85 static void destroyKeyboard(); 86 static void destroyMouse(); 87 static void destroyJoySticks(); 78 88 79 89 static void setWindowExtents(int width, int height); … … 85 95 static void setJoyStickButtonBindingState(bool bActive); 86 96 87 static bool addKeyListener( OIS::KeyListener* listener, const std::string& name);97 static bool addKeyListener(KeyHandler* listener, const std::string& name); 88 98 static bool removeKeyListener (const std::string& name); 89 99 static bool enableKeyListener (const std::string& name); 90 100 static bool disableKeyListener (const std::string& name); 91 101 static bool isKeyListenerActive(const std::string& name); 92 static OIS::KeyListener* getKeyListener(const std::string& name);102 static KeyHandler* getKeyListener(const std::string& name); 93 103 94 static bool addMouseListener( OIS::MouseListener* listener, const std::string& name);104 static bool addMouseListener(MouseHandler* listener, const std::string& name); 95 105 static bool removeMouseListener (const std::string& name); 96 106 static bool enableMouseListener (const std::string& name); 97 107 static bool disableMouseListener (const std::string& name); 98 108 static bool isMouseListenerActive(const std::string& name); 99 static OIS::MouseListener* getMouseListener(const std::string& name);109 static MouseHandler* getMouseListener(const std::string& name); 100 110 101 static bool addJoyStickListener( OIS::JoyStickListener* listener, const std::string& name);111 static bool addJoyStickListener(JoyStickHandler* listener, const std::string& name); 102 112 static bool removeJoyStickListener (const std::string& name); 103 static bool enableJoyStickListener (const std::string& name );104 static bool disableJoyStickListener (const std::string& name );113 static bool enableJoyStickListener (const std::string& name, const int id); 114 static bool disableJoyStickListener (const std::string& name, const int id); 105 115 static bool isJoyStickListenerActive(const std::string& name); 106 static OIS::JoyStickListener* getJoyStickListener(const std::string& name);116 static JoyStickHandler* getJoyStickListener(const std::string& name); 107 117 108 118 // Temporary solutions. Will be removed soon! … … 118 128 // Intenal methods 119 129 bool _initialise(size_t, int, int, bool, bool, bool); 120 void _initialiseKeyboard(); 121 void _initialiseMouse(); 122 void _initialiseJoySticks(); 130 bool _initialiseKeyboard(); 131 bool _initialiseMouse(); 132 bool _initialiseJoySticks(); 133 123 134 void _destroy(); 124 //void _setDefaultState(); 125 bool _loadBindings(); 126 void _clearBindings(); 127 void _setNumberOfJoysticks(int size); 135 void _destroyKeyboard(); 136 void _destroyMouse(); 137 void _destroyJoySticks(); 138 139 //void _setNumberOfJoysticks(int size); 128 140 129 141 void tick(float dt); … … 145 157 146 158 private: // variables 147 OIS::InputManager* inputSystem_; //!< OIS input manager148 OIS::Keyboard* keyboard_; //!< OIS mouse149 OIS::Mouse* mouse_; //!< OIS keyboard150 std:: vector<OIS::JoyStick*> joySticks_;//!< OIS joy sticks159 OIS::InputManager* inputSystem_; //!< OIS input manager 160 OIS::Keyboard* keyboard_; //!< OIS mouse 161 OIS::Mouse* mouse_; //!< OIS keyboard 162 std::map<int, OIS::JoyStick*> joySticks_; //!< OIS joy sticks 151 163 152 164 InputState state_; 153 165 InputState stateRequest_; 154 166 155 bool bKeyBindingsActive_;156 bool bMouseButtonBindingsActive_;157 std:: vector<bool> bJoyStickButtonBindingsActive_;167 std::map<std::string, KeyHandler*> listenersKey_; 168 std::map<std::string, MouseHandler*> listenersMouse_; 169 std::map<std::string, JoyStickHandler*> listenersJoySticks_; 158 170 159 std::map<std::string, OIS::KeyListener*> listenersKey_; 160 std::map<std::string, OIS::MouseListener*> listenersMouse_; 161 std::map<std::string, OIS::JoyStickListener*> listenersJoySticks_; 162 163 std::list<OIS::KeyListener*> listenersKeyActive_; 164 std::list<OIS::MouseListener*> listenersMouseActive_; 165 std::list<OIS::JoyStickListener*> listenersJoySticksActive_; 171 std::list<KeyHandler*> listenersKeyActive_; 172 std::list<MouseHandler*> listenersMouseActive_; 173 std::map< int, std::list<JoyStickHandler*> > listenersJoySticksActive_; 166 174 167 175 std::list<OIS::KeyCode> keysDown_; 168 176 std::list<OIS::MouseButtonID> mouseButtonsDown_; 169 std::vector< std::list<int> > joyStickButtonsDown_; 170 171 172 /** denotes the maximum number of different keys there are in OIS. 173 256 should be ok since the highest number in the OIS enum is 237. */ 174 static const int numberOfKeys_s = 256; 175 //! Array of input events for every pressed key 176 std::string bindingsKeyPress_ [numberOfKeys_s]; 177 //! Array of input events for every released key 178 std::string bindingsKeyRelease_[numberOfKeys_s]; 179 //! Array of input events for every held key 180 std::string bindingsKeyHold_ [numberOfKeys_s]; 181 182 /** denotes the maximum number of different buttons there are in OIS. 183 16 should be ok since the highest number in the OIS enum is 7. */ 184 static const int numberOfMouseButtons_s = 16; 185 //! Array of input events for every pressed mouse button 186 std::string bindingsMouseButtonPress_ [numberOfMouseButtons_s]; 187 //! Array of input events for every released mouse button 188 std::string bindingsMouseButtonRelease_[numberOfMouseButtons_s]; 189 //! Array of input events for every held mouse button 190 std::string bindingsMouseButtonHold_ [numberOfMouseButtons_s]; 191 192 /** denotes the maximum number of different buttons there are in OIS. 193 32 should be ok. */ 194 static const int numberOfJoyStickButtons_s = 32; 195 std::vector< std::vector< std::string > > bindingsJoyStickButtonPress_; 196 std::vector< std::vector< std::string > > bindingsJoyStickButtonRelease_; 197 std::vector< std::vector< std::string > > bindingsJoyStickButtonHold_; 177 std::map< int, std::list<int> > joySticksButtonsDown_; 198 178 199 179 };
Note: See TracChangeset
for help on using the changeset viewer.