Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellBuffer and ShellInput had the segfault-capability for years, now they have not anymore :)

File size: 13.5 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)
[7341]38->describe("Clears the shell from unwanted lines (empties all buffers)")
39->setAlias("clear");
[5201]40SHELL_COMMAND(deactivate, Shell, deactivate)
[7341]41->describe("Deactivates the Shell. (moves it into background)")
42->setAlias("hide");
[5208]43SHELL_COMMAND(textsize, Shell, setTextSize)
[7341]44->describe("Sets the size of the Text size, linespacing")
45->defaultValues(15, 0);
[5374]46SHELL_COMMAND(textcolor, Shell, setTextColor)
[7341]47->describe("Sets the Color of the Shells Text (red, green, blue, alpha)")
48->defaultValues(SHELL_DEFAULT_TEXT_COLOR);
[5374]49SHELL_COMMAND(backgroundcolor, Shell, setBackgroundColor)
[7341]50->describe("Sets the Color of the Shells Background (red, green, blue, alpha)")
51->defaultValues(SHELL_DEFAULT_BACKGROUND_COLOR);
[5374]52SHELL_COMMAND(backgroundimage, Shell, setBackgroundImage)
[7341]53->describe("sets the background image to load for the Shell");
[5254]54SHELL_COMMAND(font, Shell, setFont)
[7341]55->describe("Sets the font of the Shell")
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->bufferOffset = 0;
[7315]74  this->bufferIterator = ShellBuffer::getInstance()->getBuffer().begin();
[5372]75
76  // INPUT LINE
[7341]77  this->setLayer(E2D_LAYER_ABOVE_ALL);
78  this->shellInput.setLayer(E2D_LAYER_ABOVE_ALL);
[5372]79
80  this->backgroundMaterial = new Material;
[7341]81
[5183]82  // Element2D and generals
[5175]83  this->setAbsCoor2D(3, -400);
[5329]84  this->textSize = 20;
[5208]85  this->lineSpacing = 0;
[5420]86  this->bActive = true;
[7221]87  this->fontFile = SHELL_DEFAULT_FONT;
[5072]88
[7341]89  this->setBufferDisplaySize(10);
[5080]90
[5372]91  this->setTextColor(SHELL_DEFAULT_TEXT_COLOR);
92  this->setBackgroundColor(SHELL_DEFAULT_BACKGROUND_COLOR);
[5335]93
[7341]94
[5420]95  this->deactivate();
[5335]96  // register the shell at the ShellBuffer
97  ShellBuffer::getInstance()->registerShell(this);
[5068]98}
[4320]99
[3245]100/**
[7341]101 * @brief standard deconstructor
[5068]102 */
103Shell::~Shell ()
[3543]104{
[5335]105  ShellBuffer::getInstance()->unregisterShell(this);
106
[5099]107  // delete the displayable Buffers
[7341]108  while (!this->bufferText.empty())
109  {
110    delete this->bufferText.front();
111    this->bufferText.pop_front();
112  }
113
[5099]114  // delete the inputLine
[5372]115  delete this->backgroundMaterial;
[3543]116}
[5068]117
[5119]118/**
[7341]119 * @brief activates the shell
[5119]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;
[7341]136  for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
[5247]137  {
[7341]138    (*text)->setVisibility(true);
[5787]139    if (!top)
140    {
[7341]141      (*text)->setText((*textLine));
142      if (textLine != ShellBuffer::getInstance()->getBuffer().begin())
143        top = true;
[5787]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
[7341]163  for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
164    (*text)->setVisibility(false);
[5248]165  this->bufferOffset = 0;
[5113]166}
167
[5119]168/**
[7340]169 * @brief sets the File to load the fonts from
[5251]170 * @param fontFile the file to load the font from
[5254]171 *
172 * it is quite important, that the font pointed too really exists!
173 * (be aware that within orxonox fontFile is relative to the Data-Dir)
[5251]174 */
[7221]175void Shell::setFont(const std::string& fontFile)
[5251]176{
[7341]177  //   if (!ResourceManager::isInDataDir(fontFile))
178  //     return false;
[5335]179
[7221]180  this->fontFile = fontFile;
[5251]181
[7341]182  this->resetValues();
[5251]183}
184
185/**
[7340]186 * @brief sets the size of the text and spacing
[5119]187 * @param textSize the size of the Text in Pixels
188 * @param lineSpacing the size of the Spacing between two lines in pixels
189 *
190 * this also rebuilds the entire Text, inputLine and displayBuffer,
191 * to be accurate again.
192 */
[5113]193void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
194{
195  this->textSize = textSize;
196  this->lineSpacing = lineSpacing;
[7341]197
[5372]198  this->resetValues();
199}
[5113]200
[5372]201/**
[7341]202 * @brief sets the color of the Font.
[5372]203 * @param r: red
204 * @param g: green
205 * @param b: blue
206 * @param a: alpha-value.
207 */
208void Shell::setTextColor(float r, float g, float b, float a)
209{
210  this->textColor[0] = r;
211  this->textColor[1] = g;
212  this->textColor[2] = b;
213  this->textColor[3] = a;
214
215  this->resetValues();
216}
217
218
219/**
[7341]220 * @brief sets the color of the Backgrond.
[5372]221 * @param r: red
222 * @param g: green
223 * @param b: blue
224 * @param a: alpha-value.
225 */
226void Shell::setBackgroundColor(float r, float g, float b, float a)
227{
228  this->backgroundMaterial->setDiffuse(r, g, b);
229  this->backgroundMaterial->setTransparency(a);
230}
231
232/**
[7341]233 * @brief sets a nice background image to the Shell's background
[5374]234 * @param fileName the filename of the Image to load
235 */
[7221]236void Shell::setBackgroundImage(const std::string& fileName)
[5374]237{
238  this->backgroundMaterial->setDiffuseMap(fileName);
239}
240
241
242/**
[7341]243 * @brief resets the Values of all visible shell's commandos to the Shell's stored values
[5372]244 *
245 * this functions synchronizes the stored Data with the visible one.
246 */
247void Shell::resetValues()
248{
[7341]249  this->shellInput.setFont(this->fontFile, this->textSize);
250  this->shellInput.setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
251  this->shellInput.setBlending(this->textColor[3]);
252  this->shellInput.setRelCoor2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
253  this->shellInput.setLayer(this->getLayer());
254  if (shellInput.getParent2D() != this)
255    this->shellInput.setParent2D(this);
[5372]256
[7341]257  unsigned int i = 0;
258  for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text, ++i)
[5369]259  {
[7341]260    (*text)->setFont(this->fontFile, this->textSize);
261    (*text)->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
262    (*text)->setBlending(this->textColor[3]);
263    (*text)->setRelCoor2D( calculateLinePosition(i) );
264    (*text)->setLayer(this->getLayer());
265    if ((*text)->getParent2D() != this)
266      (*text)->setParent2D(this);
[5369]267  }
268  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5113]269}
270
271
[5074]272/**
[7341]273 * @brief sets The count of Lines to display in the buffer.
[5074]274 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
275 */
[5072]276void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
277{
[7341]278  unsigned int oldSize = this->bufferText.size();
279  if (oldSize > bufferDisplaySize)
[5072]280  {
[7341]281    for (unsigned int i = bufferDisplaySize; i <= oldSize; i++)
282    {
283      delete this->bufferText.back();
284      this->bufferText.pop_back();
285    }
[5072]286  }
[7341]287  else if (oldSize < bufferDisplaySize)
[5072]288  {
[7341]289    for (unsigned int i = oldSize; i <= bufferDisplaySize; i++)
[5787]290    {
[7341]291      this->bufferText.push_back(new Text);
[5787]292    }
[5072]293  }
[5113]294  this->bufferDisplaySize = bufferDisplaySize;
[7341]295  this->resetValues();
[5072]296}
[5068]297
298/**
[7341]299 * @brief deletes all the Buffers
[5068]300 */
[5175]301void Shell::flush()
[5068]302{
[7341]303  for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
304  {
305    (*text)->setText("");  // remove all chars from the BufferTexts.
306  }
307  ShellBuffer::getInstance()->flush();
308  // BUFFER FLUSHING
[5068]309}
310
311/**
[7341]312 * @brief prints out some text to the input-buffers
[5118]313 * @param text the text to output.
314 */
[7221]315void Shell::printToDisplayBuffer(const std::string& text)
[5118]316{
[7341]317  this->bufferText.push_front(this->bufferText.back());
318  this->bufferText.pop_back();
319
320
321  unsigned int i = 0;
322  for (std::list<Text*>::iterator textIt = ++this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt, i++)
[5118]323  {
[7341]324    (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(i+1), 5);
325  }
[5113]326
[7341]327  this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(0)- Vector2D(-1000,0));
328  this->bufferText.front()->setRelCoorSoft2D(this->calculateLinePosition(0),10);
[5375]329
330  /*  FANCY EFFECTS :)
[5376]331    1:
[5375]332        lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1000,0,0));
333        lastText->setRelCoorSoft2D(this->calculateLinePosition(0),10);
[5376]334    2:
[5383]335    lastText->setRelDir2D(-90);
336    lastText->setRelDirSoft2D(0, 20);
[7341]337  */
[5377]338
[7341]339  this->bufferText.front()->setText(text);
[5068]340}
341
342/**
[5783]343 * moves the Display buffer (up + or down - )
[5246]344 * @param lineCount the count by which to shift the InputBuffer.
[5783]345 *
346 * @todo make this work
[5246]347 */
348void Shell::moveDisplayBuffer(int lineCount)
349{
[5783]350  if (this->bufferOffset == 0)
[7341]351  {
352    this->bufferIterator = ShellBuffer::getInstance()->getBuffer().end();
353    //     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
354    //       this->bufferIterator->prevStep();
355  }
[5783]356
357  // boundraries
[7315]358  if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer().size())
359    lineCount = (int)ShellBuffer::getInstance()->getBuffer().size()- this->bufferOffset;
[5783]360  else if (this->bufferOffset + lineCount < 0)
361    lineCount = -bufferOffset;
362  this->bufferOffset += lineCount;
363
364  // moving the iterator to the right position
365  int move = 0;
366  while (move != lineCount)
367  {
368    if (move < lineCount)
369    {
370      ++move;
371      this->bufferIterator--;
372    }
373    else
374    {
375      --move;
376      this->bufferIterator++;
377    }
378  }
379  // redisplay the buffers
[7315]380  list<std::string>::const_iterator it = this->bufferIterator;
[7342]381  if (it == ShellBuffer::getInstance()->getBuffer().end())
382  {
383    /// FIXME
384    PRINTF(1)("Should not heappen\n");
385    it--;
386  }
[7341]387  for (std::list<Text*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
[5783]388  {
[7341]389    (*textIt)->setText((*it));
[7342]390    if (it == ShellBuffer::getInstance()->getBuffer().begin())
391    {
392      /// FIXME
393      PRINTF(1)("Should not heappen\n");
394      break;
395    }
[5783]396    it--;
397  }
[5246]398}
399
400/**
[5166]401 * clears the Shell (empties all buffers)
402 */
[5130]403void Shell::clear()
404{
[5175]405  this->flush();
406  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
[5130]407}
408
[5069]409/**
410 * listens for some event
411 * @param event the Event happened
412 */
413void Shell::process(const Event &event)
414{
[5093]415  if (event.bPressed)
416  {
[5425]417    if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
[5093]418    {
[5388]419      if (this->bActive == false)
[5113]420        this->activate();
[5093]421      else
[5113]422        this->deactivate();
[5093]423    }
[5246]424    else if (event.type == SDLK_PAGEUP)
425    {
[5248]426      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
[5246]427    }
428    else if (event.type == SDLK_PAGEDOWN)
429    {
[5248]430      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
[5246]431    }
[5093]432  }
[5069]433}
434
[5068]435/**
436 * displays the Shell
437 */
438void Shell::draw() const
439{
[5099]440  // transform for alignment.
441  // setting the Blending effects
442
[5372]443  this->backgroundMaterial->select();
[5099]444
[5158]445  glBegin(GL_TRIANGLE_STRIP);
[5099]446
[5158]447  glTexCoord2f(0, 0);
[5099]448  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
449
[5158]450  glTexCoord2f(1, 0);
[5113]451  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]452
[5158]453  glTexCoord2f(0, 1);
454  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
455
456  glTexCoord2f(1, 1);
[5113]457  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]458
459  glEnd();
[5068]460}
461
[5120]462///////////////////////
463// HELPER FUNCTIONS  //
464///////////////////////
[5166]465
466/**
[7316]467 * @brief calculates the position of a Buffer-Display Line
[5166]468 * @param lineNumber the lineNumber from the bottom to calculate the position from
469 * @returns the Position of the Line.
470 */
[7316]471Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
[5120]472{
[7316]473  return Vector2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber - 2) + this->textSize);
[5120]474}
475
476
477
[5113]478/**
[7316]479 * @brief displays some nice output from the Shell
[5068]480 */
481void Shell::debug() const
482{
[5119]483  PRINT(3)("Debugging output to console (not this shell)\n");
484
[7341]485  //   if (this->pressedKey != SDLK_FIRST)
486  //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]487
488
[5177]489  ShellBuffer::getInstance()->debug();
[5068]490}
[5166]491
492// void Shell::testI (int i)
493// {
494//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
495// }
496//
497// void Shell::testS (const char* s)
498// {
499//   PRINTF(3)("This is the Test for one String '%s'\n", s);
500// }
501//
502// void Shell::testB (bool b)
503// {
504//   PRINTF(3)("This is the Test for one Bool: ");
505//   if (b)
506//     PRINTF(3)("true\n");
507//   else
508//     PRINTF(3)("false\n");
509// }
510//
511// void Shell::testF (float f)
512// {
513//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
514// }
515//
516// void Shell::testSF (const char* s, float f)
517// {
518//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
519// }
Note: See TracBrowser for help on using the repository browser.