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