Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5679 was 3327, checked in by rgrieder, 15 years ago

Merged all remaining revisions from core4 back to the trunk.

  • Property svn:eol-style set to native
File size: 18.4 KB
RevLine 
[971]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1349]3 *                    > www.orxonox.net <
[971]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 */
[973]28
[971]29/**
30 @file
[973]31 @brief Implementation of the different input handlers.
[971]32 */
33
[1413]34#include "KeyBinder.h"
[2662]35
[1293]36#include "util/Convert.h"
[1747]37#include "util/Debug.h"
[1519]38#include "core/ConfigValueIncludes.h"
39#include "core/CoreIncludes.h"
[1795]40#include "core/ConfigFileManager.h"
[1520]41#include "InputCommands.h"
[3327]42#include "JoyStick.h"
[971]43
44namespace orxonox
45{
[1755]46    /**
47    @brief
48        Constructor that does as little as necessary.
49    */
50    KeyBinder::KeyBinder()
[3327]51        : deriveTime_(0.0f)
[1755]52    {
53        mouseRelative_[0] = 0;
54        mouseRelative_[1] = 0;
55        mousePosition_[0] = 0;
56        mousePosition_[1] = 0;
[1391]57
[3327]58        joyStickButtons_.reserve(1000);
59        joyStickAxes_.reserve(1000);
60
[1755]61        RegisterRootObject(KeyBinder);
[1293]62
[1887]63        // intialise all buttons and half axes to avoid creating everything with 'new'
[1755]64        // keys
[1887]65        for (unsigned int i = 0; i < KeyCode::numberOfKeys; i++)
[1755]66        {
[1887]67            std::string keyname = KeyCode::ByString[i];
68            if (!keyname.empty())
69                keys_[i].name_ = std::string("Key") + keyname;
70            else
[2662]71                keys_[i].name_ = "";
[1887]72            keys_[i].paramCommandBuffer_ = &paramCommandBuffer_;
73            keys_[i].groupName_ = "Keys";
[1755]74        }
[1887]75        // mouse buttons plus 4 mouse wheel buttons only 'generated' by KeyBinder
76        const char* const mouseWheelNames[] = { "Wheel1Down", "Wheel1Up", "Wheel2Down", "Wheel2Up" };
77        for (unsigned int i = 0; i < numberOfMouseButtons_; i++)
[1755]78        {
[1887]79            std::string nameSuffix;
80            if (i < MouseButtonCode::numberOfButtons)
81                nameSuffix = MouseButtonCode::ByString[i];
82            else
83                nameSuffix = mouseWheelNames[i - MouseButtonCode::numberOfButtons];
[3327]84            mouseButtons_[i].name_ = nameSuffix;
[1887]85            mouseButtons_[i].paramCommandBuffer_ = &paramCommandBuffer_;
86            mouseButtons_[i].groupName_ = "MouseButtons";
[1755]87        }
[1887]88        // mouse axes
89        for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++)
90        {
[3327]91            mouseAxes_[i].name_ = MouseAxisCode::ByString[i / 2];
[1887]92            if (i & 1)
93                mouseAxes_[i].name_ += "Pos";
94            else
95                mouseAxes_[i].name_ += "Neg";
96            mouseAxes_[i].paramCommandBuffer_ = &paramCommandBuffer_;
97            mouseAxes_[i].groupName_ = "MouseAxes";
98        }
[1755]99
[3280]100        // We might not even load any bindings at all (KeyDetector for instance)
101        this->configFile_ = ConfigFileType::NoType;
[2103]102
[1887]103        // initialise joy sticks separatly to allow for reloading
[3327]104        this->JoyStickQuantityChanged(this->getJoyStickList());
[1887]105
106        // set them here to use allHalfAxes_
107        setConfigValues();
[1413]108    }
109
[1755]110    /**
111    @brief
112        Destructor
113    */
114    KeyBinder::~KeyBinder()
[1413]115    {
[1755]116        // almost no destructors required because most of the arrays are static.
117        clearBindings(); // does some destruction work
[1413]118    }
119
[1755]120    /**
121    @brief
122        Loader for the key bindings, managed by config values.
123    */
124    void KeyBinder::setConfigValues()
125    {
126        SetConfigValue(analogThreshold_, 0.05f)
127            .description("Threshold for analog axes until which the state is 0.");
[2087]128        SetConfigValue(bFilterAnalogNoise_, false)
129            .description("Specifies whether to filter small analog values like joy stick fluctuations.");
[1755]130        SetConfigValue(mouseSensitivity_, 1.0f)
131            .description("Mouse sensitivity.");
132        SetConfigValue(bDeriveMouseInput_, false)
133            .description("Whether or not to derive moues movement for the absolute value.");
134        SetConfigValue(derivePeriod_, 0.05f)
135            .description("Accuracy of the mouse input deriver. The higher the more precise, but laggier.");
136        SetConfigValue(mouseSensitivityDerived_, 1.0f)
137            .description("Mouse sensitivity if mouse input is derived.");
[2087]138        SetConfigValue(mouseWheelStepSize_, 120)
[1887]139            .description("Equals one step of the mousewheel.");
[1755]140        SetConfigValue(buttonThreshold_, 0.80f)
[1887]141            .description("Threshold for analog axes until which the button is not pressed.")
142            .callback(this, &KeyBinder::buttonThresholdChanged);
143    }
[973]144
[1887]145    void KeyBinder::buttonThresholdChanged()
146    {
147        for (unsigned int i = 0; i < allHalfAxes_.size(); i++)
148            if (!allHalfAxes_[i]->bButtonThresholdUser_)
149                allHalfAxes_[i]->buttonThreshold_ = this->buttonThreshold_;
[1755]150    }
[1293]151
[3327]152    void KeyBinder::JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList)
[1755]153    {
[3327]154        unsigned int oldValue = joySticks_.size();
155        joySticks_ = joyStickList;
[1887]156
157        // initialise joy stick bindings
158        initialiseJoyStickBindings();
159
160        // collect all Buttons and HalfAxes again
161        compilePointerLists();
162
163        // load the bindings if required
[2103]164        if (configFile_ != ConfigFileType::NoType)
[1755]165        {
[3327]166            for (unsigned int iDev = oldValue; iDev < joySticks_.size(); ++iDev)
[1887]167            {
168                for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; ++i)
[2103]169                    joyStickButtons_[iDev][i].readConfigValue(this->configFile_);
[1887]170                for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; ++i)
[2103]171                    joyStickAxes_[iDev][i].readConfigValue(this->configFile_);
[1887]172            }
[1755]173        }
[1293]174
[1887]175        // Set the button threshold for potential new axes
176        buttonThresholdChanged();
177    }
178
179    void KeyBinder::initialiseJoyStickBindings()
180    {
[3327]181        this->joyStickAxes_.resize(joySticks_.size());
182        this->joyStickButtons_.resize(joySticks_.size());
[1887]183
184        // reinitialise all joy stick binings (doesn't overwrite the old ones)
[3327]185        for (unsigned int iDev = 0; iDev < joySticks_.size(); iDev++)
[1755]186        {
[3327]187            std::string deviceName = joySticks_[iDev]->getDeviceName();
[1887]188            // joy stick buttons
189            for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; i++)
190            {
[3327]191                joyStickButtons_[iDev][i].name_ = JoyStickButtonCode::ByString[i];
[1887]192                joyStickButtons_[iDev][i].paramCommandBuffer_ = &paramCommandBuffer_;
[3327]193                joyStickButtons_[iDev][i].groupName_ = "JoyStickButtons_" + deviceName;
[1887]194            }
195            // joy stick axes
196            for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++)
197            {
[3327]198                joyStickAxes_[iDev][i].name_ = JoyStickAxisCode::ByString[i / 2];
[1887]199                if (i & 1)
200                    joyStickAxes_[iDev][i].name_ += "Pos";
201                else
202                    joyStickAxes_[iDev][i].name_ += "Neg";
203                joyStickAxes_[iDev][i].paramCommandBuffer_ = &paramCommandBuffer_;
[3327]204                joyStickAxes_[iDev][i].groupName_ = "JoyStickAxes_" + deviceName;
[1887]205            }
206        }
207    }
[973]208
[1887]209    void KeyBinder::compilePointerLists()
210    {
211        allButtons_.clear();
212        allHalfAxes_.clear();
213
[2662]214        // Note: Don't include the dummy keys which don't actually exist in OIS but have a number
[1887]215        for (unsigned int i = 0; i < KeyCode::numberOfKeys; i++)
[2662]216            if (!keys_[i].name_.empty())
[3327]217                allButtons_[keys_[i].groupName_ + "." + keys_[i].name_] = keys_ + i;
[1887]218        for (unsigned int i = 0; i < numberOfMouseButtons_; i++)
[3327]219            allButtons_[mouseButtons_[i].groupName_ + "." + mouseButtons_[i].name_] = mouseButtons_ + i;
[1887]220        for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++)
221        {
[3327]222            allButtons_[mouseAxes_[i].groupName_ + "." + mouseAxes_[i].name_] = mouseAxes_ + i;
[1887]223            allHalfAxes_.push_back(mouseAxes_ + i);
[1755]224        }
[3327]225        for (unsigned int iDev = 0; iDev < joySticks_.size(); iDev++)
[1887]226        {
227            for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; i++)
[3327]228                allButtons_[joyStickButtons_[iDev][i].groupName_ + "." + joyStickButtons_[iDev][i].name_] = &(joyStickButtons_[iDev][i]);
[1887]229            for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++)
230            {
[3327]231                allButtons_[joyStickAxes_[iDev][i].groupName_ + "." + joyStickAxes_[iDev][i].name_] = &(joyStickAxes_[iDev][i]);
[1887]232                allHalfAxes_.push_back(&(joyStickAxes_[iDev][i]));
233            }
234        }
[1349]235    }
236
[1755]237    /**
238    @brief
[1887]239        Loads the key and button bindings.
240    @return
241        True if loading succeeded.
[1755]242    */
[2710]243    void KeyBinder::loadBindings(const std::string& filename)
[1349]244    {
[1887]245        COUT(3) << "KeyBinder: Loading key bindings..." << std::endl;
[1428]246
[2103]247        if (filename.empty())
[1887]248            return;
[1293]249
[3280]250        if (this->configFile_ == ConfigFileType::NoType)
251        {
252            // Get a new ConfigFileType from the ConfigFileManager
253            this->configFile_ = ConfigFileManager::getInstance().getNewConfigFileType();
254        }
255
[2103]256        ConfigFileManager::getInstance().setFilename(this->configFile_, filename);
[1219]257
[1887]258        // Parse bindings and create the ConfigValueContainers if necessary
259        clearBindings();
260        for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it)
[2103]261            it->second->readConfigValue(this->configFile_);
[1349]262
[1887]263        COUT(3) << "KeyBinder: Loading key bindings done." << std::endl;
264    }
265
266    bool KeyBinder::setBinding(const std::string& binding, const std::string& name, bool bTemporary)
267    {
268        std::map<std::string, Button*>::iterator it = allButtons_.find(name);
269        if (it != allButtons_.end())
270        {
271            if (bTemporary)
272                it->second->configContainer_->tset(binding);
273            else
274                it->second->configContainer_->set(binding);
275            it->second->configContainer_->getValue(&(it->second->bindingString_), it->second);
276            return true;
277        }
278        else
279        {
280            COUT(2) << "Could not find key/button/axis with name '" << name << "'." << std::endl;
281            return false;
282        }
283    }
284
285    /**
286    @brief
287        Overwrites all bindings with ""
288    */
289    void KeyBinder::clearBindings()
290    {
291        for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it)
292            it->second->clear();
293
[1755]294        for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
295            delete paramCommandBuffer_[i];
296        paramCommandBuffer_.clear();
297    }
[1349]298
[1755]299    void KeyBinder::resetJoyStickAxes()
[1461]300    {
[3327]301        for (unsigned int iDev = 0; iDev < joySticks_.size(); ++iDev)
[1755]302        {
[1887]303            for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++)
304            {
305                joyStickAxes_[iDev][i].absVal_ = 0.0f;
306                joyStickAxes_[iDev][i].relVal_ = 0.0f;
307            }
[1755]308        }
[1461]309    }
310
[3327]311    void KeyBinder::mouseUpdated(float dt)
[1349]312    {
[1755]313        if (bDeriveMouseInput_)
[1349]314        {
[2662]315            // only update when derivation dt has passed
[1755]316            if (deriveTime_ > derivePeriod_)
317            {
318                for (int i = 0; i < 2; i++)
319                {
[2662]320                    if (mouseRelative_[i] < 0)
[1755]321                    {
[1887]322                        mouseAxes_[2*i + 0].absVal_
[3196]323                            = -mouseRelative_[i] / deriveTime_ * 0.0005f * mouseSensitivityDerived_;
[1887]324                        mouseAxes_[2*i + 1].absVal_ = 0.0f;
[1755]325                    }
[2662]326                    else if (mouseRelative_[i] > 0)
[1755]327                    {
[1887]328                        mouseAxes_[2*i + 0].absVal_ = 0.0f;
329                        mouseAxes_[2*i + 1].absVal_
[3196]330                            =  mouseRelative_[i] / deriveTime_ * 0.0005f * mouseSensitivityDerived_;
[1755]331                    }
332                    else
333                    {
[1887]334                        mouseAxes_[2*i + 0].absVal_ = 0.0f;
335                        mouseAxes_[2*i + 1].absVal_ = 0.0f;
[1755]336                    }
337                    mouseRelative_[i] = 0;
[1887]338                    mouseAxes_[2*i + 0].hasChanged_ = true;
339                    mouseAxes_[2*i + 1].hasChanged_ = true;
[1755]340                }
341                deriveTime_ = 0.0f;
342            }
343            else
344                deriveTime_ += dt;
[1349]345        }
[1219]346
[2087]347        for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++)
[1349]348        {
[2087]349            // Why dividing relative value by dt? The reason lies in the simple fact, that when you
350            // press a button that has relative movement, that value has to be multiplied by dt to be
[2896]351            // frame rate independent. This can easily (and only) be done in updateInput(float).
[2087]352            // Hence we need to divide by dt here for the mouse to compensate, because the relative
353            // move movements have nothing to do with dt.
354            if (dt != 0.0f)
[1755]355            {
[2087]356                // just ignore if dt == 0.0 because we have multiplied by 0.0 anyway..
357                mouseAxes_[i].relVal_ /= dt;
[1755]358            }
359
[2087]360            tickHalfAxis(mouseAxes_[i]);
361        }
362    }
363
[3327]364    void KeyBinder::joyStickUpdated(unsigned int joyStick, float dt)
[2087]365    {
366        for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++)
367        {
368            tickHalfAxis(joyStickAxes_[joyStick][i]);
369        }
370    }
371
372    void KeyBinder::tickHalfAxis(HalfAxis& halfAxis)
373    {
374        // button mode
375        // TODO: optimize out all the half axes that don't act as a button at the moment
376        if (halfAxis.hasChanged_)
377        {
378            if (!halfAxis.pressed_ && halfAxis.absVal_ > halfAxis.buttonThreshold_)
[1755]379            {
[2087]380                // key pressed event
381                halfAxis.pressed_ = true;
382                if (halfAxis.nCommands_[KeybindMode::OnPress])
383                    halfAxis.execute(KeybindMode::OnPress);
[1755]384            }
[2087]385            else if (halfAxis.pressed_ && halfAxis.absVal_ < halfAxis.buttonThreshold_)
[1755]386            {
[2087]387                // key released event
388                halfAxis.pressed_ = false;
389                if (halfAxis.nCommands_[KeybindMode::OnRelease])
390                    halfAxis.execute(KeybindMode::OnRelease);
[1755]391            }
[2087]392            halfAxis.hasChanged_ = false;
[1349]393        }
[2087]394
395        if (halfAxis.pressed_)
396        {
397            // key held event
398            if (halfAxis.nCommands_[KeybindMode::OnHold])
399                halfAxis.execute(KeybindMode::OnHold);
400        }
401
402        // these are the actually useful axis bindings for analog input
403        if (!bFilterAnalogNoise_ || halfAxis.relVal_ > analogThreshold_ || halfAxis.absVal_ > analogThreshold_)
404        {
405            halfAxis.execute();
406        }
[1349]407    }
408
[1755]409    /**
410    @brief
411        Event handler for the mouseMoved Event.
412    @param e
413        Mouse state information
414    */
415    void KeyBinder::mouseMoved(IntVector2 abs_, IntVector2 rel_, IntVector2 clippingSize)
416    {
417        // y axis of mouse input is inverted
418        int rel[] = { rel_.x, -rel_.y };
[1349]419
[2087]420        if (bDeriveMouseInput_)
[1755]421        {
[2087]422            mouseRelative_[0] += rel[0];
423            mouseRelative_[1] += rel[1];
424        }
425        else
426        {
[1755]427            for (int i = 0; i < 2; i++)
428            {
[2662]429                if (rel[i]) // performance opt. for the case that rel[i] == 0
[1755]430                {
[1887]431                    // write absolute values
432                    mouseAxes_[2*i + 0].hasChanged_ = true;
433                    mouseAxes_[2*i + 1].hasChanged_ = true;
[1755]434                    mousePosition_[i] += rel[i];
[1349]435
[1887]436                    // clip absolute position
437                    if (mousePosition_[i] > mouseClippingSize_)
438                        mousePosition_[i] =  mouseClippingSize_;
439                    if (mousePosition_[i] < -mouseClippingSize_)
440                        mousePosition_[i] = -mouseClippingSize_;
[1428]441
[2662]442                    if (mousePosition_[i] < 0)
[1755]443                    {
[3301]444                        mouseAxes_[2*i + 0].absVal_ = -mouseSensitivity_ * mousePosition_[i] / mouseClippingSize_;
445                        mouseAxes_[2*i + 1].absVal_ = 0.0f;
[1755]446                    }
447                    else
448                    {
[3301]449                        mouseAxes_[2*i + 0].absVal_ = 0.0f;
450                        mouseAxes_[2*i + 1].absVal_ =  mouseSensitivity_ * mousePosition_[i] / mouseClippingSize_;
[1755]451                    }
452                }
453            }
454        }
[1428]455
[1755]456        // relative
457        for (int i = 0; i < 2; i++)
458        {
[2662]459            if (rel[i] < 0)
[3301]460                mouseAxes_[0 + 2*i].relVal_ = -mouseSensitivity_ * rel[i] / mouseClippingSize_;
[1755]461            else
[3301]462                mouseAxes_[1 + 2*i].relVal_ =  mouseSensitivity_ * rel[i] / mouseClippingSize_;
[1349]463        }
464    }
[1428]465
[1755]466    /**
[1349]467    @brief Event handler for the mouseScrolled Event.
468    @param e Mouse state information
[1755]469    */
470    void KeyBinder::mouseScrolled(int abs, int rel)
[1349]471    {
[2662]472        if (rel < 0)
473            for (int i = 0; i < -rel/mouseWheelStepSize_; i++)
[3301]474                mouseButtons_[8].execute(KeybindMode::OnPress, static_cast<float>(abs)/mouseWheelStepSize_);
[1755]475        else
[2662]476            for (int i = 0; i < rel/mouseWheelStepSize_; i++)
[3301]477                mouseButtons_[9].execute(KeybindMode::OnPress, static_cast<float>(abs)/mouseWheelStepSize_);
[1349]478    }
[1755]479
[3327]480    void KeyBinder::axisMoved(unsigned int device, unsigned int axisID, float value)
[1349]481    {
[3327]482        int i = axisID * 2;
483        JoyStickAxisVector& axis = joyStickAxes_[device];
[2662]484        if (value < 0)
[1755]485        {
[3327]486            axis[i].absVal_ = -value;
487            axis[i].relVal_ = -value;
488            axis[i].hasChanged_ = true;
489            if (axis[i + 1].absVal_ > 0.0f)
[1755]490            {
[3327]491                axis[i + 1].absVal_ = -0.0f;
492                axis[i + 1].relVal_ = -0.0f;
493                axis[i + 1].hasChanged_ = true;
[1755]494            }
495        }
496        else
497        {
[3327]498            axis[i + 1].absVal_ = value;
499            axis[i + 1].relVal_ = value;
500            axis[i + 1].hasChanged_ = true;
501            if (axis[i].absVal_ > 0.0f)
[1755]502            {
[3327]503                axis[i].absVal_ = -0.0f;
504                axis[i].relVal_ = -0.0f;
505                axis[i].hasChanged_ = true;
[1755]506            }
507        }
[1349]508    }
[971]509}
Note: See TracBrowser for help on using the repository browser.