Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: some movements

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