[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 | #include <boost/foreach.hpp> |
---|
| 34 | |
---|
| 35 | #include "core/ConfigFileManager.h" |
---|
| 36 | #include "core/ConfigValueIncludes.h" |
---|
| 37 | #include "core/CoreIncludes.h" |
---|
| 38 | #include "util/Convert.h" |
---|
| 39 | #include "InputState.h" |
---|
| 40 | |
---|
| 41 | namespace orxonox |
---|
| 42 | { |
---|
[3327] | 43 | //! Helper function that loads the config value vector of one coefficient |
---|
[3270] | 44 | void loadCalibration(std::vector<int>& list, const std::string& sectionName, const std::string& valueName, size_t size, int defaultValue); |
---|
| 45 | |
---|
[3327] | 46 | std::vector<std::string> JoyStick::deviceNames_s; |
---|
| 47 | |
---|
| 48 | JoyStick::JoyStick(unsigned int id, OIS::InputManager* oisInputManager) |
---|
| 49 | : super(id, oisInputManager) |
---|
[3270] | 50 | { |
---|
| 51 | RegisterRootObject(JoyStick); |
---|
| 52 | this->setConfigValues(); |
---|
[3327] | 53 | // Initialise POV and Slider states |
---|
| 54 | this->clearBuffersImpl(); |
---|
[3270] | 55 | |
---|
[3327] | 56 | // Generate unique name |
---|
| 57 | if (oisDevice_->vendor().empty()) |
---|
| 58 | deviceName_ = "Unknown_"; |
---|
| 59 | else |
---|
| 60 | { |
---|
| 61 | std::string name = oisDevice_->vendor(); |
---|
| 62 | replaceCharacters(name, ' ', '_'); |
---|
| 63 | deviceName_ = name + "_"; |
---|
| 64 | } |
---|
| 65 | deviceName_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Button)) + "_"; |
---|
| 66 | deviceName_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Axis)) + "_"; |
---|
| 67 | deviceName_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Slider)) + "_"; |
---|
| 68 | deviceName_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_POV)); |
---|
| 69 | //deviceName_ += multi_cast<std::string>(oisDevice_->getNumberOfComponents(OIS::OIS_Vector3)); |
---|
[3270] | 70 | |
---|
[3327] | 71 | BOOST_FOREACH(std::string& idString, deviceNames_s) |
---|
[3270] | 72 | { |
---|
[3327] | 73 | if (deviceName_ == idString) |
---|
| 74 | { |
---|
| 75 | // Make the ID unique for this execution time. |
---|
| 76 | deviceName_ += "_" + multi_cast<std::string>(this->getDeviceName()); |
---|
| 77 | break; |
---|
| 78 | } |
---|
[3270] | 79 | } |
---|
| 80 | |
---|
[3327] | 81 | COUT(4) << "Created OIS joy stick with ID " << deviceName_ << std::endl; |
---|
[3270] | 82 | |
---|
| 83 | // Load calibration |
---|
[3327] | 84 | size_t axes = sliderAxes_s + static_cast<size_t>(oisDevice_->getNumberOfComponents(OIS::OIS_Axis)); |
---|
| 85 | loadCalibration(configMinValues_, deviceName_, "MinValue", axes, -32768); |
---|
| 86 | loadCalibration(configMaxValues_, deviceName_, "MaxValue", axes, 32768); |
---|
| 87 | loadCalibration(configZeroValues_, deviceName_, "ZeroValue", axes, 0); |
---|
[3270] | 88 | this->evaluateCalibration(); |
---|
| 89 | } |
---|
| 90 | |
---|
[3327] | 91 | //! Callback for the joy stick calibration config file. |
---|
[3270] | 92 | void JoyStick::calibrationFileCallback() |
---|
| 93 | { |
---|
| 94 | ConfigFileManager::getInstance().setFilename(ConfigFileType::JoyStickCalibration, calibrationFilename_); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void JoyStick::setConfigValues() |
---|
| 98 | { |
---|
| 99 | SetConfigValue(calibrationFilename_, "joystick_calibration.ini") |
---|
| 100 | .description("Ini filename for the the joy stick calibration data.") |
---|
| 101 | .callback(this, &JoyStick::calibrationFileCallback); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | void loadCalibration(std::vector<int>& list, const std::string& sectionName, const std::string& valueName, size_t size, int defaultValue) |
---|
| 105 | { |
---|
| 106 | list.resize(size); |
---|
| 107 | unsigned int configValueVectorSize = ConfigFileManager::getInstance().getVectorSize(ConfigFileType::JoyStickCalibration, sectionName, valueName); |
---|
| 108 | if (configValueVectorSize > size) |
---|
| 109 | configValueVectorSize = size; |
---|
| 110 | |
---|
| 111 | for (unsigned int i = 0; i < configValueVectorSize; ++i) |
---|
| 112 | { |
---|
| 113 | list[i] = multi_cast<int>(ConfigFileManager::getInstance().getValue( |
---|
| 114 | ConfigFileType::JoyStickCalibration, sectionName, valueName, i, multi_cast<std::string>(defaultValue), false)); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | // fill the rest with default values |
---|
| 118 | for (unsigned int i = configValueVectorSize; i < size; ++i) |
---|
| 119 | list[i] = defaultValue; |
---|
| 120 | } |
---|
| 121 | |
---|
[3327] | 122 | //! Called by InputDevice when calibration mode has started |
---|
| 123 | void JoyStick::calibrationStarted() |
---|
[3270] | 124 | { |
---|
| 125 | // Set initial values |
---|
| 126 | BOOST_FOREACH(int& minVal, configMinValues_) |
---|
| 127 | minVal = INT_MAX; |
---|
| 128 | BOOST_FOREACH(int& minVal, configMaxValues_) |
---|
| 129 | minVal = INT_MIN; |
---|
| 130 | BOOST_FOREACH(int& zeroVal, configZeroValues_) |
---|
| 131 | zeroVal = 0; |
---|
| 132 | } |
---|
| 133 | |
---|
[3327] | 134 | //! Called by InputDevice when calibration mode has stopped |
---|
| 135 | void JoyStick::calibrationStopped() |
---|
[3270] | 136 | { |
---|
| 137 | // Get the middle positions now |
---|
| 138 | unsigned int iAxis = 0; |
---|
| 139 | for (unsigned int i = 0; i < sliderAxes_s/2; ++i) |
---|
| 140 | { |
---|
[3327] | 141 | configZeroValues_[iAxis++] = oisDevice_->getJoyStickState().mSliders[i].abX; |
---|
| 142 | configZeroValues_[iAxis++] = oisDevice_->getJoyStickState().mSliders[i].abY; |
---|
[3270] | 143 | } |
---|
[3327] | 144 | // Note: joyStickZeroValues_[iJoyStick] was already correctly resised in loadCalibration() |
---|
| 145 | assert(oisDevice_->getJoyStickState().mAxes.size() == configZeroValues_.size() - sliderAxes_s); |
---|
[3270] | 146 | for (unsigned int i = 0; i < configZeroValues_.size() - sliderAxes_s; ++i) |
---|
[3327] | 147 | configZeroValues_[iAxis++] = oisDevice_->getJoyStickState().mAxes[i].abs; |
---|
[3270] | 148 | |
---|
| 149 | for (unsigned int i = 0; i < configMinValues_.size(); ++i) |
---|
| 150 | { |
---|
| 151 | // Minimum values |
---|
| 152 | if (configMinValues_[i] == INT_MAX) |
---|
| 153 | configMinValues_[i] = -32768; |
---|
| 154 | ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration, |
---|
[3327] | 155 | deviceName_, "MinValue", i, multi_cast<std::string>(configMinValues_[i]), false); |
---|
[3270] | 156 | |
---|
| 157 | // Maximum values |
---|
| 158 | if (configMaxValues_[i] == INT_MIN) |
---|
| 159 | configMaxValues_[i] = 32767; |
---|
| 160 | ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration, |
---|
[3327] | 161 | deviceName_, "MaxValue", i, multi_cast<std::string>(configMaxValues_[i]), false); |
---|
[3270] | 162 | |
---|
| 163 | // Middle values |
---|
| 164 | ConfigFileManager::getInstance().setValue(ConfigFileType::JoyStickCalibration, |
---|
[3327] | 165 | deviceName_, "ZeroValue", i, multi_cast<std::string>(configZeroValues_[i]), false); |
---|
[3270] | 166 | } |
---|
| 167 | |
---|
| 168 | this->evaluateCalibration(); |
---|
| 169 | } |
---|
| 170 | |
---|
[3327] | 171 | //! Evaluates the accumulated values during calibration |
---|
[3270] | 172 | void JoyStick::evaluateCalibration() |
---|
| 173 | { |
---|
| 174 | for (unsigned int i = 0; i < configMinValues_.size(); i++) |
---|
| 175 | { |
---|
| 176 | zeroValues_[i] = configZeroValues_[i]; |
---|
| 177 | negativeCoeffs_[i] = - 1.0f / (configMinValues_[i] - configZeroValues_[i]); |
---|
| 178 | positiveCoeffs_[i] = 1.0f / (configMaxValues_[i] - configZeroValues_[i]); |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
[3327] | 182 | //! Resets the pov states |
---|
| 183 | void JoyStick::clearBuffersImpl() |
---|
[3270] | 184 | { |
---|
| 185 | for (int j = 0; j < 4; ++j) |
---|
| 186 | povStates_[j] = 0; |
---|
| 187 | } |
---|
| 188 | |
---|
[3327] | 189 | //! Generic method to forward axis events |
---|
[3270] | 190 | void JoyStick::fireAxis(int axis, int value) |
---|
| 191 | { |
---|
[3327] | 192 | if (this->isCalibrating()) |
---|
[3270] | 193 | { |
---|
| 194 | if (value < configMinValues_[axis]) |
---|
| 195 | configMinValues_[axis] = value; |
---|
| 196 | if (value > configMaxValues_[axis]) |
---|
| 197 | configMaxValues_[axis] = value; |
---|
| 198 | } |
---|
| 199 | else |
---|
| 200 | { |
---|
| 201 | float fValue = static_cast<float>(value - zeroValues_[axis]); |
---|
| 202 | if (fValue > 0.0f) |
---|
| 203 | fValue *= positiveCoeffs_[axis]; |
---|
| 204 | else |
---|
| 205 | fValue *= negativeCoeffs_[axis]; |
---|
| 206 | |
---|
| 207 | BOOST_FOREACH(InputState* state, inputStates_) |
---|
[3327] | 208 | state->joyStickAxisMoved(this->getDeviceID(), axis, fValue); |
---|
[3270] | 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
[3327] | 212 | //! OIS joy stick axis event handler |
---|
[3270] | 213 | bool JoyStick::axisMoved(const OIS::JoyStickEvent &arg, int axis) |
---|
| 214 | { |
---|
| 215 | // keep in mind that the first 8 axes are reserved for the sliders |
---|
| 216 | this->fireAxis(axis + sliderAxes_s, arg.state.mAxes[axis].abs); |
---|
| 217 | |
---|
| 218 | return true; |
---|
| 219 | } |
---|
| 220 | |
---|
[3327] | 221 | //! A Slider always has an X and an Y direction! |
---|
[3270] | 222 | bool JoyStick::sliderMoved(const OIS::JoyStickEvent &arg, int id) |
---|
| 223 | { |
---|
| 224 | if (sliderStates_[id][0] != arg.state.mSliders[id].abX) |
---|
| 225 | fireAxis(id * 2, arg.state.mSliders[id].abX); |
---|
| 226 | else if (sliderStates_[id][1] != arg.state.mSliders[id].abY) |
---|
| 227 | fireAxis(id * 2 + 1, arg.state.mSliders[id].abY); |
---|
| 228 | |
---|
| 229 | return true; |
---|
| 230 | } |
---|
| 231 | |
---|
[3327] | 232 | //! A POV is the big button that can point in all directions (but only in one at once) |
---|
[3270] | 233 | bool JoyStick::povMoved(const OIS::JoyStickEvent &arg, int id) |
---|
| 234 | { |
---|
| 235 | // translate the POV into 8 simple buttons |
---|
| 236 | |
---|
| 237 | int lastState = povStates_[id]; |
---|
| 238 | if (lastState & OIS::Pov::North) |
---|
| 239 | buttonReleased(arg, 32 + id * 4 + 0); |
---|
| 240 | if (lastState & OIS::Pov::South) |
---|
| 241 | buttonReleased(arg, 32 + id * 4 + 1); |
---|
| 242 | if (lastState & OIS::Pov::East) |
---|
| 243 | buttonReleased(arg, 32 + id * 4 + 2); |
---|
| 244 | if (lastState & OIS::Pov::West) |
---|
| 245 | buttonReleased(arg, 32 + id * 4 + 3); |
---|
| 246 | |
---|
| 247 | povStates_[id] = arg.state.mPOV[id].direction; |
---|
| 248 | |
---|
| 249 | int currentState = povStates_[id]; |
---|
| 250 | if (currentState & OIS::Pov::North) |
---|
| 251 | buttonPressed(arg, 32 + id * 4 + 0); |
---|
| 252 | if (currentState & OIS::Pov::South) |
---|
| 253 | buttonPressed(arg, 32 + id * 4 + 1); |
---|
| 254 | if (currentState & OIS::Pov::East) |
---|
| 255 | buttonPressed(arg, 32 + id * 4 + 2); |
---|
| 256 | if (currentState & OIS::Pov::West) |
---|
| 257 | buttonPressed(arg, 32 + id * 4 + 3); |
---|
| 258 | |
---|
| 259 | return true; |
---|
| 260 | } |
---|
| 261 | } |
---|