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