Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8005 was 7919, checked in by bensch, 18 years ago

orxonox/trunk: merged the gui branche back
merged with command:
https://svn.orxonox.net/orxonox/branches/gui
no conflicts

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