Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1725 was 1586, checked in by landauf, 16 years ago

moved Debug.h, OutputHandler and OutputBuffer to util, to make COUT(x) available everywhere

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