[3270] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Reto Grieder |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "JoyStick.h" |
---|
| 30 | |
---|
[3329] | 31 | #include <climits> |
---|
[3270] | 32 | #include <ois/OISJoyStick.h> |
---|
| 33 | |
---|
[6417] | 34 | #include "util/StringUtils.h" |
---|
[9667] | 35 | #include "core/config/ConfigFile.h" |
---|
| 36 | #include "core/config/ConfigFileManager.h" |
---|
| 37 | #include "core/config/ConfigValueIncludes.h" |
---|
[3270] | 38 | #include "core/CoreIncludes.h" |
---|
| 39 | #include "util/Convert.h" |
---|
| 40 | #include "InputState.h" |
---|
| 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[3327] | 44 | //! Helper function that loads the config value vector of one coefficient |
---|
[3270] | 45 | void loadCalibration(std::vector<int>& list, const std::string& sectionName, const std::string& valueName, size_t size, int defaultValue); |
---|
| 46 | |
---|
[3327] | 47 | std::vector<std::string> JoyStick::deviceNames_s; |
---|
| 48 | |
---|
[10624] | 49 | RegisterAbstractClass(JoyStick).inheritsFrom<Configurable>(); |
---|
| 50 | |
---|
[3327] | 51 | JoyStick::JoyStick(unsigned int id, OIS::InputManager* oisInputManager) |
---|
| 52 | : super(id, oisInputManager) |
---|
[3270] | 53 | { |
---|
[9667] | 54 | RegisterObject(JoyStick); |
---|
[3270] | 55 | this->setConfigValues(); |
---|
[3327] | 56 | // Initialise POV and Slider states |
---|
| 57 | this->clearBuffersImpl(); |
---|
[3270] | 58 | |
---|
[3327] | 59 | // Generate unique name |
---|
| 60 | if (oisDevice_->vendor().empty()) |
---|
| 61 | deviceName_ = "Unknown_"; |
---|
| 62 | else |
---|
| 63 | { |
---|
| 64 | std::string name = oisDevice_->vendor(); |
---|
| 65 | replaceCharacters(name, ' ', '_'); |
---|
[6417] | 66 | deviceName_ = name + '_'; |
---|
[3327] | 67 | } |
---|
[6417] | 68 | deviceName_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Button)) + '_'; |
---|
| 69 | deviceName_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Axis)) + '_'; |
---|
| 70 | deviceName_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Slider)) + '_'; |
---|
[3327] | 71 | deviceName_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_POV)); |
---|
| 72 | //deviceName_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Vector3)); |
---|
[3270] | 73 | |
---|
[11071] | 74 | for (const std::string& idString : deviceNames_s) |
---|
[3270] | 75 | { |
---|
[3327] | 76 | if (deviceName_ == idString) |
---|
| 77 | { |
---|
| 78 | // Make the ID unique for this execution time. |
---|
[6417] | 79 | deviceName_ += '_' + multi_cast<std::string>(this->getDeviceName()); |
---|
[3327] | 80 | break; |
---|
| 81 | } |
---|
[3270] | 82 | } |
---|
| 83 | |
---|
[8858] | 84 | orxout(verbose, context::input) << "Created OIS joy stick with ID " << deviceName_ << endl; |
---|
[3270] | 85 | |
---|
| 86 | // Load calibration |
---|
[3327] | 87 | size_t axes = sliderAxes_s + static_cast<size_t>(oisDevice_->getNumberOfComponents(OIS::OIS_Axis)); |
---|
| 88 | loadCalibration(configMinValues_, deviceName_, "MinValue", axes, -32768); |
---|
| 89 | loadCalibration(configMaxValues_, deviceName_, "MaxValue", axes, 32768); |
---|
| 90 | loadCalibration(configZeroValues_, deviceName_, "ZeroValue", axes, 0); |
---|
[3270] | 91 | this->evaluateCalibration(); |
---|
| 92 | } |
---|
| 93 | |
---|
[3327] | 94 | //! Callback for the joy stick calibration config file. |
---|
[3270] | 95 | void JoyStick::calibrationFileCallback() |
---|
| 96 | { |
---|
| 97 | ConfigFileManager::getInstance().setFilename(ConfigFileType::JoyStickCalibration, calibrationFilename_); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void JoyStick::setConfigValues() |
---|
| 101 | { |
---|
| 102 | SetConfigValue(calibrationFilename_, "joystick_calibration.ini") |
---|
| 103 | .description("Ini filename for the the joy stick calibration data.") |
---|
| 104 | .callback(this, &JoyStick::calibrationFileCallback); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | void loadCalibration(std::vector<int>& list, const std::string& sectionName, const std::string& valueName, size_t size, int defaultValue) |
---|
| 108 | { |
---|
| 109 | list.resize(size); |
---|
[6536] | 110 | unsigned int configValueVectorSize = ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration)->getVectorSize(sectionName, valueName); |
---|
[3270] | 111 | if (configValueVectorSize > size) |
---|
| 112 | configValueVectorSize = size; |
---|
| 113 | |
---|
| 114 | for (unsigned int i = 0; i < configValueVectorSize; ++i) |
---|
| 115 | { |
---|
[6536] | 116 | list[i] = multi_cast<int>(ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration) |
---|
| 117 | ->getOrCreateValue(sectionName, valueName, i, multi_cast<std::string>(defaultValue), false)); |
---|
[3270] | 118 | } |
---|
| 119 | |
---|
| 120 | // fill the rest with default values |
---|
| 121 | for (unsigned int i = configValueVectorSize; i < size; ++i) |
---|
| 122 | list[i] = defaultValue; |
---|
| 123 | } |
---|
| 124 | |
---|
[3327] | 125 | //! Called by InputDevice when calibration mode has started |
---|
| 126 | void JoyStick::calibrationStarted() |
---|
[3270] | 127 | { |
---|
| 128 | // Set initial values |
---|
[11071] | 129 | for (int& minVal : configMinValues_) |
---|
[3270] | 130 | minVal = INT_MAX; |
---|
[11071] | 131 | for (int& minVal : configMaxValues_) |
---|
[3270] | 132 | minVal = INT_MIN; |
---|
[11071] | 133 | for (int& zeroVal : configZeroValues_) |
---|
[3270] | 134 | zeroVal = 0; |
---|
| 135 | } |
---|
| 136 | |
---|
[3327] | 137 | //! Called by InputDevice when calibration mode has stopped |
---|
| 138 | void JoyStick::calibrationStopped() |
---|
[3270] | 139 | { |
---|
| 140 | // Get the middle positions now |
---|
| 141 | unsigned int iAxis = 0; |
---|
| 142 | for (unsigned int i = 0; i < sliderAxes_s/2; ++i) |
---|
| 143 | { |
---|
[3327] | 144 | configZeroValues_[iAxis++] = oisDevice_->getJoyStickState().mSliders[i].abX; |
---|
| 145 | configZeroValues_[iAxis++] = oisDevice_->getJoyStickState().mSliders[i].abY; |
---|
[3270] | 146 | } |
---|
[3327] | 147 | // Note: joyStickZeroValues_[iJoyStick] was already correctly resised in loadCalibration() |
---|
| 148 | assert(oisDevice_->getJoyStickState().mAxes.size() == configZeroValues_.size() - sliderAxes_s); |
---|
[3270] | 149 | for (unsigned int i = 0; i < configZeroValues_.size() - sliderAxes_s; ++i) |
---|
[3327] | 150 | configZeroValues_[iAxis++] = oisDevice_->getJoyStickState().mAxes[i].abs; |
---|
[3270] | 151 | |
---|
| 152 | for (unsigned int i = 0; i < configMinValues_.size(); ++i) |
---|
| 153 | { |
---|
| 154 | // Minimum values |
---|
| 155 | if (configMinValues_[i] == INT_MAX) |
---|
| 156 | configMinValues_[i] = -32768; |
---|
[6536] | 157 | ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration) |
---|
| 158 | ->getOrCreateValue(deviceName_, "MinValue", i, multi_cast<std::string>(configMinValues_[i]), false); |
---|
[3270] | 159 | |
---|
| 160 | // Maximum values |
---|
| 161 | if (configMaxValues_[i] == INT_MIN) |
---|
| 162 | configMaxValues_[i] = 32767; |
---|
[6536] | 163 | ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration) |
---|
| 164 | ->getOrCreateValue(deviceName_, "MaxValue", i, multi_cast<std::string>(configMaxValues_[i]), false); |
---|
[3270] | 165 | |
---|
| 166 | // Middle values |
---|
[6536] | 167 | ConfigFileManager::getInstance().getConfigFile(ConfigFileType::JoyStickCalibration) |
---|
| 168 | ->getOrCreateValue(deviceName_, "ZeroValue", i, multi_cast<std::string>(configZeroValues_[i]), false); |
---|
[3270] | 169 | } |
---|
| 170 | |
---|
| 171 | this->evaluateCalibration(); |
---|
| 172 | } |
---|
| 173 | |
---|
[3327] | 174 | //! Evaluates the accumulated values during calibration |
---|
[3270] | 175 | void JoyStick::evaluateCalibration() |
---|
| 176 | { |
---|
| 177 | for (unsigned int i = 0; i < configMinValues_.size(); i++) |
---|
| 178 | { |
---|
| 179 | zeroValues_[i] = configZeroValues_[i]; |
---|
| 180 | negativeCoeffs_[i] = - 1.0f / (configMinValues_[i] - configZeroValues_[i]); |
---|
| 181 | positiveCoeffs_[i] = 1.0f / (configMaxValues_[i] - configZeroValues_[i]); |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
[3327] | 185 | //! Resets the pov states |
---|
| 186 | void JoyStick::clearBuffersImpl() |
---|
[3270] | 187 | { |
---|
[11071] | 188 | for (int& state : povStates_) |
---|
| 189 | state = 0; |
---|
[3270] | 190 | } |
---|
| 191 | |
---|
[3327] | 192 | //! Generic method to forward axis events |
---|
[3270] | 193 | void JoyStick::fireAxis(int axis, int value) |
---|
| 194 | { |
---|
[3327] | 195 | if (this->isCalibrating()) |
---|
[3270] | 196 | { |
---|
| 197 | if (value < configMinValues_[axis]) |
---|
| 198 | configMinValues_[axis] = value; |
---|
| 199 | if (value > configMaxValues_[axis]) |
---|
| 200 | configMaxValues_[axis] = value; |
---|
| 201 | } |
---|
| 202 | else |
---|
| 203 | { |
---|
| 204 | float fValue = static_cast<float>(value - zeroValues_[axis]); |
---|
| 205 | if (fValue > 0.0f) |
---|
| 206 | fValue *= positiveCoeffs_[axis]; |
---|
| 207 | else |
---|
| 208 | fValue *= negativeCoeffs_[axis]; |
---|
| 209 | |
---|
[11071] | 210 | for (InputState* state : inputStates_) |
---|
[3327] | 211 | state->joyStickAxisMoved(this->getDeviceID(), axis, fValue); |
---|
[3270] | 212 | } |
---|
| 213 | } |
---|
| 214 | |
---|
[3327] | 215 | //! OIS joy stick axis event handler |
---|
[3270] | 216 | bool JoyStick::axisMoved(const OIS::JoyStickEvent &arg, int axis) |
---|
| 217 | { |
---|
| 218 | // keep in mind that the first 8 axes are reserved for the sliders |
---|
| 219 | this->fireAxis(axis + sliderAxes_s, arg.state.mAxes[axis].abs); |
---|
| 220 | |
---|
| 221 | return true; |
---|
| 222 | } |
---|
| 223 | |
---|
[3327] | 224 | //! A Slider always has an X and an Y direction! |
---|
[3270] | 225 | bool JoyStick::sliderMoved(const OIS::JoyStickEvent &arg, int id) |
---|
| 226 | { |
---|
| 227 | if (sliderStates_[id][0] != arg.state.mSliders[id].abX) |
---|
| 228 | fireAxis(id * 2, arg.state.mSliders[id].abX); |
---|
| 229 | else if (sliderStates_[id][1] != arg.state.mSliders[id].abY) |
---|
| 230 | fireAxis(id * 2 + 1, arg.state.mSliders[id].abY); |
---|
| 231 | |
---|
| 232 | return true; |
---|
| 233 | } |
---|
| 234 | |
---|
[3327] | 235 | //! A POV is the big button that can point in all directions (but only in one at once) |
---|
[3270] | 236 | bool JoyStick::povMoved(const OIS::JoyStickEvent &arg, int id) |
---|
| 237 | { |
---|
| 238 | // translate the POV into 8 simple buttons |
---|
| 239 | |
---|
| 240 | int lastState = povStates_[id]; |
---|
| 241 | if (lastState & OIS::Pov::North) |
---|
| 242 | buttonReleased(arg, 32 + id * 4 + 0); |
---|
| 243 | if (lastState & OIS::Pov::South) |
---|
| 244 | buttonReleased(arg, 32 + id * 4 + 1); |
---|
| 245 | if (lastState & OIS::Pov::East) |
---|
| 246 | buttonReleased(arg, 32 + id * 4 + 2); |
---|
| 247 | if (lastState & OIS::Pov::West) |
---|
| 248 | buttonReleased(arg, 32 + id * 4 + 3); |
---|
| 249 | |
---|
| 250 | povStates_[id] = arg.state.mPOV[id].direction; |
---|
| 251 | |
---|
| 252 | int currentState = povStates_[id]; |
---|
| 253 | if (currentState & OIS::Pov::North) |
---|
| 254 | buttonPressed(arg, 32 + id * 4 + 0); |
---|
| 255 | if (currentState & OIS::Pov::South) |
---|
| 256 | buttonPressed(arg, 32 + id * 4 + 1); |
---|
| 257 | if (currentState & OIS::Pov::East) |
---|
| 258 | buttonPressed(arg, 32 + id * 4 + 2); |
---|
| 259 | if (currentState & OIS::Pov::West) |
---|
| 260 | buttonPressed(arg, 32 + id * 4 + 3); |
---|
| 261 | |
---|
| 262 | return true; |
---|
| 263 | } |
---|
| 264 | } |
---|