Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more stl in Shell

File size: 14.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"
[5129]19#include "shell_command.h"
[5175]20#include "shell_buffer.h"
[5179]21#include "shell_input.h"
[1853]22
[5175]23
[5344]24#include "text.h"
[5072]25#include "list.h"
[5093]26#include "graphics_engine.h"
[5372]27#include "material.h"
[5093]28#include "event_handler.h"
[5129]29#include "debug.h"
[5113]30#include "class_list.h"
31
32#include "key_names.h"
[5075]33#include <stdarg.h>
34#include <stdio.h>
35
[1856]36using namespace std;
[1853]37
[5201]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)")
[5204]43    ->setAlias("hide");
[5208]44SHELL_COMMAND(textsize, Shell, setTextSize)
[5374]45    ->describe("Sets the size of the Text size, linespacing")
[5254]46    ->defaultValues(1, 15, 0);
[5374]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");
[5254]55SHELL_COMMAND(font, Shell, setFont)
56    ->describe("Sets the font of the Shell")
57    ->defaultValues(1, SHELL_DEFAULT_FONT);
[1856]58
[3245]59/**
[4838]60 * standard constructor
[5068]61 */
62Shell::Shell ()
[3365]63{
[5072]64  this->setClassID(CL_SHELL, "Shell");
65  this->setName("Shell");
66
[5245]67  // EVENT-Handler subscription of '`' to all States.
68  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
[5425]69  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_F12);
[5246]70  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
71  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
[5206]72
[5372]73  // BUFFER
74  this->bufferText = NULL;
75  this->bufferDisplaySize = 10;
76  this->bufferOffset = 0;
[5781]77  this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->begin();
[5372]78
79  // INPUT LINE
80  this->shellInput = new ShellInput;
81
82  this->backgroundMaterial = new Material;
[5183]83  // Element2D and generals
[5175]84  this->setAbsCoor2D(3, -400);
[5329]85  this->textSize = 20;
[5208]86  this->lineSpacing = 0;
[5420]87  this->bActive = true;
[5253]88  this->fontFile = new char[strlen(SHELL_DEFAULT_FONT)+1];
89  strcpy(this->fontFile, SHELL_DEFAULT_FONT);
[5072]90
[5080]91
[5113]92  this->rebuildText();
[5374]93
[5372]94  this->setTextColor(SHELL_DEFAULT_TEXT_COLOR);
95  this->setBackgroundColor(SHELL_DEFAULT_BACKGROUND_COLOR);
[5335]96
[5420]97  this->deactivate();
[5335]98  // register the shell at the ShellBuffer
99  ShellBuffer::getInstance()->registerShell(this);
[5068]100}
[4320]101
[3245]102/**
[4838]103 * standard deconstructor
[5068]104 */
105Shell::~Shell ()
[3543]106{
[5335]107  ShellBuffer::getInstance()->unregisterShell(this);
108
[5099]109  // delete the displayable Buffers
[5227]110  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
[5080]111    delete this->bufferText[i];
[5113]112  delete[] this->bufferText;
[5251]113  delete[] this->fontFile;
[5099]114  // delete the inputLine
[5180]115  delete this->shellInput;
[5372]116  delete this->backgroundMaterial;
[3543]117}
[5068]118
[5119]119/**
120 * activates the shell
121 *
122 * This also feeds the Last few lines from the main buffers into the displayBuffer
123 */
[5113]124void Shell::activate()
125{
126  if (this->bActive == true)
127    PRINTF(3)("The shell is already active\n");
128  this->bActive = true;
129
[5388]130  EventHandler::getInstance()->pushState(ES_SHELL);
[5113]131  this->setRelCoorSoft2D(0, 0, 1, 5);
[5118]132
[5781]133  list<char*>::const_reverse_iterator textLine = ShellBuffer::getInstance()->getBuffer()->rbegin();
[5246]134  for (int i = 0; i < this->bufferDisplaySize; i++)
[5247]135  {
[5781]136    this->bufferText[i]->setText((*textLine), true);
[5420]137    this->bufferText[i]->setVisibility(true);
[5781]138    textLine--;
[5247]139  }
[5113]140}
141
[5119]142/**
143 * deactiveates the Shell.
144 */
[5113]145void Shell::deactivate()
146{
147  if (this->bActive == false)
148    PRINTF(3)("The shell is already inactive\n");
149  this->bActive = false;
150
[5388]151  EventHandler::getInstance()->popState();
152
[5253]153  this->setRelCoorSoft2D(0, -(int)this->shellHeight, 1, 5);
[5118]154
[5781]155  list<char*>::const_reverse_iterator textLine = ShellBuffer::getInstance()->getBuffer()->rbegin();
[5157]156  for (int i = 0; i < this->bufferDisplaySize; i++)
[5247]157  {
[5781]158    this->bufferText[i]->setText((*textLine), false);
[5420]159    this->bufferText[i]->setVisibility(false);
[5781]160    textLine--;
[5247]161  }
[5248]162  this->bufferOffset = 0;
[5113]163}
164
[5119]165/**
[5251]166 * sets the File to load the fonts from
167 * @param fontFile the file to load the font from
[5254]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)
[5251]171 */
172void Shell::setFont(const char* fontFile)
173{
[5335]174//   if (!ResourceManager::isInDataDir(fontFile))
175//     return false;
176
[5251]177  if (this->fontFile != NULL)
178    delete[] this->fontFile;
179
180  this->fontFile = new char[strlen(fontFile)+1];
181  strcpy(this->fontFile, fontFile);
182
[5253]183  this->rebuildText();
[5251]184}
185
186/**
[5119]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 */
[5113]194void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
195{
196  this->textSize = textSize;
197  this->lineSpacing = lineSpacing;
[5372]198  this->resetValues();
199}
[5113]200
[5372]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/**
[5374]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/**
[5372]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]);
[5420]254    this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
[5372]255  }
256
[5369]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);
[5372]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));
[5369]267      }
268    }
269  }
270  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5113]271}
272
[5127]273/**
274 * rebuilds the Text's
275 *
276 * use this function, if you changed the Font/Size or something else.
277 */
[5113]278void Shell::rebuildText()
279{
[5251]280  this->shellInput->setFont(this->fontFile, this->textSize);
[5179]281  this->shellInput->setAlignment(TEXT_ALIGN_LEFT);
[5397]282  if (shellInput->getParent2D() != this)
[5253]283    this->shellInput->setParent2D(this);
[5113]284
285  this->setBufferDisplaySize(this->bufferDisplaySize);
286}
287
[5074]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 */
[5072]292void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
293{
[5251]294  Text** bufferText = this->bufferText;
295  this->bufferText = NULL;
296  if (bufferText != NULL)
[5072]297  {
[5080]298    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
[5251]299      delete bufferText[i];
300    delete[] bufferText;
[5072]301  }
[5080]302
[5781]303  list<char*>::const_reverse_iterator textLine = ShellBuffer::getInstance()->getBuffer()->rbegin();
[5251]304  bufferText = new Text*[bufferDisplaySize];
[5080]305  for (unsigned int i = 0; i < bufferDisplaySize; i++)
[5072]306  {
[5767]307    bufferText[i] = new Text(this->fontFile, this->textSize);
[5251]308    bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
[5781]309    bufferText[i]->setText(*textLine);
[5251]310    bufferText[i]->setParent2D(this);
[5781]311    textLine--;
[5072]312  }
[5113]313  this->bufferDisplaySize = bufferDisplaySize;
[5111]314
[5251]315  this->bufferText = bufferText;
[5113]316  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5072]317}
[5068]318
319/**
320 * deletes all the Buffers
321 */
[5175]322void Shell::flush()
[5068]323{
[5072]324  // remove all chars from the BufferTexts.
[5251]325  if (this->bufferText != NULL)
[5125]326    for (int i = 0; i < this->bufferDisplaySize; i++)
[5080]327    {
[5125]328      this->bufferText[i]->setText(NULL, true);
[5080]329    }
[5246]330
331    ShellBuffer::getInstance()->flush();
332    // BUFFER FLUSHING
[5068]333}
334
335/**
[5118]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];
[5113]344
[5118]345    Text* swapText;
346    Text* moveText = this->bufferText[0];
[5375]347    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
[5118]348    {
349      if ( i < this->bufferDisplaySize-1)
[5375]350        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1), 5);
[5118]351      swapText = this->bufferText[i];
[5375]352      this->bufferText[i] = moveText;
[5118]353      moveText = swapText;
354    }
[5375]355
356  /*  FANCY EFFECTS :)
[5376]357    1:
[5375]358        lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1000,0,0));
359        lastText->setRelCoorSoft2D(this->calculateLinePosition(0),10);
[5376]360    2:
[5377]361  */
[5383]362    lastText->setRelDir2D(-90);
363    lastText->setRelDirSoft2D(0, 20);
[5376]364    lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1000,0,0));
365    lastText->setRelCoorSoft2D(this->calculateLinePosition(0),10);
[5377]366
367 //   lastText->setRelCoor2D(this->calculateLinePosition(0));
[5118]368    this->bufferText[0] = lastText;
369
[5122]370    this->bufferText[0]->setText(text, true);
[5118]371  }
[5068]372}
373
374/**
[5783]375 * moves the Display buffer (up + or down - )
[5246]376 * @param lineCount the count by which to shift the InputBuffer.
[5783]377 *
378 * @todo make this work
[5246]379 */
380void Shell::moveDisplayBuffer(int lineCount)
381{
[5783]382  if (this->bufferOffset == 0)
383   {
384     this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->end();
385//     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
[5248]386//       this->bufferIterator->prevStep();
[5783]387   }
388
389  // boundraries
390  if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer()->size())
391    lineCount = (int)ShellBuffer::getInstance()->getBuffer()->size()- this->bufferOffset;
392  else if (this->bufferOffset + lineCount < 0)
393    lineCount = -bufferOffset;
394  this->bufferOffset += lineCount;
395
396  // moving the iterator to the right position
397  int move = 0;
398  while (move != lineCount)
399  {
400    if (move < lineCount)
401    {
402      ++move;
403      this->bufferIterator--;
404    }
405    else
406    {
407      --move;
408      this->bufferIterator++;
409    }
410  }
411  // redisplay the buffers
412  list<char*>::const_iterator it = this->bufferIterator;
413  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
414  {
415    this->bufferText[i]->setText((*it), false);
416    it--;
417  }
[5246]418}
419
420/**
[5166]421 * clears the Shell (empties all buffers)
422 */
[5130]423void Shell::clear()
424{
[5175]425  this->flush();
426  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
[5130]427}
428
[5069]429/**
430 * listens for some event
431 * @param event the Event happened
432 */
433void Shell::process(const Event &event)
434{
[5093]435  if (event.bPressed)
436  {
[5425]437    if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
[5093]438    {
[5388]439      if (this->bActive == false)
[5113]440        this->activate();
[5093]441      else
[5113]442        this->deactivate();
[5093]443    }
[5246]444    else if (event.type == SDLK_PAGEUP)
445    {
[5248]446      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
[5246]447    }
448    else if (event.type == SDLK_PAGEDOWN)
449    {
[5248]450      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
[5246]451    }
[5093]452  }
[5069]453}
454
[5068]455/**
456 * displays the Shell
457 */
458void Shell::draw() const
459{
[5099]460  glPushMatrix();
461  // transform for alignment.
462  // setting the Blending effects
463
[5372]464  this->backgroundMaterial->select();
[5099]465
[5158]466  glBegin(GL_TRIANGLE_STRIP);
[5099]467
[5158]468  glTexCoord2f(0, 0);
[5099]469  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
470
[5158]471  glTexCoord2f(1, 0);
[5113]472  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]473
[5158]474  glTexCoord2f(0, 1);
475  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
476
477  glTexCoord2f(1, 1);
[5113]478  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]479
480  glEnd();
[5068]481}
482
[5120]483///////////////////////
484// HELPER FUNCTIONS  //
485///////////////////////
[5166]486
487/**
488 * calculates the position of a Buffer-Display Line
489 * @param lineNumber the lineNumber from the bottom to calculate the position from
490 * @returns the Position of the Line.
491 */
[5120]492Vector Shell::calculateLinePosition(unsigned int lineNumber)
493{
[5420]494  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber - 2) + this->textSize, 0);
[5120]495}
496
497
498
[5113]499/**
[5068]500 * displays some nice output from the Shell
501 */
502void Shell::debug() const
503{
[5119]504  PRINT(3)("Debugging output to console (not this shell)\n");
505
[5180]506//   if (this->pressedKey != SDLK_FIRST)
507//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]508
509
[5177]510  ShellBuffer::getInstance()->debug();
[5068]511}
[5166]512
513// void Shell::testI (int i)
514// {
515//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
516// }
517//
518// void Shell::testS (const char* s)
519// {
520//   PRINTF(3)("This is the Test for one String '%s'\n", s);
521// }
522//
523// void Shell::testB (bool b)
524// {
525//   PRINTF(3)("This is the Test for one Bool: ");
526//   if (b)
527//     PRINTF(3)("true\n");
528//   else
529//     PRINTF(3)("false\n");
530// }
531//
532// void Shell::testF (float f)
533// {
534//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
535// }
536//
537// void Shell::testSF (const char* s, float f)
538// {
539//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
540// }
Note: See TracBrowser for help on using the repository browser.