Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/shell/shell.cc @ 7895

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

orxonox/trunk: fixed a bug in the EventHandler, that resulted from multiple inputs or so…

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