Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: shell is now scrolling through the information

File size: 6.7 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
20#include "text_engine.h"
21#include "list.h"
22
23#include <stdarg.h>
24#include <stdio.h>
25
26using namespace std;
27
28
29/**
30 * standard constructor
31 */
32Shell::Shell ()
33{
34  this->setClassID(CL_SHELL, "Shell");
35  this->setName("Shell");
36
37  this->buffer = new tList<char>;
38
39  this->textSize = 10;
40
41  //this->bufferSize = 0;
42  this->bufferText = NULL;
43  this->setBufferSize(100);
44  this->setBufferDisplaySize(10);
45
46  this->inputLineText = TextEngine::getInstance()->createText("fonts/earth.ttf", 10, TEXT_DYNAMIC, 255, 0, 0);
47  this->inputLineText->setText(NULL);
48
49  //this->addBufferLineStatic("asjflksjdvklasmv %s", "doom");
50  //TextEngine::getInstance()->debug();
51  //exit(-1);
52}
53
54Shell* Shell::singletonRef = NULL;
55
56/**
57 * standard deconstructor
58 */
59Shell::~Shell ()
60{
61  // delete what has to be deleted here
62  for (int i = 0; i < this->bufferDisplaySize; i++)
63    delete this->bufferText[i];
64  delete this->bufferText;
65  delete this->inputLineText;
66
67  Shell::singletonRef = NULL;
68}
69
70/**
71 * sets The count of Lines to display in the buffer.
72 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
73 */
74void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
75{
76  if (this->bufferText != NULL)
77  {
78    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
79      delete this->bufferText[i];
80    delete this->bufferText;
81  }
82
83  this->bufferText = new Text*[bufferDisplaySize];
84  for (unsigned int i = 0; i < bufferDisplaySize; i++)
85  {
86    this->bufferText[i] = TextEngine::getInstance()->createText("fonts/earth.ttf", this->textSize, TEXT_DYNAMIC, 255, 0, 0);
87    this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
88    this->bufferText[i]->setPosition2D(5, 400 + 12*i);
89    this->bufferText[i]->setText(NULL);
90  }
91
92
93  this->bufferDisplaySize = bufferDisplaySize;
94}
95
96/**
97 * deletes all the Buffers
98 */
99void Shell::flushBuffers()
100{
101  // remove all chars from the BufferTexts.
102  if (this->bufferText)
103    for (int i; i < this->bufferDisplaySize; i++)
104    {
105      this->bufferText[i]->setText(NULL);
106    }
107
108
109  // delete all the Chars in the Buffers
110  tIterator<char>* charIterator = this->buffer->getIterator();
111  char* charElem = charIterator->nextElement();
112
113  while (charElem != NULL)
114  {
115    delete charElem;
116
117    charElem = charIterator->nextElement();
118  }
119  delete charIterator;
120}
121
122/**
123 * adds a new Line to the List of Buffers
124 * @param line the Line as in the first argument in printf
125 * @param args the arguments as a va_list
126 *
127 * @todo optimize
128 */
129bool Shell::addBufferLineStatic(const char* line, ...)
130{
131  va_list arguments;
132  va_start(arguments, line);
133
134  if (Shell::singletonRef == NULL)
135   vprintf(line, arguments);
136  else
137    Shell::singletonRef->addBufferLine(line, arguments);
138  return true;
139}
140int curr = 0;
141
142/**
143 * add a Line to the List of Buffers
144 * @param line
145 * @param arguments
146 *
147 * This function Adds one line to the buffer.
148 * and displays the line as the First Line of the display-buffer
149 */
150void Shell::addBufferLine(const char* line, va_list arguments)
151{
152   vsprintf(this->bufferArray, line, arguments);
153
154   char* newLine = new char[strlen(this->bufferArray)+1];
155   strcpy(newLine, this->bufferArray);
156
157   this->buffer->add(newLine);
158
159   if (this->buffer->getSize() > this->bufferSize)
160   {
161     delete this->buffer->firstElement();
162     this->buffer->remove(this->buffer->firstElement());
163   }
164
165   if (likely(bufferText != NULL))
166   {
167     Text* moveText = this->bufferText[this->bufferDisplaySize-1];
168     for (int i = this->bufferDisplaySize-1; i > 0; i--)
169     {
170       this->bufferText[i] = this->bufferText[i-1];
171     // this->bufferText[i]
172     }
173     this->bufferText[0] = moveText;
174   }
175   this->bufferText[0]->setText(newLine);
176   // this->bufferText->
177//  this->inputLineText->setText(newLine);
178}
179
180/**
181 * moves the buffer around lineCount lines upwards (negative values move down)
182 * @param lineCount the Count of lines to move upwards
183 *
184 * @todo
185 */
186void Shell::moveBuffer(int lineCount)
187{
188}
189
190/**
191 * @param lineNumber the n-th line from the bottom
192 * @returns the Buffer at Line lineNumber
193 */
194const char* Shell::getBufferLine(unsigned int lineNumber)
195{
196  tIterator<char>* charIterator = this->buffer->getIterator();
197  char* charElem = charIterator->nextElement();
198
199  int i = 1;
200  while (charElem != NULL)
201  {
202    if (i++ < lineNumber)
203    {
204      delete charIterator;
205      return charElem;
206    }
207
208    charElem = charIterator->nextElement();
209  }
210  delete charIterator;
211}
212
213
214/**
215 * deletes the InputLine
216 */
217void Shell::flushInputLine()
218{
219  if (likely(this->inputLine != NULL))
220  {
221    delete [] this->inputLine;
222  }
223  this->inputLine = new char[1];
224  *this->inputLine = '\0';
225
226}
227
228/**
229 * adds one character to the inputLine
230 * @param character the character to add to the inputLine
231 */
232void Shell::addCharacter(char character)
233{
234  char* addCharLine = new char[strlen(inputLine)+2];
235
236  sprintf(addCharLine, "%s%c", this->inputLine, character);
237  delete this->inputLine;
238  this->inputLine = addCharLine;
239}
240
241/**
242 * adds multiple Characters to thr inputLine
243 * @param characters a '\0' terminated char-array to add to the InputLine
244 */
245void Shell::addCharacters(const char* characters)
246{
247  char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1];
248
249  sprintf(addCharLine, "%s%s", this->inputLine, characters);
250  delete this->inputLine;
251  this->inputLine = addCharLine;
252}
253
254/**
255 * removes characterCount characters from the InputLine
256 * @param characterCount the count of Characters to remove from the input Line
257 */
258void Shell::removeCharacters(unsigned int characterCount)
259{
260  if (characterCount > strlen(this->inputLine))
261    characterCount = strlen(this->inputLine);
262
263  char* removeCharLine = new char[strlen(inputLine)-characterCount+1];
264
265  strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount);
266  delete this->inputLine;
267  this->inputLine = removeCharLine;
268}
269
270/**
271 * listens for some event
272 * @param event the Event happened
273 */
274void Shell::process(const Event &event)
275{
276//  if (event.type)
277
278}
279
280/**
281 * ticks the Shell for dt Seconds
282 * @param dt the elapsed time since the last tick();
283 */
284//void Shell::tick(float dt)
285//{
286//}
287
288/**
289 * displays the Shell
290 */
291void Shell::draw() const
292{
293}
294
295
296/**
297 * autocompletes the Shell's inputLine
298 * @returns true, if a result was found, false otherwise
299 */
300bool Shell::autoComplete()
301{
302
303}
304
305/**
306 * displays some nice output from the Shell
307 */
308void Shell::debug() const
309{
310
311}
Note: See TracBrowser for help on using the repository browser.