Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Repositioning

File size: 14.6 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   * standard constructor
57   */
58  Shell::Shell ()
59  {
60    this->setClassID(CL_SHELL, "Shell");
61    this->setName("Shell");
62
63    // EVENT-Handler subscription of '`' to all States.
64    EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
65    EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_F12);
66    EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
67    EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
68
69    // BUFFER
70    this->bufferOffset = 0;
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().end();
127    bool top = false;
128    for (std::list<MultiLineText*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
129    {
130      (*text)->setVisibility(true);
131      if (!top)
132      {
133        (*text)->setText((*textLine));
134        if (textLine != ShellBuffer::getInstance()->getBuffer().begin())
135          top = true;
136        textLine--;
137      }
138    }
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    this->bufferOffset = 0;
158  }
159
160  /**
161   * @brief sets the File to load the fonts from
162   * @param fontFile the file to load the font from
163   *
164   * it is quite important, that the font pointed too really exists!
165   * (be aware that within orxonox fontFile is relative to the Data-Dir)
166   */
167  void Shell::setFont(const std::string& fontFile)
168  {
169    this->fontFile = fontFile;
170
171    this->applySettings();
172  }
173
174  /**
175   * @brief sets the size of the text and spacing
176   * @param textSize the size of the Text in Pixels
177   * @param lineSpacing the size of the Spacing between two lines in pixels
178   *
179   * this also rebuilds the entire Text, inputLine and displayBuffer,
180   * to be accurate again.
181   */
182  void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
183  {
184    this->textSize = textSize;
185    this->lineSpacing = lineSpacing;
186
187    this->applySettings();
188  }
189
190  /**
191   * @brief sets the color of the Font.
192   * @param r: red
193   * @param g: green
194   * @param b: blue
195   * @param a: alpha-value.
196   */
197  void Shell::setTextColor(float r, float g, float b, float a)
198  {
199    this->textColor[0] = r;
200    this->textColor[1] = g;
201    this->textColor[2] = b;
202    this->textColor[3] = a;
203
204    this->applySettings();
205  }
206
207  /**
208   * @brief sets the color of the Backgrond.
209   * @param r: red
210   * @param g: green
211   * @param b: blue
212   * @param a: alpha-value.
213   */
214  void Shell::setBackgroundColor(float r, float g, float b, float a)
215  {
216    this->backgroundMaterial.setDiffuse(r, g, b);
217    this->backgroundMaterial.setTransparency(a);
218  }
219
220  /**
221   * @brief sets a nice background image to the Shell's background
222   * @param fileName the filename of the Image to load
223   */
224  void Shell::setBackgroundImage(const std::string& fileName)
225  {
226    this->backgroundMaterial.setDiffuseMap(fileName);
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->setFont(this->fontFile, this->textSize);
251    text->setSize(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    ShellBuffer::getInstance()->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
336    // The LineCount will be started here.
337
338    // The First Line gets a special Animation
339    this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(0)- Vector2D(-1000,0));
340
341    // Move all lines one Entry up.
342    this->repositionText();
343  }
344
345
346  /**
347   * @brief moves the Display buffer (up + or down - )
348   * @param lineCount the count by which to shift the InputBuffer.
349   *
350   * @todo make this work
351   */
352  void Shell::moveDisplayBuffer(int lineCount)
353  {
354    if (this->bufferOffset == 0)
355    {
356      this->bufferIterator = ShellBuffer::getInstance()->getBuffer().end();
357      //     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
358      //       this->bufferIterator->prevStep();
359    }
360
361    // boundraries
362    if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer().size())
363      lineCount = (int)ShellBuffer::getInstance()->getBuffer().size()- this->bufferOffset;
364    else if (this->bufferOffset + lineCount < 0)
365      lineCount = -bufferOffset;
366    this->bufferOffset += lineCount;
367
368    // moving the iterator to the right position
369    int move = 0;
370    while (move != lineCount)
371    {
372      if (move < lineCount)
373      {
374        ++move;
375        this->bufferIterator--;
376      }
377      else
378      {
379        --move;
380        this->bufferIterator++;
381      }
382    }
383    // redisplay the buffers
384    std::list<std::string>::const_iterator it = this->bufferIterator;
385    if (it == ShellBuffer::getInstance()->getBuffer().end())
386    {
387      /// FIXME
388      PRINTF(1)("Should not happen\n");
389      it--;
390    }
391    for (std::list<MultiLineText*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
392    {
393      (*textIt)->setText((*it));
394      if (it == ShellBuffer::getInstance()->getBuffer().begin())
395      {
396        /// FIXME
397        PRINTF(1)("Should not happen\n");
398        break;
399      }
400      it--;
401    }
402  }
403
404  /**
405   * @brief clears the Shell (empties all buffers)
406   */
407  void Shell::clear()
408  {
409    this->flush();
410    ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
411  }
412
413  /**
414   * @brief listens for some event
415   * @param event the Event happened
416   */
417  void Shell::process(const Event &event)
418  {
419    if (event.bPressed)
420    {
421      if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
422      {
423        if (this->bActive == false)
424          this->activate();
425        else
426          this->deactivate();
427      }
428      else if (event.type == SDLK_PAGEUP)
429      {
430        this->moveDisplayBuffer(+this->bufferDisplaySize-1);
431      }
432      else if (event.type == SDLK_PAGEDOWN)
433      {
434        this->moveDisplayBuffer(-this->bufferDisplaySize+1);
435      }
436    }
437  }
438
439  /**
440   * displays the Shell
441   */
442  void Shell::draw() const
443  {
444    // transform for alignment.
445    // setting the Blending effects
446
447    this->backgroundMaterial.select();
448
449    glBegin(GL_TRIANGLE_STRIP);
450
451    glTexCoord2f(0, 0);
452    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
453
454    glTexCoord2f(1, 0);
455    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
456
457    glTexCoord2f(0, 1);
458    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
459
460    glTexCoord2f(1, 1);
461    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
462
463    glEnd();
464  }
465
466  ///////////////////////
467  // HELPER FUNCTIONS  //
468  ///////////////////////
469
470  /**
471   * @brief calculates the position of a Buffer-Display Line
472   * @param lineNumber the lineNumber from the bottom to calculate the position from
473   * @returns the Position of the Line.
474   */
475  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
476  {
477    return Vector2D(5, (int)(this->textSize + this->lineSpacing)*(int)((int)this->bufferDisplaySize - (int)lineNumber - (int)2) + (int)this->textSize);
478  }
479
480
481
482  /**
483   * @brief displays some nice output from the Shell
484   */
485  void Shell::debug() const
486  {
487    PRINT(3)("Debugging output to console (not this shell)\n");
488
489    //   if (this->pressedKey != SDLK_FIRST)
490    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
491
492
493    ShellBuffer::getInstance()->debug();
494  }
495
496  // void Shell::testI (int i)
497  // {
498  //   PRINTF(3)("This is the Test for one Int '%d'\n", i);
499  // }
500  //
501  // void Shell::testS (const char* s)
502  // {
503  //   PRINTF(3)("This is the Test for one String '%s'\n", s);
504  // }
505  //
506  // void Shell::testB (bool b)
507  // {
508  //   PRINTF(3)("This is the Test for one Bool: ");
509  //   if (b)
510  //     PRINTF(3)("true\n");
511  //   else
512  //     PRINTF(3)("false\n");
513  // }
514  //
515  // void Shell::testF (float f)
516  // {
517  //   PRINTF(3)("This is the Test for one Float '%f'\n", f);
518  // }
519  //
520  // void Shell::testSF (const char* s, float f)
521  // {
522  //   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
523  // }
524
525}
Note: See TracBrowser for help on using the repository browser.