Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/shell/shell.cc @ 9734

Last change on this file since 9734 was 9715, checked in by bensch, 18 years ago

renamed newclassid to classid and newobjectlist to objectlist

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