Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellFont can be changed in threads :)

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);
[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    }
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  {
[7426]252    this->resetText(&this->shellInput, 0);
253    this->shellInput.setRelCoor2D(15, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
[5372]254
[7422]255    unsigned int i = 0;
[7426]256    /* Here we create a Copy of the Buffer, so that we do not f**k up the List when some
257     * output is routed from Some other Thread or by Changing any Values.
258     */
259    std::list<Text*> bufferTextCopy = this->bufferText;
260    for (std::list<Text*>::iterator text = bufferTextCopy.begin(); text != bufferTextCopy.end(); ++text, ++i)
[7422]261    {
[7426]262      this->resetText(*text, i);
[7422]263      (*text)->setRelCoor2D( calculateLinePosition(i) );
264    }
265    this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5369]266  }
[5113]267
[7426]268  void Shell::resetText(Text* text, unsigned int position)
269  {
270    text->setFont(this->fontFile, this->textSize);
271    text->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
272    text->setBlending(this->textColor[3]);
273    text->setLayer(this->getLayer());
274    if (text->getParent2D() != this)
275      text->setParent2D(this);
276  }
[5113]277
[7426]278
[7422]279  /**
280   * @brief sets The count of Lines to display in the buffer.
281   * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
282   */
283  void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
[5072]284  {
[7422]285    unsigned int oldSize = this->bufferText.size();
286    if (oldSize > bufferDisplaySize)
[7341]287    {
[7422]288      for (unsigned int i = bufferDisplaySize; i <= oldSize; i++)
289      {
290        delete this->bufferText.back();
291        this->bufferText.pop_back();
292      }
[7341]293    }
[7422]294    else if (oldSize < bufferDisplaySize)
295    {
296      for (unsigned int i = oldSize; i <= bufferDisplaySize; i++)
297      {
298        this->bufferText.push_back(new Text);
299      }
300    }
301    this->bufferDisplaySize = bufferDisplaySize;
302    this->resetValues();
[5072]303  }
[7422]304
305  /**
306   * @brief deletes all the Buffers
307   */
308  void Shell::flush()
[5072]309  {
[7422]310    for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
[5787]311    {
[7422]312      (*text)->setText("");  // remove all chars from the BufferTexts.
[5787]313    }
[7422]314    ShellBuffer::getInstance()->flush();
315    // BUFFER FLUSHING
[5072]316  }
[5068]317
[7422]318  /**
319   * @brief prints out some text to the input-buffers
320   * @param text the text to output.
321   */
322  void Shell::printToDisplayBuffer(const std::string& text)
[7341]323  {
[7422]324    this->bufferText.push_front(this->bufferText.back());
325    this->bufferText.pop_back();
[5068]326
[7341]327
[7422]328    unsigned int i = 0;
329    for (std::list<Text*>::iterator textIt = ++this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt, i++)
330    {
331      (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(i+1), 5);
332    }
[7341]333
[7422]334    this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(0)- Vector2D(-1000,0));
335    this->bufferText.front()->setRelCoorSoft2D(this->calculateLinePosition(0),10);
[5113]336
[7422]337    /*  FANCY EFFECTS :)
338      1:
339          lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1000,0,0));
340          lastText->setRelCoorSoft2D(this->calculateLinePosition(0),10);
341      2:
342      lastText->setRelDir2D(-90);
343      lastText->setRelDirSoft2D(0, 20);
344    */
[5375]345
[7422]346    this->bufferText.front()->setText(text);
[7341]347  }
[5783]348
[7422]349  /**
350   * moves the Display buffer (up + or down - )
351   * @param lineCount the count by which to shift the InputBuffer.
352   *
353   * @todo make this work
354   */
355  void Shell::moveDisplayBuffer(int lineCount)
[5783]356  {
[7422]357    if (this->bufferOffset == 0)
[5783]358    {
[7422]359      this->bufferIterator = ShellBuffer::getInstance()->getBuffer().end();
360      //     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
361      //       this->bufferIterator->prevStep();
[5783]362    }
[7422]363
364    // boundraries
365    if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer().size())
366      lineCount = (int)ShellBuffer::getInstance()->getBuffer().size()- this->bufferOffset;
367    else if (this->bufferOffset + lineCount < 0)
368      lineCount = -bufferOffset;
369    this->bufferOffset += lineCount;
370
371    // moving the iterator to the right position
372    int move = 0;
373    while (move != lineCount)
[5783]374    {
[7422]375      if (move < lineCount)
376      {
377        ++move;
378        this->bufferIterator--;
379      }
380      else
381      {
382        --move;
383        this->bufferIterator++;
384      }
[5783]385    }
[7422]386    // redisplay the buffers
387    std::list<std::string>::const_iterator it = this->bufferIterator;
388    if (it == ShellBuffer::getInstance()->getBuffer().end())
[7342]389    {
390      /// FIXME
391      PRINTF(1)("Should not heappen\n");
[7422]392      it--;
[7342]393    }
[7422]394    for (std::list<Text*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
395    {
396      (*textIt)->setText((*it));
397      if (it == ShellBuffer::getInstance()->getBuffer().begin())
398      {
399        /// FIXME
400        PRINTF(1)("Should not heappen\n");
401        break;
402      }
403      it--;
404    }
[5783]405  }
[5246]406
[7422]407  /**
408   * clears the Shell (empties all buffers)
409   */
410  void Shell::clear()
411  {
412    this->flush();
413    ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
414  }
[5130]415
[7422]416  /**
417   * listens for some event
418   * @param event the Event happened
419   */
420  void Shell::process(const Event &event)
[5093]421  {
[7422]422    if (event.bPressed)
[5093]423    {
[7422]424      if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
425      {
426        if (this->bActive == false)
427          this->activate();
428        else
429          this->deactivate();
430      }
431      else if (event.type == SDLK_PAGEUP)
432      {
433        this->moveDisplayBuffer(+this->bufferDisplaySize-1);
434      }
435      else if (event.type == SDLK_PAGEDOWN)
436      {
437        this->moveDisplayBuffer(-this->bufferDisplaySize+1);
438      }
[5093]439    }
440  }
[5069]441
[7422]442  /**
443   * displays the Shell
444   */
445  void Shell::draw() const
446  {
447    // transform for alignment.
448    // setting the Blending effects
[5099]449
[7422]450    this->backgroundMaterial->select();
[5099]451
[7422]452    glBegin(GL_TRIANGLE_STRIP);
[5099]453
[7422]454    glTexCoord2f(0, 0);
455    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
[5099]456
[7422]457    glTexCoord2f(1, 0);
458    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]459
[7422]460    glTexCoord2f(0, 1);
461    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5158]462
[7422]463    glTexCoord2f(1, 1);
464    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]465
[7422]466    glEnd();
467  }
[5068]468
[7422]469  ///////////////////////
470  // HELPER FUNCTIONS  //
471  ///////////////////////
[5166]472
[7422]473  /**
474   * @brief calculates the position of a Buffer-Display Line
475   * @param lineNumber the lineNumber from the bottom to calculate the position from
476   * @returns the Position of the Line.
477   */
478  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
479  {
480    return Vector2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber - 2) + this->textSize);
481  }
[5120]482
483
484
[7422]485  /**
486   * @brief displays some nice output from the Shell
487   */
488  void Shell::debug() const
489  {
490    PRINT(3)("Debugging output to console (not this shell)\n");
[5119]491
[7422]492    //   if (this->pressedKey != SDLK_FIRST)
493    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]494
495
[7422]496    ShellBuffer::getInstance()->debug();
497  }
[5166]498
[7422]499  // void Shell::testI (int i)
500  // {
501  //   PRINTF(3)("This is the Test for one Int '%d'\n", i);
502  // }
503  //
504  // void Shell::testS (const char* s)
505  // {
506  //   PRINTF(3)("This is the Test for one String '%s'\n", s);
507  // }
508  //
509  // void Shell::testB (bool b)
510  // {
511  //   PRINTF(3)("This is the Test for one Bool: ");
512  //   if (b)
513  //     PRINTF(3)("true\n");
514  //   else
515  //     PRINTF(3)("false\n");
516  // }
517  //
518  // void Shell::testF (float f)
519  // {
520  //   PRINTF(3)("This is the Test for one Float '%f'\n", f);
521  // }
522  //
523  // void Shell::testSF (const char* s, float f)
524  // {
525  //   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
526  // }
[7374]527
528}
Note: See TracBrowser for help on using the repository browser.