| 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 | /** |
|---|
| 30 | @file |
|---|
| 31 | @brief Implementation of the different input handlers. |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | #include "InputHandler.h" |
|---|
| 35 | #include "util/Convert.h" |
|---|
| 36 | #include "util/SubString.h" |
|---|
| 37 | #include "util/String.h" |
|---|
| 38 | #include "Debug.h" |
|---|
| 39 | #include "ConfigValueIncludes.h" |
|---|
| 40 | #include "CoreIncludes.h" |
|---|
| 41 | #include "CommandExecutor.h" |
|---|
| 42 | #include "Executor.h" |
|---|
| 43 | |
|---|
| 44 | namespace orxonox |
|---|
| 45 | { |
|---|
| 46 | // ############################### |
|---|
| 47 | // ###### Button ###### |
|---|
| 48 | // ############################### |
|---|
| 49 | |
|---|
| 50 | bool BufferedParamCommand::execute() |
|---|
| 51 | { |
|---|
| 52 | if (nValuesAdded_) |
|---|
| 53 | { |
|---|
| 54 | BufferedParamCommand& cmd = *this; |
|---|
| 55 | cmd.evaluation_.setEvaluatedParameter(cmd.paramIndex_, cmd.value_); |
|---|
| 56 | // reset |
|---|
| 57 | cmd.nValuesAdded_ = 0; |
|---|
| 58 | cmd.value_ = 0; |
|---|
| 59 | return CommandExecutor::execute(cmd.evaluation_); |
|---|
| 60 | } |
|---|
| 61 | else |
|---|
| 62 | return true; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | bool SimpleCommand::execute(float abs, float rel) |
|---|
| 66 | { |
|---|
| 67 | return CommandExecutor::execute(evaluation_); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | bool ParamCommand::execute(float abs, float rel) |
|---|
| 71 | { |
|---|
| 72 | BufferedParamCommand& paramCommand = *paramCommand_; |
|---|
| 73 | // command has an additional parameter |
|---|
| 74 | if (bRelative_ && (rel > 0 || rel < 0)) |
|---|
| 75 | { |
|---|
| 76 | // we have to calculate a relative movement. |
|---|
| 77 | // paramModifier_ says how much one keystroke is |
|---|
| 78 | paramCommand.value_ += paramModifier_ * rel; |
|---|
| 79 | } |
|---|
| 80 | else if (abs > 0 || abs < 0) |
|---|
| 81 | { |
|---|
| 82 | // we have to calculate absolute position of the axis. |
|---|
| 83 | // Since there might be another axis that is affected, we have to wait and |
|---|
| 84 | // store the result in a temporary place |
|---|
| 85 | paramCommand.value_ = (paramCommand.value_ * paramCommand.nValuesAdded_ + paramModifier_ * abs) |
|---|
| 86 | /++paramCommand.nValuesAdded_; |
|---|
| 87 | } |
|---|
| 88 | return true; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | void Button::clear() |
|---|
| 92 | { |
|---|
| 93 | for (unsigned int j = 0; j < 3; j++) |
|---|
| 94 | { |
|---|
| 95 | if (nCommands_[j]) |
|---|
| 96 | { |
|---|
| 97 | // delete all commands and the command pointer array |
|---|
| 98 | for (unsigned int i = 0; i < nCommands_[j]; i++) |
|---|
| 99 | delete commands_[j][i]; |
|---|
| 100 | delete[] commands_[j]; |
|---|
| 101 | commands_[j] = 0; |
|---|
| 102 | nCommands_[j] = 0; |
|---|
| 103 | } |
|---|
| 104 | else |
|---|
| 105 | { |
|---|
| 106 | commands_[j] = 0; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | void Button::parse(std::vector<BufferedParamCommand*>& paramCommandBuffer) |
|---|
| 112 | { |
|---|
| 113 | if (isEmpty(bindingString_)) |
|---|
| 114 | { |
|---|
| 115 | clear(); |
|---|
| 116 | return; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | // use std::vector for a temporary dynamic array |
|---|
| 120 | std::vector<BaseCommand*> commands[3]; |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | // separate the commands |
|---|
| 124 | SubString commandStrings(bindingString_, "|", SubString::WhiteSpaces, false, |
|---|
| 125 | '\\', false, '"', false, '(', ')', false, '\0'); |
|---|
| 126 | |
|---|
| 127 | for (unsigned int iCommand = 0; iCommand < commandStrings.size(); iCommand++) |
|---|
| 128 | { |
|---|
| 129 | if (commandStrings[iCommand] != "") |
|---|
| 130 | { |
|---|
| 131 | SubString tokens(commandStrings[iCommand], " ", SubString::WhiteSpaces, false, |
|---|
| 132 | '\\', false, '"', false, '(', ')', false, '\0'); |
|---|
| 133 | |
|---|
| 134 | unsigned int iToken = 0; |
|---|
| 135 | |
|---|
| 136 | // for real axes, we can feed a ButtonThreshold argument as entire command |
|---|
| 137 | if (getLowercase(tokens[0]) == "buttonthreshold") |
|---|
| 138 | { |
|---|
| 139 | if (tokens.size() == 1) |
|---|
| 140 | continue; |
|---|
| 141 | // may fail, but doesn't matter |
|---|
| 142 | convertValue(&buttonThreshold_, tokens[1]); |
|---|
| 143 | continue; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | // first argument can be OnPress, OnHold OnRelease or nothing |
|---|
| 147 | KeybindMode::Enum mode = KeybindMode::None; |
|---|
| 148 | if (getLowercase(tokens[iToken]) == "onpress") |
|---|
| 149 | mode = KeybindMode::OnPress, iToken++; |
|---|
| 150 | if (getLowercase(tokens[iToken]) == "onrelease") |
|---|
| 151 | mode = KeybindMode::OnRelease, iToken++; |
|---|
| 152 | if (getLowercase(tokens[iToken]) == "onhold") |
|---|
| 153 | mode = KeybindMode::OnHold, iToken++; |
|---|
| 154 | |
|---|
| 155 | if (iToken == tokens.size()) |
|---|
| 156 | continue; |
|---|
| 157 | |
|---|
| 158 | // second argument can be the amplitude for the case it as an axis command |
|---|
| 159 | // default amplitude is 1.0f |
|---|
| 160 | float paramModifier = 1.0f; |
|---|
| 161 | if (getLowercase(tokens[iToken]) == "axisamp") |
|---|
| 162 | { |
|---|
| 163 | iToken++; |
|---|
| 164 | if (iToken == tokens.size() || !convertValue(¶mModifier, tokens[iToken])) |
|---|
| 165 | { |
|---|
| 166 | COUT(2) << "Error while parsing key binding " << name_ |
|---|
| 167 | << ". Numeric expression expected afer 'AxisAmp', switching to default value" << std::endl; |
|---|
| 168 | if (iToken == tokens.size()) |
|---|
| 169 | continue; |
|---|
| 170 | } |
|---|
| 171 | iToken++; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | // no more arguments expected except for the actual command |
|---|
| 175 | if (iToken == tokens.size()) |
|---|
| 176 | continue; |
|---|
| 177 | |
|---|
| 178 | std::string commandStr; |
|---|
| 179 | while (iToken != tokens.size()) |
|---|
| 180 | commandStr += tokens[iToken++] + " "; |
|---|
| 181 | |
|---|
| 182 | // evaluate the command |
|---|
| 183 | CommandEvaluation eval = CommandExecutor::evaluate(commandStr); |
|---|
| 184 | if (!eval.isValid()) |
|---|
| 185 | continue; |
|---|
| 186 | |
|---|
| 187 | // check for param command |
|---|
| 188 | int paramIndex = eval.getEvaluatedExecutor()->getAxisParamIndex(); |
|---|
| 189 | // TODO: check in Executor for correct paramIndex |
|---|
| 190 | if (paramIndex >= 0) |
|---|
| 191 | { |
|---|
| 192 | // parameter supported command |
|---|
| 193 | ParamCommand* cmd = new ParamCommand(); |
|---|
| 194 | cmd->paramModifier_ = paramModifier; |
|---|
| 195 | cmd->bRelative_ = eval.getEvaluatedExecutor()->getIsAxisRelative(); |
|---|
| 196 | |
|---|
| 197 | // add command to the buffer if not yet existing |
|---|
| 198 | for (unsigned int iParamCmd = 0; iParamCmd < paramCommandBuffer.size(); iParamCmd++) |
|---|
| 199 | { |
|---|
| 200 | if (getLowercase(paramCommandBuffer[iParamCmd]->evaluation_.getCommandString()) |
|---|
| 201 | == getLowercase(commandStr)) |
|---|
| 202 | { |
|---|
| 203 | // already in list |
|---|
| 204 | cmd->paramCommand_ = paramCommandBuffer[iParamCmd]; |
|---|
| 205 | break; |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | if (cmd->paramCommand_ == 0) |
|---|
| 209 | { |
|---|
| 210 | cmd->paramCommand_ = new BufferedParamCommand(); |
|---|
| 211 | paramCommandBuffer.push_back(cmd->paramCommand_); |
|---|
| 212 | cmd->paramCommand_->evaluation_ = eval; |
|---|
| 213 | cmd->paramCommand_->paramIndex_ = paramIndex; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | // we don't know whether this is an actual axis or just a button |
|---|
| 218 | if (mode == KeybindMode::None) |
|---|
| 219 | { |
|---|
| 220 | if (!addParamCommand(cmd)) |
|---|
| 221 | { |
|---|
| 222 | mode = eval.getEvaluatedExecutor()->getKeybindMode(); |
|---|
| 223 | commands[mode].push_back(cmd); |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | else |
|---|
| 228 | { |
|---|
| 229 | SimpleCommand* cmd = new SimpleCommand(); |
|---|
| 230 | cmd->evaluation_ = eval; |
|---|
| 231 | |
|---|
| 232 | //TODO: check CommandEvaluation for correct KeybindMode |
|---|
| 233 | if (mode == KeybindMode::None) |
|---|
| 234 | mode = eval.getEvaluatedExecutor()->getKeybindMode(); |
|---|
| 235 | |
|---|
| 236 | commands[mode].push_back(cmd); |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | for (unsigned int j = 0; j < 3; j++) |
|---|
| 242 | { |
|---|
| 243 | nCommands_[j] = commands[j].size(); |
|---|
| 244 | if (nCommands_[j]) |
|---|
| 245 | { |
|---|
| 246 | commands_[j] = new BaseCommand*[nCommands_[j]]; |
|---|
| 247 | for (unsigned int i = 0; i < commands[j].size(); i++) |
|---|
| 248 | commands_[j][i] = commands[j][i]; |
|---|
| 249 | } |
|---|
| 250 | else |
|---|
| 251 | commands_[j] = 0; |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | bool Button::execute(KeybindMode::Enum mode, float abs, float rel) |
|---|
| 256 | { |
|---|
| 257 | // execute all the parsed commands in the string |
|---|
| 258 | for (unsigned int iCommand = 0; iCommand < nCommands_[mode]; iCommand++) |
|---|
| 259 | commands_[mode][iCommand]->execute(abs, rel); |
|---|
| 260 | return true; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | void HalfAxis::clear() |
|---|
| 264 | { |
|---|
| 265 | Button::clear(); |
|---|
| 266 | if (nParamCommands_) |
|---|
| 267 | { |
|---|
| 268 | // delete all commands and the command pointer array |
|---|
| 269 | for (unsigned int i = 0; i < nParamCommands_; i++) |
|---|
| 270 | delete paramCommands_[i]; |
|---|
| 271 | delete[] paramCommands_; |
|---|
| 272 | } |
|---|
| 273 | else |
|---|
| 274 | { |
|---|
| 275 | nParamCommands_ = 0; nParamCommands_ = 0; |
|---|
| 276 | } |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | bool HalfAxis::addParamCommand(ParamCommand* command) |
|---|
| 280 | { |
|---|
| 281 | ParamCommand** cmds = paramCommands_; |
|---|
| 282 | paramCommands_ = new ParamCommand*[++nParamCommands_]; |
|---|
| 283 | unsigned int i; |
|---|
| 284 | for (i = 0; i < nParamCommands_ - 1; i++) |
|---|
| 285 | paramCommands_[i] = cmds[i]; |
|---|
| 286 | paramCommands_[i] = command; |
|---|
| 287 | delete[] cmds; |
|---|
| 288 | return true; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | bool HalfAxis::execute() |
|---|
| 292 | { |
|---|
| 293 | bool success = true; |
|---|
| 294 | for (unsigned int i = 0; i < nParamCommands_; i++) |
|---|
| 295 | success = success && paramCommands_[i]->execute(absVal_, relVal_); |
|---|
| 296 | return success; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | // ############################### |
|---|
| 301 | // ###### KeyBinder ###### |
|---|
| 302 | // ############################### |
|---|
| 303 | |
|---|
| 304 | /** |
|---|
| 305 | @brief Constructor that does as little as necessary. |
|---|
| 306 | */ |
|---|
| 307 | KeyBinder::KeyBinder() : deriveTime_(0.0f) |
|---|
| 308 | { |
|---|
| 309 | RegisterObject(KeyBinder); |
|---|
| 310 | |
|---|
| 311 | // keys |
|---|
| 312 | std::string keyNames[] = { |
|---|
| 313 | "UNASSIGNED", |
|---|
| 314 | "ESCAPE", |
|---|
| 315 | "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", |
|---|
| 316 | "MINUS", "EQUALS", "BACK", "TAB", |
|---|
| 317 | "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", |
|---|
| 318 | "LBRACKET", "RBRACKET", |
|---|
| 319 | "RETURN", "LCONTROL", |
|---|
| 320 | "A", "S", "D", "F", "G", "H", "J", "K", "L", |
|---|
| 321 | "SEMICOLON", "APOSTROPHE", "GRAVE", |
|---|
| 322 | "LSHIFT", "BACKSLASH", |
|---|
| 323 | "Z", "X", "C", "V", "B", "N", "M", |
|---|
| 324 | "COMMA", "PERIOD", "SLASH", |
|---|
| 325 | "RSHIFT", |
|---|
| 326 | "MULTIPLY", |
|---|
| 327 | "LMENU", |
|---|
| 328 | "SPACE", |
|---|
| 329 | "CAPITAL", |
|---|
| 330 | "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", |
|---|
| 331 | "NUMLOCK", "SCROLL", |
|---|
| 332 | "NUMPAD7", "NUMPAD8", "NUMPAD9", |
|---|
| 333 | "SUBTRACT", |
|---|
| 334 | "NUMPAD4", "NUMPAD5", "NUMPAD6", |
|---|
| 335 | "ADD", |
|---|
| 336 | "NUMPAD1", "NUMPAD2", "NUMPAD3", "NUMPAD0", |
|---|
| 337 | "DECIMAL", |
|---|
| 338 | "","", |
|---|
| 339 | "OEM_102", |
|---|
| 340 | "F11", "F12", |
|---|
| 341 | "","","","","","","","","","","", |
|---|
| 342 | "F13", "F14", "F15", |
|---|
| 343 | "","","","","","","","","","", |
|---|
| 344 | "KANA", |
|---|
| 345 | "","", |
|---|
| 346 | "ABNT_C1", |
|---|
| 347 | "","","","","", |
|---|
| 348 | "CONVERT", |
|---|
| 349 | "", |
|---|
| 350 | "NOCONVERT", |
|---|
| 351 | "", |
|---|
| 352 | "YEN", |
|---|
| 353 | "ABNT_C2", |
|---|
| 354 | "","","","","","","","","","","","","","", |
|---|
| 355 | "NUMPADEQUALS", |
|---|
| 356 | "","", |
|---|
| 357 | "PREVTRACK", |
|---|
| 358 | "AT", |
|---|
| 359 | "COLON", "UNDERLINE", |
|---|
| 360 | "KANJI", |
|---|
| 361 | "STOP", |
|---|
| 362 | "AX", |
|---|
| 363 | "UNLABELED", |
|---|
| 364 | "NEXTTRACK", |
|---|
| 365 | "","", |
|---|
| 366 | "NUMPADENTER", |
|---|
| 367 | "RCONTROL", |
|---|
| 368 | "","", |
|---|
| 369 | "MUTE", |
|---|
| 370 | "CALCULATOR", |
|---|
| 371 | "PLAYPAUSE", |
|---|
| 372 | "", |
|---|
| 373 | "MEDIASTOP", |
|---|
| 374 | "","","","","","","","","", |
|---|
| 375 | "VOLUMEDOWN", |
|---|
| 376 | "", |
|---|
| 377 | "VOLUMEUP", |
|---|
| 378 | "", |
|---|
| 379 | "WEBHOME", |
|---|
| 380 | "NUMPADCOMMA", |
|---|
| 381 | "", |
|---|
| 382 | "DIVIDE", |
|---|
| 383 | "", |
|---|
| 384 | "SYSRQ", |
|---|
| 385 | "RMENU", |
|---|
| 386 | "","","","","","","","","","","","", |
|---|
| 387 | "PAUSE", |
|---|
| 388 | "", |
|---|
| 389 | "HOME", |
|---|
| 390 | "UP", |
|---|
| 391 | "PGUP", |
|---|
| 392 | "", |
|---|
| 393 | "LEFT", |
|---|
| 394 | "", |
|---|
| 395 | "RIGHT", |
|---|
| 396 | "", |
|---|
| 397 | "END", "DOWN", "PGDOWN", "INSERT", "DELETE", |
|---|
| 398 | "","","","","","","", |
|---|
| 399 | "LWIN", "RWIN", "APPS", |
|---|
| 400 | "POWER", "SLEEP", |
|---|
| 401 | "","","", |
|---|
| 402 | "WAKE", |
|---|
| 403 | "", |
|---|
| 404 | "WEBSEARCH", "WEBFAVORITES", "WEBREFRESH", "WEBSTOP", "WEBFORWARD", "WEBBACK", |
|---|
| 405 | "MYCOMPUTER", "MAIL", "MEDIASELECT" |
|---|
| 406 | }; |
|---|
| 407 | for (unsigned int i = 0; i < nKeys_s; i++) |
|---|
| 408 | keys_[i].name_ = "Key" + keyNames[i]; |
|---|
| 409 | |
|---|
| 410 | // mouse buttons |
|---|
| 411 | std::string mouseButtonNames[] = { |
|---|
| 412 | "MouseLeft", "MouseRight", "MouseMiddle", |
|---|
| 413 | "MouseButton3", "MouseButton4", "MouseButton5", |
|---|
| 414 | "MouseButton6", "MouseButton7", |
|---|
| 415 | "MouseWheel1Up", "MouseWheel1Down", |
|---|
| 416 | "MouseWheel2Up", "MouseWheel2Down" }; |
|---|
| 417 | for (unsigned int i = 0; i < nMouseButtons_s; i++) |
|---|
| 418 | mouseButtons_[i].name_ = mouseButtonNames[i]; |
|---|
| 419 | |
|---|
| 420 | // joy stick buttons |
|---|
| 421 | for (unsigned int i = 0; i < 32; i++) |
|---|
| 422 | joyStickButtons_[i].name_ = "JoyButton" + getConvertedValue<int, std::string>(i); |
|---|
| 423 | for (unsigned int i = 32; i < nJoyStickButtons_s; i += 4) |
|---|
| 424 | { |
|---|
| 425 | joyStickButtons_[i + 0].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "North"; |
|---|
| 426 | joyStickButtons_[i + 1].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "South"; |
|---|
| 427 | joyStickButtons_[i + 2].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "East"; |
|---|
| 428 | joyStickButtons_[i + 3].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "West"; |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | // half axes |
|---|
| 432 | std::string rawNames[nHalfAxes_s/2]; |
|---|
| 433 | rawNames[0] = "MouseX"; |
|---|
| 434 | rawNames[1] = "MouseY"; |
|---|
| 435 | rawNames[2] = "Empty1"; |
|---|
| 436 | rawNames[3] = "Empty2"; |
|---|
| 437 | for (unsigned int i = 4; i < nHalfAxes_s/2; i++) |
|---|
| 438 | rawNames[i] = "JoyAxis" + getConvertedValue<int, std::string>(i - 3); |
|---|
| 439 | for (unsigned int i = 0; i < nHalfAxes_s/2; i++) |
|---|
| 440 | { |
|---|
| 441 | halfAxes_[i * 2 + 0].name_ = rawNames[i] + "Pos"; |
|---|
| 442 | halfAxes_[i * 2 + 1].name_ = rawNames[i] + "Neg"; |
|---|
| 443 | } |
|---|
| 444 | |
|---|
| 445 | for (unsigned int i = 0; i < this->nHalfAxes_s; i++) |
|---|
| 446 | halfAxes_[i].buttonThreshold_ = buttonThreshold_; |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | /** |
|---|
| 450 | @brief Destructor |
|---|
| 451 | */ |
|---|
| 452 | KeyBinder::~KeyBinder() |
|---|
| 453 | { |
|---|
| 454 | // almost no destructors required because most of the arrays are static. |
|---|
| 455 | clearBindings(); // does some destruction work |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | /** |
|---|
| 459 | @brief Loads the key and button bindings. |
|---|
| 460 | @return True if loading succeeded. |
|---|
| 461 | */ |
|---|
| 462 | void KeyBinder::loadBindings() |
|---|
| 463 | { |
|---|
| 464 | COUT(3) << "KeyBinder: Loading key bindings..." << std::endl; |
|---|
| 465 | |
|---|
| 466 | ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini"); |
|---|
| 467 | clearBindings(); |
|---|
| 468 | setConfigValues(); |
|---|
| 469 | |
|---|
| 470 | COUT(3) << "KeyBinder: Loading key bindings done." << std::endl; |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | /** |
|---|
| 474 | @brief Loader for the key bindings, managed by config values. |
|---|
| 475 | */ |
|---|
| 476 | void KeyBinder::setConfigValues() |
|---|
| 477 | { |
|---|
| 478 | SetConfigValue(analogThreshold_, 0.01f) .description("Threshold for analog axes until which the state is 0."); |
|---|
| 479 | SetConfigValue(bDeriveMouseInput_, false).description("Whether or not to derive moues movement for the absolute value."); |
|---|
| 480 | SetConfigValue(derivePeriod_, 0.1f) .description("Accuracy of the mouse input deriver. The higher the more precise, but laggier."); |
|---|
| 481 | SetConfigValue(mouseSensitivity_, 1.0f) .description("Mouse sensitivity."); |
|---|
| 482 | |
|---|
| 483 | float oldThresh = buttonThreshold_; |
|---|
| 484 | SetConfigValue(buttonThreshold_, 0.80f).description("Threshold for analog axes until which the button is not pressed."); |
|---|
| 485 | if (oldThresh != buttonThreshold_) |
|---|
| 486 | for (unsigned int i = 0; i < nHalfAxes_s; i++) |
|---|
| 487 | if (halfAxes_[i].buttonThreshold_ == oldThresh) |
|---|
| 488 | halfAxes_[i].buttonThreshold_ = buttonThreshold_; |
|---|
| 489 | |
|---|
| 490 | // keys |
|---|
| 491 | for (unsigned int i = 0; i < nKeys_s; i++) |
|---|
| 492 | readTrigger(keys_[i]); |
|---|
| 493 | // mouse buttons |
|---|
| 494 | for (unsigned int i = 0; i < nMouseButtons_s; i++) |
|---|
| 495 | readTrigger(mouseButtons_[i]); |
|---|
| 496 | // joy stick buttons |
|---|
| 497 | for (unsigned int i = 0; i < nJoyStickButtons_s; i++) |
|---|
| 498 | readTrigger(joyStickButtons_[i]); |
|---|
| 499 | // half axes |
|---|
| 500 | for (unsigned int i = 0; i < nHalfAxes_s; i++) |
|---|
| 501 | readTrigger(halfAxes_[i]); |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | void KeyBinder::readTrigger(Button& button) |
|---|
| 505 | { |
|---|
| 506 | // config value stuff |
|---|
| 507 | ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer(button.name_); |
|---|
| 508 | if (!cont) |
|---|
| 509 | { |
|---|
| 510 | cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), button.name_, ""); |
|---|
| 511 | getIdentifier()->addConfigValueContainer(button.name_, cont); |
|---|
| 512 | } |
|---|
| 513 | std::string old = button.bindingString_; |
|---|
| 514 | cont->getValue(&button.bindingString_); |
|---|
| 515 | |
|---|
| 516 | // keybinder stuff |
|---|
| 517 | if (old != button.bindingString_) |
|---|
| 518 | { |
|---|
| 519 | // binding has changed |
|---|
| 520 | button.parse(paramCommandBuffer_); |
|---|
| 521 | } |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | /** |
|---|
| 525 | @brief Overwrites all bindings with "" |
|---|
| 526 | */ |
|---|
| 527 | void KeyBinder::clearBindings(bool bInit) |
|---|
| 528 | { |
|---|
| 529 | for (unsigned int i = 0; i < nKeys_s; i++) |
|---|
| 530 | keys_[i].clear(); |
|---|
| 531 | |
|---|
| 532 | for (unsigned int i = 0; i < nMouseButtons_s; i++) |
|---|
| 533 | mouseButtons_[i].clear(); |
|---|
| 534 | |
|---|
| 535 | for (unsigned int i = 0; i < nJoyStickButtons_s; i++) |
|---|
| 536 | joyStickButtons_[i].clear(); |
|---|
| 537 | |
|---|
| 538 | for (unsigned int i = 0; i < nHalfAxes_s; i++) |
|---|
| 539 | halfAxes_[i].clear(); |
|---|
| 540 | |
|---|
| 541 | for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++) |
|---|
| 542 | delete paramCommandBuffer_[i]; |
|---|
| 543 | paramCommandBuffer_.clear(); |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | void KeyBinder::tick(float dt) |
|---|
| 547 | { |
|---|
| 548 | // we have to process all the analog input since there is e.g. no 'mouseDoesntMove' event. |
|---|
| 549 | for (unsigned int i = 0; i < nHalfAxes_s; i++) |
|---|
| 550 | { |
|---|
| 551 | if (halfAxes_[i].hasChanged_) |
|---|
| 552 | { |
|---|
| 553 | if (!halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ > halfAxes_[i].buttonThreshold_) |
|---|
| 554 | { |
|---|
| 555 | halfAxes_[i].wasDown_ = true; |
|---|
| 556 | if (halfAxes_[i].nCommands_[KeybindMode::OnPress]) |
|---|
| 557 | halfAxes_[i].execute(KeybindMode::OnPress); |
|---|
| 558 | } |
|---|
| 559 | else if (halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ < halfAxes_[i].buttonThreshold_) |
|---|
| 560 | { |
|---|
| 561 | halfAxes_[i].wasDown_ = false; |
|---|
| 562 | if (halfAxes_[i].nCommands_[KeybindMode::OnRelease]) |
|---|
| 563 | halfAxes_[i].execute(KeybindMode::OnRelease); |
|---|
| 564 | } |
|---|
| 565 | if (halfAxes_[i].wasDown_) |
|---|
| 566 | { |
|---|
| 567 | if (halfAxes_[i].nCommands_[KeybindMode::OnHold]) |
|---|
| 568 | halfAxes_[i].execute(KeybindMode::OnHold); |
|---|
| 569 | } |
|---|
| 570 | halfAxes_[i].hasChanged_ = false; |
|---|
| 571 | } |
|---|
| 572 | |
|---|
| 573 | // these are the actually useful axis bindings for analog input AND output |
|---|
| 574 | if (halfAxes_[i].relVal_ > analogThreshold_ || halfAxes_[i].absVal_ > analogThreshold_) |
|---|
| 575 | { |
|---|
| 576 | halfAxes_[i].execute(); |
|---|
| 577 | } |
|---|
| 578 | } |
|---|
| 579 | |
|---|
| 580 | if (bDeriveMouseInput_) |
|---|
| 581 | { |
|---|
| 582 | if (deriveTime_ > derivePeriod_) |
|---|
| 583 | { |
|---|
| 584 | deriveTime_ = 0.0f; |
|---|
| 585 | //CCOUT(3) << "mouse abs: "; |
|---|
| 586 | for (int i = 0; i < 2; i++) |
|---|
| 587 | { |
|---|
| 588 | if (mouseRelative_[i] > 0) |
|---|
| 589 | { |
|---|
| 590 | halfAxes_[2*i + 0].absVal_ = mouseRelative_[i] * derivePeriod_ / 500 * mouseSensitivity_; |
|---|
| 591 | halfAxes_[2*i + 1].absVal_ = 0.0f; |
|---|
| 592 | } |
|---|
| 593 | else if (mouseRelative_[0] < 0) |
|---|
| 594 | { |
|---|
| 595 | halfAxes_[2*i + 0].absVal_ = 0.0f; |
|---|
| 596 | halfAxes_[2*i + 1].absVal_ = -mouseRelative_[i] * derivePeriod_ / 500 * mouseSensitivity_; |
|---|
| 597 | } |
|---|
| 598 | //COUT(3) << mouseRelative_[i] << " | "; |
|---|
| 599 | mouseRelative_[i] = 0; |
|---|
| 600 | } |
|---|
| 601 | //COUT(3) << std::endl; |
|---|
| 602 | } |
|---|
| 603 | else |
|---|
| 604 | deriveTime_ += dt; |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | // execute all buffered bindings (addional parameter) |
|---|
| 608 | for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++) |
|---|
| 609 | paramCommandBuffer_[i]->execute(); |
|---|
| 610 | |
|---|
| 611 | // always reset the relative movement of the mouse |
|---|
| 612 | for (unsigned int i = 0; i < 8; i++) |
|---|
| 613 | halfAxes_[i].relVal_ = 0.0f; |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | void KeyBinder::keyPressed (const KeyEvent& evt) |
|---|
| 617 | { keys_[evt.key].execute(KeybindMode::OnPress); } |
|---|
| 618 | |
|---|
| 619 | void KeyBinder::keyReleased(const KeyEvent& evt) |
|---|
| 620 | { keys_[evt.key].execute(KeybindMode::OnRelease); } |
|---|
| 621 | |
|---|
| 622 | void KeyBinder::keyHeld (const KeyEvent& evt) |
|---|
| 623 | { keys_[evt.key].execute(KeybindMode::OnHold); } |
|---|
| 624 | |
|---|
| 625 | |
|---|
| 626 | void KeyBinder::mouseButtonPressed (MouseButton::Enum id) |
|---|
| 627 | { mouseButtons_[id].execute(KeybindMode::OnPress); } |
|---|
| 628 | |
|---|
| 629 | void KeyBinder::mouseButtonReleased(MouseButton::Enum id) |
|---|
| 630 | { mouseButtons_[id].execute(KeybindMode::OnRelease); } |
|---|
| 631 | |
|---|
| 632 | void KeyBinder::mouseButtonHeld (MouseButton::Enum id) |
|---|
| 633 | { mouseButtons_[id].execute(KeybindMode::OnHold); } |
|---|
| 634 | |
|---|
| 635 | |
|---|
| 636 | void KeyBinder::joyStickButtonPressed (int joyStickID, int button) |
|---|
| 637 | { joyStickButtons_[button].execute(KeybindMode::OnPress); } |
|---|
| 638 | |
|---|
| 639 | void KeyBinder::joyStickButtonReleased(int joyStickID, int button) |
|---|
| 640 | { joyStickButtons_[button].execute(KeybindMode::OnRelease); } |
|---|
| 641 | |
|---|
| 642 | void KeyBinder::joyStickButtonHeld (int joyStickID, int button) |
|---|
| 643 | { joyStickButtons_[button].execute(KeybindMode::OnHold); } |
|---|
| 644 | |
|---|
| 645 | /** |
|---|
| 646 | @brief Event handler for the mouseMoved Event. |
|---|
| 647 | @param e Mouse state information |
|---|
| 648 | */ |
|---|
| 649 | void KeyBinder::mouseMoved(IntVector2 abs_, IntVector2 rel_, IntVector2 clippingSize) |
|---|
| 650 | { |
|---|
| 651 | if (!bDeriveMouseInput_) |
|---|
| 652 | { |
|---|
| 653 | // y axis of mouse input is inverted |
|---|
| 654 | int rel[] = { rel_.x, -rel_.y }; |
|---|
| 655 | |
|---|
| 656 | //COUT(3) << rel[0] << " | " << rel[1] << std::endl; |
|---|
| 657 | |
|---|
| 658 | for (int i = 0; i < 2; i++) |
|---|
| 659 | { |
|---|
| 660 | if (rel[i]) |
|---|
| 661 | { |
|---|
| 662 | // absolute |
|---|
| 663 | if (mousePosition_[i] >= 0) |
|---|
| 664 | { |
|---|
| 665 | mousePosition_[i] += rel[i]; |
|---|
| 666 | halfAxes_[0 + 2*i].hasChanged_ = true; |
|---|
| 667 | if (mousePosition_[i] < 0) |
|---|
| 668 | { |
|---|
| 669 | halfAxes_[1 + 2*i].hasChanged_ = true; |
|---|
| 670 | halfAxes_[1 + 2*i].absVal_ = -((float)mousePosition_[i])/1024 * mouseSensitivity_; |
|---|
| 671 | halfAxes_[0 + 2*i].absVal_ = 0.0f; |
|---|
| 672 | } |
|---|
| 673 | else |
|---|
| 674 | halfAxes_[1 + 2*i].absVal_ = ((float)mousePosition_[i])/1024 * mouseSensitivity_; |
|---|
| 675 | } |
|---|
| 676 | else |
|---|
| 677 | { |
|---|
| 678 | mousePosition_[i] += rel[i]; |
|---|
| 679 | halfAxes_[1 + 2*i].hasChanged_ = true; |
|---|
| 680 | if (mousePosition_[i] > 0) |
|---|
| 681 | { |
|---|
| 682 | halfAxes_[0 + 2*i].hasChanged_ = true; |
|---|
| 683 | halfAxes_[0 + 2*i].absVal_ = ((float)mousePosition_[i])/1024 * mouseSensitivity_; |
|---|
| 684 | halfAxes_[1 + 2*i].absVal_ = 0.0f; |
|---|
| 685 | } |
|---|
| 686 | else |
|---|
| 687 | halfAxes_[1 + 2*i].absVal_ = -((float)mousePosition_[i])/1024 * mouseSensitivity_; |
|---|
| 688 | } |
|---|
| 689 | |
|---|
| 690 | // relative |
|---|
| 691 | if (rel[i] > 0) |
|---|
| 692 | halfAxes_[0 + 2*i].relVal_ = ((float)rel[i])/1024 * mouseSensitivity_; |
|---|
| 693 | else |
|---|
| 694 | halfAxes_[1 + 2*i].relVal_ = -((float)rel[i])/1024 * mouseSensitivity_; |
|---|
| 695 | } |
|---|
| 696 | } |
|---|
| 697 | } |
|---|
| 698 | else |
|---|
| 699 | { |
|---|
| 700 | mouseRelative_[0] += rel_.x; |
|---|
| 701 | mouseRelative_[1] -= rel_.y; |
|---|
| 702 | } |
|---|
| 703 | } |
|---|
| 704 | |
|---|
| 705 | /** |
|---|
| 706 | @brief Event handler for the mouseScrolled Event. |
|---|
| 707 | @param e Mouse state information |
|---|
| 708 | */ |
|---|
| 709 | void KeyBinder::mouseScrolled(int abs, int rel) |
|---|
| 710 | { |
|---|
| 711 | //COUT(3) << mouseButtons_[8].name_ << " " << abs << " | " << rel << std::endl; |
|---|
| 712 | |
|---|
| 713 | if (rel > 0) |
|---|
| 714 | for (int i = 0; i < rel/120; i++) |
|---|
| 715 | mouseButtons_[8].execute(KeybindMode::OnPress, ((float)abs)/120.0f); |
|---|
| 716 | else |
|---|
| 717 | for (int i = 0; i < -rel/120; i++) |
|---|
| 718 | mouseButtons_[9].execute(KeybindMode::OnPress, ((float)abs)/120.0f); |
|---|
| 719 | } |
|---|
| 720 | |
|---|
| 721 | void KeyBinder::joyStickAxisMoved(int joyStickID, int axis, int value) |
|---|
| 722 | { |
|---|
| 723 | // TODO: check whether 16 bit integer as general axis value is a good idea (works under windows) |
|---|
| 724 | CCOUT(3) << halfAxes_[8 + axis].name_ << std::endl; |
|---|
| 725 | if (value >= 0) |
|---|
| 726 | { |
|---|
| 727 | halfAxes_[8 + axis].absVal_ = ((float)value)/0x8000; |
|---|
| 728 | halfAxes_[8 + axis].relVal_ = ((float)value)/0x8000; |
|---|
| 729 | halfAxes_[8 + axis].hasChanged_ = true; |
|---|
| 730 | } |
|---|
| 731 | else |
|---|
| 732 | { |
|---|
| 733 | halfAxes_[8 + axis + 1].absVal_ = -((float)value)/0x8000; |
|---|
| 734 | halfAxes_[8 + axis + 1].relVal_ = -((float)value)/0x8000; |
|---|
| 735 | halfAxes_[8 + axis + 1].hasChanged_ = true; |
|---|
| 736 | } |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | |
|---|
| 740 | // ############################### |
|---|
| 741 | // ### GUIInputHandler ### |
|---|
| 742 | // ############################### |
|---|
| 743 | |
|---|
| 744 | ///** |
|---|
| 745 | // @brief standard constructor |
|---|
| 746 | //*/ |
|---|
| 747 | //GUIInputHandler::GUIInputHandler() |
|---|
| 748 | //{ |
|---|
| 749 | //} |
|---|
| 750 | |
|---|
| 751 | ///** |
|---|
| 752 | // @brief Destructor |
|---|
| 753 | //*/ |
|---|
| 754 | //GUIInputHandler::~GUIInputHandler() |
|---|
| 755 | //{ |
|---|
| 756 | //} |
|---|
| 757 | |
|---|
| 758 | ///** |
|---|
| 759 | // @brief Event handler for the keyPressed Event. |
|---|
| 760 | // @param e Event information |
|---|
| 761 | //*/ |
|---|
| 762 | //bool GUIInputHandler::keyPressed(const OIS::KeyEvent &e) |
|---|
| 763 | //{ |
|---|
| 764 | ////CEGUI::System::getSingleton().injectKeyDown( arg.key ); |
|---|
| 765 | ////CEGUI::System::getSingleton().injectChar( arg.text ); |
|---|
| 766 | // return true; |
|---|
| 767 | //} |
|---|
| 768 | |
|---|
| 769 | ///** |
|---|
| 770 | // @brief Event handler for the keyReleased Event. |
|---|
| 771 | // @param e Event information |
|---|
| 772 | //*/ |
|---|
| 773 | //bool GUIInputHandler::keyReleased(const OIS::KeyEvent &e) |
|---|
| 774 | //{ |
|---|
| 775 | ////CEGUI::System::getSingleton().injectKeyUp( arg.key ); |
|---|
| 776 | // return true; |
|---|
| 777 | //} |
|---|
| 778 | |
|---|
| 779 | ///** |
|---|
| 780 | // @brief Event handler for the mouseMoved Event. |
|---|
| 781 | // @param e Event information |
|---|
| 782 | //*/ |
|---|
| 783 | //bool GUIInputHandler::mouseMoved(const OIS::MouseEvent &e) |
|---|
| 784 | //{ |
|---|
| 785 | ////CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel ); |
|---|
| 786 | // return true; |
|---|
| 787 | //} |
|---|
| 788 | |
|---|
| 789 | ///** |
|---|
| 790 | // @brief Event handler for the mousePressed Event. |
|---|
| 791 | // @param e Event information |
|---|
| 792 | // @param id The ID of the mouse button |
|---|
| 793 | //*/ |
|---|
| 794 | //bool GUIInputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButton id) |
|---|
| 795 | //{ |
|---|
| 796 | ////CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id)); |
|---|
| 797 | // return true; |
|---|
| 798 | //} |
|---|
| 799 | |
|---|
| 800 | ///** |
|---|
| 801 | // @brief Event handler for the mouseReleased Event. |
|---|
| 802 | // @param e Event information |
|---|
| 803 | // @param id The ID of the mouse button |
|---|
| 804 | //*/ |
|---|
| 805 | //bool GUIInputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButton id) |
|---|
| 806 | //{ |
|---|
| 807 | ////CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id)); |
|---|
| 808 | // return true; |
|---|
| 809 | //} |
|---|
| 810 | |
|---|
| 811 | } |
|---|