Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5129 in orxonox.OLD for trunk/src/util/shell_command.cc


Ignore:
Timestamp:
Aug 25, 2005, 10:52:11 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: minor extension-addition to the Shell.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/util/shell_command.cc

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