Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8383 was 8362, checked in by bensch, 18 years ago

orxonox/trunk: removed stupid included in base_object.h
this should lead to faster compile-times

File size: 15.9 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
[7374]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL
[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
[7737]23#include "multi_line_text.h"
[5175]24
[5093]25#include "graphics_engine.h"
[5372]26#include "material.h"
[5093]27#include "event_handler.h"
[8362]28#include "debug.h"
[5113]29
[7374]30namespace OrxShell
31{
[1853]32
[7422]33  SHELL_COMMAND(clear, Shell, clear)
34  ->describe("Clears the shell from unwanted lines (empties all buffers)")
35  ->setAlias("clear");
36  SHELL_COMMAND(deactivate, Shell, deactivate)
37  ->describe("Deactivates the Shell. (moves it into background)")
38  ->setAlias("hide");
39  SHELL_COMMAND(textsize, Shell, setTextSize)
40  ->describe("Sets the size of the Text size, linespacing")
41  ->defaultValues(15, 0);
42  SHELL_COMMAND(textcolor, Shell, setTextColor)
43  ->describe("Sets the Color of the Shells Text (red, green, blue, alpha)")
44  ->defaultValues(SHELL_DEFAULT_TEXT_COLOR);
45  SHELL_COMMAND(backgroundcolor, Shell, setBackgroundColor)
46  ->describe("Sets the Color of the Shells Background (red, green, blue, alpha)")
47  ->defaultValues(SHELL_DEFAULT_BACKGROUND_COLOR);
48  SHELL_COMMAND(backgroundimage, Shell, setBackgroundImage)
49  ->describe("sets the background image to load for the Shell")
50  ->completionPlugin(0, OrxShell::CompletorFileSystem());
51  SHELL_COMMAND(font, Shell, setFont)
52  ->describe("Sets the font of the Shell")
53  ->defaultValues(SHELL_DEFAULT_FONT)
[7738]54  ->completionPlugin(0, OrxShell::CompletorFileSystem(".ttf", "fonts/"));
[1856]55
[7744]56
[7422]57  /**
58   * standard constructor
59   */
60  Shell::Shell ()
61  {
62    this->setClassID(CL_SHELL, "Shell");
63    this->setName("Shell");
[5072]64
[7762]65    this->shellBuffer = ShellBuffer::getInstance();
66
[7422]67    // EVENT-Handler subscription of '`' to all States.
[7868]68    this->subscribeEvent(ES_ALL, SDLK_BACKQUOTE);
69    this->subscribeEvent(ES_ALL, SDLK_F12);
70    this->subscribeEvent(ES_SHELL, SDLK_PAGEUP);
71    this->subscribeEvent(ES_SHELL, SDLK_PAGEDOWN);
72    this->subscribeEvent(ES_SHELL, EV_VIDEO_RESIZE);
[7919]73    EventHandler::getInstance()->withUNICODE(ES_SHELL, true);
[5206]74
[7422]75    // BUFFER
[7762]76    this->bufferIterator = this->shellBuffer->getBuffer().begin();
[5372]77
[7422]78    // INPUT LINE
79    this->setLayer(E2D_LAYER_ABOVE_ALL);
80    this->shellInput.setLayer(E2D_LAYER_ABOVE_ALL);
[5372]81
[7422]82    // Element2D and generals
[7757]83    this->setSizeX2D(GraphicsEngine::getInstance()->getResolutionX());
[7422]84    this->setAbsCoor2D(3, -400);
[7426]85    this->textSize = 15;
[7422]86    this->lineSpacing = 0;
[7919]87    this->bActive = false;
[7422]88    this->fontFile = SHELL_DEFAULT_FONT;
[5072]89
[7422]90    this->setBufferDisplaySize(10);
[5080]91
[7422]92    this->setTextColor(SHELL_DEFAULT_TEXT_COLOR);
93    this->setBackgroundColor(SHELL_DEFAULT_BACKGROUND_COLOR);
[5335]94
[7341]95
[7422]96    this->deactivate();
97  }
[4320]98
[7422]99  /**
100   * @brief standard deconstructor
101   */
102  Shell::~Shell ()
103  {
104    // delete the displayable Buffers
105    while (!this->bufferText.empty())
106    {
107      delete this->bufferText.front();
108      this->bufferText.pop_front();
109    }
[7341]110  }
111
[7422]112  /**
113   * @brief activates the shell
114   *
115   * This also feeds the Last few lines from the main buffers into the displayBuffer
116   */
117  void Shell::activate()
118  {
119    if (this->bActive == true)
120      PRINTF(3)("The shell is already active\n");
121    this->bActive = true;
[7762]122    this->activate2D();
[5068]123
[7422]124    EventHandler::getInstance()->pushState(ES_SHELL);
[5113]125
[7422]126    this->setRelCoorSoft2D(0, 0, 5);
[5786]127
[7762]128    this->linesProcessed = this->shellBuffer->getLineCount();
129    std::list<std::string>::const_iterator textLine = this->bufferIterator = this->shellBuffer->getBuffer().begin();
[7737]130    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
[5787]131    {
[7422]132      (*text)->setVisibility(true);
[7762]133      if (textLine !=  this->shellBuffer->getBuffer().end())
[7422]134      {
135        (*text)->setText((*textLine));
[7746]136        textLine++;
[7422]137      }
[7746]138      else
139        (*text)->setText("");
[5787]140    }
[7919]141    this->shellInput.setVisibility(true);
[7757]142    this->updateResolution( GraphicsEngine::getInstance()->getResolutionX());
143    this->repositionText();
[5247]144  }
[5113]145
[7422]146  /**
147   * @brief deactiveates the Shell.
148   */
149  void Shell::deactivate()
150  {
151    if (this->bActive == false)
152      PRINTF(3)("The shell is already inactive\n");
153    this->bActive = false;
[7762]154    this->deactivate2D();
[5113]155
[7422]156    EventHandler::getInstance()->popState();
[5388]157
[7422]158    this->setRelCoorSoft2D(0, -(int)this->shellHeight, 5);
[5118]159
[7737]160    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
[7422]161      (*text)->setVisibility(false);
[7919]162    this->shellInput.setVisibility(false);
163
[7744]164    // Go to the End again.
[7762]165    this->bufferIterator = this->shellBuffer->getBuffer().begin();
[7422]166  }
[5113]167
[7422]168  /**
169   * @brief sets the File to load the fonts from
170   * @param fontFile the file to load the font from
171   *
172   * it is quite important, that the font pointed too really exists!
173   * (be aware that within orxonox fontFile is relative to the Data-Dir)
174   */
175  void Shell::setFont(const std::string& fontFile)
176  {
177    this->fontFile = fontFile;
[5251]178
[7738]179    this->applySettings();
[7422]180  }
[5251]181
[7422]182  /**
183   * @brief sets the size of the text and spacing
184   * @param textSize the size of the Text in Pixels
185   * @param lineSpacing the size of the Spacing between two lines in pixels
186   *
187   * this also rebuilds the entire Text, inputLine and displayBuffer,
188   * to be accurate again.
189   */
190  void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
191  {
192    this->textSize = textSize;
193    this->lineSpacing = lineSpacing;
[7341]194
[7738]195    this->applySettings();
[7422]196  }
[5113]197
[7422]198  /**
199   * @brief sets the color of the Font.
200   * @param r: red
201   * @param g: green
202   * @param b: blue
203   * @param a: alpha-value.
204   */
205  void Shell::setTextColor(float r, float g, float b, float a)
206  {
207    this->textColor[0] = r;
208    this->textColor[1] = g;
209    this->textColor[2] = b;
210    this->textColor[3] = a;
[5372]211
[7738]212    this->applySettings();
[7422]213  }
[5372]214
[7422]215  /**
216   * @brief sets the color of the Backgrond.
217   * @param r: red
218   * @param g: green
219   * @param b: blue
220   * @param a: alpha-value.
221   */
222  void Shell::setBackgroundColor(float r, float g, float b, float a)
223  {
[7738]224    this->backgroundMaterial.setDiffuse(r, g, b);
225    this->backgroundMaterial.setTransparency(a);
[7422]226  }
[5372]227
[7422]228  /**
229   * @brief sets a nice background image to the Shell's background
230   * @param fileName the filename of the Image to load
231   */
232  void Shell::setBackgroundImage(const std::string& fileName)
233  {
[7738]234    this->backgroundMaterial.setDiffuseMap(fileName);
[7422]235  }
[5374]236
[7757]237
[7738]238  /**
[7757]239   * @brief updates the Shell's Width
240   * @param width the new Width.
241   */
242  void Shell::updateResolution(unsigned int width)
243  {
244    if (width == this->getSizeX2D())
245      return;
246    this->setSizeX2D(width);
247    for (std::list<MultiLineText*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
248    {
249      (*textIt)->setLineWidth(width);
250    }
251  }
252
253  /**
[7738]254   * @brief repositiones all the Texts to their position.
255   */
256  void Shell::repositionText()
257  {
258    int linePos = -1;
259    std::list<MultiLineText*>::iterator textIt;
260    for (textIt = this->bufferText.begin() ; textIt != this->bufferText.end(); ++textIt )
261    {
262      linePos += (*textIt)->getLineCount();
263      (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(linePos), 8);
264    }
265  }
[5374]266
[7738]267
[7422]268  /**
[7738]269   * @brief applies the Shells Settings to a single Text of the Shell.
270   * @param text the Text to apply the settings to.
271   */
272  void Shell::applyTextSettings(Text* text)
273  {
[7742]274    text->setSize(this->textSize);
[7738]275    text->setFont(this->fontFile, this->textSize);
276    text->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
277    text->setBlending(this->textColor[3]);
278    text->setLayer(this->getLayer());
279    if (text->getParent2D() != this)
280      text->setParent2D(this);
281  }
282
283  /**
[7422]284   * @brief resets the Values of all visible shell's commandos to the Shell's stored values
285   *
286   * this functions synchronizes the stored Data with the visible one.
287   */
[7738]288  void Shell::applySettings()
[7422]289  {
[7738]290    this->applyTextSettings(&this->shellInput);
[7426]291    this->shellInput.setRelCoor2D(15, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
[5372]292
[7426]293    /* Here we create a Copy of the Buffer, so that we do not f**k up the List when some
294     * output is routed from Some other Thread or by Changing any Values.
295     */
[7737]296    std::list<MultiLineText*> bufferTextCopy = this->bufferText;
[7738]297    for (std::list<MultiLineText*>::iterator textIt = bufferTextCopy.begin(); textIt != bufferTextCopy.end(); ++textIt)
[7422]298    {
[7738]299      this->applyTextSettings(*textIt);
300      (*textIt)->setLineSpacing(this->lineSpacing);
301      (*textIt)->setLineWidth(this->getSizeX2D() * GraphicsEngine::getInstance()->getResolutionX());
[7422]302    }
[7738]303    this->repositionText();
304
[7422]305    this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5369]306  }
[5113]307
[7422]308  /**
309   * @brief sets The count of Lines to display in the buffer.
310   * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
311   */
312  void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
[5072]313  {
[7422]314    unsigned int oldSize = this->bufferText.size();
315    if (oldSize > bufferDisplaySize)
[7341]316    {
[7422]317      for (unsigned int i = bufferDisplaySize; i <= oldSize; i++)
318      {
319        delete this->bufferText.back();
320        this->bufferText.pop_back();
321      }
[7341]322    }
[7422]323    else if (oldSize < bufferDisplaySize)
324    {
325      for (unsigned int i = oldSize; i <= bufferDisplaySize; i++)
326      {
[7737]327        this->bufferText.push_back(new MultiLineText);
[7422]328      }
329    }
330    this->bufferDisplaySize = bufferDisplaySize;
[7738]331    this->applySettings();
[5072]332  }
[7422]333
334  /**
335   * @brief deletes all the Buffers
336   */
337  void Shell::flush()
[5072]338  {
[7738]339    for (std::list<MultiLineText*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
[5787]340    {
[7738]341      (*textIt)->setText("");  // remove all chars from the BufferTexts.
[5787]342    }
[7762]343    this->shellBuffer->flush();
[7422]344    // BUFFER FLUSHING
[5072]345  }
[5068]346
[7422]347  /**
348   * @brief prints out some text to the input-buffers
349   * @param text the text to output.
350   */
351  void Shell::printToDisplayBuffer(const std::string& text)
[7341]352  {
[7737]353    // Remove Last Entry and prepend it to the front.
[7422]354    this->bufferText.push_front(this->bufferText.back());
355    this->bufferText.pop_back();
[5068]356
[7737]357    // Set the new Text.
358    this->bufferText.front()->setText(text);
[7762]359    this->bufferIterator = this->shellBuffer->getBuffer().begin();
[7341]360
[7737]361    // The LineCount will be started here.
362
363    // The First Line gets a special Animation
[7738]364    this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(0)- Vector2D(-1000,0));
[7737]365
366    // Move all lines one Entry up.
[7738]367    this->repositionText();
368  }
[7341]369
[5113]370
[7422]371  /**
[7738]372   * @brief moves the Display buffer (up + or down - )
[7422]373   * @param lineCount the count by which to shift the InputBuffer.
374   *
375   * @todo make this work
376   */
377  void Shell::moveDisplayBuffer(int lineCount)
[5783]378  {
[7744]379    // moving the iterator to the right position (counting the steps)
380    int moves = 0;
381    while (moves != lineCount)
[5783]382    {
[7744]383      if (moves < lineCount)
[7422]384      {
[7762]385        if(this->bufferIterator == --this->shellBuffer->getBuffer().end())
[7744]386          break;
387        ++moves;
388        ++this->bufferIterator;
[7422]389      }
390      else
391      {
[7762]392        if (this->bufferIterator == this->shellBuffer->getBuffer().begin())
[7744]393          break;
394        --moves;
395        --this->bufferIterator;
[7422]396      }
[5783]397    }
[7744]398
399
[7422]400    // redisplay the buffers
[7745]401    std::list<MultiLineText*>::iterator textIt;
402
403    // Give a Graphical Representation that no move is possible.
404    if (moves == 0)
405    {
406      int linePos = -1;
407      for (textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
408      {
409        linePos += (*textIt)->getLineCount();
410        (*textIt)->setRelCoor2D(this->calculateLinePosition(linePos)+ Vector2D(20,0));
411        (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(linePos), 10);
412      }
413      return;
414    }
415
416    // Move all the Lines.
[7744]417    int linePos = moves;
[7422]418    std::list<std::string>::const_iterator it = this->bufferIterator;
[7744]419    for (textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt, ++it)
[7342]420    {
[7762]421      if (it == this->shellBuffer->getBuffer().end())
[7422]422      {
[7744]423        PRINTF(1)("LAST LINE REACHED\n");
[7422]424        break;
425      }
[7744]426
427
[7757]428      (*textIt)->setRelCoor2D(calculateLinePosition( (linePos++ > 0) ? linePos : 0));
[7744]429      (*textIt)->setText((*it));
[7422]430    }
[7744]431    while (textIt != this->bufferText.end())
432    {
433      (*textIt)->setText("");
434      textIt++;
435    }
436
437    this->repositionText();
[5783]438  }
[5246]439
[7422]440  /**
[7738]441   * @brief clears the Shell (empties all buffers)
[7422]442   */
443  void Shell::clear()
444  {
445    this->flush();
446    ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
447  }
[5130]448
[7422]449  /**
[7738]450   * @brief listens for some event
[7422]451   * @param event the Event happened
452   */
453  void Shell::process(const Event &event)
[5093]454  {
[7422]455    if (event.bPressed)
[5093]456    {
[7422]457      if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
458      {
459        if (this->bActive == false)
460          this->activate();
461        else
462          this->deactivate();
463      }
464      else if (event.type == SDLK_PAGEUP)
465      {
466        this->moveDisplayBuffer(+this->bufferDisplaySize-1);
467      }
468      else if (event.type == SDLK_PAGEDOWN)
469      {
470        this->moveDisplayBuffer(-this->bufferDisplaySize+1);
471      }
[7757]472      else if (event.type == EV_VIDEO_RESIZE)
473      {
474        this->updateResolution(event.resize.w);
475        this->repositionText();
476      }
[5093]477    }
478  }
[5069]479
[7762]480  void Shell::tick(float dt)
481  {
482    if (this->linesProcessed < this->shellBuffer->getLineCount())
483    {
484      unsigned int pollLines = this->shellBuffer->getLineCount() - this->linesProcessed;
485      if (this->bufferText.size() < pollLines)
486        pollLines = this->bufferText.size();
487      if (unlikely(this->shellBuffer->getBuffer().size() < pollLines))
488        pollLines = this->shellBuffer->getBuffer().size();
489
490      std::list<std::string>::const_iterator line = this->shellBuffer->getBuffer().begin();
491      unsigned int i;
492      for(i = 0; i < pollLines; i++)
493        line ++;
494      for (i = 0; i < pollLines; i++)
495      {
496        this->printToDisplayBuffer((*--line));
497      }
498    }
499    this->linesProcessed = this->shellBuffer->getLineCount();
500  }
501
502
[7422]503  /**
504   * displays the Shell
505   */
506  void Shell::draw() const
507  {
508    // transform for alignment.
509    // setting the Blending effects
[5099]510
[7738]511    this->backgroundMaterial.select();
[5099]512
[7422]513    glBegin(GL_TRIANGLE_STRIP);
[5099]514
[7422]515    glTexCoord2f(0, 0);
516    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
[5099]517
[7422]518    glTexCoord2f(1, 0);
519    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]520
[7422]521    glTexCoord2f(0, 1);
522    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5158]523
[7422]524    glTexCoord2f(1, 1);
525    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]526
[7422]527    glEnd();
528  }
[5068]529
[7422]530  ///////////////////////
531  // HELPER FUNCTIONS  //
532  ///////////////////////
[5166]533
[7422]534  /**
535   * @brief calculates the position of a Buffer-Display Line
536   * @param lineNumber the lineNumber from the bottom to calculate the position from
537   * @returns the Position of the Line.
538   */
539  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
540  {
[7757]541    return Vector2D(5.0f, (float)(this->textSize + this->lineSpacing)*(float)((int)this->bufferDisplaySize - (int)lineNumber - (int)1));
[7422]542  }
[5120]543
544
545
[7422]546  /**
547   * @brief displays some nice output from the Shell
548   */
549  void Shell::debug() const
550  {
551    PRINT(3)("Debugging output to console (not this shell)\n");
[5119]552
[7422]553    //   if (this->pressedKey != SDLK_FIRST)
554    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]555
556
[7762]557    this->shellBuffer->debug();
[7422]558  }
[7750]559
560  /**
561   * @brief a Handy Function, to Test the behaviour of the Shell.
562   */
563  void Shell::testShell() const
564  {
565    for (unsigned int i = 0; i < 100; i++)
566      PRINT(0)("%d\n", i);
567  }
568  SHELL_COMMAND(test, Shell, testShell);
569
[7374]570}
Note: See TracBrowser for help on using the repository browser.