Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: completing file-system entries

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 = 20;
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->shellInput.setFont(this->fontFile, this->textSize);
253    this->shellInput.setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
254    this->shellInput.setBlending(this->textColor[3]);
255    this->shellInput.setRelCoor2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
256    this->shellInput.setLayer(this->getLayer());
257    if (shellInput.getParent2D() != this)
258      this->shellInput.setParent2D(this);
259
260    unsigned int i = 0;
261    for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text, ++i)
262    {
263      (*text)->setFont(this->fontFile, this->textSize);
264      (*text)->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
265      (*text)->setBlending(this->textColor[3]);
266      (*text)->setRelCoor2D( calculateLinePosition(i) );
267      (*text)->setLayer(this->getLayer());
268      if ((*text)->getParent2D() != this)
269        (*text)->setParent2D(this);
270    }
271    this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
272  }
273
274
275  /**
276   * @brief sets The count of Lines to display in the buffer.
277   * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
278   */
279  void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
280  {
281    unsigned int oldSize = this->bufferText.size();
282    if (oldSize > bufferDisplaySize)
283    {
284      for (unsigned int i = bufferDisplaySize; i <= oldSize; i++)
285      {
286        delete this->bufferText.back();
287        this->bufferText.pop_back();
288      }
289    }
290    else if (oldSize < bufferDisplaySize)
291    {
292      for (unsigned int i = oldSize; i <= bufferDisplaySize; i++)
293      {
294        this->bufferText.push_back(new Text);
295      }
296    }
297    this->bufferDisplaySize = bufferDisplaySize;
298    this->resetValues();
299  }
300
301  /**
302   * @brief deletes all the Buffers
303   */
304  void Shell::flush()
305  {
306    for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
307    {
308      (*text)->setText("");  // remove all chars from the BufferTexts.
309    }
310    ShellBuffer::getInstance()->flush();
311    // BUFFER FLUSHING
312  }
313
314  /**
315   * @brief prints out some text to the input-buffers
316   * @param text the text to output.
317   */
318  void Shell::printToDisplayBuffer(const std::string& text)
319  {
320    this->bufferText.push_front(this->bufferText.back());
321    this->bufferText.pop_back();
322
323
324    unsigned int i = 0;
325    for (std::list<Text*>::iterator textIt = ++this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt, i++)
326    {
327      (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(i+1), 5);
328    }
329
330    this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(0)- Vector2D(-1000,0));
331    this->bufferText.front()->setRelCoorSoft2D(this->calculateLinePosition(0),10);
332
333    /*  FANCY EFFECTS :)
334      1:
335          lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1000,0,0));
336          lastText->setRelCoorSoft2D(this->calculateLinePosition(0),10);
337      2:
338      lastText->setRelDir2D(-90);
339      lastText->setRelDirSoft2D(0, 20);
340    */
341
342    this->bufferText.front()->setText(text);
343  }
344
345  /**
346   * moves the Display buffer (up + or down - )
347   * @param lineCount the count by which to shift the InputBuffer.
348   *
349   * @todo make this work
350   */
351  void Shell::moveDisplayBuffer(int lineCount)
352  {
353    if (this->bufferOffset == 0)
354    {
355      this->bufferIterator = ShellBuffer::getInstance()->getBuffer().end();
356      //     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
357      //       this->bufferIterator->prevStep();
358    }
359
360    // boundraries
361    if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer().size())
362      lineCount = (int)ShellBuffer::getInstance()->getBuffer().size()- this->bufferOffset;
363    else if (this->bufferOffset + lineCount < 0)
364      lineCount = -bufferOffset;
365    this->bufferOffset += lineCount;
366
367    // moving the iterator to the right position
368    int move = 0;
369    while (move != lineCount)
370    {
371      if (move < lineCount)
372      {
373        ++move;
374        this->bufferIterator--;
375      }
376      else
377      {
378        --move;
379        this->bufferIterator++;
380      }
381    }
382    // redisplay the buffers
383    std::list<std::string>::const_iterator it = this->bufferIterator;
384    if (it == ShellBuffer::getInstance()->getBuffer().end())
385    {
386      /// FIXME
387      PRINTF(1)("Should not heappen\n");
388      it--;
389    }
390    for (std::list<Text*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
391    {
392      (*textIt)->setText((*it));
393      if (it == ShellBuffer::getInstance()->getBuffer().begin())
394      {
395        /// FIXME
396        PRINTF(1)("Should not heappen\n");
397        break;
398      }
399      it--;
400    }
401  }
402
403  /**
404   * clears the Shell (empties all buffers)
405   */
406  void Shell::clear()
407  {
408    this->flush();
409    ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
410  }
411
412  /**
413   * listens for some event
414   * @param event the Event happened
415   */
416  void Shell::process(const Event &event)
417  {
418    if (event.bPressed)
419    {
420      if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
421      {
422        if (this->bActive == false)
423          this->activate();
424        else
425          this->deactivate();
426      }
427      else if (event.type == SDLK_PAGEUP)
428      {
429        this->moveDisplayBuffer(+this->bufferDisplaySize-1);
430      }
431      else if (event.type == SDLK_PAGEDOWN)
432      {
433        this->moveDisplayBuffer(-this->bufferDisplaySize+1);
434      }
435    }
436  }
437
438  /**
439   * displays the Shell
440   */
441  void Shell::draw() const
442  {
443    // transform for alignment.
444    // setting the Blending effects
445
446    this->backgroundMaterial->select();
447
448    glBegin(GL_TRIANGLE_STRIP);
449
450    glTexCoord2f(0, 0);
451    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
452
453    glTexCoord2f(1, 0);
454    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
455
456    glTexCoord2f(0, 1);
457    glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
458
459    glTexCoord2f(1, 1);
460    glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
461
462    glEnd();
463  }
464
465  ///////////////////////
466  // HELPER FUNCTIONS  //
467  ///////////////////////
468
469  /**
470   * @brief calculates the position of a Buffer-Display Line
471   * @param lineNumber the lineNumber from the bottom to calculate the position from
472   * @returns the Position of the Line.
473   */
474  Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
475  {
476    return Vector2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber - 2) + this->textSize);
477  }
478
479
480
481  /**
482   * @brief displays some nice output from the Shell
483   */
484  void Shell::debug() const
485  {
486    PRINT(3)("Debugging output to console (not this shell)\n");
487
488    //   if (this->pressedKey != SDLK_FIRST)
489    //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
490
491
492    ShellBuffer::getInstance()->debug();
493  }
494
495  // void Shell::testI (int i)
496  // {
497  //   PRINTF(3)("This is the Test for one Int '%d'\n", i);
498  // }
499  //
500  // void Shell::testS (const char* s)
501  // {
502  //   PRINTF(3)("This is the Test for one String '%s'\n", s);
503  // }
504  //
505  // void Shell::testB (bool b)
506  // {
507  //   PRINTF(3)("This is the Test for one Bool: ");
508  //   if (b)
509  //     PRINTF(3)("true\n");
510  //   else
511  //     PRINTF(3)("false\n");
512  // }
513  //
514  // void Shell::testF (float f)
515  // {
516  //   PRINTF(3)("This is the Test for one Float '%f'\n", f);
517  // }
518  //
519  // void Shell::testSF (const char* s, float f)
520  // {
521  //   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
522  // }
523
524}
Note: See TracBrowser for help on using the repository browser.