Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellFont can be changed in threads :)

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
24#include "text.h"
25#include "graphics_engine.h"
26#include "material.h"
27#include "event_handler.h"
28#include "debug.h"
29#include "class_list.h"
30
31#include "key_names.h"
32#include <stdarg.h>
33#include <stdio.h>
34
35namespace OrxShell
36{
37
38  SHELL_COMMAND(clear, Shell, clear)
39  ->describe("Clears the shell from unwanted lines (empties all buffers)")
40  ->setAlias("clear");
41  SHELL_COMMAND(deactivate, Shell, deactivate)
42  ->describe("Deactivates the Shell. (moves it into background)")
43  ->setAlias("hide");
44  SHELL_COMMAND(textsize, Shell, setTextSize)
45  ->describe("Sets the size of the Text size, linespacing")
46  ->defaultValues(15, 0);
47  SHELL_COMMAND(textcolor, Shell, setTextColor)
48  ->describe("Sets the Color of the Shells Text (red, green, blue, alpha)")
49  ->defaultValues(SHELL_DEFAULT_TEXT_COLOR);
50  SHELL_COMMAND(backgroundcolor, Shell, setBackgroundColor)
51  ->describe("Sets the Color of the Shells Background (red, green, blue, alpha)")
52  ->defaultValues(SHELL_DEFAULT_BACKGROUND_COLOR);
53  SHELL_COMMAND(backgroundimage, Shell, setBackgroundImage)
54  ->describe("sets the background image to load for the Shell")
55  ->completionPlugin(0, OrxShell::CompletorFileSystem());
56  SHELL_COMMAND(font, Shell, setFont)
57  ->describe("Sets the font of the Shell")
58  ->defaultValues(SHELL_DEFAULT_FONT)
59      ->completionPlugin(0, OrxShell::CompletorFileSystem(".ttf", "fonts/"));
60
61  /**
62   * standard constructor
63   */
64  Shell::Shell ()
65  {
66    this->setClassID(CL_SHELL, "Shell");
67    this->setName("Shell");
68
69    // EVENT-Handler subscription of '`' to all States.
70    EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
71    EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_F12);
72    EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
73    EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
74
75    // BUFFER
76    this->bufferOffset = 0;
77    this->bufferIterator = ShellBuffer::getInstance()->getBuffer().begin();
78
79    // INPUT LINE
80    this->setLayer(E2D_LAYER_ABOVE_ALL);
81    this->shellInput.setLayer(E2D_LAYER_ABOVE_ALL);
82
83    this->backgroundMaterial = new Material;
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    // delete the inputLine
118    delete this->backgroundMaterial;
119  }
120
121  /**
122   * @brief activates the shell
123   *
124   * This also feeds the Last few lines from the main buffers into the displayBuffer
125   */
126  void Shell::activate()
127  {
128    if (this->bActive == true)
129      PRINTF(3)("The shell is already active\n");
130    this->bActive = true;
131
132    EventHandler::getInstance()->pushState(ES_SHELL);
133    EventHandler::getInstance()->withUNICODE(true);
134
135    this->setRelCoorSoft2D(0, 0, 5);
136
137    std::list<std::string>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer().end();
138    bool top = false;
139    for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
140    {
141      (*text)->setVisibility(true);
142      if (!top)
143      {
144        (*text)->setText((*textLine));
145        if (textLine != ShellBuffer::getInstance()->getBuffer().begin())
146          top = true;
147        textLine--;
148      }
149    }
150  }
151
152  /**
153   * @brief deactiveates the Shell.
154   */
155  void Shell::deactivate()
156  {
157    if (this->bActive == false)
158      PRINTF(3)("The shell is already inactive\n");
159    this->bActive = false;
160
161    EventHandler::getInstance()->withUNICODE(false);
162    EventHandler::getInstance()->popState();
163
164    this->setRelCoorSoft2D(0, -(int)this->shellHeight, 5);
165
166    for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
167      (*text)->setVisibility(false);
168    this->bufferOffset = 0;
169  }
170
171  /**
172   * @brief sets the File to load the fonts from
173   * @param fontFile the file to load the font from
174   *
175   * it is quite important, that the font pointed too really exists!
176   * (be aware that within orxonox fontFile is relative to the Data-Dir)
177   */
178  void Shell::setFont(const std::string& fontFile)
179  {
180    //   if (!ResourceManager::isInDataDir(fontFile))
181    //     return false;
182
183    this->fontFile = fontFile;
184
185    this->resetValues();
186  }
187
188  /**
189   * @brief sets the size of the text and spacing
190   * @param textSize the size of the Text in Pixels
191   * @param lineSpacing the size of the Spacing between two lines in pixels
192   *
193   * this also rebuilds the entire Text, inputLine and displayBuffer,
194   * to be accurate again.
195   */
196  void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
197  {
198    this->textSize = textSize;
199    this->lineSpacing = lineSpacing;
200
201    this->resetValues();
202  }
203
204  /**
205   * @brief sets the color of the Font.
206   * @param r: red
207   * @param g: green
208   * @param b: blue
209   * @param a: alpha-value.
210   */
211  void Shell::setTextColor(float r, float g, float b, float a)
212  {
213    this->textColor[0] = r;
214    this->textColor[1] = g;
215    this->textColor[2] = b;
216    this->textColor[3] = a;
217
218    this->resetValues();
219  }
220
221
222  /**
223   * @brief sets the color of the Backgrond.
224   * @param r: red
225   * @param g: green
226   * @param b: blue
227   * @param a: alpha-value.
228   */
229  void Shell::setBackgroundColor(float r, float g, float b, float a)
230  {
231    this->backgroundMaterial->setDiffuse(r, g, b);
232    this->backgroundMaterial->setTransparency(a);
233  }
234
235  /**
236   * @brief sets a nice background image to the Shell's background
237   * @param fileName the filename of the Image to load
238   */
239  void Shell::setBackgroundImage(const std::string& fileName)
240  {
241    this->backgroundMaterial->setDiffuseMap(fileName);
242  }
243
244
245  /**
246   * @brief resets the Values of all visible shell's commandos to the Shell's stored values
247   *
248   * this functions synchronizes the stored Data with the visible one.
249   */
250  void Shell::resetValues()
251  {
252    this->resetText(&this->shellInput, 0);
253    this->shellInput.setRelCoor2D(15, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
254
255    unsigned int i = 0;
256    /* Here we create a Copy of the Buffer, so that we do not f**k up the List when some
257     * output is routed from Some other Thread or by Changing any Values.
258     */
259    std::list<Text*> bufferTextCopy = this->bufferText;
260    for (std::list<Text*>::iterator text = bufferTextCopy.begin(); text != bufferTextCopy.end(); ++text, ++i)
261    {
262      this->resetText(*text, i);
263      (*text)->setRelCoor2D( calculateLinePosition(i) );
264    }
265    this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
266  }
267
268  void Shell::resetText(Text* text, unsigned int position)
269  {
270    text->setFont(this->fontFile, this->textSize);
271    text->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
272    text->setBlending(this->textColor[3]);
273    text->setLayer(this->getLayer());
274    if (text->getParent2D() != this)
275      text->setParent2D(this);
276  }
277
278
279  /**
280   * @brief sets The count of Lines to display in the buffer.
281   * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
282   */
283  void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
284  {
285    unsigned int oldSize = this->bufferText.size();
286    if (oldSize > bufferDisplaySize)
287    {
288      for (unsigned int i = bufferDisplaySize; i <= oldSize; i++)
289      {
290        delete this->bufferText.back();
291        this->bufferText.pop_back();
292      }
293    }
294    else if (oldSize < bufferDisplaySize)
295    {
296      for (unsigned int i = oldSize; i <= bufferDisplaySize; i++)
297      {
298        this->bufferText.push_back(new Text);
299      }
300    }
301    this->bufferDisplaySize = bufferDisplaySize;
302    this->resetValues();
303  }
304
305  /**
306   * @brief deletes all the Buffers
307   */
308  void Shell::flush()
309  {
310    for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
311    {
312      (*text)->setText("");  // remove all chars from the BufferTexts.
313    }
314    ShellBuffer::getInstance()->flush();
315    // BUFFER FLUSHING
316  }
317
318  /**
319   * @brief prints out some text to the input-buffers
320   * @param text the text to output.
321   */
322  void Shell::printToDisplayBuffer(const std::string& text)
323  {
324    this->bufferText.push_front(this->bufferText.back());
325    this->bufferText.pop_back();
326
327
328    unsigned int i = 0;
329    for (std::list<Text*>::iterator textIt = ++this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt, i++)
330    {
331      (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(i+1), 5);
332    }
333
334    this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(0)- Vector2D(-1000,0));
335    this->bufferText.front()->setRelCoorSoft2D(this->calculateLinePosition(0),10);
336
337    /*  FANCY EFFECTS :)
338      1:
339          lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1000,0,0));
340          lastText->setRelCoorSoft2D(this->calculateLinePosition(0),10);
341      2:
342      lastText->setRelDir2D(-90);
343      lastText->setRelDirSoft2D(0, 20);
344    */
345
346    this->bufferText.front()->setText(text);
347  }
348
349  /**
350   * moves the Display buffer (up + or down - )
351   * @param lineCount the count by which to shift the InputBuffer.
352   *
353   * @todo make this work
354   */
355  void Shell::moveDisplayBuffer(int lineCount)
356  {
357    if (this->bufferOffset == 0)
358    {
359      this->bufferIterator = ShellBuffer::getInstance()->getBuffer().end();
360      //     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
361      //       this->bufferIterator->prevStep();
362    }
363
364    // boundraries
365    if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer().size())
366      lineCount = (int)ShellBuffer::getInstance()->getBuffer().size()- this->bufferOffset;
367    else if (this->bufferOffset + lineCount < 0)
368      lineCount = -bufferOffset;
369    this->bufferOffset += lineCount;
370
371    // moving the iterator to the right position
372    int move = 0;
373    while (move != lineCount)
374    {
375      if (move < lineCount)
376      {
377        ++move;
378        this->bufferIterator--;
379      }
380      else
381      {
382        --move;
383        this->bufferIterator++;
384      }
385    }
386    // redisplay the buffers
387    std::list<std::string>::const_iterator it = this->bufferIterator;
388    if (it == ShellBuffer::getInstance()->getBuffer().end())
389    {
390      /// FIXME
391      PRINTF(1)("Should not heappen\n");
392      it--;
393    }
394    for (std::list<Text*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
395    {
396      (*textIt)->setText((*it));
397      if (it == ShellBuffer::getInstance()->getBuffer().begin())
398      {
399        /// FIXME
400        PRINTF(1)("Should not heappen\n");
401        break;
402      }
403      it--;
404    }
405  }
406
407  /**
408   * clears the Shell (empties all buffers)
409   */
410  void Shell::clear()
411  {
412    this->flush();
413    ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
414  }
415
416  /**
417   * listens for some event
418   * @param event the Event happened
419   */
420  void Shell::process(const Event &event)
421  {
422    if (event.bPressed)
423    {
424      if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
425      {
426        if (this->bActive == false)
427          this->activate();
428        else
429          this->deactivate();
430      }
431      else if (event.type == SDLK_PAGEUP)
432      {
433        this->moveDisplayBuffer(+this->bufferDisplaySize-1);
434      }
435      else if (event.type == SDLK_PAGEDOWN)
436      {
437        this->moveDisplayBuffer(-this->bufferDisplaySize+1);
438      }
439    }
440  }
441
442  /**
443   * displays the Shell
444   */
445  void Shell::draw() const
446  {
447    // transform for alignment.
448    // setting the Blending effects
449
450    this->backgroundMaterial->select();
451
452    glBegin(GL_TRIANGLE_STRIP);
453
454    glTexCoord2f(0, 0);
455    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
456
457    glTexCoord2f(1, 0);
458    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
459
460    glTexCoord2f(0, 1);
461    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
462
463    glTexCoord2f(1, 1);
464    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
465
466    glEnd();
467  }
468
469  ///////////////////////
470  // HELPER FUNCTIONS  //
471  ///////////////////////
472
473  /**
474   * @brief calculates the position of a Buffer-Display Line
475   * @param lineNumber the lineNumber from the bottom to calculate the position from
476   * @returns the Position of the Line.
477   */
478  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
479  {
480    return Vector2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber - 2) + this->textSize);
481  }
482
483
484
485  /**
486   * @brief displays some nice output from the Shell
487   */
488  void Shell::debug() const
489  {
490    PRINT(3)("Debugging output to console (not this shell)\n");
491
492    //   if (this->pressedKey != SDLK_FIRST)
493    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
494
495
496    ShellBuffer::getInstance()->debug();
497  }
498
499  // void Shell::testI (int i)
500  // {
501  //   PRINTF(3)("This is the Test for one Int '%d'\n", i);
502  // }
503  //
504  // void Shell::testS (const char* s)
505  // {
506  //   PRINTF(3)("This is the Test for one String '%s'\n", s);
507  // }
508  //
509  // void Shell::testB (bool b)
510  // {
511  //   PRINTF(3)("This is the Test for one Bool: ");
512  //   if (b)
513  //     PRINTF(3)("true\n");
514  //   else
515  //     PRINTF(3)("false\n");
516  // }
517  //
518  // void Shell::testF (float f)
519  // {
520  //   PRINTF(3)("This is the Test for one Float '%f'\n", f);
521  // }
522  //
523  // void Shell::testSF (const char* s, float f)
524  // {
525  //   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
526  // }
527
528}
Note: See TracBrowser for help on using the repository browser.