Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: the Buffer is moving pretty smoothly :)

File size: 13.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"
[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
[7422]134    std::list<std::string>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer().end();
135    bool top = false;
[7737]136    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
[5787]137    {
[7422]138      (*text)->setVisibility(true);
139      if (!top)
140      {
141        (*text)->setText((*textLine));
142        if (textLine != ShellBuffer::getInstance()->getBuffer().begin())
143          top = true;
144        textLine--;
145      }
[5787]146    }
[7744]147    repositionText();
[5247]148  }
[5113]149
[7422]150  /**
151   * @brief deactiveates the Shell.
152   */
153  void Shell::deactivate()
154  {
155    if (this->bActive == false)
156      PRINTF(3)("The shell is already inactive\n");
157    this->bActive = false;
[5113]158
[7422]159    EventHandler::getInstance()->withUNICODE(false);
160    EventHandler::getInstance()->popState();
[5388]161
[7422]162    this->setRelCoorSoft2D(0, -(int)this->shellHeight, 5);
[5118]163
[7737]164    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
[7422]165      (*text)->setVisibility(false);
[7744]166    // Go to the End again.
167    this->bufferIterator = ShellBuffer::getInstance()->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
[7738]239  /**
240   * @brief repositiones all the Texts to their position.
241   */
242  void Shell::repositionText()
243  {
244    int linePos = -1;
245    std::list<MultiLineText*>::iterator textIt;
246    for (textIt = this->bufferText.begin() ; textIt != this->bufferText.end(); ++textIt )
247    {
248      linePos += (*textIt)->getLineCount();
249      (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(linePos), 8);
250    }
251  }
[5374]252
[7738]253
[7422]254  /**
[7738]255   * @brief applies the Shells Settings to a single Text of the Shell.
256   * @param text the Text to apply the settings to.
257   */
258  void Shell::applyTextSettings(Text* text)
259  {
[7742]260    text->setSize(this->textSize);
[7738]261    text->setFont(this->fontFile, this->textSize);
262    text->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
263    text->setBlending(this->textColor[3]);
264    text->setLayer(this->getLayer());
265    if (text->getParent2D() != this)
266      text->setParent2D(this);
267  }
268
269  /**
[7422]270   * @brief resets the Values of all visible shell's commandos to the Shell's stored values
271   *
272   * this functions synchronizes the stored Data with the visible one.
273   */
[7738]274  void Shell::applySettings()
[7422]275  {
[7738]276    this->applyTextSettings(&this->shellInput);
[7426]277    this->shellInput.setRelCoor2D(15, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
[5372]278
[7426]279    /* Here we create a Copy of the Buffer, so that we do not f**k up the List when some
280     * output is routed from Some other Thread or by Changing any Values.
281     */
[7737]282    std::list<MultiLineText*> bufferTextCopy = this->bufferText;
[7738]283    for (std::list<MultiLineText*>::iterator textIt = bufferTextCopy.begin(); textIt != bufferTextCopy.end(); ++textIt)
[7422]284    {
[7738]285      this->applyTextSettings(*textIt);
286      (*textIt)->setLineSpacing(this->lineSpacing);
287      (*textIt)->setLineWidth(this->getSizeX2D() * GraphicsEngine::getInstance()->getResolutionX());
[7422]288    }
[7738]289    this->repositionText();
290
[7422]291    this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5369]292  }
[5113]293
[7422]294  /**
295   * @brief sets The count of Lines to display in the buffer.
296   * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
297   */
298  void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
[5072]299  {
[7422]300    unsigned int oldSize = this->bufferText.size();
301    if (oldSize > bufferDisplaySize)
[7341]302    {
[7422]303      for (unsigned int i = bufferDisplaySize; i <= oldSize; i++)
304      {
305        delete this->bufferText.back();
306        this->bufferText.pop_back();
307      }
[7341]308    }
[7422]309    else if (oldSize < bufferDisplaySize)
310    {
311      for (unsigned int i = oldSize; i <= bufferDisplaySize; i++)
312      {
[7737]313        this->bufferText.push_back(new MultiLineText);
[7422]314      }
315    }
316    this->bufferDisplaySize = bufferDisplaySize;
[7738]317    this->applySettings();
[5072]318  }
[7422]319
320  /**
321   * @brief deletes all the Buffers
322   */
323  void Shell::flush()
[5072]324  {
[7738]325    for (std::list<MultiLineText*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
[5787]326    {
[7738]327      (*textIt)->setText("");  // remove all chars from the BufferTexts.
[5787]328    }
[7422]329    ShellBuffer::getInstance()->flush();
330    // BUFFER FLUSHING
[5072]331  }
[5068]332
[7422]333  /**
334   * @brief prints out some text to the input-buffers
335   * @param text the text to output.
336   */
337  void Shell::printToDisplayBuffer(const std::string& text)
[7341]338  {
[7737]339    // Remove Last Entry and prepend it to the front.
[7422]340    this->bufferText.push_front(this->bufferText.back());
341    this->bufferText.pop_back();
[5068]342
[7737]343    // Set the new Text.
344    this->bufferText.front()->setText(text);
[7341]345
[7737]346    // The LineCount will be started here.
347
348    // The First Line gets a special Animation
[7738]349    this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(0)- Vector2D(-1000,0));
[7737]350
351    // Move all lines one Entry up.
[7738]352    this->repositionText();
353  }
[7341]354
[5113]355
[7422]356  /**
[7738]357   * @brief moves the Display buffer (up + or down - )
[7422]358   * @param lineCount the count by which to shift the InputBuffer.
359   *
360   * @todo make this work
361   */
362  void Shell::moveDisplayBuffer(int lineCount)
[5783]363  {
[7744]364    // moving the iterator to the right position (counting the steps)
365    int moves = 0;
366    while (moves != lineCount)
[5783]367    {
[7744]368      if (moves < lineCount)
[7422]369      {
[7744]370        if(this->bufferIterator == --ShellBuffer::getInstance()->getBuffer().end())
371          break;
372        ++moves;
373        ++this->bufferIterator;
[7422]374      }
375      else
376      {
[7744]377        if (this->bufferIterator == ShellBuffer::getInstance()->getBuffer().begin())
378          break;
379        --moves;
380        --this->bufferIterator;
[7422]381      }
[5783]382    }
[7744]383
384
[7422]385    // redisplay the buffers
[7744]386    int linePos = moves;
[7422]387    std::list<std::string>::const_iterator it = this->bufferIterator;
[7744]388    std::list<MultiLineText*>::iterator textIt;
389    for (textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt, ++it)
[7342]390    {
[7744]391      if (it == ShellBuffer::getInstance()->getBuffer().end())
[7422]392      {
[7744]393        PRINTF(1)("LAST LINE REACHED\n");
[7422]394        break;
395      }
[7744]396
397
398      (*textIt)->setRelCoor2D(calculateLinePosition( (linePos++ > 0)? linePos : 0));
399      (*textIt)->setText((*it));
[7422]400    }
[7744]401    while (textIt != this->bufferText.end())
402    {
403      (*textIt)->setText("");
404      textIt++;
405    }
406
407    this->repositionText();
[5783]408  }
[5246]409
[7422]410  /**
[7738]411   * @brief clears the Shell (empties all buffers)
[7422]412   */
413  void Shell::clear()
414  {
415    this->flush();
416    ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
417  }
[5130]418
[7422]419  /**
[7738]420   * @brief listens for some event
[7422]421   * @param event the Event happened
422   */
423  void Shell::process(const Event &event)
[5093]424  {
[7422]425    if (event.bPressed)
[5093]426    {
[7422]427      if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
428      {
429        if (this->bActive == false)
430          this->activate();
431        else
432          this->deactivate();
433      }
434      else if (event.type == SDLK_PAGEUP)
435      {
436        this->moveDisplayBuffer(+this->bufferDisplaySize-1);
437      }
438      else if (event.type == SDLK_PAGEDOWN)
439      {
440        this->moveDisplayBuffer(-this->bufferDisplaySize+1);
441      }
[5093]442    }
443  }
[5069]444
[7422]445  /**
446   * displays the Shell
447   */
448  void Shell::draw() const
449  {
450    // transform for alignment.
451    // setting the Blending effects
[5099]452
[7738]453    this->backgroundMaterial.select();
[5099]454
[7422]455    glBegin(GL_TRIANGLE_STRIP);
[5099]456
[7422]457    glTexCoord2f(0, 0);
458    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
[5099]459
[7422]460    glTexCoord2f(1, 0);
461    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]462
[7422]463    glTexCoord2f(0, 1);
464    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5158]465
[7422]466    glTexCoord2f(1, 1);
467    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]468
[7422]469    glEnd();
470  }
[5068]471
[7422]472  ///////////////////////
473  // HELPER FUNCTIONS  //
474  ///////////////////////
[5166]475
[7422]476  /**
477   * @brief calculates the position of a Buffer-Display Line
478   * @param lineNumber the lineNumber from the bottom to calculate the position from
479   * @returns the Position of the Line.
480   */
481  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
482  {
[7738]483    return Vector2D(5, (int)(this->textSize + this->lineSpacing)*(int)((int)this->bufferDisplaySize - (int)lineNumber - (int)2) + (int)this->textSize);
[7422]484  }
[5120]485
486
487
[7422]488  /**
489   * @brief displays some nice output from the Shell
490   */
491  void Shell::debug() const
492  {
493    PRINT(3)("Debugging output to console (not this shell)\n");
[5119]494
[7422]495    //   if (this->pressedKey != SDLK_FIRST)
496    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]497
498
[7422]499    ShellBuffer::getInstance()->debug();
500  }
[7374]501}
Note: See TracBrowser for help on using the repository browser.