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