Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: fixed the last few bugs (that i found) when scrolling

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