Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: merge Check in the Event-changes:
r7867 | bensch | 2006-05-26 13:19:46 +0200 (Fri, 26 May 2006) | 1 line

Events better subscribed


r7866 | bensch | 2006-05-26 13:11:10 +0200 (Fri, 26 May 2006) | 1 line

Events are subscribed at the EventListener, and not the EventHandler

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
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    // Move all lines one Entry up.
364    this->repositionText();
365  }
366
367
368  /**
369   * @brief moves the Display buffer (up + or down - )
370   * @param lineCount the count by which to shift the InputBuffer.
371   *
372   * @todo make this work
373   */
374  void Shell::moveDisplayBuffer(int lineCount)
375  {
376    // moving the iterator to the right position (counting the steps)
377    int moves = 0;
378    while (moves != lineCount)
379    {
380      if (moves < lineCount)
381      {
382        if(this->bufferIterator == --this->shellBuffer->getBuffer().end())
383          break;
384        ++moves;
385        ++this->bufferIterator;
386      }
387      else
388      {
389        if (this->bufferIterator == this->shellBuffer->getBuffer().begin())
390          break;
391        --moves;
392        --this->bufferIterator;
393      }
394    }
395
396
397    // redisplay the buffers
398    std::list<MultiLineText*>::iterator textIt;
399
400    // Give a Graphical Representation that no move is possible.
401    if (moves == 0)
402    {
403      int linePos = -1;
404      for (textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
405      {
406        linePos += (*textIt)->getLineCount();
407        (*textIt)->setRelCoor2D(this->calculateLinePosition(linePos)+ Vector2D(20,0));
408        (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(linePos), 10);
409      }
410      return;
411    }
412
413    // Move all the Lines.
414    int linePos = moves;
415    std::list<std::string>::const_iterator it = this->bufferIterator;
416    for (textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt, ++it)
417    {
418      if (it == this->shellBuffer->getBuffer().end())
419      {
420        PRINTF(1)("LAST LINE REACHED\n");
421        break;
422      }
423
424
425      (*textIt)->setRelCoor2D(calculateLinePosition( (linePos++ > 0) ? linePos : 0));
426      (*textIt)->setText((*it));
427    }
428    while (textIt != this->bufferText.end())
429    {
430      (*textIt)->setText("");
431      textIt++;
432    }
433
434    this->repositionText();
435  }
436
437  /**
438   * @brief clears the Shell (empties all buffers)
439   */
440  void Shell::clear()
441  {
442    this->flush();
443    ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
444  }
445
446  /**
447   * @brief listens for some event
448   * @param event the Event happened
449   */
450  void Shell::process(const Event &event)
451  {
452    if (event.bPressed)
453    {
454      if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
455      {
456        if (this->bActive == false)
457          this->activate();
458        else
459          this->deactivate();
460      }
461      else if (event.type == SDLK_PAGEUP)
462      {
463        this->moveDisplayBuffer(+this->bufferDisplaySize-1);
464      }
465      else if (event.type == SDLK_PAGEDOWN)
466      {
467        this->moveDisplayBuffer(-this->bufferDisplaySize+1);
468      }
469      else if (event.type == EV_VIDEO_RESIZE)
470      {
471        this->updateResolution(event.resize.w);
472        this->repositionText();
473      }
474    }
475  }
476
477  void Shell::tick(float dt)
478  {
479    if (this->linesProcessed < this->shellBuffer->getLineCount())
480    {
481      unsigned int pollLines = this->shellBuffer->getLineCount() - this->linesProcessed;
482      if (this->bufferText.size() < pollLines)
483        pollLines = this->bufferText.size();
484      if (unlikely(this->shellBuffer->getBuffer().size() < pollLines))
485        pollLines = this->shellBuffer->getBuffer().size();
486
487      std::list<std::string>::const_iterator line = this->shellBuffer->getBuffer().begin();
488      unsigned int i;
489      for(i = 0; i < pollLines; i++)
490        line ++;
491      for (i = 0; i < pollLines; i++)
492      {
493        this->printToDisplayBuffer((*--line));
494      }
495    }
496    this->linesProcessed = this->shellBuffer->getLineCount();
497  }
498
499
500  /**
501   * displays the Shell
502   */
503  void Shell::draw() const
504  {
505    // transform for alignment.
506    // setting the Blending effects
507
508    this->backgroundMaterial.select();
509
510    glBegin(GL_TRIANGLE_STRIP);
511
512    glTexCoord2f(0, 0);
513    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
514
515    glTexCoord2f(1, 0);
516    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
517
518    glTexCoord2f(0, 1);
519    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
520
521    glTexCoord2f(1, 1);
522    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
523
524    glEnd();
525  }
526
527  ///////////////////////
528  // HELPER FUNCTIONS  //
529  ///////////////////////
530
531  /**
532   * @brief calculates the position of a Buffer-Display Line
533   * @param lineNumber the lineNumber from the bottom to calculate the position from
534   * @returns the Position of the Line.
535   */
536  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
537  {
538    return Vector2D(5.0f, (float)(this->textSize + this->lineSpacing)*(float)((int)this->bufferDisplaySize - (int)lineNumber - (int)1));
539  }
540
541
542
543  /**
544   * @brief displays some nice output from the Shell
545   */
546  void Shell::debug() const
547  {
548    PRINT(3)("Debugging output to console (not this shell)\n");
549
550    //   if (this->pressedKey != SDLK_FIRST)
551    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
552
553
554    this->shellBuffer->debug();
555  }
556
557  /**
558   * @brief a Handy Function, to Test the behaviour of the Shell.
559   */
560  void Shell::testShell() const
561  {
562    for (unsigned int i = 0; i < 100; i++)
563      PRINT(0)("%d\n", i);
564  }
565  SHELL_COMMAND(test, Shell, testShell);
566
567}
Note: See TracBrowser for help on using the repository browser.