Changeset 2544
- Timestamp:
- Dec 28, 2008, 11:57:27 PM (16 years ago)
- Location:
- code/branches/presentation/src/core/input
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation/src/core/input/InputManager.cc
r2543 r2544 111 111 , keyDetector_(0) 112 112 , calibratorCallbackBuffer_(0) 113 , bCalibrating_(false)114 113 , keyboardModifiers_(0) 115 114 { … … 118 117 assert(singletonRef_s == 0); 119 118 singletonRef_s = this; 119 120 setConfigValues(); 121 } 122 123 /** 124 @brief 125 Sets the configurable values. 126 */ 127 void InputManager::setConfigValues() 128 { 129 SetConfigValue(calibrationFilename_, "joystick_calibration.ini") 130 .description("Ini filename for the the joy stick calibration data.") 131 .callback(this, &InputManager::_calibrationFileCallback); 132 } 133 134 /** 135 @brief 136 Callback for the joy stick calibration config file. @see setConfigValues. 137 */ 138 void InputManager::_calibrationFileCallback() 139 { 140 ConfigFileManager::getInstance().setFilename(ConfigFileType::JoyStickCalibration, calibrationFilename_); 120 141 } 121 142 … … 174 195 _initialiseJoySticks(); 175 196 // Do this anyway to also inform everything when a joystick was detached. 176 _configure NumberOfJoySticks();197 _configureJoySticks(); 177 198 178 199 // Set mouse/joystick region … … 182 203 // clear all buffers 183 204 _clearBuffers(); 184 185 // load joy stick calibration186 setConfigValues();187 205 188 206 internalState_ |= OISReady; … … 334 352 /** 335 353 @brief 354 Helper function that loads the config value vector of one coefficient 355 */ 356 void loadCalibration(std::vector<int>& list, const std::string& sectionName, const std::string& valueName, size_t size, int defaultValue) 357 { 358 list.resize(size); 359 unsigned int configValueVectorSize = ConfigFileManager::getInstance().getVectorSize(ConfigFileType::JoyStickCalibration, sectionName, valueName); 360 if (configValueVectorSize > size) 361 configValueVectorSize = size; 362 363 for (unsigned int i = 0; i < configValueVectorSize; ++i) 364 { 365 list[i] = omni_cast<int>(ConfigFileManager::getInstance().getValue( 366 ConfigFileType::JoyStickCalibration, sectionName, valueName, i, omni_cast<std::string>(defaultValue), false)); 367 } 368 369 // fill the rest with default values 370 for (unsigned int i = configValueVectorSize; i < size; ++i) 371 { 372 list[i] = defaultValue; 373 } 374 } 375 376 /** 377 @brief 336 378 Sets the size of all the different lists that are dependent on the number 337 of joy stick devices created .379 of joy stick devices created and loads the joy stick calibration. 338 380 @remarks 339 381 No matter whether there are a mouse and/or keyboard, they will always 340 382 occupy 2 places in the device number dependent lists. 341 383 */ 342 void InputManager::_configure NumberOfJoySticks()384 void InputManager::_configureJoySticks() 343 385 { 344 386 joySticksSize_ = joySticks_.size(); 345 devicesNum_ = 2 + joySticksSize_; 387 devicesNum_ = 2 + joySticksSize_; 388 joyStickIDs_ .resize(joySticksSize_); 346 389 joyStickButtonsDown_ .resize(joySticksSize_); 347 390 povStates_ .resize(joySticksSize_); 348 391 sliderStates_ .resize(joySticksSize_); 349 joySticksCalibration_.resize(joySticksSize_); 392 joyStickMinValues_ .resize(joySticksSize_); 393 joyStickMaxValues_ .resize(joySticksSize_); 394 joyStickMiddleValues_.resize(joySticksSize_); 395 joyStickCalibrations_.resize(joySticksSize_); 350 396 351 397 for (unsigned int iJoyStick = 0; iJoyStick < joySticksSize_; iJoyStick++) 352 398 { 353 // reset the calibration with default values 354 for (unsigned int i = 0; i < 24; i++) 355 { 356 joySticksCalibration_[iJoyStick].negativeCoeff[i] = 1.0f/32767.0f; 357 joySticksCalibration_[iJoyStick].positiveCoeff[i] = 1.0f/32768.0f; 358 joySticksCalibration_[iJoyStick].zeroStates[i] = 0; 359 } 360 } 399 // Generate some sort of execution unique id per joy stick 400 std::string id = "JoyStick_"; 401 id += omni_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Button)) + "_"; 402 id += omni_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Axis)) + "_"; 403 id += omni_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Slider)) + "_"; 404 id += omni_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_POV)) + "_"; 405 id += omni_cast<std::string>(joySticks_[iJoyStick]->getNumberOfComponents(OIS::OIS_Vector3)) + "_"; 406 id += joySticks_[iJoyStick]->vendor(); 407 for (unsigned int i = 0; i < iJoyStick; ++i) 408 { 409 if (id == joyStickIDs_[i]) 410 { 411 // Two joysticks are probably equal --> add the index as well 412 id += "_" + omni_cast<std::string>(iJoyStick); 413 } 414 } 415 joyStickIDs_[iJoyStick] = id; 416 417 size_t axes = sliderAxes + (size_t)this->joySticks_[i]->getNumberOfComponents(OIS::OIS_Axis); 418 loadCalibration(joyStickMinValues_[iJoyStick], id, "MinValue", axes, -32768); 419 loadCalibration(joyStickMaxValues_[iJoyStick], id, "MaxValue", axes, 32768); 420 loadCalibration(joyStickMiddleValues_[iJoyStick], id, "MiddleValue", axes, 0); 421 } 422 423 _evaluateCalibration(); 361 424 362 425 // state management … … 379 442 } 380 443 381 /** 382 @brief 383 Sets the configurable values. 384 This mainly concerns joy stick calibrations. 385 */ 386 void InputManager::setConfigValues() 387 { 388 if (joySticksSize_ > 0) 389 { 390 std::vector<double> coeffPos; 391 std::vector<double> coeffNeg; 392 std::vector<int> zero; 393 coeffPos.resize(24); 394 coeffNeg.resize(24); 395 zero.resize(24); 396 for (unsigned int i = 0; i < 24; i++) 397 { 398 coeffPos[i] = 1.0f/32767.0f; 399 coeffNeg[i] = 1.0f/32768.0f; 400 zero[i] = 0; 401 } 402 403 ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos"); 404 if (!cont) 405 { 406 cont = new ConfigValueContainer(ConfigFileType::Settings, getIdentifier(), getIdentifier()->getName(), "CoeffPos", coeffPos); 407 getIdentifier()->addConfigValueContainer("CoeffPos", cont); 408 } 409 cont->getValue(&coeffPos, this); 410 411 cont = getIdentifier()->getConfigValueContainer("CoeffNeg"); 412 if (!cont) 413 { 414 cont = new ConfigValueContainer(ConfigFileType::Settings, getIdentifier(), getIdentifier()->getName(), "CoeffNeg", coeffNeg); 415 getIdentifier()->addConfigValueContainer("CoeffNeg", cont); 416 } 417 cont->getValue(&coeffNeg, this); 418 419 cont = getIdentifier()->getConfigValueContainer("Zero"); 420 if (!cont) 421 { 422 cont = new ConfigValueContainer(ConfigFileType::Settings, getIdentifier(), getIdentifier()->getName(), "Zero", zero); 423 getIdentifier()->addConfigValueContainer("Zero", cont); 424 } 425 cont->getValue(&zero, this); 426 427 // copy values to our own variables 428 for (unsigned int i = 0; i < 24; i++) 429 { 430 joySticksCalibration_[0].positiveCoeff[i] = coeffPos[i]; 431 joySticksCalibration_[0].negativeCoeff[i] = coeffNeg[i]; 432 joySticksCalibration_[0].zeroStates[i] = zero[i]; 433 } 434 } 435 } 436 444 void InputManager::_evaluateCalibration() 445 { 446 for (unsigned int iJoyStick = 0; iJoyStick < this->joySticksSize_; ++iJoyStick) 447 { 448 for (unsigned int i = 0; i < this->joyStickMinValues_[iJoyStick].size(); i++) 449 { 450 this->joyStickCalibrations_[iJoyStick].middleValue[i] = this->joyStickMiddleValues_[iJoyStick][i]; 451 this->joyStickCalibrations_[iJoyStick].negativeCoeff[i] = - 1.0f / (this->joyStickMinValues_[iJoyStick][i] - this->joyStickMiddleValues_[iJoyStick][i]); 452 this->joyStickCalibrations_[iJoyStick].positiveCoeff[i] = 1.0f / (this->joyStickMaxValues_[iJoyStick][i] - this->joyStickMiddleValues_[iJoyStick][i]); 453 } 454 } 455 } 456 457 void InputManager::_startCalibration() 458 { 459 for (unsigned int iJoyStick = 0; iJoyStick < this->joySticksSize_; ++iJoyStick) 460 { 461 // Set initial values 462 for (unsigned int i = 0; i < this->joyStickMinValues_[iJoyStick].size(); ++i) 463 this->joyStickMinValues_[iJoyStick][i] = INT_MAX; 464 for (unsigned int i = 0; i < this->joyStickMaxValues_[iJoyStick].size(); ++i) 465 this->joyStickMaxValues_[iJoyStick][i] = INT_MIN; 466 for (unsigned int i = 0; i < this->joyStickMiddleValues_[iJoyStick].size(); ++i) 467 this->joyStickMiddleValues_[iJoyStick][i] = 0; 468 } 469 470 getInstance().internalState_ |= Calibrating; 471 getInstance().requestEnterState("calibrator"); 472 } 473 474 void InputManager::_completeCalibration() 475 { 476 for (unsigned int iJoyStick = 0; iJoyStick < this->joySticksSize_; ++iJoyStick) 477 { 478 // Get the middle positions now 479 unsigned int iAxis = 0; 480 for (unsigned int i = 0; i < sliderAxes/2; ++i) 481 { 482 this->joyStickMiddleValues_[iJoyStick][iAxis++] = this->joySticks_[iJoyStick]->getJoyStickState().mSliders[i].abX; 483 this->joyStickMiddleValues_[iJoyStick][iAxis++] = this->joySticks_[iJoyStick]->getJoyStickState().mSliders[i].abY; 484 } 485 // Note: joyStickMiddleValues_[iJoyStick] was already correctly resized in _configureJoySticks() 486 assert(joySticks_[iJoyStick]->getJoyStickState().mAxes.size() == joyStickMiddleValues_[iJoyStick].size() - sliderAxes); 487 for (unsigned int i = 0; i < joyStickMiddleValues_[iJoyStick].size() - sliderAxes; ++i) 488 { 489 this->joyStickMiddleValues_[iJoyStick][iAxis++] = this->joySticks_[iJoyStick]->getJoyStickState().mAxes[i].abs; 490 } 491 492 for (unsigned int i = 0; i < joyStickMinValues_[iJoyStick].size(); ++i) 493 { 494 // Minimum values 495 if (joyStickMinValues_[iJoyStick][i] == INT_MAX) 496 joyStickMinValues_[iJoyStick][i] = -32768; 497 ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration, 498 this->joyStickIDs_[iJoyStick], "MinValue", i, omni_cast<std::string>(joyStickMinValues_[iJoyStick][i]), false); 499 500 // Maximum values 501 if (joyStickMaxValues_[iJoyStick][i] == INT_MIN) 502 joyStickMaxValues_[iJoyStick][i] = 32767; 503 ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration, 504 this->joyStickIDs_[iJoyStick], "MaxValue", i, omni_cast<std::string>(joyStickMaxValues_[iJoyStick][i]), false); 505 506 // Middle values 507 ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration, 508 this->joyStickIDs_[iJoyStick], "MiddleValue", i, omni_cast<std::string>(joyStickMiddleValues_[iJoyStick][i]), false); 509 } 510 } 511 512 _evaluateCalibration(); 513 514 // restore old input state 515 requestLeaveState("calibrator"); 516 internalState_ &= ~Calibrating; 517 } 437 518 438 519 // ############################################################ … … 742 823 joySticks_[i]->capture(); 743 824 744 if (! bCalibrating_)825 if (!(internalState_ & Calibrating)) 745 826 { 746 827 // call all the handlers for the held key events … … 813 894 /** 814 895 @brief 815 Processes the accumultated data for the joy stick calibration. 816 */ 817 void InputManager::_completeCalibration() 818 { 819 for (unsigned int i = 0; i < 24; i++) 820 { 821 // positive coefficient 822 if (marginalsMax_[i] == INT_MIN) 823 marginalsMax_[i] = 32767; 824 // coefficients 825 if (marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i]) 826 { 827 joySticksCalibration_[0].positiveCoeff[i] 828 = 1.0f/(marginalsMax_[i] - joySticksCalibration_[0].zeroStates[i]); 829 } 830 else 831 joySticksCalibration_[0].positiveCoeff[i] = 1.0f; 832 833 // config value 834 ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer("CoeffPos"); 835 assert(cont); 836 cont->set(i, joySticksCalibration_[0].positiveCoeff[i]); 837 838 // negative coefficient 839 if (marginalsMin_[i] == INT_MAX) 840 marginalsMin_[i] = -32768; 841 // coefficients 842 if (marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i]) 843 { 844 joySticksCalibration_[0].negativeCoeff[i] = -1.0f 845 / (marginalsMin_[i] - joySticksCalibration_[0].zeroStates[i]); 846 } 847 else 848 joySticksCalibration_[0].negativeCoeff[i] = 1.0f; 849 // config value 850 cont = getIdentifier()->getConfigValueContainer("CoeffNeg"); 851 assert(cont); 852 cont->set(i, joySticksCalibration_[0].negativeCoeff[i]); 853 854 // zero states 855 if (i < 8) 856 { 857 if (!(i & 1)) 858 joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abX; 859 else 860 joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mSliders[i/2].abY; 861 } 862 else 863 { 864 if (i - 8 < joySticks_[0]->getJoyStickState().mAxes.size()) 865 joySticksCalibration_[0].zeroStates[i] = joySticks_[0]->getJoyStickState().mAxes[i - 8].abs; 866 else 867 joySticksCalibration_[0].zeroStates[i] = 0; 868 } 869 // config value 870 cont = getIdentifier()->getConfigValueContainer("Zero"); 871 assert(cont); 872 cont->set(i, joySticksCalibration_[0].zeroStates[i]); 873 } 874 875 // restore old input state 876 requestLeaveState("calibrator"); 877 bCalibrating_ = false; 878 } 879 896 Clears all buffers that store what keys/buttons are being pressed at the moment. 897 */ 880 898 void InputManager::clearBuffers() 881 899 { … … 1108 1126 void InputManager::_fireAxis(unsigned int iJoyStick, int axis, int value) 1109 1127 { 1110 if ( bCalibrating_)1111 { 1112 if (value > marginalsMax_[axis])1113 marginalsMax_[axis] = value;1114 if (value < marginalsMin_[axis])1115 marginalsMin_[axis] = value;1128 if (internalState_ & Calibrating) 1129 { 1130 if (value < joyStickMinValues_[iJoyStick][axis]) 1131 joyStickMinValues_[iJoyStick][axis] = value; 1132 if (value > joyStickMaxValues_[iJoyStick][axis]) 1133 joyStickMaxValues_[iJoyStick][axis] = value; 1116 1134 } 1117 1135 else 1118 1136 { 1119 float fValue = value - joyStick sCalibration_[iJoyStick].zeroStates[axis];1137 float fValue = value - joyStickCalibrations_[iJoyStick].middleValue[axis]; 1120 1138 if (fValue > 0.0f) 1121 fValue *= joyStick sCalibration_[iJoyStick].positiveCoeff[axis];1139 fValue *= joyStickCalibrations_[iJoyStick].positiveCoeff[axis]; 1122 1140 else 1123 fValue *= joyStick sCalibration_[iJoyStick].negativeCoeff[axis];1141 fValue *= joyStickCalibrations_[iJoyStick].negativeCoeff[axis]; 1124 1142 1125 1143 activeStatesTop_[2 + iJoyStick]->joyStickAxisMoved(iJoyStick, axis, fValue); … … 1399 1417 void InputManager::calibrate() 1400 1418 { 1401 getInstance().bCalibrating_ = true; 1402 getInstance().requestEnterState("calibrator"); 1419 COUT(0) << "Move all joy stick axes fully in all directions." << std::endl 1420 << "When done, put the axex in the middle position and press enter." << std::endl; 1421 1422 getInstance()._startCalibration(); 1403 1423 } 1404 1424 -
code/branches/presentation/src/core/input/InputManager.h
r2543 r2544 71 71 struct JoyStickCalibration 72 72 { 73 int zeroStates[24];73 int middleValue[24]; 74 74 float positiveCoeff[24]; 75 75 float negativeCoeff[24]; … … 146 146 void _initialiseMouse(); 147 147 void _initialiseJoySticks(); 148 void _configureNumberOfJoySticks(); 148 void _configureJoySticks(); 149 150 void _loadCalibration(); 151 void _startCalibration(); 152 void _completeCalibration(); 153 void _evaluateCalibration(); 149 154 150 155 void _destroyKeyboard(); … … 155 160 156 161 void _reload(bool joyStickSupport); 157 158 void _completeCalibration();159 162 160 163 void _fireAxis(unsigned int iJoyStick, int axis, int value); … … 179 182 180 183 void setConfigValues(); 184 void _calibrationFileCallback(); 181 185 182 186 private: // variables … … 186 190 std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks 187 191 unsigned int joySticksSize_; 192 std::vector<std::string> joyStickIDs_; //!< Execution unique identification strings for the joy sticks 188 193 unsigned int devicesNum_; 189 194 size_t windowHnd_; //!< Render window handle … … 208 213 209 214 // joystick calibration 210 //std::vector<int> marginalsMaxConfig_; 211 //std::vector<int> marginalsMinConfig_; 212 int marginalsMax_[24]; 213 int marginalsMin_[24]; 214 bool bCalibrated_; 215 bool bCalibrating_; 215 std::vector<std::vector<int> > joyStickMinValues_; 216 std::vector<std::vector<int> > joyStickMaxValues_; 217 std::vector<std::vector<int> > joyStickMiddleValues_; 218 std::vector<ConfigValueContainer*> calibrationConfigValueContainers_; 219 std::vector<JoyStickCalibration> joyStickCalibrations_; 216 220 217 221 unsigned int keyboardModifiers_; //!< Bit mask representing keyboard modifiers. 218 222 std::vector<POVStates> povStates_; //!< Keeps track of the joy stick POV states. 219 223 std::vector<SliderStates> sliderStates_; //!< Keeps track of the possibly two slider axes. 220 std::vector<JoyStickCalibration> joySticksCalibration_;221 224 222 225 std::vector<Key> keysDown_; … … 224 227 std::vector<std::vector<JoyStickButtonCode::ByEnum> > joyStickButtonsDown_; 225 228 229 // ConfigValues 230 std::string calibrationFilename_; //!< Joy stick calibration ini filename 226 231 227 232 static InputManager* singletonRef_s;
Note: See TracChangeset
for help on using the changeset viewer.