Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2053 was 1887, checked in by rgrieder, 16 years ago

FIRST THINGS FIRST: Delete or rename your keybindings.ini (def_keybindings.ini already has the most important bindings) or else you won't be able to do anything!

Changes:

  • Multiple joy stick support should now fully work with KeyBinder too (only tested with 0/1 joystick)
  • Reloading the OIS Devices now works with KeyBinder too
  • Modified ConfigValueContainer to accept arbitrary section names
  • added tkeybind to temporary bind a command to a key
  • Fixed dlleport issue in ArgumentCompletionFunctions.h

Internal changes:

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