Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: some movements

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