Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: graphical representation of the move in the Shell

File size: 14.3 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    std::list<MultiLineText*>::iterator textIt;
387
388    // Give a Graphical Representation that no move is possible.
389    if (moves == 0)
390    {
391      int linePos = -1;
392      for (textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
393      {
394        linePos += (*textIt)->getLineCount();
395        (*textIt)->setRelCoor2D(this->calculateLinePosition(linePos)+ Vector2D(20,0));
396        (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(linePos), 10);
397      }
398      return;
399    }
400
401    // Move all the Lines.
402    int linePos = moves;
403    std::list<std::string>::const_iterator it = this->bufferIterator;
404    for (textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt, ++it)
405    {
406      if (it == ShellBuffer::getInstance()->getBuffer().end())
407      {
408        PRINTF(1)("LAST LINE REACHED\n");
409        break;
410      }
411
412
413      (*textIt)->setRelCoor2D(calculateLinePosition( (linePos++ > 0)? linePos : 0));
414      (*textIt)->setText((*it));
415    }
416    while (textIt != this->bufferText.end())
417    {
418      (*textIt)->setText("");
419      textIt++;
420    }
421
422    this->repositionText();
423  }
424
425  /**
426   * @brief clears the Shell (empties all buffers)
427   */
428  void Shell::clear()
429  {
430    this->flush();
431    ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
432  }
433
434  /**
435   * @brief listens for some event
436   * @param event the Event happened
437   */
438  void Shell::process(const Event &event)
439  {
440    if (event.bPressed)
441    {
442      if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
443      {
444        if (this->bActive == false)
445          this->activate();
446        else
447          this->deactivate();
448      }
449      else if (event.type == SDLK_PAGEUP)
450      {
451        this->moveDisplayBuffer(+this->bufferDisplaySize-1);
452      }
453      else if (event.type == SDLK_PAGEDOWN)
454      {
455        this->moveDisplayBuffer(-this->bufferDisplaySize+1);
456      }
457    }
458  }
459
460  /**
461   * displays the Shell
462   */
463  void Shell::draw() const
464  {
465    // transform for alignment.
466    // setting the Blending effects
467
468    this->backgroundMaterial.select();
469
470    glBegin(GL_TRIANGLE_STRIP);
471
472    glTexCoord2f(0, 0);
473    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
474
475    glTexCoord2f(1, 0);
476    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
477
478    glTexCoord2f(0, 1);
479    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
480
481    glTexCoord2f(1, 1);
482    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
483
484    glEnd();
485  }
486
487  ///////////////////////
488  // HELPER FUNCTIONS  //
489  ///////////////////////
490
491  /**
492   * @brief calculates the position of a Buffer-Display Line
493   * @param lineNumber the lineNumber from the bottom to calculate the position from
494   * @returns the Position of the Line.
495   */
496  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
497  {
498    return Vector2D(5, (int)(this->textSize + this->lineSpacing)*(int)((int)this->bufferDisplaySize - (int)lineNumber - (int)2) + (int)this->textSize);
499  }
500
501
502
503  /**
504   * @brief displays some nice output from the Shell
505   */
506  void Shell::debug() const
507  {
508    PRINT(3)("Debugging output to console (not this shell)\n");
509
510    //   if (this->pressedKey != SDLK_FIRST)
511    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
512
513
514    ShellBuffer::getInstance()->debug();
515  }
516}
Note: See TracBrowser for help on using the repository browser.