Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/shell.cc @ 5095

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

orxonox/trunk: auto-repeat-function

File size: 8.9 KB
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[5068]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[5068]18#include "shell.h"
[1853]19
[5072]20#include "text_engine.h"
21#include "list.h"
[5093]22#include "graphics_engine.h"
23#include "event_handler.h"
[5072]24
[5093]25#include "debug.h"
[5075]26#include <stdarg.h>
27#include <stdio.h>
28
[1856]29using namespace std;
[1853]30
[1856]31
[3245]32/**
[4838]33 * standard constructor
[5068]34 */
35Shell::Shell ()
[3365]36{
[5072]37  this->setClassID(CL_SHELL, "Shell");
38  this->setName("Shell");
39
40  this->buffer = new tList<char>;
41
[5080]42  this->textSize = 10;
43
[5074]44  //this->bufferSize = 0;
[5080]45  this->bufferText = NULL;
[5072]46  this->setBufferSize(100);
[5083]47  this->setBufferDisplaySize(10);
[5095]48  this->setAbsCoor2D(3, GraphicsEngine::getInstance()->getResolutionY());
49  this->repeatDelay = .15;
50  this->delayed = 0;
51  this->pressedKey = SDLK_FIRST;
[5074]52
[5080]53  this->inputLineText = TextEngine::getInstance()->createText("fonts/earth.ttf", 10, TEXT_DYNAMIC, 255, 0, 0);
[5095]54  this->inputLineText->setAlignment(TEXT_ALIGN_LEFT);
[5080]55  this->inputLineText->setText(NULL);
[5093]56  this->inputLine = new char[1];
57  this->inputLine[0] = '\0';
[5095]58  this->inputLineText->setParent2D(this);
[5093]59
60
61  EventHandler* evh = EventHandler::getInstance();
[5095]62  evh->subscribe(this, ES_GAME, SDLK_BACKQUOTE);
63  for (int i = 1; i < SDLK_F15; i++)
64    evh->subscribe(this, ES_SHELL, i);
[5068]65}
[4320]66
[5068]67Shell* Shell::singletonRef = NULL;
[1853]68
[3245]69/**
[4838]70 * standard deconstructor
[5068]71 */
72Shell::~Shell ()
[3543]73{
74  // delete what has to be deleted here
[5080]75  for (int i = 0; i < this->bufferDisplaySize; i++)
76    delete this->bufferText[i];
[5079]77  delete this->bufferText;
[5093]78
[5080]79  delete this->inputLineText;
[5093]80  delete this->inputLine;
[5079]81
[5068]82  Shell::singletonRef = NULL;
[3543]83}
[5068]84
[5074]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 */
[5072]89void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
90{
[5080]91  if (this->bufferText != NULL)
[5072]92  {
[5080]93    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
94      delete this->bufferText[i];
95    delete this->bufferText;
[5072]96  }
[5080]97
98  this->bufferText = new Text*[bufferDisplaySize];
99  for (unsigned int i = 0; i < bufferDisplaySize; i++)
[5072]100  {
[5080]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);
[5095]103    this->bufferText[i]->setRelCoor2D(5, 12+12*i);
[5080]104    this->bufferText[i]->setText(NULL);
[5089]105    this->bufferText[i]->setParent2D(this);
[5072]106  }
[5080]107
108
109  this->bufferDisplaySize = bufferDisplaySize;
[5072]110}
[5068]111
112/**
113 * deletes all the Buffers
114 */
115void Shell::flushBuffers()
116{
[5072]117  // remove all chars from the BufferTexts.
[5080]118  if (this->bufferText)
119    for (int i; i < this->bufferDisplaySize; i++)
120    {
121      this->bufferText[i]->setText(NULL);
122    }
[5068]123
[5072]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;
[5068]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
[5072]142 *
143 * @todo optimize
[5068]144 */
[5075]145bool Shell::addBufferLineStatic(const char* line, ...)
[5068]146{
[5075]147  va_list arguments;
148  va_start(arguments, line);
[5072]149
[5092]150#if DEBUG < 3
[5075]151  if (Shell::singletonRef == NULL)
[5089]152#endif
153
154  vprintf(line, arguments);
[5092]155#if DEBUG < 3
[5075]156  else
[5092]157#else
158  if (Shell::singletonRef != NULL)
[5089]159#endif
[5075]160    Shell::singletonRef->addBufferLine(line, arguments);
161  return true;
162}
[5080]163int curr = 0;
[5072]164
[5080]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 */
[5075]173void Shell::addBufferLine(const char* line, va_list arguments)
174{
[5078]175   vsprintf(this->bufferArray, line, arguments);
[5072]176
[5078]177   char* newLine = new char[strlen(this->bufferArray)+1];
178   strcpy(newLine, this->bufferArray);
[5073]179
[5080]180   this->buffer->add(newLine);
[5075]181
[5080]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);
[5068]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
[5072]205 *
206 * @todo
[5068]207 */
208void 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 */
216const char* Shell::getBufferLine(unsigned int lineNumber)
217{
[5072]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;
[5068]233}
234
235
236/**
237 * deletes the InputLine
238 */
239void Shell::flushInputLine()
240{
[5072]241  if (likely(this->inputLine != NULL))
242  {
243    delete [] this->inputLine;
244  }
245  this->inputLine = new char[1];
246  *this->inputLine = '\0';
247
[5068]248}
249
250/**
251 * adds one character to the inputLine
252 * @param character the character to add to the inputLine
253 */
254void Shell::addCharacter(char character)
255{
[5072]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;
[5093]261  this->inputLineText->setText(inputLine);
[5068]262}
263
264/**
265 * adds multiple Characters to thr inputLine
266 * @param characters a '\0' terminated char-array to add to the InputLine
267 */
268void Shell::addCharacters(const char* characters)
269{
[5072]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;
[5093]275  this->inputLineText->setText(inputLine);
[5068]276}
277
278/**
279 * removes characterCount characters from the InputLine
280 * @param characterCount the count of Characters to remove from the input Line
281 */
282void Shell::removeCharacters(unsigned int characterCount)
283{
[5093]284  if (strlen(this->inputLine) == 0)
285    return;
286
[5072]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);
[5093]293  removeCharLine[strlen(inputLine)-characterCount] = '\0';
[5072]294  delete this->inputLine;
295  this->inputLine = removeCharLine;
[5093]296  this->inputLineText->setText(inputLine);
[5068]297}
298
[5093]299#include "key_names.h"
[5069]300/**
301 * listens for some event
302 * @param event the Event happened
303 */
304void Shell::process(const Event &event)
305{
[5093]306  if (event.bPressed)
307  {
308    PRINTF(4)("Shell received command %s\n", SDLKToKeyname(event.type));
309    if (event.type == SDLK_BACKQUOTE)
310    {
311      if (EventHandler::getInstance()->getState() == ES_GAME)
[5094]312      {
[5093]313        EventHandler::getInstance()->setState(ES_SHELL);
[5094]314        this->setRelCoorSoft2D(0, GraphicsEngine::getInstance()->getResolutionY()-150, 1, 5);
315      }
316
[5093]317      else
[5094]318      {
[5093]319        EventHandler::getInstance()->setState(ES_GAME);
[5094]320        this->setRelCoorSoft2D(0, GraphicsEngine::getInstance()->getResolutionY()+10, 1, 5);
321      }
[5093]322    }
323    else if (event.type == SDLK_TAB)
324      this->autoComplete();
325    else if (event.type == SDLK_BACKSPACE)
[5095]326    {
327      this->delayed = this->repeatDelay;
328      this->pressedKey = SDLK_BACKSPACE;
[5093]329      this->removeCharacters(1);
[5095]330    }
[5093]331    else if (event.type < 127)
[5095]332    {
333      this->delayed = this->repeatDelay;
334      this->pressedKey = event.type;
[5093]335      this->addCharacter(event.type);
[5095]336    }
[5093]337  }
[5095]338  else // if(!event.bPressed)
339  {
340    if (this->pressedKey == event.type)
341      this->pressedKey = SDLK_FIRST;
342    this->delayed = 0.0;
343  }
[5069]344}
345
[5068]346/**
347 * ticks the Shell for dt Seconds
348 * @param dt the elapsed time since the last tick();
349 */
[5095]350void Shell::tick(float dt)
351{
352  if (this->delayed > 0.0)
353    this->delayed -= dt;
354  else if (this->pressedKey != SDLK_FIRST )
355  {
356    this->delayed = this->repeatDelay;
357    if (this->pressedKey == SDLK_BACKSPACE)
358      this->removeCharacters(1);
359    else if (pressedKey < 127)
360      this->addCharacter(this->pressedKey);
361  }
362}
[5068]363
364/**
365 * displays the Shell
366 */
367void Shell::draw() const
368{
369}
370
371
372/**
373 * autocompletes the Shell's inputLine
374 * @returns true, if a result was found, false otherwise
375 */
376bool Shell::autoComplete()
377{
[5093]378  PRINTF(3)("AutoCompletion not implemented yet\n");
[5068]379}
380
381/**
382 * displays some nice output from the Shell
383 */
384void Shell::debug() const
385{
386
387}
Note: See TracBrowser for help on using the repository browser.