Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2672 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

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