Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Using MultiLine-Text in the Shell. This is much faster, and Nicer :)

File size: 14.8 KB
Line 
1/*
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL
17
18#include "shell.h"
19#include "shell_command.h"
20#include "shell_buffer.h"
21#include "shell_input.h"
22
23#include "multi_line_text.h"
24
25#include "graphics_engine.h"
26#include "material.h"
27#include "event_handler.h"
28
29namespace OrxShell
30{
31
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)
53      ->completionPlugin(0, OrxShell::CompletorFileSystem(".ttf", "fonts/"));
54
55  /**
56   * standard constructor
57   */
58  Shell::Shell ()
59  {
60    this->setClassID(CL_SHELL, "Shell");
61    this->setName("Shell");
62
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);
68
69    // BUFFER
70    this->bufferOffset = 0;
71    this->bufferIterator = ShellBuffer::getInstance()->getBuffer().begin();
72
73    // INPUT LINE
74    this->setLayer(E2D_LAYER_ABOVE_ALL);
75    this->shellInput.setLayer(E2D_LAYER_ABOVE_ALL);
76
77    this->backgroundMaterial = new Material;
78
79    // Element2D and generals
80    this->setAbsCoor2D(3, -400);
81    this->textSize = 15;
82    this->lineSpacing = 0;
83    this->bActive = true;
84    this->fontFile = SHELL_DEFAULT_FONT;
85
86    this->setBufferDisplaySize(10);
87
88    this->setTextColor(SHELL_DEFAULT_TEXT_COLOR);
89    this->setBackgroundColor(SHELL_DEFAULT_BACKGROUND_COLOR);
90
91
92    this->deactivate();
93    // register the shell at the ShellBuffer
94    ShellBuffer::getInstance()->registerShell(this);
95  }
96
97  /**
98   * @brief standard deconstructor
99   */
100  Shell::~Shell ()
101  {
102    ShellBuffer::getInstance()->unregisterShell(this);
103
104    // delete the displayable Buffers
105    while (!this->bufferText.empty())
106    {
107      delete this->bufferText.front();
108      this->bufferText.pop_front();
109    }
110
111    // delete the inputLine
112    delete this->backgroundMaterial;
113  }
114
115  /**
116   * @brief activates the shell
117   *
118   * This also feeds the Last few lines from the main buffers into the displayBuffer
119   */
120  void Shell::activate()
121  {
122    if (this->bActive == true)
123      PRINTF(3)("The shell is already active\n");
124    this->bActive = true;
125
126    EventHandler::getInstance()->pushState(ES_SHELL);
127    EventHandler::getInstance()->withUNICODE(true);
128
129    this->setRelCoorSoft2D(0, 0, 5);
130
131    std::list<std::string>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer().end();
132    bool top = false;
133    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
134    {
135      (*text)->setVisibility(true);
136      if (!top)
137      {
138        (*text)->setText((*textLine));
139        if (textLine != ShellBuffer::getInstance()->getBuffer().begin())
140          top = true;
141        textLine--;
142      }
143    }
144  }
145
146  /**
147   * @brief deactiveates the Shell.
148   */
149  void Shell::deactivate()
150  {
151    if (this->bActive == false)
152      PRINTF(3)("The shell is already inactive\n");
153    this->bActive = false;
154
155    EventHandler::getInstance()->withUNICODE(false);
156    EventHandler::getInstance()->popState();
157
158    this->setRelCoorSoft2D(0, -(int)this->shellHeight, 5);
159
160    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
161      (*text)->setVisibility(false);
162    this->bufferOffset = 0;
163  }
164
165  /**
166   * @brief sets the File to load the fonts from
167   * @param fontFile the file to load the font from
168   *
169   * it is quite important, that the font pointed too really exists!
170   * (be aware that within orxonox fontFile is relative to the Data-Dir)
171   */
172  void Shell::setFont(const std::string& fontFile)
173  {
174    //   if (!ResourceManager::isInDataDir(fontFile))
175    //     return false;
176
177    this->fontFile = fontFile;
178
179    this->resetValues();
180  }
181
182  /**
183   * @brief sets the size of the text and spacing
184   * @param textSize the size of the Text in Pixels
185   * @param lineSpacing the size of the Spacing between two lines in pixels
186   *
187   * this also rebuilds the entire Text, inputLine and displayBuffer,
188   * to be accurate again.
189   */
190  void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
191  {
192    this->textSize = textSize;
193    this->lineSpacing = lineSpacing;
194
195    this->resetValues();
196  }
197
198  /**
199   * @brief sets the color of the Font.
200   * @param r: red
201   * @param g: green
202   * @param b: blue
203   * @param a: alpha-value.
204   */
205  void Shell::setTextColor(float r, float g, float b, float a)
206  {
207    this->textColor[0] = r;
208    this->textColor[1] = g;
209    this->textColor[2] = b;
210    this->textColor[3] = a;
211
212    this->resetValues();
213  }
214
215
216  /**
217   * @brief sets the color of the Backgrond.
218   * @param r: red
219   * @param g: green
220   * @param b: blue
221   * @param a: alpha-value.
222   */
223  void Shell::setBackgroundColor(float r, float g, float b, float a)
224  {
225    this->backgroundMaterial->setDiffuse(r, g, b);
226    this->backgroundMaterial->setTransparency(a);
227  }
228
229  /**
230   * @brief sets a nice background image to the Shell's background
231   * @param fileName the filename of the Image to load
232   */
233  void Shell::setBackgroundImage(const std::string& fileName)
234  {
235    this->backgroundMaterial->setDiffuseMap(fileName);
236  }
237
238
239  /**
240   * @brief resets the Values of all visible shell's commandos to the Shell's stored values
241   *
242   * this functions synchronizes the stored Data with the visible one.
243   */
244  void Shell::resetValues()
245  {
246    this->resetText(&this->shellInput, 0);
247    this->shellInput.setRelCoor2D(15, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
248
249    unsigned int i = 0;
250    /* Here we create a Copy of the Buffer, so that we do not f**k up the List when some
251     * output is routed from Some other Thread or by Changing any Values.
252     */
253    std::list<MultiLineText*> bufferTextCopy = this->bufferText;
254    for (std::list<MultiLineText*>::iterator text = bufferTextCopy.begin(); text != bufferTextCopy.end(); ++text, ++i)
255    {
256      this->resetText(*text, i);
257      (*text)->setRelCoor2D( calculateLinePosition(i) );
258    }
259    this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
260  }
261
262  void Shell::resetText(Text* text, unsigned int position)
263  {
264    text->setFont(this->fontFile, this->textSize);
265    text->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
266    text->setBlending(this->textColor[3]);
267    text->setLayer(this->getLayer());
268    if (text->getParent2D() != this)
269      text->setParent2D(this);
270  }
271
272
273  /**
274   * @brief sets The count of Lines to display in the buffer.
275   * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
276   */
277  void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
278  {
279    unsigned int oldSize = this->bufferText.size();
280    if (oldSize > bufferDisplaySize)
281    {
282      for (unsigned int i = bufferDisplaySize; i <= oldSize; i++)
283      {
284        delete this->bufferText.back();
285        this->bufferText.pop_back();
286      }
287    }
288    else if (oldSize < bufferDisplaySize)
289    {
290      for (unsigned int i = oldSize; i <= bufferDisplaySize; i++)
291      {
292        this->bufferText.push_back(new MultiLineText);
293        this->bufferText.back()->setLineWidth(this->getSizeX2D() * GraphicsEngine::getInstance()->getResolutionX());
294        this->bufferText.back()->setLineSpacing(0);
295      }
296    }
297    this->bufferDisplaySize = bufferDisplaySize;
298    this->resetValues();
299  }
300
301  /**
302   * @brief deletes all the Buffers
303   */
304  void Shell::flush()
305  {
306    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
307    {
308      (*text)->setText("");  // remove all chars from the BufferTexts.
309    }
310    ShellBuffer::getInstance()->flush();
311    // BUFFER FLUSHING
312  }
313
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)
319  {
320    // Remove Last Entry and prepend it to the front.
321    this->bufferText.push_front(this->bufferText.back());
322    this->bufferText.pop_back();
323
324    // Set the new Text.
325    this->bufferText.front()->setText(text);
326
327    // The LineCount will be started here.
328    int linePos = -1;
329
330    // The First Line gets a special Animation
331    this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(linePos)- Vector2D(-1000,0));
332
333    // Move all lines one Entry up.
334    for (std::list<MultiLineText*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
335    {
336      linePos += (*textIt)->getLineCount();
337      (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(linePos), 8);
338    }
339
340
341    /*  FANCY EFFECTS :)
342      1:
343          lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1000,0,0));
344          lastText->setRelCoorSoft2D(this->calculateLinePosition(0),10);
345      2:
346      lastText->setRelDir2D(-90);
347      lastText->setRelDirSoft2D(0, 20);
348    */
349
350  }
351
352  /**
353   * moves the Display buffer (up + or down - )
354   * @param lineCount the count by which to shift the InputBuffer.
355   *
356   * @todo make this work
357   */
358  void Shell::moveDisplayBuffer(int lineCount)
359  {
360    if (this->bufferOffset == 0)
361    {
362      this->bufferIterator = ShellBuffer::getInstance()->getBuffer().end();
363      //     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
364      //       this->bufferIterator->prevStep();
365    }
366
367    // boundraries
368    if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer().size())
369      lineCount = (int)ShellBuffer::getInstance()->getBuffer().size()- this->bufferOffset;
370    else if (this->bufferOffset + lineCount < 0)
371      lineCount = -bufferOffset;
372    this->bufferOffset += lineCount;
373
374    // moving the iterator to the right position
375    int move = 0;
376    while (move != lineCount)
377    {
378      if (move < lineCount)
379      {
380        ++move;
381        this->bufferIterator--;
382      }
383      else
384      {
385        --move;
386        this->bufferIterator++;
387      }
388    }
389    // redisplay the buffers
390    std::list<std::string>::const_iterator it = this->bufferIterator;
391    if (it == ShellBuffer::getInstance()->getBuffer().end())
392    {
393      /// FIXME
394      PRINTF(1)("Should not happen\n");
395      it--;
396    }
397    for (std::list<MultiLineText*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
398    {
399      (*textIt)->setText((*it));
400      if (it == ShellBuffer::getInstance()->getBuffer().begin())
401      {
402        /// FIXME
403        PRINTF(1)("Should not happen\n");
404        break;
405      }
406      it--;
407    }
408  }
409
410  /**
411   * clears the Shell (empties all buffers)
412   */
413  void Shell::clear()
414  {
415    this->flush();
416    ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
417  }
418
419  /**
420   * listens for some event
421   * @param event the Event happened
422   */
423  void Shell::process(const Event &event)
424  {
425    if (event.bPressed)
426    {
427      if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
428      {
429        if (this->bActive == false)
430          this->activate();
431        else
432          this->deactivate();
433      }
434      else if (event.type == SDLK_PAGEUP)
435      {
436        this->moveDisplayBuffer(+this->bufferDisplaySize-1);
437      }
438      else if (event.type == SDLK_PAGEDOWN)
439      {
440        this->moveDisplayBuffer(-this->bufferDisplaySize+1);
441      }
442    }
443  }
444
445  /**
446   * displays the Shell
447   */
448  void Shell::draw() const
449  {
450    // transform for alignment.
451    // setting the Blending effects
452
453    this->backgroundMaterial->select();
454
455    glBegin(GL_TRIANGLE_STRIP);
456
457    glTexCoord2f(0, 0);
458    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
459
460    glTexCoord2f(1, 0);
461    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
462
463    glTexCoord2f(0, 1);
464    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
465
466    glTexCoord2f(1, 1);
467    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
468
469    glEnd();
470  }
471
472  ///////////////////////
473  // HELPER FUNCTIONS  //
474  ///////////////////////
475
476  /**
477   * @brief calculates the position of a Buffer-Display Line
478   * @param lineNumber the lineNumber from the bottom to calculate the position from
479   * @returns the Position of the Line.
480   */
481  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
482  {
483    Vector2D val(5, (int)(this->textSize + this->lineSpacing)*(int)((int)this->bufferDisplaySize - (int)lineNumber - (int)2) + (int)this->textSize);
484     val.debug();
485     return val;
486  }
487
488
489
490  /**
491   * @brief displays some nice output from the Shell
492   */
493  void Shell::debug() const
494  {
495    PRINT(3)("Debugging output to console (not this shell)\n");
496
497    //   if (this->pressedKey != SDLK_FIRST)
498    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
499
500
501    ShellBuffer::getInstance()->debug();
502  }
503
504  // void Shell::testI (int i)
505  // {
506  //   PRINTF(3)("This is the Test for one Int '%d'\n", i);
507  // }
508  //
509  // void Shell::testS (const char* s)
510  // {
511  //   PRINTF(3)("This is the Test for one String '%s'\n", s);
512  // }
513  //
514  // void Shell::testB (bool b)
515  // {
516  //   PRINTF(3)("This is the Test for one Bool: ");
517  //   if (b)
518  //     PRINTF(3)("true\n");
519  //   else
520  //     PRINTF(3)("false\n");
521  // }
522  //
523  // void Shell::testF (float f)
524  // {
525  //   PRINTF(3)("This is the Test for one Float '%f'\n", f);
526  // }
527  //
528  // void Shell::testSF (const char* s, float f)
529  // {
530  //   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
531  // }
532
533}
Note: See TracBrowser for help on using the repository browser.