Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Resize of the Shell works quite nice

File size: 15.2 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    // EVENT-Handler subscription of '`' to all States.
65    EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
66    EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_F12);
67    EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
68    EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
69    EventHandler::getInstance()->subscribe(this, ES_SHELL, EV_VIDEO_RESIZE);
70
71    // BUFFER
72    this->bufferIterator = ShellBuffer::getInstance()->getBuffer().begin();
73
74    // INPUT LINE
75    this->setLayer(E2D_LAYER_ABOVE_ALL);
76    this->shellInput.setLayer(E2D_LAYER_ABOVE_ALL);
77
78    // Element2D and generals
79    this->setSizeX2D(GraphicsEngine::getInstance()->getResolutionX());
80    this->setAbsCoor2D(3, -400);
81    this->textSize = 15;
82    this->lineSpacing = 0;
83    this->bActive = true;
84    this->fontFile = SHELL_DEFAULT_FONT;
85
86    this->setBufferDisplaySize(10);
87
88    this->setTextColor(SHELL_DEFAULT_TEXT_COLOR);
89    this->setBackgroundColor(SHELL_DEFAULT_BACKGROUND_COLOR);
90
91
92    this->deactivate();
93    // register the shell at the ShellBuffer
94    ShellBuffer::getInstance()->registerShell(this);
95  }
96
97  /**
98   * @brief standard deconstructor
99   */
100  Shell::~Shell ()
101  {
102    ShellBuffer::getInstance()->unregisterShell(this);
103
104    // delete the displayable Buffers
105    while (!this->bufferText.empty())
106    {
107      delete this->bufferText.front();
108      this->bufferText.pop_front();
109    }
110  }
111
112  /**
113   * @brief activates the shell
114   *
115   * This also feeds the Last few lines from the main buffers into the displayBuffer
116   */
117  void Shell::activate()
118  {
119    if (this->bActive == true)
120      PRINTF(3)("The shell is already active\n");
121    this->bActive = true;
122
123    EventHandler::getInstance()->pushState(ES_SHELL);
124    EventHandler::getInstance()->withUNICODE(true);
125
126    this->setRelCoorSoft2D(0, 0, 5);
127
128    std::list<std::string>::const_iterator textLine = ShellBuffer::getInstance()->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 !=  ShellBuffer::getInstance()->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
153    EventHandler::getInstance()->withUNICODE(false);
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 = ShellBuffer::getInstance()->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    ShellBuffer::getInstance()->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
356    // The LineCount will be started here.
357
358    // The First Line gets a special Animation
359    this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(0)- Vector2D(-1000,0));
360
361    // Move all lines one Entry up.
362    this->repositionText();
363  }
364
365
366  /**
367   * @brief moves the Display buffer (up + or down - )
368   * @param lineCount the count by which to shift the InputBuffer.
369   *
370   * @todo make this work
371   */
372  void Shell::moveDisplayBuffer(int lineCount)
373  {
374    // moving the iterator to the right position (counting the steps)
375    int moves = 0;
376    while (moves != lineCount)
377    {
378      if (moves < lineCount)
379      {
380        if(this->bufferIterator == --ShellBuffer::getInstance()->getBuffer().end())
381          break;
382        ++moves;
383        ++this->bufferIterator;
384      }
385      else
386      {
387        if (this->bufferIterator == ShellBuffer::getInstance()->getBuffer().begin())
388          break;
389        --moves;
390        --this->bufferIterator;
391      }
392    }
393
394
395    // redisplay the buffers
396    std::list<MultiLineText*>::iterator textIt;
397
398    // Give a Graphical Representation that no move is possible.
399    if (moves == 0)
400    {
401      int linePos = -1;
402      for (textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
403      {
404        linePos += (*textIt)->getLineCount();
405        (*textIt)->setRelCoor2D(this->calculateLinePosition(linePos)+ Vector2D(20,0));
406        (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(linePos), 10);
407      }
408      return;
409    }
410
411    // Move all the Lines.
412    int linePos = moves;
413    std::list<std::string>::const_iterator it = this->bufferIterator;
414    for (textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt, ++it)
415    {
416      if (it == ShellBuffer::getInstance()->getBuffer().end())
417      {
418        PRINTF(1)("LAST LINE REACHED\n");
419        break;
420      }
421
422
423      (*textIt)->setRelCoor2D(calculateLinePosition( (linePos++ > 0) ? linePos : 0));
424      (*textIt)->setText((*it));
425    }
426    while (textIt != this->bufferText.end())
427    {
428      (*textIt)->setText("");
429      textIt++;
430    }
431
432    this->repositionText();
433  }
434
435  /**
436   * @brief clears the Shell (empties all buffers)
437   */
438  void Shell::clear()
439  {
440    this->flush();
441    ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
442  }
443
444  /**
445   * @brief listens for some event
446   * @param event the Event happened
447   */
448  void Shell::process(const Event &event)
449  {
450    if (event.bPressed)
451    {
452      if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
453      {
454        if (this->bActive == false)
455          this->activate();
456        else
457          this->deactivate();
458      }
459      else if (event.type == SDLK_PAGEUP)
460      {
461        this->moveDisplayBuffer(+this->bufferDisplaySize-1);
462      }
463      else if (event.type == SDLK_PAGEDOWN)
464      {
465        this->moveDisplayBuffer(-this->bufferDisplaySize+1);
466      }
467      else if (event.type == EV_VIDEO_RESIZE)
468      {
469        this->updateResolution(event.resize.w);
470        this->repositionText();
471      }
472    }
473  }
474
475  /**
476   * displays the Shell
477   */
478  void Shell::draw() const
479  {
480    // transform for alignment.
481    // setting the Blending effects
482
483    this->backgroundMaterial.select();
484
485    glBegin(GL_TRIANGLE_STRIP);
486
487    glTexCoord2f(0, 0);
488    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
489
490    glTexCoord2f(1, 0);
491    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
492
493    glTexCoord2f(0, 1);
494    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
495
496    glTexCoord2f(1, 1);
497    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
498
499    glEnd();
500  }
501
502  ///////////////////////
503  // HELPER FUNCTIONS  //
504  ///////////////////////
505
506  /**
507   * @brief calculates the position of a Buffer-Display Line
508   * @param lineNumber the lineNumber from the bottom to calculate the position from
509   * @returns the Position of the Line.
510   */
511  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
512  {
513    return Vector2D(5.0f, (float)(this->textSize + this->lineSpacing)*(float)((int)this->bufferDisplaySize - (int)lineNumber - (int)1));
514  }
515
516
517
518  /**
519   * @brief displays some nice output from the Shell
520   */
521  void Shell::debug() const
522  {
523    PRINT(3)("Debugging output to console (not this shell)\n");
524
525    //   if (this->pressedKey != SDLK_FIRST)
526    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
527
528
529    ShellBuffer::getInstance()->debug();
530  }
531
532  /**
533   * @brief a Handy Function, to Test the behaviour of the Shell.
534   */
535  void Shell::testShell() const
536  {
537    for (unsigned int i = 0; i < 100; i++)
538      PRINT(0)("%d\n", i);
539  }
540  SHELL_COMMAND(test, Shell, testShell);
541
542}
Note: See TracBrowser for help on using the repository browser.