Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7909 was 7868, checked in by bensch, 18 years ago

trunk: merge Check in the Event-changes:
r7867 | bensch | 2006-05-26 13:19:46 +0200 (Fri, 26 May 2006) | 1 line

Events better subscribed


r7866 | bensch | 2006-05-26 13:11:10 +0200 (Fri, 26 May 2006) | 1 line

Events are subscribed at the EventListener, and not the EventHandler

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