Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/core/input/KeyBinder.cc @ 2736

Last change on this file since 2736 was 2543, checked in by rgrieder, 16 years ago
  • Fixed a bug in ConfigFileManager::getVectorSize(). If there were no entries, 1 was returned instead of 0.
  • Added getSctionName to the ConfigValueContainer
  • Bugfix in Button::clear()
  • Renamed some joy stick buttons and axes to have them sorted corrected in the config file
  • Removed annoying and useless "Key_084=" from keybindings.ini file (there were about a hundred of them)
  • Bugfix in KeyBinder: All the axes were inverted (which was then corrected in the ini file)
  • Also inverted rotateYaw in SpaceShip and Spectator because that actually corrected the bug from above ;)
  • Some small performance optimisation in InputManager
  • Property svn:eol-style set to native
File size: 18.9 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/**
30 @file
31 @brief Implementation of the different input handlers.
32 */
33
34#include "KeyBinder.h"
35
36#include <fstream>
37#include <string>
38
39#include "util/Convert.h"
40#include "util/Debug.h"
41#include "core/ConfigValueIncludes.h"
42#include "core/CoreIncludes.h"
43#include "core/ConfigFileManager.h"
44#include "InputCommands.h"
45#include "InputManager.h"
46
47namespace orxonox
48{
49    /**
50    @brief
51        Constructor that does as little as necessary.
52    */
53    KeyBinder::KeyBinder()
54        : numberOfJoySticks_(0)
55        , deriveTime_(0.0f)
56    {
57        mouseRelative_[0] = 0;
58        mouseRelative_[1] = 0;
59        mousePosition_[0] = 0;
60        mousePosition_[1] = 0;
61
62        RegisterRootObject(KeyBinder);
63
64        // intialise all buttons and half axes to avoid creating everything with 'new'
65        // keys
66        for (unsigned int i = 0; i < KeyCode::numberOfKeys; i++)
67        {
68            std::string keyname = KeyCode::ByString[i];
69            if (!keyname.empty())
70                keys_[i].name_ = std::string("Key") + keyname;
71            else
72                keys_[i].name_ = "";
73            keys_[i].paramCommandBuffer_ = &paramCommandBuffer_;
74            keys_[i].groupName_ = "Keys";
75        }
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++)
79        {
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";
88        }
89        // mouse axes
90        for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++)
91        {
92            mouseAxes_[i].name_ = std::string("Mouse") + MouseAxisCode::ByString[i / 2];
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        }
100
101        // Get a new ConfigFileType from the ConfigFileManager
102        this->configFile_ = ConfigFileManager::getInstance().getNewConfigFileType();
103
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();
113    }
114
115    /**
116    @brief
117        Destructor
118    */
119    KeyBinder::~KeyBinder()
120    {
121        // almost no destructors required because most of the arrays are static.
122        clearBindings(); // does some destruction work
123    }
124
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.");
133        SetConfigValue(bFilterAnalogNoise_, false)
134            .description("Specifies whether to filter small analog values like joy stick fluctuations.");
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.");
143        SetConfigValue(mouseWheelStepSize_, 120)
144            .description("Equals one step of the mousewheel.");
145        SetConfigValue(buttonThreshold_, 0.80f)
146            .description("Threshold for analog axes until which the button is not pressed.")
147            .callback(this, &KeyBinder::buttonThresholdChanged);
148    }
149
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_;
155    }
156
157    void KeyBinder::JoyStickDeviceNumberChanged(unsigned int value)
158    {
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
169        if (configFile_ != ConfigFileType::NoType)
170        {
171            for (unsigned int iDev = oldValue; iDev < numberOfJoySticks_; ++iDev)
172            {
173                for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; ++i)
174                    joyStickButtons_[iDev][i].readConfigValue(this->configFile_);
175                for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; ++i)
176                    joyStickAxes_[iDev][i].readConfigValue(this->configFile_);
177            }
178        }
179
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++)
191        {
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    }
213
214    void KeyBinder::compilePointerLists()
215    {
216        allButtons_.clear();
217        allHalfAxes_.clear();
218
219        // Note: Don't include the dummy keys which don't actually exist in OIS but have a number
220        for (unsigned int i = 0; i < KeyCode::numberOfKeys; i++)
221            if (!keys_[i].name_.empty())
222                allButtons_[keys_[i].name_] = keys_ + i;
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);
229        }
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        }
240    }
241
242    /**
243    @brief
244        Loads the key and button bindings.
245    @return
246        True if loading succeeded.
247    */
248    void KeyBinder::loadBindings(const std::string& filename, const std::string& defaultFilename)
249    {
250        COUT(3) << "KeyBinder: Loading key bindings..." << std::endl;
251
252        if (filename.empty())
253            return;
254
255        // get bindings from default file if filename doesn't exist.
256        std::ifstream infile;
257        infile.open(filename.c_str());
258        if (!infile)
259        {
260            ConfigFileManager::getInstance().setFilename(this->configFile_, defaultFilename);
261            ConfigFileManager::getInstance().saveAs(this->configFile_, filename);
262        }
263        else
264            infile.close();
265        ConfigFileManager::getInstance().setFilename(this->configFile_, filename);
266
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)
270            it->second->readConfigValue(this->configFile_);
271
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
303        for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
304            delete paramCommandBuffer_[i];
305        paramCommandBuffer_.clear();
306    }
307
308    void KeyBinder::resetJoyStickAxes()
309    {
310        for (unsigned int iDev = 0; iDev < numberOfJoySticks_; ++iDev)
311        {
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            }
317        }
318    }
319
320    void KeyBinder::tickMouse(float dt)
321    {
322        if (bDeriveMouseInput_)
323        {
324            // only update when derivation dt has passed
325            if (deriveTime_ > derivePeriod_)
326            {
327                for (int i = 0; i < 2; i++)
328                {
329                    if (mouseRelative_[i] < 0)
330                    {
331                        mouseAxes_[2*i + 0].absVal_
332                            = -mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_;
333                        mouseAxes_[2*i + 1].absVal_ = 0.0f;
334                    }
335                    else if (mouseRelative_[i] > 0)
336                    {
337                        mouseAxes_[2*i + 0].absVal_ = 0.0f;
338                        mouseAxes_[2*i + 1].absVal_
339                            =  mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_;
340                    }
341                    else
342                    {
343                        mouseAxes_[2*i + 0].absVal_ = 0.0f;
344                        mouseAxes_[2*i + 1].absVal_ = 0.0f;
345                    }
346                    mouseRelative_[i] = 0;
347                    mouseAxes_[2*i + 0].hasChanged_ = true;
348                    mouseAxes_[2*i + 1].hasChanged_ = true;
349                }
350                deriveTime_ = 0.0f;
351            }
352            else
353                deriveTime_ += dt;
354        }
355
356        for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++)
357        {
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
360            // frame rate independent. This can easily (and only) be done in tickInput(float).
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)
364            {
365                // just ignore if dt == 0.0 because we have multiplied by 0.0 anyway..
366                mouseAxes_[i].relVal_ /= dt;
367            }
368
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_)
388            {
389                // key pressed event
390                halfAxis.pressed_ = true;
391                if (halfAxis.nCommands_[KeybindMode::OnPress])
392                    halfAxis.execute(KeybindMode::OnPress);
393            }
394            else if (halfAxis.pressed_ && halfAxis.absVal_ < halfAxis.buttonThreshold_)
395            {
396                // key released event
397                halfAxis.pressed_ = false;
398                if (halfAxis.nCommands_[KeybindMode::OnRelease])
399                    halfAxis.execute(KeybindMode::OnRelease);
400            }
401            halfAxis.hasChanged_ = false;
402        }
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        }
416    }
417
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 };
428
429        if (bDeriveMouseInput_)
430        {
431            mouseRelative_[0] += rel[0];
432            mouseRelative_[1] += rel[1];
433        }
434        else
435        {
436            for (int i = 0; i < 2; i++)
437            {
438                if (rel[i]) // performance opt. for the case that rel[i] == 0
439                {
440                    // write absolute values
441                    mouseAxes_[2*i + 0].hasChanged_ = true;
442                    mouseAxes_[2*i + 1].hasChanged_ = true;
443                    mousePosition_[i] += rel[i];
444
445                    // clip absolute position
446                    if (mousePosition_[i] > mouseClippingSize_)
447                        mousePosition_[i] =  mouseClippingSize_;
448                    if (mousePosition_[i] < -mouseClippingSize_)
449                        mousePosition_[i] = -mouseClippingSize_;
450
451                    if (mousePosition_[i] < 0)
452                    {
453                        mouseAxes_[2*i + 0].absVal_ =  -mousePosition_[i]/(float)mouseClippingSize_ * mouseSensitivity_;
454                        mouseAxes_[2*i + 1].absVal_ =  0.0f;
455                    }
456                    else
457                    {
458                        mouseAxes_[2*i + 0].absVal_ =  0.0f;
459                        mouseAxes_[2*i + 1].absVal_ =   mousePosition_[i]/(float)mouseClippingSize_ * mouseSensitivity_;
460                    }
461                }
462            }
463        }
464
465        // relative
466        for (int i = 0; i < 2; i++)
467        {
468            if (rel[i] < 0)
469                mouseAxes_[0 + 2*i].relVal_ = -((float)rel[i])/(float)mouseClippingSize_ * mouseSensitivity_;
470            else
471                mouseAxes_[1 + 2*i].relVal_ =  ((float)rel[i])/(float)mouseClippingSize_ * mouseSensitivity_;
472        }
473    }
474
475    /**
476    @brief Event handler for the mouseScrolled Event.
477    @param e Mouse state information
478    */
479    void KeyBinder::mouseScrolled(int abs, int rel)
480    {
481        if (rel < 0)
482            for (int i = 0; i < -rel/mouseWheelStepSize_; i++)
483                mouseButtons_[8].execute(KeybindMode::OnPress, ((float)abs)/mouseWheelStepSize_);
484        else
485            for (int i = 0; i < rel/mouseWheelStepSize_; i++)
486                mouseButtons_[9].execute(KeybindMode::OnPress, ((float)abs)/mouseWheelStepSize_);
487    }
488
489    void KeyBinder::joyStickAxisMoved(unsigned int joyStickID, unsigned int axis, float value)
490    {
491        int i = axis * 2;
492        if (value < 0)
493        {
494            joyStickAxes_[joyStickID][i].absVal_ = -value;
495            joyStickAxes_[joyStickID][i].relVal_ = -value;
496            joyStickAxes_[joyStickID][i].hasChanged_ = true;
497            if (joyStickAxes_[joyStickID][i + 1].absVal_ > 0.0f)
498            {
499                joyStickAxes_[joyStickID][i + 1].absVal_ = -0.0f;
500                joyStickAxes_[joyStickID][i + 1].relVal_ = -0.0f;
501                joyStickAxes_[joyStickID][i + 1].hasChanged_ = true;
502            }
503        }
504        else
505        {
506            joyStickAxes_[joyStickID][i + 1].absVal_ = value;
507            joyStickAxes_[joyStickID][i + 1].relVal_ = value;
508            joyStickAxes_[joyStickID][i + 1].hasChanged_ = true;
509            if (joyStickAxes_[joyStickID][i].absVal_ > 0.0f)
510            {
511                joyStickAxes_[joyStickID][i].absVal_ = -0.0f;
512                joyStickAxes_[joyStickID][i].relVal_ = -0.0f;
513                joyStickAxes_[joyStickID][i].hasChanged_ = true;
514            }
515        }
516    }
517}
Note: See TracBrowser for help on using the repository browser.