Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: real strange, i thought i fixed this before
this seems to be a copy-past error

File size: 11.8 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#include "shell_buffer.h"
21#include "shell_input.h"
22
23
24#include "text_engine.h"
25#include "list.h"
26#include "graphics_engine.h"
27#include "event_handler.h"
28#include "debug.h"
29#include "class_list.h"
30
31#include "key_names.h"
32#include <stdarg.h>
33#include <stdio.h>
34
35using namespace std;
36
37SHELL_COMMAND(clear, Shell, clear)
38    ->describe("Clears the shell from unwanted lines (empties all buffers)")
39    ->setAlias("clear");
40SHELL_COMMAND(deactivate, Shell, deactivate)
41    ->describe("Deactivates the Shell. (moves it into background)")
42    ->setAlias("hide");
43SHELL_COMMAND(textsize, Shell, setTextSize)
44    ->describe("Sets the size of the Text (size, linespacing)")
45    ->defaultValues(1, 15, 0);
46SHELL_COMMAND(font, Shell, setFont)
47    ->describe("Sets the font of the Shell")
48    ->defaultValues(1, SHELL_DEFAULT_FONT);
49
50/**
51 * standard constructor
52 */
53Shell::Shell ()
54{
55  this->setClassID(CL_SHELL, "Shell");
56  this->setName("Shell");
57
58  // register the shell at the ShellBuffer
59  ShellBuffer::getInstance()->registerShell(this);
60  // EVENT-Handler subscription of '`' to all States.
61  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
62  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
63  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
64
65  // Element2D and generals
66  this->setAbsCoor2D(3, -400);
67  this->textSize = 15;
68  this->lineSpacing = 0;
69  this->bActive = false;
70  this->fontFile = new char[strlen(SHELL_DEFAULT_FONT)+1];
71  strcpy(this->fontFile, SHELL_DEFAULT_FONT);
72
73  // BUFFER
74  this->bufferText = NULL;
75  this->bufferDisplaySize = 10;
76  this->bufferOffset = 0;
77  this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->getIterator();
78
79  // INPUT LINE
80  this->shellInput = new ShellInput;
81  //this->commandList = new tList<ShellCommand>;
82
83  this->rebuildText();
84}
85
86/**
87 * standard deconstructor
88 */
89Shell::~Shell ()
90{
91  // delete the displayable Buffers
92  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
93    delete this->bufferText[i];
94  delete[] this->bufferText;
95  delete this->bufferIterator;
96  delete[] this->fontFile;
97  // delete the inputLine
98  delete this->shellInput;
99
100  ShellBuffer::getInstance()->unregisterShell(this);
101}
102
103/**
104 * activates the shell
105 *
106 * This also feeds the Last few lines from the main buffers into the displayBuffer
107 */
108void Shell::activate()
109{
110  if (this->bActive == true)
111    PRINTF(3)("The shell is already active\n");
112  this->bActive = true;
113
114  EventHandler::getInstance()->setState(ES_SHELL);
115  this->setRelCoorSoft2D(0, 0, 1, 5);
116
117  tIterator<char>* bufferIT = ShellBuffer::getInstance()->getBuffer()->getIterator();
118  bufferIT->lastElement();
119  for (int i = 0; i < this->bufferDisplaySize; i++)
120  {
121    this->bufferText[i]->setText(bufferIT->getCurrent(), true);
122    bufferIT->prevStep();
123  }
124  delete bufferIT;
125}
126
127/**
128 * deactiveates the Shell.
129 */
130void Shell::deactivate()
131{
132  if (this->bActive == false)
133    PRINTF(3)("The shell is already inactive\n");
134  this->bActive = false;
135
136  EventHandler::getInstance()->setState(ES_GAME);
137  this->setRelCoorSoft2D(0, -(int)this->shellHeight, 1, 5);
138
139  tIterator<char>* bufferIT = ShellBuffer::getInstance()->getBuffer()->getIterator();
140  bufferIT->lastElement();
141  for (int i = 0; i < this->bufferDisplaySize; i++)
142  {
143    this->bufferText[i]->setText(bufferIT->getCurrent(), false);
144    bufferIT->prevStep();
145  }
146  delete bufferIT;
147
148  this->bufferOffset = 0;
149}
150
151/**
152 * sets the File to load the fonts from
153 * @param fontFile the file to load the font from
154 *
155 * it is quite important, that the font pointed too really exists!
156 * (be aware that within orxonox fontFile is relative to the Data-Dir)
157 */
158void Shell::setFont(const char* fontFile)
159{
160  if (this->fontFile != NULL)
161    delete[] this->fontFile;
162
163  this->fontFile = new char[strlen(fontFile)+1];
164  strcpy(this->fontFile, fontFile);
165
166  this->rebuildText();
167}
168
169/**
170 * sets the size of the text and spacing
171 * @param textSize the size of the Text in Pixels
172 * @param lineSpacing the size of the Spacing between two lines in pixels
173 *
174 * this also rebuilds the entire Text, inputLine and displayBuffer,
175 * to be accurate again.
176 */
177void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
178{
179  this->textSize = textSize;
180  this->lineSpacing = lineSpacing;
181  this->shellInput->setFont(this->fontFile, this->textSize);
182
183  this->rebuildText();
184}
185
186/**
187 * rebuilds the Text's
188 *
189 * use this function, if you changed the Font/Size or something else.
190 */
191void Shell::rebuildText()
192{
193  this->shellInput->setFont(this->fontFile, this->textSize);
194  this->shellInput->setColor(1, 0, 0);
195  this->shellInput->setAlignment(TEXT_ALIGN_LEFT);
196  if (shellInput->getParent() != this)
197    this->shellInput->setParent2D(this);
198  this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize);
199
200  this->setBufferDisplaySize(this->bufferDisplaySize);
201}
202
203/**
204 * sets The count of Lines to display in the buffer.
205 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
206 */
207void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
208{
209  Text** bufferText = this->bufferText;
210  this->bufferText = NULL;
211  if (bufferText != NULL)
212  {
213    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
214      delete bufferText[i];
215    delete[] bufferText;
216  }
217
218  tIterator<char>* it = ShellBuffer::getInstance()->getBuffer()->getIterator();
219  char* text = it->lastElement();
220  bufferText = new Text*[bufferDisplaySize];
221  for (unsigned int i = 0; i < bufferDisplaySize; i++)
222  {
223    bufferText[i] = TextEngine::getInstance()->createText(this->fontFile, this->textSize, TEXT_RENDER_DYNAMIC);
224    bufferText[i]->setColor(1, 0, 0);
225    bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
226    bufferText[i]->setRelCoor2D(calculateLinePosition(i));
227    bufferText[i]->setText(text);
228    bufferText[i]->setParent2D(this);
229    text = it->prevElement();
230  }
231  delete it;
232  this->bufferDisplaySize = bufferDisplaySize;
233
234  this->bufferText = bufferText;
235  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
236}
237
238/**
239 * deletes all the Buffers
240 */
241void Shell::flush()
242{
243  // remove all chars from the BufferTexts.
244  if (this->bufferText != NULL)
245    for (int i = 0; i < this->bufferDisplaySize; i++)
246    {
247      this->bufferText[i]->setText(NULL, true);
248    }
249
250    ShellBuffer::getInstance()->flush();
251    // BUFFER FLUSHING
252}
253
254/**
255 * prints out some text to the input-buffers
256 * @param text the text to output.
257 */
258void Shell::printToDisplayBuffer(const char* text)
259{
260  if(likely(bufferText != NULL))
261  {
262    Text* lastText = this->bufferText[this->bufferDisplaySize-1];
263
264    Text* swapText;
265    Text* moveText = this->bufferText[0];
266    this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10);
267    for (unsigned int i = 1; i < this->bufferDisplaySize; i++)
268    {
269      if ( i < this->bufferDisplaySize-1)
270        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5);
271      swapText = this->bufferText[i];
272      this  ->bufferText[i] = moveText;
273      moveText = swapText;
274    }
275    lastText->setRelCoor2D(this->calculateLinePosition(0));
276    this->bufferText[0] = lastText;
277
278    this->bufferText[0]->setText(text, true);
279  }
280}
281
282/**
283 * moves the Display buffer (up or down)
284 * @param lineCount the count by which to shift the InputBuffer.
285 */
286void Shell::moveDisplayBuffer(int lineCount)
287{
288  if (!this->bufferIterator->compareListPointer(ShellBuffer::getInstance()->getBuffer()))
289  {
290    delete this->bufferIterator;
291    this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->getIterator();
292  }
293
294  if (this->bufferOffset == 0)
295   {
296     this->bufferIterator->lastElement();
297//     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
298//       this->bufferIterator->prevStep();
299   }
300
301  // boundraries
302  if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer()->getSize())
303    lineCount = (int)ShellBuffer::getInstance()->getBuffer()->getSize()- this->bufferOffset;
304  else if (this->bufferOffset + lineCount < 0)
305    lineCount = -bufferOffset;
306  this->bufferOffset += lineCount;
307
308  // moving the iterator to the right position
309  int move = 0;
310  while (move != lineCount)
311  {
312    if (move < lineCount)
313    {
314      ++move;
315      this->bufferIterator->prevStep();
316    }
317    else
318    {
319      --move;
320      this->bufferIterator->nextStep();
321    }
322  }
323  // redisplay the buffers
324  tIterator<char> it = *this->bufferIterator;
325  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
326  {
327    this->bufferText[i]->setText(it.getCurrent(), false);
328    it.prevStep();
329  }
330}
331
332/**
333 * clears the Shell (empties all buffers)
334 */
335void Shell::clear()
336{
337  this->flush();
338  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
339}
340
341/**
342 * listens for some event
343 * @param event the Event happened
344 */
345void Shell::process(const Event &event)
346{
347  if (event.bPressed)
348  {
349    if (event.type == SDLK_BACKQUOTE)
350    {
351      if (EventHandler::getInstance()->getState() == ES_GAME)
352        this->activate();
353      else
354        this->deactivate();
355    }
356    else if (event.type == SDLK_PAGEUP)
357    {
358      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
359    }
360    else if (event.type == SDLK_PAGEDOWN)
361    {
362      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
363    }
364  }
365}
366
367/**
368 * displays the Shell
369 */
370void Shell::draw() const
371{
372  glPushMatrix();
373  // transform for alignment.
374  // setting the Blending effects
375
376  glColor4f(0.0f, 0.0f, 0.8f, .4);
377  glEnable(GL_BLEND);
378  glDisable(GL_TEXTURE_2D);
379  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
380
381  glBindTexture(GL_TEXTURE_2D, 0);
382  glBegin(GL_TRIANGLE_STRIP);
383
384  glTexCoord2f(0, 0);
385  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
386
387  glTexCoord2f(1, 0);
388  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
389
390  glTexCoord2f(0, 1);
391  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
392
393  glTexCoord2f(1, 1);
394  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
395
396  glEnd();
397}
398
399///////////////////////
400// HELPER FUNCTIONS  //
401///////////////////////
402
403/**
404 * calculates the position of a Buffer-Display Line
405 * @param lineNumber the lineNumber from the bottom to calculate the position from
406 * @returns the Position of the Line.
407 */
408Vector Shell::calculateLinePosition(unsigned int lineNumber)
409{
410  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0);
411}
412
413
414
415/**
416 * displays some nice output from the Shell
417 */
418void Shell::debug() const
419{
420  PRINT(3)("Debugging output to console (not this shell)\n");
421
422//   if (this->pressedKey != SDLK_FIRST)
423//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
424
425
426  ShellBuffer::getInstance()->debug();
427}
428
429// void Shell::testI (int i)
430// {
431//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
432// }
433//
434// void Shell::testS (const char* s)
435// {
436//   PRINTF(3)("This is the Test for one String '%s'\n", s);
437// }
438//
439// void Shell::testB (bool b)
440// {
441//   PRINTF(3)("This is the Test for one Bool: ");
442//   if (b)
443//     PRINTF(3)("true\n");
444//   else
445//     PRINTF(3)("false\n");
446// }
447//
448// void Shell::testF (float f)
449// {
450//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
451// }
452//
453// void Shell::testSF (const char* s, float f)
454// {
455//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
456// }
Note: See TracBrowser for help on using the repository browser.