| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
|---|
| 17 | |
|---|
| 18 | #include "shell.h" |
|---|
| 19 | #include "shell_command.h" |
|---|
| 20 | |
|---|
| 21 | #include "text_engine.h" |
|---|
| 22 | #include "list.h" |
|---|
| 23 | #include "graphics_engine.h" |
|---|
| 24 | #include "event_handler.h" |
|---|
| 25 | #include "debug.h" |
|---|
| 26 | #include "class_list.h" |
|---|
| 27 | |
|---|
| 28 | #include "key_names.h" |
|---|
| 29 | #include <stdarg.h> |
|---|
| 30 | #include <stdio.h> |
|---|
| 31 | |
|---|
| 32 | using namespace std; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * standard constructor |
|---|
| 37 | */ |
|---|
| 38 | Shell::Shell () |
|---|
| 39 | { |
|---|
| 40 | this->setClassID(CL_SHELL, "Shell"); |
|---|
| 41 | this->setName("Shell"); |
|---|
| 42 | |
|---|
| 43 | this->keepBufferArray[0] = '\0'; |
|---|
| 44 | this->keepBuffer = false; |
|---|
| 45 | |
|---|
| 46 | this->bActive = false; |
|---|
| 47 | this->buffer = new tList<char>; |
|---|
| 48 | this->bufferIterator = this->buffer->getIterator(); |
|---|
| 49 | |
|---|
| 50 | this->inputHistory = new tList<char>; |
|---|
| 51 | //this->commandList = new tList<ShellCommand>; |
|---|
| 52 | |
|---|
| 53 | this->textSize = 15; |
|---|
| 54 | this->lineSpacing = 5; |
|---|
| 55 | |
|---|
| 56 | //this->bufferSize = 0; |
|---|
| 57 | this->bufferText = NULL; |
|---|
| 58 | this->setBufferSize(100); |
|---|
| 59 | this->bufferDisplaySize = 10; |
|---|
| 60 | this->setAbsCoor2D(3, -400); |
|---|
| 61 | this->delayed = 0; |
|---|
| 62 | this->setRepeatDelay(.3, .05); |
|---|
| 63 | this->pressedKey = SDLK_FIRST; |
|---|
| 64 | |
|---|
| 65 | this->inputLineText = NULL; |
|---|
| 66 | this->inputLine = new char[1]; |
|---|
| 67 | this->inputLine[0] = '\0'; |
|---|
| 68 | |
|---|
| 69 | this->rebuildText(); |
|---|
| 70 | this->completionList = NULL; |
|---|
| 71 | |
|---|
| 72 | // EVENT-Handler subscription of '`' to all States, and all other keyboard commands to ES_SEHLL |
|---|
| 73 | EventHandler* evh = EventHandler::getInstance(); |
|---|
| 74 | evh->subscribe(this, ES_ALL, SDLK_BACKQUOTE); |
|---|
| 75 | for (int i = 1; i < SDLK_LAST; i++) |
|---|
| 76 | evh->subscribe(this, ES_SHELL, i); |
|---|
| 77 | |
|---|
| 78 | //void ShellCommand<T>::registerCommand(const char* commandName, ClassID classID, T* object, void* functionPointer, unsigned int paramCount, ...) |
|---|
| 79 | |
|---|
| 80 | ShellCommand<Shell>::registerCommand("clear", CL_NULL, this, &Shell::clear, 0, 1); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | Shell* Shell::singletonRef = NULL; |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * standard deconstructor |
|---|
| 87 | */ |
|---|
| 88 | Shell::~Shell () |
|---|
| 89 | { |
|---|
| 90 | // delete the displayable Buffers |
|---|
| 91 | for (int i = 0; i < this->bufferDisplaySize; i++) |
|---|
| 92 | delete this->bufferText[i]; |
|---|
| 93 | delete[] this->bufferText; |
|---|
| 94 | |
|---|
| 95 | // delete the inputLine |
|---|
| 96 | delete this->inputLineText; |
|---|
| 97 | delete this->inputLine; |
|---|
| 98 | |
|---|
| 99 | // delete all the Chars in the Buffers |
|---|
| 100 | char* charElem = this->bufferIterator->firstElement(); |
|---|
| 101 | while (charElem != NULL) |
|---|
| 102 | { |
|---|
| 103 | delete charElem; |
|---|
| 104 | charElem = this->bufferIterator->nextElement(); |
|---|
| 105 | } |
|---|
| 106 | delete this->bufferIterator; |
|---|
| 107 | |
|---|
| 108 | // if (this->completionList != NULL) |
|---|
| 109 | //delete this->completionList; |
|---|
| 110 | |
|---|
| 111 | Shell::singletonRef = NULL; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | * activates the shell |
|---|
| 116 | * |
|---|
| 117 | * This also feeds the Last few lines from the main buffers into the displayBuffer |
|---|
| 118 | */ |
|---|
| 119 | void Shell::activate() |
|---|
| 120 | { |
|---|
| 121 | if (this->bActive == true) |
|---|
| 122 | PRINTF(3)("The shell is already active\n"); |
|---|
| 123 | this->bActive = true; |
|---|
| 124 | |
|---|
| 125 | EventHandler::getInstance()->setState(ES_SHELL); |
|---|
| 126 | this->setRelCoorSoft2D(0, 0, 1, 5); |
|---|
| 127 | |
|---|
| 128 | this->bufferIterator->lastElement(); |
|---|
| 129 | for (int i = 0; i < this->bufferDisplaySize; i++) |
|---|
| 130 | this->bufferText[i]->setText(this->bufferIterator->prevElement()); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | * deactiveates the Shell. |
|---|
| 135 | */ |
|---|
| 136 | void Shell::deactivate() |
|---|
| 137 | { |
|---|
| 138 | if (this->bActive == false) |
|---|
| 139 | PRINTF(3)("The shell is already inactive\n"); |
|---|
| 140 | this->bActive = false; |
|---|
| 141 | |
|---|
| 142 | EventHandler::getInstance()->setState(ES_GAME); |
|---|
| 143 | this->setRelCoorSoft2D(0, -400, 1, 5); |
|---|
| 144 | |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | /** |
|---|
| 148 | * sets the size of the text and spacing |
|---|
| 149 | * @param textSize the size of the Text in Pixels |
|---|
| 150 | * @param lineSpacing the size of the Spacing between two lines in pixels |
|---|
| 151 | * |
|---|
| 152 | * this also rebuilds the entire Text, inputLine and displayBuffer, |
|---|
| 153 | * to be accurate again. |
|---|
| 154 | */ |
|---|
| 155 | void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing) |
|---|
| 156 | { |
|---|
| 157 | this->textSize = textSize; |
|---|
| 158 | this->lineSpacing = lineSpacing; |
|---|
| 159 | |
|---|
| 160 | this->rebuildText(); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | /** |
|---|
| 164 | * rebuilds the Text's |
|---|
| 165 | * |
|---|
| 166 | * use this function, if you changed the Font/Size or something else. |
|---|
| 167 | */ |
|---|
| 168 | void Shell::rebuildText() |
|---|
| 169 | { |
|---|
| 170 | if (this->inputLineText == NULL) |
|---|
| 171 | delete this->inputLineText; |
|---|
| 172 | this->inputLineText = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC); |
|---|
| 173 | this->inputLineText->setColor(1, 0, 0); |
|---|
| 174 | this->inputLineText->setAlignment(TEXT_ALIGN_LEFT); |
|---|
| 175 | this->inputLineText->setText(NULL); |
|---|
| 176 | this->inputLineText->setParent2D(this); |
|---|
| 177 | this->inputLineText->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize); |
|---|
| 178 | |
|---|
| 179 | this->setBufferDisplaySize(this->bufferDisplaySize); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | /** |
|---|
| 183 | * sets The count of Lines to display in the buffer. |
|---|
| 184 | * @param bufferDisplaySize the count of lines to display in the Shell-Buffer. |
|---|
| 185 | */ |
|---|
| 186 | void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize) |
|---|
| 187 | { |
|---|
| 188 | if (this->bufferText != NULL) |
|---|
| 189 | { |
|---|
| 190 | for (unsigned int i = 0; i < this->bufferDisplaySize; i++) |
|---|
| 191 | delete this->bufferText[i]; |
|---|
| 192 | delete[] this->bufferText; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | this->bufferText = new Text*[bufferDisplaySize]; |
|---|
| 196 | for (unsigned int i = 0; i < bufferDisplaySize; i++) |
|---|
| 197 | { |
|---|
| 198 | this->bufferText[i] = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC); |
|---|
| 199 | this->bufferText[i]->setColor(1, 0, 0); |
|---|
| 200 | this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT); |
|---|
| 201 | this->bufferText[i]->setRelCoor2D(calculateLinePosition(i)); |
|---|
| 202 | this->bufferText[i]->setText(NULL); |
|---|
| 203 | this->bufferText[i]->setParent2D(this); |
|---|
| 204 | } |
|---|
| 205 | this->bufferDisplaySize = bufferDisplaySize; |
|---|
| 206 | |
|---|
| 207 | this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| 211 | * deletes all the Buffers |
|---|
| 212 | */ |
|---|
| 213 | void Shell::flushBuffers() |
|---|
| 214 | { |
|---|
| 215 | // remove all chars from the BufferTexts. |
|---|
| 216 | if (this->bufferText) |
|---|
| 217 | for (int i = 0; i < this->bufferDisplaySize; i++) |
|---|
| 218 | { |
|---|
| 219 | this->bufferText[i]->setText(NULL, true); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | // delete all the Chars in the Buffers |
|---|
| 223 | tIterator<char>* charIterator = this->buffer->getIterator(); |
|---|
| 224 | char* charElem = charIterator->firstElement(); |
|---|
| 225 | while (charElem != NULL) |
|---|
| 226 | { |
|---|
| 227 | delete charElem; |
|---|
| 228 | |
|---|
| 229 | charElem = charIterator->nextElement(); |
|---|
| 230 | } |
|---|
| 231 | delete charIterator; |
|---|
| 232 | delete this->buffer; |
|---|
| 233 | this->buffer = new tList<char>; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | /** |
|---|
| 237 | * adds a new Line to the List of Buffers |
|---|
| 238 | * @param line the Line as in the first argument in printf |
|---|
| 239 | * @param args the arguments as a va_list |
|---|
| 240 | */ |
|---|
| 241 | bool Shell::addBufferLineStatic(const char* line, ...) |
|---|
| 242 | { |
|---|
| 243 | va_list arguments; |
|---|
| 244 | va_start(arguments, line); |
|---|
| 245 | |
|---|
| 246 | #if DEBUG < 3 |
|---|
| 247 | if (Shell::singletonRef == NULL) |
|---|
| 248 | #endif |
|---|
| 249 | |
|---|
| 250 | vprintf(line, arguments); |
|---|
| 251 | #if DEBUG < 3 |
|---|
| 252 | else |
|---|
| 253 | #else |
|---|
| 254 | if (Shell::singletonRef != NULL) |
|---|
| 255 | #endif |
|---|
| 256 | Shell::singletonRef->addBufferLine(line, arguments); |
|---|
| 257 | return true; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | /** |
|---|
| 261 | * add a Line to the List of Buffers |
|---|
| 262 | * @param line |
|---|
| 263 | * @param arguments |
|---|
| 264 | * |
|---|
| 265 | * This function Adds one line to the buffer. |
|---|
| 266 | * and displays the line as the First Line of the display-buffer |
|---|
| 267 | */ |
|---|
| 268 | void Shell::addBufferLine(const char* line, va_list arguments) |
|---|
| 269 | { |
|---|
| 270 | vsprintf(this->bufferArray, line, arguments); |
|---|
| 271 | |
|---|
| 272 | char* inputEnd; |
|---|
| 273 | char* newLineBegin; |
|---|
| 274 | char* newLineEnd; |
|---|
| 275 | |
|---|
| 276 | // check if we have something left in the buffers |
|---|
| 277 | if (unlikely(this->keepBuffer)) |
|---|
| 278 | { |
|---|
| 279 | strcat(this->keepBufferArray, this->bufferArray); |
|---|
| 280 | inputEnd = this->keepBufferArray + strlen(this->keepBufferArray); |
|---|
| 281 | newLineBegin = this->keepBufferArray; |
|---|
| 282 | this->keepBuffer = false; |
|---|
| 283 | } |
|---|
| 284 | else |
|---|
| 285 | { |
|---|
| 286 | inputEnd = this->bufferArray + strlen(this->bufferArray); |
|---|
| 287 | newLineBegin = this->bufferArray; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | // adding all the new Lines |
|---|
| 291 | while (newLineBegin < inputEnd) |
|---|
| 292 | { |
|---|
| 293 | newLineEnd = strchr(newLineBegin, '\n'); |
|---|
| 294 | if (newLineEnd != NULL && *newLineEnd == '\n') |
|---|
| 295 | *newLineEnd = '\0'; |
|---|
| 296 | else |
|---|
| 297 | { |
|---|
| 298 | // newLineEnd = newLineBegin + strlen(newLineBegin); |
|---|
| 299 | strcpy(this->keepBufferArray, newLineBegin); |
|---|
| 300 | this->keepBuffer = true; |
|---|
| 301 | break; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | char* addLine = new char[strlen(newLineBegin)+1]; |
|---|
| 305 | strcpy(addLine, newLineBegin); |
|---|
| 306 | |
|---|
| 307 | this->buffer->add(addLine); |
|---|
| 308 | |
|---|
| 309 | if (this->buffer->getSize() > this->bufferSize) |
|---|
| 310 | { |
|---|
| 311 | delete this->buffer->firstElement(); |
|---|
| 312 | this->buffer->remove(this->buffer->firstElement()); |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | if (this->bActive) |
|---|
| 316 | { |
|---|
| 317 | this->printToDisplayBuffer(addLine); |
|---|
| 318 | } |
|---|
| 319 | newLineBegin = newLineEnd+1; |
|---|
| 320 | } |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | /** |
|---|
| 324 | * prints out some text to the input-buffers |
|---|
| 325 | * @param text the text to output. |
|---|
| 326 | */ |
|---|
| 327 | void Shell::printToDisplayBuffer(const char* text) |
|---|
| 328 | { |
|---|
| 329 | if(likely(bufferText != NULL)) |
|---|
| 330 | { |
|---|
| 331 | Text* lastText = this->bufferText[this->bufferDisplaySize-1]; |
|---|
| 332 | |
|---|
| 333 | Text* swapText; |
|---|
| 334 | Text* moveText = this->bufferText[0]; |
|---|
| 335 | this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10); |
|---|
| 336 | for (unsigned int i = 1; i < this->bufferDisplaySize; i++) |
|---|
| 337 | { |
|---|
| 338 | if ( i < this->bufferDisplaySize-1) |
|---|
| 339 | this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5); |
|---|
| 340 | swapText = this->bufferText[i]; |
|---|
| 341 | this ->bufferText[i] = moveText; |
|---|
| 342 | moveText = swapText; |
|---|
| 343 | } |
|---|
| 344 | lastText->setRelCoor2D(this->calculateLinePosition(0)); |
|---|
| 345 | this->bufferText[0] = lastText; |
|---|
| 346 | |
|---|
| 347 | this->bufferText[0]->setText(text, true); |
|---|
| 348 | } |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | /** |
|---|
| 352 | * moves the buffer around lineCount lines upwards (negative values move down) |
|---|
| 353 | * @param lineCount the Count of lines to move upwards |
|---|
| 354 | * |
|---|
| 355 | * @todo |
|---|
| 356 | */ |
|---|
| 357 | void Shell::moveBuffer(int lineCount) |
|---|
| 358 | { |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | /** |
|---|
| 362 | * @param lineNumber the n-th line from the bottom |
|---|
| 363 | * @returns the Buffer at Line lineNumber |
|---|
| 364 | */ |
|---|
| 365 | const char* Shell::getBufferLine(unsigned int lineNumber) |
|---|
| 366 | { |
|---|
| 367 | tIterator<char>* charIterator = this->buffer->getIterator(); |
|---|
| 368 | char* charElem = charIterator->firstElement(); |
|---|
| 369 | |
|---|
| 370 | int i = 1; |
|---|
| 371 | while (charElem != NULL) |
|---|
| 372 | { |
|---|
| 373 | if (i++ < lineNumber) |
|---|
| 374 | { |
|---|
| 375 | delete charIterator; |
|---|
| 376 | return charElem; |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | charElem = charIterator->nextElement(); |
|---|
| 380 | } |
|---|
| 381 | delete charIterator; |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | /** |
|---|
| 385 | * deletes the InputLine |
|---|
| 386 | */ |
|---|
| 387 | void Shell::flushInputLine() |
|---|
| 388 | { |
|---|
| 389 | if (likely(this->inputLine != NULL)) |
|---|
| 390 | { |
|---|
| 391 | delete[] this->inputLine; |
|---|
| 392 | } |
|---|
| 393 | this->inputLine = new char[1]; |
|---|
| 394 | *this->inputLine = '\0'; |
|---|
| 395 | this->inputLineText->setText(this->inputLine, true); |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | /** |
|---|
| 399 | * adds one character to the inputLine |
|---|
| 400 | * @param character the character to add to the inputLine |
|---|
| 401 | */ |
|---|
| 402 | void Shell::addCharacter(char character) |
|---|
| 403 | { |
|---|
| 404 | char* addCharLine = new char[strlen(inputLine)+2]; |
|---|
| 405 | |
|---|
| 406 | sprintf(addCharLine, "%s%c", this->inputLine, character); |
|---|
| 407 | delete this->inputLine; |
|---|
| 408 | this->inputLine = addCharLine; |
|---|
| 409 | this->inputLineText->setText(inputLine, true); |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | /** |
|---|
| 413 | * adds multiple Characters to thr inputLine |
|---|
| 414 | * @param characters a '\0' terminated char-array to add to the InputLine |
|---|
| 415 | */ |
|---|
| 416 | void Shell::addCharacters(const char* characters) |
|---|
| 417 | { |
|---|
| 418 | char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1]; |
|---|
| 419 | |
|---|
| 420 | sprintf(addCharLine, "%s%s", this->inputLine, characters); |
|---|
| 421 | delete this->inputLine; |
|---|
| 422 | this->inputLine = addCharLine; |
|---|
| 423 | this->inputLineText->setText(inputLine); |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | /** |
|---|
| 427 | * removes characterCount characters from the InputLine |
|---|
| 428 | * @param characterCount the count of Characters to remove from the input Line |
|---|
| 429 | */ |
|---|
| 430 | void Shell::removeCharacters(unsigned int characterCount) |
|---|
| 431 | { |
|---|
| 432 | if (strlen(this->inputLine) == 0) |
|---|
| 433 | return; |
|---|
| 434 | |
|---|
| 435 | if (characterCount > strlen(this->inputLine)) |
|---|
| 436 | characterCount = strlen(this->inputLine); |
|---|
| 437 | |
|---|
| 438 | char* removeCharLine = new char[strlen(inputLine)-characterCount+1]; |
|---|
| 439 | |
|---|
| 440 | strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount); |
|---|
| 441 | removeCharLine[strlen(inputLine)-characterCount] = '\0'; |
|---|
| 442 | delete this->inputLine; |
|---|
| 443 | this->inputLine = removeCharLine; |
|---|
| 444 | this->inputLineText->setText(inputLine); |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | /** |
|---|
| 448 | * executes the command stored in the inputLine |
|---|
| 449 | * @return true if the command was commited successfully, false otherwise |
|---|
| 450 | */ |
|---|
| 451 | bool Shell::executeCommand() |
|---|
| 452 | { |
|---|
| 453 | this->addBufferLineStatic("Execute Command: %s\n", this->inputLine); |
|---|
| 454 | |
|---|
| 455 | char* newCommand = new char[strlen(this->inputLine)+1]; |
|---|
| 456 | strcpy(newCommand, this->inputLine); |
|---|
| 457 | this->inputHistory->add(newCommand); |
|---|
| 458 | |
|---|
| 459 | if (!strcmp(this->inputLine, "clear")) |
|---|
| 460 | { |
|---|
| 461 | this->clear(); |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | this->flushInputLine(); |
|---|
| 465 | |
|---|
| 466 | return false; |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | void Shell::clear() |
|---|
| 470 | { |
|---|
| 471 | this->flushBuffers(); |
|---|
| 472 | this->addBufferLine("orxonox - shell\n ==================== \n", NULL); |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | /** |
|---|
| 476 | * sets the Repeate-delay and rate |
|---|
| 477 | * @param repeatDelay the Delay it takes, to repeate a key |
|---|
| 478 | * @param repeatRate the rate to repeate a pressed key |
|---|
| 479 | */ |
|---|
| 480 | void Shell::setRepeatDelay(float repeatDelay, float repeatRate) |
|---|
| 481 | { |
|---|
| 482 | this->repeatDelay = repeatDelay; |
|---|
| 483 | this->repeatRate = repeatRate; |
|---|
| 484 | |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | /** |
|---|
| 488 | * listens for some event |
|---|
| 489 | * @param event the Event happened |
|---|
| 490 | */ |
|---|
| 491 | void Shell::process(const Event &event) |
|---|
| 492 | { |
|---|
| 493 | if (event.bPressed) |
|---|
| 494 | { |
|---|
| 495 | PRINTF(4)("Shell received command %s\n", SDLKToKeyname(event.type)); |
|---|
| 496 | if (event.type == SDLK_BACKQUOTE) |
|---|
| 497 | { |
|---|
| 498 | if (EventHandler::getInstance()->getState() == ES_GAME) |
|---|
| 499 | this->activate(); |
|---|
| 500 | else |
|---|
| 501 | this->deactivate(); |
|---|
| 502 | } |
|---|
| 503 | else if (event.type == SDLK_F1) |
|---|
| 504 | this->help(); |
|---|
| 505 | else if (event.type == SDLK_F2) |
|---|
| 506 | this->debug(); |
|---|
| 507 | else if (event.type == SDLK_TAB) |
|---|
| 508 | this->autoComplete(); |
|---|
| 509 | else if (event.type == SDLK_BACKSPACE) |
|---|
| 510 | { |
|---|
| 511 | this->delayed = this->repeatDelay; |
|---|
| 512 | this->pressedKey = SDLK_BACKSPACE; |
|---|
| 513 | this->removeCharacters(1); |
|---|
| 514 | } |
|---|
| 515 | else if (event.type == SDLK_RETURN) |
|---|
| 516 | this->executeCommand(); |
|---|
| 517 | /* |
|---|
| 518 | else if (event.type == SDLK_UP) |
|---|
| 519 | { |
|---|
| 520 | // this->flushInputLine(); |
|---|
| 521 | tIterator<char>* iterator = this->commandList->getIterator(); |
|---|
| 522 | char* command = iterator->lastElement(); |
|---|
| 523 | while (command) |
|---|
| 524 | { |
|---|
| 525 | if (!strcmp (command, inputLine)) |
|---|
| 526 | { |
|---|
| 527 | inputLine = iterator->prevElement(); |
|---|
| 528 | return; |
|---|
| 529 | } |
|---|
| 530 | command = iterator->prevElement(); |
|---|
| 531 | } |
|---|
| 532 | inputLine = iterator->lastElement(); |
|---|
| 533 | } |
|---|
| 534 | */ |
|---|
| 535 | else if (likely(event.type < 127)) |
|---|
| 536 | { |
|---|
| 537 | Uint8 *keystate = SDL_GetKeyState(NULL); |
|---|
| 538 | this->delayed = this->repeatDelay; |
|---|
| 539 | if (unlikely( keystate[SDLK_LSHIFT] || keystate[SDLK_RSHIFT] )) |
|---|
| 540 | { |
|---|
| 541 | this->pressedKey = event.type-32; |
|---|
| 542 | this->addCharacter(event.type-32); |
|---|
| 543 | } |
|---|
| 544 | else |
|---|
| 545 | { |
|---|
| 546 | this->pressedKey = event.type; |
|---|
| 547 | this->addCharacter(event.type); |
|---|
| 548 | } |
|---|
| 549 | } |
|---|
| 550 | } |
|---|
| 551 | else // if(!event.bPressed) |
|---|
| 552 | { |
|---|
| 553 | if (this->pressedKey == event.type || (this->pressedKey == event.type - 32)) |
|---|
| 554 | { |
|---|
| 555 | this->pressedKey = SDLK_FIRST; |
|---|
| 556 | this->delayed = 0.0; |
|---|
| 557 | } |
|---|
| 558 | } |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | /** |
|---|
| 562 | * ticks the Shell for dt Seconds |
|---|
| 563 | * @param dt the elapsed time since the last tick(); |
|---|
| 564 | */ |
|---|
| 565 | void Shell::tick(float dt) |
|---|
| 566 | { |
|---|
| 567 | if (this->delayed > 0.0) |
|---|
| 568 | this->delayed -= dt; |
|---|
| 569 | else if (this->pressedKey != SDLK_FIRST ) |
|---|
| 570 | { |
|---|
| 571 | this->delayed = this->repeatRate; |
|---|
| 572 | if (this->pressedKey == SDLK_BACKSPACE) |
|---|
| 573 | this->removeCharacters(1); |
|---|
| 574 | else if (pressedKey < 127) |
|---|
| 575 | this->addCharacter(this->pressedKey); |
|---|
| 576 | } |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | /** |
|---|
| 580 | * displays the Shell |
|---|
| 581 | */ |
|---|
| 582 | void Shell::draw() const |
|---|
| 583 | { |
|---|
| 584 | glPushMatrix(); |
|---|
| 585 | // transform for alignment. |
|---|
| 586 | // setting the Blending effects |
|---|
| 587 | |
|---|
| 588 | glColor4f(0.0f, 0.0f, 0.8f, .4); |
|---|
| 589 | glEnable(GL_BLEND); |
|---|
| 590 | glDisable(GL_TEXTURE_2D); |
|---|
| 591 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
|---|
| 592 | |
|---|
| 593 | // glBindTexture(GL_TEXTURE_2D, this->texture); |
|---|
| 594 | glBegin(GL_QUADS); |
|---|
| 595 | |
|---|
| 596 | // glTexCoord2f(this->texCoord.minU, this->texCoord.minV); |
|---|
| 597 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
|---|
| 598 | |
|---|
| 599 | // glTexCoord2f(this->texCoord.maxU, this->texCoord.minV); |
|---|
| 600 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
|---|
| 601 | |
|---|
| 602 | // glTexCoord2f(this->texCoord.maxU, this->texCoord.maxV); |
|---|
| 603 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
|---|
| 604 | |
|---|
| 605 | // glTexCoord2f(this->texCoord.minU, this->texCoord.maxV); |
|---|
| 606 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
|---|
| 607 | |
|---|
| 608 | glEnd(); |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | |
|---|
| 612 | /** |
|---|
| 613 | * autocompletes the Shell's inputLine |
|---|
| 614 | * @returns true, if a result was found, false otherwise |
|---|
| 615 | * |
|---|
| 616 | * @todo implement it!! |
|---|
| 617 | */ |
|---|
| 618 | bool Shell::autoComplete() |
|---|
| 619 | { |
|---|
| 620 | //PRINTF(3)("AutoCompletion not implemented yet\n"); |
|---|
| 621 | |
|---|
| 622 | char* completionLine = new char[strlen(inputLine)+1]; |
|---|
| 623 | strcpy(completionLine, this->inputLine); |
|---|
| 624 | |
|---|
| 625 | char* commandBegin = strrchr(completionLine, ' '); |
|---|
| 626 | if (commandBegin == NULL) |
|---|
| 627 | commandBegin = completionLine; |
|---|
| 628 | else |
|---|
| 629 | { |
|---|
| 630 | if(commandBegin >= completionLine + strlen(completionLine)) |
|---|
| 631 | commandBegin = completionLine + strlen(completionLine); |
|---|
| 632 | else |
|---|
| 633 | commandBegin++; |
|---|
| 634 | } |
|---|
| 635 | |
|---|
| 636 | char* objectStart; |
|---|
| 637 | if (objectStart = strstr(commandBegin, "::")) |
|---|
| 638 | { |
|---|
| 639 | char* classIdentity = new char[objectStart - commandBegin +1]; |
|---|
| 640 | strncpy(classIdentity, commandBegin, objectStart - commandBegin); |
|---|
| 641 | classIdentity[objectStart - commandBegin] = '\0'; |
|---|
| 642 | this->objectComplete(objectStart+2, ClassList::StringToID(classIdentity)); |
|---|
| 643 | delete[] classIdentity; |
|---|
| 644 | } |
|---|
| 645 | else |
|---|
| 646 | this->classComplete(commandBegin); |
|---|
| 647 | |
|---|
| 648 | delete[] completionLine; |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | /** |
|---|
| 652 | * autocompletes a className |
|---|
| 653 | * @param classBegin the Beginning of a String to autoComplete |
|---|
| 654 | * @return true on success, false otherwise |
|---|
| 655 | */ |
|---|
| 656 | bool Shell::classComplete(const char* classBegin) |
|---|
| 657 | { |
|---|
| 658 | if (unlikely(classBegin == NULL)) |
|---|
| 659 | return false; |
|---|
| 660 | const tList<const char>* clList = ClassList::getClassList(); |
|---|
| 661 | if (clList != NULL) |
|---|
| 662 | { |
|---|
| 663 | const tList<const char>* classList = this->createCompleteList(clList, classBegin); |
|---|
| 664 | if (classList != NULL) |
|---|
| 665 | this->generalComplete(classList, classBegin, "%s::", "::"); |
|---|
| 666 | else |
|---|
| 667 | return false; |
|---|
| 668 | } |
|---|
| 669 | else |
|---|
| 670 | return false; |
|---|
| 671 | return true; |
|---|
| 672 | } |
|---|
| 673 | |
|---|
| 674 | /** |
|---|
| 675 | * autocompletes an ObjectName |
|---|
| 676 | * @param objectBegin the beginning string of a Object |
|---|
| 677 | * @param classID the ID of the Class to search for. |
|---|
| 678 | * @return true on success, false otherwise |
|---|
| 679 | */ |
|---|
| 680 | bool Shell::objectComplete(const char* objectBegin, long classID) |
|---|
| 681 | { |
|---|
| 682 | printf("%s\n", objectBegin); |
|---|
| 683 | |
|---|
| 684 | if (unlikely(objectBegin == NULL)) |
|---|
| 685 | return false; |
|---|
| 686 | tList<BaseObject>* boList = ClassList::getList(classID); |
|---|
| 687 | if (boList != NULL) |
|---|
| 688 | { |
|---|
| 689 | printf("\n", boList->firstElement()->getName()); |
|---|
| 690 | const tList<const char>* objectList = this->createCompleteList(boList, objectBegin); |
|---|
| 691 | if (objectList != NULL) |
|---|
| 692 | this->generalComplete(objectList, objectBegin, "%s"); |
|---|
| 693 | else |
|---|
| 694 | return false; |
|---|
| 695 | } |
|---|
| 696 | else |
|---|
| 697 | return false; |
|---|
| 698 | return true; |
|---|
| 699 | } |
|---|
| 700 | |
|---|
| 701 | bool Shell::functionComplete(const char* functionBegin) |
|---|
| 702 | { |
|---|
| 703 | } |
|---|
| 704 | |
|---|
| 705 | /** |
|---|
| 706 | * completes the inputline on grounds of an inputList |
|---|
| 707 | * @param stringList the List to parse through |
|---|
| 708 | * @param begin the String to search in the inputList, and to extend with it. |
|---|
| 709 | * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::" |
|---|
| 710 | * @param addBack what should be added at the end of the completion |
|---|
| 711 | * @param addFront what should be added to the front of one finished completion |
|---|
| 712 | * @return true if ok, false otherwise |
|---|
| 713 | */ |
|---|
| 714 | bool Shell::generalComplete(const tList<const char>* stringList, const char* begin, const char* displayAs, const char* addBack, const char* addFront) |
|---|
| 715 | { |
|---|
| 716 | if (stringList->getSize() == 0) |
|---|
| 717 | return false; |
|---|
| 718 | |
|---|
| 719 | const char* addString = stringList->firstElement(); |
|---|
| 720 | unsigned int addLength = 0; |
|---|
| 721 | unsigned int inputLenght = strlen(begin); |
|---|
| 722 | |
|---|
| 723 | if (addString != NULL) |
|---|
| 724 | addLength = strlen(addString); |
|---|
| 725 | tIterator<const char>* charIterator = stringList->getIterator(); |
|---|
| 726 | const char* charElem = charIterator->firstElement(); |
|---|
| 727 | while (charElem != NULL) |
|---|
| 728 | { |
|---|
| 729 | PRINTF(0)(displayAs, charElem); |
|---|
| 730 | for (unsigned int i = inputLenght; i < addLength; i++) |
|---|
| 731 | if (addString[i] != charElem[i]) |
|---|
| 732 | { |
|---|
| 733 | addLength = i; |
|---|
| 734 | break; |
|---|
| 735 | } |
|---|
| 736 | charElem = charIterator->nextElement(); |
|---|
| 737 | } |
|---|
| 738 | delete charIterator; |
|---|
| 739 | |
|---|
| 740 | if (addLength >= inputLenght) |
|---|
| 741 | { |
|---|
| 742 | char* adder = new char[addLength+1]; |
|---|
| 743 | strncpy(adder, addString, addLength); |
|---|
| 744 | adder[addLength] = '\0'; |
|---|
| 745 | this->removeCharacters(inputLenght); |
|---|
| 746 | this->addCharacters(adder); |
|---|
| 747 | if (addBack != NULL && stringList->getSize() == 1) |
|---|
| 748 | this->addCharacters("::"); |
|---|
| 749 | delete[] adder; |
|---|
| 750 | } |
|---|
| 751 | return true; |
|---|
| 752 | } |
|---|
| 753 | |
|---|
| 754 | /** |
|---|
| 755 | * searches for classes, which beginn with classNameBegin |
|---|
| 756 | * @param inputList the List to parse through |
|---|
| 757 | * @param classNameBegin the beginning string |
|---|
| 758 | * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards, |
|---|
| 759 | * !! The strings MUST NOT be deleted !! |
|---|
| 760 | */ |
|---|
| 761 | const tList<const char>* Shell::createCompleteList(const tList<const char>* inputList, const char* classNameBegin) |
|---|
| 762 | { |
|---|
| 763 | if (inputList == NULL || classNameBegin == NULL) |
|---|
| 764 | return NULL; |
|---|
| 765 | unsigned int searchLength = strlen(classNameBegin); |
|---|
| 766 | if (this->completionList != NULL) |
|---|
| 767 | delete this->completionList; |
|---|
| 768 | this->completionList = new tList<const char>; |
|---|
| 769 | |
|---|
| 770 | // tList<const char>* classList = ClassList::getClassList(); |
|---|
| 771 | |
|---|
| 772 | tIterator<const char>* iterator = inputList->getIterator(); |
|---|
| 773 | const char* enumString = iterator->firstElement(); |
|---|
| 774 | while (enumString != NULL) |
|---|
| 775 | { |
|---|
| 776 | if (strlen(enumString)>searchLength+1 && |
|---|
| 777 | !strncasecmp(enumString, classNameBegin, searchLength)) |
|---|
| 778 | { |
|---|
| 779 | this->completionList->add(enumString); |
|---|
| 780 | } |
|---|
| 781 | enumString = iterator->nextElement(); |
|---|
| 782 | } |
|---|
| 783 | delete iterator; |
|---|
| 784 | |
|---|
| 785 | return this->completionList; |
|---|
| 786 | } |
|---|
| 787 | |
|---|
| 788 | /** |
|---|
| 789 | * searches for classes, which beginn with classNameBegin |
|---|
| 790 | * @param inputList the List to parse through |
|---|
| 791 | * @param classNameBegin the beginning string |
|---|
| 792 | * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards, |
|---|
| 793 | * !! The strings MUST NOT be deleted !! |
|---|
| 794 | */ |
|---|
| 795 | const tList<const char>* Shell::createCompleteList(const tList<BaseObject>* inputList, const char* classNameBegin) |
|---|
| 796 | { |
|---|
| 797 | if (inputList == NULL || classNameBegin == NULL) |
|---|
| 798 | return NULL; |
|---|
| 799 | unsigned int searchLength = strlen(classNameBegin); |
|---|
| 800 | if (this->completionList != NULL) |
|---|
| 801 | delete this->completionList; |
|---|
| 802 | this->completionList = new tList<const char>; |
|---|
| 803 | |
|---|
| 804 | tIterator<BaseObject>* iterator = inputList->getIterator(); |
|---|
| 805 | BaseObject* enumBO = iterator->firstElement(); |
|---|
| 806 | while (enumBO != NULL) |
|---|
| 807 | { |
|---|
| 808 | if (enumBO->getName() != NULL && |
|---|
| 809 | strlen(enumBO->getName())>searchLength+1 && |
|---|
| 810 | !strncasecmp(enumBO->getName(), classNameBegin, searchLength)) |
|---|
| 811 | { |
|---|
| 812 | this->completionList->add(enumBO->getName()); |
|---|
| 813 | } |
|---|
| 814 | enumBO = iterator->nextElement(); |
|---|
| 815 | } |
|---|
| 816 | delete iterator; |
|---|
| 817 | |
|---|
| 818 | return this->completionList; |
|---|
| 819 | } |
|---|
| 820 | |
|---|
| 821 | void Shell::help() const |
|---|
| 822 | { |
|---|
| 823 | PRINT(0)("Help for the most important Shell-commands\n"); |
|---|
| 824 | PRINT(0)("F1 - HELP; F2 - DEBUG; ` - open/close shell\n"); |
|---|
| 825 | PRINT(0)("input order:\n"); |
|---|
| 826 | PRINT(0)("ClassName::objectName function [parameter1, [parameter2 ...]] or\n"); |
|---|
| 827 | PRINT(0)("Command [parameter]\n"); |
|---|
| 828 | } |
|---|
| 829 | |
|---|
| 830 | |
|---|
| 831 | /////////////////////// |
|---|
| 832 | // HELPER FUNCTIONS // |
|---|
| 833 | /////////////////////// |
|---|
| 834 | Vector Shell::calculateLinePosition(unsigned int lineNumber) |
|---|
| 835 | { |
|---|
| 836 | return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0); |
|---|
| 837 | } |
|---|
| 838 | |
|---|
| 839 | |
|---|
| 840 | |
|---|
| 841 | /** |
|---|
| 842 | * displays some nice output from the Shell |
|---|
| 843 | */ |
|---|
| 844 | void Shell::debug() const |
|---|
| 845 | { |
|---|
| 846 | PRINT(3)("Debugging output to console (not this shell)\n"); |
|---|
| 847 | |
|---|
| 848 | if (this->pressedKey != SDLK_FIRST) |
|---|
| 849 | printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay); |
|---|
| 850 | |
|---|
| 851 | |
|---|
| 852 | char* tmpChar = this->bufferIterator->firstElement(); |
|---|
| 853 | while(tmpChar != NULL) |
|---|
| 854 | { |
|---|
| 855 | printf(tmpChar); |
|---|
| 856 | tmpChar = this->bufferIterator->nextElement(); |
|---|
| 857 | } |
|---|
| 858 | } |
|---|