Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5090 was 5090, checked in by bensch, 19 years ago

orxonox/trunk: math should be easy…

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  this->setAbsCoor2D(2, 400);
46  this->setAbsDir2D(0);
47
48  this->inputLineText = TextEngine::getInstance()->createText("fonts/earth.ttf", 10, TEXT_DYNAMIC, 255, 0, 0);
49  this->inputLineText->setText(NULL);
50}
51
52Shell* Shell::singletonRef = NULL;
53
54/**
55 * standard deconstructor
56 */
57Shell::~Shell ()
58{
59  // delete what has to be deleted here
60  for (int i = 0; i < this->bufferDisplaySize; i++)
61    delete this->bufferText[i];
62  delete this->bufferText;
63  delete this->inputLineText;
64
65
66  Shell::singletonRef = NULL;
67}
68
69/**
70 * sets The count of Lines to display in the buffer.
71 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
72 */
73void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
74{
75  if (this->bufferText != NULL)
76  {
77    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
78      delete this->bufferText[i];
79    delete this->bufferText;
80  }
81
82  this->bufferText = new Text*[bufferDisplaySize];
83  for (unsigned int i = 0; i < bufferDisplaySize; i++)
84  {
85    this->bufferText[i] = TextEngine::getInstance()->createText("fonts/earth.ttf", this->textSize, TEXT_DYNAMIC, 255, 0, 0);
86    this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
87    this->bufferText[i]->setRelCoor2D(5, 12*i);
88    this->bufferText[i]->setText(NULL);
89    this->bufferText[i]->setParent2D(this);
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 DEBUG > 3
135  if (Shell::singletonRef == NULL)
136#endif
137
138  vprintf(line, arguments);
139#if DEBUG > 3
140  else
141#endif
142    Shell::singletonRef->addBufferLine(line, arguments);
143  return true;
144}
145int curr = 0;
146
147/**
148 * add a Line to the List of Buffers
149 * @param line
150 * @param arguments
151 *
152 * This function Adds one line to the buffer.
153 * and displays the line as the First Line of the display-buffer
154 */
155void Shell::addBufferLine(const char* line, va_list arguments)
156{
157   vsprintf(this->bufferArray, line, arguments);
158
159   char* newLine = new char[strlen(this->bufferArray)+1];
160   strcpy(newLine, this->bufferArray);
161
162   this->buffer->add(newLine);
163
164   if (this->buffer->getSize() > this->bufferSize)
165   {
166     delete this->buffer->firstElement();
167     this->buffer->remove(this->buffer->firstElement());
168   }
169
170   if (likely(bufferText != NULL))
171   {
172     Text* moveText = this->bufferText[this->bufferDisplaySize-1];
173     for (int i = this->bufferDisplaySize-1; i > 0; i--)
174     {
175       this->bufferText[i] = this->bufferText[i-1];
176     }
177     this->bufferText[0] = moveText;
178   }
179   this->bufferText[0]->setText(newLine);
180   // this->bufferText->
181//  this->inputLineText->setText(newLine);
182}
183
184/**
185 * moves the buffer around lineCount lines upwards (negative values move down)
186 * @param lineCount the Count of lines to move upwards
187 *
188 * @todo
189 */
190void Shell::moveBuffer(int lineCount)
191{
192}
193
194/**
195 * @param lineNumber the n-th line from the bottom
196 * @returns the Buffer at Line lineNumber
197 */
198const char* Shell::getBufferLine(unsigned int lineNumber)
199{
200  tIterator<char>* charIterator = this->buffer->getIterator();
201  char* charElem = charIterator->nextElement();
202
203  int i = 1;
204  while (charElem != NULL)
205  {
206    if (i++ < lineNumber)
207    {
208      delete charIterator;
209      return charElem;
210    }
211
212    charElem = charIterator->nextElement();
213  }
214  delete charIterator;
215}
216
217
218/**
219 * deletes the InputLine
220 */
221void Shell::flushInputLine()
222{
223  if (likely(this->inputLine != NULL))
224  {
225    delete [] this->inputLine;
226  }
227  this->inputLine = new char[1];
228  *this->inputLine = '\0';
229
230}
231
232/**
233 * adds one character to the inputLine
234 * @param character the character to add to the inputLine
235 */
236void Shell::addCharacter(char character)
237{
238  char* addCharLine = new char[strlen(inputLine)+2];
239
240  sprintf(addCharLine, "%s%c", this->inputLine, character);
241  delete this->inputLine;
242  this->inputLine = addCharLine;
243}
244
245/**
246 * adds multiple Characters to thr inputLine
247 * @param characters a '\0' terminated char-array to add to the InputLine
248 */
249void Shell::addCharacters(const char* characters)
250{
251  char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1];
252
253  sprintf(addCharLine, "%s%s", this->inputLine, characters);
254  delete this->inputLine;
255  this->inputLine = addCharLine;
256}
257
258/**
259 * removes characterCount characters from the InputLine
260 * @param characterCount the count of Characters to remove from the input Line
261 */
262void Shell::removeCharacters(unsigned int characterCount)
263{
264  if (characterCount > strlen(this->inputLine))
265    characterCount = strlen(this->inputLine);
266
267  char* removeCharLine = new char[strlen(inputLine)-characterCount+1];
268
269  strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount);
270  delete this->inputLine;
271  this->inputLine = removeCharLine;
272}
273
274/**
275 * listens for some event
276 * @param event the Event happened
277 */
278void Shell::process(const Event &event)
279{
280//  if (event.type)
281
282}
283
284/**
285 * ticks the Shell for dt Seconds
286 * @param dt the elapsed time since the last tick();
287 */
288//void Shell::tick(float dt)
289//{
290//}
291
292/**
293 * displays the Shell
294 */
295void Shell::draw() const
296{
297}
298
299
300/**
301 * autocompletes the Shell's inputLine
302 * @returns true, if a result was found, false otherwise
303 */
304bool Shell::autoComplete()
305{
306
307}
308
309/**
310 * displays some nice output from the Shell
311 */
312void Shell::debug() const
313{
314
315}
Note: See TracBrowser for help on using the repository browser.