Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/input/Button.cc @ 1887

Last change on this file since 1887 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: 8.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
32    Implementation of the different input handlers.
33*/
34
35#include "Button.h"
36
37#include "util/Convert.h"
38#include "util/SubString.h"
39#include "util/String.h"
40#include "util/Debug.h"
41#include "core/ConsoleCommand.h"
42#include "core/CommandEvaluation.h"
43#include "core/CommandExecutor.h"
44#include "core/ConfigValueContainer.h"
45
46namespace orxonox
47{
48    /**
49    @note
50        bButtonThresholdUser_: We set it to true so that setConfigValues in KeyBinder sets the value
51        correctly the first time. It is then set to false first and changed later in Button::parse().
52    */
53    Button::Button()
54        : configContainer_(0)
55        , bButtonThresholdUser_(false)
56        , paramCommandBuffer_(0)
57    {
58        nCommands_[0]=0;
59        nCommands_[1]=0;
60        nCommands_[2]=0;
61        clear();
62    }
63
64    void Button::clear()
65    {
66        for (unsigned int j = 0; j < 3; j++)
67        {
68            if (nCommands_[j])
69            {
70                // delete all commands and the command pointer array
71                for (unsigned int i = 0; i < nCommands_[j]; i++)
72                    delete commands_[j][i];
73                delete[] commands_[j];
74                commands_[j] = 0;
75                nCommands_[j] = 0;
76            }
77            else
78            {
79                commands_[j] = 0;
80            }
81        }
82    }
83
84    void Button::readConfigValue()
85    {
86        // create/get ConfigValueContainer
87        if (!configContainer_)
88        {
89            configContainer_ = new ConfigValueContainer(CFT_Keybindings, 0, groupName_, name_, "", name_);
90            configContainer_->callback(this, &Button::parse);
91        }
92        configContainer_->getValue(&bindingString_, this);
93    }
94
95    void Button::parse()
96    {
97        // delete all commands
98        clear();
99
100        if (isEmpty(bindingString_))
101            return;
102
103        // reset this to false first when parsing (was true before when parsing for the first time)
104        bButtonThresholdUser_ = false;
105
106        // use std::vector for a temporary dynamic array
107        std::vector<BaseCommand*> commands[3];
108
109        // separate the commands
110        SubString commandStrings(bindingString_, "|", SubString::WhiteSpaces, false,
111            '\\', false, '"', false, '(', ')', false, '\0');
112
113        for (unsigned int iCommand = 0; iCommand < commandStrings.size(); iCommand++)
114        {
115            if (commandStrings[iCommand] != "")
116            {
117                SubString tokens(commandStrings[iCommand], " ", SubString::WhiteSpaces, false,
118                    '\\', false, '"', false, '(', ')', false, '\0');
119
120                KeybindMode::Enum mode = KeybindMode::None;
121                float paramModifier = 1.0f;
122                std::string commandStr = "";
123
124                for (unsigned int iToken = 0; iToken < tokens.size(); ++iToken)
125                {
126                    std::string token = getLowercase(tokens[iToken]);
127
128                    if (token == "onpress")
129                        mode = KeybindMode::OnPress;
130                    else if (token == "onrelease")
131                        mode = KeybindMode::OnRelease;
132                    else if (token == "onhold")
133                        mode = KeybindMode::OnHold;
134                    else if (token == "buttonthreshold")
135                    {
136                        // for real axes, we can feed a ButtonThreshold argument
137                        ++iToken;
138                        if (iToken == tokens.size())
139                            continue;
140                        // may fail, but doesn't matter (default value already set)
141                        if (!convertValue(&buttonThreshold_, tokens[iToken + 1]))
142                            parseError("Could not parse 'ButtonThreshold' argument. \
143                                Switching to default value.", true);
144                        else
145                            this->bButtonThresholdUser_ = true;
146                    }
147                    else if (token == "scale")
148                    {
149                        ++iToken;
150                        if (iToken == tokens.size() || !convertValue(&paramModifier, tokens[iToken]))
151                            parseError("Could not parse 'scale' argument. Switching to default value.", true);
152                    }
153                    else
154                    {
155                        // no input related argument
156                        // we interpret everything from here as a command string
157                        while (iToken != tokens.size())
158                            commandStr += tokens[iToken++] + " ";
159                    }
160                }
161
162                if (commandStr == "")
163                {
164                    parseError("No command string given.", false);
165                    continue;
166                }
167
168                // evaluate the command
169                CommandEvaluation eval = CommandExecutor::evaluate(commandStr);
170                if (!eval.isValid())
171                {
172                    parseError("Command evaluation failed.", true);
173                    continue;
174                }
175
176                // check for param command
177                int paramIndex = eval.getConsoleCommand()->getAxisParamIndex();
178                if (paramIndex >= 0)
179                {
180                    // parameter supported command
181                    ParamCommand* cmd = new ParamCommand();
182                    cmd->paramModifier_ = paramModifier;
183                    cmd->bRelative_ = eval.getConsoleCommand()->getIsAxisRelative();
184
185                    // add command to the buffer if not yet existing
186                    for (unsigned int iParamCmd = 0; iParamCmd < paramCommandBuffer_->size(); iParamCmd++)
187                    {
188                        if (getLowercase((*paramCommandBuffer_)[iParamCmd]->evaluation_.getOriginalCommand())
189                            == getLowercase(commandStr))
190                        {
191                            // already in list
192                            cmd->paramCommand_ = (*paramCommandBuffer_)[iParamCmd];
193                            break;
194                        }
195                    }
196                    if (cmd->paramCommand_ == 0)
197                    {
198                        cmd->paramCommand_ = new BufferedParamCommand();
199                        paramCommandBuffer_->push_back(cmd->paramCommand_);
200                        cmd->paramCommand_->evaluation_ = eval;
201                        cmd->paramCommand_->paramIndex_ = paramIndex;
202                    }
203
204
205                    // we don't know whether this is an actual axis or just a button
206                    if (mode == KeybindMode::None)
207                    {
208                        if (!addParamCommand(cmd))
209                        {
210                            mode = eval.getConsoleCommand()->getKeybindMode();
211                            commands[mode].push_back(cmd);
212                        }
213                    }
214                }
215                else
216                {
217                    SimpleCommand* cmd = new SimpleCommand();
218                    cmd->evaluation_ = eval;
219
220                    if (mode == KeybindMode::None)
221                        mode = eval.getConsoleCommand()->getKeybindMode();
222
223                    commands[mode].push_back(cmd);
224                }
225            }
226        }
227
228        for (unsigned int j = 0; j < 3; j++)
229        {
230            nCommands_[j] = commands[j].size();
231            if (nCommands_[j])
232            {
233                commands_[j] = new BaseCommand*[nCommands_[j]];
234                for (unsigned int i = 0; i < commands[j].size(); i++)
235                    commands_[j][i] = commands[j][i];
236            }
237            else
238                commands_[j] = 0;
239        }
240    }
241
242    inline void Button::parseError(std::string message, bool serious)
243    {
244        if (serious)
245        {
246            COUT(2) << "Error while parsing binding for button/axis " << this->name_ << ". "
247                << message << std::endl;
248        }
249        else
250        {
251            COUT(3) << "Warning while parsing binding for button/axis " << this->name_ << ". "
252                << message << std::endl;
253        }
254    }
255}
Note: See TracBrowser for help on using the repository browser.