Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/input/JoyStick.cc @ 9667

Last change on this file since 9667 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

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