Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: fancy-shell :)

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