Changeset 1340 for code/branches/input/src/core/InputHandler.cc
- Timestamp:
- May 21, 2008, 12:23:29 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/input/src/core/InputHandler.cc
r1323 r1340 45 45 { 46 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_) 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 // ############################### 47 302 // ###### KeyBinder ###### 48 303 // ############################### … … 54 309 { 55 310 RegisterObject(KeyBinder); 56 clearBindings(true);57 311 58 312 // keys … … 60 314 "UNASSIGNED", 61 315 "ESCAPE", 62 "1", 63 "2", 64 "3", 65 "4", 66 "5", 67 "6", 68 "7", 69 "8", 70 "9", 71 "0", 72 "MINUS", 73 "EQUALS", 74 "BACK", 75 "TAB", 76 "Q", 77 "W", 78 "E", 79 "R", 80 "T", 81 "Y", 82 "U", 83 "I", 84 "O", 85 "P", 86 "LBRACKET", 87 "RBRACKET", 88 "RETURN", 89 "LCONTROL", 90 "A", 91 "S", 92 "D", 93 "F", 94 "G", 95 "H", 96 "J", 97 "K", 98 "L", 99 "SEMICOLON", 100 "APOSTROPHE", 101 "GRAVE", 102 "LSHIFT", 103 "BACKSLASH", 104 "Z", 105 "X", 106 "C", 107 "V", 108 "B", 109 "N", 110 "M", 111 "COMMA", 112 "PERIOD", 113 "SLASH", 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", 114 326 "RSHIFT", 115 327 "MULTIPLY", … … 117 329 "SPACE", 118 330 "CAPITAL", 119 "F1", 120 "F2", 121 "F3", 122 "F4", 123 "F5", 124 "F6", 125 "F7", 126 "F8", 127 "F9", 128 "F10", 129 "NUMLOCK", 130 "SCROLL", 131 "NUMPAD7", 132 "NUMPAD8", 133 "NUMPAD9", 331 "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", 332 "NUMLOCK", "SCROLL", 333 "NUMPAD7", "NUMPAD8", "NUMPAD9", 134 334 "SUBTRACT", 135 "NUMPAD4", 136 "NUMPAD5", 137 "NUMPAD6", 335 "NUMPAD4", "NUMPAD5", "NUMPAD6", 138 336 "ADD", 139 "NUMPAD1", 140 "NUMPAD2", 141 "NUMPAD3", 142 "NUMPAD0", 337 "NUMPAD1", "NUMPAD2", "NUMPAD3", "NUMPAD0", 143 338 "DECIMAL", 144 339 "","", 145 340 "OEM_102", 146 "F11", 147 "F12", 341 "F11", "F12", 148 342 "","","","","","","","","","","", 149 "F13", 150 "F14", 151 "F15", 343 "F13", "F14", "F15", 152 344 "","","","","","","","","","", 153 345 "KANA", … … 166 358 "PREVTRACK", 167 359 "AT", 168 "COLON", 169 "UNDERLINE", 360 "COLON", "UNDERLINE", 170 361 "KANJI", 171 362 "STOP", … … 205 396 "RIGHT", 206 397 "", 207 "END", 208 "DOWN", 209 "PGDOWN", 210 "INSERT", 211 "DELETE", 398 "END", "DOWN", "PGDOWN", "INSERT", "DELETE", 212 399 "","","","","","","", 213 "LWIN", 214 "RWIN", 215 "APPS", 216 "POWER", 217 "SLEEP", 400 "LWIN", "RWIN", "APPS", 401 "POWER", "SLEEP", 218 402 "","","", 219 403 "WAKE", 220 404 "", 221 "WEBSEARCH", 222 "WEBFAVORITES", 223 "WEBREFRESH", 224 "WEBSTOP", 225 "WEBFORWARD", 226 "WEBBACK", 227 "MYCOMPUTER", 228 "MAIL", 229 "MEDIASELECT" 405 "WEBSEARCH", "WEBFAVORITES", "WEBREFRESH", "WEBSTOP", "WEBFORWARD", "WEBBACK", 406 "MYCOMPUTER", "MAIL", "MEDIASELECT" 230 407 }; 231 408 for (int i = 0; i < nKeys_s; i++) 232 namesKeys_[i]= "Key" + keyNames[i];409 keys_[i].name_ = "Key" + keyNames[i]; 233 410 234 411 // mouse buttons … … 238 415 "MouseButton6", "MouseButton7" }; 239 416 for (int i = 0; i < nMouseButtons_s; i++) 240 namesMouseButtons_[i]= mouseButtonNames[i];417 mouseButtons_[i].name_ = mouseButtonNames[i]; 241 418 242 419 // joy stick buttons 243 420 for (int i = 0; i < 32; i++) 244 namesJoyStickButtons_[i]= "JoyButton" + getConvertedValue<int, std::string>(i);421 joyStickButtons_[i].name_ = "JoyButton" + getConvertedValue<int, std::string>(i); 245 422 for (int i = 32; i < nJoyStickButtons_s; i += 4) 246 423 { 247 namesJoyStickButtons_[i + 0]= "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "North";248 namesJoyStickButtons_[i + 1]= "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "South";249 namesJoyStickButtons_[i + 2]= "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "East";250 namesJoyStickButtons_[i + 3]= "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "West";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"; 251 428 } 252 429 … … 261 438 for (unsigned int i = 0; i < nHalfAxes_s/2; i++) 262 439 { 263 namesHalfAxes_[i * 2 + 0] = rawNames[i] + "Pos"; 264 namesHalfAxes_[i * 2 + 1] = rawNames[i] + "Neg"; 265 } 440 halfAxes_[i * 2 + 0].name_ = rawNames[i] + "Pos"; 441 halfAxes_[i * 2 + 1].name_ = rawNames[i] + "Neg"; 442 } 443 444 for (unsigned int i = 0; i < this->nHalfAxes_s; i++) 445 halfAxes_[i].buttonThreshold_ = buttonThreshold_; 266 446 } 267 447 … … 276 456 277 457 /** 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 /** 278 473 @brief Loader for the key bindings, managed by config values. 279 474 */ 280 475 void KeyBinder::setConfigValues() 281 476 { 282 bool success = true; 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 283 485 // keys 284 success |= readBindings(namesKeys_, bindingStringsKeys_, bindingsKeys_, nKeys_s); 486 for (unsigned int i = 0; i < nKeys_s; i++) 487 readTrigger(keys_[i]); 285 488 // mouse buttons 286 success |= readBindings(namesMouseButtons_, bindingStringsMouseButtons_, bindingsMouseButtons_, nMouseButtons_s); 489 for (unsigned int i = 0; i < nMouseButtons_s; i++) 490 readTrigger(mouseButtons_[i]); 287 491 // joy stick buttons 288 success |= readBindings(namesJoyStickButtons_, bindingStringsJoyStickButtons_,289 bindingsJoyStickButtons_, nJoyStickButtons_s);492 for (unsigned int i = 0; i < nJoyStickButtons_s; i++) 493 readTrigger(joyStickButtons_[i]); 290 494 // half axes 291 success |= readBindings(namesHalfAxes_, bindingStringsHalfAxes_, bindingsHalfAxes_, nHalfAxes_s); 292 293 // TODO: what happens if parsing didn't succeed in all parts? nothing? 294 } 295 296 bool KeyBinder::readBindings(std::string* names, std::string* bindingStrings, 297 KeyBindingBundle* bindings, unsigned int size) 298 { 299 for (unsigned int i = 0; i < size; i++) 300 { 301 // config value stuff 302 ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer(names[i]); 303 if (!cont) 304 { 305 cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), names[i], ""); 306 getIdentifier()->addConfigValueContainer(names[i], cont); 307 } 308 std::string old = bindingStrings[i]; 309 cont->getValue(&bindingStrings[i]); 310 311 // keybinder stuff 312 if (old != bindingStrings[i]) 313 { 314 // binding has changed 315 if (bindingStrings[i] == "") 316 { 317 // empty binding, occurs at least the first time since init value is " " 318 bindings[i].OnPress.clear(); 319 bindings[i].OnRelease.clear(); 320 bindings[i].OnHold.clear(); 321 } 322 else 323 { 324 // actually parse the command(s) 325 SubString commands(bindingStrings[i], "|", SubString::WhiteSpaces, false, 326 '\\', false, '"', false, '(', ')', false, '\0'); 327 bindings[i].OnHold.nCommands = 0; 328 bindings[i].OnHold.commands = new SimpleCommand[64]; 329 bindings[i].OnPress.nCommands = 0; 330 bindings[i].OnPress.commands = new SimpleCommand[64]; 331 bindings[i].OnRelease.nCommands = 0; 332 bindings[i].OnRelease.commands = new SimpleCommand[64]; 333 for (unsigned int iCommand = 0; iCommand < commands.size(); iCommand++) 334 { 335 if (commands[iCommand] != "") 336 { 337 SubString tokens(commands[iCommand], " ", SubString::WhiteSpaces, false, 338 '\\', false, '"', false, '(', ')', false, '\0'); 339 340 unsigned int iToken = 0; 341 342 // first argument can be OnPress, OnHold OnRelease or nothing 343 KeybindMode::Enum mode = KeybindMode::None; 344 if (getLowercase(tokens[iToken]) == "onpress") 345 mode = KeybindMode::OnPress, iToken++; 346 if (getLowercase(tokens[iToken]) == "onrelease") 347 mode = KeybindMode::OnRelease, iToken++; 348 if (getLowercase(tokens[iToken]) == "onhold") 349 mode = KeybindMode::OnHold, iToken++; 350 351 if (iToken == tokens.size()) 352 continue; 353 354 SimpleCommand* cmd = new SimpleCommand(); 355 356 // second argument can be the amplitude for the case it as an axis command 357 // default amplitude is 1.0f 358 if (getLowercase(tokens[iToken]) == "axisamp") 359 { 360 iToken++; 361 float value; 362 if (iToken == tokens.size() || !convertValue(&value, tokens[iToken])) 363 { 364 CCOUT(2) << "Error while parsing key binding " << names[i] 365 << ". Numeric expression expected afer 'AxisAmp', switching to default value" << std::endl; 366 if (iToken == tokens.size()) 367 { 368 delete cmd; 369 continue; 370 } 371 cmd->axisModifier = 1.0f; 372 } 373 else 374 cmd->axisModifier = value; 375 iToken++; 376 } 377 else 378 cmd->axisModifier = 1.0f; 379 380 // no more arguments expected except for the actual command 381 if (iToken == tokens.size()) 382 { // no command given 383 delete cmd; 384 continue; 385 } 386 while (iToken != tokens.size()) 387 cmd->commandStr += tokens[iToken++] + " "; 388 389 // check whether we exceed 64 commands... 390 if (bindings[i].OnHold.nCommands == 64 || bindings[i].OnPress.nCommands == 64 391 || bindings[i].OnRelease.nCommands == 64) 392 { 393 CCOUT(2) << "Error while parsing key binding " << names[i] 394 << ". You shouldn't assign more than 64 key bindings to one key " 395 << "just to test the parser" << std::endl; 396 } 397 398 // evaluate the command 399 cmd->axisCommand = 0; 400 CommandEvaluation& eval = CommandExecutor::evaluate(cmd->commandStr); 401 // TOOD: check for axis command 402 if (false) 403 { 404 cmd->axisCommand->commandStr = cmd->commandStr; 405 cmd->commandStr = ""; 406 cmd->axisCommand->evaluation = eval; 407 // add command to the buffer if not yet existing 408 for (unsigned int iAxisCmd = 0; iAxisCmd < axisCommands_.size(); iAxisCmd++) 409 { 410 if (getLowercase(axisCommands_[iAxisCmd]->commandStr) == getLowercase(cmd->commandStr)) 411 { 412 // already in list 413 cmd->axisCommand = axisCommands_[iAxisCmd]; 414 break; 415 } 416 } 417 if (cmd->axisCommand == 0) 418 { 419 cmd->axisCommand = new AxisCommand(); 420 axisCommands_.push_back(cmd->axisCommand); 421 } 422 // TODO: check for relative/absolute command 423 cmd->axisCommand->bRelative = false; 424 425 // axis commands are always OnHold 426 *(bindings[i].OnHold.commands + bindings[i].OnHold.nCommands++) = *cmd; 427 } 428 else 429 { 430 cmd->evaluation = eval; 431 432 // TODO: determine whether the command is OnHold, OnPress or OnRelease 433 switch (mode) 434 { 435 case KeybindMode::None: 436 *(bindings[i].OnPress.commands + bindings[i].OnPress.nCommands++) = *cmd; 437 break; 438 case KeybindMode::OnPress: 439 *(bindings[i].OnPress.commands + bindings[i].OnPress.nCommands++) = *cmd; 440 break; 441 case KeybindMode::OnHold: 442 *(bindings[i].OnHold.commands + bindings[i].OnHold.nCommands++) = *cmd; 443 break; 444 case KeybindMode::OnRelease: 445 *(bindings[i].OnRelease.commands + bindings[i].OnRelease.nCommands++) = *cmd; 446 break; 447 } 448 } 449 } 450 } 451 452 // redimension arrays with simple commands 453 SimpleCommand* sCmd = bindings[i].OnHold.commands; 454 if (bindings[i].OnHold.nCommands) 455 { 456 bindings[i].OnHold.commands = new SimpleCommand[bindings[i].OnHold.nCommands]; 457 for (unsigned int iCmd = 0; iCmd < bindings[i].OnHold.nCommands; iCmd++) 458 bindings[i].OnHold.commands[iCmd] = sCmd[iCmd]; 459 } 460 else 461 bindings[i].OnHold.commands = 0; 462 delete[] sCmd; 463 464 sCmd = bindings[i].OnPress.commands; 465 if (bindings[i].OnPress.nCommands) 466 { 467 bindings[i].OnPress.commands = new SimpleCommand[bindings[i].OnPress.nCommands]; 468 for (unsigned int iCmd = 0; iCmd < bindings[i].OnPress.nCommands; iCmd++) 469 bindings[i].OnPress.commands[iCmd] = sCmd[iCmd]; 470 } 471 else 472 bindings[i].OnPress.commands = 0; 473 delete[] sCmd; 474 475 sCmd = bindings[i].OnRelease.commands; 476 if (bindings[i].OnRelease.nCommands) 477 { 478 bindings[i].OnRelease.commands = new SimpleCommand[bindings[i].OnRelease.nCommands]; 479 for (unsigned int iCmd = 0; iCmd < bindings[i].OnRelease.nCommands; iCmd++) 480 bindings[i].OnRelease.commands[iCmd] = sCmd[iCmd]; 481 } 482 else 483 bindings[i].OnRelease.commands = 0; 484 delete[] sCmd; 485 } 486 } 487 } 488 return true; 495 for (unsigned int i = 0; i < nHalfAxes_s; i++) 496 readTrigger(halfAxes_[i]); 497 } 498 499 void KeyBinder::readTrigger(Button& button) 500 { 501 // config value stuff 502 ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer(button.name_); 503 if (!cont) 504 { 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_); 510 511 // keybinder stuff 512 if (old != button.bindingString_) 513 { 514 // binding has changed 515 button.parse(paramCommandBuffer_); 516 } 489 517 } 490 518 … … 495 523 { 496 524 for (int i = 0; i < nKeys_s; i++) 497 { 498 clearBundle(bindingsKeys_[i], bInit); 499 bindingStringsKeys_[i] = " "; 500 } 525 keys_[i].clear(); 526 501 527 for (int i = 0; i < nMouseButtons_s; i++) 502 { 503 clearBundle(bindingsMouseButtons_[i], bInit); 504 bindingStringsMouseButtons_[i] = " "; 505 } 528 mouseButtons_[i].clear(); 529 506 530 for (int i = 0; i < nJoyStickButtons_s; i++) 507 { 508 clearBundle(bindingsJoyStickButtons_[i], bInit); 509 bindingStringsJoyStickButtons_[i] = " "; 510 } 531 joyStickButtons_[i].clear(); 532 511 533 for (int i = 0; i < nHalfAxes_s; i++) 512 { 513 clearBundle(bindingsHalfAxes_[i], bInit); 514 bindingStringsHalfAxes_[i] = " "; 515 } 516 for (unsigned int i = 0; i < axisCommands_.size(); i++) 517 delete axisCommands_[i]; 518 axisCommands_.clear(); 519 } 520 521 void KeyBinder::clearBundle(KeyBindingBundle& bundle, bool bInit) 522 { 523 if (!bInit) 524 { 525 if (bundle.OnHold.nCommands) 526 delete[] bundle.OnHold.commands; 527 if (bundle.OnPress.nCommands) 528 delete[] bundle.OnPress.commands; 529 if (bundle.OnRelease.nCommands) 530 delete[] bundle.OnRelease.commands; 531 } 532 bundle.OnPress.nCommands = 0; 533 bundle.OnHold.nCommands = 0; 534 bundle.OnRelease.nCommands = 0; 535 } 536 537 /** 538 @brief Loads the key and button bindings. 539 @return True if loading succeeded. 540 */ 541 bool KeyBinder::loadBindings() 542 { 543 COUT(3) << "KeyBinder: Loading key bindings..." << std::endl; 544 545 // clear half axes 546 for (unsigned int i = 0; i < nHalfAxes_s; i++) 547 { 548 halfAxes_[i].hasChanged = false; 549 halfAxes_[i].abs = 0.0f; 550 halfAxes_[i].rel = 0.0f; 551 halfAxes_[i].wasDown = false; 552 halfAxes_[i].threshold = 0.01f; 553 } 554 555 ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini"); 556 clearBindings(); 557 setConfigValues(); 558 559 COUT(3) << "KeyBinder: Loading key bindings done." << std::endl; 560 return true; 534 halfAxes_[i].clear(); 535 536 for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++) 537 delete paramCommandBuffer_[i]; 538 paramCommandBuffer_.clear(); 561 539 } 562 540 … … 566 544 for (unsigned int i = 0; i < nHalfAxes_s; i++) 567 545 { 568 if (!halfAxes_[i].hasChanged) 569 { 570 if (!halfAxes_[i].wasDown && halfAxes_[i].abs > halfAxes_[i].threshold) 571 { 572 halfAxes_[i].wasDown = true; 573 if (bindingsHalfAxes_[i].OnPress.nCommands) 574 executeBinding(bindingsHalfAxes_[i].OnPress, halfAxes_[i].rel, halfAxes_[i].abs); 575 } 576 else if (halfAxes_[i].wasDown && halfAxes_[i].abs < halfAxes_[i].threshold) 577 { 578 halfAxes_[i].wasDown = false; 579 if (bindingsHalfAxes_[i].OnRelease.nCommands) 580 executeBinding(bindingsHalfAxes_[i].OnRelease, halfAxes_[i].rel, halfAxes_[i].abs); 581 } 582 if (halfAxes_[i].wasDown) 583 { 584 executeBinding(bindingsHalfAxes_[i].OnHold, halfAxes_[i].rel, halfAxes_[i].abs); 585 } 586 halfAxes_[i].hasChanged = false; 546 if (halfAxes_[i].hasChanged_) 547 { 548 if (!halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ > halfAxes_[i].buttonThreshold_) 549 { 550 halfAxes_[i].wasDown_ = true; 551 if (halfAxes_[i].nCommands_[KeybindMode::OnPress]) 552 halfAxes_[i].execute(KeybindMode::OnPress); 553 } 554 else if (halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ < halfAxes_[i].buttonThreshold_) 555 { 556 halfAxes_[i].wasDown_ = false; 557 if (halfAxes_[i].nCommands_[KeybindMode::OnRelease]) 558 halfAxes_[i].execute(KeybindMode::OnRelease); 559 } 560 if (halfAxes_[i].wasDown_) 561 { 562 if (halfAxes_[i].nCommands_[KeybindMode::OnHold]) 563 halfAxes_[i].execute(KeybindMode::OnHold); 564 } 565 halfAxes_[i].hasChanged_ = false; 566 } 567 568 // these are the actually useful axis bindings for analog input AND output 569 if (halfAxes_[i].relVal_ > analogThreshold_ || halfAxes_[i].absVal_ > analogThreshold_) 570 { 571 halfAxes_[i].execute(); 587 572 } 588 573 } 589 574 590 575 // execute all buffered bindings (addional parameter) 591 for (unsigned int i = 0; i < axisCommands_.size(); i++) 592 { 593 if (axisCommands_[i]->nValuesAdded > 0) 594 { 595 axisCommands_[i]->evaluation.setEvaluatedParameter(0, axisCommands_[i]->value); 596 // reset 597 axisCommands_[i]->nValuesAdded = 0; 598 axisCommands_[i]->value = 0.0f; 599 } 600 } 601 } 602 603 bool KeyBinder::executeBinding(KeyBinding& binding, float axisRel, float axisAbs) 604 { 605 // execute all the parsed commands in the string 606 for (unsigned int iCommand = 0; iCommand < binding.nCommands; iCommand++) 607 { 608 SimpleCommand& command = binding.commands[iCommand]; 609 if (command.axisCommand) 610 { 611 AxisCommand& axisCommand = *command.axisCommand; 612 // command has an additional parameter 613 if (command.axisCommand->bRelative) 614 { 615 // we have to calculate a relative movement. 616 // amplitude says how much one keystroke is 617 axisCommand.value += command.axisModifier * axisRel; 618 } 619 else 620 { 621 // we have to calculate absolute position of the axis. 622 // for a key this simply is 1, but multiplied by a user defined factor 623 // since there might be another axis that is affected, we have to wait and 624 // store the result in a temporary place 625 axisCommand.value = 626 (axisCommand.value * (axisCommand.nValuesAdded++) + command.axisModifier * axisAbs) 627 / axisCommand.nValuesAdded; 628 } 629 } 630 else 631 { 632 // simple command, just execute directly 633 // TODO: calculate whether this a Press, Release or Hold event 634 CommandExecutor::execute(command.evaluation); 635 } 636 } 637 return true; 638 } 639 640 641 /** 642 @brief Event handler for the keyPressed Event. 643 @param e Event information 644 */ 645 bool KeyBinder::keyPressed(const KeyEvent& evt) 646 { 647 // find the appropriate key binding 648 executeBinding(bindingsKeys_[int(evt.key)].OnPress, 1.0, 1.0); 649 650 return true; 651 } 652 653 /** 654 @brief Event handler for the keyReleased Event. 655 @param e Event information 656 */ 657 bool KeyBinder::keyReleased(const KeyEvent& evt) 658 { 659 // find the appropriate key binding 660 executeBinding(bindingsKeys_[int(evt.key)].OnRelease, 1.0, 1.0); 661 662 return true; 663 } 664 665 /** 666 @brief Event handler for the keyHeld Event. 667 @param e Mouse state information 668 */ 669 bool KeyBinder::keyHeld(const KeyEvent& evt) 670 { 671 // find the appropriate key binding 672 executeBinding(bindingsKeys_[int(evt.key)].OnHold, 1.0, 1.0); 673 674 return true; 675 } 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; 582 } 583 584 void KeyBinder::keyPressed (const KeyEvent& evt) 585 { keys_[evt.key].execute(KeybindMode::OnPress); } 586 587 void KeyBinder::keyReleased(const KeyEvent& evt) 588 { keys_[evt.key].execute(KeybindMode::OnRelease); } 589 590 void KeyBinder::keyHeld (const KeyEvent& evt) 591 { keys_[evt.key].execute(KeybindMode::OnHold); } 592 593 594 void KeyBinder::mouseButtonPressed (MouseButton::Enum id) 595 { mouseButtons_[id].execute(KeybindMode::OnPress); } 596 597 void KeyBinder::mouseButtonReleased(MouseButton::Enum id) 598 { mouseButtons_[id].execute(KeybindMode::OnRelease); } 599 600 void KeyBinder::mouseButtonHeld (MouseButton::Enum id) 601 { mouseButtons_[id].execute(KeybindMode::OnHold); } 602 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); } 676 612 677 613 /** … … 679 615 @param e Mouse state information 680 616 */ 681 bool KeyBinder::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) 682 { 683 halfAxes_[0].hasChanged = true; 684 halfAxes_[1].hasChanged = true; 685 halfAxes_[2].hasChanged = true; 686 halfAxes_[3].hasChanged = true; 617 void KeyBinder::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) 618 { 687 619 // translate absolute mouse position into joystick like behaviour 688 620 if (clippingSize.x > clippingSize.y) … … 691 623 if (abs.x - margin > clippingSize.y) 692 624 { 693 halfAxes_[0].abs = 1.0f;694 halfAxes_[1].abs = 0.0f;625 halfAxes_[0].absVal_ = 1.0f; 626 halfAxes_[1].absVal_ = 0.0f; 695 627 } 696 628 else if (abs.x < margin) 697 629 { 698 halfAxes_[0].abs = 0.0f;699 halfAxes_[1].abs = 1.0f;630 halfAxes_[0].absVal_ = 0.0f; 631 halfAxes_[1].absVal_ = 1.0f; 700 632 } 701 633 else … … 704 636 if (temp > 0) 705 637 { 706 halfAxes_[0].abs = temp;707 halfAxes_[1].abs = 0.0f;638 halfAxes_[0].absVal_ = temp; 639 halfAxes_[1].absVal_ = 0.0f; 708 640 } 709 641 else 710 642 { 711 halfAxes_[0].abs = 0.0f;712 halfAxes_[1].abs = -temp;643 halfAxes_[0].absVal_ = 0.0f; 644 halfAxes_[1].absVal_ = -temp; 713 645 } 714 646 } … … 717 649 if (temp > 0) 718 650 { 719 halfAxes_[2].abs = temp;720 halfAxes_[3].abs = 0.0;651 halfAxes_[2].absVal_ = temp; 652 halfAxes_[3].absVal_ = 0.0; 721 653 } 722 654 else 723 655 { 724 halfAxes_[2].abs = 0.0;725 halfAxes_[3].abs = -temp;656 halfAxes_[2].absVal_ = 0.0; 657 halfAxes_[3].absVal_ = -temp; 726 658 } 727 659 } … … 731 663 if (temp > 0) 732 664 { 733 halfAxes_[0].abs = temp;734 halfAxes_[1].abs = 0.0;665 halfAxes_[0].absVal_ = temp; 666 halfAxes_[1].absVal_ = 0.0; 735 667 } 736 668 else 737 669 { 738 halfAxes_[0].abs = 0.0;739 halfAxes_[1].abs = -temp;670 halfAxes_[0].absVal_ = 0.0; 671 halfAxes_[1].absVal_ = -temp; 740 672 } 741 673 … … 743 675 if (abs.y - margin > clippingSize.x) 744 676 { 745 halfAxes_[2].abs = 0.0;746 halfAxes_[3].abs = 1.0;677 halfAxes_[2].absVal_ = 0.0; 678 halfAxes_[3].absVal_ = 1.0; 747 679 } 748 680 else if (abs.y < margin) 749 681 { 750 halfAxes_[2].abs = 1.0;751 halfAxes_[3].abs = 0.0;682 halfAxes_[2].absVal_ = 1.0; 683 halfAxes_[3].absVal_ = 0.0; 752 684 } 753 685 else … … 756 688 if (temp > 0) 757 689 { 758 halfAxes_[2].abs = temp;759 halfAxes_[3].abs = 0.0;690 halfAxes_[2].absVal_ = temp; 691 halfAxes_[3].absVal_ = 0.0; 760 692 } 761 693 else 762 694 { 763 halfAxes_[2].abs = 0.0;764 halfAxes_[3].abs = -temp;765 } 766 } 767 } 768 695 halfAxes_[2].absVal_ = 0.0; 696 halfAxes_[3].absVal_ = -temp; 697 } 698 } 699 } 700 769 701 // relative movements 770 702 if (rel.x > 0) 771 703 { 772 halfAxes_[0].rel = rel.x; 773 halfAxes_[1].rel = 0.0; 774 } 775 else 776 { 777 halfAxes_[0].rel = 0.0; 778 halfAxes_[1].rel = rel.x; 704 halfAxes_[0].hasChanged_ = true; 705 halfAxes_[1].hasChanged_ = true; 706 halfAxes_[0].relVal_ = rel.x; 707 halfAxes_[1].relVal_ = 0.0; 708 } 709 else if (rel.x < 0) 710 { 711 halfAxes_[0].hasChanged_ = true; 712 halfAxes_[1].hasChanged_ = true; 713 halfAxes_[0].relVal_ = 0.0; 714 halfAxes_[1].relVal_ = rel.x; 779 715 } 780 716 781 717 if (rel.y /*!*/ < /*!*/ 0) 782 718 { 783 halfAxes_[0].rel = -rel.y; 784 halfAxes_[1].rel = 0.0; 785 } 786 else 787 { 788 halfAxes_[0].rel = 0.0; 789 halfAxes_[1].rel = -rel.y; 790 } 791 792 return true; 719 halfAxes_[2].hasChanged_ = true; 720 halfAxes_[3].hasChanged_ = true; 721 halfAxes_[0].relVal_ = -rel.y; 722 halfAxes_[1].relVal_ = 0.0; 723 } 724 else if (rel.y > 0) 725 { 726 halfAxes_[2].hasChanged_ = true; 727 halfAxes_[3].hasChanged_ = true; 728 halfAxes_[0].relVal_ = 0.0; 729 halfAxes_[1].relVal_ = -rel.y; 730 } 793 731 } 794 732 … … 797 735 @param e Mouse state information 798 736 */ 799 boolKeyBinder::mouseScrolled(int abs, int rel)737 void KeyBinder::mouseScrolled(int abs, int rel) 800 738 { 801 739 // TODO: obvious... 802 return true; 803 } 804 805 /** 806 @brief Event handler for the mousePressed Event. 807 @param e Event information 808 @param id The ID of the mouse button 809 */ 810 bool KeyBinder::mouseButtonPressed(MouseButton::Enum id) 811 { 812 // find the appropriate key binding 813 executeBinding(bindingsMouseButtons_[int(id)].OnPress, 1.0, 1.0); 814 815 return true; 816 } 817 818 /** 819 @brief Event handler for the mouseReleased Event. 820 @param e Event information 821 @param id The ID of the mouse button 822 */ 823 bool KeyBinder::mouseButtonReleased(MouseButton::Enum id) 824 { 825 // find the appropriate key binding 826 executeBinding(bindingsMouseButtons_[int(id)].OnRelease, 1.0, 1.0); 827 828 return true; 829 } 830 831 /** 832 @brief Event handler for the mouseHeld Event. 833 @param e Event information 834 @param id The ID of the mouse button 835 */ 836 bool KeyBinder::mouseButtonHeld(MouseButton::Enum id) 837 { 838 // find the appropriate key binding 839 executeBinding(bindingsMouseButtons_[int(id)].OnHold, 1.0, 1.0); 840 841 return true; 842 } 843 844 bool KeyBinder::joyStickButtonPressed(int joyStickID, int button) 845 { 846 // find the appropriate key binding 847 executeBinding(bindingsJoyStickButtons_[button].OnPress, 1.0, 1.0); 848 849 return true; 850 } 851 852 bool KeyBinder::joyStickButtonReleased(int joyStickID, int button) 853 { 854 // find the appropriate key binding 855 executeBinding(bindingsJoyStickButtons_[button].OnRelease, 1.0, 1.0); 856 857 return true; 858 } 859 860 bool KeyBinder::joyStickButtonHeld(int joyStickID, int button) 861 { 862 // find the appropriate key binding 863 executeBinding(bindingsJoyStickButtons_[button].OnHold, 1.0, 1.0); 864 865 return true; 866 } 867 868 bool KeyBinder::joyStickAxisMoved(int joyStickID, int axis, int value) 740 } 741 742 void KeyBinder::joyStickAxisMoved(int joyStickID, int axis, int value) 869 743 { 870 744 // TODO: check whether 16 bit integer as general axis value is a good idea (works under windows) 871 halfAxes_[8 + axis].hasChanged = true;745 //CCOUT(3) << axis << std::endl; 872 746 if (value >= 0) 873 747 { 874 halfAxes_[8 + axis].abs = ((float)value)/0x1000; 875 halfAxes_[8 + axis].hasChanged = true; 748 halfAxes_[8 + axis].absVal_ = ((float)value)/0x8000; 749 halfAxes_[8 + axis].relVal_ = ((float)value)/0x8000; 750 halfAxes_[8 + axis].hasChanged_ = true; 876 751 } 877 752 else 878 753 { 879 halfAxes_[8 + axis + 1].abs = -((float)value)/0x1000;880 halfAxes_[8 + axis + 1]. hasChanged = true;881 }882 return true;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; 757 } 883 758 } 884 759
Note: See TracChangeset
for help on using the changeset viewer.