Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11071 was 11071, checked in by landauf, 8 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 9.8 KB
RevLine 
[1526]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/**
[1755]30@file
31@brief
32    Implementation of the different input handlers.
33*/
[1526]34
35#include "Button.h"
[1887]36
[1526]37#include "util/Convert.h"
38#include "util/SubString.h"
[3280]39#include "util/StringUtils.h"
[8858]40#include "util/Output.h"
[7284]41#include "core/command/ConsoleCommand.h"
42#include "core/command/CommandEvaluation.h"
43#include "core/command/CommandExecutor.h"
[9667]44#include "core/config/ConfigFile.h"
[1526]45
46namespace orxonox
47{
[1887]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()
[6428]54        : bButtonThresholdUser_(false)
[11071]55        , paramCommandBuffer_(nullptr)
[1887]56    {
57        nCommands_[0]=0;
58        nCommands_[1]=0;
59        nCommands_[2]=0;
60    }
61
[2662]62    Button::~Button()
63    {
64        this->clear();
65    }
66
[1755]67    void Button::clear()
[1526]68    {
[1755]69        for (unsigned int j = 0; j < 3; j++)
70        {
71            if (nCommands_[j])
72            {
73                // delete all commands and the command pointer array
74                for (unsigned int i = 0; i < nCommands_[j]; i++)
75                    delete commands_[j][i];
76                delete[] commands_[j];
[11071]77                commands_[j] = nullptr;
[1755]78                nCommands_[j] = 0;
79            }
80        }
[6428]81        this->bindingString_.clear();
[1526]82    }
83
[6536]84    void Button::readBinding(ConfigFile* configFile, ConfigFile* fallbackFile)
[1526]85    {
[6536]86        std::string binding = configFile->getOrCreateValue(groupName_, name_, "", true);
87        if (binding.empty() && fallbackFile)
88            binding = fallbackFile->getValue(groupName_, name_, true);
[6428]89        this->parse(binding);
[1887]90    }
[1526]91
[6536]92    void Button::setBinding(ConfigFile* configFile, ConfigFile* fallbackFile, const std::string& binding, bool bTemporary)
[1887]93    {
[6428]94        if (!bTemporary)
[6536]95            configFile->setValue(groupName_, name_, binding, true);
[6428]96        this->parse(binding);
97    }
98
99    void Button::parse(const std::string& binding)
100    {
101        if (binding == this->bindingString_)
102            return;
103
[1887]104        // delete all commands
105        clear();
[6428]106        this->bindingString_ = binding;
[1887]107
[6536]108        if (isEmpty(bindingString_) || removeTrailingWhitespaces(getLowercase(binding)) == "nobinding")
[1887]109            return;
110
111        // reset this to false first when parsing (was true before when parsing for the first time)
112        bButtonThresholdUser_ = false;
113
[1755]114        // use std::vector for a temporary dynamic array
115        std::vector<BaseCommand*> commands[3];
[1526]116
[1755]117        // separate the commands
118        SubString commandStrings(bindingString_, "|", SubString::WhiteSpaces, false,
[7284]119            '\\', false, '"', false, '{', '}', false, '\0');
[1526]120
[1755]121        for (unsigned int iCommand = 0; iCommand < commandStrings.size(); iCommand++)
[1526]122        {
[6417]123            if (!commandStrings[iCommand].empty())
[1755]124            {
125                SubString tokens(commandStrings[iCommand], " ", SubString::WhiteSpaces, false,
[7284]126                    '\\', false, '"', false, '{', '}', false, '\0');
[1526]127
[3280]128                KeybindMode::Value mode = KeybindMode::None;
[1755]129                float paramModifier = 1.0f;
[6417]130                std::string commandStr;
[9982]131                bool forceArguments = false;
[1526]132
[1755]133                for (unsigned int iToken = 0; iToken < tokens.size(); ++iToken)
134                {
[6417]135                    const std::string& token = getLowercase(tokens[iToken]);
[1526]136
[1755]137                    if (token == "onpress")
138                        mode = KeybindMode::OnPress;
139                    else if (token == "onrelease")
140                        mode = KeybindMode::OnRelease;
[9978]141                    else if (token == "onpressandrelease")
142                        mode = KeybindMode::OnPressAndRelease;
[1755]143                    else if (token == "onhold")
144                        mode = KeybindMode::OnHold;
[9982]145                    else if (token == "forcearguments")
146                        forceArguments = true;
[1755]147                    else if (token == "buttonthreshold")
148                    {
149                        // for real axes, we can feed a ButtonThreshold argument
150                        ++iToken;
151                        if (iToken == tokens.size())
152                            continue;
153                        // may fail, but doesn't matter (default value already set)
154                        if (!convertValue(&buttonThreshold_, tokens[iToken + 1]))
155                            parseError("Could not parse 'ButtonThreshold' argument. \
156                                Switching to default value.", true);
[1887]157                        else
158                            this->bButtonThresholdUser_ = true;
[1755]159                    }
160                    else if (token == "scale")
161                    {
162                        ++iToken;
163                        if (iToken == tokens.size() || !convertValue(&paramModifier, tokens[iToken]))
164                            parseError("Could not parse 'scale' argument. Switching to default value.", true);
165                    }
166                    else
167                    {
168                        // no input related argument
169                        // we interpret everything from here as a command string
170                        while (iToken != tokens.size())
[6417]171                            commandStr += tokens[iToken++] + ' ';
[1755]172                    }
173                }
[1526]174
[6417]175                if (commandStr.empty())
[1755]176                {
177                    parseError("No command string given.", false);
178                    continue;
179                }
[1526]180
[1755]181                // evaluate the command
[7284]182                CommandEvaluation eval = CommandExecutor::evaluate(commandStr);
[7401]183                if (!eval.isValid() || eval.evaluateArguments(true))
[1755]184                {
[5929]185                    parseError("Command evaluation of \"" + commandStr + "\"failed.", true);
[1755]186                    continue;
187                }
[1526]188
[1755]189                // check for param command
[9983]190                int paramIndex = eval.getConsoleCommand()->getInputConfiguredParam();
[9982]191                if (paramIndex >= 0 && !forceArguments)
[1755]192                {
193                    // parameter supported command
194                    ParamCommand* cmd = new ParamCommand();
[2087]195                    cmd->scale_ = paramModifier;
[1526]196
[1755]197                    // add command to the buffer if not yet existing
[11071]198                    for (BufferedParamCommand* command : *paramCommandBuffer_)
[1755]199                    {
[11071]200                        if (command->evaluation_.getConsoleCommand()
[2087]201                            == eval.getConsoleCommand())
[1755]202                        {
203                            // already in list
[11071]204                            cmd->paramCommand_ = command;
[1755]205                            break;
206                        }
207                    }
[11071]208                    if (cmd->paramCommand_ == nullptr)
[1755]209                    {
210                        cmd->paramCommand_ = new BufferedParamCommand();
[1887]211                        paramCommandBuffer_->push_back(cmd->paramCommand_);
[1755]212                        cmd->paramCommand_->evaluation_ = eval;
213                        cmd->paramCommand_->paramIndex_ = paramIndex;
214                    }
[1526]215
216
[1755]217                    // we don't know whether this is an actual axis or just a button
[9981]218                    if (mode != KeybindMode::None || !addParamCommand(cmd))
219                        this->addCommand(cmd, mode, commands);
[1755]220                }
221                else
222                {
223                    SimpleCommand* cmd = new SimpleCommand();
224                    cmd->evaluation_ = eval;
[1526]225
[9978]226                    this->addCommand(cmd, mode, commands);
[1755]227                }
[1526]228            }
229        }
[1755]230
231        for (unsigned int j = 0; j < 3; j++)
[1526]232        {
[1755]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
[11071]241                commands_[j] = nullptr;
[1526]242        }
243    }
244
[9978]245    inline void Button::addCommand(BaseCommand* cmd, KeybindMode::Value mode, std::vector<BaseCommand*> commands[3])
246    {
[9981]247        if (mode == KeybindMode::None)
248            mode = cmd->getEvaluation()->getConsoleCommand()->getKeybindMode();
249        else
250            cmd->setFixedKeybindMode(true);
251
[9978]252        if (mode == KeybindMode::OnPressAndRelease)
253        {
254            BaseCommand* cmd2 = cmd->clone();
255
256            commands[KeybindMode::OnPress].push_back(cmd);
257            commands[KeybindMode::OnRelease].push_back(cmd2); // clone
258        }
259        else
260            commands[mode].push_back(cmd);
261    }
262
[6417]263    inline void Button::parseError(const std::string& message, bool serious)
[1755]264    {
265        if (serious)
266        {
[8858]267            orxout(internal_error, context::input) << "Error while parsing binding for button/axis " << this->name_ << ". "
268                << message << endl;
[1755]269        }
270        else
271        {
[8858]272            orxout(internal_warning, context::input) << "Warning while parsing binding for button/axis " << this->name_ << ". "
273                << message << endl;
[1755]274        }
275    }
[1526]276}
Note: See TracBrowser for help on using the repository browser.