Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the gui branche back
merged with command:
https://svn.orxonox.net/orxonox/branches/gui
no conflicts

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