Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/shell/shell.cc @ 7897

Last change on this file since 7897 was 7895, checked in by bensch, 19 years ago

orxonox/trunk: fixed a bug in the EventHandler, that resulted from multiple inputs or so…

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