Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/weapon/src/core/input/KeyBinder.cc @ 2255

Last change on this file since 2255 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
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#include <fstream>
36#include <string>
37#include "util/Convert.h"
38#include "util/Debug.h"
39#include "core/ConfigValueIncludes.h"
40#include "core/CoreIncludes.h"
41#include "core/ConfigFileManager.h"
42#include "InputCommands.h"
43#include "InputManager.h"
44
45namespace orxonox
46{
47    /**
48    @brief
49        Constructor that does as little as necessary.
50    */
51    KeyBinder::KeyBinder()
52        : numberOfJoySticks_(0)
53        , deriveTime_(0.0f)
54    {
55        mouseRelative_[0] = 0;
56        mouseRelative_[1] = 0;
57        mousePosition_[0] = 0;
58        mousePosition_[1] = 0;
59
60        RegisterRootObject(KeyBinder);
61
62        // intialise all buttons and half axes to avoid creating everything with 'new'
63        // keys
64        for (unsigned int i = 0; i < KeyCode::numberOfKeys; i++)
65        {
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";
82        }
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++)
86        {
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";
95        }
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        }
107
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();
117    }
118
119    /**
120    @brief
121        Destructor
122    */
123    KeyBinder::~KeyBinder()
124    {
125        // almost no destructors required because most of the arrays are static.
126        clearBindings(); // does some destruction work
127    }
128
129    /**
130    @brief
131        Loader for the key bindings, managed by config values.
132    */
133    void KeyBinder::setConfigValues()
134    {
135        SetConfigValue(defaultKeybindings_, "def_keybindings.ini")
136            .description("Filename of default keybindings.");
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.");
147        SetConfigValue(mouseWheelStepSize_, 120.0f)
148            .description("Equals one step of the mousewheel.");
149        SetConfigValue(buttonThreshold_, 0.80f)
150            .description("Threshold for analog axes until which the button is not pressed.")
151            .callback(this, &KeyBinder::buttonThresholdChanged);
152    }
153
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_;
159    }
160
161    void KeyBinder::JoyStickDeviceNumberChanged(unsigned int value)
162    {
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())
174        {
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            }
182        }
183
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++)
195        {
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    }
217
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);
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)
251    {
252        COUT(3) << "KeyBinder: Loading key bindings..." << std::endl;
253
254        configFile_ = filename;
255        if (configFile_.empty())
256            return;
257
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_);
269
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();
274
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
306        for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
307            delete paramCommandBuffer_[i];
308        paramCommandBuffer_.clear();
309    }
310
311    void KeyBinder::resetJoyStickAxes()
312    {
313        for (unsigned int iDev = 0; iDev < numberOfJoySticks_; ++iDev)
314        {
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            }
320        }
321    }
322
323    void KeyBinder::tickMouse(float dt)
324    {
325        tickDevices(mouseAxes_, mouseAxes_ + MouseAxisCode::numberOfAxes * 2);
326
327        if (bDeriveMouseInput_)
328        {
329            if (deriveTime_ > derivePeriod_)
330            {
331                for (int i = 0; i < 2; i++)
332                {
333                    if (mouseRelative_[i] > 0)
334                    {
335                        mouseAxes_[2*i + 0].absVal_
336                            =  mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_;
337                        mouseAxes_[2*i + 1].absVal_ = 0.0f;
338                    }
339                    else if (mouseRelative_[i] < 0)
340                    {
341                        mouseAxes_[2*i + 0].absVal_ = 0.0f;
342                        mouseAxes_[2*i + 1].absVal_
343                            = -mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_;
344                    }
345                    else
346                    {
347                        mouseAxes_[2*i + 0].absVal_ = 0.0f;
348                        mouseAxes_[2*i + 1].absVal_ = 0.0f;
349                    }
350                    mouseRelative_[i] = 0;
351                    mouseAxes_[2*i + 0].hasChanged_ = true;
352                    mouseAxes_[2*i + 1].hasChanged_ = true;
353                }
354                deriveTime_ = 0.0f;
355            }
356            else
357                deriveTime_ += dt;
358        }
359    }
360
361    void KeyBinder::tickDevices(HalfAxis* begin, HalfAxis* end)
362    {
363        for (HalfAxis* current = begin; current < end; ++current) // pointer arithmetic
364        {
365            // button mode
366            // TODO: optimize out all the half axes that don't act as a button at the moment
367            if (current->hasChanged_)
368            {
369                if (!current->wasDown_ && current->absVal_ > current->buttonThreshold_)
370                {
371                    current->wasDown_ = true;
372                    if (current->nCommands_[KeybindMode::OnPress])
373                        current->execute(KeybindMode::OnPress);
374                }
375                else if (current->wasDown_ && current->absVal_ < current->buttonThreshold_)
376                {
377                    current->wasDown_ = false;
378                    if (current->nCommands_[KeybindMode::OnRelease])
379                        current->execute(KeybindMode::OnRelease);
380                }
381                current->hasChanged_ = false;
382            }
383
384            if (current->wasDown_)
385            {
386                if (current->nCommands_[KeybindMode::OnHold])
387                    current->execute(KeybindMode::OnHold);
388            }
389
390            // these are the actually useful axis bindings for analog input
391            if (current->relVal_ > analogThreshold_ || current->absVal_ > analogThreshold_)
392            {
393                current->execute();
394            }
395        }
396    }
397
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 };
408
409        if (!bDeriveMouseInput_)
410        {
411            for (int i = 0; i < 2; i++)
412            {
413                if (rel[i]) // performance opt. if rel[i] == 0
414                {
415                    // write absolute values
416                    mouseAxes_[2*i + 0].hasChanged_ = true;
417                    mouseAxes_[2*i + 1].hasChanged_ = true;
418                    mousePosition_[i] += rel[i];
419
420                    // clip absolute position
421                    if (mousePosition_[i] > mouseClippingSize_)
422                        mousePosition_[i] =  mouseClippingSize_;
423                    if (mousePosition_[i] < -mouseClippingSize_)
424                        mousePosition_[i] = -mouseClippingSize_;
425
426                    if (mousePosition_[i] >= 0)
427                    {
428                        mouseAxes_[2*i + 0].absVal_ =   mousePosition_[i]/(float)mouseClippingSize_ * mouseSensitivity_;
429                        mouseAxes_[2*i + 1].absVal_ =  0.0f;
430                    }
431                    else
432                    {
433                        mouseAxes_[2*i + 0].absVal_ =  0.0f;
434                        mouseAxes_[2*i + 1].absVal_ =  -mousePosition_[i]/(float)mouseClippingSize_ * mouseSensitivity_;
435                    }
436                }
437            }
438        }
439        else
440        {
441            mouseRelative_[0] += rel[0];
442            mouseRelative_[1] += rel[1];
443        }
444
445        // relative
446        for (int i = 0; i < 2; i++)
447        {
448            if (rel[i] > 0)
449                mouseAxes_[0 + 2*i].relVal_ =  ((float)rel[i])/(float)mouseClippingSize_ * mouseSensitivity_;
450            else
451                mouseAxes_[1 + 2*i].relVal_ = -((float)rel[i])/(float)mouseClippingSize_ * mouseSensitivity_;
452        }
453    }
454
455    /**
456    @brief Event handler for the mouseScrolled Event.
457    @param e Mouse state information
458    */
459    void KeyBinder::mouseScrolled(int abs, int rel)
460    {
461        if (rel > 0)
462            for (int i = 0; i < rel/mouseWheelStepSize_; i++)
463                mouseButtons_[8].execute(KeybindMode::OnPress, ((float)abs)/mouseWheelStepSize_);
464        else
465            for (int i = 0; i < -rel/mouseWheelStepSize_; i++)
466                mouseButtons_[9].execute(KeybindMode::OnPress, ((float)abs)/mouseWheelStepSize_);
467    }
468
469    void KeyBinder::joyStickAxisMoved(unsigned int joyStickID, unsigned int axis, float value)
470    {
471        int i = axis * 2;
472        if (value >= 0)
473        {
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)
478            {
479                joyStickAxes_[joyStickID][i + 1].absVal_ = -0.0f;
480                joyStickAxes_[joyStickID][i + 1].relVal_ = -0.0f;
481                joyStickAxes_[joyStickID][i + 1].hasChanged_ = true;
482            }
483        }
484        else
485        {
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)
490            {
491                joyStickAxes_[joyStickID][i].absVal_ = -0.0f;
492                joyStickAxes_[joyStickID][i].relVal_ = -0.0f;
493                joyStickAxes_[joyStickID][i].hasChanged_ = true;
494            }
495        }
496    }
497}
Note: See TracBrowser for help on using the repository browser.