| 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 |  | 
|---|
| 20 | #include "text_engine.h" | 
|---|
| 21 | #include "list.h" | 
|---|
| 22 | #include "graphics_engine.h" | 
|---|
| 23 | #include "event_handler.h" | 
|---|
| 24 |  | 
|---|
| 25 | #include "debug.h" | 
|---|
| 26 | #include <stdarg.h> | 
|---|
| 27 | #include <stdio.h> | 
|---|
| 28 |  | 
|---|
| 29 | using namespace std; | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | /** | 
|---|
| 33 |  * standard constructor | 
|---|
| 34 |  */ | 
|---|
| 35 | Shell::Shell () | 
|---|
| 36 | { | 
|---|
| 37 |   this->setClassID(CL_SHELL, "Shell"); | 
|---|
| 38 |   this->setName("Shell"); | 
|---|
| 39 |  | 
|---|
| 40 |   this->buffer = new tList<char>; | 
|---|
| 41 |  | 
|---|
| 42 |   this->textSize = 10; | 
|---|
| 43 |  | 
|---|
| 44 |   //this->bufferSize = 0; | 
|---|
| 45 |   this->bufferText = NULL; | 
|---|
| 46 |   this->setBufferSize(100); | 
|---|
| 47 |   this->setBufferDisplaySize(10); | 
|---|
| 48 |   this->setAbsCoor2D(3, GraphicsEngine::getInstance()->getResolutionY()); | 
|---|
| 49 |   this->delayed = 0; | 
|---|
| 50 |   this->setRepeatDelay(.3, .05); | 
|---|
| 51 |   this->pressedKey = SDLK_FIRST; | 
|---|
| 52 |  | 
|---|
| 53 |   this->inputLineText = TextEngine::getInstance()->createText("fonts/earth.ttf", 10, TEXT_DYNAMIC, 255, 0, 0); | 
|---|
| 54 |   this->inputLineText->setAlignment(TEXT_ALIGN_LEFT); | 
|---|
| 55 |   this->inputLineText->setText(NULL); | 
|---|
| 56 |   this->inputLine = new char[1]; | 
|---|
| 57 |   this->inputLine[0] = '\0'; | 
|---|
| 58 |   this->inputLineText->setParent2D(this); | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 |   EventHandler* evh = EventHandler::getInstance(); | 
|---|
| 62 |   evh->subscribe(this, ES_ALL, SDLK_BACKQUOTE); | 
|---|
| 63 |   for (int i = 1; i < SDLK_F15; i++) | 
|---|
| 64 |     evh->subscribe(this, ES_SHELL, i); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | Shell* Shell::singletonRef = NULL; | 
|---|
| 68 |  | 
|---|
| 69 | /** | 
|---|
| 70 |  * standard deconstructor | 
|---|
| 71 |  */ | 
|---|
| 72 | Shell::~Shell () | 
|---|
| 73 | { | 
|---|
| 74 |   // delete what has to be deleted here | 
|---|
| 75 |   for (int i = 0; i < this->bufferDisplaySize; i++) | 
|---|
| 76 |     delete this->bufferText[i]; | 
|---|
| 77 |   delete this->bufferText; | 
|---|
| 78 |  | 
|---|
| 79 |   delete this->inputLineText; | 
|---|
| 80 |   delete this->inputLine; | 
|---|
| 81 |  | 
|---|
| 82 |   Shell::singletonRef = NULL; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | /** | 
|---|
| 86 |  * sets The count of Lines to display in the buffer. | 
|---|
| 87 |  * @param bufferDisplaySize the count of lines to display in the Shell-Buffer. | 
|---|
| 88 |  */ | 
|---|
| 89 | void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize) | 
|---|
| 90 | { | 
|---|
| 91 |   if (this->bufferText != NULL) | 
|---|
| 92 |   { | 
|---|
| 93 |     for (unsigned int i = 0; i < this->bufferDisplaySize; i++) | 
|---|
| 94 |       delete this->bufferText[i]; | 
|---|
| 95 |     delete this->bufferText; | 
|---|
| 96 |   } | 
|---|
| 97 |  | 
|---|
| 98 |   this->bufferText = new Text*[bufferDisplaySize]; | 
|---|
| 99 |   for (unsigned int i = 0; i < bufferDisplaySize; i++) | 
|---|
| 100 |   { | 
|---|
| 101 |     this->bufferText[i] = TextEngine::getInstance()->createText("fonts/earth.ttf", this->textSize, TEXT_DYNAMIC, 255, 0, 0); | 
|---|
| 102 |     this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT); | 
|---|
| 103 |     this->bufferText[i]->setRelCoor2D(5, 12+12*i); | 
|---|
| 104 |     this->bufferText[i]->setText(NULL); | 
|---|
| 105 |     this->bufferText[i]->setParent2D(this); | 
|---|
| 106 |   } | 
|---|
| 107 |  | 
|---|
| 108 |  | 
|---|
| 109 |   this->bufferDisplaySize = bufferDisplaySize; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | /** | 
|---|
| 113 |  * deletes all the Buffers | 
|---|
| 114 |  */ | 
|---|
| 115 | void Shell::flushBuffers() | 
|---|
| 116 | { | 
|---|
| 117 |   // remove all chars from the BufferTexts. | 
|---|
| 118 |   if (this->bufferText) | 
|---|
| 119 |     for (int i; i < this->bufferDisplaySize; i++) | 
|---|
| 120 |     { | 
|---|
| 121 |       this->bufferText[i]->setText(NULL); | 
|---|
| 122 |     } | 
|---|
| 123 |  | 
|---|
| 124 |  | 
|---|
| 125 |   // delete all the Chars in the Buffers | 
|---|
| 126 |   tIterator<char>* charIterator = this->buffer->getIterator(); | 
|---|
| 127 |   char* charElem = charIterator->nextElement(); | 
|---|
| 128 |  | 
|---|
| 129 |   while (charElem != NULL) | 
|---|
| 130 |   { | 
|---|
| 131 |     delete charElem; | 
|---|
| 132 |  | 
|---|
| 133 |     charElem = charIterator->nextElement(); | 
|---|
| 134 |   } | 
|---|
| 135 |   delete charIterator; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | /** | 
|---|
| 139 |  * adds a new Line to the List of Buffers | 
|---|
| 140 |  * @param line the Line as in the first argument in printf | 
|---|
| 141 |  * @param args the arguments as a va_list | 
|---|
| 142 |  * | 
|---|
| 143 |  * @todo optimize | 
|---|
| 144 |  */ | 
|---|
| 145 | bool Shell::addBufferLineStatic(const char* line, ...) | 
|---|
| 146 | { | 
|---|
| 147 |   va_list arguments; | 
|---|
| 148 |   va_start(arguments, line); | 
|---|
| 149 |  | 
|---|
| 150 | #if DEBUG < 3 | 
|---|
| 151 |   if (Shell::singletonRef == NULL) | 
|---|
| 152 | #endif | 
|---|
| 153 |  | 
|---|
| 154 |   vprintf(line, arguments); | 
|---|
| 155 | #if DEBUG < 3 | 
|---|
| 156 |   else | 
|---|
| 157 | #else | 
|---|
| 158 |   if (Shell::singletonRef != NULL) | 
|---|
| 159 | #endif | 
|---|
| 160 |     Shell::singletonRef->addBufferLine(line, arguments); | 
|---|
| 161 |   return true; | 
|---|
| 162 | } | 
|---|
| 163 | int curr = 0; | 
|---|
| 164 |  | 
|---|
| 165 | /** | 
|---|
| 166 |  * add a Line to the List of Buffers | 
|---|
| 167 |  * @param line | 
|---|
| 168 |  * @param arguments | 
|---|
| 169 |  * | 
|---|
| 170 |  * This function Adds one line to the buffer. | 
|---|
| 171 |  * and displays the line as the First Line of the display-buffer | 
|---|
| 172 |  */ | 
|---|
| 173 | void Shell::addBufferLine(const char* line, va_list arguments) | 
|---|
| 174 | { | 
|---|
| 175 |    vsprintf(this->bufferArray, line, arguments); | 
|---|
| 176 |  | 
|---|
| 177 |    char* newLine = new char[strlen(this->bufferArray)+1]; | 
|---|
| 178 |    strcpy(newLine, this->bufferArray); | 
|---|
| 179 |  | 
|---|
| 180 |    this->buffer->add(newLine); | 
|---|
| 181 |  | 
|---|
| 182 |    if (this->buffer->getSize() > this->bufferSize) | 
|---|
| 183 |    { | 
|---|
| 184 |      delete this->buffer->firstElement(); | 
|---|
| 185 |      this->buffer->remove(this->buffer->firstElement()); | 
|---|
| 186 |    } | 
|---|
| 187 |  | 
|---|
| 188 |    if (likely(bufferText != NULL)) | 
|---|
| 189 |    { | 
|---|
| 190 |      Text* moveText = this->bufferText[this->bufferDisplaySize-1]; | 
|---|
| 191 |      for (int i = this->bufferDisplaySize-1; i > 0; i--) | 
|---|
| 192 |      { | 
|---|
| 193 |        this->bufferText[i] = this->bufferText[i-1]; | 
|---|
| 194 |      } | 
|---|
| 195 |      this->bufferText[0] = moveText; | 
|---|
| 196 |    } | 
|---|
| 197 |    this->bufferText[0]->setText(newLine); | 
|---|
| 198 |    // this->bufferText-> | 
|---|
| 199 | //  this->inputLineText->setText(newLine); | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | /** | 
|---|
| 203 |  * moves the buffer around lineCount lines upwards (negative values move down) | 
|---|
| 204 |  * @param lineCount the Count of lines to move upwards | 
|---|
| 205 |  * | 
|---|
| 206 |  * @todo | 
|---|
| 207 |  */ | 
|---|
| 208 | void Shell::moveBuffer(int lineCount) | 
|---|
| 209 | { | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | /** | 
|---|
| 213 |  * @param lineNumber the n-th line from the bottom | 
|---|
| 214 |  * @returns the Buffer at Line lineNumber | 
|---|
| 215 |  */ | 
|---|
| 216 | const char* Shell::getBufferLine(unsigned int lineNumber) | 
|---|
| 217 | { | 
|---|
| 218 |   tIterator<char>* charIterator = this->buffer->getIterator(); | 
|---|
| 219 |   char* charElem = charIterator->nextElement(); | 
|---|
| 220 |  | 
|---|
| 221 |   int i = 1; | 
|---|
| 222 |   while (charElem != NULL) | 
|---|
| 223 |   { | 
|---|
| 224 |     if (i++ < lineNumber) | 
|---|
| 225 |     { | 
|---|
| 226 |       delete charIterator; | 
|---|
| 227 |       return charElem; | 
|---|
| 228 |     } | 
|---|
| 229 |  | 
|---|
| 230 |     charElem = charIterator->nextElement(); | 
|---|
| 231 |   } | 
|---|
| 232 |   delete charIterator; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 |  | 
|---|
| 236 | /** | 
|---|
| 237 |  * deletes the InputLine | 
|---|
| 238 |  */ | 
|---|
| 239 | void Shell::flushInputLine() | 
|---|
| 240 | { | 
|---|
| 241 |   if (likely(this->inputLine != NULL)) | 
|---|
| 242 |   { | 
|---|
| 243 |     delete [] this->inputLine; | 
|---|
| 244 |   } | 
|---|
| 245 |   this->inputLine = new char[1]; | 
|---|
| 246 |   *this->inputLine = '\0'; | 
|---|
| 247 |  | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | /** | 
|---|
| 251 |  * adds one character to the inputLine | 
|---|
| 252 |  * @param character the character to add to the inputLine | 
|---|
| 253 |  */ | 
|---|
| 254 | void Shell::addCharacter(char character) | 
|---|
| 255 | { | 
|---|
| 256 |   char* addCharLine = new char[strlen(inputLine)+2]; | 
|---|
| 257 |  | 
|---|
| 258 |   sprintf(addCharLine, "%s%c", this->inputLine, character); | 
|---|
| 259 |   delete this->inputLine; | 
|---|
| 260 |   this->inputLine = addCharLine; | 
|---|
| 261 |   this->inputLineText->setText(inputLine); | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | /** | 
|---|
| 265 |  * adds multiple Characters to thr inputLine | 
|---|
| 266 |  * @param characters a '\0' terminated char-array to add to the InputLine | 
|---|
| 267 |  */ | 
|---|
| 268 | void Shell::addCharacters(const char* characters) | 
|---|
| 269 | { | 
|---|
| 270 |   char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1]; | 
|---|
| 271 |  | 
|---|
| 272 |   sprintf(addCharLine, "%s%s", this->inputLine, characters); | 
|---|
| 273 |   delete this->inputLine; | 
|---|
| 274 |   this->inputLine = addCharLine; | 
|---|
| 275 |   this->inputLineText->setText(inputLine); | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | /** | 
|---|
| 279 |  * removes characterCount characters from the InputLine | 
|---|
| 280 |  * @param characterCount the count of Characters to remove from the input Line | 
|---|
| 281 |  */ | 
|---|
| 282 | void Shell::removeCharacters(unsigned int characterCount) | 
|---|
| 283 | { | 
|---|
| 284 |   if (strlen(this->inputLine) == 0) | 
|---|
| 285 |     return; | 
|---|
| 286 |  | 
|---|
| 287 |   if (characterCount > strlen(this->inputLine)) | 
|---|
| 288 |     characterCount = strlen(this->inputLine); | 
|---|
| 289 |  | 
|---|
| 290 |   char* removeCharLine = new char[strlen(inputLine)-characterCount+1]; | 
|---|
| 291 |  | 
|---|
| 292 |   strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount); | 
|---|
| 293 |   removeCharLine[strlen(inputLine)-characterCount] = '\0'; | 
|---|
| 294 |   delete this->inputLine; | 
|---|
| 295 |   this->inputLine = removeCharLine; | 
|---|
| 296 |   this->inputLineText->setText(inputLine); | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 | /** | 
|---|
| 300 |  * executes the command stored in the inputLine | 
|---|
| 301 |  * @return true if the command was commited successfully, false otherwise | 
|---|
| 302 |  */ | 
|---|
| 303 | bool Shell::executeCommand() | 
|---|
| 304 | { | 
|---|
| 305 |  | 
|---|
| 306 |   this->addBufferLineStatic("Execute Command: %s\n", this->inputLine); | 
|---|
| 307 |   delete this->inputLine; | 
|---|
| 308 |   this->inputLine = new char[1]; | 
|---|
| 309 |   this->inputLine[0]='\0'; | 
|---|
| 310 |   this->inputLineText->setText(this->inputLine); | 
|---|
| 311 |   return false; | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | /** | 
|---|
| 315 |  * sets the Repeate-delay and rate | 
|---|
| 316 |  * @param repeatDelay the Delay it takes, to repeate a key | 
|---|
| 317 |  * @param repeatRate the rate to repeate a pressed key | 
|---|
| 318 |  */ | 
|---|
| 319 | void Shell::setRepeatDelay(float repeatDelay, float repeatRate) | 
|---|
| 320 | { | 
|---|
| 321 |   this->repeatDelay = repeatDelay; | 
|---|
| 322 |   this->repeatRate = repeatRate; | 
|---|
| 323 |  | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 |  | 
|---|
| 327 | #include "key_names.h" | 
|---|
| 328 | /** | 
|---|
| 329 |  * listens for some event | 
|---|
| 330 |  * @param event the Event happened | 
|---|
| 331 |  */ | 
|---|
| 332 | void Shell::process(const Event &event) | 
|---|
| 333 | { | 
|---|
| 334 |   if (event.bPressed) | 
|---|
| 335 |   { | 
|---|
| 336 |     PRINTF(4)("Shell received command %s\n", SDLKToKeyname(event.type)); | 
|---|
| 337 |     if (event.type == SDLK_BACKQUOTE) | 
|---|
| 338 |     { | 
|---|
| 339 |       if (EventHandler::getInstance()->getState() == ES_GAME) | 
|---|
| 340 |       { | 
|---|
| 341 |         EventHandler::getInstance()->setState(ES_SHELL); | 
|---|
| 342 |         this->setRelCoorSoft2D(0, GraphicsEngine::getInstance()->getResolutionY()-150, 1, 5); | 
|---|
| 343 |       } | 
|---|
| 344 |  | 
|---|
| 345 |       else | 
|---|
| 346 |       { | 
|---|
| 347 |         EventHandler::getInstance()->setState(ES_GAME); | 
|---|
| 348 |         this->setRelCoorSoft2D(0, GraphicsEngine::getInstance()->getResolutionY()+10, 1, 5); | 
|---|
| 349 |       } | 
|---|
| 350 |     } | 
|---|
| 351 |     else if (event.type == SDLK_TAB) | 
|---|
| 352 |       this->autoComplete(); | 
|---|
| 353 |     else if (event.type == SDLK_BACKSPACE) | 
|---|
| 354 |     { | 
|---|
| 355 |       this->delayed = this->repeatDelay; | 
|---|
| 356 |       this->pressedKey = SDLK_BACKSPACE; | 
|---|
| 357 |       this->removeCharacters(1); | 
|---|
| 358 |     } | 
|---|
| 359 |     else if (event.type == SDLK_RETURN) | 
|---|
| 360 |       this->executeCommand(); | 
|---|
| 361 |     else if (likely(event.type < 127)) | 
|---|
| 362 |     { | 
|---|
| 363 |       this->delayed = this->repeatDelay; | 
|---|
| 364 |       this->pressedKey = event.type; | 
|---|
| 365 |       this->addCharacter(event.type); | 
|---|
| 366 |     } | 
|---|
| 367 |   } | 
|---|
| 368 |   else // if(!event.bPressed) | 
|---|
| 369 |   { | 
|---|
| 370 |     if (this->pressedKey == event.type) | 
|---|
| 371 |       this->pressedKey = SDLK_FIRST; | 
|---|
| 372 |     this->delayed = 0.0; | 
|---|
| 373 |   } | 
|---|
| 374 | } | 
|---|
| 375 |  | 
|---|
| 376 | /** | 
|---|
| 377 |  * ticks the Shell for dt Seconds | 
|---|
| 378 |  * @param dt the elapsed time since the last tick(); | 
|---|
| 379 |  */ | 
|---|
| 380 | void Shell::tick(float dt) | 
|---|
| 381 | { | 
|---|
| 382 |   if (this->delayed > 0.0) | 
|---|
| 383 |     this->delayed -= dt; | 
|---|
| 384 |   else if (this->pressedKey != SDLK_FIRST ) | 
|---|
| 385 |   { | 
|---|
| 386 |     this->delayed = this->repeatRate; | 
|---|
| 387 |     if (this->pressedKey == SDLK_BACKSPACE) | 
|---|
| 388 |       this->removeCharacters(1); | 
|---|
| 389 |     else if (pressedKey < 127) | 
|---|
| 390 |       this->addCharacter(this->pressedKey); | 
|---|
| 391 |   } | 
|---|
| 392 | } | 
|---|
| 393 |  | 
|---|
| 394 | /** | 
|---|
| 395 |  * displays the Shell | 
|---|
| 396 |  */ | 
|---|
| 397 | void Shell::draw() const | 
|---|
| 398 | { | 
|---|
| 399 | } | 
|---|
| 400 |  | 
|---|
| 401 |  | 
|---|
| 402 | /** | 
|---|
| 403 |  * autocompletes the Shell's inputLine | 
|---|
| 404 |  * @returns true, if a result was found, false otherwise | 
|---|
| 405 |  */ | 
|---|
| 406 | bool Shell::autoComplete() | 
|---|
| 407 | { | 
|---|
| 408 |   PRINTF(3)("AutoCompletion not implemented yet\n"); | 
|---|
| 409 | } | 
|---|
| 410 |  | 
|---|
| 411 | /** | 
|---|
| 412 |  * displays some nice output from the Shell | 
|---|
| 413 |  */ | 
|---|
| 414 | void Shell::debug() const | 
|---|
| 415 | { | 
|---|
| 416 |   if (this->pressedKey != SDLK_FIRST) | 
|---|
| 417 |     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay); | 
|---|
| 418 |  | 
|---|
| 419 | } | 
|---|