Changeset 3286
- Timestamp:
- Jul 13, 2009, 8:54:43 PM (15 years ago)
- Location:
- code/branches/core4/src/core/input
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core4/src/core/input/InputDevice.h
r3285 r3286 29 29 /** 30 30 @file 31 @brief 31 @brief 32 Implementation of InputDevice and InputDeviceTemplated 32 33 */ 33 34 … … 38 39 39 40 #include <vector> 40 #include <boost/foreach.hpp>41 41 #include <ois/OISInputManager.h> 42 42 43 43 #include "util/Debug.h" 44 44 #include "core/Clock.h" 45 // TODO: Try to remove this46 #include "InputManager.h"47 45 #include "InputState.h" 48 46 … … 51 49 /** 52 50 @brief 51 Abstract base class for all input devices (mouse, keyboard and joy sticks). 52 53 It provides common virtual functions to be used by the InputManager. 53 54 */ 54 55 class InputDevice 55 56 { 56 friend class InputManager;57 58 57 public: 59 InputDevice(unsigned int id) : bCalibrating_(false), id_(id) { } 58 //! Only resets the members 59 InputDevice(unsigned int id) : bCalibrating_(false), deviceID_(id) { } 60 60 virtual ~InputDevice() { } 61 virtual std::string getClassName() = 0; 61 //! Returns the device class (derived) name as string 62 virtual std::string getClassName() const = 0; 63 //! Updates the device which should in turn distribute events 62 64 virtual void update(const Clock& time) = 0; 65 //! Clear all button related buffers 63 66 virtual void clearBuffers() = 0; 64 67 68 //! Start calibrating (only useful for joy sticks) 65 69 void startCalibration() 66 70 { … … 69 73 } 70 74 75 //! Stop calibrating and evaluate the data (only useful for joy sticks) 71 76 void stopCalibration() 72 77 { … … 75 80 } 76 81 77 unsigned int getDeviceID() { return this->id_; } 82 //! Returns a reference to the internal input state vector. Use with care! 83 std::vector<InputState*>& getStateListRef() { return this->inputStates_; } 84 //! Returns the ID of the device (the same as in InputDeviceEnumerator for mouse and keyboard) 85 unsigned int getDeviceID() const { return this->deviceID_; } 86 //! Tells whether the device is in calibration mode 87 bool isCalibrating() const { return bCalibrating_; } 78 88 79 89 protected: 90 //! To be ovrridden by the subclass 80 91 virtual void calibrationStarted() { } 92 //! To be ovrridden by the subclass 81 93 virtual void calibrationStopped() { } 82 bool isCalibrating() { return bCalibrating_; } 83 84 // InputState handling 94 95 //! List of all input states that receive events from this device 85 96 std::vector<InputState*> inputStates_; 86 97 87 98 private: 88 InputDevice(const InputDevice& rhs); 89 90 std::vector<InputState*>& getStateListRef() 91 { 92 return this->inputStates_; 93 } 94 95 bool bCalibrating_; 96 const unsigned int id_; 99 InputDevice(const InputDevice& rhs); //!< Don't use! 100 101 bool bCalibrating_; //!< Whether the device is in calibration mode 102 const unsigned int deviceID_; //!< ID of the device (the same as in InputDeviceEnumerator for mouse and keyboard) 97 103 }; 98 104 99 105 /** 100 106 @brief 107 Heavily templated base class for all three input devices. 108 109 The purpose of this class is not to provide an interface but rather 110 to reduce code redundancy. This concerns device creation and destruction 111 as well as common code for button events (press, release, hold). 112 113 In order to derive from this class you have to supply it with a struct 114 as template parameter that contains the necessary type traits. 101 115 */ 102 116 template <class Traits> … … 110 124 111 125 public: 112 InputDeviceTemplated(unsigned int id) 126 //! Creates the OIS device 127 InputDeviceTemplated(unsigned int id, OIS::InputManager* oisInputManager) 113 128 : InputDevice(id) 114 { 115 OIS::InputManager* system = InputManager::getInstance().getOISInputManager(); 116 oisDevice_ = static_cast<OISDeviceClass*>(system->createInputObject(OISDeviceValue, true)); 129 , oisInputManager_(oisInputManager) 130 { 131 oisDevice_ = static_cast<OISDeviceClass*>(oisInputManager_->createInputObject(OISDeviceValue, true)); 132 // Note: after the static_cast here, the casted this pointer becomes 133 // invalid right until the subclass has been constructed! 117 134 oisDevice_->setEventCallback(static_cast<DeviceClass*>(this)); 118 135 COUT(4) << "Instantiated a " << this->getClassName() << std::endl; 119 136 } 120 137 138 //! Destroys the OIS device 121 139 virtual ~InputDeviceTemplated() 122 140 { 123 141 try 124 142 { 125 InputManager::getInstance().getOISInputManager()->destroyInputObject(oisDevice_);143 oisInputManager_->destroyInputObject(oisDevice_); 126 144 } 127 145 catch (...) … … 131 149 } 132 150 133 OISDeviceClass* getOISDevice() 134 { 135 return this->oisDevice_; 136 } 137 138 std::string getClassName() 139 { 140 return DeviceClass::getClassNameImpl(); 141 } 142 151 //! Captures OIS events (which then get distributed to the derived class) and creates the button held events 143 152 void update(const Clock& time) 144 153 { 145 154 oisDevice_->capture(); 146 155 147 if (!this->isCalibrating()) 148 { 149 // Call all the states with the held button event 150 for (unsigned int iB = 0; iB < pressedButtons_.size(); ++iB) 151 for (unsigned int iS = 0; iS < inputStates_.size(); ++iS) 152 inputStates_[iS]->buttonEvent<ButtonEvent::THold, Traits>( 153 this->getDeviceID(), static_cast<DeviceClass*>(this)->getButtonEventArg(pressedButtons_[iB])); 154 155 // Call states with device update events 156 for (unsigned int i = 0; i < inputStates_.size(); ++i) 157 inputStates_[i]->update(time.getDeltaTime(), this->getDeviceID()); 158 } 156 // Call all the states with the held button event 157 for (unsigned int iB = 0; iB < pressedButtons_.size(); ++iB) 158 for (unsigned int iS = 0; iS < inputStates_.size(); ++iS) 159 inputStates_[iS]->buttonEvent<ButtonEvent::THold, Traits>( 160 this->getDeviceID(), static_cast<DeviceClass*>(this)->getButtonEventArg(pressedButtons_[iB])); 161 162 // Call states with device update events 163 for (unsigned int i = 0; i < inputStates_.size(); ++i) 164 inputStates_[i]->update(time.getDeltaTime(), this->getDeviceID()); 159 165 160 166 static_cast<DeviceClass*>(this)->updateImpl(time); 161 167 } 162 168 169 //! Clears the list of pressed buttons and calls the derived class's method 163 170 void clearBuffers() 164 171 { … … 167 174 } 168 175 176 // Returns a pointer to the OIS device 177 OISDeviceClass* getOISDevice() { return this->oisDevice_; } 178 // Returns the name of the derived class as string 179 std::string getClassName() const { return DeviceClass::getClassNameImpl(); } 180 169 181 protected: 182 //! Common code for all button pressed events (updates pressed buttons list and calls the input states) 170 183 void buttonPressed(ButtonTypeParam button) 171 184 { … … 184 197 } 185 198 199 //! Common code for all button released events (updates pressed buttons list and calls the input states) 186 200 void buttonReleased(ButtonTypeParam button) 187 201 { … … 201 215 } 202 216 217 //! Managed pointer to the OIS device 203 218 OISDeviceClass* oisDevice_; 204 219 205 220 private: 206 void clearBuffersImpl() { } //!< Fallback dummy function for static polymorphism 207 void updateImpl(const Clock& time) { } //!< Fallback dummy function for static polymorphism 221 //!< Fallback dummy function for static polymorphism 222 void clearBuffersImpl() { } 223 //!< Fallback dummy function for static polymorphism 224 void updateImpl(const Clock& time) { } 208 225 //!< Fallback dummy function for static polymorphism 209 226 ButtonType& getButtonEventArg(ButtonType& button) { return button; } 210 227 211 std::vector<ButtonType> pressedButtons_; 228 std::vector<ButtonType> pressedButtons_; //!< List of all buttons that are currently pressed down 229 OIS::InputManager* oisInputManager_; //!< Pointer to the OIS InputManager that can create and destroy devices 212 230 }; 213 231 } -
code/branches/core4/src/core/input/InputHandler.h
r3285 r3286 26 26 * 27 27 */ 28 29 /**30 @file31 @brief32 Declarations of various interface classes for the input management.33 */34 28 35 29 #ifndef _InputHandler_H__ … … 100 94 /** 101 95 @brief 96 Base class for all input handlers like KeyBinder, InputBuffer, etc. 97 98 Derive from this class if you wish to receive input events. 99 But keep in mind that this is pointless wihtout first having an InputState. 100 @note 101 The definitions for the button events with the weird arguments are simply 102 to avoid redunant code in the input devices. 102 103 */ 103 104 class _CoreExport InputHandler … … 140 141 virtual void allDevicesUpdated(float dt) { } 141 142 143 //! Use this input handler if you want to occupy a device in an input state. 142 144 static InputHandler EMPTY; 143 145 }; -
code/branches/core4/src/core/input/InputManager.cc
r3281 r3286 201 201 202 202 if (oisInputManager_->getNumberOfDevices(OIS::OISKeyboard) > 0) 203 devices_[InputDeviceEnumerator::Keyboard] = new Keyboard(InputDeviceEnumerator::Keyboard );203 devices_[InputDeviceEnumerator::Keyboard] = new Keyboard(InputDeviceEnumerator::Keyboard, oisInputManager_); 204 204 else 205 205 ThrowException(InitialisationFailed, "InputManager: No keyboard found, cannot proceed!"); … … 232 232 try 233 233 { 234 devices_[InputDeviceEnumerator::Mouse] = new Mouse(InputDeviceEnumerator::Mouse, windowWidth, windowHeight);234 devices_[InputDeviceEnumerator::Mouse] = new Mouse(InputDeviceEnumerator::Mouse, oisInputManager_, windowWidth, windowHeight); 235 235 } 236 236 catch (const OIS::Exception& ex) … … 251 251 try 252 252 { 253 devices_.push_back(new JoyStick(InputDeviceEnumerator::FirstJoyStick + i ));253 devices_.push_back(new JoyStick(InputDeviceEnumerator::FirstJoyStick + i, oisInputManager_)); 254 254 } 255 255 catch (std::exception ex) … … 262 262 for (ObjectList<JoyStickQuantityListener>::iterator it = ObjectList<JoyStickQuantityListener>::begin(); it; ++it) 263 263 it->JoyStickQuantityChanged(devices_.size() - InputDeviceEnumerator::FirstJoyStick); 264 }265 266 bool InputManager::checkJoyStickID(const std::string& idString) const267 {268 for (unsigned int i = InputDeviceEnumerator::FirstJoyStick; i < devices_.size(); ++i)269 if (static_cast<JoyStick*>(devices_[i])->getIDString() == idString)270 return false;271 return true;272 264 } 273 265 … … 565 557 // Clear buffers to prevent button hold events 566 558 this->clearBuffers(); 559 560 COUT(0) << "Calibration has been stored." << std::endl; 567 561 } 568 562 -
code/branches/core4/src/core/input/InputManager.h
r3281 r3286 161 161 //! Sets the the name of the command used by the KeyDetector as callback. 162 162 void setKeyDetectorCallback(const std::string& command); 163 /**164 @brief165 Checks whether there is already a joy stick with the given ID string.166 @return167 Returns true if ID is ok (unique), false otherwise.168 */169 bool checkJoyStickID(const std::string& idString) const;170 163 //! Returns the number of joy stick that have been created since the c'tor or last call to reload(). 171 164 unsigned int getJoyStickQuantity() const -
code/branches/core4/src/core/input/InputPrereqs.h
r3285 r3286 30 30 @file 31 31 @brief 32 Declarations of various interface classes for the input management. 32 Declarations of all key/button/axis code enumeration and string literals 33 and an input device enumeration. 33 34 */ 34 35 … … 52 53 const unsigned int numberOfKeys = 0xEE; // 238 53 54 54 // note: KeyCode comments were directly copied from OISKeyboard.h55 //! Key codes as enumeration 55 56 enum ByEnum 56 57 { … … 202 203 }; 203 204 204 // Names as string. Has no real linkage!205 //! Key codes as strings 205 206 const char* const ByString[] = 206 207 { … … 307 308 const unsigned int numberOfButtons = 8; 308 309 310 //! Mouse button codes as enumeration 309 311 enum ByEnum 310 312 { … … 319 321 }; 320 322 321 // Names as string. Has no real linkage!323 // Mouse button codes as strings 322 324 const char* const ByString[] = 323 325 { … … 337 339 const unsigned int numberOfAxes = 2; 338 340 341 // Mouse axis codes as enumeration 339 342 enum ByEnum 340 343 { … … 343 346 }; 344 347 345 // Names as string. Has no real linkage!348 // Mouse axis codes as strings 346 349 const char* const ByString[] = 347 350 { … … 356 359 const unsigned int numberOfButtons = 64; 357 360 361 // Joy stick button codes as enumeration 358 362 enum ByEnum 359 363 { … … 380 384 }; 381 385 382 // Names as string. Has no real linkage!386 // Joy stick button codes as strings 383 387 const char* const ByString[] = 384 388 { … … 406 410 const unsigned int numberOfAxes = 24; 407 411 412 // Joy stick axis codes as enumeration 408 413 enum ByEnum 409 414 { … … 416 421 }; 417 422 418 // Names as string. Has no real linkage!423 // Joy stick axis codes as strings 419 424 const char* const ByString[] = 420 425 { … … 435 440 namespace InputDeviceEnumerator 436 441 { 442 //! Used to access the devices in an array 437 443 enum Value 438 444 { -
code/branches/core4/src/core/input/JoyStick.cc
r3274 r3286 30 30 31 31 #include <ois/OISJoyStick.h> 32 #include <ois/OISInputManager.h>33 32 #include <boost/foreach.hpp> 34 33 … … 38 37 #include "util/Convert.h" 39 38 #include "InputState.h" 40 #include "InputManager.h"41 39 42 40 namespace orxonox 43 41 { 44 /** 45 @brief 46 Helper function that loads the config value vector of one coefficient 47 */ 42 //! Helper function that loads the config value vector of one coefficient 48 43 void loadCalibration(std::vector<int>& list, const std::string& sectionName, const std::string& valueName, size_t size, int defaultValue); 49 44 50 JoyStick::JoyStick(unsigned int id) 51 : super(id) 45 std::vector<std::string> JoyStick::idStrings_s; 46 47 JoyStick::JoyStick(unsigned int id, OIS::InputManager* oisInputManager) 48 : super(id, oisInputManager) 52 49 { 53 50 RegisterRootObject(JoyStick); … … 66 63 //idString_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Vector3)); 67 64 68 if (InputManager::getInstance().checkJoyStickID(idString_) == false) 69 { 70 // Make the ID unique for this execution time. 71 idString_ += "_" + multi_cast<std::string>(this->getDeviceID()); 65 66 BOOST_FOREACH(std::string& idString, idStrings_s) 67 { 68 if (idString_ == idString) 69 { 70 // Make the ID unique for this execution time. 71 idString_ += "_" + multi_cast<std::string>(this->getDeviceID()); 72 break; 73 } 72 74 } 73 75 … … 82 84 } 83 85 84 //! <Callback for the joy stick calibration config file.86 //! Callback for the joy stick calibration config file. 85 87 void JoyStick::calibrationFileCallback() 86 88 { … … 173 175 } 174 176 175 // TODO: What do we really need to reset here?177 //! Resets the pov states 176 178 void JoyStick::clearBuffersImpl() 177 179 { 178 180 for (int j = 0; j < 4; ++j) 179 {180 181 povStates_[j] = 0; 181 sliderStates_[j][0] = 0;182 sliderStates_[j][1] = 0;183 }184 182 } 185 183 -
code/branches/core4/src/core/input/JoyStick.h
r3285 r3286 38 38 namespace orxonox 39 39 { 40 //! Template parameter collection for the base class 40 41 struct JoyStickTraits 41 42 { … … 66 67 public: 67 68 //! Assigns a generated ID string and loads the calibration (if present) 68 JoyStick(unsigned int id );69 JoyStick(unsigned int id, OIS::InputManager* oisInputManager); 69 70 ~JoyStick() { } 70 71 void setConfigValues(); 71 72 72 //! Returns the generated (from the number of knobs and the device name) ID string73 //! Returns the ID string generated from the number of knobs and the device name 73 74 const std::string& getIDString() const { return this->idString_; } 74 75 … … 99 100 bool sliderMoved (const OIS::JoyStickEvent &arg, int id); 100 101 bool povMoved (const OIS::JoyStickEvent &arg, int id); 101 //! <OIS event handler (don't remove that because of OIS version issues!)102 //! OIS event handler (don't remove that because of OIS version issues!) 102 103 bool vector3Moved (const OIS::JoyStickEvent &arg, int id) { return true; } 103 104 105 //! Returns the class name as string 104 106 static std::string getClassNameImpl() { return "JoyStick"; } 105 107 … … 120 122 std::string calibrationFilename_; //!< Joy stick calibration ini filename 121 123 124 //! Contains a list of all ID strings to avoid duplicates 125 static std::vector<std::string> idStrings_s; 126 122 127 //!< Maximum number of slider axes 123 128 static const unsigned int sliderAxes_s = 8; -
code/branches/core4/src/core/input/JoyStickQuantityListener.cc
r3274 r3286 27 27 */ 28 28 29 /**30 @file31 @brief32 Implementation of the JoyStickQuantityListener class.33 */34 35 29 #include "JoyStickQuantityListener.h" 36 30 #include "core/CoreIncludes.h" -
code/branches/core4/src/core/input/JoyStickQuantityListener.h
r3274 r3286 40 40 namespace orxonox 41 41 { 42 //! Derive from this class to get informed when joy sticks get added/removed 42 43 class _CoreExport JoyStickQuantityListener : virtual public OrxonoxClass 43 44 { … … 46 47 virtual ~JoyStickQuantityListener() { } 47 48 49 //! Called when joy sticks get added/removed 48 50 virtual void JoyStickQuantityChanged(unsigned int value) = 0; 49 51 }; -
code/branches/core4/src/core/input/Keyboard.h
r3285 r3286 31 31 32 32 #include "InputPrereqs.h" 33 33 34 #include "InputHandler.h" 34 35 #include "InputDevice.h" … … 36 37 namespace orxonox 37 38 { 39 //! Template parameter collection for the base class 38 40 struct KeyboardTraits 39 41 { … … 61 63 62 64 public: 63 Keyboard(unsigned int id) : super(id), modifiers_(0) { } 65 //! Only resets the keyboard modifiers. Initialising is done in the base class. 66 Keyboard(unsigned int id, OIS::InputManager* oisInputManager) : super(id, oisInputManager), modifiers_(0) { } 64 67 ~Keyboard() { } 65 68 66 69 private: 67 // TODO: Do we need to reset the modifiers?68 void clearBuffersImpl() { }70 //! Resets the keyboard modifiers 71 void clearBuffersImpl() { this->modifiers_ = 0; } 69 72 //! Translates the KeyHandle to a KeyEvent 70 KeyEvent& getButtonEventArg(KeyEvent& button) { button.setModifiers(modifiers_); return button; } 73 KeyEvent& getButtonEventArg(KeyEvent& button) 74 { 75 button.setModifiers(modifiers_); 76 return button; 77 } 71 78 72 79 bool keyPressed(const OIS::KeyEvent& arg); 73 80 bool keyReleased(const OIS::KeyEvent& arg); 74 81 82 //! Returns the class name as string 75 83 static std::string getClassNameImpl() { return "Keyboard"; } 76 84 -
code/branches/core4/src/core/input/Mouse.cc
r3276 r3286 30 30 31 31 #include <ois/OISMouse.h> 32 #include <boost/foreach.hpp>33 32 #include "InputState.h" 34 33 #include "core/ConsoleCommand.h" … … 41 40 namespace orxonox 42 41 { 43 Mouse::Mouse(unsigned int id, unsigned int windowWidth, unsigned int windowHeight)44 : super(id )42 Mouse::Mouse(unsigned int id, OIS::InputManager* oisInputManager, unsigned int windowWidth, unsigned int windowHeight) 43 : super(id, oisInputManager) 45 44 { 46 45 this->setMouseClipping(windowWidth, windowHeight); … … 74 73 IntVector2 rel(e.state.X.rel, e.state.Y.rel); 75 74 IntVector2 clippingSize(e.state.width, e.state.height); 76 BOOST_FOREACH(InputState* state, inputStates_)77 state->mouseMoved(abs, rel, clippingSize);75 for (unsigned int i = 0; i < inputStates_.size(); ++i) 76 inputStates_[i]->mouseMoved(abs, rel, clippingSize); 78 77 } 79 78 … … 81 80 if (e.state.Z.rel != 0) 82 81 { 83 BOOST_FOREACH(InputState* state, inputStates_)84 state->mouseScrolled(e.state.Z.abs, e.state.Z.rel);82 for (unsigned int i = 0; i < inputStates_.size(); ++i) 83 inputStates_[i]->mouseScrolled(e.state.Z.abs, e.state.Z.rel); 85 84 } 86 85 -
code/branches/core4/src/core/input/Mouse.h
r3285 r3286 35 35 namespace orxonox 36 36 { 37 //! Template parameter collection for the base class 37 38 struct MouseTraits 38 39 { … … 58 59 59 60 public: 60 Mouse(unsigned int id, unsigned int windowWidth, unsigned int windowHeight); 61 //! Only sets the clipping size. Initialising is done in the base class. 62 Mouse(unsigned int id, OIS::InputManager* oisInputManager, unsigned int windowWidth, unsigned int windowHeight); 61 63 ~Mouse() { } 62 64 … … 68 70 */ 69 71 void setMouseClipping(unsigned int width, unsigned int height); 72 // Returns the width of the mouse window 70 73 unsigned int getClippingWidth() const; 74 // Returns the height of the mouse window 71 75 unsigned int getClippingHeight() const; 72 76 … … 77 81 #ifdef ORXONOX_PLATFORM_LINUX 78 82 // HACK! 83 // TODO: Make this a feature rather than a hack 79 84 static void grabMouse(); 80 85 static void ungrabMouse(); … … 82 87 83 88 private: 84 // TODO: Do we need to reset the mouse position?85 void clearBuffersImpl() { }86 87 89 //! OIS event handler 88 90 bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id) … … 101 103 bool mouseMoved(const OIS::MouseEvent &arg); 102 104 105 // Returns the class name as string 103 106 static std::string getClassNameImpl() { return "Mouse"; } 104 107
Note: See TracChangeset
for help on using the changeset viewer.