Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9973 was 9916, checked in by bensch, 18 years ago

orxonox/trunk: Windows runs again without any segfaults
it seems, that windows links object files differently… and extremely strange

File size: 14.5 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"
[9869]20#include "debug_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{
[9869]32  ObjectListDefinition(Shell);
[7744]33
[7422]34  /**
35   * standard constructor
36   */
37  Shell::Shell ()
38  {
[9869]39    this->registerObject(this, Shell::_objectList);
40
[7422]41    this->setName("Shell");
[5072]42
[9869]43    this->shellBuffer = DebugBuffer::getInstance();
[7762]44
[7422]45    // EVENT-Handler subscription of '`' to all States.
[7868]46    this->subscribeEvent(ES_ALL, SDLK_BACKQUOTE);
47    this->subscribeEvent(ES_ALL, SDLK_F12);
48    this->subscribeEvent(ES_SHELL, SDLK_PAGEUP);
49    this->subscribeEvent(ES_SHELL, SDLK_PAGEDOWN);
50    this->subscribeEvent(ES_SHELL, EV_VIDEO_RESIZE);
[7919]51    EventHandler::getInstance()->withUNICODE(ES_SHELL, true);
[5206]52
[7422]53    // BUFFER
[7762]54    this->bufferIterator = this->shellBuffer->getBuffer().begin();
[5372]55
[7422]56    // INPUT LINE
57    this->setLayer(E2D_LAYER_ABOVE_ALL);
58    this->shellInput.setLayer(E2D_LAYER_ABOVE_ALL);
[5372]59
[7422]60    // Element2D and generals
[7757]61    this->setSizeX2D(GraphicsEngine::getInstance()->getResolutionX());
[7422]62    this->setAbsCoor2D(3, -400);
[7426]63    this->textSize = 15;
[7422]64    this->lineSpacing = 0;
[7919]65    this->bActive = false;
[7422]66    this->fontFile = SHELL_DEFAULT_FONT;
[5072]67
[7422]68    this->setBufferDisplaySize(10);
[5080]69
[7422]70    this->setTextColor(SHELL_DEFAULT_TEXT_COLOR);
71    this->setBackgroundColor(SHELL_DEFAULT_BACKGROUND_COLOR);
[5335]72
[7341]73
[7422]74    this->deactivate();
75  }
[4320]76
[7422]77  /**
78   * @brief standard deconstructor
79   */
80  Shell::~Shell ()
81  {
82    // delete the displayable Buffers
83    while (!this->bufferText.empty())
84    {
85      delete this->bufferText.front();
86      this->bufferText.pop_front();
87    }
[7341]88  }
89
[7422]90  /**
91   * @brief activates the shell
92   *
93   * This also feeds the Last few lines from the main buffers into the displayBuffer
94   */
95  void Shell::activate()
96  {
97    if (this->bActive == true)
98      PRINTF(3)("The shell is already active\n");
99    this->bActive = true;
[7762]100    this->activate2D();
[5068]101
[7422]102    EventHandler::getInstance()->pushState(ES_SHELL);
[5113]103
[7422]104    this->setRelCoorSoft2D(0, 0, 5);
[5786]105
[7762]106    this->linesProcessed = this->shellBuffer->getLineCount();
107    std::list<std::string>::const_iterator textLine = this->bufferIterator = this->shellBuffer->getBuffer().begin();
[7737]108    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
[5787]109    {
[7422]110      (*text)->setVisibility(true);
[7762]111      if (textLine !=  this->shellBuffer->getBuffer().end())
[7422]112      {
113        (*text)->setText((*textLine));
[7746]114        textLine++;
[7422]115      }
[7746]116      else
117        (*text)->setText("");
[5787]118    }
[7919]119    this->shellInput.setVisibility(true);
[7757]120    this->updateResolution( GraphicsEngine::getInstance()->getResolutionX());
121    this->repositionText();
[5247]122  }
[5113]123
[7422]124  /**
125   * @brief deactiveates the Shell.
126   */
127  void Shell::deactivate()
128  {
129    if (this->bActive == false)
130      PRINTF(3)("The shell is already inactive\n");
131    this->bActive = false;
[7762]132    this->deactivate2D();
[5113]133
[7422]134    EventHandler::getInstance()->popState();
[5388]135
[7422]136    this->setRelCoorSoft2D(0, -(int)this->shellHeight, 5);
[5118]137
[7737]138    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
[7422]139      (*text)->setVisibility(false);
[7919]140    this->shellInput.setVisibility(false);
141
[7744]142    // Go to the End again.
[7762]143    this->bufferIterator = this->shellBuffer->getBuffer().begin();
[7422]144  }
[5113]145
[7422]146  /**
147   * @brief sets the File to load the fonts from
148   * @param fontFile the file to load the font from
149   *
150   * it is quite important, that the font pointed too really exists!
151   * (be aware that within orxonox fontFile is relative to the Data-Dir)
152   */
153  void Shell::setFont(const std::string& fontFile)
154  {
155    this->fontFile = fontFile;
[5251]156
[7738]157    this->applySettings();
[7422]158  }
[5251]159
[7422]160  /**
161   * @brief sets the size of the text and spacing
162   * @param textSize the size of the Text in Pixels
163   * @param lineSpacing the size of the Spacing between two lines in pixels
164   *
165   * this also rebuilds the entire Text, inputLine and displayBuffer,
166   * to be accurate again.
167   */
168  void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
169  {
170    this->textSize = textSize;
171    this->lineSpacing = lineSpacing;
[7341]172
[7738]173    this->applySettings();
[7422]174  }
[5113]175
[7422]176  /**
177   * @brief sets the color of the Font.
178   * @param r: red
179   * @param g: green
180   * @param b: blue
181   * @param a: alpha-value.
182   */
183  void Shell::setTextColor(float r, float g, float b, float a)
184  {
185    this->textColor[0] = r;
186    this->textColor[1] = g;
187    this->textColor[2] = b;
188    this->textColor[3] = a;
[5372]189
[7738]190    this->applySettings();
[7422]191  }
[5372]192
[7422]193  /**
194   * @brief sets the color of the Backgrond.
195   * @param r: red
196   * @param g: green
197   * @param b: blue
198   * @param a: alpha-value.
199   */
200  void Shell::setBackgroundColor(float r, float g, float b, float a)
201  {
[7738]202    this->backgroundMaterial.setDiffuse(r, g, b);
203    this->backgroundMaterial.setTransparency(a);
[7422]204  }
[5372]205
[7422]206  /**
207   * @brief sets a nice background image to the Shell's background
208   * @param fileName the filename of the Image to load
209   */
210  void Shell::setBackgroundImage(const std::string& fileName)
211  {
[7738]212    this->backgroundMaterial.setDiffuseMap(fileName);
[7422]213  }
[5374]214
[7757]215
[7738]216  /**
[7757]217   * @brief updates the Shell's Width
218   * @param width the new Width.
219   */
220  void Shell::updateResolution(unsigned int width)
221  {
222    if (width == this->getSizeX2D())
223      return;
224    this->setSizeX2D(width);
225    for (std::list<MultiLineText*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
226      (*textIt)->setLineWidth(width);
227  }
228
229  /**
[7738]230   * @brief repositiones all the Texts to their position.
231   */
232  void Shell::repositionText()
233  {
234    int linePos = -1;
235    std::list<MultiLineText*>::iterator textIt;
236    for (textIt = this->bufferText.begin() ; textIt != this->bufferText.end(); ++textIt )
237    {
238      linePos += (*textIt)->getLineCount();
239      (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(linePos), 8);
240    }
241  }
[5374]242
[7738]243
[7422]244  /**
[7738]245   * @brief applies the Shells Settings to a single Text of the Shell.
246   * @param text the Text to apply the settings to.
247   */
248  void Shell::applyTextSettings(Text* text)
249  {
[7742]250    text->setSize(this->textSize);
[7738]251    text->setFont(this->fontFile, this->textSize);
252    text->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
253    text->setBlending(this->textColor[3]);
254    text->setLayer(this->getLayer());
255    if (text->getParent2D() != this)
256      text->setParent2D(this);
257  }
258
259  /**
[7422]260   * @brief resets the Values of all visible shell's commandos to the Shell's stored values
261   *
262   * this functions synchronizes the stored Data with the visible one.
263   */
[7738]264  void Shell::applySettings()
[7422]265  {
[7738]266    this->applyTextSettings(&this->shellInput);
[7426]267    this->shellInput.setRelCoor2D(15, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
[5372]268
[7426]269    /* Here we create a Copy of the Buffer, so that we do not f**k up the List when some
270     * output is routed from Some other Thread or by Changing any Values.
271     */
[7737]272    std::list<MultiLineText*> bufferTextCopy = this->bufferText;
[7738]273    for (std::list<MultiLineText*>::iterator textIt = bufferTextCopy.begin(); textIt != bufferTextCopy.end(); ++textIt)
[7422]274    {
[7738]275      this->applyTextSettings(*textIt);
276      (*textIt)->setLineSpacing(this->lineSpacing);
277      (*textIt)->setLineWidth(this->getSizeX2D() * GraphicsEngine::getInstance()->getResolutionX());
[7422]278    }
[7738]279    this->repositionText();
280
[7422]281    this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5369]282  }
[5113]283
[7422]284  /**
285   * @brief sets The count of Lines to display in the buffer.
286   * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
287   */
288  void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
[5072]289  {
[7422]290    unsigned int oldSize = this->bufferText.size();
291    if (oldSize > bufferDisplaySize)
[7341]292    {
[7422]293      for (unsigned int i = bufferDisplaySize; i <= oldSize; i++)
294      {
295        delete this->bufferText.back();
296        this->bufferText.pop_back();
297      }
[7341]298    }
[7422]299    else if (oldSize < bufferDisplaySize)
300    {
301      for (unsigned int i = oldSize; i <= bufferDisplaySize; i++)
302      {
[7737]303        this->bufferText.push_back(new MultiLineText);
[7422]304      }
305    }
306    this->bufferDisplaySize = bufferDisplaySize;
[7738]307    this->applySettings();
[5072]308  }
[7422]309
310  /**
311   * @brief deletes all the Buffers
312   */
313  void Shell::flush()
[5072]314  {
[7738]315    for (std::list<MultiLineText*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
[5787]316    {
[7738]317      (*textIt)->setText("");  // remove all chars from the BufferTexts.
[5787]318    }
[7762]319    this->shellBuffer->flush();
[7422]320    // BUFFER FLUSHING
[5072]321  }
[5068]322
[7422]323  /**
324   * @brief prints out some text to the input-buffers
325   * @param text the text to output.
326   */
327  void Shell::printToDisplayBuffer(const std::string& text)
[7341]328  {
[7737]329    // Remove Last Entry and prepend it to the front.
[7422]330    this->bufferText.push_front(this->bufferText.back());
331    this->bufferText.pop_back();
[5068]332
[7737]333    // Set the new Text.
334    this->bufferText.front()->setText(text);
[7762]335    this->bufferIterator = this->shellBuffer->getBuffer().begin();
[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      {
[7762]361        if(this->bufferIterator == --this->shellBuffer->getBuffer().end())
[7744]362          break;
363        ++moves;
364        ++this->bufferIterator;
[7422]365      }
366      else
367      {
[7762]368        if (this->bufferIterator == this->shellBuffer->getBuffer().begin())
[7744]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    {
[7762]397      if (it == this->shellBuffer->getBuffer().end())
[7422]398      {
[7744]399        PRINTF(1)("LAST LINE REACHED\n");
[7422]400        break;
401      }
[7744]402
403
[7757]404      (*textIt)->setRelCoor2D(calculateLinePosition( (linePos++ > 0) ? linePos : 0));
[7744]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();
[9869]422    DebugBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
[7422]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    }
[9869]449    else if (event.type == EV_VIDEO_RESIZE)
450    {
451      this->updateResolution(event.resize.w);
452      this->repositionText();
453    }
[5093]454  }
[5069]455
[7762]456  void Shell::tick(float dt)
457  {
458    if (this->linesProcessed < this->shellBuffer->getLineCount())
459    {
460      unsigned int pollLines = this->shellBuffer->getLineCount() - this->linesProcessed;
461      if (this->bufferText.size() < pollLines)
462        pollLines = this->bufferText.size();
463      if (unlikely(this->shellBuffer->getBuffer().size() < pollLines))
464        pollLines = this->shellBuffer->getBuffer().size();
465
466      std::list<std::string>::const_iterator line = this->shellBuffer->getBuffer().begin();
467      unsigned int i;
468      for(i = 0; i < pollLines; i++)
469        line ++;
470      for (i = 0; i < pollLines; i++)
471      {
472        this->printToDisplayBuffer((*--line));
473      }
474    }
475    this->linesProcessed = this->shellBuffer->getLineCount();
476  }
477
478
[7422]479  /**
480   * displays the Shell
481   */
482  void Shell::draw() const
483  {
484    // transform for alignment.
485    // setting the Blending effects
[5099]486
[7738]487    this->backgroundMaterial.select();
[5099]488
[7422]489    glBegin(GL_TRIANGLE_STRIP);
[5099]490
[7422]491    glTexCoord2f(0, 0);
492    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
[5099]493
[7422]494    glTexCoord2f(1, 0);
495    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]496
[7422]497    glTexCoord2f(0, 1);
498    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5158]499
[7422]500    glTexCoord2f(1, 1);
501    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]502
[7422]503    glEnd();
504  }
[5068]505
[7422]506  ///////////////////////
507  // HELPER FUNCTIONS  //
508  ///////////////////////
[5166]509
[7422]510  /**
511   * @brief calculates the position of a Buffer-Display Line
512   * @param lineNumber the lineNumber from the bottom to calculate the position from
513   * @returns the Position of the Line.
514   */
515  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
516  {
[7757]517    return Vector2D(5.0f, (float)(this->textSize + this->lineSpacing)*(float)((int)this->bufferDisplaySize - (int)lineNumber - (int)1));
[7422]518  }
[5120]519
520
521
[7422]522  /**
523   * @brief displays some nice output from the Shell
524   */
525  void Shell::debug() const
526  {
527    PRINT(3)("Debugging output to console (not this shell)\n");
[5119]528
[7422]529    //   if (this->pressedKey != SDLK_FIRST)
530    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]531
532
[7762]533    this->shellBuffer->debug();
[7422]534  }
[7374]535}
Note: See TracBrowser for help on using the repository browser.