Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/hud/src/lib/shell/shell.cc @ 10647

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

orxonox/trunk: Windows runs again without any segfaults
it seems, that windows links object files differently… and extremely strange

File size: 14.5 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 "debug_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#include "debug.h"
29
30namespace OrxShell
31{
32  ObjectListDefinition(Shell);
33
34  /**
35   * standard constructor
36   */
37  Shell::Shell ()
38  {
39    this->registerObject(this, Shell::_objectList);
40
41    this->setName("Shell");
42
43    this->shellBuffer = DebugBuffer::getInstance();
44
45    // EVENT-Handler subscription of '`' to all States.
46    this->subscribeEvent(ES_ALL, SDLK_BACKQUOTE);
47    this->subscribeEvent(ES_ALL, SDLK_F12);
48    this->subscribeEvent(ES_SHELL, SDLK_PAGEUP);
49    this->subscribeEvent(ES_SHELL, SDLK_PAGEDOWN);
50    this->subscribeEvent(ES_SHELL, EV_VIDEO_RESIZE);
51    EventHandler::getInstance()->withUNICODE(ES_SHELL, true);
52
53    // BUFFER
54    this->bufferIterator = this->shellBuffer->getBuffer().begin();
55
56    // INPUT LINE
57    this->setLayer(E2D_LAYER_ABOVE_ALL);
58    this->shellInput.setLayer(E2D_LAYER_ABOVE_ALL);
59
60    // Element2D and generals
61    this->setSizeX2D(GraphicsEngine::getInstance()->getResolutionX());
62    this->setAbsCoor2D(3, -400);
63    this->textSize = 15;
64    this->lineSpacing = 0;
65    this->bActive = false;
66    this->fontFile = SHELL_DEFAULT_FONT;
67
68    this->setBufferDisplaySize(10);
69
70    this->setTextColor(SHELL_DEFAULT_TEXT_COLOR);
71    this->setBackgroundColor(SHELL_DEFAULT_BACKGROUND_COLOR);
72
73
74    this->deactivate();
75  }
76
77  /**
78   * @brief standard deconstructor
79   */
80  Shell::~Shell ()
81  {
82    // delete the displayable Buffers
83    while (!this->bufferText.empty())
84    {
85      delete this->bufferText.front();
86      this->bufferText.pop_front();
87    }
88  }
89
90  /**
91   * @brief activates the shell
92   *
93   * This also feeds the Last few lines from the main buffers into the displayBuffer
94   */
95  void Shell::activate()
96  {
97    if (this->bActive == true)
98      PRINTF(3)("The shell is already active\n");
99    this->bActive = true;
100    this->activate2D();
101
102    EventHandler::getInstance()->pushState(ES_SHELL);
103
104    this->setRelCoorSoft2D(0, 0, 5);
105
106    this->linesProcessed = this->shellBuffer->getLineCount();
107    std::list<std::string>::const_iterator textLine = this->bufferIterator = this->shellBuffer->getBuffer().begin();
108    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
109    {
110      (*text)->setVisibility(true);
111      if (textLine !=  this->shellBuffer->getBuffer().end())
112      {
113        (*text)->setText((*textLine));
114        textLine++;
115      }
116      else
117        (*text)->setText("");
118    }
119    this->shellInput.setVisibility(true);
120    this->updateResolution( GraphicsEngine::getInstance()->getResolutionX());
121    this->repositionText();
122  }
123
124  /**
125   * @brief deactiveates the Shell.
126   */
127  void Shell::deactivate()
128  {
129    if (this->bActive == false)
130      PRINTF(3)("The shell is already inactive\n");
131    this->bActive = false;
132    this->deactivate2D();
133
134    EventHandler::getInstance()->popState();
135
136    this->setRelCoorSoft2D(0, -(int)this->shellHeight, 5);
137
138    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
139      (*text)->setVisibility(false);
140    this->shellInput.setVisibility(false);
141
142    // Go to the End again.
143    this->bufferIterator = this->shellBuffer->getBuffer().begin();
144  }
145
146  /**
147   * @brief sets the File to load the fonts from
148   * @param fontFile the file to load the font from
149   *
150   * it is quite important, that the font pointed too really exists!
151   * (be aware that within orxonox fontFile is relative to the Data-Dir)
152   */
153  void Shell::setFont(const std::string& fontFile)
154  {
155    this->fontFile = fontFile;
156
157    this->applySettings();
158  }
159
160  /**
161   * @brief sets the size of the text and spacing
162   * @param textSize the size of the Text in Pixels
163   * @param lineSpacing the size of the Spacing between two lines in pixels
164   *
165   * this also rebuilds the entire Text, inputLine and displayBuffer,
166   * to be accurate again.
167   */
168  void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
169  {
170    this->textSize = textSize;
171    this->lineSpacing = lineSpacing;
172
173    this->applySettings();
174  }
175
176  /**
177   * @brief sets the color of the Font.
178   * @param r: red
179   * @param g: green
180   * @param b: blue
181   * @param a: alpha-value.
182   */
183  void Shell::setTextColor(float r, float g, float b, float a)
184  {
185    this->textColor[0] = r;
186    this->textColor[1] = g;
187    this->textColor[2] = b;
188    this->textColor[3] = a;
189
190    this->applySettings();
191  }
192
193  /**
194   * @brief sets the color of the Backgrond.
195   * @param r: red
196   * @param g: green
197   * @param b: blue
198   * @param a: alpha-value.
199   */
200  void Shell::setBackgroundColor(float r, float g, float b, float a)
201  {
202    this->backgroundMaterial.setDiffuse(r, g, b);
203    this->backgroundMaterial.setTransparency(a);
204  }
205
206  /**
207   * @brief sets a nice background image to the Shell's background
208   * @param fileName the filename of the Image to load
209   */
210  void Shell::setBackgroundImage(const std::string& fileName)
211  {
212    this->backgroundMaterial.setDiffuseMap(fileName);
213  }
214
215
216  /**
217   * @brief updates the Shell's Width
218   * @param width the new Width.
219   */
220  void Shell::updateResolution(unsigned int width)
221  {
222    if (width == this->getSizeX2D())
223      return;
224    this->setSizeX2D(width);
225    for (std::list<MultiLineText*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
226      (*textIt)->setLineWidth(width);
227  }
228
229  /**
230   * @brief repositiones all the Texts to their position.
231   */
232  void Shell::repositionText()
233  {
234    int linePos = -1;
235    std::list<MultiLineText*>::iterator textIt;
236    for (textIt = this->bufferText.begin() ; textIt != this->bufferText.end(); ++textIt )
237    {
238      linePos += (*textIt)->getLineCount();
239      (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(linePos), 8);
240    }
241  }
242
243
244  /**
245   * @brief applies the Shells Settings to a single Text of the Shell.
246   * @param text the Text to apply the settings to.
247   */
248  void Shell::applyTextSettings(Text* text)
249  {
250    text->setSize(this->textSize);
251    text->setFont(this->fontFile, this->textSize);
252    text->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
253    text->setBlending(this->textColor[3]);
254    text->setLayer(this->getLayer());
255    if (text->getParent2D() != this)
256      text->setParent2D(this);
257  }
258
259  /**
260   * @brief resets the Values of all visible shell's commandos to the Shell's stored values
261   *
262   * this functions synchronizes the stored Data with the visible one.
263   */
264  void Shell::applySettings()
265  {
266    this->applyTextSettings(&this->shellInput);
267    this->shellInput.setRelCoor2D(15, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
268
269    /* Here we create a Copy of the Buffer, so that we do not f**k up the List when some
270     * output is routed from Some other Thread or by Changing any Values.
271     */
272    std::list<MultiLineText*> bufferTextCopy = this->bufferText;
273    for (std::list<MultiLineText*>::iterator textIt = bufferTextCopy.begin(); textIt != bufferTextCopy.end(); ++textIt)
274    {
275      this->applyTextSettings(*textIt);
276      (*textIt)->setLineSpacing(this->lineSpacing);
277      (*textIt)->setLineWidth(this->getSizeX2D() * GraphicsEngine::getInstance()->getResolutionX());
278    }
279    this->repositionText();
280
281    this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
282  }
283
284  /**
285   * @brief sets The count of Lines to display in the buffer.
286   * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
287   */
288  void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
289  {
290    unsigned int oldSize = this->bufferText.size();
291    if (oldSize > bufferDisplaySize)
292    {
293      for (unsigned int i = bufferDisplaySize; i <= oldSize; i++)
294      {
295        delete this->bufferText.back();
296        this->bufferText.pop_back();
297      }
298    }
299    else if (oldSize < bufferDisplaySize)
300    {
301      for (unsigned int i = oldSize; i <= bufferDisplaySize; i++)
302      {
303        this->bufferText.push_back(new MultiLineText);
304      }
305    }
306    this->bufferDisplaySize = bufferDisplaySize;
307    this->applySettings();
308  }
309
310  /**
311   * @brief deletes all the Buffers
312   */
313  void Shell::flush()
314  {
315    for (std::list<MultiLineText*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
316    {
317      (*textIt)->setText("");  // remove all chars from the BufferTexts.
318    }
319    this->shellBuffer->flush();
320    // BUFFER FLUSHING
321  }
322
323  /**
324   * @brief prints out some text to the input-buffers
325   * @param text the text to output.
326   */
327  void Shell::printToDisplayBuffer(const std::string& text)
328  {
329    // Remove Last Entry and prepend it to the front.
330    this->bufferText.push_front(this->bufferText.back());
331    this->bufferText.pop_back();
332
333    // Set the new Text.
334    this->bufferText.front()->setText(text);
335    this->bufferIterator = this->shellBuffer->getBuffer().begin();
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 == --this->shellBuffer->getBuffer().end())
362          break;
363        ++moves;
364        ++this->bufferIterator;
365      }
366      else
367      {
368        if (this->bufferIterator == this->shellBuffer->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 == this->shellBuffer->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    DebugBuffer::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    else if (event.type == EV_VIDEO_RESIZE)
450    {
451      this->updateResolution(event.resize.w);
452      this->repositionText();
453    }
454  }
455
456  void Shell::tick(float dt)
457  {
458    if (this->linesProcessed < this->shellBuffer->getLineCount())
459    {
460      unsigned int pollLines = this->shellBuffer->getLineCount() - this->linesProcessed;
461      if (this->bufferText.size() < pollLines)
462        pollLines = this->bufferText.size();
463      if (unlikely(this->shellBuffer->getBuffer().size() < pollLines))
464        pollLines = this->shellBuffer->getBuffer().size();
465
466      std::list<std::string>::const_iterator line = this->shellBuffer->getBuffer().begin();
467      unsigned int i;
468      for(i = 0; i < pollLines; i++)
469        line ++;
470      for (i = 0; i < pollLines; i++)
471      {
472        this->printToDisplayBuffer((*--line));
473      }
474    }
475    this->linesProcessed = this->shellBuffer->getLineCount();
476  }
477
478
479  /**
480   * displays the Shell
481   */
482  void Shell::draw() const
483  {
484    // transform for alignment.
485    // setting the Blending effects
486
487    this->backgroundMaterial.select();
488
489    glBegin(GL_TRIANGLE_STRIP);
490
491    glTexCoord2f(0, 0);
492    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
493
494    glTexCoord2f(1, 0);
495    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
496
497    glTexCoord2f(0, 1);
498    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
499
500    glTexCoord2f(1, 1);
501    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
502
503    glEnd();
504  }
505
506  ///////////////////////
507  // HELPER FUNCTIONS  //
508  ///////////////////////
509
510  /**
511   * @brief calculates the position of a Buffer-Display Line
512   * @param lineNumber the lineNumber from the bottom to calculate the position from
513   * @returns the Position of the Line.
514   */
515  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
516  {
517    return Vector2D(5.0f, (float)(this->textSize + this->lineSpacing)*(float)((int)this->bufferDisplaySize - (int)lineNumber - (int)1));
518  }
519
520
521
522  /**
523   * @brief displays some nice output from the Shell
524   */
525  void Shell::debug() const
526  {
527    PRINT(3)("Debugging output to console (not this shell)\n");
528
529    //   if (this->pressedKey != SDLK_FIRST)
530    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
531
532
533    this->shellBuffer->debug();
534  }
535}
Note: See TracBrowser for help on using the repository browser.