Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: the Buffer is moving pretty smoothly :)

File size: 13.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  void Shell::testShell()
57  {
58    for (unsigned int i = 0; i < 100; i++)
59      PRINT(0)("%d\n", i);
60  }
61  SHELL_COMMAND(test, Shell, testShell);
62
63
64  /**
65   * standard constructor
66   */
67  Shell::Shell ()
68  {
69    this->setClassID(CL_SHELL, "Shell");
70    this->setName("Shell");
71
72    // EVENT-Handler subscription of '`' to all States.
73    EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
74    EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_F12);
75    EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
76    EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
77
78    // BUFFER
79    this->bufferIterator = ShellBuffer::getInstance()->getBuffer().begin();
80
81    // INPUT LINE
82    this->setLayer(E2D_LAYER_ABOVE_ALL);
83    this->shellInput.setLayer(E2D_LAYER_ABOVE_ALL);
84
85    // Element2D and generals
86    this->setAbsCoor2D(3, -400);
87    this->textSize = 15;
88    this->lineSpacing = 0;
89    this->bActive = true;
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    // register the shell at the ShellBuffer
100    ShellBuffer::getInstance()->registerShell(this);
101  }
102
103  /**
104   * @brief standard deconstructor
105   */
106  Shell::~Shell ()
107  {
108    ShellBuffer::getInstance()->unregisterShell(this);
109
110    // delete the displayable Buffers
111    while (!this->bufferText.empty())
112    {
113      delete this->bufferText.front();
114      this->bufferText.pop_front();
115    }
116  }
117
118  /**
119   * @brief activates the shell
120   *
121   * This also feeds the Last few lines from the main buffers into the displayBuffer
122   */
123  void Shell::activate()
124  {
125    if (this->bActive == true)
126      PRINTF(3)("The shell is already active\n");
127    this->bActive = true;
128
129    EventHandler::getInstance()->pushState(ES_SHELL);
130    EventHandler::getInstance()->withUNICODE(true);
131
132    this->setRelCoorSoft2D(0, 0, 5);
133
134    std::list<std::string>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer().end();
135    bool top = false;
136    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
137    {
138      (*text)->setVisibility(true);
139      if (!top)
140      {
141        (*text)->setText((*textLine));
142        if (textLine != ShellBuffer::getInstance()->getBuffer().begin())
143          top = true;
144        textLine--;
145      }
146    }
147    repositionText();
148  }
149
150  /**
151   * @brief deactiveates the Shell.
152   */
153  void Shell::deactivate()
154  {
155    if (this->bActive == false)
156      PRINTF(3)("The shell is already inactive\n");
157    this->bActive = false;
158
159    EventHandler::getInstance()->withUNICODE(false);
160    EventHandler::getInstance()->popState();
161
162    this->setRelCoorSoft2D(0, -(int)this->shellHeight, 5);
163
164    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
165      (*text)->setVisibility(false);
166    // Go to the End again.
167    this->bufferIterator = ShellBuffer::getInstance()->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   * @brief repositiones all the Texts to their position.
241   */
242  void Shell::repositionText()
243  {
244    int linePos = -1;
245    std::list<MultiLineText*>::iterator textIt;
246    for (textIt = this->bufferText.begin() ; textIt != this->bufferText.end(); ++textIt )
247    {
248      linePos += (*textIt)->getLineCount();
249      (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(linePos), 8);
250    }
251  }
252
253
254  /**
255   * @brief applies the Shells Settings to a single Text of the Shell.
256   * @param text the Text to apply the settings to.
257   */
258  void Shell::applyTextSettings(Text* text)
259  {
260    text->setSize(this->textSize);
261    text->setFont(this->fontFile, this->textSize);
262    text->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
263    text->setBlending(this->textColor[3]);
264    text->setLayer(this->getLayer());
265    if (text->getParent2D() != this)
266      text->setParent2D(this);
267  }
268
269  /**
270   * @brief resets the Values of all visible shell's commandos to the Shell's stored values
271   *
272   * this functions synchronizes the stored Data with the visible one.
273   */
274  void Shell::applySettings()
275  {
276    this->applyTextSettings(&this->shellInput);
277    this->shellInput.setRelCoor2D(15, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
278
279    /* Here we create a Copy of the Buffer, so that we do not f**k up the List when some
280     * output is routed from Some other Thread or by Changing any Values.
281     */
282    std::list<MultiLineText*> bufferTextCopy = this->bufferText;
283    for (std::list<MultiLineText*>::iterator textIt = bufferTextCopy.begin(); textIt != bufferTextCopy.end(); ++textIt)
284    {
285      this->applyTextSettings(*textIt);
286      (*textIt)->setLineSpacing(this->lineSpacing);
287      (*textIt)->setLineWidth(this->getSizeX2D() * GraphicsEngine::getInstance()->getResolutionX());
288    }
289    this->repositionText();
290
291    this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
292  }
293
294  /**
295   * @brief sets The count of Lines to display in the buffer.
296   * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
297   */
298  void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
299  {
300    unsigned int oldSize = this->bufferText.size();
301    if (oldSize > bufferDisplaySize)
302    {
303      for (unsigned int i = bufferDisplaySize; i <= oldSize; i++)
304      {
305        delete this->bufferText.back();
306        this->bufferText.pop_back();
307      }
308    }
309    else if (oldSize < bufferDisplaySize)
310    {
311      for (unsigned int i = oldSize; i <= bufferDisplaySize; i++)
312      {
313        this->bufferText.push_back(new MultiLineText);
314      }
315    }
316    this->bufferDisplaySize = bufferDisplaySize;
317    this->applySettings();
318  }
319
320  /**
321   * @brief deletes all the Buffers
322   */
323  void Shell::flush()
324  {
325    for (std::list<MultiLineText*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
326    {
327      (*textIt)->setText("");  // remove all chars from the BufferTexts.
328    }
329    ShellBuffer::getInstance()->flush();
330    // BUFFER FLUSHING
331  }
332
333  /**
334   * @brief prints out some text to the input-buffers
335   * @param text the text to output.
336   */
337  void Shell::printToDisplayBuffer(const std::string& text)
338  {
339    // Remove Last Entry and prepend it to the front.
340    this->bufferText.push_front(this->bufferText.back());
341    this->bufferText.pop_back();
342
343    // Set the new Text.
344    this->bufferText.front()->setText(text);
345
346    // The LineCount will be started here.
347
348    // The First Line gets a special Animation
349    this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(0)- Vector2D(-1000,0));
350
351    // Move all lines one Entry up.
352    this->repositionText();
353  }
354
355
356  /**
357   * @brief moves the Display buffer (up + or down - )
358   * @param lineCount the count by which to shift the InputBuffer.
359   *
360   * @todo make this work
361   */
362  void Shell::moveDisplayBuffer(int lineCount)
363  {
364    // moving the iterator to the right position (counting the steps)
365    int moves = 0;
366    while (moves != lineCount)
367    {
368      if (moves < lineCount)
369      {
370        if(this->bufferIterator == --ShellBuffer::getInstance()->getBuffer().end())
371          break;
372        ++moves;
373        ++this->bufferIterator;
374      }
375      else
376      {
377        if (this->bufferIterator == ShellBuffer::getInstance()->getBuffer().begin())
378          break;
379        --moves;
380        --this->bufferIterator;
381      }
382    }
383
384
385    // redisplay the buffers
386    int linePos = moves;
387    std::list<std::string>::const_iterator it = this->bufferIterator;
388    std::list<MultiLineText*>::iterator textIt;
389    for (textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt, ++it)
390    {
391      if (it == ShellBuffer::getInstance()->getBuffer().end())
392      {
393        PRINTF(1)("LAST LINE REACHED\n");
394        break;
395      }
396
397
398      (*textIt)->setRelCoor2D(calculateLinePosition( (linePos++ > 0)? linePos : 0));
399      (*textIt)->setText((*it));
400    }
401    while (textIt != this->bufferText.end())
402    {
403      (*textIt)->setText("");
404      textIt++;
405    }
406
407    this->repositionText();
408  }
409
410  /**
411   * @brief clears the Shell (empties all buffers)
412   */
413  void Shell::clear()
414  {
415    this->flush();
416    ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
417  }
418
419  /**
420   * @brief listens for some event
421   * @param event the Event happened
422   */
423  void Shell::process(const Event &event)
424  {
425    if (event.bPressed)
426    {
427      if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
428      {
429        if (this->bActive == false)
430          this->activate();
431        else
432          this->deactivate();
433      }
434      else if (event.type == SDLK_PAGEUP)
435      {
436        this->moveDisplayBuffer(+this->bufferDisplaySize-1);
437      }
438      else if (event.type == SDLK_PAGEDOWN)
439      {
440        this->moveDisplayBuffer(-this->bufferDisplaySize+1);
441      }
442    }
443  }
444
445  /**
446   * displays the Shell
447   */
448  void Shell::draw() const
449  {
450    // transform for alignment.
451    // setting the Blending effects
452
453    this->backgroundMaterial.select();
454
455    glBegin(GL_TRIANGLE_STRIP);
456
457    glTexCoord2f(0, 0);
458    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
459
460    glTexCoord2f(1, 0);
461    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
462
463    glTexCoord2f(0, 1);
464    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
465
466    glTexCoord2f(1, 1);
467    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
468
469    glEnd();
470  }
471
472  ///////////////////////
473  // HELPER FUNCTIONS  //
474  ///////////////////////
475
476  /**
477   * @brief calculates the position of a Buffer-Display Line
478   * @param lineNumber the lineNumber from the bottom to calculate the position from
479   * @returns the Position of the Line.
480   */
481  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
482  {
483    return Vector2D(5, (int)(this->textSize + this->lineSpacing)*(int)((int)this->bufferDisplaySize - (int)lineNumber - (int)2) + (int)this->textSize);
484  }
485
486
487
488  /**
489   * @brief displays some nice output from the Shell
490   */
491  void Shell::debug() const
492  {
493    PRINT(3)("Debugging output to console (not this shell)\n");
494
495    //   if (this->pressedKey != SDLK_FIRST)
496    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
497
498
499    ShellBuffer::getInstance()->debug();
500  }
501}
Note: See TracBrowser for help on using the repository browser.