Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more stl::list

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