[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" |
---|
| 39 | #include "util/String.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 | clear(); |
---|
| 63 | } |
---|
| 64 | |
---|
[2662] | 65 | Button::~Button() |
---|
| 66 | { |
---|
| 67 | this->clear(); |
---|
| 68 | |
---|
| 69 | if (this->configContainer_) |
---|
| 70 | delete this->configContainer_; |
---|
| 71 | } |
---|
| 72 | |
---|
[1755] | 73 | void Button::clear() |
---|
[1526] | 74 | { |
---|
[1755] | 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 | } |
---|
[1526] | 91 | } |
---|
| 92 | |
---|
[2103] | 93 | void Button::readConfigValue(ConfigFileType configFile) |
---|
[1526] | 94 | { |
---|
[1887] | 95 | // create/get ConfigValueContainer |
---|
| 96 | if (!configContainer_) |
---|
[1755] | 97 | { |
---|
[2103] | 98 | configContainer_ = new ConfigValueContainer(configFile, 0, groupName_, name_, "", name_); |
---|
[1887] | 99 | configContainer_->callback(this, &Button::parse); |
---|
[1755] | 100 | } |
---|
[1887] | 101 | configContainer_->getValue(&bindingString_, this); |
---|
| 102 | } |
---|
[1526] | 103 | |
---|
[1887] | 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 | |
---|
[1755] | 115 | // use std::vector for a temporary dynamic array |
---|
| 116 | std::vector<BaseCommand*> commands[3]; |
---|
[1526] | 117 | |
---|
[1755] | 118 | // separate the commands |
---|
| 119 | SubString commandStrings(bindingString_, "|", SubString::WhiteSpaces, false, |
---|
[1526] | 120 | '\\', false, '"', false, '(', ')', false, '\0'); |
---|
| 121 | |
---|
[1755] | 122 | for (unsigned int iCommand = 0; iCommand < commandStrings.size(); iCommand++) |
---|
[1526] | 123 | { |
---|
[1755] | 124 | if (commandStrings[iCommand] != "") |
---|
| 125 | { |
---|
| 126 | SubString tokens(commandStrings[iCommand], " ", SubString::WhiteSpaces, false, |
---|
| 127 | '\\', false, '"', false, '(', ')', false, '\0'); |
---|
[1526] | 128 | |
---|
[1755] | 129 | KeybindMode::Enum mode = KeybindMode::None; |
---|
| 130 | float paramModifier = 1.0f; |
---|
| 131 | std::string commandStr = ""; |
---|
[1526] | 132 | |
---|
[1755] | 133 | for (unsigned int iToken = 0; iToken < tokens.size(); ++iToken) |
---|
| 134 | { |
---|
| 135 | std::string token = getLowercase(tokens[iToken]); |
---|
[1526] | 136 | |
---|
[1755] | 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); |
---|
[1887] | 153 | else |
---|
| 154 | this->bButtonThresholdUser_ = true; |
---|
[1755] | 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 | } |
---|
[1526] | 170 | |
---|
[1755] | 171 | if (commandStr == "") |
---|
| 172 | { |
---|
| 173 | parseError("No command string given.", false); |
---|
| 174 | continue; |
---|
| 175 | } |
---|
[1526] | 176 | |
---|
[1755] | 177 | // evaluate the command |
---|
| 178 | CommandEvaluation eval = CommandExecutor::evaluate(commandStr); |
---|
| 179 | if (!eval.isValid()) |
---|
| 180 | { |
---|
| 181 | parseError("Command evaluation failed.", true); |
---|
| 182 | continue; |
---|
| 183 | } |
---|
[1526] | 184 | |
---|
[1755] | 185 | // check for param command |
---|
[2087] | 186 | int paramIndex = eval.getConsoleCommand()->getInputConfiguredParam_(); |
---|
[1755] | 187 | if (paramIndex >= 0) |
---|
| 188 | { |
---|
| 189 | // parameter supported command |
---|
| 190 | ParamCommand* cmd = new ParamCommand(); |
---|
[2087] | 191 | cmd->scale_ = paramModifier; |
---|
[1526] | 192 | |
---|
[1755] | 193 | // add command to the buffer if not yet existing |
---|
[1887] | 194 | for (unsigned int iParamCmd = 0; iParamCmd < paramCommandBuffer_->size(); iParamCmd++) |
---|
[1755] | 195 | { |
---|
[2087] | 196 | if ((*paramCommandBuffer_)[iParamCmd]->evaluation_.getConsoleCommand() |
---|
| 197 | == eval.getConsoleCommand()) |
---|
[1755] | 198 | { |
---|
| 199 | // already in list |
---|
[1887] | 200 | cmd->paramCommand_ = (*paramCommandBuffer_)[iParamCmd]; |
---|
[1755] | 201 | break; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | if (cmd->paramCommand_ == 0) |
---|
| 205 | { |
---|
| 206 | cmd->paramCommand_ = new BufferedParamCommand(); |
---|
[1887] | 207 | paramCommandBuffer_->push_back(cmd->paramCommand_); |
---|
[1755] | 208 | cmd->paramCommand_->evaluation_ = eval; |
---|
| 209 | cmd->paramCommand_->paramIndex_ = paramIndex; |
---|
| 210 | } |
---|
[1526] | 211 | |
---|
| 212 | |
---|
[1755] | 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; |
---|
[1526] | 227 | |
---|
[1755] | 228 | if (mode == KeybindMode::None) |
---|
| 229 | mode = eval.getConsoleCommand()->getKeybindMode(); |
---|
| 230 | |
---|
| 231 | commands[mode].push_back(cmd); |
---|
| 232 | } |
---|
[1526] | 233 | } |
---|
| 234 | } |
---|
[1755] | 235 | |
---|
| 236 | for (unsigned int j = 0; j < 3; j++) |
---|
[1526] | 237 | { |
---|
[1755] | 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; |
---|
[1526] | 247 | } |
---|
| 248 | } |
---|
| 249 | |
---|
[1755] | 250 | inline void Button::parseError(std::string message, bool serious) |
---|
| 251 | { |
---|
| 252 | if (serious) |
---|
| 253 | { |
---|
[1783] | 254 | COUT(2) << "Error while parsing binding for button/axis " << this->name_ << ". " |
---|
[1755] | 255 | << message << std::endl; |
---|
| 256 | } |
---|
| 257 | else |
---|
| 258 | { |
---|
[1783] | 259 | COUT(3) << "Warning while parsing binding for button/axis " << this->name_ << ". " |
---|
[1755] | 260 | << message << std::endl; |
---|
| 261 | } |
---|
| 262 | } |
---|
[1526] | 263 | } |
---|