Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Repositioning

File size: 14.6 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
[7422]55  /**
56   * standard constructor
57   */
58  Shell::Shell ()
59  {
60    this->setClassID(CL_SHELL, "Shell");
61    this->setName("Shell");
[5072]62
[7422]63    // EVENT-Handler subscription of '`' to all States.
64    EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
65    EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_F12);
66    EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
67    EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
[5206]68
[7422]69    // BUFFER
70    this->bufferOffset = 0;
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
[7422]126    std::list<std::string>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer().end();
127    bool top = false;
[7737]128    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
[5787]129    {
[7422]130      (*text)->setVisibility(true);
131      if (!top)
132      {
133        (*text)->setText((*textLine));
134        if (textLine != ShellBuffer::getInstance()->getBuffer().begin())
135          top = true;
136        textLine--;
137      }
[5787]138    }
[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);
157    this->bufferOffset = 0;
158  }
[5113]159
[7422]160  /**
161   * @brief sets the File to load the fonts from
162   * @param fontFile the file to load the font from
163   *
164   * it is quite important, that the font pointed too really exists!
165   * (be aware that within orxonox fontFile is relative to the Data-Dir)
166   */
167  void Shell::setFont(const std::string& fontFile)
168  {
169    this->fontFile = fontFile;
[5251]170
[7738]171    this->applySettings();
[7422]172  }
[5251]173
[7422]174  /**
175   * @brief sets the size of the text and spacing
176   * @param textSize the size of the Text in Pixels
177   * @param lineSpacing the size of the Spacing between two lines in pixels
178   *
179   * this also rebuilds the entire Text, inputLine and displayBuffer,
180   * to be accurate again.
181   */
182  void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
183  {
184    this->textSize = textSize;
185    this->lineSpacing = lineSpacing;
[7341]186
[7738]187    this->applySettings();
[7422]188  }
[5113]189
[7422]190  /**
191   * @brief sets the color of the Font.
192   * @param r: red
193   * @param g: green
194   * @param b: blue
195   * @param a: alpha-value.
196   */
197  void Shell::setTextColor(float r, float g, float b, float a)
198  {
199    this->textColor[0] = r;
200    this->textColor[1] = g;
201    this->textColor[2] = b;
202    this->textColor[3] = a;
[5372]203
[7738]204    this->applySettings();
[7422]205  }
[5372]206
[7422]207  /**
208   * @brief sets the color of the Backgrond.
209   * @param r: red
210   * @param g: green
211   * @param b: blue
212   * @param a: alpha-value.
213   */
214  void Shell::setBackgroundColor(float r, float g, float b, float a)
215  {
[7738]216    this->backgroundMaterial.setDiffuse(r, g, b);
217    this->backgroundMaterial.setTransparency(a);
[7422]218  }
[5372]219
[7422]220  /**
221   * @brief sets a nice background image to the Shell's background
222   * @param fileName the filename of the Image to load
223   */
224  void Shell::setBackgroundImage(const std::string& fileName)
225  {
[7738]226    this->backgroundMaterial.setDiffuseMap(fileName);
[7422]227  }
[5374]228
[7738]229  /**
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  {
250    text->setFont(this->fontFile, this->textSize);
251    text->setSize(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    }
[7422]319    ShellBuffer::getInstance()->flush();
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);
[7341]335
[7737]336    // The LineCount will be started here.
337
338    // The First Line gets a special Animation
[7738]339    this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(0)- Vector2D(-1000,0));
[7737]340
341    // Move all lines one Entry up.
[7738]342    this->repositionText();
343  }
[7341]344
[5113]345
[7422]346  /**
[7738]347   * @brief moves the Display buffer (up + or down - )
[7422]348   * @param lineCount the count by which to shift the InputBuffer.
349   *
350   * @todo make this work
351   */
352  void Shell::moveDisplayBuffer(int lineCount)
[5783]353  {
[7422]354    if (this->bufferOffset == 0)
[5783]355    {
[7422]356      this->bufferIterator = ShellBuffer::getInstance()->getBuffer().end();
357      //     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
358      //       this->bufferIterator->prevStep();
[5783]359    }
[7422]360
361    // boundraries
362    if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer().size())
363      lineCount = (int)ShellBuffer::getInstance()->getBuffer().size()- this->bufferOffset;
364    else if (this->bufferOffset + lineCount < 0)
365      lineCount = -bufferOffset;
366    this->bufferOffset += lineCount;
367
368    // moving the iterator to the right position
369    int move = 0;
370    while (move != lineCount)
[5783]371    {
[7422]372      if (move < lineCount)
373      {
374        ++move;
375        this->bufferIterator--;
376      }
377      else
378      {
379        --move;
380        this->bufferIterator++;
381      }
[5783]382    }
[7422]383    // redisplay the buffers
384    std::list<std::string>::const_iterator it = this->bufferIterator;
385    if (it == ShellBuffer::getInstance()->getBuffer().end())
[7342]386    {
387      /// FIXME
[7737]388      PRINTF(1)("Should not happen\n");
[7422]389      it--;
[7342]390    }
[7737]391    for (std::list<MultiLineText*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
[7422]392    {
393      (*textIt)->setText((*it));
394      if (it == ShellBuffer::getInstance()->getBuffer().begin())
395      {
396        /// FIXME
[7737]397        PRINTF(1)("Should not happen\n");
[7422]398        break;
399      }
400      it--;
401    }
[5783]402  }
[5246]403
[7422]404  /**
[7738]405   * @brief clears the Shell (empties all buffers)
[7422]406   */
407  void Shell::clear()
408  {
409    this->flush();
410    ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
411  }
[5130]412
[7422]413  /**
[7738]414   * @brief listens for some event
[7422]415   * @param event the Event happened
416   */
417  void Shell::process(const Event &event)
[5093]418  {
[7422]419    if (event.bPressed)
[5093]420    {
[7422]421      if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
422      {
423        if (this->bActive == false)
424          this->activate();
425        else
426          this->deactivate();
427      }
428      else if (event.type == SDLK_PAGEUP)
429      {
430        this->moveDisplayBuffer(+this->bufferDisplaySize-1);
431      }
432      else if (event.type == SDLK_PAGEDOWN)
433      {
434        this->moveDisplayBuffer(-this->bufferDisplaySize+1);
435      }
[5093]436    }
437  }
[5069]438
[7422]439  /**
440   * displays the Shell
441   */
442  void Shell::draw() const
443  {
444    // transform for alignment.
445    // setting the Blending effects
[5099]446
[7738]447    this->backgroundMaterial.select();
[5099]448
[7422]449    glBegin(GL_TRIANGLE_STRIP);
[5099]450
[7422]451    glTexCoord2f(0, 0);
452    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
[5099]453
[7422]454    glTexCoord2f(1, 0);
455    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]456
[7422]457    glTexCoord2f(0, 1);
458    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5158]459
[7422]460    glTexCoord2f(1, 1);
461    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]462
[7422]463    glEnd();
464  }
[5068]465
[7422]466  ///////////////////////
467  // HELPER FUNCTIONS  //
468  ///////////////////////
[5166]469
[7422]470  /**
471   * @brief calculates the position of a Buffer-Display Line
472   * @param lineNumber the lineNumber from the bottom to calculate the position from
473   * @returns the Position of the Line.
474   */
475  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
476  {
[7738]477    return Vector2D(5, (int)(this->textSize + this->lineSpacing)*(int)((int)this->bufferDisplaySize - (int)lineNumber - (int)2) + (int)this->textSize);
[7422]478  }
[5120]479
480
481
[7422]482  /**
483   * @brief displays some nice output from the Shell
484   */
485  void Shell::debug() const
486  {
487    PRINT(3)("Debugging output to console (not this shell)\n");
[5119]488
[7422]489    //   if (this->pressedKey != SDLK_FIRST)
490    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]491
492
[7422]493    ShellBuffer::getInstance()->debug();
494  }
[5166]495
[7422]496  // void Shell::testI (int i)
497  // {
498  //   PRINTF(3)("This is the Test for one Int '%d'\n", i);
499  // }
500  //
501  // void Shell::testS (const char* s)
502  // {
503  //   PRINTF(3)("This is the Test for one String '%s'\n", s);
504  // }
505  //
506  // void Shell::testB (bool b)
507  // {
508  //   PRINTF(3)("This is the Test for one Bool: ");
509  //   if (b)
510  //     PRINTF(3)("true\n");
511  //   else
512  //     PRINTF(3)("false\n");
513  // }
514  //
515  // void Shell::testF (float f)
516  // {
517  //   PRINTF(3)("This is the Test for one Float '%f'\n", f);
518  // }
519  //
520  // void Shell::testSF (const char* s, float f)
521  // {
522  //   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
523  // }
[7374]524
525}
Note: See TracBrowser for help on using the repository browser.