Changeset 1349 for code/trunk/src/core/InputHandler.cc
- Timestamp:
- May 21, 2008, 9:07:08 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/core/InputHandler.cc
r1293 r1349 34 34 #include "InputHandler.h" 35 35 #include "util/Convert.h" 36 #include "util/SubString.h" 37 #include "util/String.h" 36 38 #include "Debug.h" 37 39 #include "ConfigValueIncludes.h" 38 40 #include "CoreIncludes.h" 39 41 #include "CommandExecutor.h" 42 #include "Executor.h" 40 43 41 44 namespace orxonox 42 45 { 43 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 // ############################### 44 301 // ###### KeyBinder ###### 45 302 // ############################### … … 48 305 @brief Constructor that does as little as necessary. 49 306 */ 50 KeyBinder::KeyBinder() 307 KeyBinder::KeyBinder() : deriveTime_(0.0f) 51 308 { 52 309 RegisterObject(KeyBinder); 53 clearBindings(); 54 310 311 // keys 55 312 std::string keyNames[] = { 56 "UNASSIGNED", 57 "ESCAPE", 58 "1", 59 "2", 60 "3", 61 "4", 62 "5", 63 "6", 64 "7", 65 "8", 66 "9", 67 "0", 68 "MINUS", 69 "EQUALS", 70 "BACK", 71 "TAB", 72 "Q", 73 "W", 74 "E", 75 "R", 76 "T", 77 "Y", 78 "U", 79 "I", 80 "O", 81 "P", 82 "LBRACKET", 83 "RBRACKET", 84 "RETURN", 85 "LCONTROL", 86 "A", 87 "S", 88 "D", 89 "F", 90 "G", 91 "H", 92 "J", 93 "K", 94 "L", 95 "SEMICOLON", 96 "APOSTROPHE", 97 "GRAVE", 98 "LSHIFT", 99 "BACKSLASH", 100 "Z", 101 "X", 102 "C", 103 "V", 104 "B", 105 "N", 106 "M", 107 "COMMA", 108 "PERIOD", 109 "SLASH", 110 "RSHIFT", 111 "MULTIPLY", 112 "LMENU", 113 "SPACE", 114 "CAPITAL", 115 "F1", 116 "F2", 117 "F3", 118 "F4", 119 "F5", 120 "F6", 121 "F7", 122 "F8", 123 "F9", 124 "F10", 125 "NUMLOCK", 126 "SCROLL", 127 "NUMPAD7", 128 "NUMPAD8", 129 "NUMPAD9", 130 "SUBTRACT", 131 "NUMPAD4", 132 "NUMPAD5", 133 "NUMPAD6", 134 "ADD", 135 "NUMPAD1", 136 "NUMPAD2", 137 "NUMPAD3", 138 "NUMPAD0", 139 "DECIMAL", 140 "","", 141 "OEM_102", 142 "F11", 143 "F12", 144 "","","","","","","","","","","", 145 "F13", 146 "F14", 147 "F15", 148 "","","","","","","","","","", 149 "KANA", 150 "","", 151 "ABNT_C1", 152 "","","","","", 153 "CONVERT", 154 "", 155 "NOCONVERT", 156 "", 157 "YEN", 158 "ABNT_C2", 159 "","","","","","","","","","","","","","", 160 "NUMPADEQUALS", 161 "","", 162 "PREVTRACK", 163 "AT", 164 "COLON", 165 "UNDERLINE", 166 "KANJI", 167 "STOP", 168 "AX", 169 "UNLABELED", 170 "NEXTTRACK", 171 "","", 172 "NUMPADENTER", 173 "RCONTROL", 174 "","", 175 "MUTE", 176 "CALCULATOR", 177 "PLAYPAUSE", 178 "", 179 "MEDIASTOP", 180 "","","","","","","","","", 181 "VOLUMEDOWN", 182 "", 183 "VOLUMEUP", 184 "", 185 "WEBHOME", 186 "NUMPADCOMMA", 187 "", 188 "DIVIDE", 189 "", 190 "SYSRQ", 191 "RMENU", 192 "","","","","","","","","","","","", 193 "PAUSE", 194 "", 195 "HOME", 196 "UP", 197 "PGUP", 198 "", 199 "LEFT", 200 "", 201 "RIGHT", 202 "", 203 "END", 204 "DOWN", 205 "PGDOWN", 206 "INSERT", 207 "DELETE", 208 "","","","","","","", 209 "LWIN", 210 "RWIN", 211 "APPS", 212 "POWER", 213 "SLEEP", 214 "","","", 215 "WAKE", 216 "", 217 "WEBSEARCH", 218 "WEBFAVORITES", 219 "WEBREFRESH", 220 "WEBSTOP", 221 "WEBFORWARD", 222 "WEBBACK", 223 "MYCOMPUTER", 224 "MAIL", 225 "MEDIASELECT" 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" 226 406 }; 227 for (int i = 0; i < numberOfKeys_s; i++) 228 keyNames_[i] = keyNames[i]; 229 407 for (unsigned int i = 0; i < nKeys_s; i++) 408 keys_[i].name_ = "Key" + keyNames[i]; 409 410 // mouse buttons 230 411 std::string mouseButtonNames[] = { 231 412 "MouseLeft", "MouseRight", "MouseMiddle", 232 413 "MouseButton3", "MouseButton4", "MouseButton5", 233 "MouseButton6", "MouseButton7" }; 234 for (int i = 0; i < numberOfMouseButtons_s; i++) 235 mouseButtonNames_[i] = mouseButtonNames[i]; 236 237 for (int i = 0; i < numberOfJoyStickButtons_s; i++) 238 joyStickButtonNames_[i] = "JoyStick" + getConvertedValue<int, std::string>(i); 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_; 239 447 } 240 448 … … 244 452 KeyBinder::~KeyBinder() 245 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; 246 471 } 247 472 … … 251 476 void KeyBinder::setConfigValues() 252 477 { 253 ConfigValueContainer* cont; 254 std::string modes[] = {"P_", "R_", "H_"}; 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_; 255 489 256 490 // keys 257 for (int i = 0; i < numberOfKeys_s; i++) 258 { 259 for (int j = 0; j < 3; j++) 260 { 261 cont = getIdentifier()->getConfigValueContainer(modes[j] + keyNames_[i]); 262 if (!cont) 263 { 264 cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), modes[j] + keyNames_[i], ""); 265 getIdentifier()->addConfigValueContainer(modes[j] + keyNames_[i], cont); 266 } 267 switch (j) 268 { 269 case 0: 270 cont->getValue(&bindingsKeyPress_[i].commandStr); 271 break; 272 case 1: 273 cont->getValue(&bindingsKeyRelease_[i].commandStr); 274 break; 275 case 2: 276 cont->getValue(&bindingsKeyHold_[i].commandStr); 277 } 278 } 279 } 280 491 for (unsigned int i = 0; i < nKeys_s; i++) 492 readTrigger(keys_[i]); 281 493 // mouse buttons 282 for (int i = 0; i < numberOfMouseButtons_s; i++) 283 { 284 for (int j = 0; j < 3; j++) 285 { 286 cont = getIdentifier()->getConfigValueContainer(modes[j] + mouseButtonNames_[i]); 287 if (!cont) 288 { 289 cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), modes[j] + mouseButtonNames_[i], ""); 290 getIdentifier()->addConfigValueContainer(modes[j] + mouseButtonNames_[i], cont); 291 } 292 switch (j) 293 { 294 case 0: 295 cont->getValue(&bindingsMouseButtonPress_[i].commandStr); 296 break; 297 case 1: 298 cont->getValue(&bindingsMouseButtonRelease_[i].commandStr); 299 break; 300 case 2: 301 cont->getValue(&bindingsMouseButtonHold_[i].commandStr); 302 } 303 } 304 } 305 494 for (unsigned int i = 0; i < nMouseButtons_s; i++) 495 readTrigger(mouseButtons_[i]); 306 496 // joy stick buttons 307 for (int i = 0; i < numberOfJoyStickButtons_s; i++) 308 { 309 for (int j = 0; j < 3; j++) 310 { 311 cont = getIdentifier()->getConfigValueContainer(modes[j] + joyStickButtonNames_[i]); 312 if (!cont) 313 { 314 cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), modes[j] + joyStickButtonNames_[i], ""); 315 getIdentifier()->addConfigValueContainer(modes[j] + joyStickButtonNames_[i], cont); 316 } 317 switch (j) 318 { 319 case 0: 320 cont->getValue(&bindingsJoyStickButtonPress_[i].commandStr); 321 break; 322 case 1: 323 cont->getValue(&bindingsJoyStickButtonRelease_[i].commandStr); 324 break; 325 case 2: 326 cont->getValue(&bindingsJoyStickButtonHold_[i].commandStr); 327 } 328 } 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_); 329 521 } 330 522 } … … 333 525 @brief Overwrites all bindings with "" 334 526 */ 335 void KeyBinder::clearBindings() 336 { 337 for (int i = 0; i < numberOfKeys_s; i++) 338 { 339 bindingsKeyPress_ [i].commandStr = ""; 340 bindingsKeyRelease_[i].commandStr = ""; 341 bindingsKeyHold_ [i].commandStr = ""; 342 } 343 for (int i = 0; i < numberOfMouseButtons_s; i++) 344 { 345 bindingsMouseButtonPress_ [i].commandStr = ""; 346 bindingsMouseButtonRelease_[i].commandStr = ""; 347 bindingsMouseButtonHold_ [i].commandStr = ""; 348 } 349 for (int i = 0; i < numberOfJoyStickButtons_s; i++) 350 { 351 bindingsJoyStickButtonPress_ [i].commandStr = ""; 352 bindingsJoyStickButtonRelease_[i].commandStr = ""; 353 bindingsJoyStickButtonHold_ [i].commandStr = ""; 354 } 355 } 356 357 /** 358 @brief Loads the key and button bindings. 359 @return True if loading succeeded. 360 */ 361 bool KeyBinder::loadBindings() 362 { 363 COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings..." << std::endl; 364 365 ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini"); 366 setConfigValues(); 367 368 // evaluate the key bindings 369 // TODO: what if binding is invalid? 370 for (int i = 0; i < numberOfKeys_s; i++) 371 { 372 if (bindingsKeyPress_[i].commandStr != "") 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_) 373 552 { 374 bindingsKeyPress_[i].evaluation = CommandExecutor::evaluate(bindingsKeyPress_[i].commandStr); 375 bindingsKeyPress_[i].commandStr = bindingsKeyPress_[i].evaluation.getCommandString(); 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; 376 571 } 377 } 378 379 COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings done." << std::endl; 380 return true; 381 } 382 383 bool KeyBinder::executeSimpleBinding(KeyBinding& binding) 384 { 385 if (binding.commandStr != "") 386 { 387 if (binding.commandStr != binding.evaluation.getCommandString()) 572 573 // these are the actually useful axis bindings for analog input AND output 574 if (halfAxes_[i].relVal_ > analogThreshold_ || halfAxes_[i].absVal_ > analogThreshold_) 388 575 { 389 // key binding has changed, reevaluate the command string. 390 binding.evaluation = CommandExecutor::evaluate(binding.commandStr); 391 binding.commandStr = binding.evaluation.getCommandString(); 576 halfAxes_[i].execute(); 392 577 } 393 COUT(ORX_DEBUG) << "Keybinding: Executing command: " << binding.commandStr << std::endl; 394 CommandExecutor::execute(binding.commandStr); 395 } 396 397 return true; 398 } 399 400 401 /** 402 @brief Event handler for the keyPressed Event. 403 @param e Event information 404 */ 405 bool KeyBinder::keyPressed(const KeyEvent& evt) 406 { 407 // find the appropriate key binding 408 executeSimpleBinding(bindingsKeyPress_[int(evt.key)]); 409 410 return true; 411 } 412 413 /** 414 @brief Event handler for the keyReleased Event. 415 @param e Event information 416 */ 417 bool KeyBinder::keyReleased(const KeyEvent& evt) 418 { 419 // find the appropriate key binding 420 executeSimpleBinding(bindingsKeyRelease_[int(evt.key)]); 421 422 return true; 423 } 424 425 /** 426 @brief Event handler for the keyHeld Event. 427 @param e Mouse state information 428 */ 429 bool KeyBinder::keyHeld(const KeyEvent& evt) 430 { 431 // find the appropriate key binding 432 executeSimpleBinding(bindingsKeyHold_[int(evt.key)]); 433 434 return true; 435 } 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); } 436 644 437 645 /** … … 439 647 @param e Mouse state information 440 648 */ 441 bool KeyBinder::mouseMoved(const MouseState &evt) 442 { 443 /*if (bindingMouseMoved_.commandStr != "") 444 { 445 if (bindingMouseMoved_.commandStr != bindingMouseMoved_.evaluation.getCommandString()) 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++) 446 659 { 447 // key binding has changed, reevaluate the command string. 448 bindingMouseMoved_.evaluation = CommandExecutor::evaluate(bindingMouseMoved_.commandStr); 449 bindingMouseMoved_.commandStr = bindingMouseMoved_.evaluation.getCommandString(); 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 } 450 696 } 451 COUT(3) << "Executing command: " << bindingMouseMoved_.commandStr << std::endl; 452 453 bindingMouseMoved_.evaluation.setEvaluatedParameter( 454 CommandExecutor::execute(bindingMouseMoved_.commandStr); 455 }*/ 456 457 return true; 697 } 698 else 699 { 700 mouseRelative_[0] += rel_.x; 701 mouseRelative_[1] -= rel_.y; 702 } 458 703 } 459 704 … … 462 707 @param e Mouse state information 463 708 */ 464 bool KeyBinder::mouseScrolled(const MouseState &evt) 465 { 466 return true; 467 } 468 469 /** 470 @brief Event handler for the mousePressed Event. 471 @param e Event information 472 @param id The ID of the mouse button 473 */ 474 bool KeyBinder::mouseButtonPressed(const MouseState& state, MouseButton::Enum id) 475 { 476 // find the appropriate key binding 477 executeSimpleBinding(bindingsMouseButtonPress_[int(id)]); 478 479 return true; 480 } 481 482 /** 483 @brief Event handler for the mouseReleased Event. 484 @param e Event information 485 @param id The ID of the mouse button 486 */ 487 bool KeyBinder::mouseButtonReleased(const MouseState& state, MouseButton::Enum id) 488 { 489 // find the appropriate key binding 490 executeSimpleBinding(bindingsMouseButtonRelease_[int(id)]); 491 492 return true; 493 } 494 495 /** 496 @brief Event handler for the mouseHeld Event. 497 @param e Event information 498 @param id The ID of the mouse button 499 */ 500 bool KeyBinder::mouseButtonHeld(const MouseState& state, MouseButton::Enum id) 501 { 502 // find the appropriate key binding 503 executeSimpleBinding(bindingsMouseButtonHold_[int(id)]); 504 505 return true; 506 } 507 508 bool KeyBinder::joyStickButtonPressed(const JoyStickState& state, int button) 509 { 510 // find the appropriate key binding 511 executeSimpleBinding(bindingsJoyStickButtonPress_[button]); 512 513 return true; 514 } 515 516 bool KeyBinder::joyStickButtonReleased(const JoyStickState& state, int button) 517 { 518 // find the appropriate key binding 519 executeSimpleBinding(bindingsJoyStickButtonRelease_[button]); 520 521 return true; 522 } 523 524 bool KeyBinder::joyStickButtonHeld(const JoyStickState& state, int button) 525 { 526 // find the appropriate key binding 527 executeSimpleBinding(bindingsJoyStickButtonHold_[button]); 528 529 return true; 530 } 531 532 bool KeyBinder::joyStickAxisMoved(const JoyStickState& state, int axis) 533 { 534 return true; 535 } 536 537 bool KeyBinder::joyStickSliderMoved(const JoyStickState& state, int index) 538 { 539 return true; 540 } 541 542 bool KeyBinder::joyStickPovMoved(const JoyStickState& state, int index) 543 { 544 return true; 545 } 546 547 bool KeyBinder::joyStickVector3Moved(const JoyStickState& state, int index) 548 { 549 return true; 550 } 551 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 } 552 738 553 739
Note: See TracChangeset
for help on using the changeset viewer.