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