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