Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: shell in c++-style now

File size: 13.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)
[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  //   lastText->setRelCoor2D(this->calculateLinePosition(0));
[5118]340
[7341]341  this->bufferText.front()->setText(text);
[5068]342}
343
344/**
[5783]345 * moves the Display buffer (up + or down - )
[5246]346 * @param lineCount the count by which to shift the InputBuffer.
[5783]347 *
348 * @todo make this work
[5246]349 */
350void Shell::moveDisplayBuffer(int lineCount)
351{
[5783]352  if (this->bufferOffset == 0)
[7341]353  {
354    this->bufferIterator = ShellBuffer::getInstance()->getBuffer().end();
355    //     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
356    //       this->bufferIterator->prevStep();
357  }
[5783]358
359  // boundraries
[7315]360  if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer().size())
361    lineCount = (int)ShellBuffer::getInstance()->getBuffer().size()- this->bufferOffset;
[5783]362  else if (this->bufferOffset + lineCount < 0)
363    lineCount = -bufferOffset;
364  this->bufferOffset += lineCount;
365
366  // moving the iterator to the right position
367  int move = 0;
368  while (move != lineCount)
369  {
370    if (move < lineCount)
371    {
372      ++move;
373      this->bufferIterator--;
374    }
375    else
376    {
377      --move;
378      this->bufferIterator++;
379    }
380  }
381  // redisplay the buffers
[7315]382  list<std::string>::const_iterator it = this->bufferIterator;
[7341]383  for (std::list<Text*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
[5783]384  {
[7341]385    (*textIt)->setText((*it));
[5783]386    it--;
387  }
[5246]388}
389
390/**
[5166]391 * clears the Shell (empties all buffers)
392 */
[5130]393void Shell::clear()
394{
[5175]395  this->flush();
396  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
[5130]397}
398
[5069]399/**
400 * listens for some event
401 * @param event the Event happened
402 */
403void Shell::process(const Event &event)
404{
[5093]405  if (event.bPressed)
406  {
[5425]407    if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
[5093]408    {
[5388]409      if (this->bActive == false)
[5113]410        this->activate();
[5093]411      else
[5113]412        this->deactivate();
[5093]413    }
[5246]414    else if (event.type == SDLK_PAGEUP)
415    {
[5248]416      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
[5246]417    }
418    else if (event.type == SDLK_PAGEDOWN)
419    {
[5248]420      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
[5246]421    }
[5093]422  }
[5069]423}
424
[5068]425/**
426 * displays the Shell
427 */
428void Shell::draw() const
429{
[5099]430  // transform for alignment.
431  // setting the Blending effects
432
[5372]433  this->backgroundMaterial->select();
[5099]434
[5158]435  glBegin(GL_TRIANGLE_STRIP);
[5099]436
[5158]437  glTexCoord2f(0, 0);
[5099]438  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
439
[5158]440  glTexCoord2f(1, 0);
[5113]441  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]442
[5158]443  glTexCoord2f(0, 1);
444  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
445
446  glTexCoord2f(1, 1);
[5113]447  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]448
449  glEnd();
[5068]450}
451
[5120]452///////////////////////
453// HELPER FUNCTIONS  //
454///////////////////////
[5166]455
456/**
[7316]457 * @brief calculates the position of a Buffer-Display Line
[5166]458 * @param lineNumber the lineNumber from the bottom to calculate the position from
459 * @returns the Position of the Line.
460 */
[7316]461Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
[5120]462{
[7316]463  return Vector2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber - 2) + this->textSize);
[5120]464}
465
466
467
[5113]468/**
[7316]469 * @brief displays some nice output from the Shell
[5068]470 */
471void Shell::debug() const
472{
[5119]473  PRINT(3)("Debugging output to console (not this shell)\n");
474
[7341]475  //   if (this->pressedKey != SDLK_FIRST)
476  //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]477
478
[5177]479  ShellBuffer::getInstance()->debug();
[5068]480}
[5166]481
482// void Shell::testI (int i)
483// {
484//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
485// }
486//
487// void Shell::testS (const char* s)
488// {
489//   PRINTF(3)("This is the Test for one String '%s'\n", s);
490// }
491//
492// void Shell::testB (bool b)
493// {
494//   PRINTF(3)("This is the Test for one Bool: ");
495//   if (b)
496//     PRINTF(3)("true\n");
497//   else
498//     PRINTF(3)("false\n");
499// }
500//
501// void Shell::testF (float f)
502// {
503//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
504// }
505//
506// void Shell::testSF (const char* s, float f)
507// {
508//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
509// }
Note: See TracBrowser for help on using the repository browser.