Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: completing file-system entries

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
[5175]23
[5344]24#include "text.h"
[5093]25#include "graphics_engine.h"
[5372]26#include "material.h"
[5093]27#include "event_handler.h"
[5129]28#include "debug.h"
[5113]29#include "class_list.h"
30
31#include "key_names.h"
[5075]32#include <stdarg.h>
33#include <stdio.h>
34
[7374]35namespace OrxShell
36{
[1853]37
[7422]38  SHELL_COMMAND(clear, Shell, clear)
39  ->describe("Clears the shell from unwanted lines (empties all buffers)")
40  ->setAlias("clear");
41  SHELL_COMMAND(deactivate, Shell, deactivate)
42  ->describe("Deactivates the Shell. (moves it into background)")
43  ->setAlias("hide");
44  SHELL_COMMAND(textsize, Shell, setTextSize)
45  ->describe("Sets the size of the Text size, linespacing")
46  ->defaultValues(15, 0);
47  SHELL_COMMAND(textcolor, Shell, setTextColor)
48  ->describe("Sets the Color of the Shells Text (red, green, blue, alpha)")
49  ->defaultValues(SHELL_DEFAULT_TEXT_COLOR);
50  SHELL_COMMAND(backgroundcolor, Shell, setBackgroundColor)
51  ->describe("Sets the Color of the Shells Background (red, green, blue, alpha)")
52  ->defaultValues(SHELL_DEFAULT_BACKGROUND_COLOR);
53  SHELL_COMMAND(backgroundimage, Shell, setBackgroundImage)
54  ->describe("sets the background image to load for the Shell")
55  ->completionPlugin(0, OrxShell::CompletorFileSystem());
56  SHELL_COMMAND(font, Shell, setFont)
57  ->describe("Sets the font of the Shell")
58  ->defaultValues(SHELL_DEFAULT_FONT)
59      ->completionPlugin(0, OrxShell::CompletorFileSystem(".ttf", "fonts/"));
[1856]60
[7422]61  /**
62   * standard constructor
63   */
64  Shell::Shell ()
65  {
66    this->setClassID(CL_SHELL, "Shell");
67    this->setName("Shell");
[5072]68
[7422]69    // EVENT-Handler subscription of '`' to all States.
70    EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
71    EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_F12);
72    EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
73    EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
[5206]74
[7422]75    // BUFFER
76    this->bufferOffset = 0;
77    this->bufferIterator = ShellBuffer::getInstance()->getBuffer().begin();
[5372]78
[7422]79    // INPUT LINE
80    this->setLayer(E2D_LAYER_ABOVE_ALL);
81    this->shellInput.setLayer(E2D_LAYER_ABOVE_ALL);
[5372]82
[7422]83    this->backgroundMaterial = new Material;
[7341]84
[7422]85    // Element2D and generals
86    this->setAbsCoor2D(3, -400);
87    this->textSize = 20;
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    }
116
117    // delete the inputLine
118    delete this->backgroundMaterial;
[7341]119  }
120
[7422]121  /**
122   * @brief activates the shell
123   *
124   * This also feeds the Last few lines from the main buffers into the displayBuffer
125   */
126  void Shell::activate()
127  {
128    if (this->bActive == true)
129      PRINTF(3)("The shell is already active\n");
130    this->bActive = true;
[5068]131
[7422]132    EventHandler::getInstance()->pushState(ES_SHELL);
133    EventHandler::getInstance()->withUNICODE(true);
[5113]134
[7422]135    this->setRelCoorSoft2D(0, 0, 5);
[5786]136
[7422]137    std::list<std::string>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer().end();
138    bool top = false;
139    for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
[5787]140    {
[7422]141      (*text)->setVisibility(true);
142      if (!top)
143      {
144        (*text)->setText((*textLine));
145        if (textLine != ShellBuffer::getInstance()->getBuffer().begin())
146          top = true;
147        textLine--;
148      }
[5787]149    }
[5247]150  }
[5113]151
[7422]152  /**
153   * @brief deactiveates the Shell.
154   */
155  void Shell::deactivate()
156  {
157    if (this->bActive == false)
158      PRINTF(3)("The shell is already inactive\n");
159    this->bActive = false;
[5113]160
[7422]161    EventHandler::getInstance()->withUNICODE(false);
162    EventHandler::getInstance()->popState();
[5388]163
[7422]164    this->setRelCoorSoft2D(0, -(int)this->shellHeight, 5);
[5118]165
[7422]166    for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
167      (*text)->setVisibility(false);
168    this->bufferOffset = 0;
169  }
[5113]170
[7422]171  /**
172   * @brief sets the File to load the fonts from
173   * @param fontFile the file to load the font from
174   *
175   * it is quite important, that the font pointed too really exists!
176   * (be aware that within orxonox fontFile is relative to the Data-Dir)
177   */
178  void Shell::setFont(const std::string& fontFile)
179  {
180    //   if (!ResourceManager::isInDataDir(fontFile))
181    //     return false;
[5335]182
[7422]183    this->fontFile = fontFile;
[5251]184
[7422]185    this->resetValues();
186  }
[5251]187
[7422]188  /**
189   * @brief sets the size of the text and spacing
190   * @param textSize the size of the Text in Pixels
191   * @param lineSpacing the size of the Spacing between two lines in pixels
192   *
193   * this also rebuilds the entire Text, inputLine and displayBuffer,
194   * to be accurate again.
195   */
196  void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
197  {
198    this->textSize = textSize;
199    this->lineSpacing = lineSpacing;
[7341]200
[7422]201    this->resetValues();
202  }
[5113]203
[7422]204  /**
205   * @brief sets the color of the Font.
206   * @param r: red
207   * @param g: green
208   * @param b: blue
209   * @param a: alpha-value.
210   */
211  void Shell::setTextColor(float r, float g, float b, float a)
212  {
213    this->textColor[0] = r;
214    this->textColor[1] = g;
215    this->textColor[2] = b;
216    this->textColor[3] = a;
[5372]217
[7422]218    this->resetValues();
219  }
[5372]220
221
[7422]222  /**
223   * @brief sets the color of the Backgrond.
224   * @param r: red
225   * @param g: green
226   * @param b: blue
227   * @param a: alpha-value.
228   */
229  void Shell::setBackgroundColor(float r, float g, float b, float a)
230  {
231    this->backgroundMaterial->setDiffuse(r, g, b);
232    this->backgroundMaterial->setTransparency(a);
233  }
[5372]234
[7422]235  /**
236   * @brief sets a nice background image to the Shell's background
237   * @param fileName the filename of the Image to load
238   */
239  void Shell::setBackgroundImage(const std::string& fileName)
240  {
241    this->backgroundMaterial->setDiffuseMap(fileName);
242  }
[5374]243
244
[7422]245  /**
246   * @brief resets the Values of all visible shell's commandos to the Shell's stored values
247   *
248   * this functions synchronizes the stored Data with the visible one.
249   */
250  void Shell::resetValues()
251  {
252    this->shellInput.setFont(this->fontFile, this->textSize);
253    this->shellInput.setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
254    this->shellInput.setBlending(this->textColor[3]);
255    this->shellInput.setRelCoor2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
256    this->shellInput.setLayer(this->getLayer());
257    if (shellInput.getParent2D() != this)
258      this->shellInput.setParent2D(this);
[5372]259
[7422]260    unsigned int i = 0;
261    for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text, ++i)
262    {
263      (*text)->setFont(this->fontFile, this->textSize);
264      (*text)->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
265      (*text)->setBlending(this->textColor[3]);
266      (*text)->setRelCoor2D( calculateLinePosition(i) );
267      (*text)->setLayer(this->getLayer());
268      if ((*text)->getParent2D() != this)
269        (*text)->setParent2D(this);
270    }
271    this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5369]272  }
[5113]273
274
[7422]275  /**
276   * @brief sets The count of Lines to display in the buffer.
277   * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
278   */
279  void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
[5072]280  {
[7422]281    unsigned int oldSize = this->bufferText.size();
282    if (oldSize > bufferDisplaySize)
[7341]283    {
[7422]284      for (unsigned int i = bufferDisplaySize; i <= oldSize; i++)
285      {
286        delete this->bufferText.back();
287        this->bufferText.pop_back();
288      }
[7341]289    }
[7422]290    else if (oldSize < bufferDisplaySize)
291    {
292      for (unsigned int i = oldSize; i <= bufferDisplaySize; i++)
293      {
294        this->bufferText.push_back(new Text);
295      }
296    }
297    this->bufferDisplaySize = bufferDisplaySize;
298    this->resetValues();
[5072]299  }
[7422]300
301  /**
302   * @brief deletes all the Buffers
303   */
304  void Shell::flush()
[5072]305  {
[7422]306    for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
[5787]307    {
[7422]308      (*text)->setText("");  // remove all chars from the BufferTexts.
[5787]309    }
[7422]310    ShellBuffer::getInstance()->flush();
311    // BUFFER FLUSHING
[5072]312  }
[5068]313
[7422]314  /**
315   * @brief prints out some text to the input-buffers
316   * @param text the text to output.
317   */
318  void Shell::printToDisplayBuffer(const std::string& text)
[7341]319  {
[7422]320    this->bufferText.push_front(this->bufferText.back());
321    this->bufferText.pop_back();
[5068]322
[7341]323
[7422]324    unsigned int i = 0;
325    for (std::list<Text*>::iterator textIt = ++this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt, i++)
326    {
327      (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(i+1), 5);
328    }
[7341]329
[7422]330    this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(0)- Vector2D(-1000,0));
331    this->bufferText.front()->setRelCoorSoft2D(this->calculateLinePosition(0),10);
[5113]332
[7422]333    /*  FANCY EFFECTS :)
334      1:
335          lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1000,0,0));
336          lastText->setRelCoorSoft2D(this->calculateLinePosition(0),10);
337      2:
338      lastText->setRelDir2D(-90);
339      lastText->setRelDirSoft2D(0, 20);
340    */
[5375]341
[7422]342    this->bufferText.front()->setText(text);
[7341]343  }
[5783]344
[7422]345  /**
346   * moves the Display buffer (up + or down - )
347   * @param lineCount the count by which to shift the InputBuffer.
348   *
349   * @todo make this work
350   */
351  void Shell::moveDisplayBuffer(int lineCount)
[5783]352  {
[7422]353    if (this->bufferOffset == 0)
[5783]354    {
[7422]355      this->bufferIterator = ShellBuffer::getInstance()->getBuffer().end();
356      //     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
357      //       this->bufferIterator->prevStep();
[5783]358    }
[7422]359
360    // boundraries
361    if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer().size())
362      lineCount = (int)ShellBuffer::getInstance()->getBuffer().size()- this->bufferOffset;
363    else if (this->bufferOffset + lineCount < 0)
364      lineCount = -bufferOffset;
365    this->bufferOffset += lineCount;
366
367    // moving the iterator to the right position
368    int move = 0;
369    while (move != lineCount)
[5783]370    {
[7422]371      if (move < lineCount)
372      {
373        ++move;
374        this->bufferIterator--;
375      }
376      else
377      {
378        --move;
379        this->bufferIterator++;
380      }
[5783]381    }
[7422]382    // redisplay the buffers
383    std::list<std::string>::const_iterator it = this->bufferIterator;
384    if (it == ShellBuffer::getInstance()->getBuffer().end())
[7342]385    {
386      /// FIXME
387      PRINTF(1)("Should not heappen\n");
[7422]388      it--;
[7342]389    }
[7422]390    for (std::list<Text*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
391    {
392      (*textIt)->setText((*it));
393      if (it == ShellBuffer::getInstance()->getBuffer().begin())
394      {
395        /// FIXME
396        PRINTF(1)("Should not heappen\n");
397        break;
398      }
399      it--;
400    }
[5783]401  }
[5246]402
[7422]403  /**
404   * clears the Shell (empties all buffers)
405   */
406  void Shell::clear()
407  {
408    this->flush();
409    ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
410  }
[5130]411
[7422]412  /**
413   * listens for some event
414   * @param event the Event happened
415   */
416  void Shell::process(const Event &event)
[5093]417  {
[7422]418    if (event.bPressed)
[5093]419    {
[7422]420      if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
421      {
422        if (this->bActive == false)
423          this->activate();
424        else
425          this->deactivate();
426      }
427      else if (event.type == SDLK_PAGEUP)
428      {
429        this->moveDisplayBuffer(+this->bufferDisplaySize-1);
430      }
431      else if (event.type == SDLK_PAGEDOWN)
432      {
433        this->moveDisplayBuffer(-this->bufferDisplaySize+1);
434      }
[5093]435    }
436  }
[5069]437
[7422]438  /**
439   * displays the Shell
440   */
441  void Shell::draw() const
442  {
443    // transform for alignment.
444    // setting the Blending effects
[5099]445
[7422]446    this->backgroundMaterial->select();
[5099]447
[7422]448    glBegin(GL_TRIANGLE_STRIP);
[5099]449
[7422]450    glTexCoord2f(0, 0);
451    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
[5099]452
[7422]453    glTexCoord2f(1, 0);
454    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]455
[7422]456    glTexCoord2f(0, 1);
457    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5158]458
[7422]459    glTexCoord2f(1, 1);
460    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]461
[7422]462    glEnd();
463  }
[5068]464
[7422]465  ///////////////////////
466  // HELPER FUNCTIONS  //
467  ///////////////////////
[5166]468
[7422]469  /**
470   * @brief calculates the position of a Buffer-Display Line
471   * @param lineNumber the lineNumber from the bottom to calculate the position from
472   * @returns the Position of the Line.
473   */
474  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
475  {
476    return Vector2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber - 2) + this->textSize);
477  }
[5120]478
479
480
[7422]481  /**
482   * @brief displays some nice output from the Shell
483   */
484  void Shell::debug() const
485  {
486    PRINT(3)("Debugging output to console (not this shell)\n");
[5119]487
[7422]488    //   if (this->pressedKey != SDLK_FIRST)
489    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]490
491
[7422]492    ShellBuffer::getInstance()->debug();
493  }
[5166]494
[7422]495  // void Shell::testI (int i)
496  // {
497  //   PRINTF(3)("This is the Test for one Int '%d'\n", i);
498  // }
499  //
500  // void Shell::testS (const char* s)
501  // {
502  //   PRINTF(3)("This is the Test for one String '%s'\n", s);
503  // }
504  //
505  // void Shell::testB (bool b)
506  // {
507  //   PRINTF(3)("This is the Test for one Bool: ");
508  //   if (b)
509  //     PRINTF(3)("true\n");
510  //   else
511  //     PRINTF(3)("false\n");
512  // }
513  //
514  // void Shell::testF (float f)
515  // {
516  //   PRINTF(3)("This is the Test for one Float '%f'\n", f);
517  // }
518  //
519  // void Shell::testSF (const char* s, float f)
520  // {
521  //   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
522  // }
[7374]523
524}
Note: See TracBrowser for help on using the repository browser.