[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; |
---|
| 61 | clear(); |
---|
| 62 | } |
---|
| 63 | |
---|
[1755] | 64 | void Button::clear() |
---|
[1526] | 65 | { |
---|
[1755] | 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 | } |
---|
[1526] | 82 | } |
---|
| 83 | |
---|
[2103] | 84 | void Button::readConfigValue(ConfigFileType configFile) |
---|
[1526] | 85 | { |
---|
[1887] | 86 | // create/get ConfigValueContainer |
---|
| 87 | if (!configContainer_) |
---|
[1755] | 88 | { |
---|
[2103] | 89 | configContainer_ = new ConfigValueContainer(configFile, 0, groupName_, name_, "", name_); |
---|
[1887] | 90 | configContainer_->callback(this, &Button::parse); |
---|
[1755] | 91 | } |
---|
[1887] | 92 | configContainer_->getValue(&bindingString_, this); |
---|
| 93 | } |
---|
[1526] | 94 | |
---|
[1887] | 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 | |
---|
[1755] | 106 | // use std::vector for a temporary dynamic array |
---|
| 107 | std::vector<BaseCommand*> commands[3]; |
---|
[1526] | 108 | |
---|
[1755] | 109 | // separate the commands |
---|
| 110 | SubString commandStrings(bindingString_, "|", SubString::WhiteSpaces, false, |
---|
[1526] | 111 | '\\', false, '"', false, '(', ')', false, '\0'); |
---|
| 112 | |
---|
[1755] | 113 | for (unsigned int iCommand = 0; iCommand < commandStrings.size(); iCommand++) |
---|
[1526] | 114 | { |
---|
[1755] | 115 | if (commandStrings[iCommand] != "") |
---|
| 116 | { |
---|
| 117 | SubString tokens(commandStrings[iCommand], " ", SubString::WhiteSpaces, false, |
---|
| 118 | '\\', false, '"', false, '(', ')', false, '\0'); |
---|
[1526] | 119 | |
---|
[1755] | 120 | KeybindMode::Enum mode = KeybindMode::None; |
---|
| 121 | float paramModifier = 1.0f; |
---|
| 122 | std::string commandStr = ""; |
---|
[1526] | 123 | |
---|
[1755] | 124 | for (unsigned int iToken = 0; iToken < tokens.size(); ++iToken) |
---|
| 125 | { |
---|
| 126 | std::string token = getLowercase(tokens[iToken]); |
---|
[1526] | 127 | |
---|
[1755] | 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); |
---|
[1887] | 144 | else |
---|
| 145 | this->bButtonThresholdUser_ = true; |
---|
[1755] | 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 | } |
---|
[1526] | 161 | |
---|
[1755] | 162 | if (commandStr == "") |
---|
| 163 | { |
---|
| 164 | parseError("No command string given.", false); |
---|
| 165 | continue; |
---|
| 166 | } |
---|
[1526] | 167 | |
---|
[1755] | 168 | // evaluate the command |
---|
| 169 | CommandEvaluation eval = CommandExecutor::evaluate(commandStr); |
---|
| 170 | if (!eval.isValid()) |
---|
| 171 | { |
---|
| 172 | parseError("Command evaluation failed.", true); |
---|
| 173 | continue; |
---|
| 174 | } |
---|
[1526] | 175 | |
---|
[1755] | 176 | // check for param command |
---|
[2087] | 177 | int paramIndex = eval.getConsoleCommand()->getInputConfiguredParam_(); |
---|
[1755] | 178 | if (paramIndex >= 0) |
---|
| 179 | { |
---|
| 180 | // parameter supported command |
---|
| 181 | ParamCommand* cmd = new ParamCommand(); |
---|
[2087] | 182 | cmd->scale_ = paramModifier; |
---|
[1526] | 183 | |
---|
[1755] | 184 | // add command to the buffer if not yet existing |
---|
[1887] | 185 | for (unsigned int iParamCmd = 0; iParamCmd < paramCommandBuffer_->size(); iParamCmd++) |
---|
[1755] | 186 | { |
---|
[2087] | 187 | if ((*paramCommandBuffer_)[iParamCmd]->evaluation_.getConsoleCommand() |
---|
| 188 | == eval.getConsoleCommand()) |
---|
[1755] | 189 | { |
---|
| 190 | // already in list |
---|
[1887] | 191 | cmd->paramCommand_ = (*paramCommandBuffer_)[iParamCmd]; |
---|
[1755] | 192 | break; |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | if (cmd->paramCommand_ == 0) |
---|
| 196 | { |
---|
| 197 | cmd->paramCommand_ = new BufferedParamCommand(); |
---|
[1887] | 198 | paramCommandBuffer_->push_back(cmd->paramCommand_); |
---|
[1755] | 199 | cmd->paramCommand_->evaluation_ = eval; |
---|
| 200 | cmd->paramCommand_->paramIndex_ = paramIndex; |
---|
| 201 | } |
---|
[1526] | 202 | |
---|
| 203 | |
---|
[1755] | 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; |
---|
[1526] | 218 | |
---|
[1755] | 219 | if (mode == KeybindMode::None) |
---|
| 220 | mode = eval.getConsoleCommand()->getKeybindMode(); |
---|
| 221 | |
---|
| 222 | commands[mode].push_back(cmd); |
---|
| 223 | } |
---|
[1526] | 224 | } |
---|
| 225 | } |
---|
[1755] | 226 | |
---|
| 227 | for (unsigned int j = 0; j < 3; j++) |
---|
[1526] | 228 | { |
---|
[1755] | 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; |
---|
[1526] | 238 | } |
---|
| 239 | } |
---|
| 240 | |
---|
[1755] | 241 | inline void Button::parseError(std::string message, bool serious) |
---|
| 242 | { |
---|
| 243 | if (serious) |
---|
| 244 | { |
---|
[1783] | 245 | COUT(2) << "Error while parsing binding for button/axis " << this->name_ << ". " |
---|
[1755] | 246 | << message << std::endl; |
---|
| 247 | } |
---|
| 248 | else |
---|
| 249 | { |
---|
[1783] | 250 | COUT(3) << "Warning while parsing binding for button/axis " << this->name_ << ". " |
---|
[1755] | 251 | << message << std::endl; |
---|
| 252 | } |
---|
| 253 | } |
---|
[1526] | 254 | } |
---|