Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/shell/shell.cc @ 9900

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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