Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/input/KeyBinder.cc @ 10624

Last change on this file since 10624 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

  • Property svn:eol-style set to native
File size: 25.5 KB
Line 
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 "KeyBinder.h"
30
31#include <algorithm>
32#include <sstream>
33#include "util/Convert.h"
34#include "util/Output.h"
35#include "util/Exception.h"
36#include "core/CoreIncludes.h"
37#include "core/config/ConfigValueIncludes.h"
38#include "core/config/ConfigFile.h"
39#include "core/ApplicationPaths.h"
40#include "core/ConfigurablePaths.h"
41#include "InputCommands.h"
42#include "JoyStick.h"
43
44namespace orxonox
45{
46    RegisterAbstractClass(KeyBinder).inheritsFrom<JoyStickQuantityListener>();
47
48    /**
49    @brief
50        Constructor that does as little as necessary.
51    */
52    KeyBinder::KeyBinder(const std::string& filename)
53        : deriveTime_(0.0f)
54        , filename_(filename)
55        , configFile_(NULL)
56        , fallbackConfigFile_(NULL)
57    {
58        mouseRelative_[0] = 0;
59        mouseRelative_[1] = 0;
60        mousePosition_[0] = 0.0;
61        mousePosition_[1] = 0.0;
62
63        RegisterObject(KeyBinder);
64
65        // initialise all buttons and half axes to avoid creating everything with 'new'
66        // keys
67        for (unsigned int i = 0; i < KeyCode::numberOfKeys; i++)
68        {
69            const std::string& keyname = KeyCode::ByString[i];
70            if (!keyname.empty())
71                keys_[i].name_ = std::string("Key") + keyname;
72            else
73                keys_[i].name_.clear();
74            keys_[i].paramCommandBuffer_ = &paramCommandBuffer_;
75            keys_[i].groupName_ = "Keys";
76        }
77        // mouse buttons plus 4 mouse wheel buttons only 'generated' by KeyBinder
78        const char* const mouseWheelNames[] = { "Wheel1Down", "Wheel1Up", "Wheel2Down", "Wheel2Up" };
79        for (unsigned int i = 0; i < numberOfMouseButtons_; i++)
80        {
81            std::string nameSuffix;
82            if (i < MouseButtonCode::numberOfButtons)
83                nameSuffix = MouseButtonCode::ByString[i];
84            else
85                nameSuffix = mouseWheelNames[i - MouseButtonCode::numberOfButtons];
86            mouseButtons_[i].name_ = nameSuffix;
87            mouseButtons_[i].paramCommandBuffer_ = &paramCommandBuffer_;
88            mouseButtons_[i].groupName_ = "MouseButtons";
89        }
90        // mouse axes
91        for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++)
92        {
93            mouseAxes_[i].name_ = MouseAxisCode::ByString[i / 2];
94            if (i & 1)
95                mouseAxes_[i].name_ += "Pos";
96            else
97                mouseAxes_[i].name_ += "Neg";
98            mouseAxes_[i].paramCommandBuffer_ = &paramCommandBuffer_;
99            mouseAxes_[i].groupName_ = "MouseAxes";
100        }
101
102        // initialise joy sticks separatly to allow for reloading
103        this->JoyStickQuantityChanged(this->getJoyStickList());
104
105        // set them here to use allHalfAxes_
106        setConfigValues();
107
108        // Load the bindings if filename was given
109        if (!this->filename_.empty())
110            this->loadBindings();
111    }
112
113    /**
114    @brief
115        Destructor
116    */
117    KeyBinder::~KeyBinder()
118    {
119        // almost no destructors required because most of the arrays are static.
120        clearBindings(); // does some destruction work
121        if (this->configFile_)
122            delete this->configFile_;
123        if (this->fallbackConfigFile_)
124            delete this->fallbackConfigFile_;
125    }
126
127    /**
128    @brief
129        Loader for the key bindings, managed by config values.
130    */
131    void KeyBinder::setConfigValues()
132    {
133        SetConfigValue(analogThreshold_, 0.05f)
134            .description("Threshold for analog axes until which the state is 0.");
135        SetConfigValue(bFilterAnalogNoise_, false)
136            .description("Specifies whether to filter small analog values like joy stick fluctuations.");
137        SetConfigValue(mouseSensitivity_, 3.0f)
138            .description("Mouse sensitivity.");
139        this->totalMouseSensitivity_ = this->mouseSensitivity_ / this->mouseClippingSize_;
140        SetConfigValue(bDeriveMouseInput_, false)
141            .description("Whether or not to derive moues movement for the absolute value.");
142        SetConfigValue(derivePeriod_, 0.05f)
143            .description("Accuracy of the mouse input deriver. The higher the more precise, but laggier.");
144        SetConfigValue(mouseSensitivityDerived_, 1.0f)
145            .description("Mouse sensitivity if mouse input is derived.");
146        SetConfigValue(mouseWheelStepSize_, 120)
147            .description("Equals one step of the mousewheel.");
148        SetConfigValue(buttonThreshold_, 0.80f)
149            .description("Threshold for analog axes until which the button is not pressed.")
150            .callback(this, &KeyBinder::buttonThresholdChanged);
151    }
152
153    void KeyBinder::buttonThresholdChanged()
154    {
155        for (unsigned int i = 0; i < allHalfAxes_.size(); i++)
156            if (!allHalfAxes_[i]->bButtonThresholdUser_)
157                allHalfAxes_[i]->buttonThreshold_ = this->buttonThreshold_;
158    }
159
160    void KeyBinder::JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList)
161    {
162        unsigned int oldValue = joySticks_.size();
163        joySticks_ = joyStickList;
164
165        // initialise joy stick bindings
166        initialiseJoyStickBindings();
167
168        // collect all Buttons and HalfAxes again
169        compilePointerLists();
170
171        // load the bindings if required
172        if (configFile_ != NULL)
173        {
174            for (unsigned int iDev = oldValue; iDev < joySticks_.size(); ++iDev)
175            {
176                for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; ++i)
177                    (*joyStickButtons_[iDev])[i].readBinding(this->configFile_, this->fallbackConfigFile_);
178                for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; ++i)
179                    (*joyStickAxes_[iDev])[i].readBinding(this->configFile_, this->fallbackConfigFile_);
180            }
181        }
182
183        // Set the button threshold for potential new axes
184        buttonThresholdChanged();
185    }
186
187    void KeyBinder::initialiseJoyStickBindings()
188    {
189        while (joyStickAxes_.size() < joySticks_.size())
190            joyStickAxes_.push_back(shared_ptr<JoyStickAxisVector>(new JoyStickAxisVector()));
191        while (joyStickButtons_.size() < joySticks_.size())
192            joyStickButtons_.push_back(shared_ptr<JoyStickButtonVector>(new JoyStickButtonVector()));
193        // For the case the new size is smaller
194        this->joyStickAxes_.resize(joySticks_.size());
195        this->joyStickButtons_.resize(joySticks_.size());
196
197        // reinitialise all joy stick bindings (doesn't overwrite the old ones)
198        for (unsigned int iDev = 0; iDev < joySticks_.size(); iDev++)
199        {
200            const std::string& deviceName = joySticks_[iDev]->getDeviceName();
201            // joy stick buttons
202            for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; i++)
203            {
204                (*joyStickButtons_[iDev])[i].name_ = JoyStickButtonCode::ByString[i];
205                (*joyStickButtons_[iDev])[i].paramCommandBuffer_ = &paramCommandBuffer_;
206                (*joyStickButtons_[iDev])[i].groupName_ = "JoyStickButtons_" + deviceName;
207            }
208            // joy stick axes
209            for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++)
210            {
211                (*joyStickAxes_[iDev])[i].name_ = JoyStickAxisCode::ByString[i / 2];
212                if (i & 1)
213                    (*joyStickAxes_[iDev])[i].name_ += "Pos";
214                else
215                    (*joyStickAxes_[iDev])[i].name_ += "Neg";
216                (*joyStickAxes_[iDev])[i].paramCommandBuffer_ = &paramCommandBuffer_;
217                (*joyStickAxes_[iDev])[i].groupName_ = "JoyStickAxes_" + deviceName;
218            }
219        }
220    }
221
222    void KeyBinder::compilePointerLists()
223    {
224        allButtons_.clear();
225        allHalfAxes_.clear();
226
227        // Note: Don't include the dummy keys which don't actually exist in OIS but have a number
228        for (unsigned int i = 0; i < KeyCode::numberOfKeys; i++)
229            if (!keys_[i].name_.empty())
230                allButtons_[keys_[i].groupName_ + '.' + keys_[i].name_] = keys_ + i;
231        for (unsigned int i = 0; i < numberOfMouseButtons_; i++)
232            allButtons_[mouseButtons_[i].groupName_ + '.' + mouseButtons_[i].name_] = mouseButtons_ + i;
233        for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++)
234        {
235            allButtons_[mouseAxes_[i].groupName_ + '.' + mouseAxes_[i].name_] = mouseAxes_ + i;
236            allHalfAxes_.push_back(mouseAxes_ + i);
237        }
238        for (unsigned int iDev = 0; iDev < joySticks_.size(); iDev++)
239        {
240            for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; i++)
241                allButtons_[(*joyStickButtons_[iDev])[i].groupName_ + '.' + (*joyStickButtons_[iDev])[i].name_] = &((*joyStickButtons_[iDev])[i]);
242            for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++)
243            {
244                allButtons_[(*joyStickAxes_[iDev])[i].groupName_ + '.' + (*joyStickAxes_[iDev])[i].name_] = &((*joyStickAxes_[iDev])[i]);
245                allHalfAxes_.push_back(&((*joyStickAxes_[iDev])[i]));
246            }
247        }
248    }
249
250    /**
251    @brief
252        Loads the key and button bindings.
253    */
254    void KeyBinder::loadBindings()
255    {
256        orxout(internal_info, context::input) << "KeyBinder: Loading key bindings..." << endl;
257
258        this->configFile_ = new ConfigFile(this->filename_, !ApplicationPaths::buildDirectoryRun());
259        this->configFile_->load();
260
261        if (ApplicationPaths::buildDirectoryRun())
262        {
263            // Dev users should have combined key bindings files
264            std::string defaultFilepath(ConfigurablePaths::getDataPathString() + ConfigFile::DEFAULT_CONFIG_FOLDER + '/' + this->filename_);
265            std::ifstream file(defaultFilepath.c_str());
266            if (file.is_open())
267            {
268                file.close();
269                // Open the default file for later use (use absolute path!)
270                this->fallbackConfigFile_ = new ConfigFile(defaultFilepath, false);
271                this->fallbackConfigFile_->load();
272            }
273        }
274
275        // Parse bindings and create the ConfigValueContainers if necessary
276        for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it)
277        {
278            it->second->readBinding(this->configFile_, this->fallbackConfigFile_);
279            addButtonToCommand(it->second->bindingString_, it->second);
280        }
281
282        orxout(internal_info, context::input) << "KeyBinder: Loading key bindings done." << endl;
283    }
284
285    bool KeyBinder::setBinding(const std::string& binding, const std::string& name, bool bTemporary)
286    {
287        std::map<std::string, Button*>::iterator it = allButtons_.find(name);
288        if (it != allButtons_.end())
289        {
290            addButtonToCommand(binding, it->second);
291            std::string str = binding;
292            if (ApplicationPaths::buildDirectoryRun() && binding.empty())
293                str = "NoBinding";
294            it->second->setBinding(this->configFile_, this->fallbackConfigFile_, binding, bTemporary);
295            return true;
296        }
297        else
298        {
299            orxout(internal_warning, context::input) << "Could not find key/button/axis with name '" << name << "'." << endl;
300            return false;
301        }
302    }
303
304     void KeyBinder::addButtonToCommand(const std::string& command, Button* button)
305     {
306        std::ostringstream stream;
307        stream << button->groupName_  << '.' << button->name_;
308
309        std::vector<std::string>& oldKeynames = this->allCommands_[button->bindingString_];
310        std::vector<std::string>::iterator it = std::find(oldKeynames.begin(), oldKeynames.end(), stream.str());
311        if (it != oldKeynames.end())
312            oldKeynames.erase(it);
313
314        if (!command.empty())
315        {
316            std::vector<std::string>& keynames = this->allCommands_[command];
317            if (std::find(keynames.begin(), keynames.end(), stream.str()) == keynames.end())
318                this->allCommands_[command].push_back(stream.str());
319        }
320     }
321
322    /**
323    @brief
324        Return the first key name for a specific command
325    */
326    const std::string& KeyBinder::getBinding(const std::string& commandName)
327    {
328        if (this->allCommands_.find(commandName) != this->allCommands_.end())
329        {
330            std::vector<std::string>& keynames = this->allCommands_[commandName];
331            return keynames.front();
332        }
333
334        return BLANKSTRING;
335    }
336
337    /**
338    @brief
339        Return the key name for a specific command at a given index.
340    @param commandName
341        The command name the key name is returned for.
342    @param index
343        The index at which the key name is returned for.
344    */
345    const std::string& KeyBinder::getBinding(const std::string& commandName, unsigned int index)
346    {
347        if (this->allCommands_.find(commandName) != this->allCommands_.end())
348        {
349            std::vector<std::string>& keynames = this->allCommands_[commandName];
350            if (index < keynames.size())
351                return keynames[index];
352
353            return BLANKSTRING;
354        }
355
356        return BLANKSTRING;
357    }
358
359    /**
360    @brief
361        Get the number of different key bindings of a specific command.
362    @param commandName
363        The command.
364    */
365    unsigned int KeyBinder::getNumberOfBindings(const std::string& commandName)
366    {
367        if (this->allCommands_.find(commandName) != this->allCommands_.end())
368        {
369            std::vector<std::string>& keynames = this->allCommands_[commandName];
370            return keynames.size();
371        }
372
373        return 0;
374    }
375
376    /**
377    @brief
378        Overwrites all bindings with ""
379    */
380    void KeyBinder::clearBindings()
381    {
382        for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it)
383            it->second->clear();
384
385        for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
386            delete paramCommandBuffer_[i];
387        paramCommandBuffer_.clear();
388    }
389
390    /**
391        @brief Changes the keybind mode of a given console command.
392    */
393    void KeyBinder::changeMode(ConsoleCommand* command, KeybindMode::Value new_mode)
394    {
395        // iterate over all buttons
396        for (std::map<std::string, Button*>::iterator it = this->allButtons_.begin(); it != this->allButtons_.end(); ++it)
397        {
398            Button* button = it->second;
399
400            // iterate over all modes
401            for (int mode_index = 0; mode_index < 3; ++mode_index)
402            {
403                if (mode_index == new_mode) // skip commands that are already in the desired mode
404                    continue;
405
406                // iterate over all commands of the given mode at the given button
407                for (size_t command_index = 0; command_index < button->nCommands_[mode_index]; ++command_index)
408                {
409                    if (button->commands_[mode_index][command_index]->hasFixedKeybindMode())
410                        continue;
411
412                    CommandEvaluation* evaluation = button->commands_[mode_index][command_index]->getEvaluation();
413
414                    // compare the command
415                    if (evaluation && evaluation->getConsoleCommand() == command)
416                    {
417                        // increase array of new mode
418                        BaseCommand** array_new_mode = new BaseCommand*[button->nCommands_[new_mode] + 1];
419                        // copy array content
420                        for (size_t c = 0; c < button->nCommands_[new_mode]; ++c)
421                            array_new_mode[c] = button->commands_[new_mode][c];
422                        // insert changed command at the end
423                        array_new_mode[button->nCommands_[new_mode]] = button->commands_[mode_index][command_index];
424                        // delete old array
425                        delete[] button->commands_[new_mode];
426                        // assign new array
427                        button->commands_[new_mode] = array_new_mode;
428                        // increase counter
429                        button->nCommands_[new_mode]++;
430
431                        // erase command from old array
432                        for (size_t c = command_index; c < button->nCommands_[mode_index] - 1; ++c)
433                            button->commands_[mode_index][c] = button->commands_[mode_index][c + 1];
434                        // decrease counter
435                        button->nCommands_[mode_index]--;
436                        // old array would not get deleted if nCommands_ is now 0
437                        // otherwise: nobody cares about an array that is one element too large - nCommands_ defines the size
438                        if (button->nCommands_[mode_index] == 0)
439                        {
440                            delete[] button->commands_[mode_index];
441                            button->commands_[mode_index] = 0;
442                        }
443
444                        // decrement the index since we shifted the array and continue searching for more occurrences of the command
445                        command_index--;
446                    }
447                }
448            }
449        }
450    }
451
452    void KeyBinder::resetJoyStickAxes()
453    {
454        for (unsigned int iDev = 0; iDev < joySticks_.size(); ++iDev)
455            for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++)
456                (*joyStickAxes_[iDev])[i].reset();
457    }
458
459    /**
460        @brief Sets the position of the mouse back to 0/0.
461    */
462    void KeyBinder::resetMouseAxes()
463    {
464        this->mousePosition_[0] = 0.0f;
465        this->mousePosition_[1] = 0.0f;
466
467        for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++)
468            mouseAxes_[i].reset();
469    }
470
471    void KeyBinder::mouseUpdated(float dt)
472    {
473        if (bDeriveMouseInput_)
474        {
475            // only update when derivation dt has passed
476            if (deriveTime_ > derivePeriod_)
477            {
478                for (int i = 0; i < 2; i++)
479                {
480                    if (mouseRelative_[i] < 0)
481                    {
482                        mouseAxes_[2*i + 0].absVal_
483                            = -mouseRelative_[i] / deriveTime_ * 0.0005f * mouseSensitivityDerived_;
484                        mouseAxes_[2*i + 1].absVal_ = 0.0f;
485                    }
486                    else if (mouseRelative_[i] > 0)
487                    {
488                        mouseAxes_[2*i + 0].absVal_ = 0.0f;
489                        mouseAxes_[2*i + 1].absVal_
490                            =  mouseRelative_[i] / deriveTime_ * 0.0005f * mouseSensitivityDerived_;
491                    }
492                    else
493                    {
494                        mouseAxes_[2*i + 0].absVal_ = 0.0f;
495                        mouseAxes_[2*i + 1].absVal_ = 0.0f;
496                    }
497                    mouseRelative_[i] = 0;
498                    mouseAxes_[2*i + 0].hasChanged_ = true;
499                    mouseAxes_[2*i + 1].hasChanged_ = true;
500                }
501                deriveTime_ = 0.0f;
502            }
503            else
504                deriveTime_ += dt;
505        }
506
507        for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++)
508        {
509            // Why dividing relative value by dt? The reason lies in the simple fact, that when you
510            // press a button that has relative movement, that value has to be multiplied by dt to be
511            // frame rate independent. This can easily (and only) be done in updateInput(float).
512            // Hence we need to divide by dt here for the mouse to compensate, because the relative
513            // move movements have nothing to do with dt.
514            if (dt != 0.0f)
515            {
516                // just ignore if dt == 0.0 because we have multiplied by 0.0 anyway..
517                mouseAxes_[i].relVal_ /= dt;
518            }
519
520            tickHalfAxis(mouseAxes_[i]);
521        }
522    }
523
524    void KeyBinder::joyStickUpdated(unsigned int joyStick, float dt)
525    {
526        for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++)
527        {
528            tickHalfAxis((*joyStickAxes_[joyStick])[i]);
529        }
530    }
531
532    void KeyBinder::tickHalfAxis(HalfAxis& halfAxis)
533    {
534        // button mode
535        // TODO: optimize out all the half axes that don't act as a button at the moment
536        if (halfAxis.hasChanged_)
537        {
538            if (!halfAxis.pressed_ && halfAxis.absVal_ > halfAxis.buttonThreshold_)
539            {
540                // key pressed event
541                halfAxis.pressed_ = true;
542                if (halfAxis.nCommands_[KeybindMode::OnPress])
543                    halfAxis.execute(KeybindMode::OnPress);
544            }
545            else if (halfAxis.pressed_ && halfAxis.absVal_ < halfAxis.buttonThreshold_)
546            {
547                // key released event
548                halfAxis.pressed_ = false;
549                if (halfAxis.nCommands_[KeybindMode::OnRelease])
550                    halfAxis.execute(KeybindMode::OnRelease);
551            }
552            halfAxis.hasChanged_ = false;
553        }
554
555        if (halfAxis.pressed_)
556        {
557            // key held event
558            if (halfAxis.nCommands_[KeybindMode::OnHold])
559                halfAxis.execute(KeybindMode::OnHold);
560        }
561
562        // these are the actually useful axis bindings for analog input
563        halfAxis.execute();
564    }
565
566    /**
567    @brief
568        Event handler for the mouseMoved Event.
569    @param abs_
570        The absolute position of the mouse
571    @param rel_
572        The relative movement of the mouse
573    @param clippingSize
574        Mouse screen area in pixels (usually 1024x1024)
575    */
576    void KeyBinder::mouseMoved(IntVector2 abs_, IntVector2 rel_, IntVector2 clippingSize)
577    {
578        // y axis of mouse input is inverted
579        int rel[] = { rel_.x, -rel_.y };
580
581        if (bDeriveMouseInput_)
582        {
583            mouseRelative_[0] += rel[0];
584            mouseRelative_[1] += rel[1];
585        }
586        else
587        {
588            for (int i = 0; i < 2; i++)
589            {
590                if (rel[i]) // performance opt. for the case that rel[i] == 0
591                {
592                    // write absolute values
593                    mouseAxes_[2*i + 0].hasChanged_ = true;
594                    mouseAxes_[2*i + 1].hasChanged_ = true;
595                    mousePosition_[i] += rel[i] * totalMouseSensitivity_;
596
597                    // clip absolute position
598                    if (mousePosition_[i] > 1.0)
599                        mousePosition_[i] =  1.0;
600                    if (mousePosition_[i] < -1.0)
601                        mousePosition_[i] = -1.0;
602
603                    if (mousePosition_[i] < 0.0)
604                    {
605                        mouseAxes_[2*i + 0].absVal_ = -mousePosition_[i];
606                        mouseAxes_[2*i + 1].absVal_ = 0.0f;
607                    }
608                    else
609                    {
610                        mouseAxes_[2*i + 0].absVal_ = 0.0f;
611                        mouseAxes_[2*i + 1].absVal_ =  mousePosition_[i];
612                    }
613                }
614            }
615        }
616
617        // relative
618        for (int i = 0; i < 2; i++)
619        {
620            if (rel[i] < 0)
621                mouseAxes_[0 + 2*i].relVal_ = -rel[i] * totalMouseSensitivity_;
622            else
623                mouseAxes_[1 + 2*i].relVal_ =  rel[i] * totalMouseSensitivity_;
624        }
625    }
626
627    /**
628    @brief Event handler for the mouseScrolled Event.
629    @param abs The absolute position of the scroll wheel
630    @param rel The relative movement of the scroll wheel
631    */
632    void KeyBinder::mouseScrolled(int abs, int rel)
633    {
634        if (rel < 0)
635            for (int i = 0; i < -rel/mouseWheelStepSize_; i++)
636                mouseButtons_[8].execute(KeybindMode::OnPress, static_cast<float>(abs)/mouseWheelStepSize_);
637        else
638            for (int i = 0; i < rel/mouseWheelStepSize_; i++)
639                mouseButtons_[9].execute(KeybindMode::OnPress, static_cast<float>(abs)/mouseWheelStepSize_);
640    }
641
642    void KeyBinder::axisMoved(unsigned int device, unsigned int axisID, float value)
643    {
644        // Filter analog noise
645        if (this->bFilterAnalogNoise_ && std::abs(value) < this->analogThreshold_)
646            value = 0.0;
647        int i = axisID * 2;
648        JoyStickAxisVector& axis = *joyStickAxes_[device];
649        if (value < 0)
650        {
651            axis[i].absVal_ = -value;
652            axis[i].relVal_ = -value;
653            axis[i].hasChanged_ = true;
654            if (axis[i + 1].absVal_ > 0.0f)
655            {
656                axis[i + 1].absVal_ = -0.0f;
657                axis[i + 1].relVal_ = -0.0f;
658                axis[i + 1].hasChanged_ = true;
659            }
660        }
661        else
662        {
663            axis[i + 1].absVal_ = value;
664            axis[i + 1].relVal_ = value;
665            axis[i + 1].hasChanged_ = true;
666            if (axis[i].absVal_ > 0.0f)
667            {
668                axis[i].absVal_ = -0.0f;
669                axis[i].relVal_ = -0.0f;
670                axis[i].hasChanged_ = true;
671            }
672        }
673    }
674}
Note: See TracBrowser for help on using the repository browser.