Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/input/src/core/input/Button.cc @ 1630

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

converted input system to 4 spaces/tab

  • Property svn:eol-style set to native
File size: 7.7 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#include "util/Convert.h"
37#include "util/SubString.h"
38#include "util/String.h"
39#include "core/Debug.h"
40#include "core/ConsoleCommand.h"
41#include "core/CommandEvaluation.h"
42#include "core/CommandExecutor.h"
43#include "InputCommands.h"
44
45namespace orxonox
46{
47    void Button::clear()
48    {
49        for (unsigned int j = 0; j < 3; j++)
50        {
51            if (nCommands_[j])
52            {
53                // delete all commands and the command pointer array
54                for (unsigned int i = 0; i < nCommands_[j]; i++)
55                    delete commands_[j][i];
56                delete[] commands_[j];
57                commands_[j] = 0;
58                nCommands_[j] = 0;
59            }
60            else
61            {
62                commands_[j] = 0;
63            }
64        }
65    }
66
67    void Button::parse(std::vector<BufferedParamCommand*>& paramCommandBuffer)
68    {
69        if (isEmpty(bindingString_))
70        {
71            clear();
72            return;
73        }
74
75        // use std::vector for a temporary dynamic array
76        std::vector<BaseCommand*> commands[3];
77
78
79        // separate the commands
80        SubString commandStrings(bindingString_, "|", SubString::WhiteSpaces, false,
81            '\\', false, '"', false, '(', ')', false, '\0');
82
83        for (unsigned int iCommand = 0; iCommand < commandStrings.size(); iCommand++)
84        {
85            if (commandStrings[iCommand] != "")
86            {
87                SubString tokens(commandStrings[iCommand], " ", SubString::WhiteSpaces, false,
88                    '\\', false, '"', false, '(', ')', false, '\0');
89
90                unsigned int iToken = 0;
91
92                // for real axes, we can feed a ButtonThreshold argument as entire command
93                if (getLowercase(tokens[0]) == "buttonthreshold")
94                {
95                    if (tokens.size() == 1)
96                        continue;
97                    // may fail, but doesn't matter
98                    convertValue(&buttonThreshold_, tokens[1]);
99                    continue;
100                }
101
102                // first argument can be OnPress, OnHold OnRelease or nothing
103                KeybindMode::Enum mode = KeybindMode::None;
104                if (getLowercase(tokens[iToken]) == "onpress")
105                    mode = KeybindMode::OnPress,   iToken++;
106                if (getLowercase(tokens[iToken]) == "onrelease")
107                    mode = KeybindMode::OnRelease, iToken++;
108                if (getLowercase(tokens[iToken]) == "onhold")
109                    mode = KeybindMode::OnHold,    iToken++;
110
111                if (iToken == tokens.size())
112                    continue;
113
114                // second argument can be the amplitude for the case it as an axis command
115                // default amplitude is 1.0f
116                float paramModifier = 1.0f;
117                if (getLowercase(tokens[iToken]) == "scale")
118                {
119                    iToken++;
120                    if (iToken == tokens.size() || !convertValue(&paramModifier, tokens[iToken]))
121                    {
122                        COUT(2) << "Error while parsing key binding " << name_
123                                << ". Numeric expression expected afer 'AxisAmp', switching to default value"
124                                << std::endl;
125                        if (iToken == tokens.size())
126                            continue;
127                    }
128                    iToken++;
129                }
130
131                // no more arguments expected except for the actual command
132                if (iToken == tokens.size())
133                    continue;
134
135                std::string commandStr;
136                while (iToken != tokens.size())
137                    commandStr += tokens[iToken++] + " ";
138
139                // evaluate the command
140                CommandEvaluation eval = CommandExecutor::evaluate(commandStr);
141                if (!eval.isValid())
142                    continue;
143
144                // check for param command
145                int paramIndex = eval.getConsoleCommand()->getAxisParamIndex();
146                if (paramIndex >= 0)
147                {
148                    // parameter supported command
149                    ParamCommand* cmd = new ParamCommand();
150                    cmd->paramModifier_ = paramModifier;
151                    cmd->bRelative_ = eval.getConsoleCommand()->getIsAxisRelative();
152
153                    // add command to the buffer if not yet existing
154                    for (unsigned int iParamCmd = 0; iParamCmd < paramCommandBuffer.size(); iParamCmd++)
155                    {
156                        if (getLowercase(paramCommandBuffer[iParamCmd]->evaluation_.getOriginalCommand())
157                            == getLowercase(commandStr))
158                        {
159                            // already in list
160                            cmd->paramCommand_ = paramCommandBuffer[iParamCmd];
161                            break;
162                        }
163                    }
164                    if (cmd->paramCommand_ == 0)
165                    {
166                        cmd->paramCommand_ = new BufferedParamCommand();
167                        paramCommandBuffer.push_back(cmd->paramCommand_);
168                        cmd->paramCommand_->evaluation_ = eval;
169                        cmd->paramCommand_->paramIndex_ = paramIndex;
170                    }
171
172
173                    // we don't know whether this is an actual axis or just a button
174                    if (mode == KeybindMode::None)
175                    {
176                        if (!addParamCommand(cmd))
177                        {
178                            mode = eval.getConsoleCommand()->getKeybindMode();
179                            commands[mode].push_back(cmd);
180                        }
181                    }
182                }
183                else
184                {
185                    SimpleCommand* cmd = new SimpleCommand();
186                    cmd->evaluation_ = eval;
187
188                    if (mode == KeybindMode::None)
189                        mode = eval.getConsoleCommand()->getKeybindMode();
190
191                    commands[mode].push_back(cmd);
192                }
193            }
194        }
195
196        for (unsigned int j = 0; j < 3; j++)
197        {
198            nCommands_[j] = commands[j].size();
199            if (nCommands_[j])
200            {
201                commands_[j] = new BaseCommand*[nCommands_[j]];
202                for (unsigned int i = 0; i < commands[j].size(); i++)
203                    commands_[j][i] = commands[j][i];
204            }
205            else
206                commands_[j] = 0;
207        }
208    }
209
210    bool Button::execute(KeybindMode::Enum mode, float abs, float rel)
211    {
212        // execute all the parsed commands in the string
213        for (unsigned int iCommand = 0; iCommand < nCommands_[mode]; iCommand++)
214            commands_[mode][iCommand]->execute(abs, rel);
215        return true;
216    }
217}
Note: See TracBrowser for help on using the repository browser.