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