Changeset 3274 for code/branches/core4/src/core/input/JoyStick.cc
- Timestamp:
- Jul 12, 2009, 4:12:04 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core4/src/core/input/JoyStick.cc
r3270 r3274 26 26 * 27 27 */ 28 29 /**30 @file31 @brief32 Implementation of the JoyStick wrapper class.33 */34 28 35 29 #include "JoyStick.h" … … 54 48 void loadCalibration(std::vector<int>& list, const std::string& sectionName, const std::string& valueName, size_t size, int defaultValue); 55 49 56 JoyStick::JoyStick(const std::vector<InputState*>& states, unsigned int id) 57 : id_(id) 58 , bCalibrating_(false) 59 , inputStates_(states) 50 JoyStick::JoyStick(unsigned int id) 51 : super(id) 60 52 { 61 53 RegisterRootObject(JoyStick); 62 54 this->setConfigValues(); 63 64 OIS::InputManager* system = InputManager::getInstance().getInputSystem(); 65 oisJoyStick_ = static_cast<OIS::JoyStick*>(system->createInputObject(OIS::OISJoyStick, true)); 66 oisJoyStick_->setEventCallback(this); 55 // Initialise POV and Slider states 56 this->clearBuffersImpl(); 67 57 68 58 idString_ = "JoyStick_"; 69 std::string name = ois JoyStick_->vendor();59 std::string name = oisDevice_->vendor(); 70 60 replaceCharacters(name, ' ', '_'); 71 61 idString_ += name + "_"; 72 idString_ += multi_cast<std::string>(ois JoyStick_->getNumberOfComponents(OIS::OIS_Button)) + "_";73 idString_ += multi_cast<std::string>(ois JoyStick_->getNumberOfComponents(OIS::OIS_Axis)) + "_";74 idString_ += multi_cast<std::string>(ois JoyStick_->getNumberOfComponents(OIS::OIS_Slider)) + "_";75 idString_ += multi_cast<std::string>(ois JoyStick_->getNumberOfComponents(OIS::OIS_POV));76 //idString_ += multi_cast<std::string>(ois JoyStick_->getNumberOfComponents(OIS::OIS_Vector3));62 idString_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Button)) + "_"; 63 idString_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Axis)) + "_"; 64 idString_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Slider)) + "_"; 65 idString_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_POV)); 66 //idString_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Vector3)); 77 67 78 68 if (InputManager::getInstance().checkJoyStickID(idString_) == false) 79 69 { 80 70 // Make the ID unique for this execution time. 81 idString_ += "_" + multi_cast<std::string>( id_);71 idString_ += "_" + multi_cast<std::string>(this->getDeviceID()); 82 72 } 83 73 … … 85 75 86 76 // Load calibration 87 size_t axes = sliderAxes_s + static_cast<size_t>(ois JoyStick_->getNumberOfComponents(OIS::OIS_Axis));77 size_t axes = sliderAxes_s + static_cast<size_t>(oisDevice_->getNumberOfComponents(OIS::OIS_Axis)); 88 78 loadCalibration(configMinValues_, idString_, "MinValue", axes, -32768); 89 79 loadCalibration(configMaxValues_, idString_, "MaxValue", axes, 32768); … … 92 82 } 93 83 94 JoyStick::~JoyStick() 95 { 96 try 97 { 98 OIS::InputManager* system = InputManager::getInstance().getInputSystem(); 99 system->destroyInputObject(oisJoyStick_); 100 } 101 catch (...) 102 { 103 COUT(1) << "Joy stick destruction failed! Potential resource leak!" << std::endl; 104 } 105 } 106 107 /** 108 @brief 109 Callback for the joy stick calibration config file. @see setConfigValues. 110 */ 84 //!< Callback for the joy stick calibration config file. 111 85 void JoyStick::calibrationFileCallback() 112 86 { … … 114 88 } 115 89 116 /**117 @brief118 Sets the configurable values.119 */120 90 void JoyStick::setConfigValues() 121 91 { … … 140 110 // fill the rest with default values 141 111 for (unsigned int i = configValueVectorSize; i < size; ++i) 142 {143 112 list[i] = defaultValue; 144 } 145 } 146 147 void JoyStick::startCalibration() 148 { 149 bCalibrating_ = true; 150 113 } 114 115 //! Called by InputDevice when calibration mode has started 116 void JoyStick::calibrationStarted() 117 { 151 118 // Set initial values 152 119 BOOST_FOREACH(int& minVal, configMinValues_) … … 158 125 } 159 126 160 void JoyStick::stopCalibration() 127 //! Called by InputDevice when calibration mode has stopped 128 void JoyStick::calibrationStopped() 161 129 { 162 130 // Get the middle positions now … … 164 132 for (unsigned int i = 0; i < sliderAxes_s/2; ++i) 165 133 { 166 configZeroValues_[iAxis++] = ois JoyStick_->getJoyStickState().mSliders[i].abX;167 configZeroValues_[iAxis++] = ois JoyStick_->getJoyStickState().mSliders[i].abY;168 } 169 // Note: joyStick MiddleValues_[iJoyStick] was already correctly resised in loadCalibration()170 assert(ois JoyStick_->getJoyStickState().mAxes.size() == configZeroValues_.size() - sliderAxes_s);134 configZeroValues_[iAxis++] = oisDevice_->getJoyStickState().mSliders[i].abX; 135 configZeroValues_[iAxis++] = oisDevice_->getJoyStickState().mSliders[i].abY; 136 } 137 // Note: joyStickZeroValues_[iJoyStick] was already correctly resised in loadCalibration() 138 assert(oisDevice_->getJoyStickState().mAxes.size() == configZeroValues_.size() - sliderAxes_s); 171 139 for (unsigned int i = 0; i < configZeroValues_.size() - sliderAxes_s; ++i) 172 configZeroValues_[iAxis++] = ois JoyStick_->getJoyStickState().mAxes[i].abs;140 configZeroValues_[iAxis++] = oisDevice_->getJoyStickState().mAxes[i].abs; 173 141 174 142 for (unsigned int i = 0; i < configMinValues_.size(); ++i) … … 192 160 193 161 this->evaluateCalibration(); 194 195 bCalibrating_ = false; 196 } 197 162 } 163 164 //! Evaluates the accumulated values during calibration 198 165 void JoyStick::evaluateCalibration() 199 166 { … … 206 173 } 207 174 208 void JoyStick::clearBuffer()209 {210 pressedButtons_.clear();175 // TODO: What do we really need to reset here? 176 void JoyStick::clearBuffersImpl() 177 { 211 178 for (int j = 0; j < 4; ++j) 212 179 { … … 217 184 } 218 185 219 220 // ###### Events ###### 221 222 void JoyStick::capture() 223 { 224 oisJoyStick_->capture(); 225 226 // call all the handlers for the held joy stick button events 227 for (unsigned int iButton = 0; iButton < pressedButtons_.size(); iButton++) 228 { 229 BOOST_FOREACH(InputState* state, inputStates_) 230 state->joyStickButtonHeld(id_, pressedButtons_[iButton]); 231 } 232 } 233 234 bool JoyStick::buttonPressed(const OIS::JoyStickEvent &arg, int button) 235 { 236 // check whether the button already is in the list (can happen when focus was lost) 237 unsigned int iButton = 0; 238 while (iButton < pressedButtons_.size() && pressedButtons_[iButton] != button) 239 iButton++; 240 if (iButton == pressedButtons_.size()) 241 pressedButtons_.push_back(static_cast<JoyStickButtonCode::ByEnum>(button)); 242 243 BOOST_FOREACH(InputState* state, inputStates_) 244 state->joyStickButtonPressed(id_, static_cast<JoyStickButtonCode::ByEnum>(button)); 245 246 return true; 247 } 248 249 bool JoyStick::buttonReleased(const OIS::JoyStickEvent &arg, int button) 250 { 251 // remove the button from the pressedButtons_ list 252 for (unsigned int iButton = 0; iButton < pressedButtons_.size(); iButton++) 253 { 254 if (static_cast<int>(pressedButtons_[iButton]) == button) 255 { 256 pressedButtons_.erase(pressedButtons_.begin() + iButton); 257 break; 258 } 259 } 260 261 BOOST_FOREACH(InputState* state, inputStates_) 262 state->joyStickButtonPressed(id_, static_cast<JoyStickButtonCode::ByEnum>(button)); 263 264 return true; 265 } 266 267 /** 268 @brief 269 Calls the states for a particular axis with our enumeration. 270 Used by OIS sliders and OIS axes. 271 */ 186 //! Generic method to forward axis events 272 187 void JoyStick::fireAxis(int axis, int value) 273 188 { 274 if ( bCalibrating_)189 if (this->isCalibrating()) 275 190 { 276 191 if (value < configMinValues_[axis]) … … 288 203 289 204 BOOST_FOREACH(InputState* state, inputStates_) 290 state->joyStickAxisMoved(id_, axis, fValue); 291 } 292 } 293 205 state->joyStickAxisMoved(this->getDeviceID(), axis, fValue); 206 } 207 } 208 209 //! OIS joy stick axis event handler 294 210 bool JoyStick::axisMoved(const OIS::JoyStickEvent &arg, int axis) 295 211 { … … 300 216 } 301 217 218 //! A Slider always has an X and an Y direction! 302 219 bool JoyStick::sliderMoved(const OIS::JoyStickEvent &arg, int id) 303 220 { … … 310 227 } 311 228 229 //! A POV is the big button that can point in all directions (but only in one at once) 312 230 bool JoyStick::povMoved(const OIS::JoyStickEvent &arg, int id) 313 231 {
Note: See TracChangeset
for help on using the changeset viewer.