[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 | |
---|
[1887] | 84 | void Button::readConfigValue() |
---|
[1526] | 85 | { |
---|
[1887] | 86 | // create/get ConfigValueContainer |
---|
| 87 | if (!configContainer_) |
---|
[1755] | 88 | { |
---|
[1887] | 89 | configContainer_ = new ConfigValueContainer(CFT_Keybindings, 0, groupName_, name_, "", name_); |
---|
| 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 |
---|
| 177 | int paramIndex = eval.getConsoleCommand()->getAxisParamIndex(); |
---|
| 178 | if (paramIndex >= 0) |
---|
| 179 | { |
---|
| 180 | // parameter supported command |
---|
| 181 | ParamCommand* cmd = new ParamCommand(); |
---|
| 182 | cmd->paramModifier_ = paramModifier; |
---|
| 183 | cmd->bRelative_ = eval.getConsoleCommand()->getIsAxisRelative(); |
---|
[1526] | 184 | |
---|
[1755] | 185 | // add command to the buffer if not yet existing |
---|
[1887] | 186 | for (unsigned int iParamCmd = 0; iParamCmd < paramCommandBuffer_->size(); iParamCmd++) |
---|
[1755] | 187 | { |
---|
[1887] | 188 | if (getLowercase((*paramCommandBuffer_)[iParamCmd]->evaluation_.getOriginalCommand()) |
---|
[1755] | 189 | == getLowercase(commandStr)) |
---|
| 190 | { |
---|
| 191 | // already in list |
---|
[1887] | 192 | cmd->paramCommand_ = (*paramCommandBuffer_)[iParamCmd]; |
---|
[1755] | 193 | break; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | if (cmd->paramCommand_ == 0) |
---|
| 197 | { |
---|
| 198 | cmd->paramCommand_ = new BufferedParamCommand(); |
---|
[1887] | 199 | paramCommandBuffer_->push_back(cmd->paramCommand_); |
---|
[1755] | 200 | cmd->paramCommand_->evaluation_ = eval; |
---|
| 201 | cmd->paramCommand_->paramIndex_ = paramIndex; |
---|
| 202 | } |
---|
[1526] | 203 | |
---|
| 204 | |
---|
[1755] | 205 | // we don't know whether this is an actual axis or just a button |
---|
| 206 | if (mode == KeybindMode::None) |
---|
| 207 | { |
---|
| 208 | if (!addParamCommand(cmd)) |
---|
| 209 | { |
---|
| 210 | mode = eval.getConsoleCommand()->getKeybindMode(); |
---|
| 211 | commands[mode].push_back(cmd); |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | else |
---|
| 216 | { |
---|
| 217 | SimpleCommand* cmd = new SimpleCommand(); |
---|
| 218 | cmd->evaluation_ = eval; |
---|
[1526] | 219 | |
---|
[1755] | 220 | if (mode == KeybindMode::None) |
---|
| 221 | mode = eval.getConsoleCommand()->getKeybindMode(); |
---|
| 222 | |
---|
| 223 | commands[mode].push_back(cmd); |
---|
| 224 | } |
---|
[1526] | 225 | } |
---|
| 226 | } |
---|
[1755] | 227 | |
---|
| 228 | for (unsigned int j = 0; j < 3; j++) |
---|
[1526] | 229 | { |
---|
[1755] | 230 | nCommands_[j] = commands[j].size(); |
---|
| 231 | if (nCommands_[j]) |
---|
| 232 | { |
---|
| 233 | commands_[j] = new BaseCommand*[nCommands_[j]]; |
---|
| 234 | for (unsigned int i = 0; i < commands[j].size(); i++) |
---|
| 235 | commands_[j][i] = commands[j][i]; |
---|
| 236 | } |
---|
| 237 | else |
---|
| 238 | commands_[j] = 0; |
---|
[1526] | 239 | } |
---|
| 240 | } |
---|
| 241 | |
---|
[1755] | 242 | inline void Button::parseError(std::string message, bool serious) |
---|
| 243 | { |
---|
| 244 | if (serious) |
---|
| 245 | { |
---|
[1783] | 246 | COUT(2) << "Error while parsing binding for button/axis " << this->name_ << ". " |
---|
[1755] | 247 | << message << std::endl; |
---|
| 248 | } |
---|
| 249 | else |
---|
| 250 | { |
---|
[1783] | 251 | COUT(3) << "Warning while parsing binding for button/axis " << this->name_ << ". " |
---|
[1755] | 252 | << message << std::endl; |
---|
| 253 | } |
---|
| 254 | } |
---|
[1526] | 255 | } |
---|