Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7749 was 7746, checked in by bensch, 19 years ago

orxonox/trunk: fixed the last few bugs (that i found) when scrolling

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