Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: inputLine is working.

File size: 8.0 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);
[5093]48  this->setAbsCoor2D(2, GraphicsEngine::getInstance()->getResolutionY());
[5090]49  this->setAbsDir2D(0);
[5074]50
[5080]51  this->inputLineText = TextEngine::getInstance()->createText("fonts/earth.ttf", 10, TEXT_DYNAMIC, 255, 0, 0);
52  this->inputLineText->setText(NULL);
[5093]53  this->inputLine = new char[1];
54  this->inputLine[0] = '\0';
55
56  EventHandler::getInstance()->subscribe(this, ES_GAME, SDLK_BACKQUOTE);
57
58  EventHandler* evh = EventHandler::getInstance();
59  for (int i = 1; i < EV_NUMBER; i++)
60    EventHandler::getInstance()->subscribe(this, ES_SHELL, i);
[5068]61}
[4320]62
[5068]63Shell* Shell::singletonRef = NULL;
[1853]64
[3245]65/**
[4838]66 * standard deconstructor
[5068]67 */
68Shell::~Shell ()
[3543]69{
70  // delete what has to be deleted here
[5080]71  for (int i = 0; i < this->bufferDisplaySize; i++)
72    delete this->bufferText[i];
[5079]73  delete this->bufferText;
[5093]74
[5080]75  delete this->inputLineText;
[5093]76  delete this->inputLine;
[5079]77
[5068]78  Shell::singletonRef = NULL;
[3543]79}
[5068]80
[5074]81/**
82 * sets The count of Lines to display in the buffer.
83 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
84 */
[5072]85void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
86{
[5080]87  if (this->bufferText != NULL)
[5072]88  {
[5080]89    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
90      delete this->bufferText[i];
91    delete this->bufferText;
[5072]92  }
[5080]93
94  this->bufferText = new Text*[bufferDisplaySize];
95  for (unsigned int i = 0; i < bufferDisplaySize; i++)
[5072]96  {
[5080]97    this->bufferText[i] = TextEngine::getInstance()->createText("fonts/earth.ttf", this->textSize, TEXT_DYNAMIC, 255, 0, 0);
98    this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
[5090]99    this->bufferText[i]->setRelCoor2D(5, 12*i);
[5080]100    this->bufferText[i]->setText(NULL);
[5089]101    this->bufferText[i]->setParent2D(this);
[5072]102  }
[5080]103
104
105  this->bufferDisplaySize = bufferDisplaySize;
[5072]106}
[5068]107
108/**
109 * deletes all the Buffers
110 */
111void Shell::flushBuffers()
112{
[5072]113  // remove all chars from the BufferTexts.
[5080]114  if (this->bufferText)
115    for (int i; i < this->bufferDisplaySize; i++)
116    {
117      this->bufferText[i]->setText(NULL);
118    }
[5068]119
[5072]120
121  // delete all the Chars in the Buffers
122  tIterator<char>* charIterator = this->buffer->getIterator();
123  char* charElem = charIterator->nextElement();
124
125  while (charElem != NULL)
126  {
127    delete charElem;
128
129    charElem = charIterator->nextElement();
130  }
131  delete charIterator;
[5068]132}
133
134/**
135 * adds a new Line to the List of Buffers
136 * @param line the Line as in the first argument in printf
137 * @param args the arguments as a va_list
[5072]138 *
139 * @todo optimize
[5068]140 */
[5075]141bool Shell::addBufferLineStatic(const char* line, ...)
[5068]142{
[5075]143  va_list arguments;
144  va_start(arguments, line);
[5072]145
[5092]146#if DEBUG < 3
[5075]147  if (Shell::singletonRef == NULL)
[5089]148#endif
149
150  vprintf(line, arguments);
[5092]151#if DEBUG < 3
[5075]152  else
[5092]153#else
154  if (Shell::singletonRef != NULL)
[5089]155#endif
[5075]156    Shell::singletonRef->addBufferLine(line, arguments);
157  return true;
158}
[5080]159int curr = 0;
[5072]160
[5080]161/**
162 * add a Line to the List of Buffers
163 * @param line
164 * @param arguments
165 *
166 * This function Adds one line to the buffer.
167 * and displays the line as the First Line of the display-buffer
168 */
[5075]169void Shell::addBufferLine(const char* line, va_list arguments)
170{
[5078]171   vsprintf(this->bufferArray, line, arguments);
[5072]172
[5078]173   char* newLine = new char[strlen(this->bufferArray)+1];
174   strcpy(newLine, this->bufferArray);
[5073]175
[5080]176   this->buffer->add(newLine);
[5075]177
[5080]178   if (this->buffer->getSize() > this->bufferSize)
179   {
180     delete this->buffer->firstElement();
181     this->buffer->remove(this->buffer->firstElement());
182   }
183
184   if (likely(bufferText != NULL))
185   {
186     Text* moveText = this->bufferText[this->bufferDisplaySize-1];
187     for (int i = this->bufferDisplaySize-1; i > 0; i--)
188     {
189       this->bufferText[i] = this->bufferText[i-1];
190     }
191     this->bufferText[0] = moveText;
192   }
193   this->bufferText[0]->setText(newLine);
194   // this->bufferText->
195//  this->inputLineText->setText(newLine);
[5068]196}
197
198/**
199 * moves the buffer around lineCount lines upwards (negative values move down)
200 * @param lineCount the Count of lines to move upwards
[5072]201 *
202 * @todo
[5068]203 */
204void Shell::moveBuffer(int lineCount)
205{
206}
207
208/**
209 * @param lineNumber the n-th line from the bottom
210 * @returns the Buffer at Line lineNumber
211 */
212const char* Shell::getBufferLine(unsigned int lineNumber)
213{
[5072]214  tIterator<char>* charIterator = this->buffer->getIterator();
215  char* charElem = charIterator->nextElement();
216
217  int i = 1;
218  while (charElem != NULL)
219  {
220    if (i++ < lineNumber)
221    {
222      delete charIterator;
223      return charElem;
224    }
225
226    charElem = charIterator->nextElement();
227  }
228  delete charIterator;
[5068]229}
230
231
232/**
233 * deletes the InputLine
234 */
235void Shell::flushInputLine()
236{
[5072]237  if (likely(this->inputLine != NULL))
238  {
239    delete [] this->inputLine;
240  }
241  this->inputLine = new char[1];
242  *this->inputLine = '\0';
243
[5068]244}
245
246/**
247 * adds one character to the inputLine
248 * @param character the character to add to the inputLine
249 */
250void Shell::addCharacter(char character)
251{
[5072]252  char* addCharLine = new char[strlen(inputLine)+2];
253
254  sprintf(addCharLine, "%s%c", this->inputLine, character);
255  delete this->inputLine;
256  this->inputLine = addCharLine;
[5093]257  this->inputLineText->setText(inputLine);
[5068]258}
259
260/**
261 * adds multiple Characters to thr inputLine
262 * @param characters a '\0' terminated char-array to add to the InputLine
263 */
264void Shell::addCharacters(const char* characters)
265{
[5072]266  char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1];
267
268  sprintf(addCharLine, "%s%s", this->inputLine, characters);
269  delete this->inputLine;
270  this->inputLine = addCharLine;
[5093]271  this->inputLineText->setText(inputLine);
[5068]272}
273
274/**
275 * removes characterCount characters from the InputLine
276 * @param characterCount the count of Characters to remove from the input Line
277 */
278void Shell::removeCharacters(unsigned int characterCount)
279{
[5093]280  if (strlen(this->inputLine) == 0)
281    return;
282
[5072]283  if (characterCount > strlen(this->inputLine))
284    characterCount = strlen(this->inputLine);
285
286  char* removeCharLine = new char[strlen(inputLine)-characterCount+1];
287
288  strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount);
[5093]289  removeCharLine[strlen(inputLine)-characterCount] = '\0';
[5072]290  delete this->inputLine;
291  this->inputLine = removeCharLine;
[5093]292  this->inputLineText->setText(inputLine);
[5068]293}
294
[5093]295#include "key_names.h"
[5069]296/**
297 * listens for some event
298 * @param event the Event happened
299 */
300void Shell::process(const Event &event)
301{
[5093]302  if (event.bPressed)
303  {
304    PRINTF(4)("Shell received command %s\n", SDLKToKeyname(event.type));
305    if (event.type == SDLK_BACKQUOTE)
306    {
307      if (EventHandler::getInstance()->getState() == ES_GAME)
308        EventHandler::getInstance()->setState(ES_SHELL);
309      else
310        EventHandler::getInstance()->setState(ES_GAME);
311    }
[5068]312
[5093]313    else if (event.type == SDLK_TAB)
314      this->autoComplete();
315    else if (event.type == SDLK_BACKSPACE)
316      this->removeCharacters(1);
317    else if (event.type < 127)
318      this->addCharacter(event.type);
319  }
[5069]320}
321
[5068]322/**
323 * ticks the Shell for dt Seconds
324 * @param dt the elapsed time since the last tick();
325 */
[5074]326//void Shell::tick(float dt)
327//{
328//}
[5068]329
330/**
331 * displays the Shell
332 */
333void Shell::draw() const
334{
335}
336
337
338/**
339 * autocompletes the Shell's inputLine
340 * @returns true, if a result was found, false otherwise
341 */
342bool Shell::autoComplete()
343{
[5093]344  PRINTF(3)("AutoCompletion not implemented yet\n");
[5068]345}
346
347/**
348 * displays some nice output from the Shell
349 */
350void Shell::debug() const
351{
352
353}
Note: See TracBrowser for help on using the repository browser.