Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merged all remaining revisions from core4 back to the trunk.

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