Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/osx/src/lib/shell/shell.cc @ 7997

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

orxonox/trunk: ShellBuffer gets polled from the Shell, and does not put the other way round

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