Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell.cc @ 5160

Last change on this file since 5160 was 5160, checked in by bensch, 20 years ago

orxonox/trunk: shell is now a lib for its own

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