Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: fixed a ShellCommand-bug

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