Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/buildsystem3/src/core/input/KeyBinder.cc @ 2685

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

Fixed install target:

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